From 43c4f29927164c88658918926f952a74e493cbe7 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sun, 8 Mar 2015 03:51:19 +0100 Subject: [PATCH] Started implementing reading/writing files --- CryDevice.cpp | 6 +++++- CryDevice.h | 1 + CryDir.cpp | 2 +- CryFile.cpp | 8 +++++--- CryFile.h | 3 ++- CryOpenFile.cpp | 14 ++++++++++---- CryOpenFile.h | 4 +++- impl/FileBlob.cpp | 13 +++++++++++++ impl/FileBlob.h | 5 +++++ 9 files changed, 45 insertions(+), 11 deletions(-) diff --git a/CryDevice.cpp b/CryDevice.cpp index 37ada8d0..c79f4c56 100644 --- a/CryDevice.cpp +++ b/CryDevice.cpp @@ -71,7 +71,7 @@ unique_ptr CryDevice::Load(const bf::path &path) { if (DirBlob::IsDir(*currentBlob)) { return make_unique(this, std::move(make_unique(std::move(currentBlob)))); } else if (FileBlob::IsFile(*currentBlob)) { - return make_unique(std::move(make_unique(std::move(currentBlob)))); + return make_unique(this, std::move(make_unique(std::move(currentBlob)))); } else { throw FuseErrnoException(EIO); } @@ -85,4 +85,8 @@ unique_ptr CryDevice::CreateBlob() { return _blobStore->create(); } +unique_ptr CryDevice::LoadBlob(const blockstore::Key &key) { + return _blobStore->load(key); +} + } diff --git a/CryDevice.h b/CryDevice.h index 715d1c60..d0466ac0 100644 --- a/CryDevice.h +++ b/CryDevice.h @@ -25,6 +25,7 @@ public: void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override; std::unique_ptr CreateBlob(); + std::unique_ptr LoadBlob(const blockstore::Key &key); private: blockstore::Key GetOrCreateRootKey(CryConfig *config); diff --git a/CryDir.cpp b/CryDir.cpp index 36591109..2f061aa6 100644 --- a/CryDir.cpp +++ b/CryDir.cpp @@ -41,7 +41,7 @@ unique_ptr CryDir::createFile(const string &name, mode_t mode) { //TODO Do we need a return value in createDir for fspp? If not, change fspp! auto fileblob = make_unique(std::move(child)); fileblob->InitializeEmptyFile(); - return make_unique(std::move(fileblob)); + return make_unique(_device, std::move(fileblob)); } unique_ptr CryDir::createDir(const string &name, mode_t mode) { diff --git a/CryFile.cpp b/CryFile.cpp index 2a4e1ca9..171fda8c 100644 --- a/CryFile.cpp +++ b/CryFile.cpp @@ -15,15 +15,17 @@ using std::make_unique; namespace cryfs { -CryFile::CryFile(unique_ptr blob) -: _blob(std::move(blob)) { +CryFile::CryFile(CryDevice *device, unique_ptr blob) +: _device(device), + _blob(std::move(blob)) { } CryFile::~CryFile() { } unique_ptr CryFile::open(int flags) const { - throw FuseErrnoException(ENOTSUP); + //TODO This is a performance issue because we open the blob twice on the "open" syscall + return make_unique(make_unique(_device->LoadBlob(_blob->key()))); } void CryFile::stat(struct ::stat *result) const { diff --git a/CryFile.h b/CryFile.h index 5334dfd9..4d4e13cb 100644 --- a/CryFile.h +++ b/CryFile.h @@ -11,7 +11,7 @@ namespace cryfs { class CryFile: public fspp::File, CryNode { public: - CryFile(std::unique_ptr blob); + CryFile(CryDevice *device, std::unique_ptr blob); virtual ~CryFile(); void stat(struct ::stat *result) const override; @@ -20,6 +20,7 @@ public: void unlink() override; private: + CryDevice *_device; std::unique_ptr _blob; DISALLOW_COPY_AND_ASSIGN(CryFile); diff --git a/CryOpenFile.cpp b/CryOpenFile.cpp index 6eb3d307..65b9b219 100644 --- a/CryOpenFile.cpp +++ b/CryOpenFile.cpp @@ -5,17 +5,21 @@ #include "CryDevice.h" #include "messmer/fspp/fuse/FuseErrnoException.h" +#include "impl/FileBlob.h" namespace bf = boost::filesystem; +using std::unique_ptr; +using blobstore::Blob; + //TODO Get rid of this in favor of a exception hierarchy using fspp::fuse::CHECK_RETVAL; using fspp::fuse::FuseErrnoException; namespace cryfs { -CryOpenFile::CryOpenFile() { - throw FuseErrnoException(ENOTSUP); +CryOpenFile::CryOpenFile(unique_ptr fileBlob) +: _fileBlob(std::move(fileBlob)) { } CryOpenFile::~CryOpenFile() { @@ -35,11 +39,13 @@ void CryOpenFile::truncate(off_t size) const { } int CryOpenFile::read(void *buf, size_t count, off_t offset) { - throw FuseErrnoException(ENOTSUP); + //TODO Return number of read bytes + _fileBlob->read(buf, offset, count); + return count; } void CryOpenFile::write(const void *buf, size_t count, off_t offset) { - throw FuseErrnoException(ENOTSUP); + _fileBlob->write(buf, offset, count); } void CryOpenFile::fsync() { diff --git a/CryOpenFile.h b/CryOpenFile.h index 0d0e1e49..aadc0752 100644 --- a/CryOpenFile.h +++ b/CryOpenFile.h @@ -7,10 +7,11 @@ namespace cryfs { class CryDevice; +class FileBlob; class CryOpenFile: public fspp::OpenFile { public: - CryOpenFile(); + CryOpenFile(std::unique_ptr fileBlob); virtual ~CryOpenFile(); void stat(struct ::stat *result) const override; @@ -22,6 +23,7 @@ public: void fdatasync() override; private: + std::unique_ptr _fileBlob; DISALLOW_COPY_AND_ASSIGN(CryOpenFile); }; diff --git a/impl/FileBlob.cpp b/impl/FileBlob.cpp index 26302fb9..60c083dc 100644 --- a/impl/FileBlob.cpp +++ b/impl/FileBlob.cpp @@ -1,6 +1,7 @@ #include "FileBlob.h" #include "MagicNumbers.h" +#include using std::unique_ptr; using blobstore::Blob; @@ -34,4 +35,16 @@ bool FileBlob::IsFile(const Blob &blob) { return magicNumber(blob) == MagicNumbers::FILE; } +void FileBlob::read(void *target, uint64_t offset, uint64_t count) const { + _blob->read(target, offset + 1, count); +} + +void FileBlob::write(const void *source, uint64_t offset, uint64_t count) { + _blob->write(source, offset + 1, count); +} + +blockstore::Key FileBlob::key() const { + return _blob->key(); + } + } diff --git a/impl/FileBlob.h b/impl/FileBlob.h index d240235e..4f3c03a5 100644 --- a/impl/FileBlob.h +++ b/impl/FileBlob.h @@ -16,6 +16,11 @@ public: void InitializeEmptyFile(); + void read(void *target, uint64_t offset, uint64_t count) const; + void write(const void *source, uint64_t offset, uint64_t count); + + blockstore::Key key() const; + private: std::unique_ptr _blob;