libcryfs/implementations/ondisk/OnDiskBlockStore.cpp

45 lines
1.3 KiB
C++
Raw Normal View History

#include "OnDiskBlock.h"
#include "OnDiskBlockStore.h"
2014-12-09 17:19:59 +01:00
using std::string;
using cpputils::Data;
using cpputils::unique_ref;
using boost::optional;
using boost::none;
2014-12-09 17:19:59 +01:00
namespace bf = boost::filesystem;
namespace blockstore {
namespace ondisk {
OnDiskBlockStore::OnDiskBlockStore(const boost::filesystem::path &rootdir)
: _rootdir(rootdir) {}
2015-10-15 04:49:31 +02:00
//TODO Do I have to lock tryCreate/remove and/or load? Or does ParallelAccessBlockStore take care of that?
optional<unique_ref<Block>> OnDiskBlockStore::tryCreate(const Key &key, Data data) {
//TODO Easier implementation? This is only so complicated because of the cast OnDiskBlock -> Block
auto result = std::move(OnDiskBlock::CreateOnDisk(_rootdir, key, std::move(data)));
if (result == boost::none) {
return boost::none;
2014-12-09 17:19:59 +01:00
}
return unique_ref<Block>(std::move(*result));
2014-12-09 17:19:59 +01:00
}
optional<unique_ref<Block>> OnDiskBlockStore::load(const Key &key) {
return optional<unique_ref<Block>>(OnDiskBlock::LoadFromDisk(_rootdir, key));
2014-12-09 17:19:59 +01:00
}
void OnDiskBlockStore::remove(unique_ref<Block> block) {
Key key = block->key();
cpputils::destruct(std::move(block));
2015-02-22 00:29:21 +01:00
OnDiskBlock::RemoveFromDisk(_rootdir, key);
}
2015-02-23 21:07:07 +01:00
uint64_t OnDiskBlockStore::numBlocks() const {
return std::distance(bf::directory_iterator(_rootdir), bf::directory_iterator());
}
2014-12-09 17:19:59 +01:00
}
}