From e157d811cb8a7de854a182d0a33de5638640fc3b Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Tue, 15 Aug 2023 21:22:36 +0900 Subject: [PATCH] Use std::string::compare and operator== where possible (#2256) --- src/addhead.cpp | 2 +- src/cache.cpp | 2 +- src/fdcache.cpp | 14 +++++++------- src/fdcache_entity.h | 2 +- src/s3objlist.cpp | 4 ++-- test/write_multiblock.cc | 8 ++++---- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/addhead.cpp b/src/addhead.cpp index acb2a34..d2c1461 100644 --- a/src/addhead.cpp +++ b/src/addhead.cpp @@ -192,7 +192,7 @@ bool AdditionalHeader::AddHeader(headers_t& meta, const char* path) const }else{ // directly comparing if(paddhead->basestring.length() < pathlength){ - if(paddhead->basestring.empty() || 0 == strcmp(&path[pathlength - paddhead->basestring.length()], paddhead->basestring.c_str())){ + if(paddhead->basestring.empty() || paddhead->basestring == &path[pathlength - paddhead->basestring.length()]){ // match -> adding header meta[paddhead->headkey] = paddhead->headvalue; } diff --git a/src/cache.cpp b/src/cache.cpp index f8a57b4..1f54118 100644 --- a/src/cache.cpp +++ b/src/cache.cpp @@ -245,7 +245,7 @@ bool StatCache::GetStat(const std::string& key, struct stat* pst, headers_t* met std::string tag = lower(hiter->first); if(tag == "etag"){ stretag = hiter->second; - if('\0' != petag[0] && 0 != strcmp(petag, stretag.c_str())){ + if('\0' != petag[0] && petag != stretag){ is_delete_cache = true; } break; diff --git a/src/fdcache.cpp b/src/fdcache.cpp index b49f999..7b56e2c 100644 --- a/src/fdcache.cpp +++ b/src/fdcache.cpp @@ -454,7 +454,7 @@ FdManager::~FdManager() if(this == FdManager::get()){ for(fdent_map_t::iterator iter = fent.begin(); fent.end() != iter; ++iter){ FdEntity* ent = (*iter).second; - S3FS_PRN_WARN("To exit with the cache file opened: path=%s, refcnt=%d", ent->GetPath(), ent->GetOpenCount()); + S3FS_PRN_WARN("To exit with the cache file opened: path=%s, refcnt=%d", ent->GetPath().c_str(), ent->GetOpenCount()); delete ent; } fent.clear(); @@ -508,7 +508,7 @@ FdEntity* FdManager::GetFdEntity(const char* path, int& existfd, bool newfd, Aut for(iter = fent.begin(); iter != fent.end(); ++iter){ if(iter->second && iter->second->FindPseudoFd(existfd)){ // found opened fd in map - if(0 == strcmp(iter->second->GetPath(), path)){ + if(iter->second->GetPath() == path){ if(newfd){ existfd = iter->second->Dup(existfd); } @@ -525,7 +525,7 @@ FdEntity* FdManager::GetFdEntity(const char* path, int& existfd, bool newfd, Aut // when the file is opened. if(!FdManager::IsCacheDir()){ for(iter = fent.begin(); iter != fent.end(); ++iter){ - if(iter->second && iter->second->IsOpen() && 0 == strcmp(iter->second->GetPath(), path)){ + if(iter->second && iter->second->IsOpen() && iter->second->GetPath() == path){ return iter->second; } } @@ -552,7 +552,7 @@ FdEntity* FdManager::Open(int& fd, const char* path, const headers_t* pmeta, off // search a entity in all which opened the temporary file. // for(iter = fent.begin(); iter != fent.end(); ++iter){ - if(iter->second && iter->second->IsOpen() && 0 == strcmp(iter->second->GetPath(), path)){ + if(iter->second && iter->second->IsOpen() && iter->second->GetPath() == path){ break; // found opened fd in mapping } } @@ -669,7 +669,7 @@ int FdManager::GetPseudoFdCount(const char* path) // search from all entity. for(fdent_map_t::iterator iter = fent.begin(); iter != fent.end(); ++iter){ - if(iter->second && 0 == strcmp(iter->second->GetPath(), path)){ + if(iter->second && iter->second->GetPath() == path){ // found the entity for the path return iter->second->GetOpenCount(); } @@ -690,7 +690,7 @@ void FdManager::Rename(const std::string &from, const std::string &to) // search a entity in all which opened the temporary file. // for(iter = fent.begin(); iter != fent.end(); ++iter){ - if(iter->second && iter->second->IsOpen() && 0 == strcmp(iter->second->GetPath(), from.c_str())){ + if(iter->second && iter->second->IsOpen() && iter->second->GetPath() == from){ break; // found opened fd in mapping } } @@ -719,7 +719,7 @@ void FdManager::Rename(const std::string &from, const std::string &to) bool FdManager::Close(FdEntity* ent, int fd) { - S3FS_PRN_DBG("[ent->file=%s][pseudo_fd=%d]", ent ? ent->GetPath() : "", fd); + S3FS_PRN_DBG("[ent->file=%s][pseudo_fd=%d]", ent ? ent->GetPath().c_str() : "", fd); if(!ent || -1 == fd){ return true; // returns success diff --git a/src/fdcache_entity.h b/src/fdcache_entity.h index 5e741f5..f70ca8e 100644 --- a/src/fdcache_entity.h +++ b/src/fdcache_entity.h @@ -111,7 +111,7 @@ class FdEntity int Dup(int fd, AutoLock::Type locktype = AutoLock::NONE); int OpenPseudoFd(int flags = O_RDONLY, AutoLock::Type locktype = AutoLock::NONE); int GetOpenCount(AutoLock::Type locktype = AutoLock::NONE) const; - const char* GetPath() const { return path.c_str(); } + const std::string& GetPath() const { return path; } bool RenamePath(const std::string& newpath, std::string& fentmapkey); int GetPhysicalFd() const { return physical_fd; } bool IsModified() const; diff --git a/src/s3objlist.cpp b/src/s3objlist.cpp index 8861c30..145171a 100644 --- a/src/s3objlist.cpp +++ b/src/s3objlist.cpp @@ -198,12 +198,12 @@ bool S3ObjList::GetLastName(std::string& lastname) const lastname = ""; for(s3obj_t::const_iterator iter = objects.begin(); iter != objects.end(); ++iter){ if((*iter).second.orgname.length()){ - if(0 > strcmp(lastname.c_str(), (*iter).second.orgname.c_str())){ + if(lastname.compare(iter->second.orgname) < 0){ lastname = (*iter).second.orgname; result = true; } }else{ - if(0 > strcmp(lastname.c_str(), (*iter).second.normalname.c_str())){ + if(lastname.compare(iter->second.normalname) < 0){ lastname = (*iter).second.normalname; result = true; } diff --git a/test/write_multiblock.cc b/test/write_multiblock.cc index 66d00ce..0f2abdf 100644 --- a/test/write_multiblock.cc +++ b/test/write_multiblock.cc @@ -217,18 +217,18 @@ int main(int argc, char** argv) struct stat st; if(0 == stat(fiter->c_str(), &st)){ if(!S_ISREG(st.st_mode)){ - std::cerr << "[ERROR] File " << fiter->c_str() << " is existed, but it is not regular file." << std::endl; + std::cerr << "[ERROR] File " << *fiter << " is existed, but it is not regular file." << std::endl; free(pData); exit(EXIT_FAILURE); } if(-1 == (fd = open(fiter->c_str(), O_WRONLY))){ - std::cerr << "[ERROR] Could not open " << fiter->c_str() << std::endl; + std::cerr << "[ERROR] Could not open " << *fiter << std::endl; free(pData); exit(EXIT_FAILURE); } }else{ if(-1 == (fd = open(fiter->c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644))){ - std::cerr << "[ERROR] Could not create " << fiter->c_str() << std::endl; + std::cerr << "[ERROR] Could not create " << *fiter << std::endl; free(pData); exit(EXIT_FAILURE); } @@ -240,7 +240,7 @@ int main(int argc, char** argv) for(ssize_t writepos = 0, writecnt = 0; writepos < piter->size; writepos += writecnt){ if(-1 == (writecnt = pwrite(fd, &(pData[writepos]), static_cast(piter->size - writepos), (piter->start + writepos)))){ if(EAGAIN != errno && EWOULDBLOCK != errno && EINTR != errno){ - std::cerr << "[ERROR] Failed writing to " << fiter->c_str() << " by errno : " << errno << std::endl; + std::cerr << "[ERROR] Failed writing to " << *fiter << " by errno : " << errno << std::endl; close(fd); free(pData); exit(EXIT_FAILURE);