]> www.infradead.org Git - mtd-utils.git/commitdiff
libubi: improve errors handling
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Wed, 22 Apr 2009 14:24:07 +0000 (17:24 +0300)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Wed, 22 Apr 2009 14:27:57 +0000 (17:27 +0300)
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 <Artem.Bityutskiy@nokia.com>
ubi-utils/include/libubi.h
ubi-utils/src/libubi.c
ubi-utils/src/ubimkvol.c
ubi-utils/src/ubinfo.c
ubi-utils/src/ubirename.c
ubi-utils/src/ubirmvol.c
ubi-utils/src/ubiupdatevol.c

index bc3404cbbbaba1986f65c853bcb5d9b3f8ab98b5..7ff0934bc6a15e7232a6324a7dec220b82b60d2e 100644 (file)
@@ -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);
index d4b29b995e1801c7827f9a4c16df9f41e14bdec1..067f17e9a0501a3daab1bdd922d5e2342e53473b 100644 (file)
@@ -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;
 
index dba713331cc25141049535cf4df6b16ec3e1a012..b5805a42d80a5e25adf47c17cb829d5c0af3ec92 100644 (file)
@@ -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;
        }
 
index 025ac62ab53f592092a946ced99b8378ff09478f..036ed6c20fc5c4215c77a46c0f39b8d8c4c21ff8 100644 (file)
@@ -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);
        }
 
index 08b3cd5b7c7a42597759493ec60603a10d4c2995..00c53e4fd95b498ea0213bcbbf6864cc9f8153bf 100644 (file)
@@ -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;
        }
 
index 17aece4e6771b3e6ddba94e9e7f916b87839d9f2..f7a38205257bac0b1f7a873beb6b7a8c2513819a 100644 (file)
@@ -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;
        }
 
index b16046112f17e91b5f747f3ce179fe1ac26c469a..df424d202bab18cc916e371bb8156f6923e48ead 100644 (file)
@@ -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;
        }