From 1f04165a33a1923c94f2f51681edb1f8f11cdad5 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 28 Jul 2023 18:21:55 +0900 Subject: [PATCH] Convert most str callers to C++11 std::to_string (#2238) Remaining ones handle timespec. --- src/curl.cpp | 12 ++++---- src/fdcache_entity.cpp | 6 ++-- src/s3fs.cpp | 64 ++++++++++++++++++++-------------------- src/s3fs_util.cpp | 2 +- src/string_util.cpp | 23 ++------------- src/string_util.h | 8 ++--- src/test_string_util.cpp | 8 ----- 7 files changed, 47 insertions(+), 76 deletions(-) diff --git a/src/curl.cpp b/src/curl.cpp index 5b96183..71f3c26 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -3007,7 +3007,7 @@ int S3fsCurl::GetIAMv2ApiToken(const char* token_url, int token_ttl, const char* responseHeaders.clear(); bodydata.Clear(); - std::string ttlstr = str(token_ttl); + std::string ttlstr = std::to_string(token_ttl); requestHeaders = curl_slist_sort_insert(requestHeaders, token_ttl_hdr, ttlstr.c_str()); // Curl appends an "Expect: 100-continue" header to the token request, @@ -3595,9 +3595,9 @@ int S3fsCurl::PreGetObjectRequest(const char* tpath, int fd, off_t start, off_t if(0 < size){ std::string range = "bytes="; - range += str(start); + range += std::to_string(start); range += "-"; - range += str(start + size - 1); + range += std::to_string(start + size - 1); requestHeaders = curl_slist_sort_insert(requestHeaders, "Range", range.c_str()); } // SSE-C @@ -3914,7 +3914,7 @@ int S3fsCurl::CompleteMultipartPostRequest(const char* tpath, const std::string& return -EIO; } postContent += "\n"; - postContent += " " + str(it->part_num) + "\n"; + postContent += " " + std::to_string(it->part_num) + "\n"; postContent += " " + it->etag + "\n"; postContent += "\n"; } @@ -4135,7 +4135,7 @@ int S3fsCurl::UploadMultipartPostSetup(const char* tpath, int part_num, const st // Encode the upload_id here. // In compatible S3 servers(Cloudflare, etc), there are cases where characters that require URL encoding are included. // - query_string = "partNumber=" + str(part_num) + "&uploadId=" + urlEncodeGeneral(upload_id); + query_string = "partNumber=" + std::to_string(part_num) + "&uploadId=" + urlEncodeGeneral(upload_id); std::string urlargs = "?" + query_string; std::string resource; std::string turl; @@ -4207,7 +4207,7 @@ int S3fsCurl::CopyMultipartPostSetup(const char* from, const char* to, int part_ // Encode the upload_id here. // In compatible S3 servers(Cloudflare, etc), there are cases where characters that require URL encoding are included. // - query_string = "partNumber=" + str(part_num) + "&uploadId=" + urlEncodeGeneral(upload_id); + query_string = "partNumber=" + std::to_string(part_num) + "&uploadId=" + urlEncodeGeneral(upload_id); std::string urlargs = "?" + query_string; std::string resource; std::string turl; diff --git a/src/fdcache_entity.cpp b/src/fdcache_entity.cpp index 46eec63..cdf5a16 100644 --- a/src/fdcache_entity.cpp +++ b/src/fdcache_entity.cpp @@ -1010,21 +1010,21 @@ bool FdEntity::SetXattr(const std::string& xattr) bool FdEntity::SetMode(mode_t mode) { AutoLock auto_lock(&fdent_lock); - orgmeta["x-amz-meta-mode"] = str(mode); + orgmeta["x-amz-meta-mode"] = std::to_string(mode); return true; } bool FdEntity::SetUId(uid_t uid) { AutoLock auto_lock(&fdent_lock); - orgmeta["x-amz-meta-uid"] = str(uid); + orgmeta["x-amz-meta-uid"] = std::to_string(uid); return true; } bool FdEntity::SetGId(gid_t gid) { AutoLock auto_lock(&fdent_lock); - orgmeta["x-amz-meta-gid"] = str(gid); + orgmeta["x-amz-meta-gid"] = std::to_string(gid); return true; } diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 829bd43..896264c 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -615,7 +615,7 @@ static int get_object_attribute(const char* path, struct stat* pstbuf, headers_t // It is better not to set meta header other than mode, // so do not do it. // - (*pheader)["x-amz-meta-mode"] = str(0); + (*pheader)["x-amz-meta-mode"] = "0"; }else if(0 != result){ if(overcheck && !is_bucket_mountpoint){ @@ -679,12 +679,12 @@ static int get_object_attribute(const char* path, struct stat* pstbuf, headers_t // information from the default stat. // (*pheader)["Content-Type"] = S3fsCurl::LookupMimeType(strpath); - (*pheader)["x-amz-meta-uid"] = str(pstat->st_uid); - (*pheader)["x-amz-meta-gid"] = str(pstat->st_gid); - (*pheader)["x-amz-meta-mode"] = str(pstat->st_mode); - (*pheader)["x-amz-meta-atime"] = str(pstat->st_atime); - (*pheader)["x-amz-meta-ctime"] = str(pstat->st_ctime); - (*pheader)["x-amz-meta-mtime"] = str(pstat->st_mtime); + (*pheader)["x-amz-meta-uid"] = std::to_string(pstat->st_uid); + (*pheader)["x-amz-meta-gid"] = std::to_string(pstat->st_gid); + (*pheader)["x-amz-meta-mode"] = std::to_string(pstat->st_mode); + (*pheader)["x-amz-meta-atime"] = std::to_string(pstat->st_atime); + (*pheader)["x-amz-meta-ctime"] = std::to_string(pstat->st_ctime); + (*pheader)["x-amz-meta-mtime"] = std::to_string(pstat->st_mtime); result = 0; }else{ @@ -1121,9 +1121,9 @@ static int create_file_object(const char* path, mode_t mode, uid_t uid, gid_t gi std::string strnow = s3fs_str_realtime(); headers_t meta; meta["Content-Type"] = S3fsCurl::LookupMimeType(std::string(path)); - meta["x-amz-meta-uid"] = str(uid); - meta["x-amz-meta-gid"] = str(gid); - meta["x-amz-meta-mode"] = str(mode); + meta["x-amz-meta-uid"] = std::to_string(uid); + meta["x-amz-meta-gid"] = std::to_string(gid); + meta["x-amz-meta-mode"] = std::to_string(mode); meta["x-amz-meta-atime"] = strnow; meta["x-amz-meta-ctime"] = strnow; meta["x-amz-meta-mtime"] = strnow; @@ -1189,9 +1189,9 @@ static int s3fs_create(const char* _path, mode_t mode, struct fuse_file_info* fi std::string strnow = s3fs_str_realtime(); headers_t meta; meta["Content-Length"] = "0"; - meta["x-amz-meta-uid"] = str(pcxt->uid); - meta["x-amz-meta-gid"] = str(pcxt->gid); - meta["x-amz-meta-mode"] = str(mode); + meta["x-amz-meta-uid"] = std::to_string(pcxt->uid); + meta["x-amz-meta-gid"] = std::to_string(pcxt->gid); + meta["x-amz-meta-mode"] = std::to_string(mode); meta["x-amz-meta-atime"] = strnow; meta["x-amz-meta-mtime"] = strnow; meta["x-amz-meta-ctime"] = strnow; @@ -1243,9 +1243,9 @@ static int create_directory_object(const char* path, mode_t mode, const struct t } headers_t meta; - meta["x-amz-meta-uid"] = str(uid); - meta["x-amz-meta-gid"] = str(gid); - meta["x-amz-meta-mode"] = str(mode); + meta["x-amz-meta-uid"] = std::to_string(uid); + meta["x-amz-meta-gid"] = std::to_string(gid); + meta["x-amz-meta-mode"] = std::to_string(mode); meta["x-amz-meta-atime"] = str(ts_atime); meta["x-amz-meta-mtime"] = str(ts_mtime); meta["x-amz-meta-ctime"] = str(ts_ctime); @@ -1438,12 +1438,12 @@ static int s3fs_symlink(const char* _from, const char* _to) std::string strnow = s3fs_str_realtime(); headers_t headers; headers["Content-Type"] = std::string("application/octet-stream"); // Static - headers["x-amz-meta-mode"] = str(S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO); + headers["x-amz-meta-mode"] = std::to_string(S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO); headers["x-amz-meta-atime"] = strnow; headers["x-amz-meta-ctime"] = strnow; headers["x-amz-meta-mtime"] = strnow; - headers["x-amz-meta-uid"] = str(pcxt->uid); - headers["x-amz-meta-gid"] = str(pcxt->gid); + headers["x-amz-meta-uid"] = std::to_string(pcxt->uid); + headers["x-amz-meta-gid"] = std::to_string(pcxt->gid); // [NOTE] // Symbolic links do not set xattrs. @@ -2000,7 +2000,7 @@ static int s3fs_chmod(const char* _path, mode_t mode) std::string strSourcePath = (mount_prefix.empty() && "/" == strpath) ? "//" : strpath; headers_t updatemeta; updatemeta["x-amz-meta-ctime"] = s3fs_str_realtime(); - updatemeta["x-amz-meta-mode"] = str(mode); + updatemeta["x-amz-meta-mode"] = std::to_string(mode); updatemeta["x-amz-copy-source"] = urlEncodePath(service_path + S3fsCred::GetBucket() + get_realpath(strSourcePath.c_str())); updatemeta["x-amz-metadata-directive"] = "REPLACE"; @@ -2204,8 +2204,8 @@ static int s3fs_chown(const char* _path, uid_t uid, gid_t gid) std::string strSourcePath = (mount_prefix.empty() && "/" == strpath) ? "//" : strpath; headers_t updatemeta; updatemeta["x-amz-meta-ctime"] = s3fs_str_realtime(); - updatemeta["x-amz-meta-uid"] = str(uid); - updatemeta["x-amz-meta-gid"] = str(gid); + updatemeta["x-amz-meta-uid"] = std::to_string(uid); + updatemeta["x-amz-meta-gid"] = std::to_string(gid); updatemeta["x-amz-copy-source"] = urlEncodePath(service_path + S3fsCred::GetBucket() + get_realpath(strSourcePath.c_str())); updatemeta["x-amz-metadata-directive"] = "REPLACE"; @@ -2775,11 +2775,11 @@ static int s3fs_truncate(const char* _path, off_t size) std::string strnow = s3fs_str_realtime(); meta["Content-Type"] = std::string("application/octet-stream"); // Static - meta["x-amz-meta-mode"] = str(S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO); + meta["x-amz-meta-mode"] = std::to_string(S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO); meta["x-amz-meta-ctime"] = strnow; meta["x-amz-meta-mtime"] = strnow; - meta["x-amz-meta-uid"] = str(pcxt->uid); - meta["x-amz-meta-gid"] = str(pcxt->gid); + meta["x-amz-meta-uid"] = std::to_string(pcxt->uid); + meta["x-amz-meta-gid"] = std::to_string(pcxt->gid); if(nullptr == (ent = autoent.Open(path, &meta, size, S3FS_OMIT_TS, O_RDWR, true, true, false, AutoLock::NONE))){ S3FS_PRN_ERR("could not open file(%s): errno=%d", path, errno); @@ -3341,12 +3341,12 @@ static int readdir_multi_head(const char* path, const S3ObjList& head, void* buf headers_t dummy_header; dummy_header["Content-Type"] = std::string("application/x-directory"); // directory - dummy_header["x-amz-meta-uid"] = str(is_s3fs_uid ? s3fs_uid : geteuid()); - dummy_header["x-amz-meta-gid"] = str(is_s3fs_gid ? s3fs_gid : getegid()); - dummy_header["x-amz-meta-mode"] = str(S_IFDIR | (~dirmask & (S_IRWXU | S_IRWXG | S_IRWXO))); - dummy_header["x-amz-meta-atime"] = str(0); - dummy_header["x-amz-meta-ctime"] = str(0); - dummy_header["x-amz-meta-mtime"] = str(0); + dummy_header["x-amz-meta-uid"] = std::to_string(is_s3fs_uid ? s3fs_uid : geteuid()); + dummy_header["x-amz-meta-gid"] = std::to_string(is_s3fs_gid ? s3fs_gid : getegid()); + dummy_header["x-amz-meta-mode"] = std::to_string(S_IFDIR | (~dirmask & (S_IRWXU | S_IRWXG | S_IRWXO))); + dummy_header["x-amz-meta-atime"] = "0"; + dummy_header["x-amz-meta-ctime"] = "0"; + dummy_header["x-amz-meta-mtime"] = "0"; for(s3obj_list_t::iterator reiter = notfound_param.notfound_list.begin(); reiter != notfound_param.notfound_list.end(); ++reiter){ int dir_result; @@ -3453,7 +3453,7 @@ static int list_bucket(const char* path, S3ObjList& head, const char* delimiter, // For dir with children, expect "dir/" and "dir/child" query_maxkey += "max-keys=2"; }else{ - query_maxkey += "max-keys=" + str(max_keys_list_object); + query_maxkey += "max-keys=" + std::to_string(max_keys_list_object); } while(truncated){ diff --git a/src/s3fs_util.cpp b/src/s3fs_util.cpp index 75f1856..96ce5ed 100644 --- a/src/s3fs_util.cpp +++ b/src/s3fs_util.cpp @@ -564,7 +564,7 @@ struct timespec* s3fs_realtime(struct timespec& ts) std::string s3fs_str_realtime() { struct timespec ts; - return str(*(s3fs_realtime(ts))); + return str(*s3fs_realtime(ts)); } /* diff --git a/src/string_util.cpp b/src/string_util.cpp index a0642f7..85c59fa 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -35,25 +35,10 @@ const char SPACES[] = " \t\r\n"; //------------------------------------------------------------------- -// Templates +// Functions //------------------------------------------------------------------- -template std::string str(T value) -{ - std::ostringstream s; - s << value; - return s.str(); -} -template std::string str(short value); -template std::string str(unsigned short value); -template std::string str(int value); -template std::string str(unsigned int value); -template std::string str(long value); -template std::string str(unsigned long value); -template std::string str(long long value); -template std::string str(unsigned long long value); - -template<> std::string str(const struct timespec value) +std::string str(const struct timespec value) { std::ostringstream s; s << value.tv_sec; @@ -63,10 +48,6 @@ template<> std::string str(const struct timespec value) return s.str(); } -//------------------------------------------------------------------- -// Functions -//------------------------------------------------------------------- - #ifdef __MSYS__ /* * Polyfill for strptime function diff --git a/src/string_util.h b/src/string_util.h index 0a34d1f..eceabc6 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -37,11 +37,6 @@ extern const char SPACES[]; static inline int is_prefix(const char *str, const char *prefix) { return strncmp(str, prefix, strlen(prefix)) == 0; } static inline const char* SAFESTRPTR(const char *strptr) { return strptr ? strptr : ""; } -//------------------------------------------------------------------- -// Templates -//------------------------------------------------------------------- -template std::string str(T value); - //------------------------------------------------------------------- // Macros(WTF8) //------------------------------------------------------------------- @@ -56,6 +51,9 @@ template std::string str(T value); //------------------------------------------------------------------- // Utilities //------------------------------------------------------------------- +// TODO: rename to to_string? +std::string str(const struct timespec value); + #ifdef __MSYS__ // // Polyfill for strptime function. diff --git a/src/test_string_util.cpp b/src/test_string_util.cpp index 93617ea..5063dc7 100644 --- a/src/test_string_util.cpp +++ b/src/test_string_util.cpp @@ -49,14 +49,6 @@ void test_trim() ASSERT_EQUALS(std::string("1234"), trim_right("1234 ")); ASSERT_EQUALS(std::string(" 1234"), trim_right(" 1234")); ASSERT_EQUALS(std::string("1234"), trim_right("1234")); - - ASSERT_EQUALS(std::string("0"), str(0)); - ASSERT_EQUALS(std::string("1"), str(1)); - ASSERT_EQUALS(std::string("-1"), str(-1)); - ASSERT_EQUALS(std::string("9223372036854775807"), str(std::numeric_limits::max())); - ASSERT_EQUALS(std::string("-9223372036854775808"), str(std::numeric_limits::min())); - ASSERT_EQUALS(std::string("0"), str(std::numeric_limits::min())); - ASSERT_EQUALS(std::string("18446744073709551615"), str(std::numeric_limits::max())); } void test_base64()