From d2cba6cfe6b9690eaa6b75d92835a2bc0e2ef2d0 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 12 Nov 2014 15:19:32 +0100 Subject: [PATCH] Implemented CryDir opening --- src/cryfs_lib/CMakeLists.txt | 2 +- src/cryfs_lib/CryDir.cpp | 5 +++++ src/cryfs_lib/CryDir.h | 3 +++ src/cryfs_lib/CryFile.cpp | 2 +- src/cryfs_lib/CryOpenDir.cpp | 42 +++++++++++++++++++++++++++++++++++ src/cryfs_lib/CryOpenDir.h | 31 ++++++++++++++++++++++++++ src/cryfs_lib/CryOpenFile.cpp | 5 +++-- src/cryfs_lib/CryOpenFile.h | 3 ++- 8 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 src/cryfs_lib/CryOpenDir.cpp create mode 100644 src/cryfs_lib/CryOpenDir.h diff --git a/src/cryfs_lib/CMakeLists.txt b/src/cryfs_lib/CMakeLists.txt index ff852416..25b7e573 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 CryErrnoException.cpp CryFile.cpp CryNode.cpp CryOpenFile.cpp CryOpenFileList.cpp) +add_library(cryfs_lib CryDevice.cpp CryDir.cpp CryErrnoException.cpp CryFile.cpp CryNode.cpp CryOpenFile.cpp CryOpenDir.cpp CryOpenFileList.cpp IdList.cpp) target_link_libraries(cryfs_lib boost_filesystem) diff --git a/src/cryfs_lib/CryDir.cpp b/src/cryfs_lib/CryDir.cpp index 727fb6d7..b1704195 100644 --- a/src/cryfs_lib/CryDir.cpp +++ b/src/cryfs_lib/CryDir.cpp @@ -5,6 +5,7 @@ #include #include "CryDevice.h" +#include "CryOpenDir.h" #include "CryFile.h" #include "CryErrnoException.h" @@ -44,4 +45,8 @@ void CryDir::rmdir() { CHECK_RETVAL(retval); } +unique_ptr CryDir::opendir() { + return make_unique(device(), path()); +} + } /* namespace cryfs */ diff --git a/src/cryfs_lib/CryDir.h b/src/cryfs_lib/CryDir.h index 8c70b80f..d4076186 100644 --- a/src/cryfs_lib/CryDir.h +++ b/src/cryfs_lib/CryDir.h @@ -10,6 +10,7 @@ namespace cryfs { class CryDevice; +class CryOpenDir; class CryDir: public CryNode { public: @@ -19,6 +20,8 @@ public: std::unique_ptr createFile(const std::string &name, mode_t mode); std::unique_ptr createDir(const std::string &name, mode_t mode); void rmdir(); + + std::unique_ptr opendir(); private: DISALLOW_COPY_AND_ASSIGN(CryDir); }; diff --git a/src/cryfs_lib/CryFile.cpp b/src/cryfs_lib/CryFile.cpp index 297a448d..eb533bab 100644 --- a/src/cryfs_lib/CryFile.cpp +++ b/src/cryfs_lib/CryFile.cpp @@ -17,7 +17,7 @@ CryFile::~CryFile() { } std::unique_ptr CryFile::open(int flags) const { - return make_unique(base_path(), flags); + return make_unique(device(), path(), flags); } void CryFile::truncate(off_t size) const { diff --git a/src/cryfs_lib/CryOpenDir.cpp b/src/cryfs_lib/CryOpenDir.cpp new file mode 100644 index 00000000..3b7faf62 --- /dev/null +++ b/src/cryfs_lib/CryOpenDir.cpp @@ -0,0 +1,42 @@ +#include + +#include "CryDevice.h" +#include "CryErrnoException.h" + +using std::unique_ptr; +using std::make_unique; +using std::vector; +using std::string; + +namespace cryfs { + +CryOpenDir::CryOpenDir(const CryDevice *device, const bf::path &path) + :_dir(::opendir((device->RootDir() / path).c_str())) { + if (_dir == nullptr) { + throw CryErrnoException(errno); + } +} + +CryOpenDir::~CryOpenDir() { + int retval = ::closedir(_dir); + CHECK_RETVAL(retval); +} + +unique_ptr> CryOpenDir::readdir() const { + ::rewinddir(_dir); + + auto result = make_unique>(); + + struct dirent *entry = ::readdir(_dir); + while(entry != nullptr) { + result->push_back(entry->d_name); + entry = ::readdir(_dir); + } + //On error, ::readdir returns nullptr and sets errno. + if (errno != 0) { + throw CryErrnoException(errno); + } + return result; +} + +} /* namespace cryfs */ diff --git a/src/cryfs_lib/CryOpenDir.h b/src/cryfs_lib/CryOpenDir.h new file mode 100644 index 00000000..013646b5 --- /dev/null +++ b/src/cryfs_lib/CryOpenDir.h @@ -0,0 +1,31 @@ +#ifndef CRYFS_LIB_CRYOPENDIR_H_ +#define CRYFS_LIB_CRYOPENDIR_H_ + +#include +#include +#include +#include +#include + +#include "utils/macros.h" + +namespace cryfs { +class CryDevice; + +namespace bf = boost::filesystem; + +class CryOpenDir { +public: + CryOpenDir(const CryDevice *device, const bf::path &path); + virtual ~CryOpenDir(); + + std::unique_ptr> readdir() const; +private: + DIR *_dir; + + DISALLOW_COPY_AND_ASSIGN(CryOpenDir); +}; + +} /* namespace cryfs */ + +#endif /* CRYFS_LIB_CRYOPENDIR_H_ */ diff --git a/src/cryfs_lib/CryOpenFile.cpp b/src/cryfs_lib/CryOpenFile.cpp index a5d86b26..00e14bf2 100644 --- a/src/cryfs_lib/CryOpenFile.cpp +++ b/src/cryfs_lib/CryOpenFile.cpp @@ -4,11 +4,12 @@ #include #include "CryErrnoException.h" +#include "CryDevice.h" using namespace cryfs; -CryOpenFile::CryOpenFile(const bf::path &path, int flags) - :_descriptor(::open(path.c_str(), flags)) { +CryOpenFile::CryOpenFile(const CryDevice *device, const bf::path &path, int flags) + :_descriptor(::open((device->RootDir() / path).c_str(), flags)) { CHECK_RETVAL(_descriptor); } diff --git a/src/cryfs_lib/CryOpenFile.h b/src/cryfs_lib/CryOpenFile.h index ca71123f..dd6033ca 100644 --- a/src/cryfs_lib/CryOpenFile.h +++ b/src/cryfs_lib/CryOpenFile.h @@ -7,12 +7,13 @@ #include "utils/macros.h" namespace cryfs { +class CryDevice; namespace bf = boost::filesystem; class CryOpenFile { public: - CryOpenFile(const bf::path &path, int flags); + CryOpenFile(const CryDevice *device, const bf::path &path, int flags); virtual ~CryOpenFile(); void stat(struct ::stat *result) const;