CryDir/CryFile don't actually open the block, but only keep a reference to the key

This commit is contained in:
Sebastian Messmer 2015-03-11 00:23:52 +01:00
parent 7ec0af147d
commit 75b9a29ae4
11 changed files with 102 additions and 84 deletions

View File

@ -8,8 +8,6 @@
#include "messmer/blobstore/implementations/onblocks/BlobStoreOnBlocks.h" #include "messmer/blobstore/implementations/onblocks/BlobStoreOnBlocks.h"
#include "messmer/blobstore/implementations/onblocks/BlobOnBlocks.h" #include "messmer/blobstore/implementations/onblocks/BlobOnBlocks.h"
using std::unique_ptr;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using std::string; using std::string;
@ -45,8 +43,7 @@ Key CryDevice::GetOrCreateRootKey(CryConfig *config) {
Key CryDevice::CreateRootBlobAndReturnKey() { Key CryDevice::CreateRootBlobAndReturnKey() {
auto rootBlob = _blobStore->create(); auto rootBlob = _blobStore->create();
Key rootBlobKey = rootBlob->key(); Key rootBlobKey = rootBlob->key();
DirBlob rootDir(std::move(rootBlob)); DirBlob::InitializeEmptyDir(std::move(rootBlob));
rootDir.InitializeEmptyDir();
return rootBlobKey; return rootBlobKey;
} }
@ -57,26 +54,38 @@ unique_ptr<fspp::Node> CryDevice::Load(const bf::path &path) {
printf("Loading %s\n", path.c_str()); printf("Loading %s\n", path.c_str());
assert(path.is_absolute()); assert(path.is_absolute());
auto currentBlob = _blobStore->load(_rootKey); if (path.parent_path().empty()) {
//We are asked to load the root directory '/'.
for (const bf::path &component : path.relative_path()) { return make_unique<CryDir>(this, _rootKey);
if (!DirBlob::IsDir(*currentBlob)) {
throw FuseErrnoException(ENOTDIR);
}
unique_ptr<DirBlob> currentDir = make_unique<DirBlob>(std::move(currentBlob));
Key childKey = currentDir->GetBlobKeyForName(component.c_str());
currentBlob = _blobStore->load(childKey);
} }
if (DirBlob::IsDir(*currentBlob)) { auto parent = LoadDirBlob(path.parent_path());
return make_unique<CryDir>(this, std::move(make_unique<DirBlob>(std::move(currentBlob)))); auto entry = parent->GetChild(path.filename().native());
} else if (FileBlob::IsFile(*currentBlob)) {
return make_unique<CryFile>(this, std::move(make_unique<FileBlob>(std::move(currentBlob)))); if (entry.first == fspp::Dir::EntryType::DIR) {
return make_unique<CryDir>(this, entry.second);
} else if (entry.first == fspp::Dir::EntryType::FILE) {
return make_unique<CryFile>(this, std::move(parent), entry.second);
} else { } else {
throw FuseErrnoException(EIO); throw FuseErrnoException(EIO);
} }
} }
unique_ptr<DirBlob> CryDevice::LoadDirBlob(const bf::path &path) {
auto currentBlob = _blobStore->load(_rootKey);
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));
Key childKey = currentDir->GetChild(component.c_str()).second;
currentBlob = _blobStore->load(childKey);
}
return make_unique<DirBlob>(std::move(currentBlob));
}
void CryDevice::statfs(const bf::path &path, struct statvfs *fsstat) { void CryDevice::statfs(const bf::path &path, struct statvfs *fsstat) {
throw FuseErrnoException(ENOTSUP); throw FuseErrnoException(ENOTSUP);
} }

View File

@ -12,6 +12,7 @@
#include "messmer/cpp-utils/macros.h" #include "messmer/cpp-utils/macros.h"
namespace cryfs { namespace cryfs {
class DirBlob;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
@ -32,6 +33,8 @@ private:
blockstore::Key CreateRootBlobAndReturnKey(); blockstore::Key CreateRootBlobAndReturnKey();
std::unique_ptr<fspp::Node> Load(const bf::path &path) override; std::unique_ptr<fspp::Node> Load(const bf::path &path) override;
std::unique_ptr<DirBlob> LoadDirBlob(const bf::path &path);
std::unique_ptr<blobstore::BlobStore> _blobStore; std::unique_ptr<blobstore::BlobStore> _blobStore;
blockstore::Key _rootKey; blockstore::Key _rootKey;

View File

@ -8,6 +8,7 @@
#include "messmer/fspp/fuse/FuseErrnoException.h" #include "messmer/fspp/fuse/FuseErrnoException.h"
#include "CryDevice.h" #include "CryDevice.h"
#include "CryFile.h" #include "CryFile.h"
#include "CryOpenFile.h"
//TODO Get rid of this in favor of exception hierarchy //TODO Get rid of this in favor of exception hierarchy
using fspp::fuse::CHECK_RETVAL; using fspp::fuse::CHECK_RETVAL;
@ -20,10 +21,12 @@ using std::make_unique;
using std::string; using std::string;
using std::vector; using std::vector;
using blockstore::Key;
namespace cryfs { namespace cryfs {
CryDir::CryDir(CryDevice *device, unique_ptr<DirBlob> blob) CryDir::CryDir(CryDevice *device, const Key &key)
: _device(device), _blob(std::move(blob)) { : _device(device), _key(key) {
} }
CryDir::~CryDir() { CryDir::~CryDir() {
@ -35,22 +38,26 @@ void CryDir::stat(struct ::stat *result) const {
throw FuseErrnoException(ENOTSUP); throw FuseErrnoException(ENOTSUP);
} }
unique_ptr<fspp::File> CryDir::createFile(const string &name, mode_t mode) { unique_ptr<fspp::OpenFile> CryDir::createAndOpenFile(const string &name, mode_t mode) {
auto blob = LoadBlob();
auto child = _device->CreateBlob(); auto child = _device->CreateBlob();
_blob->AddChildFile(name, child->key()); Key childkey = child->key();
//TODO Do we need a return value in createDir for fspp? If not, change fspp! blob->AddChildFile(name, childkey);
auto fileblob = make_unique<FileBlob>(std::move(child)); //TODO Do we need a return value in createDir for fspp? If not, change fspp Dir interface!
fileblob->InitializeEmptyFile(); auto childblob = FileBlob::InitializeEmptyFile(std::move(child));
return make_unique<CryFile>(_device, std::move(fileblob)); return make_unique<CryOpenFile>(std::move(childblob));
} }
unique_ptr<fspp::Dir> CryDir::createDir(const string &name, mode_t mode) { void CryDir::createDir(const string &name, mode_t mode) {
auto blob = LoadBlob();
auto child = _device->CreateBlob(); auto child = _device->CreateBlob();
_blob->AddChildDir(name, child->key()); Key childkey = child->key();
//TODO I don't think we need a return value in createDir for fspp. Change fspp! blob->AddChildDir(name, childkey);
auto dirblob = make_unique<DirBlob>(std::move(child)); DirBlob::InitializeEmptyDir(std::move(child));
dirblob->InitializeEmptyDir(); }
return make_unique<CryDir>(_device, std::move(dirblob));
unique_ptr<DirBlob> CryDir::LoadBlob() const {
return make_unique<DirBlob>(_device->LoadBlob(_key));
} }
void CryDir::rmdir() { void CryDir::rmdir() {
@ -58,7 +65,7 @@ void CryDir::rmdir() {
} }
unique_ptr<vector<fspp::Dir::Entry>> CryDir::children() const { unique_ptr<vector<fspp::Dir::Entry>> CryDir::children() const {
return _blob->GetChildren(); return LoadBlob()->GetChildren();
} }
} }

View File

@ -10,13 +10,13 @@ namespace cryfs {
class CryDir: public fspp::Dir, CryNode { class CryDir: public fspp::Dir, CryNode {
public: public:
CryDir(CryDevice *device, std::unique_ptr<DirBlob> blob); CryDir(CryDevice *device, const blockstore::Key &key);
virtual ~CryDir(); virtual ~CryDir();
void stat(struct ::stat *result) const override; void stat(struct ::stat *result) const override;
//TODO return type variance to CryFile/CryDir? //TODO return type variance to CryFile/CryDir?
std::unique_ptr<fspp::File> createFile(const std::string &name, mode_t mode) override; std::unique_ptr<fspp::OpenFile> createAndOpenFile(const std::string &name, mode_t mode) override;
std::unique_ptr<fspp::Dir> createDir(const std::string &name, mode_t mode) override; void createDir(const std::string &name, mode_t mode) override;
void rmdir() override; void rmdir() override;
//TODO Make Entry a public class instead of hidden in DirBlob (which is not publicly visible) //TODO Make Entry a public class instead of hidden in DirBlob (which is not publicly visible)
@ -24,7 +24,9 @@ public:
private: private:
CryDevice *_device; CryDevice *_device;
std::unique_ptr<DirBlob> _blob; blockstore::Key _key;
std::unique_ptr<DirBlob> LoadBlob() const;
DISALLOW_COPY_AND_ASSIGN(CryDir); DISALLOW_COPY_AND_ASSIGN(CryDir);
}; };

View File

@ -3,6 +3,7 @@
#include "CryDevice.h" #include "CryDevice.h"
#include "CryOpenFile.h" #include "CryOpenFile.h"
#include "messmer/fspp/fuse/FuseErrnoException.h" #include "messmer/fspp/fuse/FuseErrnoException.h"
#include "impl/DirBlob.h"
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
@ -13,19 +14,21 @@ using fspp::fuse::FuseErrnoException;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using blockstore::Key;
namespace cryfs { namespace cryfs {
CryFile::CryFile(CryDevice *device, unique_ptr<FileBlob> blob) CryFile::CryFile(CryDevice *device, unique_ptr<DirBlob> parent, const Key &key)
: _device(device), : _device(device),
_blob(std::move(blob)) { _parent(std::move(parent)),
_key(key) {
} }
CryFile::~CryFile() { CryFile::~CryFile() {
} }
unique_ptr<fspp::OpenFile> CryFile::open(int flags) const { unique_ptr<fspp::OpenFile> CryFile::open(int flags) const {
//TODO This is a performance issue because we open the blob twice on the "open" syscall return make_unique<CryOpenFile>(make_unique<FileBlob>(_device->LoadBlob(_key)));
return make_unique<CryOpenFile>(make_unique<FileBlob>(_device->LoadBlob(_blob->key())));
} }
void CryFile::stat(struct ::stat *result) const { void CryFile::stat(struct ::stat *result) const {

View File

@ -11,7 +11,7 @@ namespace cryfs {
class CryFile: public fspp::File, CryNode { class CryFile: public fspp::File, CryNode {
public: public:
CryFile(CryDevice *device, std::unique_ptr<FileBlob> blob); CryFile(CryDevice *device, std::unique_ptr<DirBlob> parent, const blockstore::Key &key);
virtual ~CryFile(); virtual ~CryFile();
void stat(struct ::stat *result) const override; void stat(struct ::stat *result) const override;
@ -21,7 +21,8 @@ public:
private: private:
CryDevice *_device; CryDevice *_device;
std::unique_ptr<FileBlob> _blob; std::unique_ptr<DirBlob> _parent;
blockstore::Key _key;
DISALLOW_COPY_AND_ASSIGN(CryFile); DISALLOW_COPY_AND_ASSIGN(CryFile);
}; };

View File

@ -31,11 +31,12 @@ void CryOpenFile::flush() {
} }
void CryOpenFile::stat(struct ::stat *result) const { void CryOpenFile::stat(struct ::stat *result) const {
throw FuseErrnoException(ENOTSUP); result->st_mode = S_IFREG | S_IRUSR | S_IXUSR | S_IWUSR;
return;
} }
void CryOpenFile::truncate(off_t size) const { void CryOpenFile::truncate(off_t size) const {
throw FuseErrnoException(ENOTSUP); _fileBlob->resize(size);
} }
int CryOpenFile::read(void *buf, size_t count, off_t offset) { int CryOpenFile::read(void *buf, size_t count, off_t offset) {

View File

@ -11,6 +11,8 @@ using std::unique_ptr;
using std::make_unique; using std::make_unique;
using std::vector; using std::vector;
using std::string; using std::string;
using std::pair;
using std::make_pair;
using blobstore::Blob; using blobstore::Blob;
using blockstore::Key; using blockstore::Key;
@ -22,31 +24,25 @@ namespace cryfs {
DirBlob::DirBlob(unique_ptr<Blob> blob) DirBlob::DirBlob(unique_ptr<Blob> blob)
: _blob(std::move(blob)) { : _blob(std::move(blob)) {
assert(magicNumber() == MagicNumbers::DIR);
} }
DirBlob::~DirBlob() { DirBlob::~DirBlob() {
} }
void DirBlob::InitializeEmptyDir() { unique_ptr<DirBlob> DirBlob::InitializeEmptyDir(unique_ptr<Blob> blob) {
_blob->resize(1); blob->resize(1);
unsigned char magicNumber = MagicNumbers::DIR; unsigned char magicNumber = MagicNumbers::DIR;
_blob->write(&magicNumber, 0, 1); blob->write(&magicNumber, 0, 1);
return make_unique<DirBlob>(std::move(blob));
} }
unsigned char DirBlob::magicNumber() const { unsigned char DirBlob::magicNumber() const {
return magicNumber(*_blob);
}
const unsigned char DirBlob::magicNumber(const blobstore::Blob &blob) {
unsigned char number; unsigned char number;
blob.read(&number, 0, 1); _blob->read(&number, 0, 1);
return number; return number;
} }
bool DirBlob::IsDir(const blobstore::Blob &blob) {
return magicNumber(blob) == MagicNumbers::DIR;
}
unique_ptr<vector<fspp::Dir::Entry>> DirBlob::GetChildren() const { unique_ptr<vector<fspp::Dir::Entry>> DirBlob::GetChildren() const {
Data entries(_blob->size()-1); Data entries(_blob->size()-1);
_blob->read(entries.data(), 1, _blob->size()-1); _blob->read(entries.data(), 1, _blob->size()-1);
@ -97,19 +93,18 @@ void DirBlob::AddChild(const std::string &name, const Key &blobKey, fspp::Dir::E
_blob->write(blobKeyStr.c_str(), oldBlobSize + 1 + name.size() + 1, blobKeyStr.size()+1); _blob->write(blobKeyStr.c_str(), oldBlobSize + 1 + name.size() + 1, blobKeyStr.size()+1);
} }
Key DirBlob::GetBlobKeyForName(const string &name) const { pair<fspp::Dir::EntryType, Key> DirBlob::GetChild(const string &name) const {
auto result = make_unique<vector<string>>();
Data entries(_blob->size()-1); Data entries(_blob->size()-1);
_blob->read(entries.data(), 1, _blob->size()-1); _blob->read(entries.data(), 1, _blob->size()-1);
const char *pos = (const char*)entries.data(); const char *pos = (const char*)entries.data();
while(pos < (const char*)entries.data()+entries.size()) { while(pos < (const char*)entries.data()+entries.size()) {
fspp::Dir::EntryType type = static_cast<fspp::Dir::EntryType>(*reinterpret_cast<const unsigned char*>(pos));
pos += 1; // Skip entry type magic number (whether it is a dir or a file) pos += 1; // Skip entry type magic number (whether it is a dir or a file)
size_t name_length = strlen(pos); size_t name_length = strlen(pos);
if (name_length == name.size() && 0==std::memcmp(pos, name.c_str(), name_length)) { if (name_length == name.size() && 0==std::memcmp(pos, name.c_str(), name_length)) {
pos += strlen(pos) + 1; // Skip name pos += strlen(pos) + 1; // Skip name
return Key::FromString(pos); // Return key return make_pair(type, Key::FromString(pos)); // Return key
} }
pos += strlen(pos) + 1; // Skip name pos += strlen(pos) + 1; // Skip name
pos += strlen(pos) + 1; // Skip key pos += strlen(pos) + 1; // Skip key

View File

@ -14,24 +14,24 @@ namespace cryfs{
class DirBlob { class DirBlob {
public: public:
static std::unique_ptr<DirBlob> InitializeEmptyDir(std::unique_ptr<blobstore::Blob> blob);
DirBlob(std::unique_ptr<blobstore::Blob> blob); DirBlob(std::unique_ptr<blobstore::Blob> blob);
virtual ~DirBlob(); virtual ~DirBlob();
void InitializeEmptyDir();
std::unique_ptr<std::vector<fspp::Dir::Entry>> GetChildren() const; std::unique_ptr<std::vector<fspp::Dir::Entry>> GetChildren() const;
//TODO Use struct instead of pair
std::pair<fspp::Dir::EntryType, blockstore::Key> GetChild(const std::string &name) const;
void AddChildDir(const std::string &name, const blockstore::Key &blobKey); void AddChildDir(const std::string &name, const blockstore::Key &blobKey);
void AddChildFile(const std::string &name, const blockstore::Key &blobKey); void AddChildFile(const std::string &name, const blockstore::Key &blobKey);
blockstore::Key GetBlobKeyForName(const std::string &name) const;
static bool IsDir(const blobstore::Blob &blob);
private: private:
unsigned char magicNumber() const; unsigned char magicNumber() const;
static const unsigned char magicNumber(const blobstore::Blob &blob);
void AddChild(const std::string &name, const blockstore::Key &blobKey, fspp::Dir::EntryType type); void AddChild(const std::string &name, const blockstore::Key &blobKey, fspp::Dir::EntryType type);
const char *readAndAddNextChild(const char *pos, std::vector<fspp::Dir::Entry> *result) const; const char *readAndAddNextChild(const char *pos, std::vector<fspp::Dir::Entry> *result) const;
const char *getStartingPosOfEntry(const char *pos, const std::string &name) const;
std::unique_ptr<blobstore::Blob> _blob; std::unique_ptr<blobstore::Blob> _blob;

View File

@ -4,6 +4,7 @@
#include <messmer/blockstore/utils/Key.h> #include <messmer/blockstore/utils/Key.h>
using std::unique_ptr; using std::unique_ptr;
using std::make_unique;
using blobstore::Blob; using blobstore::Blob;
namespace cryfs { namespace cryfs {
@ -15,26 +16,19 @@ FileBlob::FileBlob(unique_ptr<Blob> blob)
FileBlob::~FileBlob() { FileBlob::~FileBlob() {
} }
void FileBlob::InitializeEmptyFile() { unique_ptr<FileBlob> FileBlob::InitializeEmptyFile(unique_ptr<Blob> blob) {
_blob->resize(1); blob->resize(1);
unsigned char magicNumber = MagicNumbers::FILE; unsigned char magicNumber = MagicNumbers::FILE;
_blob->write(&magicNumber, 0, 1); blob->write(&magicNumber, 0, 1);
return make_unique<FileBlob>(std::move(blob));
} }
unsigned char FileBlob::magicNumber() const { unsigned char FileBlob::magicNumber() const {
return magicNumber(*_blob);
}
unsigned char FileBlob::magicNumber(const blobstore::Blob &blob) {
unsigned char value; unsigned char value;
blob.read(&value, 0, 1); _blob->read(&value, 0, 1);
return value; return value;
} }
bool FileBlob::IsFile(const Blob &blob) {
return magicNumber(blob) == MagicNumbers::FILE;
}
void FileBlob::read(void *target, uint64_t offset, uint64_t count) const { void FileBlob::read(void *target, uint64_t offset, uint64_t count) const {
_blob->read(target, offset + 1, count); _blob->read(target, offset + 1, count);
} }
@ -45,6 +39,10 @@ void FileBlob::write(const void *source, uint64_t offset, uint64_t count) {
blockstore::Key FileBlob::key() const { blockstore::Key FileBlob::key() const {
return _blob->key(); return _blob->key();
} }
void FileBlob::resize(off_t size) {
_blob->resize(size);
}
} }

View File

@ -9,23 +9,22 @@ namespace cryfs {
class FileBlob { class FileBlob {
public: public:
static std::unique_ptr<FileBlob> InitializeEmptyFile(std::unique_ptr<blobstore::Blob> blob);
FileBlob(std::unique_ptr<blobstore::Blob> blob); FileBlob(std::unique_ptr<blobstore::Blob> blob);
virtual ~FileBlob(); virtual ~FileBlob();
static bool IsFile(const blobstore::Blob &blob);
void InitializeEmptyFile();
void read(void *target, uint64_t offset, uint64_t count) const; void read(void *target, uint64_t offset, uint64_t count) const;
void write(const void *source, uint64_t offset, uint64_t count); void write(const void *source, uint64_t offset, uint64_t count);
void resize(off_t size);
blockstore::Key key() const; blockstore::Key key() const;
private: private:
std::unique_ptr<blobstore::Blob> _blob; std::unique_ptr<blobstore::Blob> _blob;
unsigned char magicNumber() const; unsigned char magicNumber() const;
static unsigned char magicNumber(const blobstore::Blob &blob);
}; };
} }