From 0418e53b3c8248f4b03f3b8f47b4b91203e699a5 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 17 Nov 2017 19:50:36 -0800 Subject: [PATCH] Reduce use of preprocessor This provides type-safety and avoids token expansion side effects. --- src/common.h | 2 +- src/curl.cpp | 41 +++++++++++++++++++------------------- src/curl.h | 10 ++++++---- src/fdcache.cpp | 2 +- src/gnutls_auth.cpp | 8 ++------ src/s3fs.cpp | 48 +++++++++++++++++++++++---------------------- src/s3fs.h | 3 ++- src/string_util.h | 5 +++-- 8 files changed, 61 insertions(+), 58 deletions(-) diff --git a/src/common.h b/src/common.h index 45396d3..84d0747 100644 --- a/src/common.h +++ b/src/common.h @@ -37,7 +37,7 @@ // // Macro // -#define SAFESTRPTR(strptr) (strptr ? strptr : "") +static inline const char *SAFESTRPTR(const char *strptr) { return strptr ? strptr : ""; } // // Debug level diff --git a/src/curl.cpp b/src/curl.cpp index 02e326f..83ae3e2 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -159,10 +159,11 @@ static string tolower_header_name(const char* head) //------------------------------------------------------------------- // Class BodyData //------------------------------------------------------------------- -#define BODYDATA_RESIZE_APPEND_MIN (1 * 1024) // 1KB -#define BODYDATA_RESIZE_APPEND_MID (1 * 1024 * 1024) // 1MB -#define BODYDATA_RESIZE_APPEND_MAX (10 * 1024 * 1024) // 10MB -#define AJUST_BLOCK(bytes, block) (((bytes / block) + ((bytes % block) ? 1 : 0)) * block) +static const int BODYDATA_RESIZE_APPEND_MIN = 1024; +static const int BODYDATA_RESIZE_APPEND_MID = 1024 * 1024; +static const int BODYDATA_RESIZE_APPEND_MAX = 10 * 1024 * 1024; + +static size_t adjust_block(size_t bytes, size_t block) { return ((bytes / block) + ((bytes % block) ? 1 : 0)) * block; } bool BodyData::Resize(size_t addbytes) { @@ -171,7 +172,7 @@ bool BodyData::Resize(size_t addbytes) } // New size - size_t need_size = AJUST_BLOCK((lastpos + addbytes + 1) - bufsize, sizeof(off_t)); + size_t need_size = adjust_block((lastpos + addbytes + 1) - bufsize, sizeof(off_t)); if(BODYDATA_RESIZE_APPEND_MAX < bufsize){ need_size = (BODYDATA_RESIZE_APPEND_MAX < need_size ? need_size : BODYDATA_RESIZE_APPEND_MAX); @@ -318,20 +319,20 @@ void CurlHandlerPool::ReturnHandler(CURL* h) //------------------------------------------------------------------- // Class S3fsCurl //------------------------------------------------------------------- -#define MULTIPART_SIZE 10485760 // 10MB -#define MAX_MULTI_COPY_SOURCE_SIZE 524288000 // 500MB +static const int MULTIPART_SIZE = 10 * 1024 * 1024; +static const int MAX_MULTI_COPY_SOURCE_SIZE = 500 * 1024 * 1024; -#define IAM_EXPIRE_MERGIN (20 * 60) // update timing -#define IAM_CRED_URL_ECS "http://169.254.170.2" -#define IAM_CRED_URL "http://169.254.169.254/latest/meta-data/iam/security-credentials/" -#define ECS_IAM_ENV_VAR "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" -#define IAMCRED_ACCESSKEYID "AccessKeyId" -#define IAMCRED_SECRETACCESSKEY "SecretAccessKey" -#define IAMCRED_ACCESSTOKEN "Token" -#define IAMCRED_EXPIRATION "Expiration" -#define IAMCRED_ROLEARN "RoleArn" -#define IAMCRED_KEYCOUNT 4 -#define IAMCRED_KEYCOUNT_ECS 5 +static const int IAM_EXPIRE_MERGIN = 20 * 60; // update timing +static const std::string IAM_CRED_URL_ECS = "http://169.254.170.2"; +static const std::string IAM_CRED_URL = "http://169.254.169.254/latest/meta-data/iam/security-credentials/"; +static const std::string ECS_IAM_ENV_VAR = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"; +static const std::string IAMCRED_ACCESSKEYID = "AccessKeyId"; +static const std::string IAMCRED_SECRETACCESSKEY = "SecretAccessKey"; +static const std::string IAMCRED_ACCESSTOKEN = "Token"; +static const std::string IAMCRED_EXPIRATION = "Expiration"; +static const std::string IAMCRED_ROLEARN = "RoleArn"; +static const int IAMCRED_KEYCOUNT = 4; +static const int IAMCRED_KEYCOUNT_ECS = 5; // [NOTICE] // This symbol is for libcurl under 7.23.0 @@ -2431,7 +2432,7 @@ int S3fsCurl::GetIAMCredentials(void) // url if (is_ecs) { - url = string(IAM_CRED_URL_ECS) + std::getenv(ECS_IAM_ENV_VAR); + url = string(IAM_CRED_URL_ECS) + std::getenv(ECS_IAM_ENV_VAR.c_str()); } else { url = string(IAM_CRED_URL) + S3fsCurl::IAM_role; @@ -3699,7 +3700,7 @@ int S3fsCurl::MultipartRenameRequest(const char* from, const char* to, headers_t //------------------------------------------------------------------- // Class S3fsMultiCurl //------------------------------------------------------------------- -#define MAX_MULTI_HEADREQ 20 // default: max request count in readdir curl_multi. +static const int MAX_MULTI_HEADREQ = 20; // default: max request count in readdir curl_multi. //------------------------------------------------------------------- // Class method for S3fsMultiCurl diff --git a/src/curl.h b/src/curl.h index 954b6aa..fe2ec85 100644 --- a/src/curl.h +++ b/src/curl.h @@ -26,7 +26,7 @@ //---------------------------------------------- // Symbols //---------------------------------------------- -#define MIN_MULTIPART_SIZE 5242880 // 5MB +static const int MIN_MULTIPART_SIZE = 5 * 1024 * 1024; //---------------------------------------------- // class BodyData @@ -175,9 +175,11 @@ enum sse_type_t { }; // share -#define SHARE_MUTEX_DNS 0 -#define SHARE_MUTEX_SSL_SESSION 1 -#define SHARE_MUTEX_MAX 2 +enum { + SHARE_MUTEX_DNS = 0, + SHARE_MUTEX_SSL_SESSION = 1, + SHARE_MUTEX_MAX = 2, +}; // Class for lapping curl // diff --git a/src/fdcache.cpp b/src/fdcache.cpp index a358186..6894996 100644 --- a/src/fdcache.cpp +++ b/src/fdcache.cpp @@ -52,7 +52,7 @@ using namespace std; //------------------------------------------------ // Symbols //------------------------------------------------ -#define MAX_MULTIPART_CNT 10000 // S3 multipart max count +static const int MAX_MULTIPART_CNT = 10 * 1000; // S3 multipart max count // // For cache directory top path diff --git a/src/gnutls_auth.cpp b/src/gnutls_auth.cpp index 2d483f9..b81cf9a 100644 --- a/src/gnutls_auth.cpp +++ b/src/gnutls_auth.cpp @@ -186,11 +186,9 @@ bool s3fs_HMAC256(const void* key, size_t keylen, const unsigned char* data, siz //------------------------------------------------------------------- // Utility Function for MD5 //------------------------------------------------------------------- -#define MD5_DIGEST_LENGTH 16 - size_t get_md5_digest_length(void) { - return MD5_DIGEST_LENGTH; + return 16; } #ifdef USE_GNUTLS_NETTLE @@ -298,11 +296,9 @@ unsigned char* s3fs_md5hexsum(int fd, off_t start, ssize_t size) //------------------------------------------------------------------- // Utility Function for SHA256 //------------------------------------------------------------------- -#define SHA256_DIGEST_LENGTH 32 - size_t get_sha256_digest_length(void) { - return SHA256_DIGEST_LENGTH; + return 32; } #ifdef USE_GNUTLS_NETTLE diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 9320a23..46671ec 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -58,14 +58,16 @@ using namespace std; //------------------------------------------------------------------- // Define //------------------------------------------------------------------- -#define DIRTYPE_UNKNOWN -1 -#define DIRTYPE_NEW 0 -#define DIRTYPE_OLD 1 -#define DIRTYPE_FOLDER 2 -#define DIRTYPE_NOOBJ 3 +enum dirtype { + DIRTYPE_UNKNOWN = -1, + DIRTYPE_NEW = 0, + DIRTYPE_OLD = 1, + DIRTYPE_FOLDER = 2, + DIRTYPE_NOOBJ = 3, +}; -#define IS_REPLACEDIR(type) (DIRTYPE_OLD == type || DIRTYPE_FOLDER == type || DIRTYPE_NOOBJ == type) -#define IS_RMTYPEDIR(type) (DIRTYPE_OLD == type || DIRTYPE_FOLDER == type) +static bool IS_REPLACEDIR(dirtype type) { return DIRTYPE_OLD == type || DIRTYPE_FOLDER == type || DIRTYPE_NOOBJ == type; } +static bool IS_RMTYPEDIR(dirtype type) { return DIRTYPE_OLD == type || DIRTYPE_FOLDER == type; } #if !defined(ENOATTR) #define ENOATTR ENODATA @@ -138,8 +140,8 @@ static bool set_s3fs_usr2_handler(void); static s3fs_log_level set_s3fs_log_level(s3fs_log_level level); static s3fs_log_level bumpup_s3fs_log_level(void); static bool is_special_name_folder_object(const char* path); -static int chk_dir_object_type(const char* path, string& newpath, string& nowpath, string& nowcache, headers_t* pmeta = NULL, int* pDirType = NULL); -static int remove_old_type_dir(const string& path, int dirtype); +static int chk_dir_object_type(const char* path, string& newpath, string& nowpath, string& nowcache, headers_t* pmeta = NULL, dirtype* pDirType = NULL); +static int remove_old_type_dir(const string& path, dirtype type); static int get_object_attribute(const char* path, struct stat* pstbuf, headers_t* pmeta = NULL, bool overcheck = true, bool* pisforce = NULL, bool add_no_truncate_cache = false); static int check_object_access(const char* path, int mask, struct stat* pstbuf); static int check_object_owner(const char* path, struct stat* pstbuf); @@ -315,12 +317,12 @@ static bool is_special_name_folder_object(const char* path) // pmeta: headers map // pDirType: directory object type // -static int chk_dir_object_type(const char* path, string& newpath, string& nowpath, string& nowcache, headers_t* pmeta, int* pDirType) +static int chk_dir_object_type(const char* path, string& newpath, string& nowpath, string& nowcache, headers_t* pmeta, dirtype* pDirType) { - int TypeTmp; + dirtype TypeTmp; int result = -1; bool isforce = false; - int* pType = pDirType ? pDirType : &TypeTmp; + dirtype* pType = pDirType ? pDirType : &TypeTmp; // Normalize new path. newpath = path; @@ -393,9 +395,9 @@ static int chk_dir_object_type(const char* path, string& newpath, string& nowpat return result; } -static int remove_old_type_dir(const string& path, int dirtype) +static int remove_old_type_dir(const string& path, dirtype type) { - if(IS_RMTYPEDIR(dirtype)){ + if(IS_RMTYPEDIR(type)){ S3fsCurl s3fscurl; int result = s3fscurl.DeleteRequest(path.c_str()); if(0 != result && -ENOENT != result){ @@ -1355,7 +1357,7 @@ static int rename_directory(const char* from, const char* to) string basepath = strfrom + "/"; string newpath; // should be from name(not used) string nowcache; // now cache path(not used) - int DirType; + dirtype DirType; bool normdir; MVNODE* mn_head = NULL; MVNODE* mn_tail = NULL; @@ -1533,7 +1535,7 @@ static int s3fs_chmod(const char* path, mode_t mode) string nowcache; headers_t meta; struct stat stbuf; - int nDirType = DIRTYPE_UNKNOWN; + dirtype nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO("[path=%s][mode=%04o]", path, mode); @@ -1607,7 +1609,7 @@ static int s3fs_chmod_nocopy(const char* path, mode_t mode) string newpath; string nowcache; struct stat stbuf; - int nDirType = DIRTYPE_UNKNOWN; + dirtype nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO1("[path=%s][mode=%04o]", path, mode); @@ -1684,7 +1686,7 @@ static int s3fs_chown(const char* path, uid_t uid, gid_t gid) string nowcache; headers_t meta; struct stat stbuf; - int nDirType = DIRTYPE_UNKNOWN; + dirtype nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO("[path=%s][uid=%u][gid=%u]", path, (unsigned int)uid, (unsigned int)gid); @@ -1753,7 +1755,7 @@ static int s3fs_chown_nocopy(const char* path, uid_t uid, gid_t gid) string newpath; string nowcache; struct stat stbuf; - int nDirType = DIRTYPE_UNKNOWN; + dirtype nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO1("[path=%s][uid=%u][gid=%u]", path, (unsigned int)uid, (unsigned int)gid); @@ -1838,7 +1840,7 @@ static int s3fs_utimens(const char* path, const struct timespec ts[2]) string nowcache; headers_t meta; struct stat stbuf; - int nDirType = DIRTYPE_UNKNOWN; + dirtype nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO("[path=%s][mtime=%jd]", path, (intmax_t)(ts[1].tv_sec)); @@ -1902,7 +1904,7 @@ static int s3fs_utimens_nocopy(const char* path, const struct timespec ts[2]) string newpath; string nowcache; struct stat stbuf; - int nDirType = DIRTYPE_UNKNOWN; + dirtype nDirType = DIRTYPE_UNKNOWN; S3FS_PRN_INFO1("[path=%s][mtime=%s]", path, str(ts[1].tv_sec).c_str()); @@ -3031,7 +3033,7 @@ static int s3fs_setxattr(const char* path, const char* name, const char* value, string nowcache; headers_t meta; struct stat stbuf; - int nDirType = DIRTYPE_UNKNOWN; + dirtype nDirType = DIRTYPE_UNKNOWN; if(0 == strcmp(path, "/")){ S3FS_PRN_ERR("Could not change mode for mount point."); @@ -3250,7 +3252,7 @@ static int s3fs_removexattr(const char* path, const char* name) headers_t meta; xattrs_t xattrs; struct stat stbuf; - int nDirType = DIRTYPE_UNKNOWN; + dirtype nDirType = DIRTYPE_UNKNOWN; if(0 == strcmp(path, "/")){ S3FS_PRN_ERR("Could not change mode for mount point."); diff --git a/src/s3fs.h b/src/s3fs.h index d0cd3b5..142d6e6 100644 --- a/src/s3fs.h +++ b/src/s3fs.h @@ -21,7 +21,8 @@ #define S3FS_S3_H_ #define FUSE_USE_VERSION 26 -#define FIVE_GB 5368709120LL + +static const int64_t FIVE_GB = 5LL * 1024LL * 1024LL * 1024LL; #include diff --git a/src/string_util.h b/src/string_util.h index 5557a38..f72a6ed 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -30,8 +30,9 @@ #include #include -#define SPACES " \t\r\n" -#define STR2NCMP(str1, str2) strncmp(str1, str2, strlen(str2)) +static const std::string SPACES = " \t\r\n"; + +static inline int STR2NCMP(const char *str1, const char *str2) { return strncmp(str1, str2, strlen(str2)); } template std::string str(T value) { std::stringstream s;