From c1b3deff1186c581cb8d9893a3a0de6bf00d3adc Mon Sep 17 00:00:00 2001 From: Shehjar Tikoo Date: Thu, 16 Apr 2009 23:52:04 -0700 Subject: libglusterfsclient: Use macros for argument checks Having those if {} blocks to check for argument validity hurts the eye so this patch replaces those checks with macros. One macro already exists in libglusterfs. The second macro is introduced by this commit for libglusterfsclient-specific check for paths. Signed-off-by: Anand V. Avati --- libglusterfs/src/common-utils.h | 10 +++++ libglusterfsclient/src/libglusterfsclient.c | 59 +++++++++++------------------ 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index b3630d478a7..c90342f7a3d 100644 --- a/libglusterfs/src/common-utils.h +++ b/libglusterfs/src/common-utils.h @@ -116,6 +116,16 @@ extern char *gf_cbk_list[GF_CBK_MAXVALUE]; } \ } while (0); +#define GF_VALIDATE_ABSOLUTE_PATH_OR_GOTO(name,arg,label) \ + do { \ + GF_VALIDATE_OR_GOTO (name, arg, label); \ + if ((arg[0]) != '/') { \ + errno = EINVAL; \ + gf_log (name, GF_LOG_ERROR, \ + "invalid argument: " #arg); \ + goto label; \ + } \ + } while (0); #define GF_FILE_CONTENT_REQUESTED(_xattr_req,_content_limit) \ (dict_get_uint64 (_xattr_req, "glusterfs.content", _content_limit) == 0) diff --git a/libglusterfsclient/src/libglusterfsclient.c b/libglusterfsclient/src/libglusterfsclient.c index f5d6703665f..22145748f96 100755 --- a/libglusterfsclient/src/libglusterfsclient.c +++ b/libglusterfsclient/src/libglusterfsclient.c @@ -881,18 +881,15 @@ glusterfs_get (glusterfs_handle_t handle, size_t size, struct stat *stbuf) { - int32_t op_ret = 0; + int32_t op_ret = -1; loc_t loc = {0, }; libglusterfs_client_ctx_t *ctx = handle; dict_t *dict = NULL; dict_t *xattr_req = NULL; char *name = NULL, *pathname = NULL; - if (!ctx || !path || path[0] != '/') { - errno = EINVAL; - op_ret = -1; - goto out; - } + GF_VALIDATE_OR_GOTO (LIBGF_XL_NAME, ctx, out); + GF_VALIDATE_ABSOLUTE_PATH_OR_GOTO (LIBGF_XL_NAME, path, out); loc.path = strdup (path); op_ret = libgf_client_path_lookup (&loc, ctx, 0); @@ -1264,16 +1261,19 @@ glusterfs_getxattr (glusterfs_handle_t handle, void *value, size_t size) { - int32_t op_ret = 0; + int32_t op_ret = -1; loc_t loc = {0, }; dict_t *dict = NULL; libglusterfs_client_ctx_t *ctx = handle; char *file = NULL; dict_t *xattr_req = NULL; - if (!ctx || !path || path[0] != '/' || !name || name[0] == '\0') { + GF_VALIDATE_OR_GOTO (LIBGF_XL_NAME, ctx, out); + GF_VALIDATE_ABSOLUTE_PATH_OR_GOTO (LIBGF_XL_NAME, path, out); + GF_VALIDATE_OR_GOTO (LIBGF_XL_NAME, name, out); + + if (name[0] == '\0') { errno = EINVAL; - op_ret = -1; goto out; } @@ -1508,18 +1508,15 @@ glusterfs_open (glusterfs_handle_t handle, mode_t mode) { loc_t loc = {0, }; - long op_ret = 0; + long op_ret = -1; fd_t *fd = NULL; int32_t ret = -1; xlator_t *this = NULL; libglusterfs_client_ctx_t *ctx = handle; char *name = NULL, *pathname = NULL; - if (!ctx || !path || path[0] != '/') { - errno = EINVAL; - op_ret = -1; - goto out; - } + GF_VALIDATE_OR_GOTO (LIBGF_XL_NAME, ctx, out); + GF_VALIDATE_ABSOLUTE_PATH_OR_GOTO (LIBGF_XL_NAME, path, out); loc.path = strdup (path); op_ret = libgf_client_path_lookup (&loc, ctx, 1); @@ -1784,17 +1781,14 @@ glusterfs_setxattr (glusterfs_handle_t handle, size_t size, int flags) { - int32_t op_ret = 0; + int32_t op_ret = -1; loc_t loc = {0, }; xlator_t *this = NULL; libglusterfs_client_ctx_t *ctx = handle; char *file = NULL; - if (!ctx || !path || path[0] != '/') { - errno = EINVAL; - op_ret = -1; - goto out; - } + GF_VALIDATE_OR_GOTO (LIBGF_XL_NAME, ctx, out); + GF_VALIDATE_ABSOLUTE_PATH_OR_GOTO (LIBGF_XL_NAME, path, out); loc.path = strdup (path); op_ret = libgf_client_path_lookup (&loc, ctx, 1); @@ -3118,17 +3112,14 @@ glusterfs_stat (glusterfs_handle_t handle, const char *path, struct stat *buf) { - int32_t op_ret = 0; + int32_t op_ret = -1; loc_t loc = {0, }; libglusterfs_client_ctx_t *ctx = handle; xlator_t *this = NULL; char *name = NULL, *pathname = NULL; - if (!ctx || !path || path[0] != '/') { - errno = EINVAL; - op_ret = -1; - goto out; - } + GF_VALIDATE_OR_GOTO (LIBGF_XL_NAME, ctx, out); + GF_VALIDATE_ABSOLUTE_PATH_OR_GOTO (LIBGF_XL_NAME, path, out); loc.path = strdup (path); op_ret = libgf_client_path_lookup (&loc, ctx, 1); @@ -3363,11 +3354,8 @@ glusterfs_mkdir (glusterfs_handle_t handle, char *pathname = NULL, *name = NULL; int32_t op_ret = -1; - if (!ctx || !path || path[0] != '/') { - errno = EINVAL; - op_ret = -1; - goto out; - } + GF_VALIDATE_OR_GOTO (LIBGF_XL_NAME, ctx, out); + GF_VALIDATE_ABSOLUTE_PATH_OR_GOTO (LIBGF_XL_NAME, path, out); loc.path = strdup (path); op_ret = libgf_client_path_lookup (&loc, ctx, 1); @@ -3461,11 +3449,8 @@ glusterfs_rmdir (glusterfs_handle_t handle, char *pathname = NULL, *name = NULL; int32_t op_ret = -1; - if (!ctx || !path || path[0] != '/') { - errno = EINVAL; - op_ret = -1; - goto out; - } + GF_VALIDATE_OR_GOTO (LIBGF_XL_NAME, ctx, out); + GF_VALIDATE_ABSOLUTE_PATH_OR_GOTO (LIBGF_XL_NAME, path, out); loc.path = strdup (path); op_ret = libgf_client_path_lookup (&loc, ctx, 1); -- cgit