libcryfs/CryDevice.cpp

89 lines
2.5 KiB
C++
Raw Normal View History

#include <messmer/cryfs/impl/DirBlob.h>
#include "CryDevice.h"
#include "CryDir.h"
#include "CryFile.h"
2015-02-17 01:02:15 +01:00
#include "messmer/fspp/fuse/FuseErrnoException.h"
#include "messmer/blobstore/implementations/onblocks/BlobStoreOnBlocks.h"
#include "messmer/blobstore/implementations/onblocks/BlobOnBlocks.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;
using blobstore::onblocks::BlobStoreOnBlocks;
using blobstore::onblocks::BlobOnBlocks;
namespace cryfs {
2015-03-06 02:06:31 +01:00
constexpr uint32_t CryDevice::BLOCKSIZE_BYTES;
2014-12-09 17:19:59 +01:00
CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore)
2015-03-06 02:06:31 +01:00
: _blobStore(make_unique<BlobStoreOnBlocks>(std::move(blockStore), BLOCKSIZE_BYTES)), _rootKey(GetOrCreateRootKey(config.get())) {
}
Key CryDevice::GetOrCreateRootKey(CryConfig *config) {
string root_key = config->RootBlob();
if (root_key == "") {
auto key = CreateRootBlobAndReturnKey();
config->SetRootBlob(key.ToString());
return key;
}
return Key::FromString(root_key);
}
Key CryDevice::CreateRootBlobAndReturnKey() {
auto rootBlob = _blobStore->create();
Key rootBlobKey = rootBlob->key();
DirBlob rootDir(std::move(rootBlob));
rootDir.InitializeEmptyDir();
return rootBlobKey;
}
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
auto currentBlob = _blobStore->load(_rootKey);
2014-12-09 11:28:52 +01:00
for (const bf::path &component : path.relative_path()) {
if (!DirBlob::IsDir(*currentBlob)) {
2014-12-09 11:28:52 +01:00
throw FuseErrnoException(ENOTDIR);
}
unique_ptr<DirBlob> currentDir = make_unique<DirBlob>(std::move(currentBlob));
2014-12-09 11:28:52 +01:00
Key childKey = currentDir->GetBlobKeyForName(component.c_str());
currentBlob = _blobStore->load(childKey);
2014-12-09 11:28:52 +01:00
}
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))));
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<blobstore::Blob> CryDevice::CreateBlob() {
return _blobStore->create();
}
2014-12-07 08:57:23 +01:00
}