2010-01-25 01:00:05 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-09-09 13:49:14 +08:00
|
|
|
#define _GNU_SOURCE
|
2010-01-25 01:00:05 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-08-10 10:46:16 +08:00
|
|
|
#include "crc32c.h"
|
2012-02-04 03:00:17 +08:00
|
|
|
#include "commands.h"
|
2010-01-25 01:00:05 +08:00
|
|
|
#include "version.h"
|
|
|
|
|
2012-02-08 23:45:54 +08:00
|
|
|
static const char * const btrfs_cmd_group_usage[] = {
|
|
|
|
"btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
|
|
|
|
NULL
|
|
|
|
};
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static const char btrfs_cmd_group_info[] =
|
|
|
|
"Use --help as an argument for information on a specific group or command.";
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2013-08-15 07:16:41 +08:00
|
|
|
static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static inline const char *skip_prefix(const char *str, const char *prefix)
|
2010-01-25 01:00:05 +08:00
|
|
|
{
|
2012-02-04 03:00:17 +08:00
|
|
|
size_t len = strlen(prefix);
|
|
|
|
return strncmp(str, prefix, len) ? NULL : str + len;
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
int prefixcmp(const char *str, const char *prefix)
|
2010-01-25 01:00:05 +08:00
|
|
|
{
|
2012-02-04 03:00:17 +08:00
|
|
|
for (; ; str++, prefix++)
|
|
|
|
if (!*prefix)
|
|
|
|
return 0;
|
|
|
|
else if (*str != *prefix)
|
|
|
|
return (unsigned char)*prefix - (unsigned char)*str;
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static int parse_one_token(const char *arg, const struct cmd_group *grp,
|
|
|
|
const struct cmd_struct **cmd_ret)
|
|
|
|
{
|
|
|
|
const struct cmd_struct *cmd = grp->commands;
|
|
|
|
const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
|
|
|
|
|
|
|
|
for (; cmd->token; cmd++) {
|
|
|
|
const char *rest;
|
|
|
|
|
|
|
|
rest = skip_prefix(arg, cmd->token);
|
|
|
|
if (!rest) {
|
|
|
|
if (!prefixcmp(cmd->token, arg)) {
|
|
|
|
if (abbrev_cmd) {
|
|
|
|
/*
|
|
|
|
* If this is abbreviated, it is
|
|
|
|
* ambiguous. So when there is no
|
|
|
|
* exact match later, we need to
|
|
|
|
* error out.
|
|
|
|
*/
|
|
|
|
ambiguous_cmd = abbrev_cmd;
|
|
|
|
}
|
|
|
|
abbrev_cmd = cmd;
|
|
|
|
}
|
|
|
|
continue;
|
2011-01-23 20:42:43 +08:00
|
|
|
}
|
2012-02-04 03:00:17 +08:00
|
|
|
if (*rest)
|
|
|
|
continue;
|
2011-01-23 20:42:43 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
*cmd_ret = cmd;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
if (ambiguous_cmd)
|
|
|
|
return -2;
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
if (abbrev_cmd) {
|
|
|
|
*cmd_ret = abbrev_cmd;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
return -1;
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static const struct cmd_struct *
|
|
|
|
parse_command_token(const char *arg, const struct cmd_group *grp)
|
2010-01-25 01:00:05 +08:00
|
|
|
{
|
2012-08-10 10:46:16 +08:00
|
|
|
const struct cmd_struct *cmd = NULL;
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
switch(parse_one_token(arg, grp, &cmd)) {
|
|
|
|
case -1:
|
|
|
|
help_unknown_token(arg, grp);
|
|
|
|
case -2:
|
|
|
|
help_ambiguous_token(arg, grp);
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
return cmd;
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 04:20:47 +08:00
|
|
|
static void handle_help_options_next_level(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2012-02-04 03:00:17 +08:00
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "--help")) {
|
|
|
|
if (cmd->next) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
help_command_group(cmd->next, argc, argv);
|
|
|
|
} else {
|
|
|
|
usage_command(cmd, 1, 0);
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
2012-02-04 03:00:17 +08:00
|
|
|
|
|
|
|
exit(0);
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static void fixup_argv0(char **argv, const char *token)
|
|
|
|
{
|
|
|
|
int len = strlen(argv0_buf);
|
2010-03-12 04:08:22 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
|
|
|
|
argv[0] = argv0_buf;
|
|
|
|
}
|
2010-03-12 04:08:22 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
int handle_command_group(const struct cmd_group *grp, int argc,
|
|
|
|
char **argv)
|
2010-03-12 04:08:22 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
{
|
|
|
|
const struct cmd_struct *cmd;
|
2010-03-12 04:08:22 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
if (argc < 1) {
|
|
|
|
usage_command_group(grp, 0, 0);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-03-12 04:08:22 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
cmd = parse_command_token(argv[0], grp);
|
|
|
|
|
|
|
|
handle_help_options_next_level(cmd, argc, argv);
|
|
|
|
|
|
|
|
fixup_argv0(argv, cmd->token);
|
|
|
|
return cmd->fn(argc, argv);
|
2010-03-12 04:08:22 +08:00
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
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);
|
2010-03-12 04:08:22 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
return nargs != expected;
|
|
|
|
}
|
2010-03-12 04:08:22 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
int check_argc_min(int nargs, int expected)
|
2010-01-25 01:00:05 +08:00
|
|
|
{
|
2012-02-04 03:00:17 +08:00
|
|
|
if (nargs < expected) {
|
|
|
|
fprintf(stderr, "%s: too few arguments\n", argv0_buf);
|
|
|
|
return 1;
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
int check_argc_max(int nargs, int expected)
|
|
|
|
{
|
|
|
|
if (nargs > expected) {
|
|
|
|
fprintf(stderr, "%s: too many arguments\n", argv0_buf);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2013-08-15 07:16:41 +08:00
|
|
|
static const struct cmd_group btrfs_cmd_group;
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static const char * const cmd_help_usage[] = {
|
|
|
|
"btrfs help [--full]",
|
2013-07-23 10:43:21 +08:00
|
|
|
"Display help information",
|
2012-02-04 03:00:17 +08:00
|
|
|
"",
|
|
|
|
"--full display detailed help on every command",
|
|
|
|
NULL
|
|
|
|
};
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static int cmd_help(int argc, char **argv)
|
|
|
|
{
|
|
|
|
help_command_group(&btrfs_cmd_group, argc, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static const char * const cmd_version_usage[] = {
|
|
|
|
"btrfs version",
|
|
|
|
"Display btrfs-progs version",
|
|
|
|
NULL
|
|
|
|
};
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static int cmd_version(int argc, char **argv)
|
|
|
|
{
|
|
|
|
printf("%s\n", BTRFS_BUILD_VERSION);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
static int handle_options(int *argc, char ***argv)
|
|
|
|
{
|
|
|
|
char **orig_argv = *argv;
|
|
|
|
|
|
|
|
while (*argc > 0) {
|
|
|
|
const char *arg = (*argv)[0];
|
|
|
|
if (arg[0] != '-')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!strcmp(arg, "--help")) {
|
|
|
|
break;
|
|
|
|
} else if (!strcmp(arg, "--version")) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown option: %s\n", arg);
|
|
|
|
fprintf(stderr, "usage: %s\n",
|
2012-02-08 23:45:54 +08:00
|
|
|
btrfs_cmd_group.usagestr[0]);
|
2012-02-04 03:00:17 +08:00
|
|
|
exit(129);
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
(*argv)++;
|
|
|
|
(*argc)--;
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
return (*argv) - orig_argv;
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
|
2013-08-15 07:16:41 +08:00
|
|
|
static const struct cmd_group btrfs_cmd_group = {
|
2012-02-04 03:00:17 +08:00
|
|
|
btrfs_cmd_group_usage, btrfs_cmd_group_info, {
|
2012-02-04 03:00:17 +08:00
|
|
|
{ "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
|
|
|
|
{ "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
|
2012-02-04 03:02:30 +08:00
|
|
|
{ "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
|
2012-02-04 03:00:17 +08:00
|
|
|
{ "device", cmd_device, NULL, &device_cmd_group, 0 },
|
|
|
|
{ "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
|
2013-02-08 08:36:58 +08:00
|
|
|
{ "check", cmd_check, cmd_check_usage, NULL, 0 },
|
2013-07-03 21:25:17 +08:00
|
|
|
{ "chunk-recover", cmd_chunk_recover, cmd_chunk_recover_usage, NULL, 0},
|
2013-02-08 08:37:02 +08:00
|
|
|
{ "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
|
2012-02-04 03:00:17 +08:00
|
|
|
{ "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
|
2013-01-18 14:52:12 +08:00
|
|
|
{ "send", cmd_send, cmd_send_usage, NULL, 0 },
|
|
|
|
{ "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
|
2012-08-07 18:37:54 +08:00
|
|
|
{ "quota", cmd_quota, NULL, "a_cmd_group, 0 },
|
|
|
|
{ "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
|
2012-05-07 20:00:20 +08:00
|
|
|
{ "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
|
2012-02-04 03:00:17 +08:00
|
|
|
{ "help", cmd_help, cmd_help_usage, NULL, 0 },
|
|
|
|
{ "version", cmd_version, cmd_version_usage, NULL, 0 },
|
2013-08-15 07:16:45 +08:00
|
|
|
NULL_CMD_STRUCT
|
2012-02-04 03:00:17 +08:00
|
|
|
},
|
|
|
|
};
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const struct cmd_struct *cmd;
|
2013-02-13 03:24:50 +08:00
|
|
|
const char *bname;
|
2012-02-04 03:00:17 +08:00
|
|
|
|
2013-02-13 03:24:50 +08:00
|
|
|
if ((bname = strrchr(argv[0], '/')) != NULL)
|
|
|
|
bname++;
|
|
|
|
else
|
|
|
|
bname = argv[0];
|
|
|
|
|
|
|
|
if (!strcmp(bname, "btrfsck")) {
|
|
|
|
argv[0] = "check";
|
2012-02-04 03:00:17 +08:00
|
|
|
} else {
|
2013-02-13 03:24:50 +08:00
|
|
|
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);
|
|
|
|
}
|
2010-01-25 01:00:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
cmd = parse_command_token(argv[0], &btrfs_cmd_group);
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
handle_help_options_next_level(cmd, argc, argv);
|
2010-01-25 01:00:05 +08:00
|
|
|
|
2013-02-13 03:24:50 +08:00
|
|
|
crc32c_optimization_init();
|
|
|
|
|
2012-02-04 03:00:17 +08:00
|
|
|
fixup_argv0(argv, cmd->token);
|
|
|
|
exit(cmd->fn(argc, argv));
|
|
|
|
}
|