diff options
Diffstat (limited to 'cli/src/cli-cmd-parser.c')
-rw-r--r-- | cli/src/cli-cmd-parser.c | 193 |
1 files changed, 181 insertions, 12 deletions
diff --git a/cli/src/cli-cmd-parser.c b/cli/src/cli-cmd-parser.c index 87d29972b..b05fad315 100644 --- a/cli/src/cli-cmd-parser.c +++ b/cli/src/cli-cmd-parser.c @@ -2972,6 +2972,142 @@ out: return ret; } +/* snapshot list [<volnames> | <volname> -s <snapname> | -c <cgname>] [-d] + * cmdi is command index which contains number of standard arguments in + * command, here cmdi is 2 i.e "gluster snapshot list" + */ +int +cli_snap_list_parse (dict_t *dict, const char **words, int wordcount, int cmdi) +{ + int ret = -1; + int i = 0; + int64_t vol_count = 0; + int vol_start_index = -1; + int8_t snap_details = 0; + char *snap_name = NULL; + char *cg_name = NULL; + char *vol_name = NULL; + char key[256] = ""; + + GF_ASSERT (dict); + GF_ASSERT (words); + GF_ASSERT (wordcount >= cmdi); + /* if command is "gluster snapshot list*/ + if (wordcount == cmdi) { + ret = 0; + goto out; + } + + for (i = cmdi; i < wordcount; ++i) { + if (strcmp (words[i], "-d") == 0) { + if (snap_details == 1) { + /* If option is already set */ + gf_log("", GF_LOG_ERROR, + "snap_details already set"); + goto out; + } + snap_details = 1; + } else if (strcmp (words[i], "-s") == 0) { + if ((wordcount - 1) == i || (snap_name != NULL) + || vol_count != 1 || strcmp (words[++i], "-d") == 0 + || strcmp (words[i], "-c") == 0) { + /* if -s is not followed by a valid snap_name + * or if snap_name is already parsed + * or number of volname specified + * is not equal to 1 + */ + gf_log("", GF_LOG_ERROR, "Invalid snap_name" + " or snap_name already parsed" + " or volname specified is not equal 1"); + goto out; + } + snap_name = words[i]; // word followed by -s is snapname + } else if (strcmp (words[i], "-c") == 0) { + if ((wordcount - 1) == i || (cg_name != NULL) + || strcmp (words[++i], "-d") == 0 + || strcmp (words[i], "-s") == 0) { + /* if -c is not followed by a valid cg_name + * or if cg_name is already parsed + */ + gf_log("", GF_LOG_ERROR, "Invalid cg_name" + " or cg_name already parsed"); + goto out; + } + cg_name = words[i]; + } else { + if (vol_count != 0) { + /* if vol names already set */ + gf_log("", GF_LOG_ERROR, + "Vol Names already set"); + goto out; + } + + vol_start_index = i; + vol_count = 1; + + while (++i < wordcount) { + if ((strcmp (words[i], "-d") == 0) || + (strcmp (words[i], "-s") == 0) || + (strcmp (words[i], "-c") == 0)) { + /*if option -d, -s or -c is given after volname + *then go back in index to parse this option + *again + */ + --i; + break; + } + ++vol_count; + } + } + } + + /* if CG name is present in the command then fill it to dictionary */ + if (cg_name != NULL) { + if (snap_name != NULL || vol_count != 0) { + /* When -s option or volume name is given along + * with -c option. Details of single snap belonging + * to a CG is not supported. + */ + gf_log("", GF_LOG_ERROR, "details of single snap" + " belonging to a CG is not supported"); + goto out; + } + ret = dict_set_str(dict, "cg_name", cg_name); + if (ret) { + gf_log("", GF_LOG_ERROR, "Failed to set cg_name"); + goto out; + } + } else { + /* if snap name is present in the command + * then fill it to dictionary + */ + if (snap_name != NULL) { + ret = dict_set_str (dict, "snap_name", snap_name); + if (ret) { + gf_log ("", GF_LOG_ERROR, + "Failed to set snap_name"); + goto out; + } + } + + ret = dict_set_int64 (dict, "vol_count", vol_count); + /* fill volume name in dictionary */ + for (i = 0; i < vol_count; ++i) { + vol_name = words[vol_start_index + i]; + snprintf (key, sizeof (key), "vol%d", i); + ret = dict_set_str (dict, key, vol_name); + if (ret) { + gf_log("", GF_LOG_ERROR, + "Failed to set vol_name"); + goto out; + } + } + } +out: + return ret; +} + + int32_t cli_cmd_snapshot_parse (const char **words, int wordcount, dict_t **options) { @@ -2979,8 +3115,9 @@ cli_cmd_snapshot_parse (const char **words, int wordcount, dict_t **options) dict_t *dict = NULL; gf1_cli_snapshot type = GF_SNAP_OPTION_TYPE_NONE; int32_t cmdi = 0; - char *opwords[] = {"create", NULL}; + char *opwords[] = {"create", "list", NULL}; char *w = NULL; + int i = 0; GF_ASSERT (words); GF_ASSERT (options); @@ -2989,23 +3126,14 @@ cli_cmd_snapshot_parse (const char **words, int wordcount, dict_t **options) if (!dict) goto out; - /* syntax: - * snapshot create <volnames> [-n <snap-name/cg-name>] [-d <description>] - */ /* Lowest wordcount possible */ if (wordcount < 2) { - gf_log ("", GF_LOG_ERROR, "Invalid command: Not enough arguments"); + gf_log ("", GF_LOG_ERROR, + "Invalid command: Not enough arguments"); goto out; } - /* In cases where the vol-name is not given - * parsing fails. volname cannot be an opword. - * and that is what this check verifies */ - w = str_getunamb (words[2], opwords); - if (w) - goto out; - w = str_getunamb (words[1], opwords); if (!w) { /* Checks if the operation is a valid operation */ @@ -3015,6 +3143,16 @@ cli_cmd_snapshot_parse (const char **words, int wordcount, dict_t **options) /* Check which op is intended */ if (strcmp (w, "create") == 0) { + /*syntax: + *snapshot create <volnames> [-n <snap-name/cg-name>] [-d <description>] + */ + + /* In cases where the vol-name is not given + * parsing fails. volname cannot be an opword. + * and that is what this check verifies */ + w = str_getunamb (words[2], opwords); + if (w) + goto out; type = GF_SNAP_OPTION_TYPE_CREATE; cmdi = 1; ret = dict_set_int32 (dict, "type", type); @@ -3031,6 +3169,37 @@ cli_cmd_snapshot_parse (const char **words, int wordcount, dict_t **options) "create command parsing failed."); goto out; } + } else if (strcmp (w, "list") == 0) { + /* snapshot list [<volnames> | <volname> [-s <snapname>] + * | -c <cgname>] [-d] */ + /* check if arguments contains any Keyword */ + cmdi = 2; + for (i = cmdi ; i < wordcount ; i++) { + w = str_getunamb (words[i], opwords); + if (w) { + /*Checks if the operation is a valid operation*/ + cli_out ("Usage of Keyword in wrong place"); + gf_log ("", GF_LOG_ERROR, "Opword Mismatch"); + goto out; + } + } + type = GF_SNAP_OPTION_TYPE_LIST; + ret = dict_set_int32 (dict, "type" , type); + + if (ret) { + gf_log ("" , GF_LOG_ERROR, + "Failed to set type."); + goto out; + } + + ret = cli_snap_list_parse (dict, words, + wordcount, cmdi); + + if (ret) { + gf_log ("", GF_LOG_ERROR, + "list command parsing failed."); + goto out; + } } else { gf_log ("", GF_LOG_ERROR, "Opword Mismatch"); goto out; |