diff options
author | Junaid <junaid@gluster.com> | 2011-07-21 13:49:26 +0530 |
---|---|---|
committer | Anand Avati <avati@gluster.com> | 2011-07-24 11:44:15 -0700 |
commit | ace2a14a7d527eb792600d127f981068060c9d97 (patch) | |
tree | 9958e20834b4a48e6083a5ed80411fdb183d5de3 | |
parent | be32fb15e66869cfab37f80b86578b2b7d203432 (diff) |
mgmt/glusterd: Read the contents of vol-info file into a dynamically alloced string.
Change-Id: I64d7e143eb4b0fda76a9b97134d0233763a1679a
BUG: 3154
Reviewed-on: http://review.gluster.com/71
Tested-by: Gluster Build System <jenkins@build.gluster.com>
Reviewed-by: Pavan T C <tcp@gluster.com>
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-store.c | 125 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-store.h | 3 |
2 files changed, 92 insertions, 36 deletions
diff --git a/xlators/mgmt/glusterd/src/glusterd-store.c b/xlators/mgmt/glusterd/src/glusterd-store.c index f3fa2c0aabe..0f0a2460359 100644 --- a/xlators/mgmt/glusterd/src/glusterd-store.c +++ b/xlators/mgmt/glusterd/src/glusterd-store.c @@ -838,17 +838,57 @@ out: } +int +glusterd_store_read_and_tokenize (FILE *file, char *str, + char **iter_key, char **iter_val, + glusterd_store_op_errno_t *store_errno) +{ + int32_t ret = -1; + + GF_ASSERT (file); + GF_ASSERT (str); + GF_ASSERT (iter_key); + GF_ASSERT (iter_val); + GF_ASSERT (store_errno); + + ret = fscanf (file, "%s", str); + if (ret <= 0) { + ret = -1; + *store_errno = GD_STORE_EOF; + goto out; + } + + *iter_key = strtok (str, "="); + if (*iter_key == NULL) { + ret = -1; + *store_errno = GD_STORE_KEY_NULL; + goto out; + } + + *iter_val = strtok (NULL, "="); + if (*iter_key == NULL) { + ret = -1; + *store_errno = GD_STORE_VALUE_NULL; + goto out; + } + + *store_errno = GD_STORE_SUCCESS; + ret = 0; +out: + return ret; +} int32_t glusterd_store_retrieve_value (glusterd_store_handle_t *handle, char *key, char **value) { int32_t ret = -1; - char scan_str[4096] = {0,}; + char *scan_str = NULL; char *iter_key = NULL; char *iter_val = NULL; - char *str = NULL; char *free_str = NULL; + struct stat st = {0,}; + glusterd_store_op_errno_t store_errno = GD_STORE_SUCCESS; GF_ASSERT (handle); @@ -863,35 +903,43 @@ glusterd_store_retrieve_value (glusterd_store_handle_t *handle, goto out; } - ret = fscanf (handle->read, "%s", scan_str); + ret = fstat (handle->fd, &st); + if (ret < 0) { + gf_log ("glusterd", GF_LOG_WARNING, + "stat on file failed"); + ret = -1; + store_errno = GD_STORE_STAT_FAILED; + goto out; + } - while (ret != EOF) { - if (free_str) { - GF_FREE (free_str); - free_str = NULL; - } - str = gf_strdup (scan_str); - if (!str) + scan_str = GF_CALLOC (1, st.st_size, + gf_gld_mt_char); + if (scan_str == NULL) { + ret = -1; + store_errno = GD_STORE_ENOMEM; + goto out; + } + + free_str = scan_str; + + do { + ret = glusterd_store_read_and_tokenize (handle->read, scan_str, + &iter_key, &iter_val, + &store_errno); + if (ret < 0) { goto out; - else - free_str = str; - iter_key = strtok (str, "="); + } + gf_log ("", GF_LOG_DEBUG, "key %s read", iter_key); if (!strcmp (key, iter_key)) { gf_log ("", GF_LOG_DEBUG, "key %s found", key); - iter_val = strtok (NULL, "="); ret = 0; if (iter_val) *value = gf_strdup (iter_val); goto out; } - - ret = fscanf (handle->read, "%s", scan_str); - } - - if (EOF == ret) - ret = -1; + } while (1); out: if (handle->fd > 0) { close (handle->fd); @@ -1203,11 +1251,11 @@ glusterd_store_iter_get_next (glusterd_store_iter_t *iter, glusterd_store_op_errno_t *op_errno) { int32_t ret = -1; - char scan_str[4096] = {0,}; - char *str = NULL; + char *scan_str = NULL; char *free_str = NULL; char *iter_key = NULL; char *iter_val = NULL; + struct stat st = {0,}; glusterd_store_op_errno_t store_errno = GD_STORE_SUCCESS; GF_ASSERT (iter); @@ -1215,28 +1263,35 @@ glusterd_store_iter_get_next (glusterd_store_iter_t *iter, GF_ASSERT (key); GF_ASSERT (value); - *key = NULL; - *value = NULL; - - ret = fscanf (iter->file, "%s", scan_str); - - if (ret <= 0) { + ret = fstat (iter->fd, &st); + if (ret < 0) { + gf_log ("glusterd", GF_LOG_WARNING, + "stat on file failed"); ret = -1; - store_errno = GD_STORE_EOF; + store_errno = GD_STORE_STAT_FAILED; goto out; } - str = gf_strdup (scan_str); - if (!str) { + scan_str = GF_CALLOC (1, st.st_size, + gf_gld_mt_char); + if (scan_str == NULL) { ret = -1; store_errno = GD_STORE_ENOMEM; goto out; - } else { - free_str = str; } - iter_key = strtok (str, "="); - iter_val = strtok (NULL, "="); + *key = NULL; + *value = NULL; + + free_str = scan_str; + + ret = glusterd_store_read_and_tokenize (iter->file, scan_str, + &iter_key, &iter_val, + &store_errno); + if (ret < 0) { + goto out; + } + ret = glusterd_store_validate_key_value (iter->filepath, iter_key, iter_val, &store_errno); diff --git a/xlators/mgmt/glusterd/src/glusterd-store.h b/xlators/mgmt/glusterd/src/glusterd-store.h index ad9ca6a952e..af8a316c9f7 100644 --- a/xlators/mgmt/glusterd/src/glusterd-store.h +++ b/xlators/mgmt/glusterd/src/glusterd-store.h @@ -82,7 +82,8 @@ typedef enum { GD_STORE_VALUE_NULL, GD_STORE_KEY_VALUE_NULL, GD_STORE_EOF, - GD_STORE_ENOMEM + GD_STORE_ENOMEM, + GD_STORE_STAT_FAILED } glusterd_store_op_errno_t; int32_t |