]> www.infradead.org Git - mtd-utils.git/commitdiff
ubirmvol: learn to remove volume by name
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Wed, 3 Sep 2008 16:18:29 +0000 (19:18 +0300)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Wed, 3 Sep 2008 16:37:41 +0000 (19:37 +0300)
Add -N option to ubirmvol to support deleting volumes
by name.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
ubi-utils/new-utils/include/libubi.h
ubi-utils/new-utils/src/libubi.c
ubi-utils/new-utils/src/ubimkvol.c
ubi-utils/new-utils/src/ubirmvol.c

index 3010ed40843aa08b0c562bb5e5f48d9f2944fbb8..2eeae035f913fccc4e26507c375b39ee7c41b3be 100644 (file)
@@ -336,6 +336,19 @@ int ubi_get_vol_info(libubi_t desc, const char *node,
 int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id,
                      struct ubi_vol_info *info);
 
+/**
+ * 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
+ * @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.
+ */
+int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
+                        struct ubi_vol_info *info);
+
 /**
  * ubi_update_start - start UBI volume update.
  * @desc: UBI library descriptor
index 0fca82205b5c3d4949fc39fffba2027b6876d6d7..461a40224291d26a5e57ebf90d0574eb50a7525a 100644 (file)
@@ -1147,3 +1147,36 @@ int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info)
 
        return ubi_get_vol_info1(desc, dev_num, vol_id, info);
 }
+
+int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
+                        struct ubi_vol_info *info)
+{
+       int i, err, nlen = strlen(name);
+       struct ubi_dev_info dev_info;
+
+       if (nlen == 0) {
+               errmsg("bad \"name\" input parameter");
+               errno = EINVAL;
+               return -1;
+       }
+
+       err = ubi_get_dev_info1(desc, dev_num, &dev_info);
+       if (err)
+               return err;
+
+       for (i = dev_info.lowest_vol_id;
+            i <= dev_info.highest_vol_id; i++) {
+               err = ubi_get_vol_info1(desc, dev_num, i, info);
+               if (err == -1) {
+                       if (errno == ENOENT)
+                               continue;
+                       return -1;
+               }
+
+               if (nlen == strlen(info->name) && !strcmp(name, info->name))
+                       return 0;
+       }
+
+       errno = ENOENT;
+       return -1;
+}
index ad072c4a5cfd1d9df1c8813aee9df2c18fbede31..26ddda297b6acc4c33c62663e51c639daa6cb305 100644 (file)
@@ -43,7 +43,6 @@ struct args {
        int lebs;
        int alignment;
        const char *name;
-       int nlen;
        const char *node;
        int maxavs;
        /* For deprecated -d option handling */
@@ -182,7 +181,6 @@ static int parse_opt(int argc, char * const argv[])
 
                case 'N':
                        args.name = optarg;
-                       args.nlen = strlen(args.name);
                        break;
 
                case 'h':
index 10be9758f6fd2b1b348f6da4ddb72c3e5300fca5..a4cf1dfcf7209e111331e35e7a5e0bc1e2b8f592 100644 (file)
@@ -39,6 +39,7 @@
 struct args {
        int vol_id;
        const char *node;
+       const char *name;
        /* For deprecated -d option handling */
        int devn;
        char dev_name[256];
@@ -54,6 +55,7 @@ static const char *doc = PROGRAM_NAME " version " PROGRAM_VERSION
 
 static const char *optionsstr =
 "-n, --vol_id=<volume id>   volume ID to remove\n"
+"-N, --name=<volume name>   volume name to remove\n"
 "-h, -?, --help             print help message\n"
 "-V, --version              print program version\n\n"
 "The following is a compatibility option which is deprecated, do not use it\n"
@@ -62,12 +64,16 @@ static const char *optionsstr =
 "                           that the device node is \"/dev/ubi<devn>\"";
 
 static const char *usage =
-"Usage: " PROGRAM_NAME " <UBI device node file name> [-n <volume id>] [--vol_id=<volume id>] [-h] [--help]\n\n"
+"Usage: " PROGRAM_NAME " <UBI device node file name> [-n <volume id>] [--vol_id=<volume id>]\n\n"
+"         [-N <volume name>] [--name=<volume name>] [-h] [--help]\n\n"
 "Example: " PROGRAM_NAME "/dev/ubi0 -n 1 - remove UBI volume 1 from UBI device corresponding\n"
-"         to the node file /dev/ubi0.";
+"         to /dev/ubi0\n"
+"         " PROGRAM_NAME "/dev/ubi0 -N my_vol - remove UBI named \"my_vol\" from UBI device\n"
+"         corresponding to /dev/ubi0";
 
 static const struct option long_options[] = {
        { .name = "vol_id",  .has_arg = 1, .flag = NULL, .val = 'n' },
+       { .name = "name",    .has_arg = 1, .flag = NULL, .val = 'N' },
        { .name = "help",    .has_arg = 0, .flag = NULL, .val = 'h' },
        { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
        /* Deprecated -d option */
@@ -77,8 +83,13 @@ static const struct option long_options[] = {
 
 static int param_sanity_check(void)
 {
-       if (args.vol_id == -1) {
-               errmsg("volume ID is was not specified");
+       if (args.vol_id == -1 && !args.name) {
+               errmsg("please, specify either volume ID or volume name");
+               return -1;
+       }
+
+       if (args.vol_id != -1 && args.name) {
+               errmsg("please, specify either volume ID or volume name, not both");
                return -1;
        }
 
@@ -91,7 +102,7 @@ static int parse_opt(int argc, char * const argv[])
                int key;
                char *endp;
 
-               key = getopt_long(argc, argv, "n:h?Vd:", long_options, NULL);
+               key = getopt_long(argc, argv, "n:N:h?Vd:", long_options, NULL);
                if (key == -1)
                        break;
 
@@ -105,6 +116,10 @@ static int parse_opt(int argc, char * const argv[])
                        }
                        break;
 
+               case 'N':
+                       args.name = optarg;
+                       break;
+
                case 'h':
                case '?':
                        fprintf(stderr, "%s\n\n", doc);
@@ -150,7 +165,6 @@ static int parse_opt(int argc, char * const argv[])
                args.node = argv[optind];
        }
 
-
        if (param_sanity_check())
                return -1;
 
@@ -180,6 +194,27 @@ int main(int argc, char * const argv[])
                goto out_libubi;
        }
 
+       if (args.name) {
+               struct ubi_dev_info dev_info;
+               struct ubi_vol_info vol_info;
+
+               err = ubi_get_dev_info(libubi, args.node, &dev_info);
+               if (err) {
+                       sys_errmsg("cannot get information about UBI device \"%s\"",
+                                  args.node);
+                       goto out_libubi;
+               }
+
+               err = ubi_get_vol_info1_nm(libubi, dev_info.dev_num,
+                                          args.name, &vol_info);
+               if (err) {
+                       sys_errmsg("cannot find UBI volume \"%s\"", args.name);
+                       goto out_libubi;
+               }
+
+               args.vol_id = vol_info.vol_id;
+       }
+
        err = ubi_rmvol(libubi, args.node, args.vol_id);
        if (err) {
                sys_errmsg("cannot UBI remove volume");