int32_t gf_store_retrieve_value (gf_store_handle_t *handle, char *key, char **value) { int32_t ret = -1; char *scan_str = NULL; char *iter_key = NULL; char *iter_val = NULL; char *free_str = NULL; struct stat st = {0,}; gf_store_op_errno_t store_errno = GD_STORE_SUCCESS; GF_ASSERT (handle); if (handle->locked == F_ULOCK) /* no locking is used handle->fd gets closed() after usage */ handle->fd = open (handle->path, O_RDWR); else /* handle->fd is valid already, kept open for lockf() */ lseek (handle->fd, 0, SEEK_SET); if (handle->fd == -1) { gf_log ("", GF_LOG_ERROR, "Unable to open file %s errno: %s", handle->path, strerror (errno)); goto out; } if (!handle->read) handle->read = fdopen (dup(handle->fd), "r"); else fseek (handle->read, 0, SEEK_SET); if (!handle->read) { gf_log ("", GF_LOG_ERROR, "Unable to open file %s errno: %s", handle->path, strerror (errno)); goto out; } ret = fstat (handle->fd, &st); if (ret < 0) { gf_log ("", GF_LOG_WARNING, "stat on file %s failed", handle->path); ret = -1; store_errno = GD_STORE_STAT_FAILED; goto out; } scan_str = GF_CALLOC (1, st.st_size, gf_common_mt_char); if (scan_str == NULL) { ret = -1; store_errno = GD_STORE_ENOMEM; goto out; } free_str = scan_str; do { ret = gf_store_read_and_tokenize (handle->read, scan_str, &iter_key, &iter_val, &store_errno); if (ret < 0) { gf_log ("", GF_LOG_TRACE, "error while reading key " "'%s': %s", key, gf_store_strerror (store_errno)); goto out; } gf_log ("", GF_LOG_TRACE, "key %s read", iter_key); if (!strcmp (key, iter_key)) { gf_log ("", GF_LOG_DEBUG, "key %s found", key); ret = 0; if (iter_val) *value = gf_strdup (iter_val); goto out; } } while (1); out: if (handle->read) { fclose (handle->read); handle->read = NULL; } if (handle->fd > 0 && handle->locked == F_ULOCK) { /* only invalidate handle->fd if not locked */ close (handle->fd); } GF_FREE (free_str); return ret; }
int32_t gf_store_retrieve_value (gf_store_handle_t *handle, char *key, char **value) { int32_t ret = -1; char *scan_str = NULL; char *iter_key = NULL; char *iter_val = NULL; char *free_str = NULL; struct stat st = {0,}; gf_store_op_errno_t store_errno = GD_STORE_SUCCESS; GF_ASSERT (handle); handle->fd = open (handle->path, O_RDWR); if (handle->fd == -1) { gf_log ("", GF_LOG_ERROR, "Unable to open file %s errno: %s", handle->path, strerror (errno)); goto out; } if (!handle->read) handle->read = fdopen (handle->fd, "r"); if (!handle->read) { gf_log ("", GF_LOG_ERROR, "Unable to open file %s errno: %s", handle->path, strerror (errno)); goto out; } ret = fstat (handle->fd, &st); if (ret < 0) { gf_log ("", GF_LOG_WARNING, "stat on file %s failed", handle->path); ret = -1; store_errno = GD_STORE_STAT_FAILED; goto out; } scan_str = GF_CALLOC (1, st.st_size, gf_common_mt_char); if (scan_str == NULL) { ret = -1; store_errno = GD_STORE_ENOMEM; goto out; } free_str = scan_str; do { ret = gf_store_read_and_tokenize (handle->read, scan_str, &iter_key, &iter_val, &store_errno); if (ret < 0) { gf_log ("", GF_LOG_TRACE, "error while reading key " "'%s': %s", key, gf_store_strerror (store_errno)); goto out; } gf_log ("", GF_LOG_TRACE, "key %s read", iter_key); if (!strcmp (key, iter_key)) { gf_log ("", GF_LOG_DEBUG, "key %s found", key); ret = 0; if (iter_val) *value = gf_strdup (iter_val); goto out; } } while (1); out: if (handle->fd > 0) { close (handle->fd); handle->read = NULL; } GF_FREE (free_str); return ret; }