From c112def9858aa672004aaa632d30e3caef1baa56 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 12 Nov 2014 12:27:23 +0100 Subject: [PATCH] mkdir --- src/CryFuse.cpp | 10 ++++++---- src/cryfs_lib/CryDevice.cpp | 15 ++++++++++++--- src/cryfs_lib/CryDevice.h | 2 ++ src/cryfs_lib/CryDir.cpp | 14 +++++++++++++- src/cryfs_lib/CryDir.h | 3 ++- src/cryfs_lib/CryFile.cpp | 1 + src/cryfs_lib/CryNode.h | 10 ++++++++++ 7 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/CryFuse.cpp b/src/CryFuse.cpp index 7188fa16..8cab4910 100644 --- a/src/CryFuse.cpp +++ b/src/CryFuse.cpp @@ -80,12 +80,14 @@ int CryFuse::mknod(const path &path, mode_t mode, dev_t rdev) { return 0; } -//TODO int CryFuse::mkdir(const path &path, mode_t mode) { //printf("mkdir(%s, %d)\n", path.c_str(), mode); - auto real_path = _device->RootDir() / path; - int retstat = ::mkdir(real_path.c_str(), mode); - return errcode_map(retstat); + try { + _device->mkdir(path, mode); + return 0; + } catch(cryfs::CryErrnoException &e) { + return -e.getErrno(); + } } //TODO diff --git a/src/cryfs_lib/CryDevice.cpp b/src/cryfs_lib/CryDevice.cpp index 9b784689..ad15ff08 100644 --- a/src/cryfs_lib/CryDevice.cpp +++ b/src/cryfs_lib/CryDevice.cpp @@ -52,7 +52,11 @@ unique_ptr CryDevice::LoadDir(const bf::path &path) { int CryDevice::openFile(const bf::path &path, int flags) { auto file = LoadFile(path); - return _open_files.open(*file, flags); + return openFile(*file, flags); +} + +int CryDevice::openFile(const CryFile &file, int flags) { + return _open_files.open(file, flags); } void CryDevice::closeFile(int descriptor) { @@ -99,6 +103,11 @@ int CryDevice::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()); - dir->createFile(path.filename().native(), mode); - return openFile(path, O_CREAT | O_WRONLY | O_TRUNC); + auto file = dir->createFile(path.filename().native(), mode); + return openFile(*file, O_CREAT | O_WRONLY | O_TRUNC); +} + +void CryDevice::mkdir(const bf::path &path, mode_t mode) { + auto dir = LoadDir(path.parent_path()); + dir->createDir(path.filename().native(), mode); } diff --git a/src/cryfs_lib/CryDevice.h b/src/cryfs_lib/CryDevice.h index e667a078..f47a8638 100644 --- a/src/cryfs_lib/CryDevice.h +++ b/src/cryfs_lib/CryDevice.h @@ -34,12 +34,14 @@ public: void fdatasync(int descriptor); void access(const bf::path &path, int mask); int createAndOpenFile(const bf::path &path, mode_t mode); + void mkdir(const bf::path &path, mode_t mode); const bf::path &RootDir() const; private: std::unique_ptr Load(const bf::path &path); std::unique_ptr LoadFile(const bf::path &path); std::unique_ptr LoadDir(const bf::path &path); + int openFile(const CryFile &file, int flags); const bf::path _rootdir; CryOpenFileList _open_files; diff --git a/src/cryfs_lib/CryDir.cpp b/src/cryfs_lib/CryDir.cpp index 2037cbf0..eb73a632 100644 --- a/src/cryfs_lib/CryDir.cpp +++ b/src/cryfs_lib/CryDir.cpp @@ -5,26 +5,38 @@ #include #include "CryDevice.h" +#include "CryFile.h" #include "CryErrnoException.h" using std::string; using std::unique_ptr; +using std::make_unique; namespace cryfs { CryDir::CryDir(CryDevice *device, const bf::path &path) :CryNode(device, path) { + assert(bf::is_directory(base_path())); } CryDir::~CryDir() { } -void CryDir::createFile(const string &name, mode_t mode) { +unique_ptr CryDir::createFile(const string &name, mode_t mode) { auto file_path = base_path() / name; //Create file int fd = ::creat(file_path.c_str(), mode); CHECK_RETVAL(fd); ::close(fd); + return make_unique(device(), path() / name); +} + +unique_ptr CryDir::createDir(const string &name, mode_t mode) { + auto dir_path = base_path() / name; + //Create dir + int retval = ::mkdir(dir_path.c_str(), mode); + CHECK_RETVAL(retval); + return make_unique(device(), path() / name); } } /* namespace cryfs */ diff --git a/src/cryfs_lib/CryDir.h b/src/cryfs_lib/CryDir.h index 26d4bc8e..0fe172c8 100644 --- a/src/cryfs_lib/CryDir.h +++ b/src/cryfs_lib/CryDir.h @@ -16,7 +16,8 @@ public: CryDir(CryDevice *device, const bf::path &path); virtual ~CryDir(); - void createFile(const std::string &name, mode_t mode); + std::unique_ptr createFile(const std::string &name, mode_t mode); + std::unique_ptr createDir(const std::string &name, mode_t mode); private: DISALLOW_COPY_AND_ASSIGN(CryDir); }; diff --git a/src/cryfs_lib/CryFile.cpp b/src/cryfs_lib/CryFile.cpp index 8993ae1a..2cd2fd95 100644 --- a/src/cryfs_lib/CryFile.cpp +++ b/src/cryfs_lib/CryFile.cpp @@ -10,6 +10,7 @@ namespace cryfs { CryFile::CryFile(CryDevice *device, const bf::path &path) :CryNode(device, path) { + assert(bf::is_regular_file(base_path())); } CryFile::~CryFile() { diff --git a/src/cryfs_lib/CryNode.h b/src/cryfs_lib/CryNode.h index d7ac65ea..c0109498 100644 --- a/src/cryfs_lib/CryNode.h +++ b/src/cryfs_lib/CryNode.h @@ -22,6 +22,8 @@ public: protected: bf::path base_path() const; + const bf::path &path() const; + CryDevice *device(); private: CryDevice *const _device; @@ -34,6 +36,14 @@ inline bf::path CryNode::base_path() const { return _device->RootDir() / _path; } +inline const bf::path &CryNode::path() const { + return _path; +} + +inline CryDevice *CryNode::device() { + return _device; +} + } /* namespace cryfs */ #endif /* CRYFS_LIB_CRYNODE_H_ */