use TMP and TMPDIR for temporary files

This commit is contained in:
Peter Hatina 2013-09-24 15:57:41 +02:00
parent 12da55b830
commit 24302a482a
4 changed files with 16 additions and 23 deletions

View File

@ -709,21 +709,6 @@ int SMTPFileSystem::fgetattr(const char *path, struct stat *buf, fuse_file_info
return 0;
}
char *SMTPFileSystem::expandTmpDir(char *tmp)
{
std::string tmp_dir(tmp ? tmp : "/tmp/");
if (tmp) {
tmp_dir = tmp;
auto it = tmp_dir.find('~');
for (; it != std::string::npos; it = tmp_dir.find('~'))
tmp_dir.replace(it, 1, getenv("HOME"));
if (tmp_dir[tmp_dir.length() - 1] != '/')
tmp_dir += '/';
}
tmp_dir += "simple-mtpfs-XXXXXX";
return mktemp(strdup(tmp_dir.c_str()));
}
bool SMTPFileSystem::removeDir(const std::string &dirname)
{
DIR *dir;
@ -749,13 +734,21 @@ bool SMTPFileSystem::removeDir(const std::string &dirname)
bool SMTPFileSystem::createTmpDir()
{
if (m_options.m_tmp_dir)
removeTmpDir();
removeTmpDir();
m_options.m_tmp_dir = expandTmpDir(getenv("TMPDIR"));
if (!m_options.m_tmp_dir)
return false;
const char *c_tmp = getenv("TMP");
std::string tmp_dir;
if (c_tmp) {
tmp_dir = smtpfs_realpath(c_tmp);
} else {
c_tmp = getenv("TMPDIR");
if (!c_tmp)
c_tmp = "/tmp";
tmp_dir = smtpfs_realpath(c_tmp);
}
tmp_dir += "/simple-mtpfs-XXXXXX";
m_options.m_tmp_dir = ::mktemp(::strdup(tmp_dir.c_str()));
m_tmp_files_pool.setTmpDir(m_options.m_tmp_dir);
if (::mkdir(static_cast<const char*>(m_options.m_tmp_dir), S_IRWXU) != 0)
return false;

View File

@ -114,7 +114,6 @@ public:
int create(const char *path, mode_t mode, fuse_file_info *file_info);
private:
static char *expandTmpDir(char *tmp);
static bool removeDir(const std::string &dirname);
bool createTmpDir();

View File

@ -57,14 +57,14 @@ std::string smtpfs_basename(const std::string &path)
return result;
}
#ifdef HAVE_LIBUSB1
std::string smtpfs_realpath(std::string path)
std::string smtpfs_realpath(const std::string &path)
{
char buf[PATH_MAX + 1];
char *real_path = realpath(path.c_str(), buf);
return std::string(real_path ? buf : "");
}
#ifdef HAVE_LIBUSB1
LIBMTP_raw_device_t *smtpfs_raw_device_new_priv(libusb_device *usb_device)
{
if (!usb_device)

View File

@ -27,6 +27,7 @@
std::string smtpfs_dirname(const std::string &path);
std::string smtpfs_basename(const std::string &path);
std::string smtpfs_realpath(const std::string &path);
bool smtpfs_check_dir(const std::string &path);