new util: 'btrfs'
[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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "btrfs_cmds.h"
23 #include "version.h"
24
25 typedef int (*CommandFunction)(int argc, char **argv);
26
27 struct Command {
28         CommandFunction func;   /* function which implements the command */
29         int     nargs;          /* if == 999, any number of arguments
30                                    if >= 0, number of arguments,
31                                    if < 0, _minimum_ number of arguments */
32         char    *verb;          /* verb */
33         char    *help;          /* help lines; form the 2nd onward they are
34                                    indented */
35
36         /* the following fields are run-time filled by the program */
37         char    **cmds;         /* array of subcommands */
38         int     ncmds;          /* number of subcommand */
39 };
40
41 static struct Command commands[] = {
42
43         /*
44                 avoid short commands different for the case only
45         */
46         { do_clone, 2,
47           "subvolume snapshot", "<source> [<dest>/]<name>\n"
48                 "Create a writable snapshot of the subvolume <source> with\n"
49                 "the name <name> in the <dest> directory."
50         },
51         { do_delete_subvolume, 1,
52           "subvolume delete", "<subvolume>\n"
53                 "Delete the subvolume <subvolume>."
54         },
55         { do_create_subvol, 1,
56           "subvolume create", "[<dest>/]<name>\n"
57                 "Create a subvolume in <dest> (or the current directory if\n"
58                 "not passed)."
59         },
60         { do_subvol_list, 1, "subvolume list", "<path>\n"
61                 "List the snapshot/subvolume of a filesystem."
62         },
63
64         { do_defrag, -1,
65           "filesystem defragment", "[-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
66                 "Defragment a file or a directory."
67         },
68         { do_fssync, 1,
69           "filesystem sync", "<path>\n"
70                 "Force a sync on the filesystem <path>."
71         },
72         { do_resize, 2,
73           "filesystem resize", "[+/-]<newsize>[gkm]|max <filesystem>\n"
74                 "Resize the file system. If 'max' is passed, the filesystem\n"
75                 "will occupe all available space on the device."
76         },
77         { do_show_filesystem, 999,
78           "filesystem show", "[<uuid>|<label>]\n"
79                 "Show the info of a btrfs filesystem. If no <uuid> or <label>\n"
80                 "is passed, info of all the btrfs filesystem are shown."
81         },
82         { do_balance, 1,
83           "filesystem balance", "<path>\n"
84                 "Balance the chunks across the device."
85         },
86         { do_scan,
87           999, "device scan", "[<device> [<device>..]\n"
88                 "Scan all device for or the passed device for a btrfs\n"
89                 "filesystem."
90         },
91         { do_add_volume, -1,
92           "device add", "<dev> [<dev>..] <path>\n"
93                 "Add a device to a filesystem."
94         },
95         { do_remove_volume, -1,
96           "device delete", "<dev> [<dev>..] <path>\n"
97                 "Remove a device from a filesystem."
98         },
99         /* coming soon
100         { 2, "filesystem label", "<label> <path>\n"
101                 "Set the label of a filesystem"
102         }
103         */
104         { 0, 0 , 0 }
105 };
106
107 static char *get_prgname(char *programname)
108 {
109         char    *np;
110         np = strrchr(programname,'/');
111         if(!np)
112                 np = programname;
113         else
114                 np++;
115
116         return np;
117 }
118
119 static void print_help(char *programname, struct Command *cmd)
120 {
121         char    *pc;
122
123         printf("\t%s %s ", programname, cmd->verb );
124
125         for(pc = cmd->help; *pc; pc++){
126                 putchar(*pc);
127                 if(*pc == '\n')
128                         printf("\t\t");
129         }
130         putchar('\n');
131 }
132
133 static void help(char *np)
134 {
135         struct Command *cp;
136
137         printf("Usage:\n");
138         for( cp = commands; cp->verb; cp++ )
139                 print_help(np, cp);
140
141         printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
142         printf("\n%s\n", BTRFS_BUILD_VERSION);
143 }
144
145 static int split_command(char *cmd, char ***commands)
146 {
147         int     c, l;
148         char    *p, *s;
149
150         for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
151                 if ( *p && *p != ' ' )
152                         continue;
153
154                 /* c + 2 so that we have room for the null */
155                 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
156                 (*commands)[c] = strndup(s, l);
157                 c++;
158                 l = 0;
159                 s = p+1;
160                 if( !*p ) break;
161         }
162
163         (*commands)[c] = 0;
164         return c;
165 }
166
167 /*
168         This function checks if the passed command is ambiguous
169 */
170 static int check_ambiguity(struct Command *cmd, char **argv){
171         int             i;
172         struct Command  *cp;
173         /* check for ambiguity */
174         for( i = 0 ; i < cmd->ncmds ; i++ ){
175                 int match;
176                 for( match = 0, cp = commands; cp->verb; cp++ ){
177                         int     j, skip;
178                         char    *s1, *s2;
179
180                         if( cp->ncmds < i )
181                                 continue;
182
183                         for( skip = 0, j = 0 ; j < i ; j++ )
184                                 if( strcmp(cmd->cmds[j], cp->cmds[j])){
185                                         skip=1;
186                                         break;
187                                 }
188                         if(skip)
189                                 continue;
190
191                         if( !strcmp(cmd->cmds[i], cp->cmds[i]))
192                                 continue;
193                         for(s2 = cp->cmds[i], s1 = argv[i+1];
194                                 *s1 == *s2 && *s1; s1++, s2++ ) ;
195                         if( !*s1 )
196                                 match++;
197                 }
198                 if(match){
199                         int j;
200                         fprintf(stderr, "ERROR: in command '");
201                         for( j = 0 ; j <= i ; j++ )
202                                 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
203                         fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
204                         return -2;
205                 }
206         }
207         return 0;
208 }
209
210 /*
211
212         This function perform the following jobs:
213         - show the help if '--help' or 'help' or '-h' are passed
214         - verify that a command is not ambiguous, otherwise show which
215           part of the command is ambiguous
216         - if after a (even partial) command there is '--help' show the help
217           for all the matching commands
218         - if the command doesn't' match show an error
219         - finally, if a command match, they return which command is matched and
220           the arguments
221
222         The function return 0 in case of help is requested; <0 in case
223         of uncorrect command; >0 in case of matching commands
224         argc, argv are the arg-counter and arg-vector (input)
225         *nargs_ is the number of the arguments after the command (output)
226         **cmd_  is the invoked command (output)
227         ***args_ are the arguments after the command
228
229 */
230 static int parse_args(int argc, char **argv,
231                       CommandFunction *func_,
232                       int *nargs_, char **cmd_, char ***args_ )
233 {
234         struct Command  *cp;
235         struct Command  *matchcmd=0;
236         char            *prgname = get_prgname(argv[0]);
237         int             i=0, helprequested=0;
238
239         if( argc < 2 || !strcmp(argv[1], "help") ||
240                 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
241                 help(prgname);
242                 return 0;
243         }
244
245         for( cp = commands; cp->verb; cp++ )
246                 if( !cp->ncmds)
247                         cp->ncmds = split_command(cp->verb, &(cp->cmds));
248
249         for( cp = commands; cp->verb; cp++ ){
250                 int     match;
251
252                 if( argc-1 < cp->ncmds )
253                         continue;
254                 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
255                         char    *s1, *s2;
256                         s1 = cp->cmds[i];
257                         s2 = argv[i+1];
258
259                         for(s2 = cp->cmds[i], s1 = argv[i+1];
260                                 *s1 == *s2 && *s1;
261                                 s1++, s2++ ) ;
262                         if( *s1 ){
263                                 match=0;
264                                 break;
265                         }
266                 }
267
268                 /* If you understand why this code works ...
269                         you are a genious !! */
270                 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
271                         if(!helprequested)
272                                 printf("Usage:\n");
273                         print_help(prgname, cp);
274                         helprequested=1;
275                         continue;
276                 }
277
278                 if(!match)
279                         continue;
280
281                 matchcmd = cp;
282                 *nargs_  = argc-matchcmd->ncmds-1;
283                 *cmd_ = matchcmd->verb;
284                 *args_ = argv+matchcmd->ncmds+1;
285                 *func_ = cp->func;
286
287                 break;
288         }
289
290         if(helprequested){
291                 printf("\n%s\n", BTRFS_BUILD_VERSION);
292                 return 0;
293         }
294
295         if(!matchcmd){
296                 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
297                 help(prgname);
298                 return -1;
299         }
300
301         if(check_ambiguity(matchcmd, argv))
302                 return -2;
303
304         /* check the number of argument */
305         if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
306                 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
307                         matchcmd->verb, -matchcmd->nargs);
308                         return -2;
309         }
310         if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999 ){
311                 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
312                         matchcmd->verb, matchcmd->nargs);
313                         return -2;
314         }
315
316         return 1;
317 }
318
319 int main(int ac, char **av )
320 {
321
322         char            *cmd=0, **args=0;
323         int             nargs=0, r;
324         CommandFunction func=0;
325
326         r = parse_args(ac, av, &func, &nargs, &cmd, &args);
327         if( r <= 0 ){
328                 /* error or no command to parse*/
329                 exit(-r);
330         }
331
332         exit(func(nargs, args));
333
334 }
335