From 83fad1ca53b4a354298bd0b9ef90ad074de53d69 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Tue, 9 Dec 2014 20:36:32 +0100 Subject: [PATCH] Create dedicated Key class for addressing blocks --- src/cryfs_lib/CryDevice.cpp | 22 +++++++++++++++------- src/cryfs_lib/CryDevice.h | 7 ++++--- src/cryfs_lib/impl/DirBlock.cpp | 13 ++++++++----- src/cryfs_lib/impl/DirBlock.h | 5 +++-- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/cryfs_lib/CryDevice.cpp b/src/cryfs_lib/CryDevice.cpp index b7a7efae..9cbe2ee7 100644 --- a/src/cryfs_lib/CryDevice.cpp +++ b/src/cryfs_lib/CryDevice.cpp @@ -17,18 +17,26 @@ using fspp::fuse::CHECK_RETVAL; using fspp::fuse::FuseErrnoException; using blockstore::BlockStore; +using blockstore::Key; namespace cryfs { CryDevice::CryDevice(unique_ptr config, unique_ptr blockStore) -: _block_store(std::move(blockStore)), _root_key(config->RootBlock()) { - if (_root_key == "") { - _root_key = CreateRootBlockAndReturnKey(); - config->SetRootBlock(_root_key); - } +: _block_store(std::move(blockStore)), _root_key(GetOrCreateRootKey(config.get())) { } -string CryDevice::CreateRootBlockAndReturnKey() { +Key CryDevice::GetOrCreateRootKey(CryConfig *config) { + string root_key = config->RootBlock(); + if (root_key == "") { + auto key = CreateRootBlockAndReturnKey(); + config->SetRootBlock(key.AsString()); + return key; + } + + return Key::FromString(root_key); +} + +Key CryDevice::CreateRootBlockAndReturnKey() { auto rootBlock = _block_store->create(DIR_BLOCKSIZE); DirBlock rootDir(std::move(rootBlock.block)); rootDir.InitializeEmptyDir(); @@ -50,7 +58,7 @@ unique_ptr CryDevice::Load(const bf::path &path) { } unique_ptr currentDir = make_unique(std::move(current_block)); - string childKey = currentDir->GetBlockKeyForName(component.c_str()); + Key childKey = currentDir->GetBlockKeyForName(component.c_str()); current_block = _block_store->load(childKey); } if (DirBlock::IsDir(*current_block)) { diff --git a/src/cryfs_lib/CryDevice.h b/src/cryfs_lib/CryDevice.h index 9d241d77..a20e4637 100644 --- a/src/cryfs_lib/CryDevice.h +++ b/src/cryfs_lib/CryDevice.h @@ -18,7 +18,7 @@ class CryDevice: public fspp::Device { public: static constexpr size_t DIR_BLOCKSIZE = 4096; - CryDevice(std::unique_ptr config, std::unique_ptr blockbStore); + CryDevice(std::unique_ptr config, std::unique_ptr blockStore); virtual ~CryDevice(); void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override; @@ -26,12 +26,13 @@ public: blockstore::BlockWithKey CreateBlock(size_t size); private: - std::string CreateRootBlockAndReturnKey(); + blockstore::Key GetOrCreateRootKey(CryConfig *config); + blockstore::Key CreateRootBlockAndReturnKey(); std::unique_ptr Load(const bf::path &path) override; std::unique_ptr _block_store; - std::string _root_key; + blockstore::Key _root_key; DISALLOW_COPY_AND_ASSIGN(CryDevice); }; diff --git a/src/cryfs_lib/impl/DirBlock.cpp b/src/cryfs_lib/impl/DirBlock.cpp index 6e2337fe..0c2c7891 100644 --- a/src/cryfs_lib/impl/DirBlock.cpp +++ b/src/cryfs_lib/impl/DirBlock.cpp @@ -13,6 +13,7 @@ using std::vector; using std::string; using blockstore::Block; +using blockstore::Key; namespace cryfs { @@ -61,12 +62,14 @@ const char *DirBlock::readAndAddNextChild(const char *pos, vector *resul return posAfterKey; } -void DirBlock::AddChild(const std::string &name, const std::string &blockKey) { +void DirBlock::AddChild(const std::string &name, const Key &blockKey) { + string blockKeyStr = blockKey.AsString(); + char *insertPos = entriesEnd(); - assertEnoughSpaceLeft(insertPos, name.size() + 1 + blockKey.size() + 1); + assertEnoughSpaceLeft(insertPos, name.size() + 1 + blockKeyStr.size() + 1); memcpy(insertPos, name.c_str(), name.size()+1); - memcpy(insertPos + name.size()+1, blockKey.c_str(), blockKey.size()+1); + memcpy(insertPos + name.size()+1, blockKeyStr.c_str(), blockKeyStr.size()+1); ++(*entryCounter()); } @@ -75,14 +78,14 @@ void DirBlock::assertEnoughSpaceLeft(char *insertPos, size_t insertSize) const { assert(usedSize + insertSize <= _block->size()); } -string DirBlock::GetBlockKeyForName(const string &name) const { +Key DirBlock::GetBlockKeyForName(const string &name) const { unsigned int entryCount = *entryCounter(); const char *pos = entriesBegin(); for(unsigned int i = 0; i < entryCount; ++i) { size_t name_length = strlen(pos); if (name_length == name.size() && 0==std::memcmp(pos, name.c_str(), name_length)) { pos += strlen(pos) + 1; // Skip name - return pos; // Return key + return Key::FromString(pos); // Return key } pos += strlen(pos) + 1; // Skip name pos += strlen(pos) + 1; // Skip key diff --git a/src/cryfs_lib/impl/DirBlock.h b/src/cryfs_lib/impl/DirBlock.h index 2defb18d..a33e38d6 100644 --- a/src/cryfs_lib/impl/DirBlock.h +++ b/src/cryfs_lib/impl/DirBlock.h @@ -3,6 +3,7 @@ #define CRYFS_LIB_IMPL_DIRBLOCK_H_ #include +#include #include "fspp/utils/macros.h" #include @@ -17,8 +18,8 @@ public: void InitializeEmptyDir(); std::unique_ptr> GetChildren() const; - void AddChild(const std::string &name, const std::string &blockKey); - std::string GetBlockKeyForName(const std::string &name) const; + void AddChild(const std::string &name, const blockstore::Key &blockKey); + blockstore::Key GetBlockKeyForName(const std::string &name) const; static bool IsDir(const blockstore::Block &block);