diff --git a/src/CryDir.cpp b/src/CryDir.cpp index 800feae9..0db032a3 100644 --- a/src/CryDir.cpp +++ b/src/CryDir.cpp @@ -26,7 +26,7 @@ using blockstore::Key; namespace cryfs { CryDir::CryDir(CryDevice *device, unique_ptr parent, const Key &key) -: _device(device), _parent(std::move(parent)), _key(key) { +: CryNode(device, std::move(parent), key) { } CryDir::~CryDir() { @@ -40,7 +40,7 @@ void CryDir::stat(struct ::stat *result) const { unique_ptr CryDir::createAndOpenFile(const string &name, mode_t mode) { auto blob = LoadBlob(); - auto child = _device->CreateBlob(); + auto child = device()->CreateBlob(); Key childkey = child->key(); blob->AddChildFile(name, childkey); //TODO Do we need a return value in createDir for fspp? If not, change fspp Dir interface! @@ -50,19 +50,14 @@ unique_ptr CryDir::createAndOpenFile(const string &name, mode_t void CryDir::createDir(const string &name, mode_t mode) { auto blob = LoadBlob(); - auto child = _device->CreateBlob(); + auto child = device()->CreateBlob(); Key childkey = child->key(); blob->AddChildDir(name, childkey); DirBlob::InitializeEmptyDir(std::move(child)); } unique_ptr CryDir::LoadBlob() const { - return make_unique(_device->LoadBlob(_key)); -} - -void CryDir::rmdir() { - _parent->RemoveChild(_key); - _device->RemoveBlob(_key); + return make_unique(CryNode::LoadBlob()); } unique_ptr> CryDir::children() const { diff --git a/src/CryDir.h b/src/CryDir.h index 9b5c1b7c..10aa5e3a 100644 --- a/src/CryDir.h +++ b/src/CryDir.h @@ -17,16 +17,11 @@ public: //TODO return type variance to CryFile/CryDir? std::unique_ptr createAndOpenFile(const std::string &name, mode_t mode) override; void createDir(const std::string &name, mode_t mode) override; - void rmdir() override; //TODO Make Entry a public class instead of hidden in DirBlob (which is not publicly visible) std::unique_ptr> children() const override; private: - CryDevice *_device; - std::unique_ptr _parent; - blockstore::Key _key; - std::unique_ptr LoadBlob() const; DISALLOW_COPY_AND_ASSIGN(CryDir); diff --git a/src/CryFile.cpp b/src/CryFile.cpp index a847d7f0..9ca13098 100644 --- a/src/CryFile.cpp +++ b/src/CryFile.cpp @@ -19,16 +19,14 @@ using blockstore::Key; namespace cryfs { CryFile::CryFile(CryDevice *device, unique_ptr parent, const Key &key) -: _device(device), - _parent(std::move(parent)), - _key(key) { +: CryNode(device, std::move(parent), key) { } CryFile::~CryFile() { } unique_ptr CryFile::open(int flags) const { - auto blob = _device->LoadBlob(_key); + auto blob = LoadBlob(); assert(blob.get() != nullptr); return make_unique(make_unique(std::move(blob))); } @@ -36,18 +34,13 @@ unique_ptr CryFile::open(int flags) const { void CryFile::stat(struct ::stat *result) const { result->st_mode = S_IFREG | S_IRUSR | S_IXUSR | S_IWUSR; //TODO Loading the blob for only getting the size is not very performant. - result->st_size = FileBlob(_device->LoadBlob(_key)).size(); + result->st_size = FileBlob(LoadBlob()).size(); return; throw FuseErrnoException(ENOTSUP); } void CryFile::truncate(off_t size) const { - FileBlob(_device->LoadBlob(_key)).resize(size); -} - -void CryFile::unlink() { - _parent->RemoveChild(_key); - _device->RemoveBlob(_key); + FileBlob(LoadBlob()).resize(size); } } diff --git a/src/CryFile.h b/src/CryFile.h index 3b553fd4..04d49fd9 100644 --- a/src/CryFile.h +++ b/src/CryFile.h @@ -17,12 +17,8 @@ public: void stat(struct ::stat *result) const override; std::unique_ptr open(int flags) const override; void truncate(off_t size) const override; - void unlink() override; private: - CryDevice *_device; - std::unique_ptr _parent; - blockstore::Key _key; DISALLOW_COPY_AND_ASSIGN(CryFile); }; diff --git a/src/CryNode.cpp b/src/CryNode.cpp index 24ddb3ff..b6105b31 100644 --- a/src/CryNode.cpp +++ b/src/CryNode.cpp @@ -9,13 +9,20 @@ namespace bf = boost::filesystem; +using std::unique_ptr; +using blockstore::Key; +using blobstore::Blob; + //TODO Get rid of this in favor of an exception hierarchy using fspp::fuse::CHECK_RETVAL; using fspp::fuse::FuseErrnoException; namespace cryfs { -CryNode::CryNode() { +CryNode::CryNode(CryDevice *device, unique_ptr parent, const Key &key) +: _device(device), + _parent(std::move(parent)), + _key(key) { } CryNode::~CryNode() { @@ -34,4 +41,21 @@ void CryNode::utimens(const timespec times[2]) { throw FuseErrnoException(ENOTSUP); } -} /* namespace cryfs */ +void CryNode::remove() { + _parent->RemoveChild(_key); + _device->RemoveBlob(_key); +} + +CryDevice *CryNode::device() { + return _device; +} + +const CryDevice *CryNode::device() const { + return _device; +} + +unique_ptr CryNode::LoadBlob() const { + return _device->LoadBlob(_key); +} + +} diff --git a/src/CryNode.h b/src/CryNode.h index c31e67df..c0bf1aa8 100644 --- a/src/CryNode.h +++ b/src/CryNode.h @@ -11,15 +11,24 @@ namespace cryfs { class CryNode: public virtual fspp::Node { public: + CryNode(CryDevice *device, std::unique_ptr parent, const blockstore::Key &key); void access(int mask) const override; void rename(const boost::filesystem::path &to) override; void utimens(const timespec times[2]) override; + void remove() override; protected: CryNode(); virtual ~CryNode(); + CryDevice *device(); + const CryDevice *device() const; + std::unique_ptr LoadBlob() const; + private: + CryDevice *_device; + std::unique_ptr _parent; + blockstore::Key _key; DISALLOW_COPY_AND_ASSIGN(CryNode); };