summaryrefslogtreecommitdiffstats
path: root/cli/src/cli-cmd-parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'cli/src/cli-cmd-parser.c')
-rw-r--r--cli/src/cli-cmd-parser.c103
1 files changed, 61 insertions, 42 deletions
diff --git a/cli/src/cli-cmd-parser.c b/cli/src/cli-cmd-parser.c
index 59e951636..6abd97ce7 100644
--- a/cli/src/cli-cmd-parser.c
+++ b/cli/src/cli-cmd-parser.c
@@ -2754,12 +2754,10 @@ out:
int32_t
cli_snap_create_desc_parse (dict_t *dict, const char **words,
- int wordcount, int32_t desc_opt_loc,
- unsigned int no_of_wrds_in_desc)
+ size_t wordcount, int32_t desc_opt_loc)
{
int32_t ret = -1;
char *desc = NULL;
- int32_t i = 0;
int32_t desc_len = 0;
desc = GF_CALLOC (MAX_SNAP_DESCRIPTION_LEN + 1, sizeof(char),
@@ -2771,31 +2769,26 @@ cli_snap_create_desc_parse (dict_t *dict, const char **words,
}
/* Creating the description string */
- for (i = 0; i < no_of_wrds_in_desc; i++) {
- if ((strcmp (words[desc_opt_loc + 1 + i], "-n") == 0) ||
- (strcmp (words[desc_opt_loc + 1 + i], "-d") == 0)) {
- cli_out ("snapshot create: failed: Options(-n/-d) "
- "are not valid descriptions");
- ret = -1;
- goto out;
- }
-
- strcat (desc, words[desc_opt_loc + 1 + i]);
- strcat (desc, " ");
- /* Calculating the size of the description as given by the user */
- desc_len += strlen(words[desc_opt_loc + 1 + i]);
- desc_len++;
+ if ((strcmp (words[desc_opt_loc], "-n") == 0) ||
+ (strcmp (words[desc_opt_loc], "-d") == 0)) {
+ cli_out ("snapshot create: failed: Options(-n/-d) "
+ "are not valid descriptions");
+ ret = -1;
+ goto out;
}
- /* Removing the last space in the string */
- desc[--desc_len] = '\0';
-
- if (desc_len > MAX_SNAP_DESCRIPTION_LEN) {
+ if (strlen (words[desc_opt_loc]) >= MAX_SNAP_DESCRIPTION_LEN) {
cli_out ("snapshot create: description truncated: "
"Description provided is longer than 1024 characters");
- desc[MAX_SNAP_DESCRIPTION_LEN] = '\0';
+ desc_len = MAX_SNAP_DESCRIPTION_LEN;
+ } else {
+ desc_len = strlen (words[desc_opt_loc]);
}
+ strncpy (desc, words[desc_opt_loc], desc_len);
+ desc[desc_len] = '\0';
+ /* Calculating the size of the description as given by the user */
+
ret = dict_set_dynstr (dict, "snap-description", desc);
if (ret) {
gf_log ("", GF_LOG_ERROR, "Unable to save snap description");
@@ -2804,11 +2797,8 @@ cli_snap_create_desc_parse (dict_t *dict, const char **words,
ret = 0;
out:
-
- if (ret) {
- if (desc)
- GF_FREE (desc);
- }
+ if (ret && desc)
+ GF_FREE (desc);
return ret;
}
@@ -2859,9 +2849,9 @@ cli_snap_create_parse (dict_t *dict, const char **words, int wordcount,
/* Description should not be blank */
no_of_wrds_in_desc = (wordcount - 1) - desc_opt_loc;
- if (no_of_wrds_in_desc == 0) {
+ if (no_of_wrds_in_desc != 1) {
cli_out ("snapshot create: failed: "
- "No description provided");
+ "Invalid description");
ret = -1;
goto out;
}
@@ -2882,9 +2872,9 @@ cli_snap_create_parse (dict_t *dict, const char **words, int wordcount,
/* Description should not be blank */
no_of_wrds_in_desc = (wordcount - 1) - desc_opt_loc;
- if (no_of_wrds_in_desc == 0) {
+ if (no_of_wrds_in_desc != 1) {
cli_out ("snapshot create: failed: "
- "No description provided");
+ "Invalid description");
ret = -1;
goto out;
}
@@ -2901,9 +2891,9 @@ cli_snap_create_parse (dict_t *dict, const char **words, int wordcount,
/* Description should not be blank */
no_of_wrds_in_desc = (name_opt_loc) - desc_opt_loc -1;
- if (no_of_wrds_in_desc == 0) {
+ if (no_of_wrds_in_desc != 1) {
cli_out ("snapshot create: failed: "
- "No description provided");
+ "Invalid description");
ret = -1;
goto out;
}
@@ -2977,10 +2967,14 @@ cli_snap_create_parse (dict_t *dict, const char **words, int wordcount,
/* Parsing the description and saving it in the dict */
if (desc_opt_loc > cmdi + 1) {
+ /* desc_opt_loc contains the position of -d option
+ used to indicate description. So send directly
+ the position of the description (i.e desc_opt_loc + 1)
+ for parsing
+ */
ret = cli_snap_create_desc_parse (dict, words,
wordcount,
- desc_opt_loc,
- no_of_wrds_in_desc);
+ desc_opt_loc + 1);
if (ret) {
gf_log ("", GF_LOG_ERROR,
"Unable to parse snap-description");
@@ -3184,6 +3178,11 @@ out:
return ret;
}
+/* function cli_snap_config_parse
+ return value: -1 on failure
+ 1 if user cancels the operation
+ 0 on success
+*/
int32_t
cli_snap_config_parse (const char **words, int wordcount, dict_t *options,
struct cli_state *state)
@@ -3297,7 +3296,9 @@ cli_snap_config_parse (const char **words, int wordcount, dict_t *options,
answer = cli_cmd_get_confirmation (state,
conf_vals->question);
if (GF_ANSWER_NO == answer) {
- ret = -1;
+ ret = 1;
+ gf_log ("", GF_LOG_DEBUG, "User cancelled "
+ "snapshot config operation");
goto out;
}
}
@@ -3312,15 +3313,23 @@ out:
also should be given in the command. If cg should be removed,
then volume name is not necessary.
"gluster snapshot delete (<volname> -s <snapname> | -c <cgname>)"
+ return value: -1 on failure
+ 1 if user cancels the operation
+ 0 on success
*/
int32_t
cli_snap_remove_parse (dict_t *dict, const char **words, int wordcount,
- unsigned int cmdi)
+ unsigned int cmdi, struct cli_state *state)
{
uint32_t name_opt_loc = 0;
int32_t ret = -1;
uint32_t i = 0;
gf_boolean_t is_cg = _gf_false;
+ const char *question = NULL;
+ gf_answer_t answer = GF_ANSWER_NO;
+
+ question = "Deleting snap will erase all information about the snap. "
+ "Do you want to continue?";
GF_ASSERT (dict);
GF_ASSERT (words);
@@ -3395,6 +3404,14 @@ cli_snap_remove_parse (dict_t *dict, const char **words, int wordcount,
}
}
+ answer = cli_cmd_get_confirmation (state, question);
+ if (GF_ANSWER_NO == answer) {
+ ret = 1;
+ gf_log ("", GF_LOG_DEBUG, "User cancelled "
+ "snapshot delete operation");
+ goto out;
+ }
+
out:
return ret;
}
@@ -3613,10 +3630,11 @@ cli_cmd_snapshot_parse (const char **words, int wordcount, dict_t **options,
cmdi = 2;
ret = cli_snap_remove_parse (dict, words,
- wordcount, cmdi);
+ wordcount, cmdi, state);
if (ret) {
- gf_log ("", GF_LOG_ERROR,
- "remove command parsing failed.");
+ if (ret < 0)
+ gf_log ("", GF_LOG_ERROR,
+ "remove command parsing failed.");
goto out;
}
break;
@@ -3628,8 +3646,9 @@ cli_cmd_snapshot_parse (const char **words, int wordcount, dict_t **options,
ret = cli_snap_config_parse (words, wordcount, dict,
state);
if (ret) {
- gf_log ("cli", GF_LOG_ERROR,
- "config command parsing failed.");
+ if (ret < 0)
+ gf_log ("cli", GF_LOG_ERROR,
+ "config command parsing failed.");
goto out;
}
break;