CryFS uses BlobStore instead of BlockStore as a base

This commit is contained in:
Sebastian Messmer 2015-02-27 00:51:51 +01:00
parent 7580ac9e80
commit 784e9e050a
16 changed files with 270 additions and 286 deletions

View File

@ -11,7 +11,7 @@ using boost::property_tree::ptree;
namespace cryfs {
CryConfig::CryConfig(const bf::path &configfile)
:_configfile(configfile), _root_block("") {
:_configfile(configfile), _rootBlob("") {
if (bf::exists(_configfile)) {
load();
}
@ -21,23 +21,23 @@ void CryConfig::load() {
ptree pt;
read_json(_configfile.native(), pt);
_root_block = pt.get("cryfs.rootblock", "");
_rootBlob = pt.get("cryfs.rootblob", "");
}
void CryConfig::save() const {
ptree pt;
pt.put("cryfs.rootblock", _root_block);
pt.put("cryfs.rootblob", _rootBlob);
write_json(_configfile.native(), pt);
}
const std::string &CryConfig::RootBlock() const {
return _root_block;
const std::string &CryConfig::RootBlob() const {
return _rootBlob;
}
void CryConfig::SetRootBlock(const std::string &value) {
_root_block = value;
void CryConfig::SetRootBlob(const std::string &value) {
_rootBlob = value;
}
CryConfig::~CryConfig() {

View File

@ -13,8 +13,8 @@ public:
CryConfig(const boost::filesystem::path &configfile);
virtual ~CryConfig();
const std::string &RootBlock() const;
void SetRootBlock(const std::string &value);
const std::string &RootBlob() const;
void SetRootBlob(const std::string &value);
private:
boost::filesystem::path _configfile;
@ -22,7 +22,7 @@ private:
void load();
void save() const;
std::string _root_block;
std::string _rootBlob;
DISALLOW_COPY_AND_ASSIGN(CryConfig);
};

View File

@ -1,10 +1,12 @@
#include <messmer/cryfs/impl/DirBlob.h>
#include "CryDevice.h"
#include "CryDir.h"
#include "CryFile.h"
#include "messmer/fspp/fuse/FuseErrnoException.h"
#include "impl/DirBlock.h"
#include "messmer/blobstore/implementations/onblocks/BlobStoreOnBlocks.h"
#include "messmer/blobstore/implementations/onblocks/BlobOnBlocks.h"
using std::unique_ptr;
@ -18,29 +20,32 @@ using fspp::fuse::FuseErrnoException;
using blockstore::BlockStore;
using blockstore::Key;
using blobstore::onblocks::BlobStoreOnBlocks;
using blobstore::onblocks::BlobOnBlocks;
namespace cryfs {
CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore)
: _block_store(std::move(blockStore)), _root_key(GetOrCreateRootKey(config.get())) {
: _blobStore(make_unique<BlobStoreOnBlocks>(std::move(blockStore))), _rootKey(GetOrCreateRootKey(config.get())) {
}
Key CryDevice::GetOrCreateRootKey(CryConfig *config) {
string root_key = config->RootBlock();
string root_key = config->RootBlob();
if (root_key == "") {
auto key = CreateRootBlockAndReturnKey();
config->SetRootBlock(key.ToString());
auto key = CreateRootBlobAndReturnKey();
config->SetRootBlob(key.ToString());
return key;
}
return Key::FromString(root_key);
}
Key CryDevice::CreateRootBlockAndReturnKey() {
auto rootBlock = _block_store->create(DIR_BLOCKSIZE);
DirBlock rootDir(std::move(rootBlock));
Key CryDevice::CreateRootBlobAndReturnKey() {
auto rootBlob = _blobStore->create();
Key rootBlobKey = rootBlob->key();
DirBlob rootDir(std::move(rootBlob));
rootDir.InitializeEmptyDir();
return rootBlock->key();
return rootBlobKey;
}
CryDevice::~CryDevice() {
@ -50,21 +55,21 @@ unique_ptr<fspp::Node> CryDevice::Load(const bf::path &path) {
printf("Loading %s\n", path.c_str());
assert(path.is_absolute());
auto current_block = _block_store->load(_root_key);
auto currentBlob = _blobStore->load(_rootKey);
for (const bf::path &component : path.relative_path()) {
if (!DirBlock::IsDir(*current_block)) {
if (!DirBlob::IsDir(*currentBlob)) {
throw FuseErrnoException(ENOTDIR);
}
unique_ptr<DirBlock> currentDir = make_unique<DirBlock>(std::move(current_block));
unique_ptr<DirBlob> currentDir = make_unique<DirBlob>(std::move(currentBlob));
Key childKey = currentDir->GetBlockKeyForName(component.c_str());
current_block = _block_store->load(childKey);
Key childKey = currentDir->GetBlobKeyForName(component.c_str());
currentBlob = _blobStore->load(childKey);
}
if (DirBlock::IsDir(*current_block)) {
return make_unique<CryDir>(this, std::move(make_unique<DirBlock>(std::move(current_block))));
} else if (FileBlock::IsFile(*current_block)) {
return make_unique<CryFile>(std::move(make_unique<FileBlock>(std::move(current_block))));
if (DirBlob::IsDir(*currentBlob)) {
return make_unique<CryDir>(this, std::move(make_unique<DirBlob>(std::move(currentBlob))));
} else if (FileBlob::IsFile(*currentBlob)) {
return make_unique<CryFile>(std::move(make_unique<FileBlob>(std::move(currentBlob))));
} else {
throw FuseErrnoException(EIO);
}
@ -74,8 +79,8 @@ void CryDevice::statfs(const bf::path &path, struct statvfs *fsstat) {
throw FuseErrnoException(ENOTSUP);
}
unique_ptr<blockstore::Block> CryDevice::CreateBlock(size_t size) {
return _block_store->create(size);
unique_ptr<blobstore::Blob> CryDevice::CreateBlob() {
return _blobStore->create();
}
}

View File

@ -3,6 +3,7 @@
#define CRYFS_LIB_CRYDEVICE_H_
#include <messmer/blockstore/interface/BlockStore.h>
#include <messmer/blobstore/interface/BlobStore.h>
#include "CryConfig.h"
#include <boost/filesystem.hpp>
@ -16,23 +17,21 @@ namespace bf = boost::filesystem;
class CryDevice: public fspp::Device {
public:
static constexpr size_t DIR_BLOCKSIZE = 4096;
CryDevice(std::unique_ptr<CryConfig> config, std::unique_ptr<blockstore::BlockStore> blockStore);
virtual ~CryDevice();
void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override;
std::unique_ptr<blockstore::Block> CreateBlock(size_t size);
std::unique_ptr<blobstore::Blob> CreateBlob();
private:
blockstore::Key GetOrCreateRootKey(CryConfig *config);
blockstore::Key CreateRootBlockAndReturnKey();
blockstore::Key CreateRootBlobAndReturnKey();
std::unique_ptr<fspp::Node> Load(const bf::path &path) override;
std::unique_ptr<blockstore::BlockStore> _block_store;
std::unique_ptr<blobstore::BlobStore> _blobStore;
blockstore::Key _root_key;
blockstore::Key _rootKey;
DISALLOW_COPY_AND_ASSIGN(CryDevice);
};

View File

@ -22,29 +22,29 @@ using std::vector;
namespace cryfs {
CryDir::CryDir(CryDevice *device, unique_ptr<DirBlock> block)
: _device(device), _block(std::move(block)) {
CryDir::CryDir(CryDevice *device, unique_ptr<DirBlob> blob)
: _device(device), _blob(std::move(blob)) {
}
CryDir::~CryDir() {
}
unique_ptr<fspp::File> CryDir::createFile(const string &name, mode_t mode) {
auto child = _device->CreateBlock(0);
_block->AddChild(name, child->key());
//TODO Di we need a return value in createDir for fspp? If not, change fspp!
auto fileblock = make_unique<FileBlock>(std::move(child));
fileblock->InitializeEmptyFile();
return make_unique<CryFile>(std::move(fileblock));
auto child = _device->CreateBlob();
_blob->AddChild(name, child->key());
//TODO Do we need a return value in createDir for fspp? If not, change fspp!
auto fileblob = make_unique<FileBlob>(std::move(child));
fileblob->InitializeEmptyFile();
return make_unique<CryFile>(std::move(fileblob));
}
unique_ptr<fspp::Dir> CryDir::createDir(const string &name, mode_t mode) {
auto child = _device->CreateBlock(CryDevice::DIR_BLOCKSIZE);
_block->AddChild(name, child->key());
auto child = _device->CreateBlob();
_blob->AddChild(name, child->key());
//TODO I don't think we need a return value in createDir for fspp. Change fspp!
auto dirblock = make_unique<DirBlock>(std::move(child));
dirblock->InitializeEmptyDir();
return make_unique<CryDir>(_device, std::move(dirblock));
auto dirblob = make_unique<DirBlob>(std::move(child));
dirblob->InitializeEmptyDir();
return make_unique<CryDir>(_device, std::move(dirblob));
}
void CryDir::rmdir() {
@ -52,7 +52,7 @@ void CryDir::rmdir() {
}
unique_ptr<vector<string>> CryDir::children() const {
return _block->GetChildren();
return _blob->GetChildren();
}
}

View File

@ -4,13 +4,13 @@
#include <messmer/fspp/fs_interface/Dir.h>
#include "CryNode.h"
#include "impl/DirBlock.h"
#include "impl/DirBlob.h"
namespace cryfs {
class CryDir: public fspp::Dir, CryNode {
public:
CryDir(CryDevice *device, std::unique_ptr<DirBlock> block);
CryDir(CryDevice *device, std::unique_ptr<DirBlob> blob);
virtual ~CryDir();
//TODO return type variance to CryFile/CryDir?
@ -22,7 +22,7 @@ public:
private:
CryDevice *_device;
std::unique_ptr<DirBlock> _block;
std::unique_ptr<DirBlob> _blob;
DISALLOW_COPY_AND_ASSIGN(CryDir);
};

View File

@ -15,8 +15,8 @@ using std::make_unique;
namespace cryfs {
CryFile::CryFile(unique_ptr<FileBlock> block)
: _block(std::move(block)) {
CryFile::CryFile(unique_ptr<FileBlob> blob)
: _blob(std::move(blob)) {
}
CryFile::~CryFile() {

View File

@ -2,16 +2,16 @@
#ifndef CRYFS_LIB_CRYFILE_H_
#define CRYFS_LIB_CRYFILE_H_
#include <messmer/cryfs/impl/FileBlob.h>
#include <messmer/fspp/fs_interface/File.h>
#include "CryNode.h"
#include "impl/FileBlock.h"
namespace cryfs {
class CryFile: public fspp::File, CryNode {
public:
CryFile(std::unique_ptr<FileBlock> block);
CryFile(std::unique_ptr<FileBlob> blob);
virtual ~CryFile();
std::unique_ptr<fspp::OpenFile> open(int flags) const override;
@ -19,7 +19,7 @@ public:
void unlink() override;
private:
std::unique_ptr<FileBlock> _block;
std::unique_ptr<FileBlob> _blob;
DISALLOW_COPY_AND_ASSIGN(CryFile);
};

104
impl/DirBlob.cpp Normal file
View File

@ -0,0 +1,104 @@
#include "DirBlob.h"
#include <cassert>
//TODO Remove and replace with exception hierarchy
#include "messmer/fspp/fuse/FuseErrnoException.h"
#include <messmer/blockstore/utils/Data.h>
#include "MagicNumbers.h"
using std::unique_ptr;
using std::make_unique;
using std::vector;
using std::string;
using blobstore::Blob;
using blockstore::Key;
using blockstore::Data;
//TODO Refactor: Keep a parsed dir structure (list of entries and blob keys they're pointing to) in memory and serialize/deserialize it
namespace cryfs {
DirBlob::DirBlob(unique_ptr<Blob> blob)
: _blob(std::move(blob)) {
}
DirBlob::~DirBlob() {
}
void DirBlob::InitializeEmptyDir() {
_blob->resize(1);
unsigned char magicNumber = MagicNumbers::DIR;
_blob->write(&magicNumber, 0, 1);
}
unsigned char DirBlob::magicNumber() const {
return magicNumber(*_blob);
}
const unsigned char DirBlob::magicNumber(const blobstore::Blob &blob) {
unsigned char number;
blob.read(&number, 0, 1);
return number;
}
bool DirBlob::IsDir(const blobstore::Blob &blob) {
return magicNumber(blob) == MagicNumbers::DIR;
}
unique_ptr<vector<string>> DirBlob::GetChildren() const {
Data entries(_blob->size()-1);
_blob->read(entries.data(), 1, _blob->size()-1);
auto result = make_unique<vector<string>>();
const char *pos = (const char*)entries.data();
while(pos < (const char*)entries.data()+entries.size()) {
pos = readAndAddNextChild(pos, result.get());
}
return result;
}
const char *DirBlob::readAndAddNextChild(const char *pos, vector<string> *result) const {
size_t length = strlen(pos);
result->emplace_back(pos, length);
const char *posAfterName = pos + length + 1;
const char *posAfterKey = posAfterName + strlen(posAfterName) + 1;
return posAfterKey;
}
void DirBlob::AddChild(const std::string &name, const Key &blobKey) {
//TODO blob.resize(blob.size()+X) has to traverse tree twice. Better would be blob.addSize(X) which returns old size
uint64_t oldBlobSize = _blob->size();
string blobKeyStr = blobKey.ToString();
_blob->resize(oldBlobSize + name.size() + 1 + blobKeyStr.size() + 1);
//Write entry name inclusive null terminator
_blob->write(name.c_str(), oldBlobSize, name.size()+1);
//Write blob key inclusive null terminator
_blob->write(blobKeyStr.c_str(), oldBlobSize + name.size() + 1, blobKeyStr.size()+1);
}
Key DirBlob::GetBlobKeyForName(const string &name) const {
auto result = make_unique<vector<string>>();
Data entries(_blob->size()-1);
_blob->read(entries.data(), 1, _blob->size()-1);
const char *pos = (const char*)entries.data();
while(pos < (const char*)entries.data()+entries.size()) {
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 Key::FromString(pos); // Return key
}
pos += strlen(pos) + 1; // Skip name
pos += strlen(pos) + 1; // Skip key
}
throw fspp::fuse::FuseErrnoException(ENOENT);
}
}

39
impl/DirBlob.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#ifndef CRYFS_LIB_IMPL_DIRBLOB_H_
#define CRYFS_LIB_IMPL_DIRBLOB_H_
#include <messmer/blobstore/interface/Blob.h>
#include <messmer/blockstore/utils/Key.h>
#include "messmer/cpp-utils/macros.h"
#include <memory>
#include <vector>
namespace cryfs{
class DirBlob {
public:
DirBlob(std::unique_ptr<blobstore::Blob> blob);
virtual ~DirBlob();
void InitializeEmptyDir();
std::unique_ptr<std::vector<std::string>> GetChildren() const;
void AddChild(const std::string &name, const blockstore::Key &blobKey);
blockstore::Key GetBlobKeyForName(const std::string &name) const;
static bool IsDir(const blobstore::Blob &blob);
private:
unsigned char magicNumber() const;
static const unsigned char magicNumber(const blobstore::Blob &blob);
const char *readAndAddNextChild(const char *pos, std::vector<std::string> *result) const;
std::unique_ptr<blobstore::Blob> _blob;
DISALLOW_COPY_AND_ASSIGN(DirBlob);
};
}
#endif

View File

@ -1,122 +0,0 @@
#include "DirBlock.h"
#include <cassert>
//TODO Remove and replace with exception hierarchy
#include "messmer/fspp/fuse/FuseErrnoException.h"
#include "MagicNumbers.h"
using std::unique_ptr;
using std::make_unique;
using std::vector;
using std::string;
using blockstore::Block;
using blockstore::Key;
namespace cryfs {
DirBlock::DirBlock(unique_ptr<Block> block)
: _block(std::move(block)) {
}
DirBlock::~DirBlock() {
}
void DirBlock::InitializeEmptyDir() {
*magicNumber() = MagicNumbers::DIR;
*entryCounter() = 0;
}
unsigned char *DirBlock::magicNumber() {
return const_cast<unsigned char*>(magicNumber(const_cast<const Block&>(*_block)));
}
const unsigned char *DirBlock::magicNumber(const blockstore::Block &block) {
return (unsigned char*)block.data();
}
bool DirBlock::IsDir(const blockstore::Block &block) {
return *magicNumber(block) == MagicNumbers::DIR;
}
unique_ptr<vector<string>> DirBlock::GetChildren() const {
auto result = make_unique<vector<string>>();
unsigned int entryCount = *entryCounter();
result->reserve(entryCount);
const char *pos = entriesBegin();
for (unsigned int i = 0; i < entryCount; ++i) {
pos = readAndAddNextChild(pos, result.get());
}
return result;
}
const char *DirBlock::readAndAddNextChild(const char *pos, vector<string> *result) const {
size_t length = strlen(pos);
result->emplace_back(pos, length);
const char *posAfterName = pos + length + 1;
const char *posAfterKey = posAfterName + strlen(posAfterName) + 1;
return posAfterKey;
}
void DirBlock::AddChild(const std::string &name, const Key &blockKey) {
string blockKeyStr = blockKey.ToString();
char *insertPos = entriesEnd();
assertEnoughSpaceLeft(insertPos, name.size() + 1 + blockKeyStr.size() + 1);
memcpy(insertPos, name.c_str(), name.size()+1);
memcpy(insertPos + name.size()+1, blockKeyStr.c_str(), blockKeyStr.size()+1);
++(*entryCounter());
}
void DirBlock::assertEnoughSpaceLeft(char *insertPos, size_t insertSize) const {
size_t usedSize = insertPos - (char*)_block->data();
assert(usedSize + insertSize <= _block->size());
}
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 Key::FromString(pos); // Return key
}
pos += strlen(pos) + 1; // Skip name
pos += strlen(pos) + 1; // Skip key
}
throw fspp::fuse::FuseErrnoException(ENOENT);
}
unsigned int *DirBlock::entryCounter() {
return const_cast<unsigned int*>(const_cast<const DirBlock*>(this)->entryCounter());
}
const unsigned int *DirBlock::entryCounter() const {
return (unsigned int*)((char*)_block->data() + sizeof(unsigned char));
}
char *DirBlock::entriesBegin() {
return const_cast<char*>(const_cast<const DirBlock*>(this)->entriesBegin());
}
const char *DirBlock::entriesBegin() const {
return (char *)(_block->data())+sizeof(unsigned char) + sizeof(unsigned int);
}
char *DirBlock::entriesEnd() {
unsigned int entryCount = *entryCounter();
char *pos = entriesBegin();
for(unsigned int i = 0; i < entryCount; ++i) {
pos += strlen(pos) + 1; // Skip entry name
pos += strlen(pos) + 1; // Skip entry key
}
return pos;
}
}

View File

@ -1,45 +0,0 @@
#pragma once
#ifndef CRYFS_LIB_IMPL_DIRBLOCK_H_
#define CRYFS_LIB_IMPL_DIRBLOCK_H_
#include <messmer/blockstore/interface/Block.h>
#include <messmer/blockstore/utils/Key.h>
#include "messmer/cpp-utils/macros.h"
#include <memory>
#include <vector>
namespace cryfs{
class DirBlock {
public:
DirBlock(std::unique_ptr<blockstore::Block> block);
virtual ~DirBlock();
void InitializeEmptyDir();
std::unique_ptr<std::vector<std::string>> GetChildren() 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);
private:
unsigned char *magicNumber();
static const unsigned char *magicNumber(const blockstore::Block &block);
unsigned int *entryCounter();
const unsigned int *entryCounter() const;
char *entriesBegin();
const char *entriesBegin() const;
char *entriesEnd();
const char *readAndAddNextChild(const char *pos, std::vector<std::string> *result) const;
void assertEnoughSpaceLeft(char *insertPos, size_t insertSize) const;
std::unique_ptr<blockstore::Block> _block;
DISALLOW_COPY_AND_ASSIGN(DirBlock);
};
}
#endif

37
impl/FileBlob.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "FileBlob.h"
#include "MagicNumbers.h"
using std::unique_ptr;
using blobstore::Blob;
namespace cryfs {
FileBlob::FileBlob(unique_ptr<Blob> blob)
: _blob(std::move(blob)) {
}
FileBlob::~FileBlob() {
}
void FileBlob::InitializeEmptyFile() {
_blob->resize(1);
unsigned char magicNumber = MagicNumbers::FILE;
_blob->write(&magicNumber, 0, 1);
}
unsigned char FileBlob::magicNumber() const {
return magicNumber(*_blob);
}
unsigned char FileBlob::magicNumber(const blobstore::Blob &blob) {
unsigned char value;
blob.read(&value, 0, 1);
return value;
}
bool FileBlob::IsFile(const Blob &blob) {
return magicNumber(blob) == MagicNumbers::FILE;
}
}

28
impl/FileBlob.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#ifndef CRYFS_LIB_IMPL_FILEBLOB_H_
#define CRYFS_LIB_IMPL_FILEBLOB_H_
#include <messmer/blobstore/interface/Blob.h>
#include <memory>
namespace cryfs {
class FileBlob {
public:
FileBlob(std::unique_ptr<blobstore::Blob> blob);
virtual ~FileBlob();
static bool IsFile(const blobstore::Blob &blob);
void InitializeEmptyFile();
private:
std::unique_ptr<blobstore::Blob> _blob;
unsigned char magicNumber() const;
static unsigned char magicNumber(const blobstore::Blob &blob);
};
}
#endif

View File

@ -1,33 +0,0 @@
#include "FileBlock.h"
#include "MagicNumbers.h"
using std::unique_ptr;
using blockstore::Block;
namespace cryfs {
FileBlock::FileBlock(unique_ptr<Block> block)
: _block(std::move(block)) {
}
FileBlock::~FileBlock() {
}
void FileBlock::InitializeEmptyFile() {
*magicNumber() = MagicNumbers::FILE;
}
unsigned char *FileBlock::magicNumber() {
return const_cast<unsigned char*>(magicNumber(const_cast<const Block&>(*_block)));
}
const unsigned char *FileBlock::magicNumber(const blockstore::Block &block) {
return (unsigned char*)block.data();
}
bool FileBlock::IsFile(const Block &block) {
return *magicNumber(block) == MagicNumbers::FILE;
}
}

View File

@ -1,28 +0,0 @@
#pragma once
#ifndef CRYFS_LIB_IMPL_FILEBLOCK_H_
#define CRYFS_LIB_IMPL_FILEBLOCK_H_
#include <messmer/blockstore/interface/Block.h>
#include <memory>
namespace cryfs {
class FileBlock {
public:
FileBlock(std::unique_ptr<blockstore::Block> block);
virtual ~FileBlock();
static bool IsFile(const blockstore::Block &block);
void InitializeEmptyFile();
private:
std::unique_ptr<blockstore::Block> _block;
unsigned char *magicNumber();
static const unsigned char *magicNumber(const blockstore::Block &block);
};
}
#endif