From b2285cc68923af63aa6d0f4069a38b2ec4963164 Mon Sep 17 00:00:00 2001 From: xhaopan Date: Fri, 26 Apr 2024 16:55:30 +0800 Subject: [PATCH] fix: Allow configuration for temporary files directory --- configure.ac | 2 +- src/curl.cpp | 7 ++++--- src/fdcache.cpp | 44 +++++++++++++++++++++++++++++--------------- src/fdcache.h | 2 ++ src/s3fs.cpp | 16 ++++++++++++---- 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/configure.ac b/configure.ac index feb997e..f58b4dc 100644 --- a/configure.ac +++ b/configure.ac @@ -21,7 +21,7 @@ dnl Process this file with autoconf to produce a configure script. AC_PREREQ(2.59) -AC_INIT(cosfs, 1.0.21) +AC_INIT(cosfs, 1.0.22) AC_CONFIG_HEADER([config.h]) AC_CANONICAL_SYSTEM diff --git a/src/curl.cpp b/src/curl.cpp index 6426c28..0ef8f97 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -49,6 +49,7 @@ #include "s3fs.h" #include "s3fs_util.h" #include "s3fs_auth.h" +#include "fdcache.h" using namespace std; @@ -69,13 +70,13 @@ static bool make_md5_from_string(const char* pstr, string& md5) return false; } FILE* fp; - if(NULL == (fp = tmpfile())){ - S3FS_PRN_ERR("Could not make tmpfile."); + if(NULL == (fp = FdManager::MakeTempFile())){ + S3FS_PRN_ERR("failed to open temporary file by errno(%d)", errno); return false; } size_t length = strlen(pstr); if(length != fwrite(pstr, sizeof(char), length, fp)){ - S3FS_PRN_ERR("Failed to write tmpfile."); + S3FS_PRN_ERR("failed to write temporary file by errno(%d)", errno); fclose(fp); return false; } diff --git a/src/fdcache.cpp b/src/fdcache.cpp index 4ad54db..a6399fe 100644 --- a/src/fdcache.cpp +++ b/src/fdcache.cpp @@ -54,15 +54,6 @@ using namespace std; //------------------------------------------------ #define MAX_MULTIPART_CNT 10000 // OSS multipart max count -// -// For cache directory top path -// -#if defined(P_tmpdir) -#define TMPFILE_DIR_0PATH P_tmpdir -#else -#define TMPFILE_DIR_0PATH "/tmp" -#endif - size_t FdEntity::max_prefetch_bytes = 100 * 1024 * 1024; //------------------------------------------------ @@ -1229,8 +1220,8 @@ int FdEntity::NoCacheLoadAndPost(off_t start, size_t size) // open temporary file FILE* ptmpfp; int tmpfd; - if(NULL == (ptmpfp = tmpfile()) || -1 ==(tmpfd = fileno(ptmpfp))){ - S3FS_PRN_ERR("failed to open tmp file. err(%d)", errno); + if(NULL == (ptmpfp = FdManager::MakeTempFile()) || -1 ==(tmpfd = fileno(ptmpfp))){ + S3FS_PRN_ERR("failed to open temporary file by errno(%d)", errno); if(ptmpfp){ fclose(ptmpfp); } @@ -1836,7 +1827,7 @@ pthread_mutex_t FdManager::fd_manager_lock; bool FdManager::is_lock_init(false); string FdManager::cache_dir(""); size_t FdManager::free_disk_space = 0; -std::string FdManager::tmp_dir = ""; +std::string FdManager::tmp_dir = "/tmp"; //------------------------------------------------ // FdManager class methods @@ -1969,7 +1960,7 @@ fsblkcnt_t FdManager::GetFreeDiskSpace(const char* path) if(0 < FdManager::cache_dir.size()){ ctoppath = FdManager::cache_dir + "/"; }else{ - ctoppath = TMPFILE_DIR_0PATH "/"; + ctoppath = tmp_dir + "/"; } if(path && '\0' != *path){ ctoppath += path; @@ -1983,8 +1974,31 @@ fsblkcnt_t FdManager::GetFreeDiskSpace(const char* path) return (vfsbuf.f_bavail * vfsbuf.f_bsize); } +bool FdManager::IsDir(const std::string* dir) +{ + // check the directory + struct stat st; + if(0 != stat(dir->c_str(), &st)){ + S3FS_PRN_ERR("could not stat() directory %s by errno(%d).", dir->c_str(), errno); + return false; + } + if(!S_ISDIR(st.st_mode)){ + S3FS_PRN_ERR("the directory %s is not a directory.", dir->c_str()); + return false; + } + return true; +} + +bool FdManager::CheckTmpDirExist() +{ + if(FdManager::tmp_dir.empty()){ + return true; + } + return IsDir(&tmp_dir); +} + FILE* FdManager::MakeTempFile() { - if (tmp_dir.empty()) { + if (tmp_dir.empty() || tmp_dir == "/tmp") { return tmpfile(); } int fd; @@ -2237,7 +2251,7 @@ bool FdManager::SetTmpDir(const char *dir) tmp_dir = "/tmp"; }else{ tmp_dir = dir; - } + } return true; } diff --git a/src/fdcache.h b/src/fdcache.h index 00a458c..422f6f3 100644 --- a/src/fdcache.h +++ b/src/fdcache.h @@ -201,6 +201,7 @@ class FdManager private: static fsblkcnt_t GetFreeDiskSpace(const char* path); + static bool IsDir(const std::string* dir); public: FdManager(); @@ -230,6 +231,7 @@ class FdManager bool Close(FdEntity* ent); bool ChangeEntityToTempPath(FdEntity* ent, const char* path); static bool SetTmpDir(const char* dir); + static bool CheckTmpDirExist(); static FILE* MakeTempFile(); }; diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 2605ae6..d92cc83 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -4625,6 +4625,10 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar S3fsCurl::SetRetries(static_cast(s3fs_strtoofft(strchr(arg, '=') + sizeof(char)))); return 0; } + if(0 == STR2NCMP(arg, "tmpdir=")){ + FdManager::SetTmpDir(strchr(arg, '=') + sizeof(char)); + return 0; + } if(0 == STR2NCMP(arg, "use_cache=")){ FdManager::SetCacheDir(strchr(arg, '=') + sizeof(char)); return 0; @@ -4661,10 +4665,6 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar S3fsCurl::SetUserAgentSuffix(user_agent_suffix); return 0; } - if(0 == STR2NCMP(arg, "tmpdir=")){ - FdManager::SetTmpDir(strchr(arg, '=') + sizeof(char)); - return 0; - } // old format for storage_class if(0 == strcmp(arg, "use_rrs") || 0 == STR2NCMP(arg, "use_rrs=")){ off_t rrs = 1; @@ -5212,6 +5212,14 @@ int main(int argc, char* argv[]) // like checking for appropriate lengths and characters } + // check tmp dir permission + if(!FdManager::CheckTmpDirExist()){ + S3FS_PRN_EXIT("temporary directory doesn't exists."); + S3fsCurl::DestroyS3fsCurl(); + s3fs_destroy_global_ssl(); + exit(EXIT_FAILURE); + } + // check cache dir permission if(!FdManager::CheckCacheTopDir() || !CacheFileStat::CheckCacheFileStatTopDir()){ S3FS_PRN_EXIT("could not allow cache directory permission, check permission of cache directories.");