summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libglusterfs/src/dict.c28
-rw-r--r--libglusterfs/src/dict.h1
2 files changed, 29 insertions, 0 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
index a0de0947b1a..839b42685e8 100644
--- a/libglusterfs/src/dict.c
+++ b/libglusterfs/src/dict.c
@@ -22,6 +22,7 @@
#include "hashfn.h"
#include "logging.h"
#include "compat.h"
+#include "compat-errno.h"
#include "byte-order.h"
#include "globals.h"
#include "statedump.h"
@@ -2387,6 +2388,33 @@ err:
return ret;
}
+int
+dict_rename_key (dict_t *this, char *key, char *replace_key)
+{
+ data_pair_t *pair = NULL;
+ int ret = -EINVAL;
+
+ /* replacing a key by itself is a NO-OP */
+ if (strcmp (key, replace_key) == 0)
+ return 0;
+
+ LOCK (&this->lock);
+ {
+ /* no need to data_ref(pair->value), dict_set_lk() does it */
+ pair = dict_lookup_common (this, key);
+ if (!pair)
+ ret = -ENODATA;
+ else
+ ret = dict_set_lk (this, replace_key, pair->value, 1);
+ }
+ UNLOCK (&this->lock);
+
+ if (!ret)
+ /* only delete the key on success */
+ dict_del (this, key);
+
+ return ret;
+}
/**
* Serialization format:
diff --git a/libglusterfs/src/dict.h b/libglusterfs/src/dict.h
index a7fb6c78425..bef58e102cc 100644
--- a/libglusterfs/src/dict.h
+++ b/libglusterfs/src/dict.h
@@ -239,6 +239,7 @@ GF_MUST_CHECK int dict_add_dynstr_with_alloc (dict_t *this, char *key, char *str
GF_MUST_CHECK int dict_get_str (dict_t *this, char *key, char **str);
GF_MUST_CHECK int dict_get_str_boolean (dict_t *this, char *key, int default_val);
+GF_MUST_CHECK int dict_rename_key (dict_t *this, char *key, char *replace_key);
GF_MUST_CHECK int dict_serialize_value_with_delim (dict_t *this, char *buf, int32_t *serz_len,
char delimiter);
void