From a5391a854db186e1d5a498bd281b0fa07cde2d9f Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Tue, 21 Jun 2016 16:29:56 -0700 Subject: [PATCH] Use VersionCountingBlockStore --- src/cpp-utils/CMakeLists.txt | 1 + src/cpp-utils/system/homedir.cpp | 25 ++++++++++++++++++++++++ src/cpp-utils/system/homedir.h | 15 +++++++++++++++ src/cryfs/filesystem/CryDevice.cpp | 31 +++++++++++++++++++++++++++--- src/cryfs/filesystem/CryDevice.h | 3 +++ 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/cpp-utils/system/homedir.cpp create mode 100644 src/cpp-utils/system/homedir.h diff --git a/src/cpp-utils/CMakeLists.txt b/src/cpp-utils/CMakeLists.txt index 7eb3da79..ce4b384e 100644 --- a/src/cpp-utils/CMakeLists.txt +++ b/src/cpp-utils/CMakeLists.txt @@ -32,6 +32,7 @@ set(SOURCES assert/backtrace.cpp assert/AssertFailed.cpp system/get_total_memory.cpp + system/homedir.cpp ) add_library(${PROJECT_NAME} STATIC ${SOURCES}) diff --git a/src/cpp-utils/system/homedir.cpp b/src/cpp-utils/system/homedir.cpp new file mode 100644 index 00000000..9e07463a --- /dev/null +++ b/src/cpp-utils/system/homedir.cpp @@ -0,0 +1,25 @@ +#include "homedir.h" +#include +#include + +namespace bf = boost::filesystem; +using std::string; + +namespace cpputils { + namespace system { + bf::path home_directory() { + struct passwd* pwd = getpwuid(getuid()); + string homedir; + if (pwd) { + homedir = pwd->pw_dir; + } else { + // try the $HOME environment variable + homedir = getenv("HOME"); + } + if (homedir == "") { + throw std::runtime_error("Couldn't determine home directory for user"); + } + return homedir; + } + } +} \ No newline at end of file diff --git a/src/cpp-utils/system/homedir.h b/src/cpp-utils/system/homedir.h new file mode 100644 index 00000000..3c8888c8 --- /dev/null +++ b/src/cpp-utils/system/homedir.h @@ -0,0 +1,15 @@ +#pragma once +#ifndef MESSMER_CPPUTILS_SYSTEM_GETTOTALMEMORY_H +#define MESSMER_CPPUTILS_SYSTEM_GETTOTALMEMORY_H + +#include + +namespace cpputils { + namespace system { + + boost::filesystem::path home_directory(); + + } +} + +#endif diff --git a/src/cryfs/filesystem/CryDevice.cpp b/src/cryfs/filesystem/CryDevice.cpp index 743f279d..20d0ab15 100644 --- a/src/cryfs/filesystem/CryDevice.cpp +++ b/src/cryfs/filesystem/CryDevice.cpp @@ -11,9 +11,11 @@ #include #include #include +#include #include "parallelaccessfsblobstore/ParallelAccessFsBlobStore.h" #include "cachingfsblobstore/CachingFsBlobStore.h" #include "../config/CryCipher.h" +#include using std::string; @@ -27,6 +29,7 @@ using blockstore::encrypted::EncryptedBlockStore; using blobstore::onblocks::BlobStoreOnBlocks; using blobstore::onblocks::BlobOnBlocks; using blockstore::caching::CachingBlockStore; +using blockstore::versioncounting::VersionCountingBlockStore; using cpputils::unique_ref; using cpputils::make_unique_ref; using cpputils::dynamic_pointer_move; @@ -52,10 +55,16 @@ CryDevice::CryDevice(CryConfigFile configFile, unique_ref blockStore make_unique_ref( make_unique_ref( make_unique_ref( - CreateEncryptedBlockStore(*configFile.config(), std::move(blockStore)) - ), configFile.config()->BlocksizeBytes()))) + make_unique_ref( + CreateEncryptedBlockStore(*configFile.config(), std::move(blockStore)), + _integrityFilePath(configFile.config()->FilesystemId()) + ) + ) + , configFile.config()->BlocksizeBytes()) + ) ) - ), + ) + ), _rootKey(GetOrCreateRootKey(&configFile)), _onFsAction() { } @@ -66,6 +75,22 @@ Key CryDevice::CreateRootBlobAndReturnKey() { return rootBlob->key(); } +bf::path CryDevice::_integrityFilePath(const CryConfig::FilesystemID &filesystemId) { + bf::path app_dir = cpputils::system::home_directory() / ".cryfs"; + _createDirIfNotExists(app_dir); + bf::path filesystems_dir = app_dir / "filesystems"; + _createDirIfNotExists(filesystems_dir); + bf::path this_filesystem_dir = filesystems_dir / filesystemId.ToString(); + _createDirIfNotExists(this_filesystem_dir); + return this_filesystem_dir / "integritydata.knownblockversions"; +} + +void CryDevice::_createDirIfNotExists(const bf::path &path) { + if (!bf::exists(path)) { + bf::create_directory(path); + } +} + optional> CryDevice::Load(const bf::path &path) { // TODO Split into smaller functions ASSERT(path.is_absolute(), "Non absolute path given"); diff --git a/src/cryfs/filesystem/CryDevice.h b/src/cryfs/filesystem/CryDevice.h index 6848b481..3a595b8d 100644 --- a/src/cryfs/filesystem/CryDevice.h +++ b/src/cryfs/filesystem/CryDevice.h @@ -47,6 +47,9 @@ private: blockstore::Key _rootKey; std::vector> _onFsAction; + static boost::filesystem::path _integrityFilePath(const CryConfig::FilesystemID &filesystemId); + static void _createDirIfNotExists(const boost::filesystem::path &path); + blockstore::Key GetOrCreateRootKey(CryConfigFile *config); blockstore::Key CreateRootBlobAndReturnKey(); static cpputils::unique_ref CreateEncryptedBlockStore(const CryConfig &config, cpputils::unique_ref baseBlockStore);