Permalink
Browse files

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 <idryomov@gmail.com>
  • Loading branch information...
1 parent 8b4e3d8 commit d675085a67fba325c7c35dbf3b2ec544354f7742 @idryomov committed Feb 3, 2012
Showing with 466 additions and 200 deletions.
  1. +5 −0 btrfs.c
  2. +0 −41 btrfs_cmds.h
  3. +70 −34 cmds-device.c
  4. +127 −43 cmds-filesystem.c
  5. +44 −17 cmds-inspect.c
  6. +81 −28 cmds-scrub.c
  7. +124 −37 cmds-subvolume.c
  8. +15 −0 commands.h
View
@@ -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 }
View
@@ -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);
View
@@ -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,75 +39,87 @@ 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 <command> [<args>]";
+
+static const char * const cmd_add_dev_usage[] = {
+ "btrfs device add <device> [<device>...] <path>",
+ "Add a device to a filesystem",
+ NULL
+};
- char *mntpnt = args[nargs-1];
+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) {
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 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 <device> [<device>...] <path>",
+ "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 [<device>...]",
+ "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,14 +234,26 @@ 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;
}
}
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);
+}
Oops, something went wrong.

0 comments on commit d675085

Please sign in to comment.