From 6dce6ffd800b6de65616cc10a9dc3e9a1dac51dd Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 15 Nov 2014 17:24:07 +0100 Subject: [PATCH] Put fuse bindings in their own directory --- src/cryfs_lib/CMakeLists.txt | 2 +- src/cryfs_lib/CryDevice.cpp | 2 +- src/cryfs_lib/CryDevice.h | 5 +- src/cryfs_lib/CryDir.cpp | 2 +- src/cryfs_lib/CryDir.h | 2 +- src/cryfs_lib/CryFile.cpp | 2 +- src/cryfs_lib/CryFile.h | 2 +- src/cryfs_lib/CryNode.cpp | 2 +- src/cryfs_lib/CryNode.h | 2 +- src/cryfs_lib/CryOpenFile.cpp | 2 +- src/cryfs_lib/CryOpenFile.h | 2 +- src/fusepp/CMakeLists.txt | 5 +- src/fusepp/FuseDevice.cpp | 132 ----------------- src/fusepp/FuseDir.cpp | 1 - src/fusepp/FuseFile.cpp | 1 - src/fusepp/FuseNode.cpp | 1 - src/fusepp/FuseOpenFile.cpp | 1 - src/fusepp/fs_interface/FuseDevice.h | 22 +++ src/fusepp/{ => fs_interface}/FuseDir.h | 0 src/fusepp/{ => fs_interface}/FuseFile.h | 0 src/fusepp/{ => fs_interface}/FuseNode.h | 1 - src/fusepp/{ => fs_interface}/FuseOpenFile.h | 0 src/fusepp/fusebindings/CMakeLists.txt | 3 + src/fusepp/{ => fusebindings}/Fuse.cpp | 60 ++++---- src/fusepp/{ => fusebindings}/Fuse.h | 10 +- src/fusepp/{ => fusebindings}/params.h | 0 src/fusepp/impl/CMakeLists.txt | 1 + src/fusepp/impl/FilesystemImpl.cpp | 137 ++++++++++++++++++ .../{FuseDevice.h => impl/FilesystemImpl.h} | 23 +-- src/fusepp/{ => impl}/FuseErrnoException.cpp | 0 src/fusepp/{ => impl}/FuseErrnoException.h | 0 src/fusepp/{ => impl}/FuseOpenFileList.cpp | 0 src/fusepp/{ => impl}/FuseOpenFileList.h | 6 +- src/fusepp/{ => impl}/IdList.cpp | 0 src/fusepp/{ => impl}/IdList.h | 2 +- src/main.cpp | 6 +- 36 files changed, 235 insertions(+), 202 deletions(-) delete mode 100644 src/fusepp/FuseDevice.cpp delete mode 100644 src/fusepp/FuseDir.cpp delete mode 100644 src/fusepp/FuseFile.cpp delete mode 100644 src/fusepp/FuseNode.cpp delete mode 100644 src/fusepp/FuseOpenFile.cpp create mode 100644 src/fusepp/fs_interface/FuseDevice.h rename src/fusepp/{ => fs_interface}/FuseDir.h (100%) rename src/fusepp/{ => fs_interface}/FuseFile.h (100%) rename src/fusepp/{ => fs_interface}/FuseNode.h (94%) rename src/fusepp/{ => fs_interface}/FuseOpenFile.h (100%) create mode 100644 src/fusepp/fusebindings/CMakeLists.txt rename src/fusepp/{ => fusebindings}/Fuse.cpp (92%) rename src/fusepp/{ => fusebindings}/Fuse.h (94%) rename src/fusepp/{ => fusebindings}/params.h (100%) create mode 100644 src/fusepp/impl/CMakeLists.txt create mode 100644 src/fusepp/impl/FilesystemImpl.cpp rename src/fusepp/{FuseDevice.h => impl/FilesystemImpl.h} (78%) rename src/fusepp/{ => impl}/FuseErrnoException.cpp (100%) rename src/fusepp/{ => impl}/FuseErrnoException.h (100%) rename src/fusepp/{ => impl}/FuseOpenFileList.cpp (100%) rename src/fusepp/{ => impl}/FuseOpenFileList.h (88%) rename src/fusepp/{ => impl}/IdList.cpp (100%) rename src/fusepp/{ => impl}/IdList.h (97%) diff --git a/src/cryfs_lib/CMakeLists.txt b/src/cryfs_lib/CMakeLists.txt index 0c07f1ad..9507631c 100644 --- a/src/cryfs_lib/CMakeLists.txt +++ b/src/cryfs_lib/CMakeLists.txt @@ -1,3 +1,3 @@ add_library(cryfs_lib CryDevice.cpp CryDir.cpp CryFile.cpp CryNode.cpp CryOpenFile.cpp) -target_link_libraries(cryfs_lib fusepp) +target_link_libraries(cryfs_lib boost_filesystem boost_system) diff --git a/src/cryfs_lib/CryDevice.cpp b/src/cryfs_lib/CryDevice.cpp index f66f2cf2..e7c324a5 100644 --- a/src/cryfs_lib/CryDevice.cpp +++ b/src/cryfs_lib/CryDevice.cpp @@ -3,7 +3,7 @@ #include "CryDir.h" #include "CryFile.h" -#include "fusepp/FuseErrnoException.h" +#include "fusepp/impl/FuseErrnoException.h" using std::unique_ptr; diff --git a/src/cryfs_lib/CryDevice.h b/src/cryfs_lib/CryDevice.h index b3de1843..025813e6 100644 --- a/src/cryfs_lib/CryDevice.h +++ b/src/cryfs_lib/CryDevice.h @@ -4,7 +4,8 @@ #include -#include "fusepp/FuseDevice.h" +#include "fusepp/fs_interface/FuseDevice.h" +#include "fusepp/utils/macros.h" namespace cryfs { @@ -15,7 +16,7 @@ public: CryDevice(const bf::path &rootdir); virtual ~CryDevice(); - void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) override; + void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override; const bf::path &RootDir() const; private: diff --git a/src/cryfs_lib/CryDir.cpp b/src/cryfs_lib/CryDir.cpp index f72f2836..e1258371 100644 --- a/src/cryfs_lib/CryDir.cpp +++ b/src/cryfs_lib/CryDir.cpp @@ -5,7 +5,7 @@ #include #include -#include "fusepp/FuseErrnoException.h" +#include "fusepp/impl/FuseErrnoException.h" #include "CryDevice.h" #include "CryFile.h" diff --git a/src/cryfs_lib/CryDir.h b/src/cryfs_lib/CryDir.h index 5653abbc..cc03b6f4 100644 --- a/src/cryfs_lib/CryDir.h +++ b/src/cryfs_lib/CryDir.h @@ -2,7 +2,7 @@ #ifndef CRYFS_LIB_CRYDIR_H_ #define CRYFS_LIB_CRYDIR_H_ -#include "fusepp/FuseDir.h" +#include "fusepp/fs_interface/FuseDir.h" #include "CryNode.h" namespace cryfs { diff --git a/src/cryfs_lib/CryFile.cpp b/src/cryfs_lib/CryFile.cpp index 51a1bd5b..563816d0 100644 --- a/src/cryfs_lib/CryFile.cpp +++ b/src/cryfs_lib/CryFile.cpp @@ -2,7 +2,7 @@ #include "CryDevice.h" #include "CryOpenFile.h" -#include "fusepp/FuseErrnoException.h" +#include "fusepp/impl/FuseErrnoException.h" namespace bf = boost::filesystem; diff --git a/src/cryfs_lib/CryFile.h b/src/cryfs_lib/CryFile.h index cc872640..5636c82f 100644 --- a/src/cryfs_lib/CryFile.h +++ b/src/cryfs_lib/CryFile.h @@ -3,7 +3,7 @@ #define CRYFS_LIB_CRYFILE_H_ #include "CryNode.h" -#include "fusepp/FuseFile.h" +#include "fusepp/fs_interface/FuseFile.h" namespace cryfs { diff --git a/src/cryfs_lib/CryNode.cpp b/src/cryfs_lib/CryNode.cpp index 855e5cba..50e31e89 100644 --- a/src/cryfs_lib/CryNode.cpp +++ b/src/cryfs_lib/CryNode.cpp @@ -3,7 +3,7 @@ #include #include "CryDevice.h" -#include "fusepp/FuseErrnoException.h" +#include "fusepp/impl/FuseErrnoException.h" namespace bf = boost::filesystem; diff --git a/src/cryfs_lib/CryNode.h b/src/cryfs_lib/CryNode.h index 38c3b709..d3b7263e 100644 --- a/src/cryfs_lib/CryNode.h +++ b/src/cryfs_lib/CryNode.h @@ -2,7 +2,7 @@ #ifndef CRYFS_LIB_CRYNODE_H_ #define CRYFS_LIB_CRYNODE_H_ -#include "fusepp/FuseNode.h" +#include "fusepp/fs_interface/FuseNode.h" #include "fusepp/utils/macros.h" #include "CryDevice.h" diff --git a/src/cryfs_lib/CryOpenFile.cpp b/src/cryfs_lib/CryOpenFile.cpp index 11c8eaa9..2d400ca3 100644 --- a/src/cryfs_lib/CryOpenFile.cpp +++ b/src/cryfs_lib/CryOpenFile.cpp @@ -4,7 +4,7 @@ #include #include "CryDevice.h" -#include "fusepp/FuseErrnoException.h" +#include "fusepp/impl/FuseErrnoException.h" namespace bf = boost::filesystem; diff --git a/src/cryfs_lib/CryOpenFile.h b/src/cryfs_lib/CryOpenFile.h index e2a784b4..89b1ad0f 100644 --- a/src/cryfs_lib/CryOpenFile.h +++ b/src/cryfs_lib/CryOpenFile.h @@ -2,7 +2,7 @@ #ifndef CRYFS_LIB_CRYOPENFILE_H_ #define CRYFS_LIB_CRYOPENFILE_H_ -#include "fusepp/FuseOpenFile.h" +#include "fusepp/fs_interface/FuseOpenFile.h" #include "fusepp/utils/macros.h" namespace cryfs { diff --git a/src/fusepp/CMakeLists.txt b/src/fusepp/CMakeLists.txt index 1cc95e99..3b92f95d 100644 --- a/src/fusepp/CMakeLists.txt +++ b/src/fusepp/CMakeLists.txt @@ -1,3 +1,2 @@ -add_library(fusepp Fuse.cpp FuseDevice.cpp FuseDir.cpp FuseErrnoException.cpp FuseFile.cpp FuseNode.cpp FuseOpenFile.cpp FuseOpenFileList.cpp IdList.cpp) - -target_link_libraries(fusepp fuse boost_filesystem boost_system) +add_subdirectory(fusebindings) +add_subdirectory(impl) diff --git a/src/fusepp/FuseDevice.cpp b/src/fusepp/FuseDevice.cpp deleted file mode 100644 index 84ff1c8b..00000000 --- a/src/fusepp/FuseDevice.cpp +++ /dev/null @@ -1,132 +0,0 @@ -#include "FuseDevice.h" - -#include -#include - -#include "FuseDir.h" -#include "FuseErrnoException.h" -#include "FuseFile.h" - - -#include "utils/pointer.h" - -using namespace fusepp; - -using std::unique_ptr; -using std::make_unique; -using std::vector; -using std::string; - -namespace bf = boost::filesystem; - -FuseDevice::FuseDevice() - :_open_files() { -} - -FuseDevice::~FuseDevice() { -} - -unique_ptr FuseDevice::LoadFile(const bf::path &path) { - auto node = Load(path); - auto file = dynamic_pointer_move(node); - if (!file) { - throw FuseErrnoException(EISDIR); - } - return file; -} - -unique_ptr FuseDevice::LoadDir(const bf::path &path) { - auto node = Load(path); - auto dir = dynamic_pointer_move(node); - if (!dir) { - throw FuseErrnoException(ENOTDIR); - } - return dir; -} - -int FuseDevice::openFile(const bf::path &path, int flags) { - auto file = LoadFile(path); - return openFile(*file, flags); -} - -int FuseDevice::openFile(const FuseFile &file, int flags) { - return _open_files.open(file, flags); -} - -void FuseDevice::closeFile(int descriptor) { - _open_files.close(descriptor); -} - -void FuseDevice::lstat(const bf::path &path, struct ::stat *stbuf) { - Load(path)->stat(stbuf); -} - -void FuseDevice::fstat(int descriptor, struct ::stat *stbuf) { - _open_files.get(descriptor)->stat(stbuf); -} - -void FuseDevice::truncate(const bf::path &path, off_t size) { - LoadFile(path)->truncate(size); -} - -void FuseDevice::ftruncate(int descriptor, off_t size) { - _open_files.get(descriptor)->truncate(size); -} - -int FuseDevice::read(int descriptor, void *buf, size_t count, off_t offset) { - return _open_files.get(descriptor)->read(buf, count, offset); -} - -void FuseDevice::write(int descriptor, const void *buf, size_t count, off_t offset) { - _open_files.get(descriptor)->write(buf, count, offset); -} - -void FuseDevice::fsync(int descriptor) { - _open_files.get(descriptor)->fsync(); -} - -void FuseDevice::fdatasync(int descriptor) { - _open_files.get(descriptor)->fdatasync(); -} - -void FuseDevice::access(const bf::path &path, int mask) { - Load(path)->access(mask); -} - -int FuseDevice::createAndOpenFile(const bf::path &path, mode_t mode) { - //TODO Creating the file opens and closes it. We then reopen it afterwards. - // This is slow. Improve! - auto dir = LoadDir(path.parent_path()); - auto file = dir->createFile(path.filename().native(), mode); - return openFile(*file, O_WRONLY | O_TRUNC); -} - -void FuseDevice::mkdir(const bf::path &path, mode_t mode) { - auto dir = LoadDir(path.parent_path()); - dir->createDir(path.filename().native(), mode); -} - -void FuseDevice::rmdir(const bf::path &path) { - auto dir = LoadDir(path); - dir->rmdir(); -} - -void FuseDevice::unlink(const bf::path &path) { - auto file = LoadFile(path); - file->unlink(); -} - -void FuseDevice::rename(const bf::path &from, const bf::path &to) { - auto node = Load(from); - node->rename(to); -} - -unique_ptr> FuseDevice::readDir(const bf::path &path) { - auto dir = LoadDir(path); - return dir->children(); -} - -void FuseDevice::utimens(const bf::path &path, const timespec times[2]) { - auto node = Load(path); - node->utimens(times); -} diff --git a/src/fusepp/FuseDir.cpp b/src/fusepp/FuseDir.cpp deleted file mode 100644 index 8c3076fd..00000000 --- a/src/fusepp/FuseDir.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "FuseDir.h" diff --git a/src/fusepp/FuseFile.cpp b/src/fusepp/FuseFile.cpp deleted file mode 100644 index b3f66c26..00000000 --- a/src/fusepp/FuseFile.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "FuseFile.h" diff --git a/src/fusepp/FuseNode.cpp b/src/fusepp/FuseNode.cpp deleted file mode 100644 index feaf46c3..00000000 --- a/src/fusepp/FuseNode.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "FuseNode.h" diff --git a/src/fusepp/FuseOpenFile.cpp b/src/fusepp/FuseOpenFile.cpp deleted file mode 100644 index 27e40456..00000000 --- a/src/fusepp/FuseOpenFile.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "FuseOpenFile.h" diff --git a/src/fusepp/fs_interface/FuseDevice.h b/src/fusepp/fs_interface/FuseDevice.h new file mode 100644 index 00000000..f0a3a991 --- /dev/null +++ b/src/fusepp/fs_interface/FuseDevice.h @@ -0,0 +1,22 @@ +#pragma once +#ifndef FUSEPP_FUSEDEVICE_H_ +#define FUSEPP_FUSEDEVICE_H_ + +#include +#include +#include + +namespace fusepp { +class FuseNode; + +class FuseDevice { +public: + virtual ~FuseDevice() {} + + virtual void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) = 0; + virtual std::unique_ptr Load(const boost::filesystem::path &path) = 0; +}; + +} + +#endif /* FUSEPP_FUSEDEVICE_H_ */ diff --git a/src/fusepp/FuseDir.h b/src/fusepp/fs_interface/FuseDir.h similarity index 100% rename from src/fusepp/FuseDir.h rename to src/fusepp/fs_interface/FuseDir.h diff --git a/src/fusepp/FuseFile.h b/src/fusepp/fs_interface/FuseFile.h similarity index 100% rename from src/fusepp/FuseFile.h rename to src/fusepp/fs_interface/FuseFile.h diff --git a/src/fusepp/FuseNode.h b/src/fusepp/fs_interface/FuseNode.h similarity index 94% rename from src/fusepp/FuseNode.h rename to src/fusepp/fs_interface/FuseNode.h index 5e7bb78f..4bdc98fb 100644 --- a/src/fusepp/FuseNode.h +++ b/src/fusepp/fs_interface/FuseNode.h @@ -4,7 +4,6 @@ #include -#include "utils/macros.h" #include namespace fusepp { diff --git a/src/fusepp/FuseOpenFile.h b/src/fusepp/fs_interface/FuseOpenFile.h similarity index 100% rename from src/fusepp/FuseOpenFile.h rename to src/fusepp/fs_interface/FuseOpenFile.h diff --git a/src/fusepp/fusebindings/CMakeLists.txt b/src/fusepp/fusebindings/CMakeLists.txt new file mode 100644 index 00000000..0acb4f61 --- /dev/null +++ b/src/fusepp/fusebindings/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(fusepp_fuse_bindings Fuse.cpp) + +target_link_libraries(fusepp_fuse_bindings fuse fusepp_impl) diff --git a/src/fusepp/Fuse.cpp b/src/fusepp/fusebindings/Fuse.cpp similarity index 92% rename from src/fusepp/Fuse.cpp rename to src/fusepp/fusebindings/Fuse.cpp index e214d96a..3a615be4 100644 --- a/src/fusepp/Fuse.cpp +++ b/src/fusepp/fusebindings/Fuse.cpp @@ -2,8 +2,8 @@ #include #include -#include "FuseDevice.h" -#include "FuseErrnoException.h" +#include "fusepp/impl/FuseErrnoException.h" +#include "fusepp/impl/FilesystemImpl.h" using std::unique_ptr; using std::make_unique; @@ -11,7 +11,7 @@ using std::string; namespace bf = boost::filesystem; -using namespace fusepp; +using namespace fusepp::fusebindings; #define FUSE_OBJ ((Fuse *) fuse_get_context()->private_data) @@ -205,8 +205,8 @@ fuse_operations *operations() { Fuse::~Fuse() { } -Fuse::Fuse(FuseDevice *device) - :_device(device) { +Fuse::Fuse(FilesystemImpl *impl) + :_impl(impl) { } void Fuse::run(int argc, char **argv) { @@ -216,7 +216,7 @@ void Fuse::run(int argc, char **argv) { int Fuse::getattr(const bf::path &path, struct stat *stbuf) { //printf("getattr(%s, _, _)\n", path.c_str()); try { - _device->lstat(path, stbuf); + _impl->lstat(path, stbuf); return 0; } catch(fusepp::FuseErrnoException &e) { return -e.getErrno(); @@ -236,7 +236,7 @@ int Fuse::fgetattr(const bf::path &path, struct stat *stbuf, fuse_file_info *fil } try { - _device->fstat(fileinfo->fh, stbuf); + _impl->fstat(fileinfo->fh, stbuf); return 0; } catch(fusepp::FuseErrnoException &e) { return -e.getErrno(); @@ -263,7 +263,7 @@ int Fuse::mknod(const bf::path &path, mode_t mode, dev_t rdev) { int Fuse::mkdir(const bf::path &path, mode_t mode) { //printf("mkdir(%s, %d)\n", path.c_str(), mode); try { - _device->mkdir(path, mode); + _impl->mkdir(path, mode); return 0; } catch(fusepp::FuseErrnoException &e) { return -e.getErrno(); @@ -273,7 +273,7 @@ int Fuse::mkdir(const bf::path &path, mode_t mode) { int Fuse::unlink(const bf::path &path) { //printf("unlink(%s)\n", path.c_str()); try { - _device->unlink(path); + _impl->unlink(path); return 0; } catch(fusepp::FuseErrnoException &e) { return -e.getErrno(); @@ -282,7 +282,7 @@ int Fuse::unlink(const bf::path &path) { int Fuse::rmdir(const bf::path &path) { try { - _device->rmdir(path); + _impl->rmdir(path); return 0; } catch(fusepp::FuseErrnoException &e) { return -e.getErrno(); @@ -292,8 +292,8 @@ int Fuse::rmdir(const bf::path &path) { //TODO int Fuse::symlink(const bf::path &from, const bf::path &to) { printf("NOT IMPLEMENTED: symlink(%s, %s)\n", from.c_str(), to.c_str()); - //auto real_from = _device->RootDir() / from; - //auto real_to = _device->RootDir() / to; + //auto real_from = _impl->RootDir() / from; + //auto real_to = _impl->RootDir() / to; //int retstat = ::symlink(real_from.c_str(), real_to.c_str()); //return errcode_map(retstat); return ENOSYS; @@ -302,7 +302,7 @@ int Fuse::symlink(const bf::path &from, const bf::path &to) { int Fuse::rename(const bf::path &from, const bf::path &to) { //printf("rename(%s, %s)\n", from.c_str(), to.c_str()); try { - _device->rename(from, to); + _impl->rename(from, to); return 0; } catch(fusepp::FuseErrnoException &e) { return -e.getErrno(); @@ -312,8 +312,8 @@ int Fuse::rename(const bf::path &from, const bf::path &to) { //TODO int Fuse::link(const bf::path &from, const bf::path &to) { printf("NOT IMPLEMENTED: link(%s, %s)\n", from.c_str(), to.c_str()); - //auto real_from = _device->RootDir() / from; - //auto real_to = _device->RootDir() / to; + //auto real_from = _impl->RootDir() / from; + //auto real_to = _impl->RootDir() / to; //int retstat = ::link(real_from.c_str(), real_to.c_str()); //return errcode_map(retstat); return ENOSYS; @@ -322,7 +322,7 @@ int Fuse::link(const bf::path &from, const bf::path &to) { //TODO int Fuse::chmod(const bf::path &path, mode_t mode) { printf("NOT IMPLEMENTED: chmod(%s, %d)\n", path.c_str(), mode); - //auto real_path = _device->RootDir() / path; + //auto real_path = _impl->RootDir() / path; //int retstat = ::chmod(real_path.c_str(), mode); //return errcode_map(retstat); return ENOSYS; @@ -331,7 +331,7 @@ int Fuse::chmod(const bf::path &path, mode_t mode) { //TODO int Fuse::chown(const bf::path &path, uid_t uid, gid_t gid) { printf("NOT IMPLEMENTED: chown(%s, %d, %d)\n", path.c_str(), uid, gid); - //auto real_path = _device->RootDir() / path; + //auto real_path = _impl->RootDir() / path; //int retstat = ::chown(real_path.c_str(), uid, gid); //return errcode_map(retstat); return ENOSYS; @@ -340,7 +340,7 @@ int Fuse::chown(const bf::path &path, uid_t uid, gid_t gid) { int Fuse::truncate(const bf::path &path, off_t size) { //printf("truncate(%s, %zu)\n", path.c_str(), size); try { - _device->truncate(path, size); + _impl->truncate(path, size); return 0; } catch (FuseErrnoException &e) { return -e.getErrno(); @@ -351,7 +351,7 @@ int Fuse::ftruncate(const bf::path &path, off_t size, fuse_file_info *fileinfo) //printf("ftruncate(%s, %zu, _)\n", path.c_str(), size); UNUSED(path); try { - _device->ftruncate(fileinfo->fh, size); + _impl->ftruncate(fileinfo->fh, size); return 0; } catch (FuseErrnoException &e) { return -e.getErrno(); @@ -362,7 +362,7 @@ int Fuse::ftruncate(const bf::path &path, off_t size, fuse_file_info *fileinfo) int Fuse::utimens(const bf::path &path, const timespec times[2]) { //printf("utimens(%s, _)\n", path.c_str()); try { - _device->utimens(path, times); + _impl->utimens(path, times); return 0; } catch (FuseErrnoException &e) { return -e.getErrno(); @@ -372,7 +372,7 @@ int Fuse::utimens(const bf::path &path, const timespec times[2]) { int Fuse::open(const bf::path &path, fuse_file_info *fileinfo) { //printf("open(%s, _)\n", path.c_str()); try { - fileinfo->fh = _device->openFile(path, fileinfo->flags); + fileinfo->fh = _impl->openFile(path, fileinfo->flags); return 0; } catch (FuseErrnoException &e) { return -e.getErrno(); @@ -383,7 +383,7 @@ int Fuse::release(const bf::path &path, fuse_file_info *fileinfo) { //printf("release(%s, _)\n", path.c_str()); UNUSED(path); try { - _device->closeFile(fileinfo->fh); + _impl->closeFile(fileinfo->fh); return 0; } catch (FuseErrnoException &e) { return -e.getErrno(); @@ -396,7 +396,7 @@ int Fuse::read(const bf::path &path, char *buf, size_t size, off_t offset, fuse_ try { //printf("Reading from file %d\n", fileinfo->fh); //fflush(stdout); - return _device->read(fileinfo->fh, buf, size, offset); + return _impl->read(fileinfo->fh, buf, size, offset); } catch (FuseErrnoException &e) { return -e.getErrno(); } @@ -406,7 +406,7 @@ int Fuse::write(const bf::path &path, const char *buf, size_t size, off_t offset //printf("write(%s, _, %zu, %zu, _)\n", path.c_str(), size, offset); UNUSED(path); try { - _device->write(fileinfo->fh, buf, size, offset); + _impl->write(fileinfo->fh, buf, size, offset); return size; } catch (FuseErrnoException &e) { return -e.getErrno(); @@ -417,7 +417,7 @@ int Fuse::write(const bf::path &path, const char *buf, size_t size, off_t offset int Fuse::statfs(const bf::path &path, struct statvfs *fsstat) { //printf("statfs(%s, _)\n", path.c_str()); try { - _device->statfs(path, fsstat); + _impl->statfs(path, fsstat); return 0; } catch (FuseErrnoException &e) { return -e.getErrno(); @@ -437,9 +437,9 @@ int Fuse::fsync(const bf::path &path, int datasync, fuse_file_info *fileinfo) { UNUSED(path); try { if (datasync) { - _device->fdatasync(fileinfo->fh); + _impl->fdatasync(fileinfo->fh); } else { - _device->fsync(fileinfo->fh); + _impl->fsync(fileinfo->fh); } return 0; } catch (FuseErrnoException &e) { @@ -460,7 +460,7 @@ int Fuse::readdir(const bf::path &path, void *buf, fuse_fill_dir_t filler, off_t //printf("readdir(%s, _, _, %zu, _)\n", path.c_str(), offset); UNUSED(offset); try { - auto entries = _device->readDir(path); + auto entries = _impl->readDir(path); for (const auto &entry : *entries) { //We could pass file metadata to filler() in its third parameter, //but it doesn't help performance since fuse seems to ignore it. @@ -504,7 +504,7 @@ void Fuse::destroy() { int Fuse::access(const bf::path &path, int mask) { //printf("access(%s, %d)\n", path.c_str(), mask); try { - _device->access(path, mask); + _impl->access(path, mask); return 0; } catch (FuseErrnoException &e) { return -e.getErrno(); @@ -514,7 +514,7 @@ int Fuse::access(const bf::path &path, int mask) { int Fuse::create(const bf::path &path, mode_t mode, fuse_file_info *fileinfo) { //printf("create(%s, %d, _)\n", path.c_str(), mode); try { - fileinfo->fh = _device->createAndOpenFile(path, mode); + fileinfo->fh = _impl->createAndOpenFile(path, mode); return 0; } catch (FuseErrnoException &e) { return -e.getErrno(); diff --git a/src/fusepp/Fuse.h b/src/fusepp/fusebindings/Fuse.h similarity index 94% rename from src/fusepp/Fuse.h rename to src/fusepp/fusebindings/Fuse.h index cede4982..4b6bba72 100644 --- a/src/fusepp/Fuse.h +++ b/src/fusepp/fusebindings/Fuse.h @@ -8,14 +8,17 @@ #include #include #include -#include "utils/macros.h" +#include "../utils/macros.h" namespace fusepp { class FuseDevice; +class FilesystemImpl; + +namespace fusebindings { class Fuse { public: - Fuse(FuseDevice *device); + Fuse(FilesystemImpl *implementation); virtual ~Fuse(); void run(int argc, char **argv); @@ -52,10 +55,11 @@ public: int create(const boost::filesystem::path &path, mode_t mode, fuse_file_info *fileinfo); private: - FuseDevice *_device; + FilesystemImpl *_impl; DISALLOW_COPY_AND_ASSIGN(Fuse); }; } +} #endif /* FUSEPP_FUSE_H_ */ diff --git a/src/fusepp/params.h b/src/fusepp/fusebindings/params.h similarity index 100% rename from src/fusepp/params.h rename to src/fusepp/fusebindings/params.h diff --git a/src/fusepp/impl/CMakeLists.txt b/src/fusepp/impl/CMakeLists.txt new file mode 100644 index 00000000..4d28ec72 --- /dev/null +++ b/src/fusepp/impl/CMakeLists.txt @@ -0,0 +1 @@ +add_library(fusepp_impl FilesystemImpl.cpp FuseOpenFileList.cpp IdList.cpp FuseErrnoException.cpp) diff --git a/src/fusepp/impl/FilesystemImpl.cpp b/src/fusepp/impl/FilesystemImpl.cpp new file mode 100644 index 00000000..5cfa6b68 --- /dev/null +++ b/src/fusepp/impl/FilesystemImpl.cpp @@ -0,0 +1,137 @@ +#include "FilesystemImpl.h" + +#include +#include + +#include "fusepp/fs_interface/FuseDevice.h" +#include "fusepp/fs_interface/FuseDir.h" +#include "FuseErrnoException.h" +#include "fusepp/fs_interface/FuseFile.h" + + +#include "fusepp/utils/pointer.h" + +using namespace fusepp; + +using std::unique_ptr; +using std::make_unique; +using std::vector; +using std::string; + +namespace bf = boost::filesystem; + +FilesystemImpl::FilesystemImpl(FuseDevice *device) + :_device(device), _open_files() { +} + +FilesystemImpl::~FilesystemImpl() { +} + +unique_ptr FilesystemImpl::LoadFile(const bf::path &path) { + auto node = _device->Load(path); + auto file = dynamic_pointer_move(node); + if (!file) { + throw FuseErrnoException(EISDIR); + } + return file; +} + +unique_ptr FilesystemImpl::LoadDir(const bf::path &path) { + auto node = _device->Load(path); + auto dir = dynamic_pointer_move(node); + if (!dir) { + throw FuseErrnoException(ENOTDIR); + } + return dir; +} + +int FilesystemImpl::openFile(const bf::path &path, int flags) { + auto file = LoadFile(path); + return openFile(*file, flags); +} + +int FilesystemImpl::openFile(const FuseFile &file, int flags) { + return _open_files.open(file, flags); +} + +void FilesystemImpl::closeFile(int descriptor) { + _open_files.close(descriptor); +} + +void FilesystemImpl::lstat(const bf::path &path, struct ::stat *stbuf) { + _device->Load(path)->stat(stbuf); +} + +void FilesystemImpl::fstat(int descriptor, struct ::stat *stbuf) { + _open_files.get(descriptor)->stat(stbuf); +} + +void FilesystemImpl::truncate(const bf::path &path, off_t size) { + LoadFile(path)->truncate(size); +} + +void FilesystemImpl::ftruncate(int descriptor, off_t size) { + _open_files.get(descriptor)->truncate(size); +} + +int FilesystemImpl::read(int descriptor, void *buf, size_t count, off_t offset) { + return _open_files.get(descriptor)->read(buf, count, offset); +} + +void FilesystemImpl::write(int descriptor, const void *buf, size_t count, off_t offset) { + _open_files.get(descriptor)->write(buf, count, offset); +} + +void FilesystemImpl::fsync(int descriptor) { + _open_files.get(descriptor)->fsync(); +} + +void FilesystemImpl::fdatasync(int descriptor) { + _open_files.get(descriptor)->fdatasync(); +} + +void FilesystemImpl::access(const bf::path &path, int mask) { + _device->Load(path)->access(mask); +} + +int FilesystemImpl::createAndOpenFile(const bf::path &path, mode_t mode) { + //TODO Creating the file opens and closes it. We then reopen it afterwards. + // This is slow. Improve! + auto dir = LoadDir(path.parent_path()); + auto file = dir->createFile(path.filename().native(), mode); + return openFile(*file, O_WRONLY | O_TRUNC); +} + +void FilesystemImpl::mkdir(const bf::path &path, mode_t mode) { + auto dir = LoadDir(path.parent_path()); + dir->createDir(path.filename().native(), mode); +} + +void FilesystemImpl::rmdir(const bf::path &path) { + auto dir = LoadDir(path); + dir->rmdir(); +} + +void FilesystemImpl::unlink(const bf::path &path) { + auto file = LoadFile(path); + file->unlink(); +} + +void FilesystemImpl::rename(const bf::path &from, const bf::path &to) { + auto node = _device->Load(from); + node->rename(to); +} + +unique_ptr> FilesystemImpl::readDir(const bf::path &path) { + auto dir = LoadDir(path); + return dir->children(); +} + +void FilesystemImpl::utimens(const bf::path &path, const timespec times[2]) { + auto node = _device->Load(path); + node->utimens(times); +} + +void FilesystemImpl::statfs(const bf::path &path, struct statvfs *fsstat) { + _device->statfs(path, fsstat); +} diff --git a/src/fusepp/FuseDevice.h b/src/fusepp/impl/FilesystemImpl.h similarity index 78% rename from src/fusepp/FuseDevice.h rename to src/fusepp/impl/FilesystemImpl.h index 2e9a1247..0ed97afb 100644 --- a/src/fusepp/FuseDevice.h +++ b/src/fusepp/impl/FilesystemImpl.h @@ -1,14 +1,14 @@ #pragma once -#ifndef FUSEPP_FUSEDEVICE_H_ -#define FUSEPP_FUSEDEVICE_H_ +#ifndef FUSEPP_FILESYSTEMIMPL_H_ +#define FUSEPP_FILESYSTEMIMPL_H_ #include -#include +#include "FuseOpenFileList.h" #include #include #include -#include "utils/macros.h" +#include "fusepp/utils/macros.h" namespace fusepp { class FuseNode; @@ -16,10 +16,10 @@ class FuseFile; class FuseOpenFile; class FuseDir; -class FuseDevice { +class FilesystemImpl { public: - FuseDevice(); - virtual ~FuseDevice(); + FilesystemImpl(FuseDevice *device); + virtual ~FilesystemImpl(); int openFile(const boost::filesystem::path &path, int flags); void closeFile(int descriptor); @@ -39,18 +39,19 @@ public: void rename(const boost::filesystem::path &from, const boost::filesystem::path &to); std::unique_ptr> readDir(const boost::filesystem::path &path); void utimens(const boost::filesystem::path &path, const timespec times[2]); - virtual void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) = 0; + void statfs(const boost::filesystem::path &path, struct statvfs *fsstat); private: - virtual std::unique_ptr Load(const boost::filesystem::path &path) = 0; std::unique_ptr LoadFile(const boost::filesystem::path &path); std::unique_ptr LoadDir(const boost::filesystem::path &path); int openFile(const FuseFile &file, int flags); + + FuseDevice *_device; FuseOpenFileList _open_files; - DISALLOW_COPY_AND_ASSIGN(FuseDevice); + DISALLOW_COPY_AND_ASSIGN(FilesystemImpl); }; } -#endif /* FUSEPP_FUSEDEVICE_H_ */ +#endif /* FUSEPP_FILESYSTEMIMPL_H_ */ diff --git a/src/fusepp/FuseErrnoException.cpp b/src/fusepp/impl/FuseErrnoException.cpp similarity index 100% rename from src/fusepp/FuseErrnoException.cpp rename to src/fusepp/impl/FuseErrnoException.cpp diff --git a/src/fusepp/FuseErrnoException.h b/src/fusepp/impl/FuseErrnoException.h similarity index 100% rename from src/fusepp/FuseErrnoException.h rename to src/fusepp/impl/FuseErrnoException.h diff --git a/src/fusepp/FuseOpenFileList.cpp b/src/fusepp/impl/FuseOpenFileList.cpp similarity index 100% rename from src/fusepp/FuseOpenFileList.cpp rename to src/fusepp/impl/FuseOpenFileList.cpp diff --git a/src/fusepp/FuseOpenFileList.h b/src/fusepp/impl/FuseOpenFileList.h similarity index 88% rename from src/fusepp/FuseOpenFileList.h rename to src/fusepp/impl/FuseOpenFileList.h index 27640146..e3fe9642 100644 --- a/src/fusepp/FuseOpenFileList.h +++ b/src/fusepp/impl/FuseOpenFileList.h @@ -2,10 +2,10 @@ #ifndef FUSEPP_FUSEOPENFILELIST_H_ #define FUSEPP_FUSEOPENFILELIST_H_ -#include "utils/macros.h" +#include "fusepp/utils/macros.h" #include "IdList.h" -#include "FuseFile.h" -#include "FuseOpenFile.h" +#include "fusepp/fs_interface/FuseFile.h" +#include "fusepp/fs_interface/FuseOpenFile.h" namespace fusepp { diff --git a/src/fusepp/IdList.cpp b/src/fusepp/impl/IdList.cpp similarity index 100% rename from src/fusepp/IdList.cpp rename to src/fusepp/impl/IdList.cpp diff --git a/src/fusepp/IdList.h b/src/fusepp/impl/IdList.h similarity index 97% rename from src/fusepp/IdList.h rename to src/fusepp/impl/IdList.h index 0e88ab51..95b1c0aa 100644 --- a/src/fusepp/IdList.h +++ b/src/fusepp/impl/IdList.h @@ -5,7 +5,7 @@ #include #include #include -#include "utils/macros.h" +#include "fusepp/utils/macros.h" namespace fusepp { diff --git a/src/main.cpp b/src/main.cpp index 0818e963..4b888315 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,8 @@ #include #include "buildconfig/BuildConfig.h" -#include "fusepp/Fuse.h" +#include "fusepp/fusebindings/Fuse.h" +#include "fusepp/impl/FilesystemImpl.h" #include "cryfs_lib/CryDevice.h" namespace bf = boost::filesystem; @@ -12,7 +13,8 @@ int main (int argc, char *argv[]) { printf("Version: %d\n", buildconfig::VERSION::MAJOR); cryfs::CryDevice device(bf::path("/home/heinzi/cryfstest/root")); - fusepp::Fuse fuse(&device); + fusepp::FilesystemImpl fsimpl(&device); + fusepp::fusebindings::Fuse fuse(&fsimpl); fuse.run(argc, argv); return 0; }