Skip to content

Commit 804e2af

Browse files
authored
Merge pull request #51 from suninsky/master
fix: Allow configuration for temporary files directory
2 parents 7d7de81 + b2285cc commit 804e2af

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
dnl Process this file with autoconf to produce a configure script.
2222

2323
AC_PREREQ(2.59)
24-
AC_INIT(cosfs, 1.0.21)
24+
AC_INIT(cosfs, 1.0.22)
2525
AC_CONFIG_HEADER([config.h])
2626

2727
AC_CANONICAL_SYSTEM

src/curl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "s3fs.h"
5050
#include "s3fs_util.h"
5151
#include "s3fs_auth.h"
52+
#include "fdcache.h"
5253

5354
using namespace std;
5455

@@ -69,13 +70,13 @@ static bool make_md5_from_string(const char* pstr, string& md5)
6970
return false;
7071
}
7172
FILE* fp;
72-
if(NULL == (fp = tmpfile())){
73-
S3FS_PRN_ERR("Could not make tmpfile.");
73+
if(NULL == (fp = FdManager::MakeTempFile())){
74+
S3FS_PRN_ERR("failed to open temporary file by errno(%d)", errno);
7475
return false;
7576
}
7677
size_t length = strlen(pstr);
7778
if(length != fwrite(pstr, sizeof(char), length, fp)){
78-
S3FS_PRN_ERR("Failed to write tmpfile.");
79+
S3FS_PRN_ERR("failed to write temporary file by errno(%d)", errno);
7980
fclose(fp);
8081
return false;
8182
}

src/fdcache.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ using namespace std;
5454
//------------------------------------------------
5555
#define MAX_MULTIPART_CNT 10000 // OSS multipart max count
5656

57-
//
58-
// For cache directory top path
59-
//
60-
#if defined(P_tmpdir)
61-
#define TMPFILE_DIR_0PATH P_tmpdir
62-
#else
63-
#define TMPFILE_DIR_0PATH "/tmp"
64-
#endif
65-
6657
size_t FdEntity::max_prefetch_bytes = 100 * 1024 * 1024;
6758

6859
//------------------------------------------------
@@ -1229,8 +1220,8 @@ int FdEntity::NoCacheLoadAndPost(off_t start, size_t size)
12291220
// open temporary file
12301221
FILE* ptmpfp;
12311222
int tmpfd;
1232-
if(NULL == (ptmpfp = tmpfile()) || -1 ==(tmpfd = fileno(ptmpfp))){
1233-
S3FS_PRN_ERR("failed to open tmp file. err(%d)", errno);
1223+
if(NULL == (ptmpfp = FdManager::MakeTempFile()) || -1 ==(tmpfd = fileno(ptmpfp))){
1224+
S3FS_PRN_ERR("failed to open temporary file by errno(%d)", errno);
12341225
if(ptmpfp){
12351226
fclose(ptmpfp);
12361227
}
@@ -1836,7 +1827,7 @@ pthread_mutex_t FdManager::fd_manager_lock;
18361827
bool FdManager::is_lock_init(false);
18371828
string FdManager::cache_dir("");
18381829
size_t FdManager::free_disk_space = 0;
1839-
std::string FdManager::tmp_dir = "";
1830+
std::string FdManager::tmp_dir = "/tmp";
18401831

18411832
//------------------------------------------------
18421833
// FdManager class methods
@@ -1969,7 +1960,7 @@ fsblkcnt_t FdManager::GetFreeDiskSpace(const char* path)
19691960
if(0 < FdManager::cache_dir.size()){
19701961
ctoppath = FdManager::cache_dir + "/";
19711962
}else{
1972-
ctoppath = TMPFILE_DIR_0PATH "/";
1963+
ctoppath = tmp_dir + "/";
19731964
}
19741965
if(path && '\0' != *path){
19751966
ctoppath += path;
@@ -1983,8 +1974,31 @@ fsblkcnt_t FdManager::GetFreeDiskSpace(const char* path)
19831974
return (vfsbuf.f_bavail * vfsbuf.f_bsize);
19841975
}
19851976

1977+
bool FdManager::IsDir(const std::string* dir)
1978+
{
1979+
// check the directory
1980+
struct stat st;
1981+
if(0 != stat(dir->c_str(), &st)){
1982+
S3FS_PRN_ERR("could not stat() directory %s by errno(%d).", dir->c_str(), errno);
1983+
return false;
1984+
}
1985+
if(!S_ISDIR(st.st_mode)){
1986+
S3FS_PRN_ERR("the directory %s is not a directory.", dir->c_str());
1987+
return false;
1988+
}
1989+
return true;
1990+
}
1991+
1992+
bool FdManager::CheckTmpDirExist()
1993+
{
1994+
if(FdManager::tmp_dir.empty()){
1995+
return true;
1996+
}
1997+
return IsDir(&tmp_dir);
1998+
}
1999+
19862000
FILE* FdManager::MakeTempFile() {
1987-
if (tmp_dir.empty()) {
2001+
if (tmp_dir.empty() || tmp_dir == "/tmp") {
19882002
return tmpfile();
19892003
}
19902004
int fd;
@@ -2237,7 +2251,7 @@ bool FdManager::SetTmpDir(const char *dir)
22372251
tmp_dir = "/tmp";
22382252
}else{
22392253
tmp_dir = dir;
2240-
}
2254+
}
22412255
return true;
22422256
}
22432257

src/fdcache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class FdManager
201201

202202
private:
203203
static fsblkcnt_t GetFreeDiskSpace(const char* path);
204+
static bool IsDir(const std::string* dir);
204205

205206
public:
206207
FdManager();
@@ -230,6 +231,7 @@ class FdManager
230231
bool Close(FdEntity* ent);
231232
bool ChangeEntityToTempPath(FdEntity* ent, const char* path);
232233
static bool SetTmpDir(const char* dir);
234+
static bool CheckTmpDirExist();
233235
static FILE* MakeTempFile();
234236
};
235237

src/s3fs.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4625,6 +4625,10 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar
46254625
S3fsCurl::SetRetries(static_cast<int>(s3fs_strtoofft(strchr(arg, '=') + sizeof(char))));
46264626
return 0;
46274627
}
4628+
if(0 == STR2NCMP(arg, "tmpdir=")){
4629+
FdManager::SetTmpDir(strchr(arg, '=') + sizeof(char));
4630+
return 0;
4631+
}
46284632
if(0 == STR2NCMP(arg, "use_cache=")){
46294633
FdManager::SetCacheDir(strchr(arg, '=') + sizeof(char));
46304634
return 0;
@@ -4661,10 +4665,6 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar
46614665
S3fsCurl::SetUserAgentSuffix(user_agent_suffix);
46624666
return 0;
46634667
}
4664-
if(0 == STR2NCMP(arg, "tmpdir=")){
4665-
FdManager::SetTmpDir(strchr(arg, '=') + sizeof(char));
4666-
return 0;
4667-
}
46684668
// old format for storage_class
46694669
if(0 == strcmp(arg, "use_rrs") || 0 == STR2NCMP(arg, "use_rrs=")){
46704670
off_t rrs = 1;
@@ -5212,6 +5212,14 @@ int main(int argc, char* argv[])
52125212
// like checking for appropriate lengths and characters
52135213
}
52145214

5215+
// check tmp dir permission
5216+
if(!FdManager::CheckTmpDirExist()){
5217+
S3FS_PRN_EXIT("temporary directory doesn't exists.");
5218+
S3fsCurl::DestroyS3fsCurl();
5219+
s3fs_destroy_global_ssl();
5220+
exit(EXIT_FAILURE);
5221+
}
5222+
52155223
// check cache dir permission
52165224
if(!FdManager::CheckCacheTopDir() || !CacheFileStat::CheckCacheFileStatTopDir()){
52175225
S3FS_PRN_EXIT("could not allow cache directory permission, check permission of cache directories.");

0 commit comments

Comments
 (0)