Fix use after free in close_ctree
[btrfs-progs-unstable.git] / btrfs.c
blob46314cfed94661f3460cd0b72af4700c53b4287d
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.
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.
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.
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include "kerncompat.h"
23 #include "btrfs_cmds.h"
24 #include "version.h"
26 typedef int (*CommandFunction)(int argc, char **argv);
28 struct Command {
29 CommandFunction func; /* function which implements the command */
30 int nargs; /* if == 999, any number of arguments
31 if >= 0, number of arguments,
32 if < 0, _minimum_ number of arguments */
33 char *verb; /* verb */
34 char *help; /* help lines; form the 2nd onward they are
35 indented */
37 /* the following fields are run-time filled by the program */
38 char **cmds; /* array of subcommands */
39 int ncmds; /* number of subcommand */
42 static struct Command commands[] = {
45 avoid short commands different for the case only
47 { do_clone, 2,
48 "subvolume snapshot", "<source> [<dest>/]<name>\n"
49 "Create a writable snapshot of the subvolume <source> with\n"
50 "the name <name> in the <dest> directory."
52 { do_delete_subvolume, 1,
53 "subvolume delete", "<subvolume>\n"
54 "Delete the subvolume <subvolume>."
56 { do_create_subvol, 1,
57 "subvolume create", "[<dest>/]<name>\n"
58 "Create a subvolume in <dest> (or the current directory if\n"
59 "not passed)."
61 { do_subvol_list, 1, "subvolume list", "<path>\n"
62 "List the snapshot/subvolume of a filesystem."
64 { do_find_newer, 2, "subvolume find-new", "<path> <last_gen>\n"
65 "List the recently modified files in a filesystem."
67 { do_defrag, -1,
68 "filesystem defragment", "[-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
69 "Defragment a file or a directory."
71 { do_set_default_subvol, 2,
72 "subvolume set-default", "<id> <path>\n"
73 "Set the subvolume of the filesystem <path> which will be mounted\n"
74 "as default."
76 { do_fssync, 1,
77 "filesystem sync", "<path>\n"
78 "Force a sync on the filesystem <path>."
80 { do_resize, 2,
81 "filesystem resize", "[+/-]<newsize>[gkm]|max <filesystem>\n"
82 "Resize the file system. If 'max' is passed, the filesystem\n"
83 "will occupe all available space on the device."
85 { do_show_filesystem, 999,
86 "filesystem show", "[<uuid>|<label>]\n"
87 "Show the info of a btrfs filesystem. If no <uuid> or <label>\n"
88 "is passed, info of all the btrfs filesystem are shown."
90 { do_df_filesystem, 1,
91 "filesystem df", "<path>\n"
92 "Show space usage information for a mount point\n."
94 { do_balance, 1,
95 "filesystem balance", "<path>\n"
96 "Balance the chunks across the device."
98 { do_scan,
99 999, "device scan", "[<device> [<device>..]\n"
100 "Scan all device for or the passed device for a btrfs\n"
101 "filesystem."
103 { do_add_volume, -2,
104 "device add", "<dev> [<dev>..] <path>\n"
105 "Add a device to a filesystem."
107 { do_remove_volume, -2,
108 "device delete", "<dev> [<dev>..] <path>\n"
109 "Remove a device from a filesystem."
111 /* coming soon
112 { 2, "filesystem label", "<label> <path>\n"
113 "Set the label of a filesystem"
116 { 0, 0 , 0 }
119 static char *get_prgname(char *programname)
121 char *np;
122 np = strrchr(programname,'/');
123 if(!np)
124 np = programname;
125 else
126 np++;
128 return np;
131 static void print_help(char *programname, struct Command *cmd)
133 char *pc;
135 printf("\t%s %s ", programname, cmd->verb );
137 for(pc = cmd->help; *pc; pc++){
138 putchar(*pc);
139 if(*pc == '\n')
140 printf("\t\t");
142 putchar('\n');
145 static void help(char *np)
147 struct Command *cp;
149 printf("Usage:\n");
150 for( cp = commands; cp->verb; cp++ )
151 print_help(np, cp);
153 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
154 printf("\n%s\n", BTRFS_BUILD_VERSION);
157 static int split_command(char *cmd, char ***commands)
159 int c, l;
160 char *p, *s;
162 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
163 if ( *p && *p != ' ' )
164 continue;
166 /* c + 2 so that we have room for the null */
167 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
168 (*commands)[c] = strndup(s, l);
169 c++;
170 l = 0;
171 s = p+1;
172 if( !*p ) break;
175 (*commands)[c] = 0;
176 return c;
180 This function checks if the passed command is ambiguous
182 static int check_ambiguity(struct Command *cmd, char **argv){
183 int i;
184 struct Command *cp;
185 /* check for ambiguity */
186 for( i = 0 ; i < cmd->ncmds ; i++ ){
187 int match;
188 for( match = 0, cp = commands; cp->verb; cp++ ){
189 int j, skip;
190 char *s1, *s2;
192 if( cp->ncmds < i )
193 continue;
195 for( skip = 0, j = 0 ; j < i ; j++ )
196 if( strcmp(cmd->cmds[j], cp->cmds[j])){
197 skip=1;
198 break;
200 if(skip)
201 continue;
203 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
204 continue;
205 for(s2 = cp->cmds[i], s1 = argv[i+1];
206 *s1 == *s2 && *s1; s1++, s2++ ) ;
207 if( !*s1 )
208 match++;
210 if(match){
211 int j;
212 fprintf(stderr, "ERROR: in command '");
213 for( j = 0 ; j <= i ; j++ )
214 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
215 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
216 return -2;
219 return 0;
223 * This function, compacts the program name and the command in the first
224 * element of the '*av' array
226 static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
228 char **ret;
229 int i;
230 char *newname;
232 ret = (char **)malloc(sizeof(char*)*(*ac+1));
233 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
234 if( !ret || !newname ){
235 free(ret);
236 free(newname);
237 return -1;
240 ret[0] = newname;
241 for(i=0; i < *ac ; i++ )
242 ret[i+1] = (*av)[i];
244 strcpy(newname, prgname);
245 strcat(newname, " ");
246 strcat(newname, cmd->verb);
248 (*ac)++;
249 *av = ret;
251 return 0;
259 This function perform the following jobs:
260 - show the help if '--help' or 'help' or '-h' are passed
261 - verify that a command is not ambiguous, otherwise show which
262 part of the command is ambiguous
263 - if after a (even partial) command there is '--help' show the help
264 for all the matching commands
265 - if the command doesn't' match show an error
266 - finally, if a command match, they return which command is matched and
267 the arguments
269 The function return 0 in case of help is requested; <0 in case
270 of uncorrect command; >0 in case of matching commands
271 argc, argv are the arg-counter and arg-vector (input)
272 *nargs_ is the number of the arguments after the command (output)
273 **cmd_ is the invoked command (output)
274 ***args_ are the arguments after the command
277 static int parse_args(int argc, char **argv,
278 CommandFunction *func_,
279 int *nargs_, char **cmd_, char ***args_ )
281 struct Command *cp;
282 struct Command *matchcmd=0;
283 char *prgname = get_prgname(argv[0]);
284 int i=0, helprequested=0;
286 if( argc < 2 || !strcmp(argv[1], "help") ||
287 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
288 help(prgname);
289 return 0;
292 for( cp = commands; cp->verb; cp++ )
293 if( !cp->ncmds)
294 cp->ncmds = split_command(cp->verb, &(cp->cmds));
296 for( cp = commands; cp->verb; cp++ ){
297 int match;
299 if( argc-1 < cp->ncmds )
300 continue;
301 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
302 char *s1, *s2;
303 s1 = cp->cmds[i];
304 s2 = argv[i+1];
306 for(s2 = cp->cmds[i], s1 = argv[i+1];
307 *s1 == *s2 && *s1;
308 s1++, s2++ ) ;
309 if( *s1 ){
310 match=0;
311 break;
315 /* If you understand why this code works ...
316 you are a genious !! */
317 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
318 if(!helprequested)
319 printf("Usage:\n");
320 print_help(prgname, cp);
321 helprequested=1;
322 continue;
325 if(!match)
326 continue;
328 matchcmd = cp;
329 *nargs_ = argc-matchcmd->ncmds-1;
330 *cmd_ = matchcmd->verb;
331 *args_ = argv+matchcmd->ncmds+1;
332 *func_ = cp->func;
334 break;
337 if(helprequested){
338 printf("\n%s\n", BTRFS_BUILD_VERSION);
339 return 0;
342 if(!matchcmd){
343 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
344 help(prgname);
345 return -1;
348 if(check_ambiguity(matchcmd, argv))
349 return -2;
351 /* check the number of argument */
352 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
353 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
354 matchcmd->verb, -matchcmd->nargs);
355 return -2;
357 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
358 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
359 matchcmd->verb, matchcmd->nargs);
360 return -2;
363 if (prepare_args( nargs_, args_, prgname, matchcmd )){
364 fprintf(stderr, "ERROR: not enough memory\\n");
365 return -20;
369 return 1;
371 int main(int ac, char **av )
374 char *cmd=0, **args=0;
375 int nargs=0, r;
376 CommandFunction func=0;
378 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
379 if( r <= 0 ){
380 /* error or no command to parse*/
381 exit(-r);
384 exit(func(nargs, args));