make btrfs cross compilation friendly
[btrfs-progs-unstable/devel.git] / btrfs.c
blobff84260c5e42474f9748fb2052075af3320e48ba
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 #define BASIC_HELP 0
27 #define ADVANCED_HELP 1
29 typedef int (*CommandFunction)(int argc, char **argv);
31 struct Command {
32 CommandFunction func; /* function which implements the command */
33 int nargs; /* if == 999, any number of arguments
34 if >= 0, number of arguments,
35 if < 0, _minimum_ number of arguments */
36 char *verb; /* verb */
37 char *help; /* help lines; from the 2nd line onward they
38 are automatically indented */
39 char *adv_help; /* advanced help message; from the 2nd line
40 onward they are automatically indented */
42 /* the following fields are run-time filled by the program */
43 char **cmds; /* array of subcommands */
44 int ncmds; /* number of subcommand */
47 static struct Command commands[] = {
50 avoid short commands different for the case only
52 { do_clone, -2,
53 "subvolume snapshot", "[-r] <source> [<dest>/]<name>\n"
54 "Create a writable/readonly snapshot of the subvolume <source> with\n"
55 "the name <name> in the <dest> directory.",
56 NULL
58 { do_delete_subvolume, 1,
59 "subvolume delete", "<subvolume>\n"
60 "Delete the subvolume <subvolume>.",
61 NULL
63 { do_create_subvol, 1,
64 "subvolume create", "[<dest>/]<name>\n"
65 "Create a subvolume in <dest> (or the current directory if\n"
66 "not passed).",
67 NULL
69 { do_subvol_list, 1, "subvolume list", "<path>\n"
70 "List the snapshot/subvolume of a filesystem.",
71 NULL
73 { do_set_default_subvol, 2,
74 "subvolume set-default", "<id> <path>\n"
75 "Set the subvolume of the filesystem <path> which will be mounted\n"
76 "as default.",
77 NULL
79 { do_find_newer, 2, "subvolume find-new", "<path> <last_gen>\n"
80 "List the recently modified files in a filesystem.",
81 NULL
83 { do_defrag, -1,
84 "filesystem defragment", "[-vf] [-c[zlib,lzo]] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
85 "Defragment a file or a directory.",
86 "[-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
87 "Defragment file data or directory metadata.\n"
88 "-v be verbose\n"
89 "-c compress the file while defragmenting\n"
90 "-f flush data to disk immediately after defragmenting\n"
91 "-s start defragment only from byte onward\n"
92 "-l len defragment only up to len bytes\n"
93 "-t size minimal size of file to be considered for defragmenting\n"
95 { do_fssync, 1,
96 "filesystem sync", "<path>\n"
97 "Force a sync on the filesystem <path>.",
98 NULL
100 { do_resize, 2,
101 "filesystem resize", "[+/-]<newsize>[gkm]|max <filesystem>\n"
102 "Resize the file system. If 'max' is passed, the filesystem\n"
103 "will occupe all available space on the device.",
104 NULL
106 { do_show_filesystem, 999,
107 "filesystem show", "[<device>|<uuid>|<label>]\n"
108 "Show the info of a btrfs filesystem. If no argument\n"
109 "is passed, info of all the btrfs filesystem are shown.",
110 NULL
112 { do_df_filesystem, 1,
113 "filesystem df", "<path>\n"
114 "Show space usage information for a mount point.",
115 NULL
117 { do_balance, 1,
118 "filesystem balance", "<path>\n"
119 "Balance the chunks across the device.",
120 NULL
122 { do_change_label, -1,
123 "filesystem label", "<device> [<newlabel>]\n"
124 "With one argument, get the label of filesystem on <device>.\n"
125 "If <newlabel> is passed, set the filesystem label to <newlabel>.\n"
126 "The filesystem must be unmounted.\n"
128 { do_scan, 999,
129 "device scan", "[<device>...]\n"
130 "Scan all device for or the passed device for a btrfs\n"
131 "filesystem.",
132 NULL
134 { do_add_volume, -2,
135 "device add", "<device> [<device>...] <path>\n"
136 "Add a device to a filesystem.",
137 NULL
139 { do_remove_volume, -2,
140 "device delete", "<device> [<device>...] <path>\n"
141 "Remove a device from a filesystem.",
142 NULL
144 { 0, 0, 0, 0 }
147 static char *get_prgname(char *programname)
149 char *np;
150 np = strrchr(programname,'/');
151 if(!np)
152 np = programname;
153 else
154 np++;
156 return np;
159 static void print_help(char *programname, struct Command *cmd, int helptype)
161 char *pc;
163 printf("\t%s %s ", programname, cmd->verb );
165 if (helptype == ADVANCED_HELP && cmd->adv_help)
166 for(pc = cmd->adv_help; *pc; pc++){
167 putchar(*pc);
168 if(*pc == '\n')
169 printf("\t\t");
171 else
172 for(pc = cmd->help; *pc; pc++){
173 putchar(*pc);
174 if(*pc == '\n')
175 printf("\t\t");
178 putchar('\n');
181 static void help(char *np)
183 struct Command *cp;
185 printf("Usage:\n");
186 for( cp = commands; cp->verb; cp++ )
187 print_help(np, cp, BASIC_HELP);
189 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
190 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or\n\t\t"
191 "subset of commands.\n",np);
192 printf("\n%s\n", BTRFS_BUILD_VERSION);
195 static int split_command(char *cmd, char ***commands)
197 int c, l;
198 char *p, *s;
200 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
201 if ( *p && *p != ' ' )
202 continue;
204 /* c + 2 so that we have room for the null */
205 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
206 (*commands)[c] = strndup(s, l);
207 c++;
208 l = 0;
209 s = p+1;
210 if( !*p ) break;
213 (*commands)[c] = 0;
214 return c;
218 This function checks if the passed command is ambiguous
220 static int check_ambiguity(struct Command *cmd, char **argv){
221 int i;
222 struct Command *cp;
223 /* check for ambiguity */
224 for( i = 0 ; i < cmd->ncmds ; i++ ){
225 int match;
226 for( match = 0, cp = commands; cp->verb; cp++ ){
227 int j, skip;
228 char *s1, *s2;
230 if( cp->ncmds < i )
231 continue;
233 for( skip = 0, j = 0 ; j < i ; j++ )
234 if( strcmp(cmd->cmds[j], cp->cmds[j])){
235 skip=1;
236 break;
238 if(skip)
239 continue;
241 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
242 continue;
243 for(s2 = cp->cmds[i], s1 = argv[i+1];
244 *s1 == *s2 && *s1; s1++, s2++ ) ;
245 if( !*s1 )
246 match++;
248 if(match){
249 int j;
250 fprintf(stderr, "ERROR: in command '");
251 for( j = 0 ; j <= i ; j++ )
252 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
253 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
254 return -2;
257 return 0;
261 * This function, compacts the program name and the command in the first
262 * element of the '*av' array
264 static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
266 char **ret;
267 int i;
268 char *newname;
270 ret = (char **)malloc(sizeof(char*)*(*ac+1));
271 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
272 if( !ret || !newname ){
273 free(ret);
274 free(newname);
275 return -1;
278 ret[0] = newname;
279 for(i=0; i < *ac ; i++ )
280 ret[i+1] = (*av)[i];
282 strcpy(newname, prgname);
283 strcat(newname, " ");
284 strcat(newname, cmd->verb);
286 (*ac)++;
287 *av = ret;
289 return 0;
296 This function performs the following jobs:
297 - show the help if '--help' or 'help' or '-h' are passed
298 - verify that a command is not ambiguous, otherwise show which
299 part of the command is ambiguous
300 - if after a (even partial) command there is '--help' show detailed help
301 for all the matching commands
302 - if the command doesn't match show an error
303 - finally, if a command matches, they return which command matched and
304 the arguments
306 The function return 0 in case of help is requested; <0 in case
307 of uncorrect command; >0 in case of matching commands
308 argc, argv are the arg-counter and arg-vector (input)
309 *nargs_ is the number of the arguments after the command (output)
310 **cmd_ is the invoked command (output)
311 ***args_ are the arguments after the command
314 static int parse_args(int argc, char **argv,
315 CommandFunction *func_,
316 int *nargs_, char **cmd_, char ***args_ )
318 struct Command *cp;
319 struct Command *matchcmd=0;
320 char *prgname = get_prgname(argv[0]);
321 int i=0, helprequested=0;
323 if( argc < 2 || !strcmp(argv[1], "help") ||
324 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
325 help(prgname);
326 return 0;
329 for( cp = commands; cp->verb; cp++ )
330 if( !cp->ncmds)
331 cp->ncmds = split_command(cp->verb, &(cp->cmds));
333 for( cp = commands; cp->verb; cp++ ){
334 int match;
336 if( argc-1 < cp->ncmds )
337 continue;
338 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
339 char *s1, *s2;
340 s1 = cp->cmds[i];
341 s2 = argv[i+1];
343 for(s2 = cp->cmds[i], s1 = argv[i+1];
344 *s1 == *s2 && *s1;
345 s1++, s2++ ) ;
346 if( *s1 ){
347 match=0;
348 break;
352 /* If you understand why this code works ...
353 you are a genious !! */
354 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
355 if(!helprequested)
356 printf("Usage:\n");
357 print_help(prgname, cp, ADVANCED_HELP);
358 helprequested=1;
359 continue;
362 if(!match)
363 continue;
365 matchcmd = cp;
366 *nargs_ = argc-matchcmd->ncmds-1;
367 *cmd_ = matchcmd->verb;
368 *args_ = argv+matchcmd->ncmds+1;
369 *func_ = cp->func;
371 break;
374 if(helprequested){
375 printf("\n%s\n", BTRFS_BUILD_VERSION);
376 return 0;
379 if(!matchcmd){
380 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
381 help(prgname);
382 return -1;
385 if(check_ambiguity(matchcmd, argv))
386 return -2;
388 /* check the number of argument */
389 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
390 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
391 matchcmd->verb, -matchcmd->nargs);
392 return -2;
394 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
395 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
396 matchcmd->verb, matchcmd->nargs);
397 return -2;
400 if (prepare_args( nargs_, args_, prgname, matchcmd )){
401 fprintf(stderr, "ERROR: not enough memory\\n");
402 return -20;
406 return 1;
408 int main(int ac, char **av )
411 char *cmd=0, **args=0;
412 int nargs=0, r;
413 CommandFunction func=0;
415 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
416 if( r <= 0 ){
417 /* error or no command to parse*/
418 exit(-r);
421 exit(func(nargs, args));