Convert most str callers to C++11 std::to_string

Remaining ones handle timespec.
This commit is contained in:
Andrew Gaul 2023-07-25 11:14:00 +09:00 committed by Andrew Gaul
parent 36db898d01
commit 05d40af87e
7 changed files with 47 additions and 76 deletions

View File

@ -3007,7 +3007,7 @@ int S3fsCurl::GetIAMv2ApiToken(const char* token_url, int token_ttl, const char*
responseHeaders.clear(); responseHeaders.clear();
bodydata.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()); requestHeaders = curl_slist_sort_insert(requestHeaders, token_ttl_hdr, ttlstr.c_str());
// Curl appends an "Expect: 100-continue" header to the token request, // 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){ if(0 < size){
std::string range = "bytes="; std::string range = "bytes=";
range += str(start); range += std::to_string(start);
range += "-"; range += "-";
range += str(start + size - 1); range += std::to_string(start + size - 1);
requestHeaders = curl_slist_sort_insert(requestHeaders, "Range", range.c_str()); requestHeaders = curl_slist_sort_insert(requestHeaders, "Range", range.c_str());
} }
// SSE-C // SSE-C
@ -3914,7 +3914,7 @@ int S3fsCurl::CompleteMultipartPostRequest(const char* tpath, const std::string&
return -EIO; return -EIO;
} }
postContent += "<Part>\n"; postContent += "<Part>\n";
postContent += " <PartNumber>" + str(it->part_num) + "</PartNumber>\n"; postContent += " <PartNumber>" + std::to_string(it->part_num) + "</PartNumber>\n";
postContent += " <ETag>" + it->etag + "</ETag>\n"; postContent += " <ETag>" + it->etag + "</ETag>\n";
postContent += "</Part>\n"; postContent += "</Part>\n";
} }
@ -4135,7 +4135,7 @@ int S3fsCurl::UploadMultipartPostSetup(const char* tpath, int part_num, const st
// Encode the upload_id here. // Encode the upload_id here.
// In compatible S3 servers(Cloudflare, etc), there are cases where characters that require URL encoding are included. // 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 urlargs = "?" + query_string;
std::string resource; std::string resource;
std::string turl; std::string turl;
@ -4207,7 +4207,7 @@ int S3fsCurl::CopyMultipartPostSetup(const char* from, const char* to, int part_
// Encode the upload_id here. // Encode the upload_id here.
// In compatible S3 servers(Cloudflare, etc), there are cases where characters that require URL encoding are included. // 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 urlargs = "?" + query_string;
std::string resource; std::string resource;
std::string turl; std::string turl;

View File

@ -1010,21 +1010,21 @@ bool FdEntity::SetXattr(const std::string& xattr)
bool FdEntity::SetMode(mode_t mode) bool FdEntity::SetMode(mode_t mode)
{ {
AutoLock auto_lock(&fdent_lock); AutoLock auto_lock(&fdent_lock);
orgmeta["x-amz-meta-mode"] = str(mode); orgmeta["x-amz-meta-mode"] = std::to_string(mode);
return true; return true;
} }
bool FdEntity::SetUId(uid_t uid) bool FdEntity::SetUId(uid_t uid)
{ {
AutoLock auto_lock(&fdent_lock); AutoLock auto_lock(&fdent_lock);
orgmeta["x-amz-meta-uid"] = str(uid); orgmeta["x-amz-meta-uid"] = std::to_string(uid);
return true; return true;
} }
bool FdEntity::SetGId(gid_t gid) bool FdEntity::SetGId(gid_t gid)
{ {
AutoLock auto_lock(&fdent_lock); AutoLock auto_lock(&fdent_lock);
orgmeta["x-amz-meta-gid"] = str(gid); orgmeta["x-amz-meta-gid"] = std::to_string(gid);
return true; return true;
} }

View File

@ -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, // It is better not to set meta header other than mode,
// so do not do it. // so do not do it.
// //
(*pheader)["x-amz-meta-mode"] = str(0); (*pheader)["x-amz-meta-mode"] = "0";
}else if(0 != result){ }else if(0 != result){
if(overcheck && !is_bucket_mountpoint){ 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. // information from the default stat.
// //
(*pheader)["Content-Type"] = S3fsCurl::LookupMimeType(strpath); (*pheader)["Content-Type"] = S3fsCurl::LookupMimeType(strpath);
(*pheader)["x-amz-meta-uid"] = str(pstat->st_uid); (*pheader)["x-amz-meta-uid"] = std::to_string(pstat->st_uid);
(*pheader)["x-amz-meta-gid"] = str(pstat->st_gid); (*pheader)["x-amz-meta-gid"] = std::to_string(pstat->st_gid);
(*pheader)["x-amz-meta-mode"] = str(pstat->st_mode); (*pheader)["x-amz-meta-mode"] = std::to_string(pstat->st_mode);
(*pheader)["x-amz-meta-atime"] = str(pstat->st_atime); (*pheader)["x-amz-meta-atime"] = std::to_string(pstat->st_atime);
(*pheader)["x-amz-meta-ctime"] = str(pstat->st_ctime); (*pheader)["x-amz-meta-ctime"] = std::to_string(pstat->st_ctime);
(*pheader)["x-amz-meta-mtime"] = str(pstat->st_mtime); (*pheader)["x-amz-meta-mtime"] = std::to_string(pstat->st_mtime);
result = 0; result = 0;
}else{ }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(); std::string strnow = s3fs_str_realtime();
headers_t meta; headers_t meta;
meta["Content-Type"] = S3fsCurl::LookupMimeType(std::string(path)); meta["Content-Type"] = S3fsCurl::LookupMimeType(std::string(path));
meta["x-amz-meta-uid"] = str(uid); meta["x-amz-meta-uid"] = std::to_string(uid);
meta["x-amz-meta-gid"] = str(gid); meta["x-amz-meta-gid"] = std::to_string(gid);
meta["x-amz-meta-mode"] = str(mode); meta["x-amz-meta-mode"] = std::to_string(mode);
meta["x-amz-meta-atime"] = strnow; meta["x-amz-meta-atime"] = strnow;
meta["x-amz-meta-ctime"] = strnow; meta["x-amz-meta-ctime"] = strnow;
meta["x-amz-meta-mtime"] = 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(); std::string strnow = s3fs_str_realtime();
headers_t meta; headers_t meta;
meta["Content-Length"] = "0"; meta["Content-Length"] = "0";
meta["x-amz-meta-uid"] = str(pcxt->uid); meta["x-amz-meta-uid"] = std::to_string(pcxt->uid);
meta["x-amz-meta-gid"] = str(pcxt->gid); meta["x-amz-meta-gid"] = std::to_string(pcxt->gid);
meta["x-amz-meta-mode"] = str(mode); meta["x-amz-meta-mode"] = std::to_string(mode);
meta["x-amz-meta-atime"] = strnow; meta["x-amz-meta-atime"] = strnow;
meta["x-amz-meta-mtime"] = strnow; meta["x-amz-meta-mtime"] = strnow;
meta["x-amz-meta-ctime"] = 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; headers_t meta;
meta["x-amz-meta-uid"] = str(uid); meta["x-amz-meta-uid"] = std::to_string(uid);
meta["x-amz-meta-gid"] = str(gid); meta["x-amz-meta-gid"] = std::to_string(gid);
meta["x-amz-meta-mode"] = str(mode); meta["x-amz-meta-mode"] = std::to_string(mode);
meta["x-amz-meta-atime"] = str(ts_atime); meta["x-amz-meta-atime"] = str(ts_atime);
meta["x-amz-meta-mtime"] = str(ts_mtime); meta["x-amz-meta-mtime"] = str(ts_mtime);
meta["x-amz-meta-ctime"] = str(ts_ctime); 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(); std::string strnow = s3fs_str_realtime();
headers_t headers; headers_t headers;
headers["Content-Type"] = std::string("application/octet-stream"); // Static 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-atime"] = strnow;
headers["x-amz-meta-ctime"] = strnow; headers["x-amz-meta-ctime"] = strnow;
headers["x-amz-meta-mtime"] = strnow; headers["x-amz-meta-mtime"] = strnow;
headers["x-amz-meta-uid"] = str(pcxt->uid); headers["x-amz-meta-uid"] = std::to_string(pcxt->uid);
headers["x-amz-meta-gid"] = str(pcxt->gid); headers["x-amz-meta-gid"] = std::to_string(pcxt->gid);
// [NOTE] // [NOTE]
// Symbolic links do not set xattrs. // 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; std::string strSourcePath = (mount_prefix.empty() && "/" == strpath) ? "//" : strpath;
headers_t updatemeta; headers_t updatemeta;
updatemeta["x-amz-meta-ctime"] = s3fs_str_realtime(); 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-copy-source"] = urlEncodePath(service_path + S3fsCred::GetBucket() + get_realpath(strSourcePath.c_str()));
updatemeta["x-amz-metadata-directive"] = "REPLACE"; 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; std::string strSourcePath = (mount_prefix.empty() && "/" == strpath) ? "//" : strpath;
headers_t updatemeta; headers_t updatemeta;
updatemeta["x-amz-meta-ctime"] = s3fs_str_realtime(); updatemeta["x-amz-meta-ctime"] = s3fs_str_realtime();
updatemeta["x-amz-meta-uid"] = str(uid); updatemeta["x-amz-meta-uid"] = std::to_string(uid);
updatemeta["x-amz-meta-gid"] = str(gid); 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-copy-source"] = urlEncodePath(service_path + S3fsCred::GetBucket() + get_realpath(strSourcePath.c_str()));
updatemeta["x-amz-metadata-directive"] = "REPLACE"; 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(); std::string strnow = s3fs_str_realtime();
meta["Content-Type"] = std::string("application/octet-stream"); // Static 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-ctime"] = strnow;
meta["x-amz-meta-mtime"] = strnow; meta["x-amz-meta-mtime"] = strnow;
meta["x-amz-meta-uid"] = str(pcxt->uid); meta["x-amz-meta-uid"] = std::to_string(pcxt->uid);
meta["x-amz-meta-gid"] = str(pcxt->gid); 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))){ 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); 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; headers_t dummy_header;
dummy_header["Content-Type"] = std::string("application/x-directory"); // directory 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-uid"] = std::to_string(is_s3fs_uid ? s3fs_uid : geteuid());
dummy_header["x-amz-meta-gid"] = str(is_s3fs_gid ? s3fs_gid : getegid()); dummy_header["x-amz-meta-gid"] = std::to_string(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-mode"] = std::to_string(S_IFDIR | (~dirmask & (S_IRWXU | S_IRWXG | S_IRWXO)));
dummy_header["x-amz-meta-atime"] = str(0); dummy_header["x-amz-meta-atime"] = "0";
dummy_header["x-amz-meta-ctime"] = str(0); dummy_header["x-amz-meta-ctime"] = "0";
dummy_header["x-amz-meta-mtime"] = str(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){ for(s3obj_list_t::iterator reiter = notfound_param.notfound_list.begin(); reiter != notfound_param.notfound_list.end(); ++reiter){
int dir_result; 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" // For dir with children, expect "dir/" and "dir/child"
query_maxkey += "max-keys=2"; query_maxkey += "max-keys=2";
}else{ }else{
query_maxkey += "max-keys=" + str(max_keys_list_object); query_maxkey += "max-keys=" + std::to_string(max_keys_list_object);
} }
while(truncated){ while(truncated){

View File

@ -564,7 +564,7 @@ struct timespec* s3fs_realtime(struct timespec& ts)
std::string s3fs_str_realtime() std::string s3fs_str_realtime()
{ {
struct timespec ts; struct timespec ts;
return str(*(s3fs_realtime(ts))); return str(*s3fs_realtime(ts));
} }
/* /*

View File

@ -35,25 +35,10 @@
const char SPACES[] = " \t\r\n"; const char SPACES[] = " \t\r\n";
//------------------------------------------------------------------- //-------------------------------------------------------------------
// Templates // Functions
//------------------------------------------------------------------- //-------------------------------------------------------------------
template <class T> std::string str(T value)
{
std::ostringstream s;
s << value;
return s.str();
}
template std::string str(short value); std::string str(const struct timespec 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::ostringstream s; std::ostringstream s;
s << value.tv_sec; s << value.tv_sec;
@ -63,10 +48,6 @@ template<> std::string str(const struct timespec value)
return s.str(); return s.str();
} }
//-------------------------------------------------------------------
// Functions
//-------------------------------------------------------------------
#ifdef __MSYS__ #ifdef __MSYS__
/* /*
* Polyfill for strptime function * Polyfill for strptime function

View File

@ -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 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 : ""; } static inline const char* SAFESTRPTR(const char *strptr) { return strptr ? strptr : ""; }
//-------------------------------------------------------------------
// Templates
//-------------------------------------------------------------------
template <class T> std::string str(T value);
//------------------------------------------------------------------- //-------------------------------------------------------------------
// Macros(WTF8) // Macros(WTF8)
//------------------------------------------------------------------- //-------------------------------------------------------------------
@ -56,6 +51,9 @@ template <class T> std::string str(T value);
//------------------------------------------------------------------- //-------------------------------------------------------------------
// Utilities // Utilities
//------------------------------------------------------------------- //-------------------------------------------------------------------
// TODO: rename to to_string?
std::string str(const struct timespec value);
#ifdef __MSYS__ #ifdef __MSYS__
// //
// Polyfill for strptime function. // Polyfill for strptime function.

View File

@ -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(" 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<int64_t>::max()));
ASSERT_EQUALS(std::string("-9223372036854775808"), str(std::numeric_limits<int64_t>::min()));
ASSERT_EQUALS(std::string("0"), str(std::numeric_limits<uint64_t>::min()));
ASSERT_EQUALS(std::string("18446744073709551615"), str(std::numeric_limits<uint64_t>::max()));
} }
void test_base64() void test_base64()