diff --git a/src/CryFuse.cpp b/src/CryFuse.cpp index ed438834..d8074ae2 100644 --- a/src/CryFuse.cpp +++ b/src/CryFuse.cpp @@ -5,6 +5,9 @@ #include #include +#include "cryfs_lib/CryNode.h" +#include "cryfs_lib/CryErrnoException.h" + #define UNUSED(expr) (void)(expr) using fusepp::path; @@ -25,11 +28,12 @@ CryFuse::CryFuse(CryDevice *device) } int CryFuse::getattr(const path &path, struct stat *stbuf) { - UNUSED(stbuf); - //printf("getattr(%s, _)\n", path.c_str()); - auto real_path = _device->RootDir() / path; - int retstat = lstat(real_path.c_str(), stbuf); - return errcode_map(retstat); + try { + _device->LoadFromPath(path)->stat(stbuf); + return 0; + } catch(cryfs::CryErrnoException &e) { + return -e.getErrno(); + } } int CryFuse::fgetattr(const path &path, struct stat *stbuf, fuse_file_info *fileinfo) { diff --git a/src/cryfs_lib/CMakeLists.txt b/src/cryfs_lib/CMakeLists.txt index 95b0c809..0a0910f7 100644 --- a/src/cryfs_lib/CMakeLists.txt +++ b/src/cryfs_lib/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(cryfs_lib CryDevice.cpp) +add_library(cryfs_lib CryDevice.cpp CryDir.cpp CryErrnoException.cpp CryFile.cpp CryNode.cpp) -target_link_libraries(cryfs_lib) +target_link_libraries(cryfs_lib boost_filesystem) diff --git a/src/cryfs_lib/CryDevice.cpp b/src/cryfs_lib/CryDevice.cpp index a91135be..d9fe5395 100644 --- a/src/cryfs_lib/CryDevice.cpp +++ b/src/cryfs_lib/CryDevice.cpp @@ -1,12 +1,30 @@ #include "../cryfs_lib/CryDevice.h" -#include +#include + +#include "CryDir.h" +#include "CryFile.h" +#include "CryErrnoException.h" using namespace cryfs; -CryDevice::CryDevice(const fusepp::path &rootdir) +using std::unique_ptr; +using std::make_unique; + +CryDevice::CryDevice(const bf::path &rootdir) :_rootdir(rootdir) { } CryDevice::~CryDevice() { } + +unique_ptr CryDevice::LoadFromPath(const bf::path &path) { + auto real_path = RootDir() / path; + if(bf::is_directory(real_path)) { + return make_unique(this, path); + } else if(bf::is_regular_file(real_path)) { + return make_unique(this, path); + } + + throw CryErrnoException(ENOENT); +} diff --git a/src/cryfs_lib/CryDevice.h b/src/cryfs_lib/CryDevice.h index 02fab583..e63a3718 100644 --- a/src/cryfs_lib/CryDevice.h +++ b/src/cryfs_lib/CryDevice.h @@ -2,24 +2,31 @@ #ifndef CRYFS_LIB_CRYDEVICE_H_ #define CRYFS_LIB_CRYDEVICE_H_ -#include "fusepp/Fuse.h" +#include #include "utils/macros.h" +#include namespace cryfs { +class CryNode; + +namespace bf = boost::filesystem; class CryDevice { public: - CryDevice(const fusepp::path &rootdir); + CryDevice(const bf::path &rootdir); virtual ~CryDevice(); - const fusepp::path &RootDir() const; + std::unique_ptr LoadFromPath(const bf::path &path); + //std::unique_ptr LoadFromPath(const bf::path &path) const; + + const bf::path &RootDir() const; private: - const fusepp::path _rootdir; + const bf::path _rootdir; DISALLOW_COPY_AND_ASSIGN(CryDevice); }; -inline const fusepp::path &CryDevice::RootDir() const { +inline const bf::path &CryDevice::RootDir() const { return _rootdir; } diff --git a/src/cryfs_lib/CryDir.cpp b/src/cryfs_lib/CryDir.cpp new file mode 100644 index 00000000..a48c1555 --- /dev/null +++ b/src/cryfs_lib/CryDir.cpp @@ -0,0 +1,13 @@ +#include "CryDir.h" +#include "CryDevice.h" + +namespace cryfs { + +CryDir::CryDir(CryDevice *device, const bf::path &path) + :CryNode(device, path) { +} + +CryDir::~CryDir() { +} + +} /* namespace cryfs */ diff --git a/src/cryfs_lib/CryDir.h b/src/cryfs_lib/CryDir.h new file mode 100644 index 00000000..16d74865 --- /dev/null +++ b/src/cryfs_lib/CryDir.h @@ -0,0 +1,18 @@ +#pragma once +#ifndef CRYFS_LIB_CRYDIR_H_ +#define CRYFS_LIB_CRYDIR_H_ + +#include "CryNode.h" + +namespace cryfs { +class CryDevice; + +class CryDir: public CryNode { +public: + CryDir(CryDevice *device, const bf::path &path); + virtual ~CryDir(); +}; + +} /* namespace cryfs */ + +#endif /* CRYFS_LIB_CRYDIR_H_ */ diff --git a/src/cryfs_lib/CryErrnoException.cpp b/src/cryfs_lib/CryErrnoException.cpp new file mode 100644 index 00000000..b81ad3fc --- /dev/null +++ b/src/cryfs_lib/CryErrnoException.cpp @@ -0,0 +1,24 @@ +#include + +#include +#include +#include + +using std::string; +using std::runtime_error; + +namespace cryfs { + +CryErrnoException::CryErrnoException(int errno_) + :runtime_error(strerror(errno_)), _errno(errno_) { + assert(_errno != 0); +} + +CryErrnoException::~CryErrnoException() { +} + +int CryErrnoException::getErrno() const { + return _errno; +} + +} /* namespace cryfs */ diff --git a/src/cryfs_lib/CryErrnoException.h b/src/cryfs_lib/CryErrnoException.h new file mode 100644 index 00000000..4428dda0 --- /dev/null +++ b/src/cryfs_lib/CryErrnoException.h @@ -0,0 +1,28 @@ +#pragma once +#ifndef CRYFS_LIB_CRYERRNOEXCEPTION_H_ +#define CRYFS_LIB_CRYERRNOEXCEPTION_H_ + +#include +#include + +namespace cryfs { + +class CryErrnoException: public std::runtime_error { +public: + CryErrnoException(int errno_); + virtual ~CryErrnoException(); + + int getErrno() const; +private: + int _errno; +}; + +inline void CHECK_RETVAL(int retval) { + if (retval < 0) { + throw CryErrnoException(errno); + } +} + +} /* namespace cryfs */ + +#endif /* CRYFS_LIB_CRYERRNOEXCEPTION_H_ */ diff --git a/src/cryfs_lib/CryFile.cpp b/src/cryfs_lib/CryFile.cpp new file mode 100644 index 00000000..db0ee485 --- /dev/null +++ b/src/cryfs_lib/CryFile.cpp @@ -0,0 +1,13 @@ +#include "CryFile.h" +#include "CryErrnoException.h" + +namespace cryfs { + +CryFile::CryFile(CryDevice *device, const bf::path &path) + :CryNode(device, path) { +} + +CryFile::~CryFile() { +} + +} /* namespace cryfs */ diff --git a/src/cryfs_lib/CryFile.h b/src/cryfs_lib/CryFile.h new file mode 100644 index 00000000..3e1ac14b --- /dev/null +++ b/src/cryfs_lib/CryFile.h @@ -0,0 +1,20 @@ +#pragma once +#ifndef CRYFS_LIB_CRYFILE_H_ +#define CRYFS_LIB_CRYFILE_H_ + +#include + +#include "CryDevice.h" +#include "CryNode.h" + +namespace cryfs { + +class CryFile: public CryNode { +public: + CryFile(CryDevice *device, const bf::path &path); + virtual ~CryFile(); +}; + +} /* namespace cryfs */ + +#endif /* CRYFS_LIB_CRYFILE_H_ */ diff --git a/src/cryfs_lib/CryNode.cpp b/src/cryfs_lib/CryNode.cpp new file mode 100644 index 00000000..c6ea9d61 --- /dev/null +++ b/src/cryfs_lib/CryNode.cpp @@ -0,0 +1,20 @@ +#include "CryNode.h" + +#include "CryDevice.h" +#include "CryErrnoException.h" + +namespace cryfs { + +CryNode::CryNode(CryDevice *device, const bf::path &path) + :_device(device), _path(path) { +} + +CryNode::~CryNode() { +} + +void CryNode::stat(struct stat *result) const { + int retval = ::lstat(base_path().c_str(), result); + CHECK_RETVAL(retval); +} + +} /* namespace cryfs */ diff --git a/src/cryfs_lib/CryNode.h b/src/cryfs_lib/CryNode.h new file mode 100644 index 00000000..a39747e3 --- /dev/null +++ b/src/cryfs_lib/CryNode.h @@ -0,0 +1,39 @@ +#pragma once +#ifndef CRYFS_LIB_CRYNODE_H_ +#define CRYFS_LIB_CRYNODE_H_ + +#include + +#include "utils/macros.h" +#include "CryDevice.h" +#include + +namespace cryfs { + +namespace bf = boost::filesystem; + +class CryNode { +public: + CryNode(CryDevice *device, const bf::path &path); + virtual ~CryNode(); + + void stat(struct stat *result) const; + +protected: + bf::path base_path() const; + CryDevice *device(); + +private: + CryDevice *const _device; + const bf::path _path; + + DISALLOW_COPY_AND_ASSIGN(CryNode); +}; + +inline bf::path CryNode::base_path() const { + return _device->RootDir() / _path; +} + +} /* namespace cryfs */ + +#endif /* CRYFS_LIB_CRYNODE_H_ */ diff --git a/src/cryfs_lib/utils/macros.h b/src/cryfs_lib/utils/macros.h index e0dca2f5..e6e725c0 100644 --- a/src/cryfs_lib/utils/macros.h +++ b/src/cryfs_lib/utils/macros.h @@ -6,5 +6,4 @@ Class(const Class &rhs) = delete; \ Class &operator=(const Class &rhs) = delete; - #endif /* CRYFS_LIB_UTILS_MACROS_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 2bb7c793..1edfa3ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,7 @@ int main (int argc, char *argv[]) { printf("Version: %d\n", buildconfig::VERSION::MAJOR); - cryfs::CryDevice device(fusepp::path("/mnt/root")); + cryfs::CryDevice device(fusepp::path("/home/heinzi/cryfstest/root")); cryfs::CryFuse fuse(&device); fuse.run(argc, argv); return 0;