btrfs-progs: check: introduce function to check tree block backref in extent tree
[platform/upstream/btrfs-progs.git] / btrfs.c
diff --git a/btrfs.c b/btrfs.c
index a9ff8fa..cc70515 100644 (file)
--- a/btrfs.c
+++ b/btrfs.c
  * Boston, MA 021110-1307, USA.
  */
 
-#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "volumes.h"
 #include "crc32c.h"
 #include "commands.h"
-#include "version.h"
+#include "utils.h"
 
 static const char * const btrfs_cmd_group_usage[] = {
        "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
@@ -31,23 +31,12 @@ static const char * const btrfs_cmd_group_usage[] = {
 static const char btrfs_cmd_group_info[] =
        "Use --help as an argument for information on a specific group or command.";
 
-static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
-
 static inline const char *skip_prefix(const char *str, const char *prefix)
 {
        size_t len = strlen(prefix);
        return strncmp(str, prefix, len) ? NULL : str + len;
 }
 
-int prefixcmp(const char *str, const char *prefix)
-{
-       for (; ; str++, prefix++)
-               if (!*prefix)
-                       return 0;
-               else if (*str != *prefix)
-                       return (unsigned char)*prefix - (unsigned char)*str;
-}
-
 static int parse_one_token(const char *arg, const struct cmd_group *grp,
                           const struct cmd_struct **cmd_ret)
 {
@@ -125,14 +114,6 @@ static void handle_help_options_next_level(const struct cmd_struct *cmd,
        }
 }
 
-static void fixup_argv0(char **argv, const char *token)
-{
-       int len = strlen(argv0_buf);
-
-       snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
-       argv[0] = argv0_buf;
-}
-
 int handle_command_group(const struct cmd_group *grp, int argc,
                         char **argv)
 
@@ -154,36 +135,6 @@ int handle_command_group(const struct cmd_group *grp, int argc,
        return cmd->fn(argc, argv);
 }
 
-int check_argc_exact(int nargs, int expected)
-{
-       if (nargs < expected)
-               fprintf(stderr, "%s: too few arguments\n", argv0_buf);
-       if (nargs > expected)
-               fprintf(stderr, "%s: too many arguments\n", argv0_buf);
-
-       return nargs != expected;
-}
-
-int check_argc_min(int nargs, int expected)
-{
-       if (nargs < expected) {
-               fprintf(stderr, "%s: too few arguments\n", argv0_buf);
-               return 1;
-       }
-
-       return 0;
-}
-
-int check_argc_max(int nargs, int expected)
-{
-       if (nargs > expected) {
-               fprintf(stderr, "%s: too many arguments\n", argv0_buf);
-               return 1;
-       }
-
-       return 0;
-}
-
 static const struct cmd_group btrfs_cmd_group;
 
 static const char * const cmd_help_usage[] = {
@@ -208,35 +159,28 @@ static const char * const cmd_version_usage[] = {
 
 static int cmd_version(int argc, char **argv)
 {
-       printf("%s\n", BTRFS_BUILD_VERSION);
+       printf("%s\n", PACKAGE_STRING);
        return 0;
 }
 
-static int handle_options(int *argc, char ***argv)
+static void check_options(int argc, char **argv)
 {
-       char **orig_argv = *argv;
+       const char *arg;
 
-       while (*argc > 0) {
-               const char *arg = (*argv)[0];
-               if (arg[0] != '-')
-                       break;
+       if (argc == 0)
+               return;
 
-               if (!strcmp(arg, "--help")) {
-                       break;
-               } else if (!strcmp(arg, "--version")) {
-                       break;
-               } else {
-                       fprintf(stderr, "Unknown option: %s\n", arg);
-                       fprintf(stderr, "usage: %s\n",
-                               btrfs_cmd_group.usagestr[0]);
-                       exit(129);
-               }
+       arg = argv[0];
 
-               (*argv)++;
-               (*argc)--;
-       }
+       if (arg[0] != '-' ||
+           !strcmp(arg, "--help") ||
+           !strcmp(arg, "--version"))
+               return;
 
-       return (*argv) - orig_argv;
+       fprintf(stderr, "Unknown option: %s\n", arg);
+       fprintf(stderr, "usage: %s\n",
+               btrfs_cmd_group.usagestr[0]);
+       exit(129);
 }
 
 static const struct cmd_group btrfs_cmd_group = {
@@ -248,9 +192,9 @@ static const struct cmd_group btrfs_cmd_group = {
                { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
                { "check", cmd_check, cmd_check_usage, NULL, 0 },
                { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
-               { "chunk-recover", cmd_chunk_recover, cmd_chunk_recover_usage, NULL, 0},
                { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
                { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
+               { "property", cmd_property, NULL, &property_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 },
@@ -266,6 +210,7 @@ int main(int argc, char **argv)
 {
        const struct cmd_struct *cmd;
        const char *bname;
+       int ret;
 
        if ((bname = strrchr(argv[0], '/')) != NULL)
                bname++;
@@ -277,12 +222,12 @@ int main(int argc, char **argv)
        } else {
                argc--;
                argv++;
-               handle_options(&argc, &argv);
+               check_options(argc, argv);
                if (argc > 0) {
                        if (!prefixcmp(argv[0], "--"))
                                argv[0] += 2;
                } else {
-                       usage_command_group(&btrfs_cmd_group, 0, 0);
+                       usage_command_group_short(&btrfs_cmd_group);
                        exit(1);
                }
        }
@@ -294,5 +239,10 @@ int main(int argc, char **argv)
        crc32c_optimization_init();
 
        fixup_argv0(argv, cmd->token);
-       exit(cmd->fn(argc, argv));
+
+       ret = cmd->fn(argc, argv);
+
+       btrfs_close_all_devices();
+
+       exit(ret);
 }