From f6d6c0f65f7b512724694b3436358e1113cbfccb Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Wed, 28 Aug 2019 19:38:01 +0300 Subject: posix*.c: remove unneeded strlen() calls In various places, we can re-use knowledge of string length or result of snprintf() and such instead of strlen(). Change-Id: I4c9b1decf1169b3f8ac83699a0afbd7c38fad746 updates: bz#1193929 Signed-off-by: Yaniv Kaul --- xlators/storage/posix/src/posix-common.c | 2 +- xlators/storage/posix/src/posix-gfid-path.c | 35 +++++++++++++------------- xlators/storage/posix/src/posix-handle.h | 2 +- xlators/storage/posix/src/posix-helpers.c | 26 +++++++++---------- xlators/storage/posix/src/posix-inode-fd-ops.c | 11 +++----- xlators/storage/posix/src/posix.h | 2 +- 6 files changed, 37 insertions(+), 41 deletions(-) (limited to 'xlators/storage/posix/src') diff --git a/xlators/storage/posix/src/posix-common.c b/xlators/storage/posix/src/posix-common.c index eee39fd000f..231723b6bef 100644 --- a/xlators/storage/posix/src/posix-common.c +++ b/xlators/storage/posix/src/posix-common.c @@ -620,7 +620,7 @@ posix_init(xlator_t *this) } _private->base_path = gf_strdup(dir_data->data); - _private->base_path_length = strlen(_private->base_path); + _private->base_path_length = dir_data->len - 1; ret = dict_get_str(this->options, "hostname", &_private->hostname); if (ret) { diff --git a/xlators/storage/posix/src/posix-gfid-path.c b/xlators/storage/posix/src/posix-gfid-path.c index d7ba324a95f..37b68c25d8d 100644 --- a/xlators/storage/posix/src/posix-gfid-path.c +++ b/xlators/storage/posix/src/posix-gfid-path.c @@ -30,26 +30,25 @@ posix_set_gfid2path_xattr(xlator_t *this, const char *path, uuid_t pgfid, }; char *key = NULL; char *val = NULL; - size_t key_size = 0; - size_t val_size = 0; + const size_t key_size = GFID2PATH_XATTR_KEY_PREFIX_LENGTH + + GF_XXH64_DIGEST_LENGTH * 2 + 1; + const size_t val_size = UUID_CANONICAL_FORM_LEN + NAME_MAX + 2; int ret = 0; + int len; GF_VALIDATE_OR_GOTO("posix", this, err); - snprintf(pgfid_bname, sizeof(pgfid_bname), "%s/%s", uuid_utoa(pgfid), - bname); - gf_xxh64_wrapper((unsigned char *)pgfid_bname, strlen(pgfid_bname), + len = snprintf(pgfid_bname, sizeof(pgfid_bname), "%s/%s", uuid_utoa(pgfid), + bname); + gf_xxh64_wrapper((unsigned char *)pgfid_bname, len, GF_XXHSUM64_DEFAULT_SEED, xxh64); - key_size = GFID2PATH_XATTR_KEY_PREFIX_LENGTH + GF_XXH64_DIGEST_LENGTH * 2 + - 1; key = alloca(key_size); snprintf(key, key_size, GFID2PATH_XATTR_KEY_PREFIX "%s", xxh64); - val_size = UUID_CANONICAL_FORM_LEN + NAME_MAX + 2; val = alloca(val_size); - snprintf(val, val_size, "%s/%s", uuid_utoa(pgfid), bname); + len = snprintf(val, val_size, "%s/%s", uuid_utoa(pgfid), bname); - ret = sys_lsetxattr(path, key, val, strlen(val), XATTR_CREATE); + ret = sys_lsetxattr(path, key, val, len, XATTR_CREATE); if (ret == -1) { gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_PGFID_OP, "setting gfid2path xattr failed on %s: key = %s ", path, key); @@ -72,16 +71,16 @@ posix_remove_gfid2path_xattr(xlator_t *this, const char *path, uuid_t pgfid, }; int ret = 0; char *key = NULL; - size_t key_size = 0; + const size_t key_size = GFID2PATH_XATTR_KEY_PREFIX_LENGTH + + GF_XXH64_DIGEST_LENGTH * 2 + 1; + int len; GF_VALIDATE_OR_GOTO("posix", this, err); - snprintf(pgfid_bname, sizeof(pgfid_bname), "%s/%s", uuid_utoa(pgfid), - bname); - gf_xxh64_wrapper((unsigned char *)pgfid_bname, strlen(pgfid_bname), + len = snprintf(pgfid_bname, sizeof(pgfid_bname), "%s/%s", uuid_utoa(pgfid), + bname); + gf_xxh64_wrapper((unsigned char *)pgfid_bname, len, GF_XXHSUM64_DEFAULT_SEED, xxh64); - key_size = GFID2PATH_XATTR_KEY_PREFIX_LENGTH + GF_XXH64_DIGEST_LENGTH * 2 + - 1; key = alloca(key_size); snprintf(key, key_size, GFID2PATH_XATTR_KEY_PREFIX "%s", xxh64); @@ -214,7 +213,8 @@ posix_get_gfid2path(xlator_t *this, inode_t *inode, const char *real_path, remaining_size = size; list_offset = 0; while (remaining_size > 0) { - snprintf(keybuffer, sizeof(keybuffer), "%s", list + list_offset); + len = snprintf(keybuffer, sizeof(keybuffer), "%s", + list + list_offset); if (!posix_is_gfid2path_xattr(keybuffer)) { goto ignore; @@ -244,7 +244,6 @@ posix_get_gfid2path(xlator_t *this, inode_t *inode, const char *real_path, i++; ignore: - len = strlen(keybuffer); remaining_size -= (len + 1); list_offset += (len + 1); } /* while (remaining_size > 0) */ diff --git a/xlators/storage/posix/src/posix-handle.h b/xlators/storage/posix/src/posix-handle.h index c4d7cb14503..3e9020c0f8a 100644 --- a/xlators/storage/posix/src/posix-handle.h +++ b/xlators/storage/posix/src/posix-handle.h @@ -18,7 +18,7 @@ #define MAKE_PGFID_XATTR_KEY(var, prefix, pgfid) \ do { \ - var = alloca(strlen(prefix) + UUID_CANONICAL_FORM_LEN + 1); \ + var = alloca(SLEN(prefix) + UUID_CANONICAL_FORM_LEN + 1); \ strcpy(var, prefix); \ strcat(var, uuid_utoa(pgfid)); \ } while (0) diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c index dd1ec1e0b77..c185b4d9049 100644 --- a/xlators/storage/posix/src/posix-helpers.c +++ b/xlators/storage/posix/src/posix-helpers.c @@ -358,11 +358,10 @@ _posix_get_marker_all_contributions(posix_xattr_filler_t *filler) list_offset = 0; while (remaining_size > 0) { - snprintf(key, sizeof(key), "%s", list + list_offset); + len = snprintf(key, sizeof(key), "%s", list + list_offset); if (fnmatch(marker_contri_key, key, 0) == 0) { ret = _posix_xattr_get_set_from_backend(filler, key); } - len = strlen(key); remaining_size -= (len + 1); list_offset += (len + 1); } @@ -566,7 +565,7 @@ _posix_xattr_get_set(dict_t *xattr_req, char *key, data_t *data, /* ACL_TYPE_DEFAULT is not supported for non-directory, skip */ if (!IA_ISDIR(stbuf.ia_type) && - !strncmp(key, GF_POSIX_ACL_DEFAULT, strlen(GF_POSIX_ACL_DEFAULT))) + !strncmp(key, GF_POSIX_ACL_DEFAULT, SLEN(GF_POSIX_ACL_DEFAULT))) goto out; ret = posix_pacl_get(filler->real_path, filler->fdnum, key, &value); @@ -1204,10 +1203,11 @@ posix_handle_pair(xlator_t *this, const char *real_path, char *key, if (stbuf && IS_DHT_LINKFILE_MODE(stbuf)) goto out; ret = posix_pacl_set(real_path, -1, key, value->data); - } else if (!strncmp(key, POSIX_ACL_ACCESS_XATTR, strlen(key)) && stbuf && - IS_DHT_LINKFILE_MODE(stbuf)) { + } else if (!strncmp(key, POSIX_ACL_ACCESS_XATTR, + SLEN(POSIX_ACL_ACCESS_XATTR)) && + stbuf && IS_DHT_LINKFILE_MODE(stbuf)) { goto out; - } else if (!strncmp(key, GF_INTERNAL_CTX_KEY, strlen(key))) { + } else if (!strncmp(key, GF_INTERNAL_CTX_KEY, SLEN(GF_INTERNAL_CTX_KEY))) { /* ignore this key value pair */ ret = 0; goto out; @@ -1257,8 +1257,9 @@ posix_fhandle_pair(call_frame_t *frame, xlator_t *this, int fd, char *key, } else if (posix_is_gfid2path_xattr(key)) { ret = -ENOTSUP; goto out; - } else if (!strncmp(key, POSIX_ACL_ACCESS_XATTR, strlen(key)) && stbuf && - IS_DHT_LINKFILE_MODE(stbuf)) { + } else if (!strncmp(key, POSIX_ACL_ACCESS_XATTR, + SLEN(POSIX_ACL_ACCESS_XATTR)) && + stbuf && IS_DHT_LINKFILE_MODE(stbuf)) { goto out; } @@ -2760,10 +2761,10 @@ posix_resolve_dirgfid_to_path(const uuid_t dirgfid, const char *brick_path, linkname[len] = '\0'; - pgfidstr = strtok_r(linkname + strlen("../../00/00/"), "/", &saveptr); + pgfidstr = strtok_r(linkname + SLEN("../../00/00/"), "/", &saveptr); dir_name = strtok_r(NULL, "/", &saveptr); - if (strlen(pre_dir_name) != 0) { /* Remove '/' at the end */ + if (pre_dir_name[0] != '\0') { /* Remove '/' at the end */ len = snprintf(result, PATH_MAX, "%s/%s", dir_name, pre_dir_name); } else { len = snprintf(result, PATH_MAX, "%s", dir_name); @@ -2891,7 +2892,7 @@ posix_inode_ctx_get_all(inode_t *inode, xlator_t *this, posix_inode_ctx_t **ctx) gf_boolean_t posix_is_bulk_removexattr(char *name, dict_t *xdata) { - if (name && (strlen(name) == 0) && xdata) + if (name && (name[0] == '\0') && xdata) return _gf_true; return _gf_false; } @@ -3509,8 +3510,7 @@ posix_update_iatt_buf(struct iatt *buf, int fd, char *loc, dict_t *xattr_req) if (!xattr_req) return; - if (!(dict_getn(xattr_req, GF_CS_OBJECT_STATUS, - strlen(GF_CS_OBJECT_STATUS)))) + if (!dict_get_sizen(xattr_req, GF_CS_OBJECT_STATUS)) return; if (fd != -1) { diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c index 6d2b3106a9a..497502e5d42 100644 --- a/xlators/storage/posix/src/posix-inode-fd-ops.c +++ b/xlators/storage/posix/src/posix-inode-fd-ops.c @@ -109,7 +109,7 @@ posix_cs_build_xattr_rsp(xlator_t *this, dict_t **rsp, dict_t *req, int fd, int ret = 0; uuid_t uuid; - if (!(dict_getn(req, GF_CS_OBJECT_STATUS, strlen(GF_CS_OBJECT_STATUS)))) + if (!dict_get_sizen(req, GF_CS_OBJECT_STATUS)) return; if (!(*rsp)) { @@ -120,8 +120,7 @@ posix_cs_build_xattr_rsp(xlator_t *this, dict_t **rsp, dict_t *req, int fd, } if (fd != -1) { - if (dict_getn(req, GF_CS_XATTR_ARCHIVE_UUID, - strlen(GF_CS_XATTR_ARCHIVE_UUID))) { + if (dict_get_sizen(req, GF_CS_XATTR_ARCHIVE_UUID)) { ret = sys_fgetxattr(fd, GF_CS_XATTR_ARCHIVE_UUID, uuid, 16); if (ret > 0) { ret = dict_set_gfuuid(*rsp, GF_CS_XATTR_ARCHIVE_UUID, uuid, @@ -138,8 +137,7 @@ posix_cs_build_xattr_rsp(xlator_t *this, dict_t **rsp, dict_t *req, int fd, } } } else { - if (dict_getn(req, GF_CS_XATTR_ARCHIVE_UUID, - strlen(GF_CS_XATTR_ARCHIVE_UUID))) { + if (dict_get_sizen(req, GF_CS_XATTR_ARCHIVE_UUID)) { ret = sys_lgetxattr(loc, GF_CS_XATTR_ARCHIVE_UUID, uuid, 16); if (ret > 0) { ret = dict_set_gfuuid(*rsp, GF_CS_XATTR_ARCHIVE_UUID, uuid, @@ -3255,7 +3253,7 @@ posix_get_ancestry_non_directory(xlator_t *this, inode_t *leaf_inode, } while (remaining_size > 0) { - snprintf(key, sizeof(key), "%s", list + list_offset); + len = snprintf(key, sizeof(key), "%s", list + list_offset); if (strncmp(key, PGFID_XATTR_KEY_PREFIX, SLEN(PGFID_XATTR_KEY_PREFIX)) != 0) goto next; @@ -3303,7 +3301,6 @@ posix_get_ancestry_non_directory(xlator_t *this, inode_t *leaf_inode, } next: - len = strlen(key); remaining_size -= (len + 1); list_offset += (len + 1); } /* while (remaining_size > 0) */ diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h index 9c04c55db02..9d670625d94 100644 --- a/xlators/storage/posix/src/posix.h +++ b/xlators/storage/posix/src/posix.h @@ -296,7 +296,7 @@ typedef struct { char gfid_str[64] = {0}; \ uuid_utoa_r(gfid, gfid_str); \ path_len = strlen(base_path) + 1 + SLEN(GF_UNLINK_PATH) + 1 + \ - strlen(gfid_str) + 1; \ + UUID_CANONICAL_FORM_LEN + 1; \ unlink_path = alloca(path_len); \ if (!unlink_path) { \ gf_msg("posix", GF_LOG_ERROR, ENOMEM, P_MSG_UNLINK_FAILED, \ -- cgit