Use using for shorter code

This commit is contained in:
Sebastian Messmer 2017-07-13 02:13:13 -05:00
parent 534968484c
commit 872c94865c
2 changed files with 21 additions and 17 deletions

View File

@ -25,8 +25,8 @@ Key LowToHighLevelBlockStore::createKey() {
optional<unique_ref<Block>> LowToHighLevelBlockStore::tryCreate(const Key &key, Data data) { optional<unique_ref<Block>> LowToHighLevelBlockStore::tryCreate(const Key &key, Data data) {
//TODO Easier implementation? This is only so complicated because of the cast LowToHighLevelBlock -> Block //TODO Easier implementation? This is only so complicated because of the cast LowToHighLevelBlock -> Block
auto result = LowToHighLevelBlock::TryCreateNew(_baseBlockStore.get(), key, std::move(data)); auto result = LowToHighLevelBlock::TryCreateNew(_baseBlockStore.get(), key, std::move(data));
if (result == boost::none) { if (result == none) {
return boost::none; return none;
} }
return unique_ref<Block>(std::move(*result)); return unique_ref<Block>(std::move(*result));
} }
@ -39,8 +39,8 @@ unique_ref<Block> LowToHighLevelBlockStore::overwrite(const Key &key, Data data)
optional<unique_ref<Block>> LowToHighLevelBlockStore::load(const Key &key) { optional<unique_ref<Block>> LowToHighLevelBlockStore::load(const Key &key) {
auto result = optional<unique_ref<Block>>(LowToHighLevelBlock::Load(_baseBlockStore.get(), key)); auto result = optional<unique_ref<Block>>(LowToHighLevelBlock::Load(_baseBlockStore.get(), key));
if (result == boost::none) { if (result == none) {
return boost::none; return none;
} }
return unique_ref<Block>(std::move(*result)); return unique_ref<Block>(std::move(*result));
} }

View File

@ -1,6 +1,10 @@
#include "OnDiskBlockStore2.h" #include "OnDiskBlockStore2.h"
using std::string; using std::string;
using boost::future;
using boost::optional;
using boost::none;
using cpputils::Data;
namespace blockstore { namespace blockstore {
namespace ondisk { namespace ondisk {
@ -13,7 +17,7 @@ boost::filesystem::path OnDiskBlockStore2::_getFilepath(const Key &key) const {
return _rootDir / keyStr.substr(0,3) / keyStr.substr(3); return _rootDir / keyStr.substr(0,3) / keyStr.substr(3);
} }
cpputils::Data OnDiskBlockStore2::_checkAndRemoveHeader(const cpputils::Data &data) { Data OnDiskBlockStore2::_checkAndRemoveHeader(const Data &data) {
if (!_isAcceptedCryfsHeader(data)) { if (!_isAcceptedCryfsHeader(data)) {
if (_isOtherCryfsHeader(data)) { if (_isOtherCryfsHeader(data)) {
throw std::runtime_error("This block is not supported yet. Maybe it was created with a newer version of CryFS?"); throw std::runtime_error("This block is not supported yet. Maybe it was created with a newer version of CryFS?");
@ -21,16 +25,16 @@ cpputils::Data OnDiskBlockStore2::_checkAndRemoveHeader(const cpputils::Data &da
throw std::runtime_error("This is not a valid block."); throw std::runtime_error("This is not a valid block.");
} }
} }
cpputils::Data result(data.size() - formatVersionHeaderSize()); Data result(data.size() - formatVersionHeaderSize());
std::memcpy(result.data(), data.dataOffset(formatVersionHeaderSize()), result.size()); std::memcpy(result.data(), data.dataOffset(formatVersionHeaderSize()), result.size());
return result; return result;
} }
bool OnDiskBlockStore2::_isAcceptedCryfsHeader(const cpputils::Data &data) { bool OnDiskBlockStore2::_isAcceptedCryfsHeader(const Data &data) {
return 0 == std::memcmp(data.data(), FORMAT_VERSION_HEADER.c_str(), formatVersionHeaderSize()); return 0 == std::memcmp(data.data(), FORMAT_VERSION_HEADER.c_str(), formatVersionHeaderSize());
} }
bool OnDiskBlockStore2::_isOtherCryfsHeader(const cpputils::Data &data) { bool OnDiskBlockStore2::_isOtherCryfsHeader(const Data &data) {
return 0 == std::memcmp(data.data(), FORMAT_VERSION_HEADER_PREFIX.c_str(), FORMAT_VERSION_HEADER_PREFIX.size()); return 0 == std::memcmp(data.data(), FORMAT_VERSION_HEADER_PREFIX.c_str(), FORMAT_VERSION_HEADER_PREFIX.size());
} }
@ -41,7 +45,7 @@ unsigned int OnDiskBlockStore2::formatVersionHeaderSize() {
OnDiskBlockStore2::OnDiskBlockStore2(const boost::filesystem::path& path) OnDiskBlockStore2::OnDiskBlockStore2(const boost::filesystem::path& path)
: _rootDir(path) {} : _rootDir(path) {}
boost::future<bool> OnDiskBlockStore2::tryCreate(const Key &key, const cpputils::Data &data) { future<bool> OnDiskBlockStore2::tryCreate(const Key &key, const Data &data) {
auto filepath = _getFilepath(key); auto filepath = _getFilepath(key);
if (boost::filesystem::exists(filepath)) { if (boost::filesystem::exists(filepath)) {
return boost::make_ready_future(false); return boost::make_ready_future(false);
@ -51,7 +55,7 @@ boost::future<bool> OnDiskBlockStore2::tryCreate(const Key &key, const cpputils:
return boost::make_ready_future(true); return boost::make_ready_future(true);
} }
boost::future<bool> OnDiskBlockStore2::remove(const Key &key) { future<bool> OnDiskBlockStore2::remove(const Key &key) {
auto filepath = _getFilepath(key); auto filepath = _getFilepath(key);
if (!boost::filesystem::is_regular_file(filepath)) { // TODO Is this branch necessary? if (!boost::filesystem::is_regular_file(filepath)) { // TODO Is this branch necessary?
return boost::make_ready_future(false); return boost::make_ready_future(false);
@ -67,16 +71,16 @@ boost::future<bool> OnDiskBlockStore2::remove(const Key &key) {
return boost::make_ready_future(true); return boost::make_ready_future(true);
} }
boost::future<boost::optional<cpputils::Data>> OnDiskBlockStore2::load(const Key &key) const { future<optional<Data>> OnDiskBlockStore2::load(const Key &key) const {
auto fileContent = cpputils::Data::LoadFromFile(_getFilepath(key)); auto fileContent = Data::LoadFromFile(_getFilepath(key));
if (fileContent == boost::none) { if (fileContent == none) {
return boost::make_ready_future(boost::optional<cpputils::Data>(boost::none)); return boost::make_ready_future(optional<Data>(none));
} }
return boost::make_ready_future(boost::optional<cpputils::Data>(_checkAndRemoveHeader(std::move(*fileContent)))); return boost::make_ready_future(optional<Data>(_checkAndRemoveHeader(std::move(*fileContent))));
} }
boost::future<void> OnDiskBlockStore2::store(const Key &key, const cpputils::Data &data) { future<void> OnDiskBlockStore2::store(const Key &key, const Data &data) {
cpputils::Data fileContent(formatVersionHeaderSize() + data.size()); Data fileContent(formatVersionHeaderSize() + data.size());
std::memcpy(fileContent.data(), FORMAT_VERSION_HEADER.c_str(), formatVersionHeaderSize()); std::memcpy(fileContent.data(), FORMAT_VERSION_HEADER.c_str(), formatVersionHeaderSize());
std::memcpy(fileContent.dataOffset(formatVersionHeaderSize()), data.data(), data.size()); std::memcpy(fileContent.dataOffset(formatVersionHeaderSize()), data.data(), data.size());
auto filepath = _getFilepath(key); auto filepath = _getFilepath(key);