From 503c86bb8aee54978d18279ccf0e8b5b217c7b5d Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sat, 26 Sep 2020 14:09:20 +0900 Subject: [PATCH] Call is_prefix instead of compare and substr --- src/cache.cpp | 2 +- src/curl.cpp | 16 ++++++++-------- src/curl_util.cpp | 4 ++-- src/s3fs.cpp | 8 ++++---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cache.cpp b/src/cache.cpp index 43f18e1..3e5ee8c 100644 --- a/src/cache.cpp +++ b/src/cache.cpp @@ -410,7 +410,7 @@ bool StatCache::AddStat(const std::string& key, headers_t& meta, bool forcedir, ent->meta[iter->first] = value; }else if(tag == "last-modified"){ ent->meta[iter->first] = value; - }else if(tag.substr(0, 5) == "x-amz"){ + }else if(is_prefix(tag.c_str(), "x-amz")){ ent->meta[tag] = value; // key is lower case for "x-amz" } } diff --git a/src/curl.cpp b/src/curl.cpp index f1bd996..8a378e4 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -617,7 +617,7 @@ size_t S3fsCurl::HeaderCallback(void* data, size_t blockSize, size_t numBlocks, // Force to lower, only "x-amz" std::string lkey = key; transform(lkey.begin(), lkey.end(), lkey.begin(), static_cast(std::tolower)); - if(lkey.compare(0, 5, "x-amz") == 0){ + if(is_prefix(lkey.c_str(), "x-amz")){ key = lkey; } std::string value; @@ -2904,7 +2904,7 @@ int S3fsCurl::HeadRequest(const char* tpath, headers_t& meta) meta[iter->first] = value; }else if(key == "last-modified"){ meta[iter->first] = value; - }else if(key.substr(0, 5) == "x-amz"){ + }else if(is_prefix(key.c_str(), "x-amz")){ meta[key] = value; // key is lower case for "x-amz" } } @@ -2938,9 +2938,9 @@ int S3fsCurl::PutHeadRequest(const char* tpath, headers_t& meta, bool is_copy) for(headers_t::iterator iter = meta.begin(); iter != meta.end(); ++iter){ std::string key = lower(iter->first); std::string value = iter->second; - if(key.substr(0, 9) == "x-amz-acl"){ + if(is_prefix(key.c_str(), "x-amz-acl")){ // not set value, but after set it. - }else if(key.substr(0, 10) == "x-amz-meta"){ + }else if(is_prefix(key.c_str(), "x-amz-meta")){ requestHeaders = curl_slist_sort_insert(requestHeaders, iter->first.c_str(), value.c_str()); }else if(key == "x-amz-copy-source"){ requestHeaders = curl_slist_sort_insert(requestHeaders, iter->first.c_str(), value.c_str()); @@ -3077,9 +3077,9 @@ int S3fsCurl::PutRequest(const char* tpath, headers_t& meta, int fd) for(headers_t::iterator iter = meta.begin(); iter != meta.end(); ++iter){ std::string key = lower(iter->first); std::string value = iter->second; - if(key.substr(0, 9) == "x-amz-acl"){ + if(is_prefix(key.c_str(), "x-amz-acl")){ // not set value, but after set it. - }else if(key.substr(0, 10) == "x-amz-meta"){ + }else if(is_prefix(key.c_str(), "x-amz-meta")){ requestHeaders = curl_slist_sort_insert(requestHeaders, iter->first.c_str(), value.c_str()); }else if(key == "x-amz-server-side-encryption" && value != "aws:kms"){ // skip this header, because this header is specified after logic. @@ -3325,9 +3325,9 @@ int S3fsCurl::PreMultipartPostRequest(const char* tpath, headers_t& meta, std::s for(headers_t::iterator iter = meta.begin(); iter != meta.end(); ++iter){ std::string key = lower(iter->first); std::string value = iter->second; - if(key.substr(0, 9) == "x-amz-acl"){ + if(is_prefix(key.c_str(), "x-amz-acl")){ // not set value, but after set it. - }else if(key.substr(0, 10) == "x-amz-meta"){ + }else if(is_prefix(key.c_str(), "x-amz-meta")){ requestHeaders = curl_slist_sort_insert(requestHeaders, iter->first.c_str(), value.c_str()); }else if(key == "x-amz-server-side-encryption" && value != "aws:kms"){ // Only copy mode. diff --git a/src/curl_util.cpp b/src/curl_util.cpp index edf8998..989f4fb 100644 --- a/src/curl_util.cpp +++ b/src/curl_util.cpp @@ -323,9 +323,9 @@ std::string url_to_host(const std::string &url) static const std::string https = "https://"; std::string hostname; - if (url.compare(0, http.size(), http) == 0) { + if (is_prefix(url.c_str(), http.c_str())) { hostname = url.substr(http.size()); - } else if (url.compare(0, https.size(), https) == 0) { + } else if (is_prefix(url.c_str(), https.c_str())) { hostname = url.substr(https.size()); } else { S3FS_PRN_EXIT("url does not begin with http:// or https://"); diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 9002abb..5022b74 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -4284,13 +4284,13 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar } if(is_prefix(arg, "ibm_iam_endpoint=")){ std::string endpoint_url; - std::string iam_endpoint = strchr(arg, '=') + sizeof(char); + const char *iam_endpoint = strchr(arg, '=') + sizeof(char); // Check url for http / https protocol std::string - if((iam_endpoint.compare(0, 8, "https://") != 0) && (iam_endpoint.compare(0, 7, "http://") != 0)) { + if(!is_prefix(iam_endpoint, "https://") && !is_prefix(iam_endpoint, "http://")) { S3FS_PRN_EXIT("option ibm_iam_endpoint has invalid format, missing http / https protocol"); return -1; } - endpoint_url = iam_endpoint + "/oidc/token"; + endpoint_url = std::string(iam_endpoint) + "/oidc/token"; S3fsCurl::SetIAMCredentialsURL(endpoint_url.c_str()); return 0; } @@ -4495,7 +4495,7 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar length = s3host.length(); } // Check url for http / https protocol std::string - if((s3host.compare(0, 8, "https://") != 0) && (s3host.compare(0, 7, "http://") != 0)) { + if(!is_prefix(s3host.c_str(), "https://") && !is_prefix(s3host.c_str(), "http://")){ S3FS_PRN_EXIT("option url has invalid format, missing http / https protocol"); return -1; }