Btrfs-progs: check out if the swap device
[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 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 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 const struct cmd_group btrfs_cmd_group;
188
189 static const char * const cmd_help_usage[] = {
190         "btrfs help [--full]",
191         "Dislay 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 int handle_options(int *argc, char ***argv)
216 {
217         char **orig_argv = *argv;
218
219         while (*argc > 0) {
220                 const char *arg = (*argv)[0];
221                 if (arg[0] != '-')
222                         break;
223
224                 if (!strcmp(arg, "--help")) {
225                         break;
226                 } else if (!strcmp(arg, "--version")) {
227                         break;
228                 } else {
229                         fprintf(stderr, "Unknown option: %s\n", arg);
230                         fprintf(stderr, "usage: %s\n",
231                                 btrfs_cmd_group.usagestr[0]);
232                         exit(129);
233                 }
234
235                 (*argv)++;
236                 (*argc)--;
237         }
238
239         return (*argv) - orig_argv;
240 }
241
242 const struct cmd_group btrfs_cmd_group = {
243         btrfs_cmd_group_usage, btrfs_cmd_group_info, {
244                 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
245                 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
246                 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
247                 { "device", cmd_device, NULL, &device_cmd_group, 0 },
248                 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
249                 { "check", cmd_check, cmd_check_usage, NULL, 0 },
250                 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
251                 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
252                 { "send", cmd_send, cmd_send_usage, NULL, 0 },
253                 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
254                 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
255                 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
256                 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
257                 { "help", cmd_help, cmd_help_usage, NULL, 0 },
258                 { "version", cmd_version, cmd_version_usage, NULL, 0 },
259                 { 0, 0, 0, 0, 0 }
260         },
261 };
262
263 int main(int argc, char **argv)
264 {
265         const struct cmd_struct *cmd;
266         const char *bname;
267
268         if ((bname = strrchr(argv[0], '/')) != NULL)
269                 bname++;
270         else
271                 bname = argv[0];
272
273         if (!strcmp(bname, "btrfsck")) {
274                 argv[0] = "check";
275         } else {
276                 argc--;
277                 argv++;
278                 handle_options(&argc, &argv);
279                 if (argc > 0) {
280                         if (!prefixcmp(argv[0], "--"))
281                                 argv[0] += 2;
282                 } else {
283                         usage_command_group(&btrfs_cmd_group, 0, 0);
284                         exit(1);
285                 }
286         }
287
288         cmd = parse_command_token(argv[0], &btrfs_cmd_group);
289
290         handle_help_options_next_level(cmd, argc, argv);
291
292         crc32c_optimization_init();
293
294         fixup_argv0(argv, cmd->token);
295         exit(cmd->fn(argc, argv));
296 }