diff --git a/src/cryfs_lib/CMakeLists.txt b/src/cryfs_lib/CMakeLists.txt index 2d214d86..34c6b20a 100644 --- a/src/cryfs_lib/CMakeLists.txt +++ b/src/cryfs_lib/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(cryfs_lib CryDevice.cpp CryDir.cpp CryFile.cpp CryNode.cpp CryOpenFile.cpp impl/DirBlob.cpp) +add_library(cryfs_lib CryDevice.cpp CryDir.cpp CryFile.cpp CryNode.cpp CryOpenFile.cpp CryConfig.cpp impl/DirBlob.cpp) target_link_libraries(cryfs_lib) diff --git a/src/cryfs_lib/CryConfig.cpp b/src/cryfs_lib/CryConfig.cpp new file mode 100644 index 00000000..61b4b98f --- /dev/null +++ b/src/cryfs_lib/CryConfig.cpp @@ -0,0 +1,47 @@ +#include + +#include +#include +#include + +namespace bf = boost::filesystem; + +using boost::property_tree::ptree; + +namespace cryfs { + +CryConfig::CryConfig(const bf::path &configfile) +:_configfile(configfile), _root_blob("") { + if (bf::exists(_configfile)) { + load(); + } +} + +void CryConfig::load() { + ptree pt; + read_json(_configfile.native(), pt); + + _root_blob = pt.get("cryfs.rootblob", ""); +} + +void CryConfig::save() const { + ptree pt; + + pt.put("cryfs.rootblob", _root_blob); + + write_json(_configfile.native(), pt); +} + +const std::string &CryConfig::RootBlob() const { + return _root_blob; +} + +void CryConfig::SetRootBlob(const std::string &value) { + _root_blob = value; +} + +CryConfig::~CryConfig() { + save(); +} + +} diff --git a/src/cryfs_lib/CryConfig.h b/src/cryfs_lib/CryConfig.h new file mode 100644 index 00000000..12d1d77b --- /dev/null +++ b/src/cryfs_lib/CryConfig.h @@ -0,0 +1,29 @@ +#pragma once +#ifndef CRYFS_LIB_CRYCONFIG_H_ +#define CRYFS_LIB_CRYCONFIG_H_ + +#include + +namespace cryfs { + +class CryConfig { +public: + CryConfig(const boost::filesystem::path &configfile); + virtual ~CryConfig(); + + const std::string &RootBlob() const; + void SetRootBlob(const std::string &value); + +private: + boost::filesystem::path _configfile; + + void load(); + void save() const; + + std::string _root_blob; + +}; + +} + +#endif diff --git a/src/cryfs_lib/CryDevice.cpp b/src/cryfs_lib/CryDevice.cpp index c6d0c144..de7206a6 100644 --- a/src/cryfs_lib/CryDevice.cpp +++ b/src/cryfs_lib/CryDevice.cpp @@ -20,12 +20,19 @@ using blobstore::BlobStore; namespace cryfs { -CryDevice::CryDevice(unique_ptr blobStore) -: _blob_store(std::move(blobStore)), _root_key() { +CryDevice::CryDevice(unique_ptr config, unique_ptr blobStore) +: _blob_store(std::move(blobStore)), _root_key(config->RootBlob()) { + if (_root_key == "") { + _root_key = CreateRootBlobAndReturnKey(); + config->SetRootBlob(_root_key); + } +} + +string CryDevice::CreateRootBlobAndReturnKey() { auto rootBlob = _blob_store->create(DIR_BLOBSIZE); DirBlob rootDir(std::move(rootBlob.blob)); rootDir.InitializeEmptyDir(); - _root_key = rootBlob.key; + return rootBlob.key; } CryDevice::~CryDevice() { diff --git a/src/cryfs_lib/CryDevice.h b/src/cryfs_lib/CryDevice.h index 0b043b34..9fbce741 100644 --- a/src/cryfs_lib/CryDevice.h +++ b/src/cryfs_lib/CryDevice.h @@ -2,6 +2,8 @@ #ifndef CRYFS_LIB_CRYDEVICE_H_ #define CRYFS_LIB_CRYDEVICE_H_ +#include "CryConfig.h" + #include #include @@ -17,7 +19,7 @@ class CryDevice: public fspp::Device { public: static constexpr size_t DIR_BLOBSIZE = 4096; - CryDevice(std::unique_ptr blobStore); + CryDevice(std::unique_ptr config, std::unique_ptr blobStore); virtual ~CryDevice(); void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) override; @@ -25,12 +27,11 @@ public: blobstore::BlobWithKey CreateBlob(size_t size); private: + std::string CreateRootBlobAndReturnKey(); std::unique_ptr Load(const bf::path &path) override; std::unique_ptr _blob_store; - //TODO This takes a new root key each run, the filesystem won't survive restarting the daemon... - // Find a way so that the next run will recognize the created root element. std::string _root_key; DISALLOW_COPY_AND_ASSIGN(CryDevice); diff --git a/src/main.cpp b/src/main.cpp index 0be0e435..bf179de1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,8 @@ int main (int argc, char *argv[]) { printf("Version: %d\n", buildconfig::VERSION::MAJOR); auto blobStore = make_unique(bf::path("/home/heinzi/cryfstest/root")); - cryfs::CryDevice device(std::move(blobStore)); + auto config = make_unique(bf::path("/home/heinzi/cryfstest/config.json")); + cryfs::CryDevice device(std::move(config), std::move(blobStore)); fspp::FilesystemImpl fsimpl(&device); fspp::fuse::Fuse fuse(&fsimpl); fuse.run(argc, argv);