More use of unique_ref instead of unique_ptr

This commit is contained in:
Sebastian Meßmer 2015-06-18 13:45:08 +02:00
parent f4d925aa9d
commit e883bc5747
10 changed files with 67 additions and 41 deletions

View File

@ -2,21 +2,23 @@
#include <boost/filesystem.hpp>
namespace bf = boost::filesystem;
using std::unique_ptr;
using std::make_unique;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using boost::optional;
using boost::none;
namespace cryfs {
unique_ptr<CryConfig> CryConfigLoader::loadOrCreate(const bf::path &filename) {
unique_ref<CryConfig> CryConfigLoader::loadOrCreate(const bf::path &filename) {
auto config = loadExisting(filename);
if (config.get() != nullptr) {
return config;
if (config != none) {
return std::move(*config);
}
return createNew(filename);
}
unique_ptr<CryConfig> CryConfigLoader::createNew(const bf::path &filename) {
auto config = make_unique<CryConfig>(filename);
unique_ref<CryConfig> CryConfigLoader::createNew(const bf::path &filename) {
auto config = make_unique_ref<CryConfig>(filename);
_initializeConfig(config.get());
config->save();
return config;
@ -51,23 +53,23 @@ void CryConfigLoader::_generateRootBlobKey(CryConfig *config) {
config->SetRootBlob("");
}
unique_ptr<CryConfig> CryConfigLoader::loadExisting(const bf::path &filename) {
optional<unique_ref<CryConfig>> CryConfigLoader::loadExisting(const bf::path &filename) {
if (bf::exists(filename)) {
return make_unique<CryConfig>(filename);
return make_unique_ref<CryConfig>(filename);
}
return nullptr;
return none;
}
unique_ptr<CryConfig> CryConfigLoader::loadOrCreateWithWeakKey(const bf::path &filename) {
unique_ref<CryConfig> CryConfigLoader::loadOrCreateWithWeakKey(const bf::path &filename) {
auto config = loadExisting(filename);
if (config.get() != nullptr) {
return config;
if (config != none) {
return std::move(*config);
}
return createNewWithWeakKey(filename);
}
unique_ptr<CryConfig> CryConfigLoader::createNewWithWeakKey(const bf::path &filename) {
auto config = make_unique<CryConfig>(filename);
unique_ref<CryConfig> CryConfigLoader::createNewWithWeakKey(const bf::path &filename) {
auto config = make_unique_ref<CryConfig>(filename);
_initializeConfigWithWeakKey(config.get());
config->save();
return config;

View File

@ -2,7 +2,7 @@
#ifndef MESSMER_CRYFS_SRC_CRYCONFIGLOADER_H_
#define MESSMER_CRYFS_SRC_CRYCONFIGLOADER_H_
#include <memory>
#include <messmer/cpp-utils/unique_ref.h>
#include <boost/filesystem/path.hpp>
#include "CryConfig.h"
#include <messmer/blockstore/implementations/encrypted/ciphers/AES256_GCM.h>
@ -13,14 +13,14 @@ class CryConfigLoader {
public:
using Cipher = blockstore::encrypted::AES256_GCM;
static std::unique_ptr<CryConfig> loadOrCreate(const boost::filesystem::path &filename);
static cpputils::unique_ref<CryConfig> loadOrCreate(const boost::filesystem::path &filename);
static std::unique_ptr<CryConfig> createNew(const boost::filesystem::path &filename);
static std::unique_ptr<CryConfig> loadExisting(const boost::filesystem::path &filename);
static cpputils::unique_ref<CryConfig> createNew(const boost::filesystem::path &filename);
static boost::optional<cpputils::unique_ref<CryConfig>> loadExisting(const boost::filesystem::path &filename);
//This method is only for testing purposes, because creating weak keys is much faster than creating strong keys.
static std::unique_ptr<CryConfig> loadOrCreateWithWeakKey(const boost::filesystem::path &filename);
static std::unique_ptr<CryConfig> createNewWithWeakKey(const boost::filesystem::path &filename);
static cpputils::unique_ref<CryConfig> loadOrCreateWithWeakKey(const boost::filesystem::path &filename);
static cpputils::unique_ref<CryConfig> createNewWithWeakKey(const boost::filesystem::path &filename);
private:
static void _initializeConfig(CryConfig *config);

View File

@ -38,8 +38,8 @@ namespace cryfs {
constexpr uint32_t CryDevice::BLOCKSIZE_BYTES;
CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore)
: _blobStore(make_unique<BlobStoreOnBlocks>(make_unique<CachingBlockStore>(make_unique<EncryptedBlockStore<Cipher>>(std::move(blockStore), GetEncryptionKey(config.get()))), BLOCKSIZE_BYTES)), _rootKey(GetOrCreateRootKey(config.get())) {
CryDevice::CryDevice(unique_ref<CryConfig> config, unique_ptr<BlockStore> blockStore)
: _blobStore(make_unique_ref<BlobStoreOnBlocks>(make_unique<CachingBlockStore>(make_unique<EncryptedBlockStore<Cipher>>(std::move(blockStore), GetEncryptionKey(config.get()))), BLOCKSIZE_BYTES)), _rootKey(GetOrCreateRootKey(config.get())) {
}
Key CryDevice::GetOrCreateRootKey(CryConfig *config) {

View File

@ -20,7 +20,7 @@ public:
using Cipher = CryConfigLoader::Cipher;
CryDevice(std::unique_ptr<CryConfig> config, std::unique_ptr<blockstore::BlockStore> blockStore);
CryDevice(cpputils::unique_ref<CryConfig> config, std::unique_ptr<blockstore::BlockStore> blockStore);
virtual ~CryDevice();
void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override;
@ -38,7 +38,7 @@ private:
Cipher::EncryptionKey GetEncryptionKey(CryConfig *config);
blockstore::Key CreateRootBlobAndReturnKey();
std::unique_ptr<blobstore::BlobStore> _blobStore;
cpputils::unique_ref<blobstore::BlobStore> _blobStore;
blockstore::Key _rootKey;

View File

@ -24,6 +24,7 @@ using std::vector;
using blockstore::Key;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using boost::optional;
using boost::none;
@ -38,9 +39,13 @@ CryDir::~CryDir() {
unique_ptr<fspp::OpenFile> CryDir::createAndOpenFile(const string &name, mode_t mode, uid_t uid, gid_t gid) {
auto blob = LoadBlob();
if (blob == none) {
//TODO Return correct fuse error
throw FuseErrnoException(EIO);
}
auto child = device()->CreateBlob();
Key childkey = child->key();
blob->AddChildFile(name, childkey, mode, uid, gid);
(*blob)->AddChildFile(name, childkey, mode, uid, gid);
//TODO Do we need a return value in createDir for fspp? If not, change fspp Dir interface!
auto childblob = FileBlob::InitializeEmptyFile(std::move(child));
return make_unique<CryOpenFile>(std::move(childblob));
@ -48,26 +53,35 @@ unique_ptr<fspp::OpenFile> CryDir::createAndOpenFile(const string &name, mode_t
void CryDir::createDir(const string &name, mode_t mode, uid_t uid, gid_t gid) {
auto blob = LoadBlob();
if (blob == none) {
//TODO Return correct fuse error
throw FuseErrnoException(EIO);
}
auto child = device()->CreateBlob();
Key childkey = child->key();
blob->AddChildDir(name, childkey, mode, uid, gid);
(*blob)->AddChildDir(name, childkey, mode, uid, gid);
DirBlob::InitializeEmptyDir(std::move(child), device());
}
unique_ptr<DirBlob> CryDir::LoadBlob() const {
optional<unique_ref<DirBlob>> CryDir::LoadBlob() const {
auto blob = CryNode::LoadBlob();
if(blob == none) {
return nullptr;
return none;
}
//TODO Without const_cast?
return make_unique<DirBlob>(std::move(*blob), const_cast<CryDevice*>(device()));
return make_unique_ref<DirBlob>(std::move(*blob), const_cast<CryDevice*>(device()));
}
unique_ptr<vector<fspp::Dir::Entry>> CryDir::children() const {
auto children = make_unique<vector<fspp::Dir::Entry>>();
children->push_back(fspp::Dir::Entry(fspp::Dir::EntryType::DIR, "."));
children->push_back(fspp::Dir::Entry(fspp::Dir::EntryType::DIR, ".."));
LoadBlob()->AppendChildrenTo(children.get());
auto blob = LoadBlob();
if (blob == none) {
//TODO Return correct fuse error
throw FuseErrnoException(EIO);
}
(*blob)->AppendChildrenTo(children.get());
return children;
}
@ -77,9 +91,13 @@ fspp::Dir::EntryType CryDir::getType() const {
void CryDir::createSymlink(const string &name, const bf::path &target, uid_t uid, gid_t gid) {
auto blob = LoadBlob();
if (blob == none) {
//TODO Return correct fuse error
throw FuseErrnoException(EIO);
}
auto child = device()->CreateBlob();
Key childkey = child->key();
blob->AddChildSymlink(name, childkey, uid, gid);
(*blob)->AddChildSymlink(name, childkey, uid, gid);
SymlinkBlob::InitializeSymlink(std::move(child), target);
}

View File

@ -24,7 +24,7 @@ public:
fspp::Dir::EntryType getType() const override;
private:
std::unique_ptr<DirBlob> LoadBlob() const;
boost::optional<cpputils::unique_ref<DirBlob>> LoadBlob() const;
DISALLOW_COPY_AND_ASSIGN(CryDir);
};

View File

@ -10,7 +10,6 @@
namespace bf = boost::filesystem;
using std::unique_ptr;
using blockstore::Key;
using blobstore::Blob;
using cpputils::dynamic_pointer_move;

View File

@ -18,7 +18,9 @@ using std::vector;
using blockstore::Key;
using boost::none;
using boost::optional;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
namespace cryfs {
@ -29,12 +31,12 @@ CrySymlink::CrySymlink(CryDevice *device, unique_ref<DirBlob> parent, const Key
CrySymlink::~CrySymlink() {
}
unique_ptr<SymlinkBlob> CrySymlink::LoadBlob() const {
optional<unique_ref<SymlinkBlob>> CrySymlink::LoadBlob() const {
auto blob = CryNode::LoadBlob();
if (blob == none) {
return nullptr;
return none;
}
return make_unique<SymlinkBlob>(std::move(*blob));
return make_unique_ref<SymlinkBlob>(std::move(*blob));
}
fspp::Dir::EntryType CrySymlink::getType() const {
@ -42,7 +44,12 @@ fspp::Dir::EntryType CrySymlink::getType() const {
}
bf::path CrySymlink::target() const {
return LoadBlob()->target();
auto blob = LoadBlob();
if (blob == none) {
//TODO Return correct fuse error
throw FuseErrnoException(EIO);
}
return (*blob)->target();
}
}

View File

@ -19,7 +19,7 @@ public:
fspp::Dir::EntryType getType() const override;
private:
std::unique_ptr<SymlinkBlob> LoadBlob() const;
boost::optional<cpputils::unique_ref<SymlinkBlob>> LoadBlob() const;
DISALLOW_COPY_AND_ASSIGN(CrySymlink);
};

View File

@ -31,7 +31,7 @@ TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) {
{
CryDevice dev(CryConfigLoader::createNewWithWeakKey(config.path()), make_unique<OnDiskBlockStore>(rootdir.path()));
}
CryDevice dev(CryConfigLoader::loadExisting(config.path()), make_unique<OnDiskBlockStore>(rootdir.path()));
CryDevice dev(std::move(CryConfigLoader::loadExisting(config.path()).get()), make_unique<OnDiskBlockStore>(rootdir.path()));
auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root)->children();
}
@ -40,7 +40,7 @@ TEST_F(CryFsTest, UsingStrongKey1_CreatedRootdirIsLoadableAfterClosing) {
{
CryDevice dev(CryConfigLoader::createNew(config.path()), make_unique<OnDiskBlockStore>(rootdir.path()));
}
CryDevice dev(CryConfigLoader::loadExisting(config.path()), make_unique<OnDiskBlockStore>(rootdir.path()));
CryDevice dev(std::move(CryConfigLoader::loadExisting(config.path()).get()), make_unique<OnDiskBlockStore>(rootdir.path()));
auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root)->children();
}