-
Notifications
You must be signed in to change notification settings - Fork 2
/
btrfs.c
283 lines (232 loc) · 6.07 KB
/
btrfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crc32c.h"
#include "commands.h"
#include "version.h"
static const char * const btrfs_cmd_group_usage[] = {
"btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
NULL
};
static const char btrfs_cmd_group_info[] =
"Use --help as an argument for information on a specific group or command.";
char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
static inline const char *skip_prefix(const char *str, const char *prefix)
{
size_t len = strlen(prefix);
return strncmp(str, prefix, len) ? NULL : str + len;
}
int prefixcmp(const char *str, const char *prefix)
{
for (; ; str++, prefix++)
if (!*prefix)
return 0;
else if (*str != *prefix)
return (unsigned char)*prefix - (unsigned char)*str;
}
static int parse_one_token(const char *arg, const struct cmd_group *grp,
const struct cmd_struct **cmd_ret)
{
const struct cmd_struct *cmd = grp->commands;
const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
for (; cmd->token; cmd++) {
const char *rest;
rest = skip_prefix(arg, cmd->token);
if (!rest) {
if (!prefixcmp(cmd->token, arg)) {
if (abbrev_cmd) {
/*
* If this is abbreviated, it is
* ambiguous. So when there is no
* exact match later, we need to
* error out.
*/
ambiguous_cmd = abbrev_cmd;
}
abbrev_cmd = cmd;
}
continue;
}
if (*rest)
continue;
*cmd_ret = cmd;
return 0;
}
if (ambiguous_cmd)
return -2;
if (abbrev_cmd) {
*cmd_ret = abbrev_cmd;
return 0;
}
return -1;
}
static const struct cmd_struct *
parse_command_token(const char *arg, const struct cmd_group *grp)
{
const struct cmd_struct *cmd = NULL;
switch(parse_one_token(arg, grp, &cmd)) {
case -1:
help_unknown_token(arg, grp);
case -2:
help_ambiguous_token(arg, grp);
}
return cmd;
}
void handle_help_options_next_level(const struct cmd_struct *cmd,
int argc, char **argv)
{
if (argc < 2)
return;
if (!strcmp(argv[1], "--help")) {
if (cmd->next) {
argc--;
argv++;
help_command_group(cmd->next, argc, argv);
} else {
usage_command(cmd, 1, 0);
}
exit(0);
}
}
static void fixup_argv0(char **argv, const char *token)
{
int len = strlen(argv0_buf);
snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token);
argv[0] = argv0_buf;
}
int handle_command_group(const struct cmd_group *grp, int argc,
char **argv)
{
const struct cmd_struct *cmd;
argc--;
argv++;
if (argc < 1) {
usage_command_group(grp, 0, 0);
exit(1);
}
cmd = parse_command_token(argv[0], grp);
handle_help_options_next_level(cmd, argc, argv);
fixup_argv0(argv, cmd->token);
return cmd->fn(argc, argv);
}
int check_argc_exact(int nargs, int expected)
{
if (nargs < expected)
fprintf(stderr, "%s: too few arguments\n", argv0_buf);
if (nargs > expected)
fprintf(stderr, "%s: too many arguments\n", argv0_buf);
return nargs != expected;
}
int check_argc_min(int nargs, int expected)
{
if (nargs < expected) {
fprintf(stderr, "%s: too few arguments\n", argv0_buf);
return 1;
}
return 0;
}
int check_argc_max(int nargs, int expected)
{
if (nargs > expected) {
fprintf(stderr, "%s: too many arguments\n", argv0_buf);
return 1;
}
return 0;
}
const struct cmd_group btrfs_cmd_group;
static const char * const cmd_help_usage[] = {
"btrfs help [--full]",
"Dislay help information",
"",
"--full display detailed help on every command",
NULL
};
static int cmd_help(int argc, char **argv)
{
help_command_group(&btrfs_cmd_group, argc, argv);
return 0;
}
static const char * const cmd_version_usage[] = {
"btrfs version",
"Display btrfs-progs version",
NULL
};
static int cmd_version(int argc, char **argv)
{
printf("%s\n", BTRFS_BUILD_VERSION);
return 0;
}
static int handle_options(int *argc, char ***argv)
{
char **orig_argv = *argv;
while (*argc > 0) {
const char *arg = (*argv)[0];
if (arg[0] != '-')
break;
if (!strcmp(arg, "--help")) {
break;
} else if (!strcmp(arg, "--version")) {
break;
} else {
fprintf(stderr, "Unknown option: %s\n", arg);
fprintf(stderr, "usage: %s\n",
btrfs_cmd_group.usagestr[0]);
exit(129);
}
(*argv)++;
(*argc)--;
}
return (*argv) - orig_argv;
}
const struct cmd_group btrfs_cmd_group = {
btrfs_cmd_group_usage, btrfs_cmd_group_info, {
{ "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
{ "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
{ "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
{ "device", cmd_device, NULL, &device_cmd_group, 0 },
{ "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
{ "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
{ "send", cmd_send, NULL, &send_cmd_group, 0 },
{ "receive", cmd_receive, NULL, &receive_cmd_group, 0 },
{ "quota", cmd_quota, NULL, "a_cmd_group, 0 },
{ "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
{ "help", cmd_help, cmd_help_usage, NULL, 0 },
{ "version", cmd_version, cmd_version_usage, NULL, 0 },
{ 0, 0, 0, 0, 0 }
},
};
int main(int argc, char **argv)
{
const struct cmd_struct *cmd;
crc32c_optimization_init();
argc--;
argv++;
handle_options(&argc, &argv);
if (argc > 0) {
if (!prefixcmp(argv[0], "--"))
argv[0] += 2;
} else {
usage_command_group(&btrfs_cmd_group, 0, 0);
exit(1);
}
cmd = parse_command_token(argv[0], &btrfs_cmd_group);
handle_help_options_next_level(cmd, argc, argv);
fixup_argv0(argv, cmd->token);
exit(cmd->fn(argc, argv));
}