From 7d40937b9a1d1b9f9e17b2270499f0f3d12350f3 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 25 May 2019 12:27:31 -0700 Subject: [PATCH] Better logging when local state can't be loaded --- ChangeLog.txt | 2 ++ src/cryfs/config/CryConfig.cpp | 1 + src/cryfs/localstate/BasedirMetadata.cpp | 20 ++++++++++++----- src/cryfs/localstate/LocalStateMetadata.cpp | 25 +++++++++++++-------- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 9faa688a..53025964 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,5 +1,7 @@ Version 0.10.2 (unreleased) --------------- +Improvements: +* Better logging when local state can't be loaded Version 0.10.1 diff --git a/src/cryfs/config/CryConfig.cpp b/src/cryfs/config/CryConfig.cpp index 01b49843..14adb360 100644 --- a/src/cryfs/config/CryConfig.cpp +++ b/src/cryfs/config/CryConfig.cpp @@ -40,6 +40,7 @@ CryConfig::CryConfig() CryConfig CryConfig::load(const Data &data) { stringstream stream; data.StoreToStream(stream); + ptree pt; read_json(stream, pt); diff --git a/src/cryfs/localstate/BasedirMetadata.cpp b/src/cryfs/localstate/BasedirMetadata.cpp index 3477a8fa..d32ced93 100644 --- a/src/cryfs/localstate/BasedirMetadata.cpp +++ b/src/cryfs/localstate/BasedirMetadata.cpp @@ -4,6 +4,7 @@ #include #include #include "LocalStateDir.h" +#include namespace bf = boost::filesystem; using boost::property_tree::ptree; @@ -15,20 +16,27 @@ using std::istream; using std::ifstream; using std::ofstream; using std::string; +using namespace cpputils::logging; namespace cryfs { namespace { ptree _load(const bf::path &metadataFilePath) { - ptree result; + try { + ptree result; - ifstream file(metadataFilePath.string()); - if (file.good()) { - read_json(file, result); - } + ifstream file(metadataFilePath.string()); + if (file.good()) { + read_json(file, result); + } - return result; + return result; + } + catch (...) { + LOG(ERR, "Error loading BasedirMetadata"); + throw; + } } void _save(const bf::path &metadataFilePath, const ptree& data) { diff --git a/src/cryfs/localstate/LocalStateMetadata.cpp b/src/cryfs/localstate/LocalStateMetadata.cpp index d87fd050..529a2612 100644 --- a/src/cryfs/localstate/LocalStateMetadata.cpp +++ b/src/cryfs/localstate/LocalStateMetadata.cpp @@ -21,6 +21,7 @@ using cpputils::hash::Hash; using cpputils::Data; using cpputils::Random; namespace bf = boost::filesystem; +using namespace cpputils::logging; namespace cryfs { @@ -106,17 +107,23 @@ void LocalStateMetadata::_serialize(ostream& stream) const { } LocalStateMetadata LocalStateMetadata::_deserialize(istream& stream) { - ptree pt; - read_json(stream, pt); + try { + ptree pt; + read_json(stream, pt); - uint32_t myClientId = pt.get("myClientId"); - string encryptionKeySalt = pt.get("encryptionKey.salt"); - string encryptionKeyDigest = pt.get("encryptionKey.hash"); + uint32_t myClientId = pt.get("myClientId"); + string encryptionKeySalt = pt.get("encryptionKey.salt"); + string encryptionKeyDigest = pt.get("encryptionKey.hash"); - return LocalStateMetadata(myClientId, Hash{ - /*.digest = */ cpputils::hash::Digest::FromString(encryptionKeyDigest), - /*.salt = */ cpputils::hash::Salt::FromString(encryptionKeySalt) - }); + return LocalStateMetadata(myClientId, Hash{ + /*.digest = */ cpputils::hash::Digest::FromString(encryptionKeyDigest), + /*.salt = */ cpputils::hash::Salt::FromString(encryptionKeySalt) + }); + } + catch (...) { + LOG(ERR, "Error loading LocalStateMetadata"); + throw; + } } }