diff --git a/src/simple-mtpfs-fuse.cpp b/src/simple-mtpfs-fuse.cpp index 8c1257c..af73737 100644 --- a/src/simple-mtpfs-fuse.cpp +++ b/src/simple-mtpfs-fuse.cpp @@ -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(m_options.m_tmp_dir), S_IRWXU) != 0) return false; diff --git a/src/simple-mtpfs-fuse.h b/src/simple-mtpfs-fuse.h index c443e59..3a01ff0 100644 --- a/src/simple-mtpfs-fuse.h +++ b/src/simple-mtpfs-fuse.h @@ -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(); diff --git a/src/simple-mtpfs-util.cpp b/src/simple-mtpfs-util.cpp index 56679fb..c21bd12 100644 --- a/src/simple-mtpfs-util.cpp +++ b/src/simple-mtpfs-util.cpp @@ -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) diff --git a/src/simple-mtpfs-util.h b/src/simple-mtpfs-util.h index 3230373..94d962a 100644 --- a/src/simple-mtpfs-util.h +++ b/src/simple-mtpfs-util.h @@ -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);