libcryfs/CryDevice.cpp

82 lines
2.2 KiB
C++
Raw Normal View History

#include "CryDevice.h"
#include "CryDir.h"
#include "CryFile.h"
2015-02-17 01:02:15 +01:00
#include "messmer/fspp/fuse/FuseErrnoException.h"
2014-12-09 17:19:59 +01:00
#include "impl/DirBlock.h"
using std::unique_ptr;
using std::unique_ptr;
using std::make_unique;
using std::string;
//TODO Get rid of this in favor of exception hierarchy
using fspp::fuse::CHECK_RETVAL;
2014-12-07 08:57:23 +01:00
using fspp::fuse::FuseErrnoException;
2014-12-09 17:19:59 +01:00
using blockstore::BlockStore;
using blockstore::Key;
namespace cryfs {
2014-12-09 17:19:59 +01:00
CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore)
: _block_store(std::move(blockStore)), _root_key(GetOrCreateRootKey(config.get())) {
}
Key CryDevice::GetOrCreateRootKey(CryConfig *config) {
string root_key = config->RootBlock();
if (root_key == "") {
auto key = CreateRootBlockAndReturnKey();
config->SetRootBlock(key.ToString());
return key;
}
return Key::FromString(root_key);
}
Key CryDevice::CreateRootBlockAndReturnKey() {
2014-12-09 17:19:59 +01:00
auto rootBlock = _block_store->create(DIR_BLOCKSIZE);
DirBlock rootDir(std::move(rootBlock));
rootDir.InitializeEmptyDir();
return rootBlock->key();
}
CryDevice::~CryDevice() {
}
2014-11-16 00:05:28 +01:00
unique_ptr<fspp::Node> CryDevice::Load(const bf::path &path) {
printf("Loading %s\n", path.c_str());
assert(path.is_absolute());
2014-12-09 11:28:52 +01:00
2014-12-09 17:19:59 +01:00
auto current_block = _block_store->load(_root_key);
2014-12-09 11:28:52 +01:00
for (const bf::path &component : path.relative_path()) {
2014-12-09 17:19:59 +01:00
if (!DirBlock::IsDir(*current_block)) {
2014-12-09 11:28:52 +01:00
throw FuseErrnoException(ENOTDIR);
}
2014-12-09 17:19:59 +01:00
unique_ptr<DirBlock> currentDir = make_unique<DirBlock>(std::move(current_block));
2014-12-09 11:28:52 +01:00
Key childKey = currentDir->GetBlockKeyForName(component.c_str());
2014-12-09 17:19:59 +01:00
current_block = _block_store->load(childKey);
2014-12-09 11:28:52 +01:00
}
2014-12-09 17:19:59 +01:00
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))));
2014-12-09 11:28:52 +01:00
} else {
throw FuseErrnoException(EIO);
}
}
void CryDevice::statfs(const bf::path &path, struct statvfs *fsstat) {
2014-12-07 08:57:23 +01:00
throw FuseErrnoException(ENOTSUP);
}
unique_ptr<blockstore::Block> CryDevice::CreateBlock(size_t size) {
2014-12-09 17:19:59 +01:00
return _block_store->create(size);
}
2014-12-07 08:57:23 +01:00
}