btrfs-progs: build: mention library dependency for reiserfs
[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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <getopt.h>
21
22 #include "volumes.h"
23 #include "crc32c.h"
24 #include "commands.h"
25 #include "utils.h"
26 #include "help.h"
27
28 static const char * const btrfs_cmd_group_usage[] = {
29         "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
30         NULL
31 };
32
33 static const char btrfs_cmd_group_info[] =
34         "Use --help as an argument for information on a specific group or command.";
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 static int parse_one_token(const char *arg, const struct cmd_group *grp,
43                            const struct cmd_struct **cmd_ret)
44 {
45         const struct cmd_struct *cmd = grp->commands;
46         const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
47
48         for (; cmd->token; cmd++) {
49                 const char *rest;
50
51                 rest = skip_prefix(arg, cmd->token);
52                 if (!rest) {
53                         if (!prefixcmp(cmd->token, arg)) {
54                                 if (abbrev_cmd) {
55                                         /*
56                                          * If this is abbreviated, it is
57                                          * ambiguous. So when there is no
58                                          * exact match later, we need to
59                                          * error out.
60                                          */
61                                         ambiguous_cmd = abbrev_cmd;
62                                 }
63                                 abbrev_cmd = cmd;
64                         }
65                         continue;
66                 }
67                 if (*rest)
68                         continue;
69
70                 *cmd_ret = cmd;
71                 return 0;
72         }
73
74         if (ambiguous_cmd)
75                 return -2;
76
77         if (abbrev_cmd) {
78                 *cmd_ret = abbrev_cmd;
79                 return 0;
80         }
81
82         return -1;
83 }
84
85 static const struct cmd_struct *
86 parse_command_token(const char *arg, const struct cmd_group *grp)
87 {
88         const struct cmd_struct *cmd = NULL;
89
90         switch(parse_one_token(arg, grp, &cmd)) {
91         case -1:
92                 help_unknown_token(arg, grp);
93         case -2:
94                 help_ambiguous_token(arg, grp);
95         }
96
97         return cmd;
98 }
99
100 static void handle_help_options_next_level(const struct cmd_struct *cmd,
101                 int argc, char **argv)
102 {
103         if (argc < 2)
104                 return;
105
106         if (!strcmp(argv[1], "--help")) {
107                 if (cmd->next) {
108                         argc--;
109                         argv++;
110                         help_command_group(cmd->next, argc, argv);
111                 } else {
112                         usage_command(cmd, 1, 0);
113                 }
114
115                 exit(0);
116         }
117 }
118
119 int handle_command_group(const struct cmd_group *grp, int argc,
120                          char **argv)
121
122 {
123         const struct cmd_struct *cmd;
124
125         argc--;
126         argv++;
127         if (argc < 1) {
128                 usage_command_group(grp, 0, 0);
129                 exit(1);
130         }
131
132         cmd = parse_command_token(argv[0], grp);
133
134         handle_help_options_next_level(cmd, argc, argv);
135
136         fixup_argv0(argv, cmd->token);
137         return cmd->fn(argc, argv);
138 }
139
140 static const struct cmd_group btrfs_cmd_group;
141
142 static const char * const cmd_help_usage[] = {
143         "btrfs help [--full]",
144         "Display help information",
145         "",
146         "--full     display detailed help on every command",
147         NULL
148 };
149
150 static int cmd_help(int argc, char **argv)
151 {
152         help_command_group(&btrfs_cmd_group, argc, argv);
153         return 0;
154 }
155
156 static const char * const cmd_version_usage[] = {
157         "btrfs version",
158         "Display btrfs-progs version",
159         NULL
160 };
161
162 static int cmd_version(int argc, char **argv)
163 {
164         printf("%s\n", PACKAGE_STRING);
165         return 0;
166 }
167
168 /*
169  * Parse global options, between binary name and first non-option argument
170  * after processing all valid options (including those with arguments).
171  *
172  * Returns index to argv where parsting stopped, optind is reset to 1
173  */
174 static int handle_global_options(int argc, char **argv)
175 {
176         enum { OPT_HELP = 256, OPT_VERSION, OPT_FULL };
177         static const struct option long_options[] = {
178                 { "help", no_argument, NULL, OPT_HELP },
179                 { "version", no_argument, NULL, OPT_VERSION },
180                 { "full", no_argument, NULL, OPT_FULL },
181                 { NULL, 0, NULL, 0}
182         };
183         int shift;
184
185         if (argc == 0)
186                 return 0;
187
188         opterr = 0;
189         while (1) {
190                 int c;
191
192                 c = getopt_long(argc, argv, "+", long_options, NULL);
193                 if (c < 0)
194                         break;
195
196                 switch (c) {
197                 case OPT_HELP: break;
198                 case OPT_VERSION: break;
199                 case OPT_FULL: break;
200                 default:
201                         fprintf(stderr, "Unknown global option: %s\n",
202                                         argv[optind - 1]);
203                         exit(129);
204                 }
205         }
206
207         shift = optind;
208         optind = 1;
209
210         return shift;
211 }
212
213 void handle_special_globals(int shift, int argc, char **argv)
214 {
215         int has_help = 0;
216         int has_full = 0;
217         int i;
218
219         for (i = 0; i < shift; i++) {
220                 if (strcmp(argv[i], "--help") == 0)
221                         has_help = 1;
222                 else if (strcmp(argv[i], "--full") == 0)
223                         has_full = 1;
224         }
225
226         if (has_help) {
227                 if (has_full)
228                         usage_command_group(&btrfs_cmd_group, 1, 0);
229                 else
230                         cmd_help(argc, argv);
231                 exit(0);
232         }
233
234         for (i = 0; i < shift; i++)
235                 if (strcmp(argv[i], "--version") == 0) {
236                         cmd_version(argc, argv);
237                         exit(0);
238                 }
239 }
240
241 static const struct cmd_group btrfs_cmd_group = {
242         btrfs_cmd_group_usage, btrfs_cmd_group_info, {
243                 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
244                 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
245                 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
246                 { "device", cmd_device, NULL, &device_cmd_group, 0 },
247                 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
248                 { "check", cmd_check, cmd_check_usage, NULL, 0 },
249                 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
250                 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
251                 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
252                 { "property", cmd_property, NULL, &property_cmd_group, 0 },
253                 { "send", cmd_send, cmd_send_usage, NULL, 0 },
254                 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
255                 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
256                 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
257                 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
258                 { "help", cmd_help, cmd_help_usage, NULL, 0 },
259                 { "version", cmd_version, cmd_version_usage, NULL, 0 },
260                 NULL_CMD_STRUCT
261         },
262 };
263
264 int main(int argc, char **argv)
265 {
266         const struct cmd_struct *cmd;
267         const char *bname;
268         int ret;
269
270         btrfs_config_init();
271
272         if ((bname = strrchr(argv[0], '/')) != NULL)
273                 bname++;
274         else
275                 bname = argv[0];
276
277         if (!strcmp(bname, "btrfsck")) {
278                 argv[0] = "check";
279         } else {
280                 int shift;
281
282                 shift = handle_global_options(argc, argv);
283                 handle_special_globals(shift, argc, argv);
284                 while (shift-- > 0) {
285                         argc--;
286                         argv++;
287                 }
288                 if (argc == 0) {
289                         usage_command_group_short(&btrfs_cmd_group);
290                         exit(1);
291                 }
292         }
293
294         cmd = parse_command_token(argv[0], &btrfs_cmd_group);
295
296         handle_help_options_next_level(cmd, argc, argv);
297
298         crc32c_optimization_init();
299
300         fixup_argv0(argv, cmd->token);
301
302         ret = cmd->fn(argc, argv);
303
304         btrfs_close_all_devices();
305
306         exit(ret);
307 }