mirror of
https://github.com/phatina/simple-mtpfs
synced 2025-03-09 22:58:55 +00:00
temporary storage rewrite
This commit is contained in:
parent
01c28b1e53
commit
34e9c8ffb3
@ -18,16 +18,10 @@
|
||||
#include <config.h>
|
||||
#include <iostream>
|
||||
extern "C" {
|
||||
# include <dirent.h>
|
||||
# include <errno.h>
|
||||
# include <fcntl.h>
|
||||
# include <fuse/fuse_opt.h>
|
||||
# include <libgen.h>
|
||||
# include <unistd.h>
|
||||
# include <stddef.h>
|
||||
# include <stdlib.h>
|
||||
# include <sys/stat.h>
|
||||
# include <sys/types.h>
|
||||
}
|
||||
#include "simple-mtpfs-fuse.h"
|
||||
#include "simple-mtpfs-log.h"
|
||||
@ -167,7 +161,6 @@ SMTPFileSystem::SMTPFileSystemOptions::SMTPFileSystemOptions()
|
||||
, m_enable_move(false)
|
||||
, m_list_devices(false)
|
||||
, m_device_no(1)
|
||||
, m_tmp_dir(nullptr)
|
||||
#ifdef HAVE_LIBUSB1
|
||||
, m_device_file(nullptr)
|
||||
#endif // HAVE_LIBUSB1
|
||||
@ -177,7 +170,6 @@ SMTPFileSystem::SMTPFileSystemOptions::SMTPFileSystemOptions()
|
||||
|
||||
SMTPFileSystem::SMTPFileSystemOptions::~SMTPFileSystemOptions()
|
||||
{
|
||||
free(static_cast<void*>(m_tmp_dir));
|
||||
#ifdef HAVE_LIBUSB1
|
||||
free(static_cast<void*>(m_device_file));
|
||||
#endif // HAVE_LIBUSB1
|
||||
@ -388,7 +380,7 @@ bool SMTPFileSystem::exec()
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!createTmpDir()) {
|
||||
if (!m_tmp_files_pool.createTmpDir()) {
|
||||
logerr("Can not create a temporary directory.\n");
|
||||
return false;
|
||||
}
|
||||
@ -415,7 +407,7 @@ bool SMTPFileSystem::exec()
|
||||
}
|
||||
m_device.disconnect();
|
||||
|
||||
removeTmpDir();
|
||||
m_tmp_files_pool.removeTmpDir();
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -761,57 +753,3 @@ int SMTPFileSystem::ftruncate(const char *path, off_t offset,
|
||||
const_cast<TypeTmpFile*>(tmp_file)->setModified();
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool SMTPFileSystem::removeDir(const std::string &dirname)
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
std::string path;
|
||||
|
||||
dir = ::opendir(dirname.c_str());
|
||||
if (!dir)
|
||||
return false;
|
||||
|
||||
while ((entry = ::readdir(dir))) {
|
||||
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
|
||||
path = dirname + "/" + entry->d_name;
|
||||
if (entry->d_type == DT_DIR)
|
||||
return removeDir(path);
|
||||
::unlink(path.c_str());
|
||||
}
|
||||
}
|
||||
::closedir(dir);
|
||||
::remove(dirname.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SMTPFileSystem::createTmpDir()
|
||||
{
|
||||
removeTmpDir();
|
||||
|
||||
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 = TMPDIR;
|
||||
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;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SMTPFileSystem::removeTmpDir()
|
||||
{
|
||||
if (m_options.m_tmp_dir)
|
||||
return removeDir(std::string(m_options.m_tmp_dir));
|
||||
return false;
|
||||
}
|
||||
|
@ -47,7 +47,6 @@ private:
|
||||
int m_enable_move;
|
||||
int m_list_devices;
|
||||
int m_device_no;
|
||||
char *m_tmp_dir;
|
||||
#ifdef HAVE_LIBUSB1
|
||||
char *m_device_file;
|
||||
#endif // HAVE_LIBUSB1
|
||||
|
@ -20,9 +20,10 @@
|
||||
#include <sstream>
|
||||
#include "simple-mtpfs-tmp-files-pool.h"
|
||||
#include "simple-mtpfs-sha1.h"
|
||||
#include "simple-mtpfs-util.h"
|
||||
|
||||
TmpFilesPool::TmpFilesPool():
|
||||
m_tmp_path(TMPDIR "/simple-mtpfs"),
|
||||
m_tmp_dir(smtpfs_get_tmpdir()),
|
||||
m_pool()
|
||||
{
|
||||
}
|
||||
@ -52,5 +53,18 @@ std::string TmpFilesPool::makeTmpPath(const std::string &path_device) const
|
||||
static int cnt = 0;
|
||||
std::stringstream ss;
|
||||
ss << path_device << ++cnt;
|
||||
return m_tmp_path + std::string("/") + SHA1::sumString(ss.str());
|
||||
return m_tmp_dir + std::string("/") + SHA1::sumString(ss.str());
|
||||
}
|
||||
|
||||
bool TmpFilesPool::createTmpDir()
|
||||
{
|
||||
removeTmpDir();
|
||||
return smtpfs_create_dir(m_tmp_dir);
|
||||
}
|
||||
|
||||
bool TmpFilesPool::removeTmpDir()
|
||||
{
|
||||
if (!m_tmp_dir.empty())
|
||||
return smtpfs_remove_dir(m_tmp_dir);
|
||||
return false;
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ public:
|
||||
TmpFilesPool();
|
||||
~TmpFilesPool();
|
||||
|
||||
void setTmpDir(const std::string &path) { m_tmp_path = path; }
|
||||
void setTmpDir(const std::string &tmp_dir) { m_tmp_dir = tmp_dir; }
|
||||
|
||||
void addFile(const TypeTmpFile &tmp) { m_pool.insert(tmp); }
|
||||
void removeFile(int desc);
|
||||
@ -37,9 +37,11 @@ public:
|
||||
const TypeTmpFile *getFile(int desc) const;
|
||||
|
||||
std::string makeTmpPath(const std::string &path_device) const;
|
||||
bool createTmpDir();
|
||||
bool removeTmpDir();
|
||||
|
||||
private:
|
||||
std::string m_tmp_path;
|
||||
std::string m_tmp_dir;
|
||||
std::set<TypeTmpFile> m_pool;
|
||||
};
|
||||
|
||||
|
@ -18,14 +18,16 @@
|
||||
#include <config.h>
|
||||
#include <cstring>
|
||||
#ifdef HAVE_LIBUSB1
|
||||
# include <string>
|
||||
# include <iomanip>
|
||||
# include <sstream>
|
||||
#endif // HAVE_LIBUSB1
|
||||
extern "C" {
|
||||
# include <dirent.h>
|
||||
# include <libgen.h>
|
||||
# include <limits.h>
|
||||
# include <stdlib.h>
|
||||
# include <sys/stat.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
}
|
||||
#ifdef HAVE_LIBUSB1
|
||||
@ -103,6 +105,56 @@ std::string smtpfs_realpath(const std::string &path)
|
||||
return std::string(real_path ? buf : "");
|
||||
}
|
||||
|
||||
std::string smtpfs_get_tmpdir()
|
||||
{
|
||||
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 = TMPDIR;
|
||||
tmp_dir = smtpfs_realpath(c_tmp);
|
||||
}
|
||||
|
||||
tmp_dir += "/simple-mtpfs-XXXXXX";
|
||||
char *c_tmp_dir = ::mktemp(::strdup(tmp_dir.c_str()));
|
||||
|
||||
tmp_dir.assign(c_tmp_dir);
|
||||
::free(static_cast<void*>(c_tmp_dir));
|
||||
|
||||
return tmp_dir;
|
||||
}
|
||||
|
||||
bool smtpfs_create_dir(const std::string &dirname)
|
||||
{
|
||||
return ::mkdir(dirname.c_str(), S_IRWXU) == 0;
|
||||
}
|
||||
|
||||
bool smtpfs_remove_dir(const std::string &dirname)
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
std::string path;
|
||||
|
||||
dir = ::opendir(dirname.c_str());
|
||||
if (!dir)
|
||||
return false;
|
||||
|
||||
while ((entry = ::readdir(dir))) {
|
||||
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
|
||||
path = dirname + "/" + entry->d_name;
|
||||
if (entry->d_type == DT_DIR)
|
||||
return smtpfs_remove_dir(path);
|
||||
::unlink(path.c_str());
|
||||
}
|
||||
}
|
||||
::closedir(dir);
|
||||
::remove(dirname.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBUSB1
|
||||
LIBMTP_raw_device_t *smtpfs_raw_device_new_priv(libusb_device *usb_device)
|
||||
{
|
||||
|
@ -42,7 +42,10 @@ private:
|
||||
std::string smtpfs_dirname(const std::string &path);
|
||||
std::string smtpfs_basename(const std::string &path);
|
||||
std::string smtpfs_realpath(const std::string &path);
|
||||
std::string smtpfs_get_tmpdir();
|
||||
|
||||
bool smtpfs_create_dir(const std::string &dirname);
|
||||
bool smtpfs_remove_dir(const std::string &dirname);
|
||||
bool smtpfs_check_dir(const std::string &path);
|
||||
|
||||
#ifdef HAVE_LIBUSB1
|
||||
|
Loading…
Reference in New Issue
Block a user