Separate serialization and deserialization code (#2566)

This is clearer than a bool parameter.
This commit is contained in:
Andrew Gaul 2024-10-24 08:22:35 +09:00 committed by GitHub
parent a8af6cb3b4
commit 31061416bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 148 additions and 139 deletions

View File

@ -981,7 +981,7 @@ bool FdManager::RawCheckAllCache(FILE* fp, const char* cache_stat_top_dir, const
// open cache stat file and load page info. // open cache stat file and load page info.
PageList pagelist; PageList pagelist;
CacheFileStat cfstat(object_file_path.c_str()); CacheFileStat cfstat(object_file_path.c_str());
if(!cfstat.ReadOnlyOpen() || !pagelist.Serialize(cfstat, false, cache_file_inode)){ if(!cfstat.ReadOnlyOpen() || !pagelist.Deserialize(cfstat, cache_file_inode)){
++err_file_cnt; ++err_file_cnt;
S3FS_PRN_CACHE(fp, CACHEDBG_FMT_FILE_PROB, object_file_path.c_str(), strOpenedWarn.c_str()); S3FS_PRN_CACHE(fp, CACHEDBG_FMT_FILE_PROB, object_file_path.c_str(), strOpenedWarn.c_str());
S3FS_PRN_CACHE(fp, CACHEDBG_FMT_CRIT_HEAD, "Could not load cache file stats information"); S3FS_PRN_CACHE(fp, CACHEDBG_FMT_CRIT_HEAD, "Could not load cache file stats information");

View File

@ -137,7 +137,7 @@ void FdEntity::Clear()
ino_t cur_inode = GetInode(); ino_t cur_inode = GetInode();
if(0 != cur_inode && cur_inode == inode){ if(0 != cur_inode && cur_inode == inode){
CacheFileStat cfstat(path.c_str()); CacheFileStat cfstat(path.c_str());
if(!pagelist.Serialize(cfstat, true, inode)){ if(!pagelist.Serialize(cfstat, inode)){
S3FS_PRN_WARN("failed to save cache stat file(%s).", path.c_str()); S3FS_PRN_WARN("failed to save cache stat file(%s).", path.c_str());
} }
} }
@ -204,7 +204,7 @@ void FdEntity::Close(int fd)
ino_t cur_inode = GetInode(); ino_t cur_inode = GetInode();
if(0 != cur_inode && cur_inode == inode){ if(0 != cur_inode && cur_inode == inode){
CacheFileStat cfstat(path.c_str()); CacheFileStat cfstat(path.c_str());
if(!pagelist.Serialize(cfstat, true, inode)){ if(!pagelist.Serialize(cfstat, inode)){
S3FS_PRN_WARN("failed to save cache stat file(%s).", path.c_str()); S3FS_PRN_WARN("failed to save cache stat file(%s).", path.c_str());
} }
} }
@ -449,7 +449,7 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
// try to open cache file // try to open cache file
if( -1 != (physical_fd = open(cachepath.c_str(), O_RDWR)) && if( -1 != (physical_fd = open(cachepath.c_str(), O_RDWR)) &&
0 != (inode = FdEntity::GetInode(physical_fd)) && 0 != (inode = FdEntity::GetInode(physical_fd)) &&
pagelist.Serialize(*pcfstat, false, inode) ) pagelist.Deserialize(*pcfstat, inode))
{ {
// succeed to open cache file and to load stats data // succeed to open cache file and to load stats data
st = {}; st = {};
@ -581,7 +581,7 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
// reset cache stat file // reset cache stat file
if(need_save_csf && pcfstat.get()){ if(need_save_csf && pcfstat.get()){
if(!pagelist.Serialize(*pcfstat, true, inode)){ if(!pagelist.Serialize(*pcfstat, inode)){
S3FS_PRN_WARN("failed to save cache stat file(%s), but continue...", path.c_str()); S3FS_PRN_WARN("failed to save cache stat file(%s), but continue...", path.c_str());
} }
} }

View File

@ -819,12 +819,12 @@ bool PageList::ClearAllModified()
return Compress(); return Compress();
} }
bool PageList::Serialize(CacheFileStat& file, bool is_output, ino_t inode) bool PageList::Serialize(CacheFileStat& file, ino_t inode)
{ {
if(!file.Open()){ if(!file.Open()){
return false; return false;
} }
if(is_output){
// //
// put to file // put to file
// //
@ -845,7 +845,15 @@ bool PageList::Serialize(CacheFileStat& file, bool is_output, ino_t inode)
return false; return false;
} }
}else{ return true;
}
bool PageList::Deserialize(CacheFileStat& file, ino_t inode)
{
if(!file.Open()){
return false;
}
// //
// loading from file // loading from file
// //
@ -966,7 +974,7 @@ bool PageList::Serialize(CacheFileStat& file, bool is_output, ino_t inode)
Clear(); Clear();
return false; return false;
} }
}
return true; return true;
} }

View File

@ -94,6 +94,7 @@ class PageList
void Clear(); void Clear();
bool Parse(off_t new_pos); bool Parse(off_t new_pos);
bool Serialize(CacheFileStat& file, ino_t inode);
public: public:
static void FreeList(fdpage_list_t& list); static void FreeList(fdpage_list_t& list);
@ -122,7 +123,7 @@ class PageList
bool ClearAllModified(); bool ClearAllModified();
bool Compress(); bool Compress();
bool Serialize(CacheFileStat& file, bool is_output, ino_t inode); bool Deserialize(CacheFileStat& file, ino_t inode);
void Dump() const; void Dump() const;
bool CompareSparseFile(int fd, size_t file_size, fdpage_list_t& err_area_list, fdpage_list_t& warn_area_list); bool CompareSparseFile(int fd, size_t file_size, fdpage_list_t& err_area_list, fdpage_list_t& warn_area_list);
}; };