From 7df8b87128ac10602fa966582b17079f21b85f13 Mon Sep 17 00:00:00 2001 From: Mark Fasheh Date: Fri, 17 Nov 2017 22:24:28 -0600 Subject: [PATCH] scoutfs-utils: cmd_register - pass a parsing friendly argv We were chopping off the command string when passing the argument array into registered commands. getopt expects a program name as the first argument, so change cmd_execute() to only chop off the scoutfs program name now. Now we can parse command arguments in an easy and standard manner. This necessitates a small update of each commands usage of argv/argc. Signed-off-by: Mark Fasheh --- utils/src/cmd.c | 2 +- utils/src/ctrstat.c | 8 ++--- utils/src/ino_path.c | 10 +++--- utils/src/item-cache-keys.c | 6 ++-- utils/src/mkfs.c | 4 +-- utils/src/print.c | 4 +-- utils/src/stage_release.c | 64 ++++++++++++++++++------------------- utils/src/stat.c | 4 +-- utils/src/walk_inodes.c | 32 +++++++++---------- 9 files changed, 67 insertions(+), 67 deletions(-) diff --git a/utils/src/cmd.c b/utils/src/cmd.c index e723f859..92e799e5 100644 --- a/utils/src/cmd.c +++ b/utils/src/cmd.c @@ -76,7 +76,7 @@ int cmd_execute(int argc, char **argv) return 1; } - ret = com->func(argc - 2, argv + 2); + ret = com->func(argc - 1, argv + 1); if (ret < 0) { fprintf(stderr, "scoutfs: %s failed: %s (%d)\n", com->name, strerror(-ret), -ret); diff --git a/utils/src/ctrstat.c b/utils/src/ctrstat.c index cfad0740..1e30f090 100644 --- a/utils/src/ctrstat.c +++ b/utils/src/ctrstat.c @@ -129,16 +129,16 @@ static int ctrstat_cmd(int argc, char **argv) int iter; int ret; - if (argc > 1) { + if (argc > 2) { printf("scoutfs ctrstat: too many arguments\n"); return -EINVAL; } /* set the sleep duration */ - if (argc == 1) { - seconds = strtof(argv[0], NULL); + if (argc == 2) { + seconds = strtof(argv[1], NULL); if (fpclassify(seconds) != FP_NORMAL || seconds <= 0) { - printf("invalid sleep duration float: %s\n", argv[0]); + printf("invalid sleep duration float: %s\n", argv[1]); return -EINVAL; } } diff --git a/utils/src/ino_path.c b/utils/src/ino_path.c index 36fe0535..f23f3c7c 100644 --- a/utils/src/ino_path.c +++ b/utils/src/ino_path.c @@ -25,24 +25,24 @@ static int ino_path_cmd(int argc, char **argv) int ret; int fd; - if (argc != 2) { + if (argc != 3) { fprintf(stderr, "must specify ino and path\n"); return -EINVAL; } - ino = strtoull(argv[0], &endptr, 0); + ino = strtoull(argv[1], &endptr, 0); if (*endptr != '\0' || ((ino == LLONG_MIN || ino == LLONG_MAX) && errno == ERANGE)) { fprintf(stderr, "error parsing inode number '%s'\n", - argv[0]); + argv[1]); return -EINVAL; } - fd = open(argv[1], O_RDONLY); + fd = open(argv[2], O_RDONLY); if (fd < 0) { ret = -errno; fprintf(stderr, "failed to open '%s': %s (%d)\n", - argv[1], strerror(errno), errno); + argv[2], strerror(errno), errno); return ret; } diff --git a/utils/src/item-cache-keys.c b/utils/src/item-cache-keys.c index 765f77d8..704a8cc4 100644 --- a/utils/src/item-cache-keys.c +++ b/utils/src/item-cache-keys.c @@ -28,7 +28,7 @@ static int item_cache_keys(int argc, char **argv, int which) int ret; int fd; - if (argc != 1) { + if (argc != 2) { fprintf(stderr, "too many arguments, only scoutfs path needed"); return -EINVAL; } @@ -41,11 +41,11 @@ static int item_cache_keys(int argc, char **argv, int which) return ret; } - fd = open(argv[0], O_RDONLY); + fd = open(argv[1], O_RDONLY); if (fd < 0) { ret = -errno; fprintf(stderr, "failed to open '%s': %s (%d)\n", - argv[0], strerror(errno), errno); + argv[1], strerror(errno), errno); free(buf); return ret; } diff --git a/utils/src/mkfs.c b/utils/src/mkfs.c index add474f7..86543c10 100644 --- a/utils/src/mkfs.c +++ b/utils/src/mkfs.c @@ -398,11 +398,11 @@ out: static int mkfs_func(int argc, char *argv[]) { - char *path = argv[0]; + char *path = argv[1]; int ret; int fd; - if (argc != 1) { + if (argc != 2) { printf("scoutfs: mkfs: a single path argument is required\n"); return -EINVAL; } diff --git a/utils/src/print.c b/utils/src/print.c index 3020ed79..3007c063 100644 --- a/utils/src/print.c +++ b/utils/src/print.c @@ -643,11 +643,11 @@ static int print_cmd(int argc, char **argv) int ret; int fd; - if (argc != 1) { + if (argc != 2) { printf("scoutfs print: a single path argument is required\n"); return -EINVAL; } - path = argv[0]; + path = argv[1]; fd = open(path, O_RDONLY); if (fd < 0) { diff --git a/utils/src/stage_release.c b/utils/src/stage_release.c index 1efbaa7d..a7ee719d 100644 --- a/utils/src/stage_release.c +++ b/utils/src/stage_release.c @@ -26,42 +26,42 @@ static int stage_cmd(int argc, char **argv) u64 vers; int ret; - if (argc != 5) { + if (argc != 6) { fprintf(stderr, "must specify moar args\n"); return -EINVAL; } - fd = open(argv[0], O_RDWR); + fd = open(argv[1], O_RDWR); if (fd < 0) { ret = -errno; fprintf(stderr, "failed to open '%s': %s (%d)\n", - argv[0], strerror(errno), errno); + argv[1], strerror(errno), errno); return ret; } - vers = strtoull(argv[1], &endptr, 0); + vers = strtoull(argv[2], &endptr, 0); if (*endptr != '\0' || ((vers == LLONG_MIN || vers == LLONG_MAX) && errno == ERANGE)) { fprintf(stderr, "error parsing data version '%s'\n", - argv[1]); - ret = -EINVAL; - goto out; - } - - offset = strtoull(argv[2], &endptr, 0); - if (*endptr != '\0' || - ((offset == LLONG_MIN || offset == LLONG_MAX) && errno == ERANGE)) { - fprintf(stderr, "error parsing offset '%s'\n", argv[2]); ret = -EINVAL; goto out; } - count = strtoull(argv[3], &endptr, 0); + offset = strtoull(argv[3], &endptr, 0); + if (*endptr != '\0' || + ((offset == LLONG_MIN || offset == LLONG_MAX) && errno == ERANGE)) { + fprintf(stderr, "error parsing offset '%s'\n", + argv[3]); + ret = -EINVAL; + goto out; + } + + count = strtoull(argv[4], &endptr, 0); if (*endptr != '\0' || ((count == LLONG_MIN || count == LLONG_MAX) && errno == ERANGE)) { fprintf(stderr, "error parsing count '%s'\n", - argv[3]); + argv[4]); ret = -EINVAL; goto out; } @@ -73,11 +73,11 @@ static int stage_cmd(int argc, char **argv) goto out; } - afd = open(argv[4], O_RDONLY); + afd = open(argv[5], O_RDONLY); if (afd < 0) { ret = -errno; fprintf(stderr, "failed to open '%s': %s (%d)\n", - argv[4], strerror(errno), errno); + argv[5], strerror(errno), errno); goto out; } @@ -133,42 +133,42 @@ static int release_cmd(int argc, char **argv) int ret; int fd; - if (argc != 4) { + if (argc != 5) { fprintf(stderr, "must specify path, data version, offset, and count\n"); return -EINVAL; } - fd = open(argv[0], O_RDWR); + fd = open(argv[1], O_RDWR); if (fd < 0) { ret = -errno; fprintf(stderr, "failed to open '%s': %s (%d)\n", - argv[0], strerror(errno), errno); + argv[1], strerror(errno), errno); return ret; } - vers = strtoull(argv[1], &endptr, 0); + vers = strtoull(argv[2], &endptr, 0); if (*endptr != '\0' || ((vers == LLONG_MIN || vers == LLONG_MAX) && errno == ERANGE)) { fprintf(stderr, "error parsing data version '%s'\n", - argv[1]); - ret = -EINVAL; - goto out; - } - - block = strtoull(argv[2], &endptr, 0); - if (*endptr != '\0' || - ((block == LLONG_MIN || block == LLONG_MAX) && errno == ERANGE)) { - fprintf(stderr, "error parsing starting 4K block offset '%s'\n", argv[2]); ret = -EINVAL; goto out; } - count = strtoull(argv[3], &endptr, 0); + block = strtoull(argv[3], &endptr, 0); + if (*endptr != '\0' || + ((block == LLONG_MIN || block == LLONG_MAX) && errno == ERANGE)) { + fprintf(stderr, "error parsing starting 4K block offset '%s'\n", + argv[3]); + ret = -EINVAL; + goto out; + } + + count = strtoull(argv[4], &endptr, 0); if (*endptr != '\0' || ((count == LLONG_MIN || count == LLONG_MAX) && errno == ERANGE)) { fprintf(stderr, "error parsing length '%s'\n", - argv[3]); + argv[4]); ret = -EINVAL; goto out; } diff --git a/utils/src/stat.c b/utils/src/stat.c index b584441b..f9f4f569 100644 --- a/utils/src/stat.c +++ b/utils/src/stat.c @@ -22,12 +22,12 @@ static int stat_more_cmd(int argc, char **argv) int fd; int i; - if (argc == 0) { + if (argc == 1) { fprintf(stderr, "must specify at least one path argument\n"); return -EINVAL; } - for (i = 0; i < argc; i++) { + for (i = 1; i < argc; i++) { path = argv[i]; fd = open(path, O_RDONLY); diff --git a/utils/src/walk_inodes.c b/utils/src/walk_inodes.c index 91378ef3..c6b0bc99 100644 --- a/utils/src/walk_inodes.c +++ b/utils/src/walk_inodes.c @@ -75,44 +75,44 @@ static int walk_inodes_cmd(int argc, char **argv) int fd; int i; - if (argc != 4) { + if (argc != 5) { fprintf(stderr, "must specify seq and path\n"); return -EINVAL; } - if (!strcasecmp(argv[0], "size")) + if (!strcasecmp(argv[1], "size")) walk.index = SCOUTFS_IOC_WALK_INODES_SIZE; - else if (!strcasecmp(argv[0], "meta_seq")) + else if (!strcasecmp(argv[1], "meta_seq")) walk.index = SCOUTFS_IOC_WALK_INODES_META_SEQ; - else if (!strcasecmp(argv[0], "data_seq")) + else if (!strcasecmp(argv[1], "data_seq")) walk.index = SCOUTFS_IOC_WALK_INODES_DATA_SEQ; else { fprintf(stderr, "unknown index '%s', try 'size', 'ctime, or " - "mtime'\n", argv[0]); + "mtime'\n", argv[1]); return -EINVAL; } - ret = parse_walk_entry(&walk.first, argv[1]); + ret = parse_walk_entry(&walk.first, argv[2]); if (ret) { fprintf(stderr, "invalid first position '%s', try '1.2.3' or " - "'-1'\n", argv[1]); - return -EINVAL; - - } - - ret = parse_walk_entry(&walk.last, argv[2]); - if (ret) { - fprintf(stderr, "invalid last position '%s', try '1.2.3' or " "'-1'\n", argv[2]); return -EINVAL; } - fd = open(argv[3], O_RDONLY); + ret = parse_walk_entry(&walk.last, argv[3]); + if (ret) { + fprintf(stderr, "invalid last position '%s', try '1.2.3' or " + "'-1'\n", argv[3]); + return -EINVAL; + + } + + fd = open(argv[4], O_RDONLY); if (fd < 0) { ret = -errno; fprintf(stderr, "failed to open '%s': %s (%d)\n", - argv[3], strerror(errno), errno); + argv[4], strerror(errno), errno); return ret; }