summaryrefslogtreecommitdiff
path: root/cmds-device.c
diff options
context:
space:
mode:
authorGui Hecheng <guihc.fnst@cn.fujitsu.com>2014-03-06 11:36:48 +0800
committerChris Mason <clm@fb.com>2014-03-21 06:23:32 -0700
commit0e1b00c666cae6c89ebff2f6746dd91a64e85f12 (patch)
tree43c93bbcb9dc7a71203d770445dad6853e0f069f /cmds-device.c
parent6831721a431529e770c936f8cd479332d91fb652 (diff)
btrfs-progs: make the device scan logic more clear
1. Use long option to replace the original strcmp() to parse the "--all-devices". 2. the "int ret" is defined in 2 places, just define it once and make the return pattern into "goto + single return". This does not change the actual scan procedure and return values. Just make it clear, the original seems a little confusing. Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <clm@fb.com>
Diffstat (limited to 'cmds-device.c')
-rw-r--r--cmds-device.c61
1 files changed, 39 insertions, 22 deletions
diff --git a/cmds-device.c b/cmds-device.c
index d8bdcf4b..309b49e9 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -187,51 +187,67 @@ static int cmd_rm_dev(int argc, char **argv)
}
static const char * const cmd_scan_dev_usage[] = {
- "btrfs device scan [<--all-devices>|<device> [<device>...]]",
+ "btrfs device scan [options] [<device> [<device>...]]",
"Scan devices for a btrfs filesystem",
+ "-d|--all-devices scan all devices under /dev",
NULL
};
static int cmd_scan_dev(int argc, char **argv)
{
- int i, fd, e;
- int where = BTRFS_SCAN_LBLKID;
- int devstart = 1;
+ int i, fd, e;
+ int where = BTRFS_SCAN_LBLKID;
+ int devstart = 1;
+ int all = 0;
+ int ret = 0;
- if( argc > 1 && !strcmp(argv[1],"--all-devices")){
- if (check_argc_max(argc, 2))
+ optind = 1;
+ while (1) {
+ int long_index;
+ static struct option long_options[] = {
+ { "all-devices", no_argument, NULL, 'd'},
+ { 0, 0, 0, 0 },
+ };
+ int c = getopt_long(argc, argv, "d", long_options,
+ &long_index);
+ if (c < 0)
+ break;
+ switch (c) {
+ case 'd':
+ where = BTRFS_SCAN_DEV;
+ all = 1;
+ break;
+ default:
usage(cmd_scan_dev_usage);
-
- where = BTRFS_SCAN_DEV;
- devstart += 1;
+ }
}
- if(argc<=devstart){
- int ret;
+ if (all && check_argc_max(argc, 2))
+ usage(cmd_scan_dev_usage);
+
+ if (all || argc == 1) {
printf("Scanning for Btrfs filesystems\n");
ret = scan_for_btrfs(where, BTRFS_UPDATE_KERNEL);
- if (ret){
+ if (ret)
fprintf(stderr, "ERROR: error %d while scanning\n", ret);
- return 1;
- }
- return 0;
+ goto out;
}
fd = open("/dev/btrfs-control", O_RDWR);
if (fd < 0) {
perror("failed to open /dev/btrfs-control");
- return 1;
+ ret = 1;
+ goto out;
}
for( i = devstart ; i < argc ; i++ ){
struct btrfs_ioctl_vol_args args;
- int ret;
if (!is_block_device(argv[i])) {
fprintf(stderr,
"ERROR: %s is not a block device\n", argv[i]);
- close(fd);
- return 1;
+ ret = 1;
+ goto close_out;
}
printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
@@ -245,15 +261,16 @@ static int cmd_scan_dev(int argc, char **argv)
e = errno;
if( ret < 0 ){
- close(fd);
fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
argv[i], strerror(e));
- return 1;
+ goto close_out;
}
}
+close_out:
close(fd);
- return 0;
+out:
+ return !!ret;
}
static const char * const cmd_ready_dev_usage[] = {