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 <mfasheh@versity.com>
This commit is contained in:
Mark Fasheh
2017-11-28 14:47:50 -08:00
committed by Zach Brown
parent 0876fb31c6
commit 7df8b87128
9 changed files with 67 additions and 67 deletions
+1 -1
View File
@@ -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);
+4 -4
View File
@@ -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;
}
}
+5 -5
View File
@@ -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;
}
+3 -3
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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) {
+32 -32
View File
@@ -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;
}
+2 -2
View File
@@ -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);
+16 -16
View File
@@ -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;
}