From 00d99ae46cfef3a399e82d161f8bf705ec0c10d0 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Wed, 22 Apr 2009 17:24:07 +0300 Subject: libubi: improve errors handling Improve comments about what is returned if UBI device or node does not exist. Better check for ENODEV error code. Signed-off-by: Artem Bityutskiy --- ubi-utils/include/libubi.h | 22 +++++++++++++-------- ubi-utils/src/libubi.c | 47 ++++++++++++++++++++++++++++++++++++-------- ubi-utils/src/ubimkvol.c | 5 ++++- ubi-utils/src/ubinfo.c | 4 ++-- ubi-utils/src/ubirename.c | 5 ++++- ubi-utils/src/ubirmvol.c | 5 ++++- ubi-utils/src/ubiupdatevol.c | 5 ++++- 7 files changed, 71 insertions(+), 22 deletions(-) diff --git a/ubi-utils/include/libubi.h b/ubi-utils/include/libubi.h index bc3404c..7ff0934 100644 --- a/ubi-utils/include/libubi.h +++ b/ubi-utils/include/libubi.h @@ -295,8 +295,8 @@ int ubi_rsvol(libubi_t desc, const char *node, int vol_id, long long bytes); * * This function tests whether @node is a UBI device or volume node and returns * %1 if this is an UBI device node, %2 if this is a volume node, and %-1 if - * this is not an UBI node or if an error occurred (the latter is indicated by - * a non-zero errno). + * this is not an UBI device or volume node (errno is ENODEV in this case) or + * if an error occurred. */ int ubi_probe_node(libubi_t desc, const char *node); @@ -307,7 +307,8 @@ int ubi_probe_node(libubi_t desc, const char *node); * @info: pointer to the &struct ubi_dev_info object to fill * * This function fills the passed @info object with UBI device information and - * returns %0 in case of success and %-1 in case of failure. + * returns %0 in case of success and %-1 in case of failure. If the UBI device + * corresponding to @node does not exist, errno is set to @ENODEV. */ int ubi_get_dev_info(libubi_t desc, const char *node, struct ubi_dev_info *info); @@ -319,7 +320,8 @@ int ubi_get_dev_info(libubi_t desc, const char *node, * @info: pointer to the &struct ubi_dev_info object to fill * * This function is identical to 'ubi_get_dev_info()' except that it accepts UBI - * device number, not UBI character device. + * device number, not UBI character device. If the UBI device @dev_num does not + * exist, errno is set to @ENODEV. */ int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info); @@ -330,7 +332,8 @@ int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info); * @info: pointer to the &struct ubi_vol_info object to fill * * This function fills the passed @info object with UBI volume information and - * returns %0 in case of success and %-1 in case of failure. + * returns %0 in case of success and %-1 in case of failure. If the UBI volume + * corresponding to @node does not exist, errno is set to @ENODEV. */ int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info); @@ -343,7 +346,9 @@ int ubi_get_vol_info(libubi_t desc, const char *node, * @info: pointer to the &struct ubi_vol_info object to fill * * This function is identical to 'ubi_get_vol_info()' except that it accepts UBI - * volume ID, not UBI volume character device. + * volume ID, not UBI volume character device. If the UBI device @dev_num does + * not exist, or if the UBI volume @vol_id does not exist, errno is set to + * @ENODEV. */ int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id, struct ubi_vol_info *info); @@ -352,11 +357,12 @@ int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id, * ubi_get_vol_info1_nm - get UBI volume information by volume name. * @desc: UBI library descriptor * @dev_num: UBI device number - * @vol_id: ID of the UBI volume to fetch information about + * @name: name of the UBI volume to fetch information about * @info: pointer to the &struct ubi_vol_info object to fill * * This function is identical to 'ubi_get_vol_info()' except that it accepts UBI - * volume name, not UBI volume ID. + * volume name, not UBI volume ID. If the UBI device @dev_num does not exist, + * or if the UBI volume @name does not exist, errno is set to @ENODEV. */ int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name, struct ubi_vol_info *info); diff --git a/ubi-utils/src/libubi.c b/ubi-utils/src/libubi.c index d4b29b9..067f17e 100644 --- a/ubi-utils/src/libubi.c +++ b/ubi-utils/src/libubi.c @@ -787,17 +787,15 @@ int ubi_probe_node(libubi_t desc, const char *node) /* This is supposdely an UBI volume device node */ sprintf(file, lib->ubi_vol, i, minor - 1); fd = open(file, O_RDONLY); - if (fd == -1) { - sys_errmsg("cannot open \"%s\"", node); - return -1; - } + if (fd == -1) + goto out_not_ubi; return 2; out_not_ubi: errmsg("\"%s\" has major:minor %d:%d, but this does not correspond to " - "any UBI device or volume", node, major, minor); - errno = 0; + "any existing UBI device or volume", node, major, minor); + errno = ENODEV; return -1; } @@ -1002,6 +1000,22 @@ int ubi_leb_change_start(libubi_t desc, int fd, int lnum, int bytes, int dtype) return 0; } +/** + * dev_present - check whether an UBI device is present. + * @lib: libubi descriptor + * @dev_num: UBI device number to check + * + * This function returns %1 if UBI device is present and %0 if not. + */ +static int dev_present(struct libubi *lib, int dev_num) +{ + struct stat st; + char file[strlen(lib->ubi_dev) + 50]; + + sprintf(file, lib->ubi_dev, dev_num); + return !stat(file, &st); +} + int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info) { DIR *sysfs_ubi; @@ -1011,6 +1025,11 @@ int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info) memset(info, '\0', sizeof(struct ubi_dev_info)); info->dev_num = dev_num; + if (!dev_present(lib, dev_num)) { + errno = ENODEV; + return -1; + } + sysfs_ubi = opendir(lib->sysfs_ubi); if (!sysfs_ubi) return -1; @@ -1085,9 +1104,15 @@ out_close: int ubi_get_dev_info(libubi_t desc, const char *node, struct ubi_dev_info *info) { - int dev_num; + int err, dev_num; struct libubi *lib = (struct libubi *)desc; + err = ubi_probe_node(desc, node); + if (err != 1) { + errno = ENODEV; + return -1; + } + if (dev_node2num(lib, node, &dev_num)) return -1; @@ -1155,9 +1180,15 @@ int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id, int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info) { - int vol_id, dev_num; + int err, vol_id, dev_num; struct libubi *lib = (struct libubi *)desc; + err = ubi_probe_node(desc, node); + if (err != 2) { + errno = ENODEV; + return -1; + } + if (vol_node2nums(lib, node, &dev_num, &vol_id)) return -1; diff --git a/ubi-utils/src/ubimkvol.c b/ubi-utils/src/ubimkvol.c index dba7133..b5805a4 100644 --- a/ubi-utils/src/ubimkvol.c +++ b/ubi-utils/src/ubimkvol.c @@ -251,7 +251,10 @@ int main(int argc, char * const argv[]) args.node); goto out_libubi; } else if (err < 0) { - errmsg("\"%s\" is not an UBI device node", args.node); + if (errno == ENODEV) + errmsg("\"%s\" is not an UBI device node", args.node); + else + sys_errmsg("error while probing \"%s\"", args.node); goto out_libubi; } diff --git a/ubi-utils/src/ubinfo.c b/ubi-utils/src/ubinfo.c index 025ac62..036ed6c 100644 --- a/ubi-utils/src/ubinfo.c +++ b/ubi-utils/src/ubinfo.c @@ -142,8 +142,8 @@ static int translate_dev(libubi_t libubi, const char *node) err = ubi_probe_node(libubi, node); if (err == -1) { - if (errno) - return errmsg("unrecognized device node \"%s\"", node); + if (errno != ENODEV) + return sys_errmsg("error while probing \"%s\"", node); return errmsg("\"%s\" does not correspond to any UBI device or volume", node); } diff --git a/ubi-utils/src/ubirename.c b/ubi-utils/src/ubirename.c index 08b3cd5..00c53e4 100644 --- a/ubi-utils/src/ubirename.c +++ b/ubi-utils/src/ubirename.c @@ -105,7 +105,10 @@ int main(int argc, char * const argv[]) node); goto out_libubi; } else if (err < 0) { - errmsg("\"%s\" is not an UBI device node", node); + if (errno == ENODEV) + errmsg("\"%s\" is not an UBI device node", node); + else + sys_errmsg("error while probing \"%s\"", node); goto out_libubi; } diff --git a/ubi-utils/src/ubirmvol.c b/ubi-utils/src/ubirmvol.c index 17aece4..f7a3820 100644 --- a/ubi-utils/src/ubirmvol.c +++ b/ubi-utils/src/ubirmvol.c @@ -193,7 +193,10 @@ int main(int argc, char * const argv[]) args.node); goto out_libubi; } else if (err < 0) { - errmsg("\"%s\" is not an UBI device node", args.node); + if (errno == ENODEV) + errmsg("\"%s\" is not an UBI device node", args.node); + else + sys_errmsg("error while probing \"%s\"", args.node); goto out_libubi; } diff --git a/ubi-utils/src/ubiupdatevol.c b/ubi-utils/src/ubiupdatevol.c index b160461..df424d2 100644 --- a/ubi-utils/src/ubiupdatevol.c +++ b/ubi-utils/src/ubiupdatevol.c @@ -331,7 +331,10 @@ int main(int argc, char * const argv[]) args.node); goto out_libubi; } else if (err < 0) { - errmsg("\"%s\" is not an UBI volume node", args.node); + if (errno == ENODEV) + errmsg("\"%s\" is not an UBI volume node", args.node); + else + sys_errmsg("error while probing \"%s\"", args.node); goto out_libubi; } -- cgit v1.2.3