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", "[-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.",
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", "[-p] <path>\n"
70 "List the snapshot/subvolume of a filesystem.",
72 "List the snapshot/subvolume of a filesystem.\n"
75 { do_set_default_subvol, 2,
76 "subvolume set-default", "<id> <path>\n"
77 "Set the subvolume of the filesystem <path> which will be mounted\n"
81 { do_find_newer, 2, "subvolume find-new", "<path> <last_gen>\n"
82 "List the recently modified files in a filesystem.",
86 "filesystem defragment", "[-vf] [-c[zlib,lzo]] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
87 "Defragment a file or a directory.",
88 "[-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
89 "Defragment file data or directory metadata.\n"
91 "-c compress the file while defragmenting\n"
92 "-f flush data to disk immediately after defragmenting\n"
93 "-s start defragment only from byte onward\n"
94 "-l len defragment only up to len bytes\n"
95 "-t size minimal size of file to be considered for defragmenting\n"
97 { do_get_default_subvol, 1, "subvolume get-default", "<path>\n"
98 "Get the default subvolume of a filesystem."
101 "filesystem sync", "<path>\n"
102 "Force a sync on the filesystem <path>.",
106 "filesystem resize", "[+/-]<newsize>[gkm]|max <filesystem>\n"
107 "Resize the file system. If 'max' is passed, the filesystem\n"
108 "will occupe all available space on the device.",
111 { do_show_filesystem, 999,
112 "filesystem show", "[--all-devices][<uuid>|<label>]\n"
113 "Show the info of a btrfs filesystem. If no argument\n"
114 "is passed, info of all the btrfs filesystem are shown.",
117 { do_df_filesystem, 1,
118 "filesystem df", "<path>\n"
119 "Show space usage information for a mount point.",
123 "filesystem balance", "<path>\n"
124 "Balance the chunks across the device.",
127 { do_change_label, -1,
128 "filesystem label", "<device> [<newlabel>]\n"
129 "With one argument, get the label of filesystem on <device>.\n"
130 "If <newlabel> is passed, set the filesystem label to <newlabel>.\n"
131 "The filesystem must be unmounted.\n"
133 { do_scrub_start, -1,
134 "scrub start", "[-Bdqr] <path>|<device>\n"
135 "Start a new scrub.",
136 "\n-B do not background\n"
137 "-d stats per device (-B only)\n"
139 "-r read only mode\n"
141 { do_scrub_cancel, 1,
142 "scrub cancel", "<path>|<device>\n"
143 "Cancel a running scrub.",
146 { do_scrub_resume, -1,
147 "scrub resume", "[-Bdqr] <path>|<device>\n"
148 "Resume previously canceled or interrupted scrub.",
151 { do_scrub_status, -1,
152 "scrub status", "[-d] <path>|<device>\n"
153 "Show status of running or finished scrub.",
157 "device scan", "[<device>...]\n"
158 "Scan all device for or the passed device for a btrfs\n"
163 "device add", "<device> [<device>...] <path>\n"
164 "Add a device to a filesystem.",
167 { do_remove_volume, -2,
168 "device delete", "<device> [<device>...] <path>\n"
169 "Remove a device from a filesystem.",
175 static char *get_prgname(char *programname)
178 np = strrchr(programname,'/');
187 static void print_help(char *programname, struct Command *cmd, int helptype)
191 printf("\t%s %s ", programname, cmd->verb );
193 if (helptype == ADVANCED_HELP && cmd->adv_help)
194 for(pc = cmd->adv_help; *pc; pc++){
200 for(pc = cmd->help; *pc; pc++){
209 static void help(char *np)
214 for( cp = commands; cp->verb; cp++ )
215 print_help(np, cp, BASIC_HELP);
217 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
218 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or\n\t\t"
219 "subset of commands.\n",np);
220 printf("\n%s\n", BTRFS_BUILD_VERSION);
223 static int split_command(char *cmd, char ***commands)
228 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
229 if ( *p && *p != ' ' )
232 /* c + 2 so that we have room for the null */
233 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
234 (*commands)[c] = strndup(s, l);
246 This function checks if the passed command is ambiguous
248 static int check_ambiguity(struct Command *cmd, char **argv){
251 /* check for ambiguity */
252 for( i = 0 ; i < cmd->ncmds ; i++ ){
254 for( match = 0, cp = commands; cp->verb; cp++ ){
261 for( skip = 0, j = 0 ; j < i ; j++ )
262 if( strcmp(cmd->cmds[j], cp->cmds[j])){
269 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
271 for(s2 = cp->cmds[i], s1 = argv[i+1];
272 *s1 == *s2 && *s1; s1++, s2++ ) ;
278 fprintf(stderr, "ERROR: in command '");
279 for( j = 0 ; j <= i ; j++ )
280 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
281 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
289 * This function, compacts the program name and the command in the first
290 * element of the '*av' array
292 static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
298 ret = (char **)malloc(sizeof(char*)*(*ac+1));
299 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
300 if( !ret || !newname ){
307 for(i=0; i < *ac ; i++ )
310 strcpy(newname, prgname);
311 strcat(newname, " ");
312 strcat(newname, cmd->verb);
324 This function performs the following jobs:
325 - show the help if '--help' or 'help' or '-h' are passed
326 - verify that a command is not ambiguous, otherwise show which
327 part of the command is ambiguous
328 - if after a (even partial) command there is '--help' show detailed help
329 for all the matching commands
330 - if the command doesn't match show an error
331 - finally, if a command matches, they return which command matched and
334 The function return 0 in case of help is requested; <0 in case
335 of uncorrect command; >0 in case of matching commands
336 argc, argv are the arg-counter and arg-vector (input)
337 *nargs_ is the number of the arguments after the command (output)
338 **cmd_ is the invoked command (output)
339 ***args_ are the arguments after the command
342 static int parse_args(int argc, char **argv,
343 CommandFunction *func_,
344 int *nargs_, char **cmd_, char ***args_ )
347 struct Command *matchcmd=0;
348 char *prgname = get_prgname(argv[0]);
349 int i=0, helprequested=0;
351 if( argc < 2 || !strcmp(argv[1], "help") ||
352 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
357 for( cp = commands; cp->verb; cp++ )
359 cp->ncmds = split_command(cp->verb, &(cp->cmds));
361 for( cp = commands; cp->verb; cp++ ){
364 if( argc-1 < cp->ncmds )
366 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
371 for(s2 = cp->cmds[i], s1 = argv[i+1];
380 /* If you understand why this code works ...
381 you are a genious !! */
382 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
385 print_help(prgname, cp, ADVANCED_HELP);
394 *nargs_ = argc-matchcmd->ncmds-1;
395 *cmd_ = matchcmd->verb;
396 *args_ = argv+matchcmd->ncmds+1;
403 printf("\n%s\n", BTRFS_BUILD_VERSION);
408 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
413 if(check_ambiguity(matchcmd, argv))
416 /* check the number of argument */
417 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
418 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
419 matchcmd->verb, -matchcmd->nargs);
422 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
423 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
424 matchcmd->verb, matchcmd->nargs);
428 if (prepare_args( nargs_, args_, prgname, matchcmd )){
429 fprintf(stderr, "ERROR: not enough memory\\n");
436 int main(int ac, char **av )
439 char *cmd=0, **args=0;
441 CommandFunction func=0;
443 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
445 /* error or no command to parse*/
449 exit(func(nargs, args));