Convert FdEntity to std::unique_ptr (#2383)

This commit is contained in:
Andrew Gaul 2023-12-07 23:56:35 +09:00 committed by GitHub
parent b139507ae6
commit e3b50ad3e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 24 deletions

View File

@ -483,9 +483,8 @@ FdManager::~FdManager()
{ {
if(this == FdManager::get()){ if(this == FdManager::get()){
for(fdent_map_t::iterator iter = fent.begin(); fent.end() != iter; ++iter){ for(fdent_map_t::iterator iter = fent.begin(); fent.end() != iter; ++iter){
FdEntity* ent = (*iter).second; FdEntity* ent = (*iter).second.get();
S3FS_PRN_WARN("To exit with the cache file opened: path=%s, refcnt=%d", ent->GetPath().c_str(), 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(); fent.clear();
@ -525,12 +524,12 @@ FdEntity* FdManager::GetFdEntity(const char* path, int& existfd, bool newfd, Aut
if(newfd){ if(newfd){
existfd = iter->second->OpenPseudoFd(O_RDWR); // [NOTE] O_RDWR flags existfd = iter->second->OpenPseudoFd(O_RDWR); // [NOTE] O_RDWR flags
} }
return iter->second; return iter->second.get();
}else if(iter->second->FindPseudoFd(existfd)){ }else if(iter->second->FindPseudoFd(existfd)){
if(newfd){ if(newfd){
existfd = iter->second->Dup(existfd); existfd = iter->second->Dup(existfd);
} }
return iter->second; return iter->second.get();
} }
} }
@ -542,7 +541,7 @@ FdEntity* FdManager::GetFdEntity(const char* path, int& existfd, bool newfd, Aut
if(newfd){ if(newfd){
existfd = iter->second->Dup(existfd); existfd = iter->second->Dup(existfd);
} }
return iter->second; return iter->second.get();
} }
// found fd, but it is used another file(file descriptor is recycled) // found fd, but it is used another file(file descriptor is recycled)
// so returns nullptr. // so returns nullptr.
@ -556,7 +555,7 @@ FdEntity* FdManager::GetFdEntity(const char* path, int& existfd, bool newfd, Aut
if(!FdManager::IsCacheDir()){ if(!FdManager::IsCacheDir()){
for(iter = fent.begin(); iter != fent.end(); ++iter){ for(iter = fent.begin(); iter != fent.end(); ++iter){
if(iter->second && iter->second->IsOpen() && iter->second->GetPath() == path){ if(iter->second && iter->second->IsOpen() && iter->second->GetPath() == path){
return iter->second; return iter->second.get();
} }
} }
} }
@ -588,10 +587,9 @@ FdEntity* FdManager::Open(int& fd, const char* path, const headers_t* pmeta, off
} }
} }
FdEntity* ent;
if(fent.end() != iter){ if(fent.end() != iter){
// found // found
ent = iter->second; FdEntity* ent = iter->second.get();
// [NOTE] // [NOTE]
// If the file is being modified and ignore_modify flag is false, // If the file is being modified and ignore_modify flag is false,
@ -613,6 +611,7 @@ FdEntity* FdManager::Open(int& fd, const char* path, const headers_t* pmeta, off
return nullptr; return nullptr;
} }
return ent;
}else if(is_create){ }else if(is_create){
// not found // not found
std::string cache_path; std::string cache_path;
@ -621,18 +620,17 @@ FdEntity* FdManager::Open(int& fd, const char* path, const headers_t* pmeta, off
return nullptr; return nullptr;
} }
// make new obj // make new obj
ent = new FdEntity(path, cache_path.c_str()); std::unique_ptr<FdEntity> ent(new FdEntity(path, cache_path.c_str()));
// open // open
if(0 > (fd = ent->Open(pmeta, size, ts_mctime, flags, type))){ if(0 > (fd = ent->Open(pmeta, size, ts_mctime, flags, type))){
S3FS_PRN_ERR("failed to open and create new pseudo fd for path(%s) errno:%d.", path, fd); S3FS_PRN_ERR("failed to open and create new pseudo fd for path(%s) errno:%d.", path, fd);
delete ent;
return nullptr; return nullptr;
} }
if(!cache_path.empty()){ if(!cache_path.empty()){
// using cache // using cache
fent[path] = ent; return (fent[path] = std::move(ent)).get();
}else{ }else{
// not using cache, so the key of fdentity is set not really existing path. // not using cache, so the key of fdentity is set not really existing path.
// (but not strictly unexisting path.) // (but not strictly unexisting path.)
@ -643,12 +641,11 @@ FdEntity* FdManager::Open(int& fd, const char* path, const headers_t* pmeta, off
// //
std::string tmppath; std::string tmppath;
FdManager::MakeRandomTempPath(path, tmppath); FdManager::MakeRandomTempPath(path, tmppath);
fent[tmppath] = ent; return (fent[tmppath] = std::move(ent)).get();
} }
}else{ }else{
return nullptr; return nullptr;
} }
return ent;
} }
// [NOTE] // [NOTE]
@ -665,7 +662,7 @@ FdEntity* FdManager::GetExistFdEntity(const char* path, int existfd)
for(fdent_map_t::iterator iter = fent.begin(); iter != fent.end(); ++iter){ for(fdent_map_t::iterator iter = fent.begin(); iter != fent.end(); ++iter){
if(iter->second && iter->second->FindPseudoFd(existfd)){ if(iter->second && iter->second->FindPseudoFd(existfd)){
// found existfd in entity // found existfd in entity
return iter->second; return iter->second.get();
} }
} }
// not found entity // not found entity
@ -730,7 +727,7 @@ void FdManager::Rename(const std::string &from, const std::string &to)
// found // found
S3FS_PRN_DBG("[from=%s][to=%s]", from.c_str(), to.c_str()); S3FS_PRN_DBG("[from=%s][to=%s]", from.c_str(), to.c_str());
FdEntity* ent = iter->second; std::unique_ptr<FdEntity> ent(std::move(iter->second));
// retrieve old fd entity from map // retrieve old fd entity from map
fent.erase(iter); fent.erase(iter);
@ -743,7 +740,7 @@ void FdManager::Rename(const std::string &from, const std::string &to)
} }
// set new fd entity to map // set new fd entity to map
fent[fentmapkey] = ent; fent[fentmapkey] = std::move(ent);
} }
} }
@ -757,7 +754,7 @@ bool FdManager::Close(FdEntity* ent, int fd)
AutoLock auto_lock(&FdManager::fd_manager_lock); AutoLock auto_lock(&FdManager::fd_manager_lock);
for(fdent_map_t::iterator iter = fent.begin(); iter != fent.end(); ++iter){ for(fdent_map_t::iterator iter = fent.begin(); iter != fent.end(); ++iter){
if(iter->second == ent){ if(iter->second.get() == ent){
ent->Close(fd); ent->Close(fd);
if(!ent->IsOpen()){ if(!ent->IsOpen()){
// remove found entity from map. // remove found entity from map.
@ -765,13 +762,12 @@ bool FdManager::Close(FdEntity* ent, int fd)
// check another key name for entity value to be on the safe side // check another key name for entity value to be on the safe side
for(; iter != fent.end(); ){ for(; iter != fent.end(); ){
if(iter->second == ent){ if(iter->second.get() == ent){
iter = fent.erase(iter); iter = fent.erase(iter);
}else{ }else{
++iter; ++iter;
} }
} }
delete ent;
} }
return true; return true;
} }
@ -784,12 +780,11 @@ bool FdManager::ChangeEntityToTempPath(FdEntity* ent, const char* path)
AutoLock auto_lock(&FdManager::fd_manager_lock); AutoLock auto_lock(&FdManager::fd_manager_lock);
for(fdent_map_t::iterator iter = fent.begin(); iter != fent.end(); ){ for(fdent_map_t::iterator iter = fent.begin(); iter != fent.end(); ){
if(iter->second == ent){ if(iter->second.get() == ent){
iter = fent.erase(iter);
std::string tmppath; std::string tmppath;
FdManager::MakeRandomTempPath(path, tmppath); FdManager::MakeRandomTempPath(path, tmppath);
fent[tmppath] = ent; iter->second.reset(ent);
break;
}else{ }else{
++iter; ++iter;
} }

View File

@ -22,6 +22,7 @@
#define S3FS_FDCACHE_ENTITY_H_ #define S3FS_FDCACHE_ENTITY_H_
#include <fcntl.h> #include <fcntl.h>
#include <memory>
#include "autolock.h" #include "autolock.h"
#include "fdcache_page.h" #include "fdcache_page.h"
@ -160,7 +161,7 @@ class FdEntity
bool ReplaceLastUpdateUntreatedPart(off_t front_start, off_t front_size, off_t behind_start, off_t behind_size); bool ReplaceLastUpdateUntreatedPart(off_t front_start, off_t front_size, off_t behind_start, off_t behind_size);
}; };
typedef std::map<std::string, class FdEntity*> fdent_map_t; // key=path, value=FdEntity* typedef std::map<std::string, std::unique_ptr<FdEntity>> fdent_map_t; // key=path, value=FdEntity*
#endif // S3FS_FDCACHE_ENTITY_H_ #endif // S3FS_FDCACHE_ENTITY_H_