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