Btrfs-progs: add btrfs filesystem df to print space info
[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         { do_defrag, -1,
64           "filesystem defragment", "[-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
65                 "Defragment a file or a directory."
66         },
67         { do_set_default_subvol, 2,
68           "subvolume set-default", "<id> <path>\n"
69                 "Set the subvolume of the filesystem <path> which will be mounted\n"
70                 "as default."
71         },
72         { do_fssync, 1,
73           "filesystem sync", "<path>\n"
74                 "Force a sync on the filesystem <path>."
75         },
76         { do_resize, 2,
77           "filesystem resize", "[+/-]<newsize>[gkm]|max <filesystem>\n"
78                 "Resize the file system. If 'max' is passed, the filesystem\n"
79                 "will occupe all available space on the device."
80         },
81         { do_show_filesystem, 999,
82           "filesystem show", "[<uuid>|<label>]\n"
83                 "Show the info of a btrfs filesystem. If no <uuid> or <label>\n"
84                 "is passed, info of all the btrfs filesystem are shown."
85         },
86         { do_df_filesystem, 1,
87           "filesystem df", "<path>\n"
88                 "Show space usage information for a mount point\n."
89         },
90         { do_balance, 1,
91           "filesystem balance", "<path>\n"
92                 "Balance the chunks across the device."
93         },
94         { do_scan,
95           999, "device scan", "[<device> [<device>..]\n"
96                 "Scan all device for or the passed device for a btrfs\n"
97                 "filesystem."
98         },
99         { do_add_volume, -2,
100           "device add", "<dev> [<dev>..] <path>\n"
101                 "Add a device to a filesystem."
102         },
103         { do_remove_volume, -2,
104           "device delete", "<dev> [<dev>..] <path>\n"
105                 "Remove a device from a filesystem."
106         },
107         /* coming soon
108         { 2, "filesystem label", "<label> <path>\n"
109                 "Set the label of a filesystem"
110         }
111         */
112         { 0, 0 , 0 }
113 };
114
115 static char *get_prgname(char *programname)
116 {
117         char    *np;
118         np = strrchr(programname,'/');
119         if(!np)
120                 np = programname;
121         else
122                 np++;
123
124         return np;
125 }
126
127 static void print_help(char *programname, struct Command *cmd)
128 {
129         char    *pc;
130
131         printf("\t%s %s ", programname, cmd->verb );
132
133         for(pc = cmd->help; *pc; pc++){
134                 putchar(*pc);
135                 if(*pc == '\n')
136                         printf("\t\t");
137         }
138         putchar('\n');
139 }
140
141 static void help(char *np)
142 {
143         struct Command *cp;
144
145         printf("Usage:\n");
146         for( cp = commands; cp->verb; cp++ )
147                 print_help(np, cp);
148
149         printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
150         printf("\n%s\n", BTRFS_BUILD_VERSION);
151 }
152
153 static int split_command(char *cmd, char ***commands)
154 {
155         int     c, l;
156         char    *p, *s;
157
158         for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
159                 if ( *p && *p != ' ' )
160                         continue;
161
162                 /* c + 2 so that we have room for the null */
163                 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
164                 (*commands)[c] = strndup(s, l);
165                 c++;
166                 l = 0;
167                 s = p+1;
168                 if( !*p ) break;
169         }
170
171         (*commands)[c] = 0;
172         return c;
173 }
174
175 /*
176         This function checks if the passed command is ambiguous
177 */
178 static int check_ambiguity(struct Command *cmd, char **argv){
179         int             i;
180         struct Command  *cp;
181         /* check for ambiguity */
182         for( i = 0 ; i < cmd->ncmds ; i++ ){
183                 int match;
184                 for( match = 0, cp = commands; cp->verb; cp++ ){
185                         int     j, skip;
186                         char    *s1, *s2;
187
188                         if( cp->ncmds < i )
189                                 continue;
190
191                         for( skip = 0, j = 0 ; j < i ; j++ )
192                                 if( strcmp(cmd->cmds[j], cp->cmds[j])){
193                                         skip=1;
194                                         break;
195                                 }
196                         if(skip)
197                                 continue;
198
199                         if( !strcmp(cmd->cmds[i], cp->cmds[i]))
200                                 continue;
201                         for(s2 = cp->cmds[i], s1 = argv[i+1];
202                                 *s1 == *s2 && *s1; s1++, s2++ ) ;
203                         if( !*s1 )
204                                 match++;
205                 }
206                 if(match){
207                         int j;
208                         fprintf(stderr, "ERROR: in command '");
209                         for( j = 0 ; j <= i ; j++ )
210                                 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
211                         fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
212                         return -2;
213                 }
214         }
215         return 0;
216 }
217
218 /*
219  * This function, compacts the program name and the command in the first
220  * element of the '*av' array
221  */
222 static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
223
224         char    **ret;
225         int     i;
226         char    *newname;
227
228         ret = (char **)malloc(sizeof(char*)*(*ac+1));
229         newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
230         if( !ret || !newname ){
231                 free(ret);
232                 free(newname);
233                 return -1;
234         }
235
236         ret[0] = newname;
237         for(i=0; i < *ac ; i++ )
238                 ret[i+1] = (*av)[i];
239
240         strcpy(newname, prgname);
241         strcat(newname, " ");
242         strcat(newname, cmd->verb);
243
244         (*ac)++;
245         *av = ret;
246
247         return 0;
248
249 }
250
251
252
253 /*
254
255         This function perform the following jobs:
256         - show the help if '--help' or 'help' or '-h' are passed
257         - verify that a command is not ambiguous, otherwise show which
258           part of the command is ambiguous
259         - if after a (even partial) command there is '--help' show the help
260           for all the matching commands
261         - if the command doesn't' match show an error
262         - finally, if a command match, they return which command is matched and
263           the arguments
264
265         The function return 0 in case of help is requested; <0 in case
266         of uncorrect command; >0 in case of matching commands
267         argc, argv are the arg-counter and arg-vector (input)
268         *nargs_ is the number of the arguments after the command (output)
269         **cmd_  is the invoked command (output)
270         ***args_ are the arguments after the command
271
272 */
273 static int parse_args(int argc, char **argv,
274                       CommandFunction *func_,
275                       int *nargs_, char **cmd_, char ***args_ )
276 {
277         struct Command  *cp;
278         struct Command  *matchcmd=0;
279         char            *prgname = get_prgname(argv[0]);
280         int             i=0, helprequested=0;
281
282         if( argc < 2 || !strcmp(argv[1], "help") ||
283                 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
284                 help(prgname);
285                 return 0;
286         }
287
288         for( cp = commands; cp->verb; cp++ )
289                 if( !cp->ncmds)
290                         cp->ncmds = split_command(cp->verb, &(cp->cmds));
291
292         for( cp = commands; cp->verb; cp++ ){
293                 int     match;
294
295                 if( argc-1 < cp->ncmds )
296                         continue;
297                 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
298                         char    *s1, *s2;
299                         s1 = cp->cmds[i];
300                         s2 = argv[i+1];
301
302                         for(s2 = cp->cmds[i], s1 = argv[i+1];
303                                 *s1 == *s2 && *s1;
304                                 s1++, s2++ ) ;
305                         if( *s1 ){
306                                 match=0;
307                                 break;
308                         }
309                 }
310
311                 /* If you understand why this code works ...
312                         you are a genious !! */
313                 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
314                         if(!helprequested)
315                                 printf("Usage:\n");
316                         print_help(prgname, cp);
317                         helprequested=1;
318                         continue;
319                 }
320
321                 if(!match)
322                         continue;
323
324                 matchcmd = cp;
325                 *nargs_  = argc-matchcmd->ncmds-1;
326                 *cmd_ = matchcmd->verb;
327                 *args_ = argv+matchcmd->ncmds+1;
328                 *func_ = cp->func;
329
330                 break;
331         }
332
333         if(helprequested){
334                 printf("\n%s\n", BTRFS_BUILD_VERSION);
335                 return 0;
336         }
337
338         if(!matchcmd){
339                 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
340                 help(prgname);
341                 return -1;
342         }
343
344         if(check_ambiguity(matchcmd, argv))
345                 return -2;
346
347         /* check the number of argument */
348         if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
349                 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
350                         matchcmd->verb, -matchcmd->nargs);
351                         return -2;
352         }
353         if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
354                 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
355                         matchcmd->verb, matchcmd->nargs);
356                         return -2;
357         }
358         
359         if (prepare_args( nargs_, args_, prgname, matchcmd )){
360                 fprintf(stderr, "ERROR: not enough memory\\n");
361                 return -20;
362         }
363
364
365         return 1;
366 }
367 int main(int ac, char **av )
368 {
369
370         char            *cmd=0, **args=0;
371         int             nargs=0, r;
372         CommandFunction func=0;
373
374         r = parse_args(ac, av, &func, &nargs, &cmd, &args);
375         if( r <= 0 ){
376                 /* error or no command to parse*/
377                 exit(-r);
378         }
379
380         exit(func(nargs, args));
381
382 }
383