From 2dfe3715b56a90d5b7df914c7b67d308b0b45b67 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Thu, 14 Aug 2014 17:24:12 +0200 Subject: dict: add dict_set_dynstr_with_alloc There is an overwhelming no. of instances of the following pattern in glusterd module. ... char *dynstr = gf_strdup (str); if (!dynstr) goto err; ret = dict_set_dynstr (dict, key, dynstr); if (ret) goto err; ... With this changes it would look as below, ret = dict_set_dynstr_with_alloc (dict, key, str); if (ret) goto err; Cherry picked from commit a9d4d369efc978511e3cb69e5643945710cc9416: > Change-Id: I6a47b1cbab4834badadc48c56d0b5c8c06c6dd4d > Signed-off-by: Krishnan Parthasarathi > Reviewed-on: http://review.gluster.org/7379 > Tested-by: Gluster Build System > Reviewed-by: Jeff Darcy Backport notes: Included this change to accommodate additional backports. BUG: 1081016 Change-Id: I6a47b1cbab4834badadc48c56d0b5c8c06c6dd4d Signed-off-by: Niels de Vos Reviewed-on: http://review.gluster.org/8489 Tested-by: Gluster Build System Reviewed-by: Kaleb KEITHLEY --- libglusterfs/src/dict.c | 17 +++++++++++++++ libglusterfs/src/dict.h | 1 + xlators/mgmt/glusterd/src/glusterd-utils.c | 34 ++++++++++-------------------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c index f2df5a6d431..065990b42ef 100644 --- a/libglusterfs/src/dict.c +++ b/libglusterfs/src/dict.c @@ -2062,6 +2062,23 @@ err: return ret; } +int +dict_set_dynstr_with_alloc (dict_t *this, char *key, const char *str) +{ + char *alloc_str = NULL; + int ret = -1; + + alloc_str = gf_strdup (str); + if (!alloc_str) + return -1; + + ret = dict_set_dynstr (this, key, alloc_str); + if (ret) + GF_FREE (alloc_str); + + return ret; +} + int dict_set_dynstr (dict_t *this, char *key, char *str) { diff --git a/libglusterfs/src/dict.h b/libglusterfs/src/dict.h index 6e5d8aa0650..a92fd2cb61a 100644 --- a/libglusterfs/src/dict.h +++ b/libglusterfs/src/dict.h @@ -228,6 +228,7 @@ GF_MUST_CHECK int dict_set_static_bin (dict_t *this, char *key, void *ptr, size_ GF_MUST_CHECK int dict_set_str (dict_t *this, char *key, char *str); GF_MUST_CHECK int dict_set_dynmstr (dict_t *this, char *key, char *str); GF_MUST_CHECK int dict_set_dynstr (dict_t *this, char *key, char *str); +GF_MUST_CHECK int dict_set_dynstr_with_alloc (dict_t *this, char *key, const 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); diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c index 36cd2ddc741..ded09f154ce 100644 --- a/xlators/mgmt/glusterd/src/glusterd-utils.c +++ b/xlators/mgmt/glusterd/src/glusterd-utils.c @@ -5119,9 +5119,6 @@ glusterd_add_brick_mount_details (glusterd_brickinfo_t *brickinfo, char key[1024] = {0}; char base_key[1024] = {0}; char *mnt_pt = NULL; - char *fs_name = NULL; - char *mnt_options = NULL; - char *device = NULL; FILE *mtab = NULL; struct mntent *entry = NULL; @@ -5154,8 +5151,7 @@ glusterd_add_brick_mount_details (glusterd_brickinfo_t *brickinfo, memset (key, 0, sizeof (key)); snprintf (key, sizeof (key), "%s.device", base_key); - device = gf_strdup (entry->mnt_fsname); - ret = dict_set_dynstr (dict, key, device); + ret = dict_set_dynstr_with_alloc (dict, key, entry->mnt_fsname); if (ret) goto out; @@ -5163,8 +5159,7 @@ glusterd_add_brick_mount_details (glusterd_brickinfo_t *brickinfo, memset (key, 0, sizeof (key)); snprintf (key, sizeof (key), "%s.fs_name", base_key); - fs_name = gf_strdup (entry->mnt_type); - ret = dict_set_dynstr (dict, key, fs_name); + ret = dict_set_dynstr_with_alloc (dict, key, entry->mnt_type); if (ret) goto out; @@ -5172,8 +5167,7 @@ glusterd_add_brick_mount_details (glusterd_brickinfo_t *brickinfo, memset (key, 0, sizeof (key)); snprintf (key, sizeof (key), "%s.mnt_options", base_key); - mnt_options = gf_strdup (entry->mnt_opts); - ret = dict_set_dynstr (dict, key, mnt_options); + ret = dict_set_dynstr_with_alloc (dict, key, entry->mnt_opts); out: GF_FREE (mnt_pt); @@ -5279,7 +5273,6 @@ glusterd_add_brick_to_dict (glusterd_volinfo_t *volinfo, int ret = -1; int32_t pid = -1; int32_t brick_online = -1; - char *peer_id_str = NULL; char key[1024] = {0}; char base_key[1024] = {0}; char pidfile[PATH_MAX] = {0}; @@ -5310,16 +5303,11 @@ glusterd_add_brick_to_dict (glusterd_volinfo_t *volinfo, goto out; /* add peer uuid */ - peer_id_str = gf_strdup (uuid_utoa (brickinfo->uuid)); - if (!peer_id_str) { - ret = -1; - goto out; - } memset (key, 0, sizeof (key)); snprintf (key, sizeof (key), "%s.peerid", base_key); - ret = dict_set_dynstr (dict, key, peer_id_str); + ret = dict_set_dynstr_with_alloc (dict, key, + uuid_utoa (brickinfo->uuid)); if (ret) { - GF_FREE (peer_id_str); goto out; } @@ -5981,8 +5969,7 @@ glusterd_sm_tr_log_transition_add_to_dict (dict_t *dict, snprintf (key, sizeof (key), "log%d-time", count); gf_time_fmt (timestr, sizeof timestr, log->transitions[i].time, gf_timefmt_FT); - str = gf_strdup (timestr); - ret = dict_set_dynstr (dict, key, str); + ret = dict_set_dynstr_with_alloc (dict, key, timestr); if (ret) goto out; @@ -7389,7 +7376,7 @@ glusterd_append_gsync_status (dict_t *dst, dict_t *src) goto out; } - ret = dict_set_dynstr (dst, "gsync-status", gf_strdup (stop_msg)); + ret = dict_set_dynstr_with_alloc (dst, "gsync-status", stop_msg); if (ret) { gf_log ("glusterd", GF_LOG_WARNING, "Unable to set the stop" "message in the ctx dictionary"); @@ -7493,8 +7480,8 @@ glusterd_gsync_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict, char *op_errstr) ret = dict_get_str (rsp_dict, "conf_path", &conf_path); if (!ret && conf_path) { - ret = dict_set_dynstr (ctx, "conf_path", - gf_strdup(conf_path)); + ret = dict_set_dynstr_with_alloc (ctx, "conf_path", + conf_path); if (ret) { gf_log ("", GF_LOG_ERROR, "Unable to store conf path."); @@ -7503,7 +7490,8 @@ glusterd_gsync_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict, char *op_errstr) } } if ((op_errstr) && (strcmp ("", op_errstr))) { - ret = dict_set_dynstr (ctx, "errstr", gf_strdup(op_errstr)); + ret = dict_set_dynstr_with_alloc (ctx, "errstr", + op_errstr); if (ret) goto out; } -- cgit