More use of unique_ref instead of unique_ptr

This commit is contained in:
Sebastian Meßmer 2015-06-18 13:14:43 +02:00
parent b2e661a328
commit f4d925aa9d
18 changed files with 77 additions and 48 deletions

View File

@ -28,7 +28,9 @@ using blobstore::onblocks::BlobStoreOnBlocks;
using blobstore::onblocks::BlobOnBlocks;
using blockstore::caching::CachingBlockStore;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using boost::optional;
using boost::none;
namespace bf = boost::filesystem;
@ -71,44 +73,48 @@ unique_ptr<fspp::Node> CryDevice::Load(const bf::path &path) {
if (path.parent_path().empty()) {
//We are asked to load the root directory '/'.
return make_unique<CryDir>(this, nullptr, _rootKey);
return make_unique<CryDir>(this, none, _rootKey);
}
auto parent = LoadDirBlob(path.parent_path());
auto entry = parent->GetChild(path.filename().native());
if (parent == none) {
//TODO Return correct fuse error
return nullptr;
}
auto entry = (*parent)->GetChild(path.filename().native());
if (entry.type == fspp::Dir::EntryType::DIR) {
return make_unique<CryDir>(this, std::move(parent), entry.key);
return make_unique<CryDir>(this, std::move(*parent), entry.key);
} else if (entry.type == fspp::Dir::EntryType::FILE) {
return make_unique<CryFile>(this, std::move(parent), entry.key);
return make_unique<CryFile>(this, std::move(*parent), entry.key);
} else if (entry.type == fspp::Dir::EntryType::SYMLINK) {
return make_unique<CrySymlink>(this, std::move(parent), entry.key);
return make_unique<CrySymlink>(this, std::move(*parent), entry.key);
} else {
throw FuseErrnoException(EIO);
}
}
unique_ptr<DirBlob> CryDevice::LoadDirBlob(const bf::path &path) {
optional<unique_ref<DirBlob>> CryDevice::LoadDirBlob(const bf::path &path) {
auto currentBlob = _blobStore->load(_rootKey);
if(!currentBlob) {
if(currentBlob == none) {
//TODO Return correct fuse error
return nullptr;
return none;
}
for (const bf::path &component : path.relative_path()) {
//TODO Check whether the next path component is a dir.
// Right now, an assertion in DirBlob constructor will fail if it isn't.
// But fuse should rather return the correct error code.
unique_ptr<DirBlob> currentDir = make_unique<DirBlob>(std::move(*currentBlob), this);
unique_ref<DirBlob> currentDir = make_unique_ref<DirBlob>(std::move(*currentBlob), this);
Key childKey = currentDir->GetChild(component.c_str()).key;
currentBlob = _blobStore->load(childKey);
if(!currentBlob) {
if(currentBlob == none) {
//TODO Return correct fuse error
return nullptr;
return none;
}
}
return make_unique<DirBlob>(std::move(*currentBlob), this);
return make_unique_ref<DirBlob>(std::move(*currentBlob), this);
}
void CryDevice::statfs(const bf::path &path, struct statvfs *fsstat) {

View File

@ -31,7 +31,7 @@ public:
std::unique_ptr<fspp::Node> Load(const boost::filesystem::path &path) override;
std::unique_ptr<DirBlob> LoadDirBlob(const boost::filesystem::path &path);
boost::optional<cpputils::unique_ref<DirBlob>> LoadDirBlob(const boost::filesystem::path &path);
private:
blockstore::Key GetOrCreateRootKey(CryConfig *config);

View File

@ -29,7 +29,7 @@ using boost::none;
namespace cryfs {
CryDir::CryDir(CryDevice *device, unique_ptr<DirBlob> parent, const Key &key)
CryDir::CryDir(CryDevice *device, boost::optional<unique_ref<DirBlob>> parent, const Key &key)
: CryNode(device, std::move(parent), key) {
}

View File

@ -10,7 +10,7 @@ namespace cryfs {
class CryDir: public fspp::Dir, CryNode {
public:
CryDir(CryDevice *device, std::unique_ptr<DirBlob> parent, const blockstore::Key &key);
CryDir(CryDevice *device, boost::optional<cpputils::unique_ref<DirBlob>> parent, const blockstore::Key &key);
virtual ~CryDir();
//TODO return type variance to CryFile/CryDir?

View File

@ -16,10 +16,12 @@ using std::make_unique;
using blockstore::Key;
using boost::none;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
namespace cryfs {
CryFile::CryFile(CryDevice *device, unique_ptr<DirBlob> parent, const Key &key)
CryFile::CryFile(CryDevice *device, unique_ref<DirBlob> parent, const Key &key)
: CryNode(device, std::move(parent), key) {
}
@ -29,7 +31,7 @@ CryFile::~CryFile() {
unique_ptr<fspp::OpenFile> CryFile::open(int flags) const {
auto blob = LoadBlob();
assert(blob != none);
return make_unique<CryOpenFile>(make_unique<FileBlob>(std::move(*blob)));
return make_unique<CryOpenFile>(make_unique_ref<FileBlob>(std::move(*blob)));
}
void CryFile::truncate(off_t size) const {

View File

@ -11,7 +11,7 @@ namespace cryfs {
class CryFile: public fspp::File, CryNode {
public:
CryFile(CryDevice *device, std::unique_ptr<DirBlob> parent, const blockstore::Key &key);
CryFile(CryDevice *device, cpputils::unique_ref<DirBlob> parent, const blockstore::Key &key);
virtual ~CryFile();
std::unique_ptr<fspp::OpenFile> open(int flags) const override;

View File

@ -16,6 +16,7 @@ using blobstore::Blob;
using cpputils::dynamic_pointer_move;
using cpputils::unique_ref;
using boost::optional;
using boost::none;
//TODO Get rid of this in favor of an exception hierarchy
using fspp::fuse::CHECK_RETVAL;
@ -23,7 +24,7 @@ using fspp::fuse::FuseErrnoException;
namespace cryfs {
CryNode::CryNode(CryDevice *device, unique_ptr<DirBlob> parent, const Key &key)
CryNode::CryNode(CryDevice *device, optional<unique_ref<DirBlob>> parent, const Key &key)
: _device(device),
_parent(std::move(parent)),
_key(key) {
@ -39,16 +40,25 @@ void CryNode::access(int mask) const {
}
void CryNode::rename(const bf::path &to) {
if (_parent == none) {
//We are the root direcory.
//TODO What should we do?
throw FuseErrnoException(EIO);
}
//TODO More efficient implementation possible: directly rename when it's actually not moved to a different directory
// It's also quite ugly code because in the parent==targetDir case, it depends on _parent not overriding the changes made by targetDir.
auto old = _parent->GetChild(_key);
auto old = (*_parent)->GetChild(_key);
auto mode = old.mode;
auto uid = old.uid;
auto gid = old.gid;
_parent->RemoveChild(_key);
_parent->flush();
(*_parent)->RemoveChild(_key);
(*_parent)->flush();
auto targetDir = _device->LoadDirBlob(to.parent_path());
targetDir->AddChild(to.filename().native(), _key, getType(), mode, uid, gid);
if (targetDir == none) {
//TODO Return correct fuse error
throw FuseErrnoException(ENOSPC);
}
(*targetDir)->AddChild(to.filename().native(), _key, getType(), mode, uid, gid);
}
void CryNode::utimens(const timespec times[2]) {
@ -57,7 +67,13 @@ void CryNode::utimens(const timespec times[2]) {
}
void CryNode::remove() {
_parent->RemoveChild(_key);
//TODO Instead of all these if-else and having _parent being an optional, we could also introduce a CryRootDir which inherits from fspp::Dir.
if (_parent == none) {
//We are the root direcory.
//TODO What should we do?
throw FuseErrnoException(EIO);
}
(*_parent)->RemoveChild(_key);
_device->RemoveBlob(_key);
}
@ -74,31 +90,31 @@ optional<unique_ref<Blob>> CryNode::LoadBlob() const {
}
void CryNode::stat(struct ::stat *result) const {
if(_parent.get() == nullptr) {
if(_parent == none) {
//We are the root directory.
//TODO What should we do?
result->st_mode = S_IFDIR;
} else {
_parent->statChild(_key, result);
(*_parent)->statChild(_key, result);
}
}
void CryNode::chmod(mode_t mode) {
if (_parent.get() == nullptr) {
if (_parent == none) {
//We are the root direcory.
//TODO What should we do?
throw FuseErrnoException(EIO);
}
_parent->chmodChild(_key, mode);
(*_parent)->chmodChild(_key, mode);
}
void CryNode::chown(uid_t uid, gid_t gid) {
if (_parent.get() == nullptr) {
if (_parent == none) {
//We are the root direcory.
//TODO What should we do?
throw FuseErrnoException(EIO);
}
_parent->chownChild(_key, uid, gid);
(*_parent)->chownChild(_key, uid, gid);
}
}

View File

@ -12,7 +12,7 @@ namespace cryfs {
class CryNode: public virtual fspp::Node {
public:
CryNode(CryDevice *device, std::unique_ptr<DirBlob> parent, const blockstore::Key &key);
CryNode(CryDevice *device, boost::optional<cpputils::unique_ref<DirBlob>> parent, const blockstore::Key &key);
void access(int mask) const override;
void stat(struct ::stat *result) const override;
void chmod(mode_t mode) override;
@ -33,7 +33,7 @@ protected:
private:
CryDevice *_device;
std::unique_ptr<DirBlob> _parent;
boost::optional<cpputils::unique_ref<DirBlob>> _parent;
blockstore::Key _key;
DISALLOW_COPY_AND_ASSIGN(CryNode);

View File

@ -9,7 +9,7 @@
namespace bf = boost::filesystem;
using std::unique_ptr;
using cpputils::unique_ref;
using blobstore::Blob;
//TODO Get rid of this in favor of a exception hierarchy
@ -18,7 +18,7 @@ using fspp::fuse::FuseErrnoException;
namespace cryfs {
CryOpenFile::CryOpenFile(unique_ptr<FileBlob> fileBlob)
CryOpenFile::CryOpenFile(unique_ref<FileBlob> fileBlob)
: _fileBlob(std::move(fileBlob)) {
}

View File

@ -3,7 +3,8 @@
#define CRYFS_LIB_CRYOPENFILE_H_
#include "messmer/fspp/fs_interface/OpenFile.h"
#include "messmer/cpp-utils/macros.h"
#include <messmer/cpp-utils/macros.h>
#include <messmer/cpp-utils/unique_ref.h>
namespace cryfs {
class CryDevice;
@ -11,7 +12,7 @@ class FileBlob;
class CryOpenFile: public fspp::OpenFile {
public:
explicit CryOpenFile(std::unique_ptr<FileBlob> fileBlob);
explicit CryOpenFile(cpputils::unique_ref<FileBlob> fileBlob);
virtual ~CryOpenFile();
void stat(struct ::stat *result) const override;
@ -23,7 +24,7 @@ public:
void fdatasync() override;
private:
std::unique_ptr<FileBlob> _fileBlob;
cpputils::unique_ref<FileBlob> _fileBlob;
DISALLOW_COPY_AND_ASSIGN(CryOpenFile);
};

View File

@ -18,10 +18,11 @@ using std::vector;
using blockstore::Key;
using boost::none;
using cpputils::unique_ref;
namespace cryfs {
CrySymlink::CrySymlink(CryDevice *device, unique_ptr<DirBlob> parent, const Key &key)
CrySymlink::CrySymlink(CryDevice *device, unique_ref<DirBlob> parent, const Key &key)
: CryNode(device, std::move(parent), key) {
}

View File

@ -11,7 +11,7 @@ namespace cryfs {
class CrySymlink: public fspp::Symlink, CryNode {
public:
CrySymlink(CryDevice *device, std::unique_ptr<DirBlob> parent, const blockstore::Key &key);
CrySymlink(CryDevice *device, cpputils::unique_ref<DirBlob> parent, const blockstore::Key &key);
virtual ~CrySymlink();
boost::filesystem::path target() const override;

View File

@ -22,6 +22,7 @@ using blobstore::Blob;
using blockstore::Key;
using cpputils::Data;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using boost::none;
namespace cryfs {
@ -42,11 +43,11 @@ void DirBlob::flush() {
_blob->flush();
}
unique_ptr<DirBlob> DirBlob::InitializeEmptyDir(unique_ref<Blob> blob, CryDevice *device) {
unique_ref<DirBlob> DirBlob::InitializeEmptyDir(unique_ref<Blob> blob, CryDevice *device) {
blob->resize(1);
unsigned char magicNumber = MagicNumbers::DIR;
blob->write(&magicNumber, 0, 1);
return make_unique<DirBlob>(std::move(blob), device);
return make_unique_ref<DirBlob>(std::move(blob), device);
}
unsigned char DirBlob::magicNumber() const {

View File

@ -40,7 +40,7 @@ public:
gid_t gid;
};
static std::unique_ptr<DirBlob> InitializeEmptyDir(cpputils::unique_ref<blobstore::Blob> blob, CryDevice *device);
static cpputils::unique_ref<DirBlob> InitializeEmptyDir(cpputils::unique_ref<blobstore::Blob> blob, CryDevice *device);
DirBlob(cpputils::unique_ref<blobstore::Blob> blob, CryDevice *device);
virtual ~DirBlob();

View File

@ -8,6 +8,7 @@ using std::unique_ptr;
using std::make_unique;
using blobstore::Blob;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
namespace cryfs {
@ -18,12 +19,12 @@ FileBlob::FileBlob(unique_ref<Blob> blob)
FileBlob::~FileBlob() {
}
unique_ptr<FileBlob> FileBlob::InitializeEmptyFile(unique_ref<Blob> blob) {
unique_ref<FileBlob> FileBlob::InitializeEmptyFile(unique_ref<Blob> blob) {
assert(blob.get() != nullptr);
blob->resize(1);
unsigned char magicNumber = MagicNumbers::FILE;
blob->write(&magicNumber, 0, 1);
return make_unique<FileBlob>(std::move(blob));
return make_unique_ref<FileBlob>(std::move(blob));
}
unsigned char FileBlob::magicNumber() const {

View File

@ -10,7 +10,7 @@ namespace cryfs {
class FileBlob {
public:
static std::unique_ptr<FileBlob> InitializeEmptyFile(cpputils::unique_ref<blobstore::Blob> blob);
static cpputils::unique_ref<FileBlob> InitializeEmptyFile(cpputils::unique_ref<blobstore::Blob> blob);
FileBlob(cpputils::unique_ref<blobstore::Blob> blob);
virtual ~FileBlob();

View File

@ -9,6 +9,7 @@ using std::make_unique;
using std::string;
using blobstore::Blob;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
namespace bf = boost::filesystem;
@ -24,14 +25,14 @@ SymlinkBlob::SymlinkBlob(const bf::path &target) :_target(target) {
SymlinkBlob::~SymlinkBlob() {
}
unique_ptr<SymlinkBlob> SymlinkBlob::InitializeSymlink(unique_ref<Blob> blob, const bf::path &target) {
unique_ref<SymlinkBlob> SymlinkBlob::InitializeSymlink(unique_ref<Blob> blob, const bf::path &target) {
assert(blob.get() != nullptr);
string targetStr = target.native();
blob->resize(1 + targetStr.size());
unsigned char magicNumber = MagicNumbers::SYMLINK;
blob->write(&magicNumber, 0, 1);
blob->write(targetStr.c_str(), 1, targetStr.size());
return make_unique<SymlinkBlob>(target);
return make_unique_ref<SymlinkBlob>(target);
}
void SymlinkBlob::_checkMagicNumber(const Blob &blob) {

View File

@ -11,7 +11,7 @@ namespace cryfs {
class SymlinkBlob {
public:
static std::unique_ptr<SymlinkBlob> InitializeSymlink(cpputils::unique_ref<blobstore::Blob> blob, const boost::filesystem::path &target);
static cpputils::unique_ref<SymlinkBlob> InitializeSymlink(cpputils::unique_ref<blobstore::Blob> blob, const boost::filesystem::path &target);
SymlinkBlob(cpputils::unique_ref<blobstore::Blob> blob);
SymlinkBlob(const boost::filesystem::path &target);