btrfs-progs: fix find_mount_root() to handle duplicated mount point correctly
[btrfs-progs-unstable/devel.git] / btrfs.c
blobe83349cbb770d66b4147abf453501dbd57524234
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include "crc32c.h"
23 #include "commands.h"
24 #include "version.h"
25 #include "utils.h"
27 static const char * const btrfs_cmd_group_usage[] = {
28 "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
29 NULL
32 static const char btrfs_cmd_group_info[] =
33 "Use --help as an argument for information on a specific group or command.";
35 static inline const char *skip_prefix(const char *str, const char *prefix)
37 size_t len = strlen(prefix);
38 return strncmp(str, prefix, len) ? NULL : str + len;
41 int prefixcmp(const char *str, const char *prefix)
43 for (; ; str++, prefix++)
44 if (!*prefix)
45 return 0;
46 else if (*str != *prefix)
47 return (unsigned char)*prefix - (unsigned char)*str;
50 static int parse_one_token(const char *arg, const struct cmd_group *grp,
51 const struct cmd_struct **cmd_ret)
53 const struct cmd_struct *cmd = grp->commands;
54 const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
56 for (; cmd->token; cmd++) {
57 const char *rest;
59 rest = skip_prefix(arg, cmd->token);
60 if (!rest) {
61 if (!prefixcmp(cmd->token, arg)) {
62 if (abbrev_cmd) {
64 * If this is abbreviated, it is
65 * ambiguous. So when there is no
66 * exact match later, we need to
67 * error out.
69 ambiguous_cmd = abbrev_cmd;
71 abbrev_cmd = cmd;
73 continue;
75 if (*rest)
76 continue;
78 *cmd_ret = cmd;
79 return 0;
82 if (ambiguous_cmd)
83 return -2;
85 if (abbrev_cmd) {
86 *cmd_ret = abbrev_cmd;
87 return 0;
90 return -1;
93 static const struct cmd_struct *
94 parse_command_token(const char *arg, const struct cmd_group *grp)
96 const struct cmd_struct *cmd = NULL;
98 switch(parse_one_token(arg, grp, &cmd)) {
99 case -1:
100 help_unknown_token(arg, grp);
101 case -2:
102 help_ambiguous_token(arg, grp);
105 return cmd;
108 static void handle_help_options_next_level(const struct cmd_struct *cmd,
109 int argc, char **argv)
111 if (argc < 2)
112 return;
114 if (!strcmp(argv[1], "--help")) {
115 if (cmd->next) {
116 argc--;
117 argv++;
118 help_command_group(cmd->next, argc, argv);
119 } else {
120 usage_command(cmd, 1, 0);
123 exit(0);
127 int handle_command_group(const struct cmd_group *grp, int argc,
128 char **argv)
131 const struct cmd_struct *cmd;
133 argc--;
134 argv++;
135 if (argc < 1) {
136 usage_command_group(grp, 0, 0);
137 exit(1);
140 cmd = parse_command_token(argv[0], grp);
142 handle_help_options_next_level(cmd, argc, argv);
144 fixup_argv0(argv, cmd->token);
145 return cmd->fn(argc, argv);
148 static const struct cmd_group btrfs_cmd_group;
150 static const char * const cmd_help_usage[] = {
151 "btrfs help [--full]",
152 "Display help information",
154 "--full display detailed help on every command",
155 NULL
158 static int cmd_help(int argc, char **argv)
160 help_command_group(&btrfs_cmd_group, argc, argv);
161 return 0;
164 static const char * const cmd_version_usage[] = {
165 "btrfs version",
166 "Display btrfs-progs version",
167 NULL
170 static int cmd_version(int argc, char **argv)
172 printf("%s\n", BTRFS_BUILD_VERSION);
173 return 0;
176 static void handle_options(int *argc, char ***argv)
178 if (*argc > 0) {
179 const char *arg = (*argv)[0];
180 if (arg[0] != '-' ||
181 !strcmp(arg, "--help") ||
182 !strcmp(arg, "--version"))
183 return;
184 fprintf(stderr, "Unknown option: %s\n", arg);
185 fprintf(stderr, "usage: %s\n",
186 btrfs_cmd_group.usagestr[0]);
187 exit(129);
189 return;
192 static const struct cmd_group btrfs_cmd_group = {
193 btrfs_cmd_group_usage, btrfs_cmd_group_info, {
194 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
195 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
196 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
197 { "device", cmd_device, NULL, &device_cmd_group, 0 },
198 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
199 { "check", cmd_check, cmd_check_usage, NULL, 0 },
200 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
201 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
202 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
203 { "property", cmd_property, NULL, &property_cmd_group, 0 },
204 { "send", cmd_send, cmd_send_usage, NULL, 0 },
205 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
206 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
207 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
208 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
209 { "help", cmd_help, cmd_help_usage, NULL, 0 },
210 { "version", cmd_version, cmd_version_usage, NULL, 0 },
211 NULL_CMD_STRUCT
215 int main(int argc, char **argv)
217 const struct cmd_struct *cmd;
218 const char *bname;
220 if ((bname = strrchr(argv[0], '/')) != NULL)
221 bname++;
222 else
223 bname = argv[0];
225 if (!strcmp(bname, "btrfsck")) {
226 argv[0] = "check";
227 } else {
228 argc--;
229 argv++;
230 handle_options(&argc, &argv);
231 if (argc > 0) {
232 if (!prefixcmp(argv[0], "--"))
233 argv[0] += 2;
234 } else {
235 usage_command_group(&btrfs_cmd_group, 0, 0);
236 exit(1);
240 cmd = parse_command_token(argv[0], &btrfs_cmd_group);
242 handle_help_options_next_level(cmd, argc, argv);
244 crc32c_optimization_init();
246 fixup_argv0(argv, cmd->token);
247 exit(cmd->fn(argc, argv));