btrfs-show-super: don't try to print not-superblocks
[platform/upstream/btrfs-progs.git] / btrfs.c
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.
5  *
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.
10  *
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.
15  */
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "crc32c.h"
23 #include "commands.h"
24 #include "version.h"
25
26 static const char * const btrfs_cmd_group_usage[] = {
27         "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
28         NULL
29 };
30
31 static const char btrfs_cmd_group_info[] =
32         "Use --help as an argument for information on a specific group or command.";
33
34 static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
35
36 static inline const char *skip_prefix(const char *str, const char *prefix)
37 {
38         size_t len = strlen(prefix);
39         return strncmp(str, prefix, len) ? NULL : str + len;
40 }
41
42 int prefixcmp(const char *str, const char *prefix)
43 {
44         for (; ; str++, prefix++)
45                 if (!*prefix)
46                         return 0;
47                 else if (*str != *prefix)
48                         return (unsigned char)*prefix - (unsigned char)*str;
49 }
50
51 static int parse_one_token(const char *arg, const struct cmd_group *grp,
52                            const struct cmd_struct **cmd_ret)
53 {
54         const struct cmd_struct *cmd = grp->commands;
55         const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
56
57         for (; cmd->token; cmd++) {
58                 const char *rest;
59
60                 rest = skip_prefix(arg, cmd->token);
61                 if (!rest) {
62                         if (!prefixcmp(cmd->token, arg)) {
63                                 if (abbrev_cmd) {
64                                         /*
65                                          * If this is abbreviated, it is
66                                          * ambiguous. So when there is no
67                                          * exact match later, we need to
68                                          * error out.
69                                          */
70                                         ambiguous_cmd = abbrev_cmd;
71                                 }
72                                 abbrev_cmd = cmd;
73                         }
74                         continue;
75                 }
76                 if (*rest)
77                         continue;
78
79                 *cmd_ret = cmd;
80                 return 0;
81         }
82
83         if (ambiguous_cmd)
84                 return -2;
85
86         if (abbrev_cmd) {
87                 *cmd_ret = abbrev_cmd;
88                 return 0;
89         }
90
91         return -1;
92 }
93
94 static const struct cmd_struct *
95 parse_command_token(const char *arg, const struct cmd_group *grp)
96 {
97         const struct cmd_struct *cmd = NULL;
98
99         switch(parse_one_token(arg, grp, &cmd)) {
100         case -1:
101                 help_unknown_token(arg, grp);
102         case -2:
103                 help_ambiguous_token(arg, grp);
104         }
105
106         return cmd;
107 }
108
109 static void handle_help_options_next_level(const struct cmd_struct *cmd,
110                 int argc, char **argv)
111 {
112         if (argc < 2)
113                 return;
114
115         if (!strcmp(argv[1], "--help")) {
116                 if (cmd->next) {
117                         argc--;
118                         argv++;
119                         help_command_group(cmd->next, argc, argv);
120                 } else {
121                         usage_command(cmd, 1, 0);
122                 }
123
124                 exit(0);
125         }
126 }
127
128 static void fixup_argv0(char **argv, const char *token)
129 {
130         int len = strlen(argv0_buf);
131
132         snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
133         argv[0] = argv0_buf;
134 }
135
136 int handle_command_group(const struct cmd_group *grp, int argc,
137                          char **argv)
138
139 {
140         const struct cmd_struct *cmd;
141
142         argc--;
143         argv++;
144         if (argc < 1) {
145                 usage_command_group(grp, 0, 0);
146                 exit(1);
147         }
148
149         cmd = parse_command_token(argv[0], grp);
150
151         handle_help_options_next_level(cmd, argc, argv);
152
153         fixup_argv0(argv, cmd->token);
154         return cmd->fn(argc, argv);
155 }
156
157 int check_argc_exact(int nargs, int expected)
158 {
159         if (nargs < expected)
160                 fprintf(stderr, "%s: too few arguments\n", argv0_buf);
161         if (nargs > expected)
162                 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
163
164         return nargs != expected;
165 }
166
167 int check_argc_min(int nargs, int expected)
168 {
169         if (nargs < expected) {
170                 fprintf(stderr, "%s: too few arguments\n", argv0_buf);
171                 return 1;
172         }
173
174         return 0;
175 }
176
177 int check_argc_max(int nargs, int expected)
178 {
179         if (nargs > expected) {
180                 fprintf(stderr, "%s: too many arguments\n", argv0_buf);
181                 return 1;
182         }
183
184         return 0;
185 }
186
187 static const struct cmd_group btrfs_cmd_group;
188
189 static const char * const cmd_help_usage[] = {
190         "btrfs help [--full]",
191         "Display help information",
192         "",
193         "--full     display detailed help on every command",
194         NULL
195 };
196
197 static int cmd_help(int argc, char **argv)
198 {
199         help_command_group(&btrfs_cmd_group, argc, argv);
200         return 0;
201 }
202
203 static const char * const cmd_version_usage[] = {
204         "btrfs version",
205         "Display btrfs-progs version",
206         NULL
207 };
208
209 static int cmd_version(int argc, char **argv)
210 {
211         printf("%s\n", BTRFS_BUILD_VERSION);
212         return 0;
213 }
214
215 static void handle_options(int *argc, char ***argv)
216 {
217         if (*argc > 0) {
218                 const char *arg = (*argv)[0];
219                 if (arg[0] != '-' ||
220                     !strcmp(arg, "--help") ||
221                     !strcmp(arg, "--version"))
222                         return;
223                 fprintf(stderr, "Unknown option: %s\n", arg);
224                 fprintf(stderr, "usage: %s\n",
225                         btrfs_cmd_group.usagestr[0]);
226                 exit(129);
227         }
228         return;
229 }
230
231 static const struct cmd_group btrfs_cmd_group = {
232         btrfs_cmd_group_usage, btrfs_cmd_group_info, {
233                 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
234                 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
235                 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
236                 { "device", cmd_device, NULL, &device_cmd_group, 0 },
237                 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
238                 { "check", cmd_check, cmd_check_usage, NULL, 0 },
239                 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
240                 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
241                 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
242                 { "property", cmd_property, NULL, &property_cmd_group, 0 },
243                 { "send", cmd_send, cmd_send_usage, NULL, 0 },
244                 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
245                 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
246                 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
247                 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
248                 { "help", cmd_help, cmd_help_usage, NULL, 0 },
249                 { "version", cmd_version, cmd_version_usage, NULL, 0 },
250                 NULL_CMD_STRUCT
251         },
252 };
253
254 int main(int argc, char **argv)
255 {
256         const struct cmd_struct *cmd;
257         const char *bname;
258
259         if ((bname = strrchr(argv[0], '/')) != NULL)
260                 bname++;
261         else
262                 bname = argv[0];
263
264         if (!strcmp(bname, "btrfsck")) {
265                 argv[0] = "check";
266         } else {
267                 argc--;
268                 argv++;
269                 handle_options(&argc, &argv);
270                 if (argc > 0) {
271                         if (!prefixcmp(argv[0], "--"))
272                                 argv[0] += 2;
273                 } else {
274                         usage_command_group(&btrfs_cmd_group, 0, 0);
275                         exit(1);
276                 }
277         }
278
279         cmd = parse_command_token(argv[0], &btrfs_cmd_group);
280
281         handle_help_options_next_level(cmd, argc, argv);
282
283         crc32c_optimization_init();
284
285         fixup_argv0(argv, cmd->token);
286         exit(cmd->fn(argc, argv));
287 }