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