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

View File

@ -2,7 +2,7 @@
#ifndef MESSMER_CRYFS_SRC_CRYCONFIGLOADER_H_ #ifndef MESSMER_CRYFS_SRC_CRYCONFIGLOADER_H_
#define 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 <boost/filesystem/path.hpp>
#include "CryConfig.h" #include "CryConfig.h"
#include <messmer/blockstore/implementations/encrypted/ciphers/AES256_GCM.h> #include <messmer/blockstore/implementations/encrypted/ciphers/AES256_GCM.h>
@ -13,14 +13,14 @@ class CryConfigLoader {
public: public:
using Cipher = blockstore::encrypted::AES256_GCM; 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 cpputils::unique_ref<CryConfig> createNew(const boost::filesystem::path &filename);
static std::unique_ptr<CryConfig> loadExisting(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. //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 cpputils::unique_ref<CryConfig> loadOrCreateWithWeakKey(const boost::filesystem::path &filename);
static std::unique_ptr<CryConfig> createNewWithWeakKey(const boost::filesystem::path &filename); static cpputils::unique_ref<CryConfig> createNewWithWeakKey(const boost::filesystem::path &filename);
private: private:
static void _initializeConfig(CryConfig *config); static void _initializeConfig(CryConfig *config);

View File

@ -38,8 +38,8 @@ namespace cryfs {
constexpr uint32_t CryDevice::BLOCKSIZE_BYTES; constexpr uint32_t CryDevice::BLOCKSIZE_BYTES;
CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore) CryDevice::CryDevice(unique_ref<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())) { : _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) { Key CryDevice::GetOrCreateRootKey(CryConfig *config) {

View File

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

View File

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

View File

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

View File

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

View File

@ -18,7 +18,9 @@ using std::vector;
using blockstore::Key; using blockstore::Key;
using boost::none; using boost::none;
using boost::optional;
using cpputils::unique_ref; using cpputils::unique_ref;
using cpputils::make_unique_ref;
namespace cryfs { namespace cryfs {
@ -29,12 +31,12 @@ CrySymlink::CrySymlink(CryDevice *device, unique_ref<DirBlob> parent, const Key
CrySymlink::~CrySymlink() { CrySymlink::~CrySymlink() {
} }
unique_ptr<SymlinkBlob> CrySymlink::LoadBlob() const { optional<unique_ref<SymlinkBlob>> CrySymlink::LoadBlob() const {
auto blob = CryNode::LoadBlob(); auto blob = CryNode::LoadBlob();
if (blob == none) { 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 { fspp::Dir::EntryType CrySymlink::getType() const {
@ -42,7 +44,12 @@ fspp::Dir::EntryType CrySymlink::getType() const {
} }
bf::path CrySymlink::target() 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; fspp::Dir::EntryType getType() const override;
private: private:
std::unique_ptr<SymlinkBlob> LoadBlob() const; boost::optional<cpputils::unique_ref<SymlinkBlob>> LoadBlob() const;
DISALLOW_COPY_AND_ASSIGN(CrySymlink); 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::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("/")); auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root)->children(); 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::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("/")); auto root = dev.Load(bf::path("/"));
dynamic_pointer_move<CryDir>(root)->children(); dynamic_pointer_move<CryDir>(root)->children();
} }