From 4c3aa910e7913c34db24f864a33dfb6d1e0234a4 Mon Sep 17 00:00:00 2001 From: Manikandan Selvaganesh Date: Tue, 14 Feb 2017 17:50:27 +0100 Subject: libglusterfs: add dict_rename_key() The dict_rename_key() function will be used for converting the "security.selinux" xattr to "trusted.gluster.selinux" in the upcoming SELinux xlator. BUG: 1318100 Change-Id: Ic5d0b9127e2c360d355f02e200a820597e83fa2c Signed-off-by: Manikandan Selvaganesh Signed-off-by: Jiffin Tony Thottan [ndevos: split from change Id8916bd8e064ccf74ba86225ead95f86dc5a1a25] Reviewed-on: https://review.gluster.org/16616 Reviewed-by: Niels de Vos Tested-by: Niels de Vos Smoke: Gluster Build System NetBSD-regression: NetBSD Build System CentOS-regression: Gluster Build System Reviewed-by: Jeff Darcy --- libglusterfs/src/dict.c | 28 ++++++++++++++++++++++++++++ libglusterfs/src/dict.h | 1 + 2 files changed, 29 insertions(+) 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 -- cgit