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."
87 "filesystem balance", "<path>\n"
88 "Balance the chunks across the device."
91 999, "device scan", "[<device> [<device>..]\n"
92 "Scan all device for or the passed device for a btrfs\n"
96 "device add", "<dev> [<dev>..] <path>\n"
97 "Add a device to a filesystem."
99 { do_remove_volume, -2,
100 "device delete", "<dev> [<dev>..] <path>\n"
101 "Remove a device from a filesystem."
104 { 2, "filesystem label", "<label> <path>\n"
105 "Set the label of a filesystem"
111 static char *get_prgname(char *programname)
114 np = strrchr(programname,'/');
123 static void print_help(char *programname, struct Command *cmd)
127 printf("\t%s %s ", programname, cmd->verb );
129 for(pc = cmd->help; *pc; pc++){
137 static void help(char *np)
142 for( cp = commands; cp->verb; cp++ )
145 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
146 printf("\n%s\n", BTRFS_BUILD_VERSION);
149 static int split_command(char *cmd, char ***commands)
154 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
155 if ( *p && *p != ' ' )
158 /* c + 2 so that we have room for the null */
159 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
160 (*commands)[c] = strndup(s, l);
172 This function checks if the passed command is ambiguous
174 static int check_ambiguity(struct Command *cmd, char **argv){
177 /* check for ambiguity */
178 for( i = 0 ; i < cmd->ncmds ; i++ ){
180 for( match = 0, cp = commands; cp->verb; cp++ ){
187 for( skip = 0, j = 0 ; j < i ; j++ )
188 if( strcmp(cmd->cmds[j], cp->cmds[j])){
195 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
197 for(s2 = cp->cmds[i], s1 = argv[i+1];
198 *s1 == *s2 && *s1; s1++, s2++ ) ;
204 fprintf(stderr, "ERROR: in command '");
205 for( j = 0 ; j <= i ; j++ )
206 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
207 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
215 * This function, compacts the program name and the command in the first
216 * element of the '*av' array
218 static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
224 ret = (char **)malloc(sizeof(char*)*(*ac+1));
225 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
226 if( !ret || !newname ){
233 for(i=0; i < *ac ; i++ )
236 strcpy(newname, prgname);
237 strcat(newname, " ");
238 strcat(newname, cmd->verb);
251 This function perform the following jobs:
252 - show the help if '--help' or 'help' or '-h' are passed
253 - verify that a command is not ambiguous, otherwise show which
254 part of the command is ambiguous
255 - if after a (even partial) command there is '--help' show the help
256 for all the matching commands
257 - if the command doesn't' match show an error
258 - finally, if a command match, they return which command is matched and
261 The function return 0 in case of help is requested; <0 in case
262 of uncorrect command; >0 in case of matching commands
263 argc, argv are the arg-counter and arg-vector (input)
264 *nargs_ is the number of the arguments after the command (output)
265 **cmd_ is the invoked command (output)
266 ***args_ are the arguments after the command
269 static int parse_args(int argc, char **argv,
270 CommandFunction *func_,
271 int *nargs_, char **cmd_, char ***args_ )
274 struct Command *matchcmd=0;
275 char *prgname = get_prgname(argv[0]);
276 int i=0, helprequested=0;
278 if( argc < 2 || !strcmp(argv[1], "help") ||
279 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
284 for( cp = commands; cp->verb; cp++ )
286 cp->ncmds = split_command(cp->verb, &(cp->cmds));
288 for( cp = commands; cp->verb; cp++ ){
291 if( argc-1 < cp->ncmds )
293 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
298 for(s2 = cp->cmds[i], s1 = argv[i+1];
307 /* If you understand why this code works ...
308 you are a genious !! */
309 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
312 print_help(prgname, cp);
321 *nargs_ = argc-matchcmd->ncmds-1;
322 *cmd_ = matchcmd->verb;
323 *args_ = argv+matchcmd->ncmds+1;
330 printf("\n%s\n", BTRFS_BUILD_VERSION);
335 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
340 if(check_ambiguity(matchcmd, argv))
343 /* check the number of argument */
344 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
345 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
346 matchcmd->verb, -matchcmd->nargs);
349 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
350 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
351 matchcmd->verb, matchcmd->nargs);
355 if (prepare_args( nargs_, args_, prgname, matchcmd )){
356 fprintf(stderr, "ERROR: not enough memory\\n");
363 int main(int ac, char **av )
366 char *cmd=0, **args=0;
368 CommandFunction func=0;
370 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
372 /* error or no command to parse*/
376 exit(func(nargs, args));