From 228d11a9c91a5f5be9a2827a93cc5b7afef2d96a Mon Sep 17 00:00:00 2001 From: Pranith Kumar K Date: Fri, 27 Aug 2010 00:33:31 +0000 Subject: cli: command parse validation Signed-off-by: Pranith Kumar K Signed-off-by: Vijay Bellur BUG: 1431 (cli: check if arguments exist before the accessing them) URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1431 --- cli/src/cli-cmd-volume.c | 83 +++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c index 92307ceb3ad..8b240401297 100644 --- a/cli/src/cli-cmd-volume.c +++ b/cli/src/cli-cmd-volume.c @@ -43,7 +43,25 @@ cli_cmd_volume_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, void cli_cmd_volume_start_usage () { - cli_out ("Usage: volume start "); + cli_out ("Usage: volume start "); +} + +void +cli_cmd_volume_stop_usage () +{ + cli_out ("Usage: volume stop "); +} + +void +cli_cmd_volume_rename_usage () +{ + cli_out ("Usage: volume rename "); +} + +void +cli_cmd_volume_delete_usage () +{ + cli_out ("Usage: volume delete "); } int @@ -74,7 +92,7 @@ out: void cli_cmd_volume_create_usage () { - cli_out ("usage: volume create " + cli_out ("Usage: volume create " "[stripe ] [replica ] ..."); } @@ -96,7 +114,6 @@ cli_cmd_volume_create_cbk (struct cli_state *state, struct cli_cmd_word *word, ret = cli_cmd_volume_create_parse (words, wordcount, &options); if (ret) { - printf ("Command Parsing failed, "); cli_cmd_volume_create_usage (); goto out; } @@ -134,16 +151,19 @@ cli_cmd_volume_delete_cbk (struct cli_state *state, struct cli_cmd_word *word, if (!frame) goto out; - //TODO: Build validation here + if (wordcount != 3) { + cli_cmd_volume_delete_usage (); + goto out; + } + volname = (char *)words[2]; - GF_ASSERT (volname); if (proc->fn) { ret = proc->fn (frame, THIS, volname); } out: - if (ret) + if (ret && volname) cli_out ("Deleting Volume %s failed", volname); return ret; @@ -164,14 +184,12 @@ cli_cmd_volume_start_cbk (struct cli_state *state, struct cli_cmd_word *word, if (!frame) goto out; - //TODO: Build validation here - if (wordcount < 3) { + if (wordcount != 3) { cli_cmd_volume_start_usage (); goto out; } volname = (char *)words[2]; - GF_ASSERT (volname); proc = &cli_rpc_prog->proctable[GF1_CLI_START_VOLUME]; @@ -201,9 +219,12 @@ cli_cmd_volume_stop_cbk (struct cli_state *state, struct cli_cmd_word *word, if (!frame) goto out; - //TODO: Build validation here + if (wordcount != 3) { + cli_cmd_volume_stop_usage (); + goto out; + } + volname = (char *)words[2]; - GF_ASSERT (volname); proc = &cli_rpc_prog->proctable[GF1_CLI_STOP_VOLUME]; @@ -212,7 +233,7 @@ cli_cmd_volume_stop_cbk (struct cli_state *state, struct cli_cmd_word *word, } out: - if (!proc && ret) + if (!proc && ret && volname) cli_out ("Stopping Volume %s failed", volname); return ret; @@ -237,10 +258,11 @@ cli_cmd_volume_rename_cbk (struct cli_state *state, struct cli_cmd_word *word, if (!dict) goto out; - GF_ASSERT (words[2]); - GF_ASSERT (words[3]); + if (wordcount != 4) { + cli_cmd_volume_rename_usage (); + goto out; + } - //TODO: Build validation here ret = dict_set_str (dict, "old-volname", (char *)words[2]); if (ret) @@ -259,10 +281,10 @@ cli_cmd_volume_rename_cbk (struct cli_state *state, struct cli_cmd_word *word, out: if (!proc && ret) { - char *volname = (char *) words[2]; if (dict) dict_destroy (dict); - cli_out ("Renaming Volume %s failed", volname ); + if (wordcount > 2) + cli_out ("Renaming Volume %s failed", (char *)words[2]); } return ret; @@ -271,7 +293,7 @@ out: void cli_cmd_volume_defrag_usage () { - cli_out ("Usage: volume rebalance "); + cli_out ("Usage: volume rebalance "); } int @@ -291,13 +313,11 @@ cli_cmd_volume_defrag_cbk (struct cli_state *state, struct cli_cmd_word *word, if (!dict) goto out; - GF_ASSERT (words[2]); - - if (!(words[3])) { + if (wordcount != 4) { cli_cmd_volume_defrag_usage(); goto out; } - //TODO: Build validation here + ret = dict_set_str (dict, "volname", (char *)words[2]); if (ret) goto out; @@ -317,7 +337,9 @@ out: if (dict) dict_destroy (dict); - cli_out ("Defrag of Volume %s failed", (char *)words[2]); + if (wordcount > 2) + cli_out ("Rebalance of Volume %s failed", + (char *)words[2]); } return 0; @@ -335,7 +357,7 @@ cli_cmd_volume_set_cbk (struct cli_state *state, struct cli_cmd_word *word, void cli_cmd_volume_add_brick_usage () { - cli_out ("usage: volume add-brick " + cli_out ("Usage: volume add-brick " "[(replica )|(stripe )] ..."); } @@ -356,7 +378,6 @@ cli_cmd_volume_add_brick_cbk (struct cli_state *state, ret = cli_cmd_volume_add_brick_parse (words, wordcount, &options); if (ret) { - printf("Command Parsing failed, "); cli_cmd_volume_add_brick_usage (); goto out; } @@ -381,7 +402,7 @@ out: void cli_cmd_volume_remove_brick_usage () { - cli_out ("usage: volume remove-brick " + cli_out ("Usage: volume remove-brick " "[(replica )|(stripe )] ..."); } @@ -402,7 +423,6 @@ cli_cmd_volume_remove_brick_cbk (struct cli_state *state, ret = cli_cmd_volume_remove_brick_parse (words, wordcount, &options); if (ret) { - printf("Command Parsing failed, "); cli_cmd_volume_remove_brick_usage (); goto out; } @@ -427,8 +447,8 @@ out: void cli_cmd_volume_replace_brick_usage () { - cli_out("usage: volume replace-brick " - "( )|pause|abort|start|status"); + cli_out("Usage: volume replace-brick " + "( ) start|pause|abort|status"); } @@ -452,7 +472,6 @@ cli_cmd_volume_replace_brick_cbk (struct cli_state *state, ret = cli_cmd_volume_replace_brick_parse (words, wordcount, &options); if (ret) { - printf("Command Parsing failed, "); cli_cmd_volume_replace_brick_usage (); goto out; } @@ -528,7 +547,7 @@ struct cli_cmd volume_cmds[] = { cli_cmd_volume_defrag_cbk, "rebalance status of volume "}, - { "volume replace-brick ( )|pause|abort|start|status", + { "volume replace-brick ( ) start|pause|abort|status", cli_cmd_volume_replace_brick_cbk, "replace-brick operations"}, @@ -557,7 +576,7 @@ cli_cmd_volume_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, for (cmd = volume_cmds; cmd->pattern; cmd++) cli_out ("%s - %s", cmd->pattern, cmd->desc); - + if (!state->rl_enabled) exit (0); -- cgit