]> www.infradead.org Git - mtd-utils.git/commitdiff
ubi-utils: few mor fixes and cleanups
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Sun, 23 Dec 2007 20:56:49 +0000 (22:56 +0200)
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Sun, 23 Dec 2007 20:59:19 +0000 (22:59 +0200)
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
ubi-utils/src/common.h
ubi-utils/src/libubi.c
ubi-utils/src/ubimkvol.c
ubi-utils/src/ubinfo.c
ubi-utils/src/ubirmvol.c
ubi-utils/src/ubiupdate.c

index 0e33dd1c65aaf762445bda55df38d73e848ad040..06ae6236c1593e5eaebb5cf804b6835b55670ded 100644 (file)
@@ -23,9 +23,6 @@
 extern "C" {
 #endif
 
-/* Maximum device node name length */
-#define MAX_NODE_LEN 255
-
 /* Error messages */
 #define errmsg(fmt, ...) do {                                             \
         fprintf(stderr, PROGRAM_NAME " error: " fmt "\n", ##__VA_ARGS__); \
index 9aa78473384c723f4eec9e6c93568c340d147543..32d988ee45904aca77c464beb73018da7fdfa2fc 100644 (file)
@@ -691,7 +691,6 @@ int ubi_node_type(libubi_t desc, const char *node)
                return -1;
 
        if (!S_ISCHR(st.st_mode)) {
-               errmsg("\"%s\" is not a character device", node);
                errno = EINVAL;
                return -1;
        }
index b8bc03ac0910f6071e56f028970f9664cf65021f..38c737d2890a4816b322b64b98780c208a719f22 100644 (file)
@@ -45,7 +45,7 @@ struct args {
        int alignment;
        const char *name;
        int nlen;
-       char node[MAX_NODE_LEN + 2];
+       const char *node;
        int maxavs;
 };
 
@@ -57,6 +57,7 @@ static struct args myargs = {
        .vol_id = UBI_VOL_NUM_AUTO,
        .name = NULL,
        .nlen = 0,
+       .node = NULL,
        .maxavs = 0,
 };
 
@@ -99,7 +100,38 @@ static const struct option long_options[] = {
        { NULL, 0, NULL, 0},
 };
 
-static int parse_opt(int argc, char * const argv[], struct args *args)
+static int param_sanity_check(void)
+{
+       int len;
+
+       if (myargs.bytes == -1 && !myargs.maxavs && myargs.lebs == -1) {
+               errmsg("volume size was not specified (use -h for help)");
+               return -1;
+       }
+
+       if ((myargs.bytes != -1 && (myargs.maxavs || myargs.lebs != -1))  ||
+           (myargs.lebs != -1  && (myargs.maxavs || myargs.bytes != -1)) ||
+           (myargs.maxavs && (myargs.bytes != -1 || myargs.lebs != -1))) {
+               errmsg("size specified with more then one option");
+               return -1;
+       }
+
+       if (myargs.name == NULL) {
+               errmsg("volume name was not specified (use -h for help)");
+               return -1;
+       }
+
+       len = strlen(myargs.name);
+       if (len > UBI_MAX_VOLUME_NAME) {
+               errmsg("too long name (%d symbols), max is %d",
+                       len, UBI_MAX_VOLUME_NAME);
+               return -1;
+       }
+
+       return 0;
+}
+
+static int parse_opt(int argc, char * const argv[])
 {
        while (1) {
                int key;
@@ -112,9 +144,9 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                switch (key) {
                case 't':
                        if (!strcmp(optarg, "dynamic"))
-                               args->vol_type = UBI_DYNAMIC_VOLUME;
+                               myargs.vol_type = UBI_DYNAMIC_VOLUME;
                        else if (!strcmp(optarg, "static"))
-                               args->vol_type = UBI_STATIC_VOLUME;
+                               myargs.vol_type = UBI_STATIC_VOLUME;
                        else {
                                errmsg("bad volume type: \"%s\"", optarg);
                                return -1;
@@ -122,8 +154,8 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                        break;
 
                case 's':
-                       args->bytes = strtoull(optarg, &endp, 0);
-                       if (endp == optarg || args->bytes <= 0) {
+                       myargs.bytes = strtoull(optarg, &endp, 0);
+                       if (endp == optarg || myargs.bytes <= 0) {
                                errmsg("bad volume size: \"%s\"", optarg);
                                return -1;
                        }
@@ -135,37 +167,37 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                                               "should be 'KiB', 'MiB' or 'GiB'", endp);
                                        return -1;
                                }
-                               args->bytes *= mult;
+                               myargs.bytes *= mult;
                        }
                        break;
 
                case 'S':
-                       args->lebs = strtoull(optarg, &endp, 0);
-                       if (endp == optarg || args->lebs <= 0 || *endp != '\0') {
+                       myargs.lebs = strtoull(optarg, &endp, 0);
+                       if (endp == optarg || myargs.lebs <= 0 || *endp != '\0') {
                                errmsg("bad volume size: \"%s\"", optarg);
                                return -1;
                        }
                        break;
 
                case 'a':
-                       args->alignment = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args->alignment <= 0) {
+                       myargs.alignment = strtoul(optarg, &endp, 0);
+                       if (*endp != '\0' || endp == optarg || myargs.alignment <= 0) {
                                errmsg("bad volume alignment: \"%s\"", optarg);
                                return -1;
                        }
                        break;
 
                case 'n':
-                       args->vol_id = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args->vol_id < 0) {
+                       myargs.vol_id = strtoul(optarg, &endp, 0);
+                       if (*endp != '\0' || endp == optarg || myargs.vol_id < 0) {
                                errmsg("bad volume ID: " "\"%s\"", optarg);
                                return -1;
                        }
                        break;
 
                case 'N':
-                       args->name = optarg;
-                       args->nlen = strlen(args->name);
+                       myargs.name = optarg;
+                       myargs.nlen = strlen(myargs.name);
                        break;
 
                case 'h':
@@ -179,7 +211,7 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                        exit(0);
 
                case 'm':
-                       args->maxavs = 1;
+                       myargs.maxavs = 1;
                        break;
 
                case ':':
@@ -192,42 +224,18 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                }
        }
 
-       return 0;
-}
-
-static int param_sanity_check(struct args *args)
-{
-       int len;
-
-       if (strlen(args->node) > MAX_NODE_LEN) {
-               errmsg("too long device node name: \"%s\" (%d characters), max. is %d",
-                      args->node, strlen(args->node), MAX_NODE_LEN);
-               return -1;
-       }
-
-       if (args->bytes == -1 && !args->maxavs && args->lebs == -1) {
-               errmsg("volume size was not specified (use -h for help)");
+       if (optind == argc) {
+               errmsg("UBI device name was not specified (use -h for help)");
                return -1;
-       }
-
-       if ((args->bytes != -1 && (args->maxavs || args->lebs != -1)) ||
-           (args->lebs != -1 && (args->maxavs || args->bytes != -1)) ||
-           (args->maxavs && (args->bytes != -1 || args->lebs != -1))) {
-               errmsg("size specified with more then one option");
+       } else if (optind != argc - 1) {
+               errmsg("more then one UBI devices specified (use -h for help)");
                return -1;
        }
 
-       if (args->name == NULL) {
-               errmsg("volume name was not specified (use -h for help)");
-               return -1;
-       }
+       myargs.node = argv[optind];
 
-       len = strlen(args->name);
-       if (len > UBI_MAX_VOLUME_NAME) {
-               errmsg("too long name (%d symbols), max is %d",
-                       len, UBI_MAX_VOLUME_NAME);
+       if (param_sanity_check())
                return -1;
-       }
 
        return 0;
 }
@@ -240,29 +248,7 @@ int main(int argc, char * const argv[])
        struct ubi_vol_info vol_info;
        struct ubi_mkvol_request req;
 
-       if (argc < 2) {
-               errmsg("UBI device name was not specified (use -h for help)");
-               return -1;
-       }
-
-       if (argc < 3) {
-               errmsg("too few arguments (use -h for help)");
-               return -1;
-       }
-
-       if (argv[1][0] == '-') {
-               errmsg("UBI device was not specified (use -h for help)");
-               return -1;
-       }
-
-       if (argv[2][0] != '-') {
-               errmsg("incorrect arguments, use -h for help");
-               return -1;
-       }
-
-       strncpy(myargs.node, argv[1], MAX_NODE_LEN + 1);
-
-       err = parse_opt(argc, argv, &myargs);
+       err = parse_opt(argc, argv);
        if (err)
                return err;
 
@@ -273,9 +259,15 @@ int main(int argc, char * const argv[])
                return -1;
        }
 
-       err = param_sanity_check(&myargs);
-       if (err)
+       err = ubi_node_type(libubi, myargs.node);
+       if (err == 2) {
+               errmsg("\"%s\" is an UBI volume node, not an UBI device node",
+                      myargs.node);
+               goto out_libubi;
+       } else if (err < 0) {
+               errmsg("\"%s\" is not an UBI device node", myargs.node);
                goto out_libubi;
+       }
 
        err = ubi_get_dev_info(libubi, myargs.node, &dev_info);
        if (err) {
index 280c770418c7f321a920f7b5f476084ecbfc99d5..c907335f10a84e7c042bf5fd34d3acf187aa2791 100644 (file)
@@ -39,15 +39,14 @@ struct args {
        int devn;
        int vol_id;
        int all;
-       char node[MAX_NODE_LEN + 2];
-       int node_given;
+       const char *node;
 };
 
 static struct args myargs = {
        .vol_id = -1,
        .devn = -1,
        .all = 0,
-       .node_given = 0,
+       .node = NULL,
 };
 
 static const char *doc = "Version " PROGRAM_VERSION "\n"
@@ -83,7 +82,7 @@ static const struct option long_options[] = {
        { NULL, 0, NULL, 0},
 };
 
-static int parse_opt(int argc, char * const argv[], struct args *args)
+static int parse_opt(int argc, char * const argv[])
 {
        while (1) {
                int key;
@@ -95,20 +94,20 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
 
                switch (key) {
                case 'a':
-                       args->all = 1;
+                       myargs.all = 1;
                        break;
 
                case 'n':
-                       args->vol_id = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args->vol_id < 0) {
+                       myargs.vol_id = strtoul(optarg, &endp, 0);
+                       if (*endp != '\0' || endp == optarg || myargs.vol_id < 0) {
                                errmsg("bad volume ID: " "\"%s\"", optarg);
                                return -1;
                        }
                        break;
 
                case 'd':
-                       args->devn = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args->devn < 0) {
+                       myargs.devn = strtoul(optarg, &endp, 0);
+                       if (*endp != '\0' || endp == optarg || myargs.devn < 0) {
                                errmsg("bad UBI device number: \"%s\"", optarg);
                                return -1;
                        }
@@ -135,6 +134,13 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                }
        }
 
+       if (optind == argc - 1) {
+               myargs.node = argv[optind];
+       } else if (optind < argc) {
+               errmsg("more then one UBI devices specified (use -h for help)");
+               return -1;
+       }
+
        return 0;
 }
 
@@ -142,17 +148,10 @@ static int translate_dev(libubi_t libubi, const char *node)
 {
        int err;
 
-       if (strlen(node) > MAX_NODE_LEN) {
-               errmsg("too long device node name: \"%s\" (%d characters), max. is %d",
-                      node, strlen(node), MAX_NODE_LEN);
-               return -1;
-       }
-
        err = ubi_node_type(libubi, node);
        if (err == -1) {
                if (errno) {
                        errmsg("unrecognized device node \"%s\"", node);
-                       perror("ubi_node_type");
                        return -1;
                }
                errmsg("\"%s\" does not correspond to any UBI device or volume",
@@ -399,21 +398,11 @@ int main(int argc, char * const argv[])
        int err;
        libubi_t libubi;
 
-       if (argc > 1 && argv[1][0] != '-') {
-               strncpy(myargs.node, argv[1], MAX_NODE_LEN + 1);
-               myargs.node_given = 1;
-       }
-
-       if (argc > 2 && argv[2][0] != '-') {
-               errmsg("incorrect arguments, use -h for help");
-               return -1;
-       }
-
-       err = parse_opt(argc, argv, &myargs);
+       err = parse_opt(argc, argv);
        if (err)
                return -1;
 
-       if (argc > 1 && !myargs.node_given && myargs.devn != -1) {
+       if (!myargs.node && myargs.devn != -1) {
                errmsg("specify either device number or node file (use -h for help)");
                return -1;
        }
@@ -425,7 +414,7 @@ int main(int argc, char * const argv[])
                return -1;
        }
 
-       if (myargs.node_given) {
+       if (myargs.node) {
                /*
                 * A character device was specified, translate this into UBI
                 * device number and volume ID.
index 10ce617de54313e8feafac96c8b7e5250b2ead9b..5f3552505acd6f8605c7ee38273182b06803a250 100644 (file)
 /* The variables below is set by command line arguments */
 struct args {
        int vol_id;
-       char node[MAX_NODE_LEN + 2];
+       const char *node;
 };
 
 static struct args myargs = {
        .vol_id = -1,
+       .node = NULL,
 };
 
 static const char *doc = "Version: " PROGRAM_VERSION "\n"
@@ -66,7 +67,17 @@ static const struct option long_options[] = {
        { NULL, 0, NULL, 0},
 };
 
-static int parse_opt(int argc, char * const argv[], struct args *args)
+static int param_sanity_check(void)
+{
+       if (myargs.vol_id == -1) {
+               errmsg("volume ID is was not specified");
+               return -1;
+       }
+
+       return 0;
+}
+
+static int parse_opt(int argc, char * const argv[])
 {
        while (1) {
                int key;
@@ -79,8 +90,8 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                switch (key) {
 
                case 'n':
-                       args->vol_id = strtoul(optarg, &endp, 0);
-                       if (*endp != '\0' || endp == optarg || args->vol_id < 0) {
+                       myargs.vol_id = strtoul(optarg, &endp, 0);
+                       if (*endp != '\0' || endp == optarg || myargs.vol_id < 0) {
                                errmsg("bad volume ID: " "\"%s\"", optarg);
                                return -1;
                        }
@@ -98,7 +109,7 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
 
                case ':':
                        errmsg("parameter is missing");
-                       goto out;
+                       return -1;
 
                default:
                        fprintf(stderr, "Use -h for help\n");
@@ -106,23 +117,18 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                }
        }
 
-       return 0;
- out:
-       return -1;
-}
-
-static int param_sanity_check(struct args *args)
-{
-       if (strlen(args->node) > MAX_NODE_LEN) {
-               errmsg("too long device node name: \"%s\" (%d characters), max. is %d",
-                      args->node, strlen(args->node), MAX_NODE_LEN);
+       if (optind == argc) {
+               errmsg("UBI device name was not specified (use -h for help)");
+               return -1;
+       } else if (optind != argc - 1) {
+               errmsg("more then one UBI devices specified (use -h for help)");
                return -1;
        }
 
-       if (args->vol_id == -1) {
-               errmsg("volume ID is was not specified");
+       myargs.node = argv[optind];
+
+       if (param_sanity_check())
                return -1;
-       }
 
        return 0;
 }
@@ -132,29 +138,7 @@ int main(int argc, char * const argv[])
        int err;
        libubi_t libubi;
 
-       strncpy(myargs.node, argv[1], MAX_NODE_LEN + 1);
-
-       if (argc < 2) {
-               errmsg("UBI device name was not specified (use -h for help)");
-               return -1;
-       }
-
-       if (argc < 3) {
-               errmsg("too few arguments (use -h for help)");
-               return -1;
-       }
-
-       if (argv[1][0] == '-') {
-               errmsg("UBI device was not specified (use -h for help)");
-               return -1;
-       }
-
-       if (argv[2][0] != '-') {
-               errmsg("incorrect arguments, use -h for help");
-               return -1;
-       }
-
-       err = parse_opt(argc, argv, &myargs);
+       err = parse_opt(argc, argv);
        if (err)
                return -1;
 
@@ -165,9 +149,15 @@ int main(int argc, char * const argv[])
                return -1;
        }
 
-       err = param_sanity_check(&myargs);
-       if (err)
+       err = ubi_node_type(libubi, myargs.node);
+       if (err == 2) {
+               errmsg("\"%s\" is an UBI volume node, not an UBI device node",
+                      myargs.node);
                goto out_libubi;
+       } else if (err < 0) {
+               errmsg("\"%s\" is not an UBI device node", myargs.node);
+               goto out_libubi;
+       }
 
        err = ubi_rmvol(libubi, myargs.node, myargs.vol_id);
        if (err) {
index 19caf5ae02c1ab27feae2632de866d91038ef319..1b9188e3ebed777dff1ca100e0371a87d228457f 100644 (file)
@@ -49,6 +49,7 @@ struct args {
 
 static struct args myargs = {
        .truncate = 0,
+       .node = NULL,
        .img = NULL,
 };
 
@@ -74,7 +75,7 @@ struct option long_options[] = {
        { NULL, 0, NULL, 0}
 };
 
-static int parse_opt(int argc, char * const argv[], struct args *args)
+static int parse_opt(int argc, char * const argv[])
 {
        while (1) {
                int key;
@@ -85,7 +86,7 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
 
                switch (key) {
                case 't':
-                       args->truncate = 1;
+                       myargs.truncate = 1;
                        break;
 
                case 'h':
@@ -108,6 +109,18 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
                }
        }
 
+       if (optind == argc) {
+               errmsg("UBI device name was not specified (use -h for help)");
+               return -1;
+       } else if (optind != argc - 2) {
+               errmsg("specify UBI device name and image file name as first 2 "
+                      "parameters (use -h for help)");
+               return -1;
+       }
+
+       myargs.node = argv[optind];
+       myargs.img  = argv[optind + 1];
+
        return 0;
 }
 
@@ -256,23 +269,13 @@ int main(int argc, char * const argv[])
        libubi_t libubi;
        struct ubi_vol_info vol_info;
 
-       if (argc < 2 || argv[1][0] == '-') {
-               errmsg("UBI device name was not specified (use -h for help)");
-               return -1;
-       }
-
-       if (argc < 3) {
-               errmsg("too few arguments (use -h for help)");
+       err = parse_opt(argc, argv);
+       if (err)
                return -1;
-       }
-
-       myargs.img = argv[argc - 1];
-       myargs.node = argv[1];
-
-       parse_opt(argc, argv, &myargs);
 
        if (!myargs.img && !myargs.truncate) {
                errmsg("incorrect arguments, use -h for help");
+               return -1;
        }
 
        libubi = libubi_open();
@@ -281,6 +284,16 @@ int main(int argc, char * const argv[])
                goto out_libubi;
        }
 
+       err = ubi_node_type(libubi, myargs.node);
+       if (err == 1) {
+               errmsg("\"%s\" is an UBI device node, not an UBI volume node",
+                      myargs.node);
+               goto out_libubi;
+       } else if (err < 0) {
+               errmsg("\"%s\" is not an UBI volume node", myargs.node);
+               goto out_libubi;
+       }
+
        err = ubi_get_vol_info(libubi, myargs.node, &vol_info);
        if (err) {
                errmsg("cannot get information about UBI volume \"%s\"",