From d675085a67fba325c7c35dbf3b2ec544354f7742 Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Fri, 3 Feb 2012 21:00:17 +0200 Subject: [PATCH] Btrfs-progs: switch all existing commands to a new parser The new infrastructure offloads checking number of arguments passed to a command to individual command handlers. Fix them up accordingly. Signed-off-by: Ilya Dryomov --- btrfs.c | 5 ++ btrfs_cmds.h | 41 ----------- cmds-device.c | 104 ++++++++++++++++++---------- cmds-filesystem.c | 170 ++++++++++++++++++++++++++++++++++------------ cmds-inspect.c | 61 ++++++++++++----- cmds-scrub.c | 111 ++++++++++++++++++++++-------- cmds-subvolume.c | 161 +++++++++++++++++++++++++++++++++---------- commands.h | 15 ++++ 8 files changed, 467 insertions(+), 201 deletions(-) delete mode 100644 btrfs_cmds.h diff --git a/btrfs.c b/btrfs.c index 7cff30bd..599a3e77 100644 --- a/btrfs.c +++ b/btrfs.c @@ -238,6 +238,11 @@ static int handle_options(int *argc, char ***argv) const struct cmd_group btrfs_cmd_group = { btrfs_cmd_group_usage, btrfs_cmd_group_info, { + { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 }, + { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 }, + { "device", cmd_device, NULL, &device_cmd_group, 0 }, + { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 }, + { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 }, { "help", cmd_help, cmd_help_usage, NULL, 0 }, { "version", cmd_version, cmd_version_usage, NULL, 0 }, { 0, 0, 0, 0, 0 } diff --git a/btrfs_cmds.h b/btrfs_cmds.h deleted file mode 100644 index efca7c51..00000000 --- a/btrfs_cmds.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public - * License v2 as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 021110-1307, USA. - */ - -/* btrfs_cmds.c*/ -int do_clone(int nargs, char **argv); -int do_delete_subvolume(int nargs, char **argv); -int do_create_subvol(int nargs, char **argv); -int do_fssync(int nargs, char **argv); -int do_defrag(int argc, char **argv); -int do_show_filesystem(int nargs, char **argv); -int do_add_volume(int nargs, char **args); -int do_balance(int nargs, char **argv); -int do_scrub_start(int nargs, char **argv); -int do_scrub_status(int argc, char **argv); -int do_scrub_resume(int argc, char **argv); -int do_scrub_cancel(int nargs, char **argv); -int do_remove_volume(int nargs, char **args); -int do_scan(int nargs, char **argv); -int do_resize(int nargs, char **argv); -int do_subvol_list(int nargs, char **argv); -int do_set_default_subvol(int nargs, char **argv); -int do_get_default_subvol(int nargs, char **argv); -int do_df_filesystem(int nargs, char **argv); -int do_find_newer(int argc, char **argv); -int do_change_label(int argc, char **argv); -int open_file_or_dir(const char *fname); -int do_ino_to_path(int nargs, char **argv); -int do_logical_to_ino(int nargs, char **argv); diff --git a/cmds-device.c b/cmds-device.c index 0fc4c7fa..51089bae 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -28,7 +28,7 @@ #include "ioctl.h" #include "utils.h" -#include "btrfs_cmds.h" +#include "commands.h" /* FIXME - imported cruft, fix sparse errors and warnings */ #ifdef __CHECKER__ @@ -39,12 +39,24 @@ struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; }; static inline int ioctl(int fd, int define, void *arg) { return 0; } #endif -int do_add_volume(int nargs, char **args) -{ +static const char device_cmd_group_usage[] = + "btrfs device []"; - char *mntpnt = args[nargs-1]; +static const char * const cmd_add_dev_usage[] = { + "btrfs device add [...] ", + "Add a device to a filesystem", + NULL +}; + +static int cmd_add_dev(int argc, char **argv) +{ + char *mntpnt; int i, fdmnt, ret=0, e; + if (check_argc_min(argc, 3)) + usage(cmd_add_dev_usage); + + mntpnt = argv[argc - 1]; fdmnt = open_file_or_dir(mntpnt); if (fdmnt < 0) { @@ -52,62 +64,62 @@ int do_add_volume(int nargs, char **args) return 12; } - for (i = 1; i < (nargs-1); i++ ){ + for (i = 1; i < argc - 1; i++ ){ struct btrfs_ioctl_vol_args ioctl_args; int devfd, res; u64 dev_block_count = 0; struct stat st; int mixed = 0; - res = check_mounted(args[i]); + res = check_mounted(argv[i]); if (res < 0) { fprintf(stderr, "error checking %s mount status\n", - args[i]); + argv[i]); ret++; continue; } if (res == 1) { - fprintf(stderr, "%s is mounted\n", args[i]); + fprintf(stderr, "%s is mounted\n", argv[i]); ret++; continue; } - devfd = open(args[i], O_RDWR); + devfd = open(argv[i], O_RDWR); if (!devfd) { - fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]); + fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]); close(devfd); ret++; continue; } res = fstat(devfd, &st); if (res) { - fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]); + fprintf(stderr, "ERROR: Unable to stat '%s'\n", argv[i]); close(devfd); ret++; continue; } if (!S_ISBLK(st.st_mode)) { - fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]); + fprintf(stderr, "ERROR: '%s' is not a block device\n", argv[i]); close(devfd); ret++; continue; } - res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed); + res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count, &mixed); if (res) { - fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]); + fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]); close(devfd); ret++; continue; } close(devfd); - strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX); + strncpy(ioctl_args.name, argv[i], BTRFS_PATH_NAME_MAX); res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args); e = errno; if(res<0){ - fprintf(stderr, "ERROR: error adding the device '%s' - %s\n", - args[i], strerror(e)); + fprintf(stderr, "ERROR: error adding the device '%s' - %s\n", + argv[i], strerror(e)); ret++; } @@ -118,31 +130,40 @@ int do_add_volume(int nargs, char **args) return ret+20; else return 0; - } -int do_remove_volume(int nargs, char **args) -{ +static const char * const cmd_rm_dev_usage[] = { + "btrfs device delete [...] ", + "Remove a device from a filesystem", + NULL +}; - char *mntpnt = args[nargs-1]; +static int cmd_rm_dev(int argc, char **argv) +{ + char *mntpnt; int i, fdmnt, ret=0, e; + if (check_argc_min(argc, 3)) + usage(cmd_rm_dev_usage); + + mntpnt = argv[argc - 1]; + fdmnt = open_file_or_dir(mntpnt); if (fdmnt < 0) { fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt); return 12; } - for(i=1 ; i < (nargs-1) ; i++ ){ + for(i=1 ; i < argc - 1; i++ ){ struct btrfs_ioctl_vol_args arg; int res; - strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX); + strncpy(arg.name, argv[i], BTRFS_PATH_NAME_MAX); res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg); e = errno; if(res<0){ - fprintf(stderr, "ERROR: error removing the device '%s' - %s\n", - args[i], strerror(e)); + fprintf(stderr, "ERROR: error removing the device '%s' - %s\n", + argv[i], strerror(e)); ret++; } } @@ -154,18 +175,21 @@ int do_remove_volume(int nargs, char **args) return 0; } -int do_scan(int argc, char **argv) +static const char * const cmd_scan_dev_usage[] = { + "btrfs device scan [...]", + "Scan devices for a btrfs filesystem", + NULL +}; + +static int cmd_scan_dev(int argc, char **argv) { int i, fd, e; int checklist = 1; int devstart = 1; - if( argc >= 2 && !strcmp(argv[1],"--all-devices")){ - - if( argc >2 ){ - fprintf(stderr, "ERROR: too may arguments\n"); - return 22; - } + if( argc > 1 && !strcmp(argv[1],"--all-devices")){ + if (check_argc_max(argc, 2)) + usage(cmd_scan_dev_usage); checklist = 0; devstart += 1; @@ -210,7 +234,7 @@ int do_scan(int argc, char **argv) if( ret < 0 ){ close(fd); - fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", + fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", argv[i], strerror(e)); return 11; } @@ -218,6 +242,18 @@ int do_scan(int argc, char **argv) close(fd); return 0; - } +const struct cmd_group device_cmd_group = { + device_cmd_group_usage, NULL, { + { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 }, + { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 }, + { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 }, + { 0, 0, 0, 0, 0 } + } +}; + +int cmd_device(int argc, char **argv) +{ + return handle_command_group(&device_cmd_group, argc, argv); +} diff --git a/cmds-filesystem.c b/cmds-filesystem.c index 2e607d2c..828ca0c7 100644 --- a/cmds-filesystem.c +++ b/cmds-filesystem.c @@ -31,17 +31,31 @@ #include "version.h" -#include "btrfs_cmds.h" +#include "commands.h" #include "btrfslabel.h" -int do_df_filesystem(int nargs, char **argv) +static const char filesystem_cmd_group_usage[] = + "btrfs filesystem [] []"; + +static const char * const cmd_df_usage[] = { + "btrfs filesystem df ", + "Show space usage information for a mount point", + NULL +}; + +static int cmd_df(int argc, char **argv) { struct btrfs_ioctl_space_args *sargs; u64 count = 0, i; int ret; int fd; int e; - char *path = argv[1]; + char *path; + + if (check_argc_exact(argc, 2)) + usage(cmd_df_usage); + + path = argv[1]; fd = open_file_or_dir(path); if (fd < 0) { @@ -195,7 +209,14 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices) printf("\n"); } -int do_show_filesystem(int argc, char **argv) +static const char * const cmd_show_usage[] = { + "btrfs filesystem show [--all-devices] [|