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 "btrfs_cmds.h"
25 typedef int (*CommandFunction)(int argc, char **argv);
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
36 /* the following fields are run-time filled by the program */
37 char **cmds; /* array of subcommands */
38 int ncmds; /* number of subcommand */
41 static struct Command commands[] = {
44 avoid short commands different for the case only
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."
51 { do_delete_subvolume, 1,
52 "subvolume delete", "<subvolume>\n"
53 "Delete the subvolume <subvolume>."
55 { do_create_subvol, 1,
56 "subvolume create", "[<dest>/]<name>\n"
57 "Create a subvolume in <dest> (or the current directory if\n"
60 { do_subvol_list, 1, "subvolume list", "<path>\n"
61 "List the snapshot/subvolume of a filesystem."
64 "filesystem defragment", "[-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
65 "Defragment a file or a directory."
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"
73 "filesystem sync", "<path>\n"
74 "Force a sync on the filesystem <path>."
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."
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."
86 { do_df_filesystem, 1,
87 "filesystem df", "<path>\n"
88 "Show space usage information for a mount point\n."
91 "filesystem balance", "<path>\n"
92 "Balance the chunks across the device."
95 999, "device scan", "[<device> [<device>..]\n"
96 "Scan all device for or the passed device for a btrfs\n"
100 "device add", "<dev> [<dev>..] <path>\n"
101 "Add a device to a filesystem."
103 { do_remove_volume, -2,
104 "device delete", "<dev> [<dev>..] <path>\n"
105 "Remove a device from a filesystem."
108 { 2, "filesystem label", "<label> <path>\n"
109 "Set the label of a filesystem"
115 static char *get_prgname(char *programname)
118 np = strrchr(programname,'/');
127 static void print_help(char *programname, struct Command *cmd)
131 printf("\t%s %s ", programname, cmd->verb );
133 for(pc = cmd->help; *pc; pc++){
141 static void help(char *np)
146 for( cp = commands; cp->verb; cp++ )
149 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
150 printf("\n%s\n", BTRFS_BUILD_VERSION);
153 static int split_command(char *cmd, char ***commands)
158 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
159 if ( *p && *p != ' ' )
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);
176 This function checks if the passed command is ambiguous
178 static int check_ambiguity(struct Command *cmd, char **argv){
181 /* check for ambiguity */
182 for( i = 0 ; i < cmd->ncmds ; i++ ){
184 for( match = 0, cp = commands; cp->verb; cp++ ){
191 for( skip = 0, j = 0 ; j < i ; j++ )
192 if( strcmp(cmd->cmds[j], cp->cmds[j])){
199 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
201 for(s2 = cp->cmds[i], s1 = argv[i+1];
202 *s1 == *s2 && *s1; s1++, s2++ ) ;
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]);
219 * This function, compacts the program name and the command in the first
220 * element of the '*av' array
222 static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
228 ret = (char **)malloc(sizeof(char*)*(*ac+1));
229 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
230 if( !ret || !newname ){
237 for(i=0; i < *ac ; i++ )
240 strcpy(newname, prgname);
241 strcat(newname, " ");
242 strcat(newname, cmd->verb);
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
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
273 static int parse_args(int argc, char **argv,
274 CommandFunction *func_,
275 int *nargs_, char **cmd_, char ***args_ )
278 struct Command *matchcmd=0;
279 char *prgname = get_prgname(argv[0]);
280 int i=0, helprequested=0;
282 if( argc < 2 || !strcmp(argv[1], "help") ||
283 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
288 for( cp = commands; cp->verb; cp++ )
290 cp->ncmds = split_command(cp->verb, &(cp->cmds));
292 for( cp = commands; cp->verb; cp++ ){
295 if( argc-1 < cp->ncmds )
297 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
302 for(s2 = cp->cmds[i], s1 = argv[i+1];
311 /* If you understand why this code works ...
312 you are a genious !! */
313 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
316 print_help(prgname, cp);
325 *nargs_ = argc-matchcmd->ncmds-1;
326 *cmd_ = matchcmd->verb;
327 *args_ = argv+matchcmd->ncmds+1;
334 printf("\n%s\n", BTRFS_BUILD_VERSION);
339 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
344 if(check_ambiguity(matchcmd, argv))
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);
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);
359 if (prepare_args( nargs_, args_, prgname, matchcmd )){
360 fprintf(stderr, "ERROR: not enough memory\\n");
367 int main(int ac, char **av )
370 char *cmd=0, **args=0;
372 CommandFunction func=0;
374 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
376 /* error or no command to parse*/
380 exit(func(nargs, args));