mirror of
https://github.com/s3fs-fuse/s3fs-fuse.git
synced 2024-12-22 08:48:55 +00:00
Expand use of std::unique_ptr for FILE* (#2555)
This commit is contained in:
parent
141d74f187
commit
a505cebf9b
@ -345,7 +345,7 @@ bool FdManager::HaveLseekHole()
|
||||
|
||||
// create temporary file
|
||||
int fd;
|
||||
std::unique_ptr<FILE, decltype(&s3fs_fclose)> ptmpfp(MakeTempFile(), &s3fs_fclose);
|
||||
auto ptmpfp = MakeTempFile();
|
||||
if(nullptr == ptmpfp || -1 == (fd = fileno(ptmpfp.get()))){
|
||||
S3FS_PRN_ERR("failed to open temporary file by errno(%d)", errno);
|
||||
FdManager::checked_lseek = true;
|
||||
@ -406,7 +406,7 @@ bool FdManager::CheckTmpDirExist()
|
||||
return IsDir(tmp_dir);
|
||||
}
|
||||
|
||||
FILE* FdManager::MakeTempFile() {
|
||||
std::unique_ptr<FILE, decltype(&s3fs_fclose)> FdManager::MakeTempFile() {
|
||||
int fd;
|
||||
char cfn[PATH_MAX];
|
||||
std::string fn = tmp_dir + "/s3fstmp.XXXXXX";
|
||||
@ -416,13 +416,13 @@ FILE* FdManager::MakeTempFile() {
|
||||
fd = mkstemp(cfn);
|
||||
if (-1 == fd) {
|
||||
S3FS_PRN_ERR("failed to create tmp file. errno(%d)", errno);
|
||||
return nullptr;
|
||||
return {nullptr, &s3fs_fclose};
|
||||
}
|
||||
if (-1 == unlink(cfn)) {
|
||||
S3FS_PRN_ERR("failed to delete tmp file. errno(%d)", errno);
|
||||
return nullptr;
|
||||
return {nullptr, &s3fs_fclose};
|
||||
}
|
||||
return fdopen(fd, "rb+");
|
||||
return {fdopen(fd, "rb+"), &s3fs_fclose};
|
||||
}
|
||||
|
||||
bool FdManager::HasOpenEntityFd(const char* path)
|
||||
@ -1041,11 +1041,13 @@ bool FdManager::CheckAllCache()
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<FILE, decltype(&s3fs_fclose)> pfp(nullptr, &s3fs_fclose);
|
||||
FILE* fp;
|
||||
if(FdManager::check_cache_output.empty()){
|
||||
fp = stdout;
|
||||
}else{
|
||||
if(nullptr == (fp = fopen(FdManager::check_cache_output.c_str(), "a+"))){
|
||||
pfp.reset(fp = fopen(FdManager::check_cache_output.c_str(), "a+"));
|
||||
if(nullptr == pfp){
|
||||
S3FS_PRN_ERR("Could not open(create) output file(%s) for checking all cache by errno(%d)", FdManager::check_cache_output.c_str(), errno);
|
||||
return false;
|
||||
}
|
||||
@ -1067,10 +1069,6 @@ bool FdManager::CheckAllCache()
|
||||
// print foot message
|
||||
S3FS_PRN_CACHE(fp, CACHEDBG_FMT_FOOT, total_file_cnt, err_file_cnt, err_dir_cnt);
|
||||
|
||||
if(stdout != fp){
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
|
||||
#include "common.h"
|
||||
#include "fdcache_entity.h"
|
||||
#include "s3fs_util.h"
|
||||
|
||||
//------------------------------------------------
|
||||
// class FdManager
|
||||
@ -100,7 +101,7 @@ class FdManager
|
||||
static bool HaveLseekHole();
|
||||
static bool SetTmpDir(const char* dir);
|
||||
static bool CheckTmpDirExist();
|
||||
static FILE* MakeTempFile();
|
||||
static std::unique_ptr<FILE, decltype(&s3fs_fclose)> MakeTempFile();
|
||||
static off_t GetTotalDiskSpaceByRatio(int ratio);
|
||||
|
||||
// Return FdEntity associated with path, returning nullptr on error. This operation increments the reference count; callers must decrement via Close after use.
|
||||
|
@ -108,7 +108,7 @@ ino_t FdEntity::GetInode(int fd)
|
||||
//------------------------------------------------
|
||||
FdEntity::FdEntity(const char* tpath, const char* cpath) :
|
||||
path(SAFESTRPTR(tpath)),
|
||||
physical_fd(-1), pfile(nullptr), inode(0), size_orgmeta(0),
|
||||
physical_fd(-1), inode(0), size_orgmeta(0),
|
||||
cachepath(SAFESTRPTR(cpath)), pending_status(pending_status_t::NO_UPDATE_PENDING)
|
||||
{
|
||||
holding_mtime.tv_sec = -1;
|
||||
@ -142,10 +142,7 @@ void FdEntity::Clear()
|
||||
}
|
||||
}
|
||||
}
|
||||
if(pfile){
|
||||
fclose(pfile);
|
||||
pfile = nullptr;
|
||||
}
|
||||
pfile.reset();
|
||||
physical_fd = -1;
|
||||
inode = 0;
|
||||
|
||||
@ -212,10 +209,7 @@ void FdEntity::Close(int fd)
|
||||
}
|
||||
}
|
||||
}
|
||||
if(pfile){
|
||||
fclose(pfile);
|
||||
pfile = nullptr;
|
||||
}
|
||||
pfile.reset();
|
||||
physical_fd = -1;
|
||||
inode = 0;
|
||||
|
||||
@ -537,7 +531,7 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
|
||||
physical_fd = mirrorfd;
|
||||
|
||||
// make file pointer(for being same tmpfile)
|
||||
if(nullptr == (pfile = fdopen(physical_fd, "wb"))){
|
||||
if(nullptr == (pfile = {fdopen(physical_fd, "wb"), &s3fs_fclose})){
|
||||
S3FS_PRN_ERR("failed to get fileno(%s). errno(%d)", cachepath.c_str(), errno);
|
||||
close(physical_fd);
|
||||
physical_fd = -1;
|
||||
@ -550,14 +544,12 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
|
||||
inode = 0;
|
||||
|
||||
// open temporary file
|
||||
if(nullptr == (pfile = FdManager::MakeTempFile()) || -1 ==(physical_fd = fileno(pfile))){
|
||||
auto tmpfile = FdManager::MakeTempFile();
|
||||
if(nullptr == tmpfile || -1 ==(physical_fd = fileno(tmpfile.get()))){
|
||||
S3FS_PRN_ERR("failed to open temporary file by errno(%d)", errno);
|
||||
if(pfile){
|
||||
fclose(pfile);
|
||||
pfile = nullptr;
|
||||
}
|
||||
return (0 == errno ? -EIO : -errno);
|
||||
}
|
||||
pfile = std::move(tmpfile);
|
||||
if(-1 == size){
|
||||
size = 0;
|
||||
pagelist.Init(0, false, false);
|
||||
@ -580,8 +572,7 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
|
||||
if(is_truncate){
|
||||
if(0 != ftruncate(physical_fd, size) || 0 != fsync(physical_fd)){
|
||||
S3FS_PRN_ERR("ftruncate(%s) or fsync returned err(%d)", cachepath.c_str(), errno);
|
||||
fclose(pfile);
|
||||
pfile = nullptr;
|
||||
pfile.reset();
|
||||
physical_fd = -1;
|
||||
inode = 0;
|
||||
return (0 == errno ? -EIO : -errno);
|
||||
@ -614,8 +605,7 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
|
||||
if(UTIME_OMIT != ts_mctime.tv_nsec){
|
||||
if(0 != SetMCtimeHasLock(ts_mctime, ts_mctime)){
|
||||
S3FS_PRN_ERR("failed to set mtime/ctime. errno(%d)", errno);
|
||||
fclose(pfile);
|
||||
pfile = nullptr;
|
||||
pfile.reset();
|
||||
physical_fd = -1;
|
||||
inode = 0;
|
||||
return (0 == errno ? -EIO : -errno);
|
||||
@ -632,10 +622,7 @@ int FdEntity::Open(const headers_t* pmeta, off_t size, const struct timespec& ts
|
||||
if(0 < truncated_size){
|
||||
if(!AddUntreated(truncated_start, truncated_size)){
|
||||
pseudo_fd_map.erase(pseudo_fd);
|
||||
if(pfile){
|
||||
fclose(pfile);
|
||||
pfile = nullptr;
|
||||
}
|
||||
pfile.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1103,7 +1090,7 @@ int FdEntity::NoCacheLoadAndPost(PseudoFdInfo* pseudo_obj, off_t start, off_t si
|
||||
|
||||
// open temporary file
|
||||
int tmpfd;
|
||||
std::unique_ptr<FILE, decltype(&s3fs_fclose)> ptmpfp(FdManager::MakeTempFile(), &s3fs_fclose);
|
||||
auto ptmpfp = FdManager::MakeTempFile();
|
||||
if(nullptr == ptmpfp || -1 == (tmpfd = fileno(ptmpfp.get()))){
|
||||
S3FS_PRN_ERR("failed to open temporary file by errno(%d)", errno);
|
||||
return (0 == errno ? -EIO : -errno);
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "fdcache_page.h"
|
||||
#include "fdcache_untreated.h"
|
||||
#include "metaheader.h"
|
||||
#include "s3fs_util.h"
|
||||
|
||||
//----------------------------------------------
|
||||
// Typedef
|
||||
@ -62,7 +63,7 @@ class FdEntity
|
||||
int physical_fd GUARDED_BY(fdent_lock); // physical file(cache or temporary file) descriptor
|
||||
UntreatedParts untreated_list GUARDED_BY(fdent_lock); // list of untreated parts that have been written and not yet uploaded(for streamupload)
|
||||
fdinfo_map_t pseudo_fd_map GUARDED_BY(fdent_lock); // pseudo file descriptor information map
|
||||
FILE* pfile GUARDED_BY(fdent_lock); // file pointer(tmp file or cache file)
|
||||
std::unique_ptr<FILE, decltype(&s3fs_fclose)> pfile GUARDED_BY(fdent_lock) = {nullptr, &s3fs_fclose}; // file pointer(tmp file or cache file)
|
||||
ino_t inode GUARDED_BY(fdent_lock); // inode number for cache file
|
||||
headers_t orgmeta GUARDED_BY(fdent_lock); // original headers at opening
|
||||
off_t size_orgmeta GUARDED_BY(fdent_lock); // original file size in original headers
|
||||
|
Loading…
Reference in New Issue
Block a user