Btrfs-progs: receive: fix the case that we can not find the subvolume
[platform/upstream/btrfs-progs.git] / btrfs.c
diff --git a/btrfs.c b/btrfs.c
index 76f725a..d5fc738 100644 (file)
--- a/btrfs.c
+++ b/btrfs.c
 #include <stdlib.h>
 #include <string.h>
 
+#include "crc32c.h"
 #include "commands.h"
 #include "version.h"
 
-static const char btrfs_cmd_group_usage[] =
-       "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]";
+static const char * const btrfs_cmd_group_usage[] = {
+       "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
+       NULL
+};
 
 static const char btrfs_cmd_group_info[] =
        "Use --help as an argument for information on a specific group or command.";
 
-char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
+static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
 
 static inline const char *skip_prefix(const char *str, const char *prefix)
 {
@@ -91,7 +94,7 @@ static int parse_one_token(const char *arg, const struct cmd_group *grp,
 static const struct cmd_struct *
 parse_command_token(const char *arg, const struct cmd_group *grp)
 {
-       const struct cmd_struct *cmd;
+       const struct cmd_struct *cmd = NULL;
 
        switch(parse_one_token(arg, grp, &cmd)) {
        case -1:
@@ -103,8 +106,8 @@ parse_command_token(const char *arg, const struct cmd_group *grp)
        return cmd;
 }
 
-void handle_help_options_next_level(const struct cmd_struct *cmd,
-                                   int argc, char **argv)
+static void handle_help_options_next_level(const struct cmd_struct *cmd,
+               int argc, char **argv)
 {
        if (argc < 2)
                return;
@@ -181,11 +184,11 @@ int check_argc_max(int nargs, int expected)
        return 0;
 }
 
-const struct cmd_group btrfs_cmd_group;
+static const struct cmd_group btrfs_cmd_group;
 
 static const char * const cmd_help_usage[] = {
        "btrfs help [--full]",
-       "Dislay help information",
+       "Display help information",
        "",
        "--full     display detailed help on every command",
        NULL
@@ -225,7 +228,7 @@ static int handle_options(int *argc, char ***argv)
                } else {
                        fprintf(stderr, "Unknown option: %s\n", arg);
                        fprintf(stderr, "usage: %s\n",
-                               btrfs_cmd_group.usagestr);
+                               btrfs_cmd_group.usagestr[0]);
                        exit(129);
                }
 
@@ -236,39 +239,59 @@ static int handle_options(int *argc, char ***argv)
        return (*argv) - orig_argv;
 }
 
-const struct cmd_group btrfs_cmd_group = {
+static 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 },
                { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
                { "device", cmd_device, NULL, &device_cmd_group, 0 },
                { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
+               { "check", cmd_check, cmd_check_usage, NULL, 0 },
+               { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
+               { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
                { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
+               { "send", cmd_send, cmd_send_usage, NULL, 0 },
+               { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
+               { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
+               { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
+               { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
                { "help", cmd_help, cmd_help_usage, NULL, 0 },
                { "version", cmd_version, cmd_version_usage, NULL, 0 },
-               { 0, 0, 0, 0, 0 }
+               NULL_CMD_STRUCT
        },
 };
 
 int main(int argc, char **argv)
 {
        const struct cmd_struct *cmd;
+       const char *bname;
 
-       argc--;
-       argv++;
-       handle_options(&argc, &argv);
-       if (argc > 0) {
-               if (!prefixcmp(argv[0], "--"))
-                       argv[0] += 2;
+       if ((bname = strrchr(argv[0], '/')) != NULL)
+               bname++;
+       else
+               bname = argv[0];
+
+       if (!strcmp(bname, "btrfsck")) {
+               argv[0] = "check";
        } else {
-               usage_command_group(&btrfs_cmd_group, 0, 0);
-               exit(1);
+               argc--;
+               argv++;
+               handle_options(&argc, &argv);
+               if (argc > 0) {
+                       if (!prefixcmp(argv[0], "--"))
+                               argv[0] += 2;
+               } else {
+                       usage_command_group(&btrfs_cmd_group, 0, 0);
+                       exit(1);
+               }
        }
 
        cmd = parse_command_token(argv[0], &btrfs_cmd_group);
 
        handle_help_options_next_level(cmd, argc, argv);
 
+       crc32c_optimization_init();
+
        fixup_argv0(argv, cmd->token);
        exit(cmd->fn(argc, argv));
 }