Create dedicated Key class for addressing blocks

This commit is contained in:
Sebastian Messmer 2014-12-09 20:36:32 +01:00
parent e757da0ad3
commit 83fad1ca53
4 changed files with 30 additions and 17 deletions

View File

@ -17,18 +17,26 @@ using fspp::fuse::CHECK_RETVAL;
using fspp::fuse::FuseErrnoException; using fspp::fuse::FuseErrnoException;
using blockstore::BlockStore; using blockstore::BlockStore;
using blockstore::Key;
namespace cryfs { namespace cryfs {
CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore) CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore)
: _block_store(std::move(blockStore)), _root_key(config->RootBlock()) { : _block_store(std::move(blockStore)), _root_key(GetOrCreateRootKey(config.get())) {
if (_root_key == "") {
_root_key = CreateRootBlockAndReturnKey();
config->SetRootBlock(_root_key);
}
} }
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); auto rootBlock = _block_store->create(DIR_BLOCKSIZE);
DirBlock rootDir(std::move(rootBlock.block)); DirBlock rootDir(std::move(rootBlock.block));
rootDir.InitializeEmptyDir(); rootDir.InitializeEmptyDir();
@ -50,7 +58,7 @@ unique_ptr<fspp::Node> CryDevice::Load(const bf::path &path) {
} }
unique_ptr<DirBlock> currentDir = make_unique<DirBlock>(std::move(current_block)); unique_ptr<DirBlock> currentDir = make_unique<DirBlock>(std::move(current_block));
string childKey = currentDir->GetBlockKeyForName(component.c_str()); Key childKey = currentDir->GetBlockKeyForName(component.c_str());
current_block = _block_store->load(childKey); current_block = _block_store->load(childKey);
} }
if (DirBlock::IsDir(*current_block)) { if (DirBlock::IsDir(*current_block)) {

View File

@ -18,7 +18,7 @@ class CryDevice: public fspp::Device {
public: public:
static constexpr size_t DIR_BLOCKSIZE = 4096; static constexpr size_t DIR_BLOCKSIZE = 4096;
CryDevice(std::unique_ptr<CryConfig> config, std::unique_ptr<blockstore::BlockStore> blockbStore); CryDevice(std::unique_ptr<CryConfig> config, std::unique_ptr<blockstore::BlockStore> blockStore);
virtual ~CryDevice(); virtual ~CryDevice();
void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override; void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override;
@ -26,12 +26,13 @@ public:
blockstore::BlockWithKey CreateBlock(size_t size); blockstore::BlockWithKey CreateBlock(size_t size);
private: private:
std::string CreateRootBlockAndReturnKey(); blockstore::Key GetOrCreateRootKey(CryConfig *config);
blockstore::Key CreateRootBlockAndReturnKey();
std::unique_ptr<fspp::Node> Load(const bf::path &path) override; std::unique_ptr<fspp::Node> Load(const bf::path &path) override;
std::unique_ptr<blockstore::BlockStore> _block_store; std::unique_ptr<blockstore::BlockStore> _block_store;
std::string _root_key; blockstore::Key _root_key;
DISALLOW_COPY_AND_ASSIGN(CryDevice); DISALLOW_COPY_AND_ASSIGN(CryDevice);
}; };

View File

@ -13,6 +13,7 @@ using std::vector;
using std::string; using std::string;
using blockstore::Block; using blockstore::Block;
using blockstore::Key;
namespace cryfs { namespace cryfs {
@ -61,12 +62,14 @@ const char *DirBlock::readAndAddNextChild(const char *pos, vector<string> *resul
return posAfterKey; 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(); 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.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()); ++(*entryCounter());
} }
@ -75,14 +78,14 @@ void DirBlock::assertEnoughSpaceLeft(char *insertPos, size_t insertSize) const {
assert(usedSize + insertSize <= _block->size()); assert(usedSize + insertSize <= _block->size());
} }
string DirBlock::GetBlockKeyForName(const string &name) const { Key DirBlock::GetBlockKeyForName(const string &name) const {
unsigned int entryCount = *entryCounter(); unsigned int entryCount = *entryCounter();
const char *pos = entriesBegin(); const char *pos = entriesBegin();
for(unsigned int i = 0; i < entryCount; ++i) { for(unsigned int i = 0; i < entryCount; ++i) {
size_t name_length = strlen(pos); size_t name_length = strlen(pos);
if (name_length == name.size() && 0==std::memcmp(pos, name.c_str(), name_length)) { if (name_length == name.size() && 0==std::memcmp(pos, name.c_str(), name_length)) {
pos += strlen(pos) + 1; // Skip name 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 name
pos += strlen(pos) + 1; // Skip key pos += strlen(pos) + 1; // Skip key

View File

@ -3,6 +3,7 @@
#define CRYFS_LIB_IMPL_DIRBLOCK_H_ #define CRYFS_LIB_IMPL_DIRBLOCK_H_
#include <blockstore/interface/Block.h> #include <blockstore/interface/Block.h>
#include <blockstore/utils/Key.h>
#include "fspp/utils/macros.h" #include "fspp/utils/macros.h"
#include <memory> #include <memory>
@ -17,8 +18,8 @@ public:
void InitializeEmptyDir(); void InitializeEmptyDir();
std::unique_ptr<std::vector<std::string>> GetChildren() const; std::unique_ptr<std::vector<std::string>> GetChildren() const;
void AddChild(const std::string &name, const std::string &blockKey); void AddChild(const std::string &name, const blockstore::Key &blockKey);
std::string GetBlockKeyForName(const std::string &name) const; blockstore::Key GetBlockKeyForName(const std::string &name) const;
static bool IsDir(const blockstore::Block &block); static bool IsDir(const blockstore::Block &block);