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.
22 #include "kerncompat.h"
23 #include "btrfs_cmds.h"
27 #define ADVANCED_HELP 1
29 typedef int (*CommandFunction)(int argc, char **argv);
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
53 "subvolume snapshot", "<source> [<dest>/]<name>\n"
54 "Create a writable snapshot of the subvolume <source> with\n"
55 "the name <name> in the <dest> directory.",
58 { do_delete_subvolume, 1,
59 "subvolume delete", "<subvolume>\n"
60 "Delete the subvolume <subvolume>.",
63 { do_create_subvol, 1,
64 "subvolume create", "[<dest>/]<name>\n"
65 "Create a subvolume in <dest> (or the current directory if\n"
69 { do_subvol_list, 1, "subvolume list", "<path>\n"
70 "List the snapshot/subvolume of a filesystem.",
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"
79 { do_find_newer, 2, "subvolume find-new", "<path> <last_gen>\n"
80 "List the recently modified files in a filesystem.",
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"
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"
96 "filesystem sync", "<path>\n"
97 "Force a sync on the filesystem <path>.",
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.",
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.",
112 { do_df_filesystem, 1,
113 "filesystem df", "<path>\n"
114 "Show space usage information for a mount point.",
118 "filesystem balance", "<path>\n"
119 "Balance the chunks across the device.",
123 "device scan", "[<device>...]\n"
124 "Scan all device for or the passed device for a btrfs\n"
129 "device add", "<device> [<device>...] <path>\n"
130 "Add a device to a filesystem.",
133 { do_remove_volume, -2,
134 "device delete", "<device> [<device>...] <path>\n"
135 "Remove a device from a filesystem.",
141 static char *get_prgname(char *programname)
144 np = strrchr(programname,'/');
153 static void print_help(char *programname, struct Command *cmd, int helptype)
157 printf("\t%s %s ", programname, cmd->verb );
159 if (helptype == ADVANCED_HELP && cmd->adv_help)
160 for(pc = cmd->adv_help; *pc; pc++){
166 for(pc = cmd->help; *pc; pc++){
175 static void help(char *np)
180 for( cp = commands; cp->verb; cp++ )
181 print_help(np, cp, BASIC_HELP);
183 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
184 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or\n\t\t"
185 "subset of commands.\n",np);
186 printf("\n%s\n", BTRFS_BUILD_VERSION);
189 static int split_command(char *cmd, char ***commands)
194 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
195 if ( *p && *p != ' ' )
198 /* c + 2 so that we have room for the null */
199 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
200 (*commands)[c] = strndup(s, l);
212 This function checks if the passed command is ambiguous
214 static int check_ambiguity(struct Command *cmd, char **argv){
217 /* check for ambiguity */
218 for( i = 0 ; i < cmd->ncmds ; i++ ){
220 for( match = 0, cp = commands; cp->verb; cp++ ){
227 for( skip = 0, j = 0 ; j < i ; j++ )
228 if( strcmp(cmd->cmds[j], cp->cmds[j])){
235 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
237 for(s2 = cp->cmds[i], s1 = argv[i+1];
238 *s1 == *s2 && *s1; s1++, s2++ ) ;
244 fprintf(stderr, "ERROR: in command '");
245 for( j = 0 ; j <= i ; j++ )
246 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
247 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
255 * This function, compacts the program name and the command in the first
256 * element of the '*av' array
258 static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
264 ret = (char **)malloc(sizeof(char*)*(*ac+1));
265 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
266 if( !ret || !newname ){
273 for(i=0; i < *ac ; i++ )
276 strcpy(newname, prgname);
277 strcat(newname, " ");
278 strcat(newname, cmd->verb);
290 This function performs the following jobs:
291 - show the help if '--help' or 'help' or '-h' are passed
292 - verify that a command is not ambiguous, otherwise show which
293 part of the command is ambiguous
294 - if after a (even partial) command there is '--help' show detailed help
295 for all the matching commands
296 - if the command doesn't match show an error
297 - finally, if a command matches, they return which command matched and
300 The function return 0 in case of help is requested; <0 in case
301 of uncorrect command; >0 in case of matching commands
302 argc, argv are the arg-counter and arg-vector (input)
303 *nargs_ is the number of the arguments after the command (output)
304 **cmd_ is the invoked command (output)
305 ***args_ are the arguments after the command
308 static int parse_args(int argc, char **argv,
309 CommandFunction *func_,
310 int *nargs_, char **cmd_, char ***args_ )
313 struct Command *matchcmd=0;
314 char *prgname = get_prgname(argv[0]);
315 int i=0, helprequested=0;
317 if( argc < 2 || !strcmp(argv[1], "help") ||
318 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
323 for( cp = commands; cp->verb; cp++ )
325 cp->ncmds = split_command(cp->verb, &(cp->cmds));
327 for( cp = commands; cp->verb; cp++ ){
330 if( argc-1 < cp->ncmds )
332 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
337 for(s2 = cp->cmds[i], s1 = argv[i+1];
346 /* If you understand why this code works ...
347 you are a genious !! */
348 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
351 print_help(prgname, cp, ADVANCED_HELP);
360 *nargs_ = argc-matchcmd->ncmds-1;
361 *cmd_ = matchcmd->verb;
362 *args_ = argv+matchcmd->ncmds+1;
369 printf("\n%s\n", BTRFS_BUILD_VERSION);
374 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
379 if(check_ambiguity(matchcmd, argv))
382 /* check the number of argument */
383 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
384 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
385 matchcmd->verb, -matchcmd->nargs);
388 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
389 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
390 matchcmd->verb, matchcmd->nargs);
394 if (prepare_args( nargs_, args_, prgname, matchcmd )){
395 fprintf(stderr, "ERROR: not enough memory\\n");
402 int main(int ac, char **av )
405 char *cmd=0, **args=0;
407 CommandFunction func=0;
409 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
411 /* error or no command to parse*/
415 exit(func(nargs, args));