From 95997d27106daf91336847f50efaaa32279b7fc7 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 16 Oct 2023 17:54:05 +0800 Subject: fix: check and update copyright at files --- src/core/function/gpg/GpgKeyGetter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index ee6d2b09..6401e750 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2021 Saturneric + * Copyright (C) 2021 Saturneric * * This file is part of GpgFrontend. * @@ -20,7 +20,7 @@ * the gpg4usb project, which is under GPL-3.0-or-later. * * All the source code of GpgFrontend was modified and released by - * Saturneric starting on May 12, 2021. + * Saturneric starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * -- cgit v1.2.3 From fd46d4667611c0db9cea3f06205727399b6fb5fd Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 29 Oct 2023 02:46:15 +0800 Subject: refactor: start to tidy up code using clang-tidy --- src/core/function/gpg/GpgKeyGetter.cpp | 263 ++++++++++++++++++++------------- 1 file changed, 163 insertions(+), 100 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 6401e750..09c77c28 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -32,136 +32,199 @@ #include #include -#include -#include "GpgConstants.h" -#include "model/GpgKey.h" +#include "core/GpgConstants.h" +#include "core/GpgContext.h" -GpgFrontend::GpgKeyGetter::GpgKeyGetter(int channel) - : SingletonFunctionObject(channel) { - SPDLOG_DEBUG("called channel: {}", channel); -} +namespace GpgFrontend { -GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetKey(const std::string& fpr, - bool use_cache) { - // find in cache first - if (use_cache) { - auto key = get_key_in_cache(fpr); - if (key.IsGood()) return key; +class GpgKeyGetter::Impl : public SingletonFunctionObject { + public: + explicit Impl(int channel) + : SingletonFunctionObject(channel) { + SPDLOG_DEBUG("called channel: {}", channel); } - gpgme_key_t _p_key = nullptr; - gpgme_get_key(ctx_, fpr.c_str(), &_p_key, 1); - if (_p_key == nullptr) { - SPDLOG_WARN("GpgKeyGetter GetKey Private _p_key Null fpr", fpr); - return GetPubkey(fpr); - } else { - return GpgKey(std::move(_p_key)); - } -} + auto GetKey(const std::string& fpr, bool use_cache) -> GpgKey { + // find in cache first + if (use_cache) { + auto key = get_key_in_cache(fpr); + if (key.IsGood()) return key; + } -GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::GetPubkey(const std::string& fpr, - bool use_cache) { - // find in cache first - if (use_cache) { - auto key = get_key_in_cache(fpr); - if (key.IsGood()) return key; + gpgme_key_t p_key = nullptr; + gpgme_get_key(ctx_, fpr.c_str(), &p_key, 1); + if (p_key == nullptr) { + SPDLOG_WARN("GpgKeyGetter GetKey Private _p_key Null fpr", fpr); + return GetPubkey(fpr, true); + } + return GpgKey(std::move(p_key)); } - gpgme_key_t _p_key = nullptr; - gpgme_get_key(ctx_, fpr.c_str(), &_p_key, 0); - if (_p_key == nullptr) SPDLOG_WARN("GpgKeyGetter GetKey _p_key Null", fpr); - return GpgKey(std::move(_p_key)); -} + auto GetPubkey(const std::string& fpr, bool use_cache) -> GpgKey { + // find in cache first + if (use_cache) { + auto key = get_key_in_cache(fpr); + if (key.IsGood()) return key; + } -GpgFrontend::KeyLinkListPtr GpgFrontend::GpgKeyGetter::FetchKey() { - // get the lock - std::lock_guard lock(keys_cache_mutex_); + gpgme_key_t p_key = nullptr; + gpgme_get_key(ctx_, fpr.c_str(), &p_key, 0); + if (p_key == nullptr) SPDLOG_WARN("GpgKeyGetter GetKey _p_key Null", fpr); + return GpgKey(std::move(p_key)); + } + + auto FetchKey() -> KeyLinkListPtr { + // get the lock + std::lock_guard lock(keys_cache_mutex_); - auto keys_list = std::make_unique(); + auto keys_list = std::make_unique(); - for (const auto& [key, value] : keys_cache_) { - keys_list->push_back(value.Copy()); + for (const auto& [key, value] : keys_cache_) { + keys_list->push_back(value.Copy()); + } + return keys_list; } - return keys_list; -} -void GpgFrontend::GpgKeyGetter::FlushKeyCache() { - SPDLOG_DEBUG("called channel id: {}", GetChannel()); + void FlushKeyCache() { + SPDLOG_DEBUG("called channel id: {}", GetChannel()); - // clear the keys cache - keys_cache_.clear(); + // clear the keys cache + keys_cache_.clear(); - // init - GpgError err = gpgme_op_keylist_start(ctx_, nullptr, 0); + // init + GpgError err = gpgme_op_keylist_start(ctx_, nullptr, 0); - // for debug - assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); + // for debug + assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - // return when error - if (check_gpg_error_2_err_code(err) != GPG_ERR_NO_ERROR) return; + // return when error + if (check_gpg_error_2_err_code(err) != GPG_ERR_NO_ERROR) return; - { - // get the lock - std::lock_guard lock(keys_cache_mutex_); - gpgme_key_t key; - while ((err = gpgme_op_keylist_next(ctx_, &key)) == GPG_ERR_NO_ERROR) { - auto gpg_key = GpgKey(std::move(key)); - - // detect if the key is in a smartcard - // if so, try to get full information using gpgme_get_key() - // this maybe a bug in gpgme - if (gpg_key.IsHasCardKey()) { - gpg_key = GetKey(gpg_key.GetId(), false); - } + { + // get the lock + std::lock_guard lock(keys_cache_mutex_); + gpgme_key_t key; + while ((err = gpgme_op_keylist_next(ctx_, &key)) == GPG_ERR_NO_ERROR) { + auto gpg_key = GpgKey(std::move(key)); - keys_cache_.insert({gpg_key.GetId(), std::move(gpg_key)}); + // detect if the key is in a smartcard + // if so, try to get full information using gpgme_get_key() + // this maybe a bug in gpgme + if (gpg_key.IsHasCardKey()) { + gpg_key = GetKey(gpg_key.GetId(), false); + } + + keys_cache_.insert({gpg_key.GetId(), std::move(gpg_key)}); + } } + + SPDLOG_DEBUG("cache address: {} object address: {}", + static_cast(&keys_cache_), static_cast(this)); + + // for debug + assert(check_gpg_error_2_err_code(err, GPG_ERR_EOF) == GPG_ERR_EOF); + + err = gpgme_op_keylist_end(ctx_); + assert(check_gpg_error_2_err_code(err, GPG_ERR_EOF) == GPG_ERR_NO_ERROR); } - SPDLOG_DEBUG("cache address: {} object address: {}", - static_cast(&keys_cache_), static_cast(this)); + auto GetKeys(const KeyIdArgsListPtr& ids) -> KeyListPtr { + auto keys = std::make_unique(); + for (const auto& key_id : *ids) keys->emplace_back(GetKey(key_id, true)); + return keys; + } + + auto GetKeysCopy(const KeyLinkListPtr& keys) -> KeyLinkListPtr { + // get the lock + std::lock_guard lock(ctx_mutex_); + auto keys_copy = std::make_unique(); + for (const auto& key : *keys) keys_copy->emplace_back(key.Copy()); + return keys_copy; + } - // for debug - assert(check_gpg_error_2_err_code(err, GPG_ERR_EOF) == GPG_ERR_EOF); + auto GetKeysCopy(const KeyListPtr& keys) -> KeyListPtr { + // get the lock + std::lock_guard lock(ctx_mutex_); + auto keys_copy = std::make_unique(); + for (const auto& key : *keys) keys_copy->emplace_back(key.Copy()); + return keys_copy; + } + + private: + /** + * @brief Get the gpgme context object + * + */ + GpgContext& ctx_ = + GpgContext::GetInstance(SingletonFunctionObject::GetChannel()); + + /** + * @brief shared mutex for the keys cache + * + */ + mutable std::mutex ctx_mutex_; + + /** + * @brief cache the keys with key id + * + */ + std::map keys_cache_; + + /** + * @brief shared mutex for the keys cache + * + */ + mutable std::mutex keys_cache_mutex_; + + /** + * @brief Get the Key object + * + * @param id + * @return GpgKey + */ + auto get_key_in_cache(const std::string& key_id) -> GpgKey { + std::lock_guard lock(keys_cache_mutex_); + if (keys_cache_.find(key_id) != keys_cache_.end()) { + std::lock_guard lock(ctx_mutex_); + // return a copy of the key in cache + return keys_cache_[key_id].Copy(); + } - err = gpgme_op_keylist_end(ctx_); - assert(check_gpg_error_2_err_code(err, GPG_ERR_EOF) == GPG_ERR_NO_ERROR); + // return a bad key + return {}; + } +}; + +GpgKeyGetter::GpgKeyGetter(int channel) + : SingletonFunctionObject(channel), + p_(std::make_unique(channel)) { + SPDLOG_DEBUG("called channel: {}", channel); } -GpgFrontend::KeyListPtr GpgFrontend::GpgKeyGetter::GetKeys( - const KeyIdArgsListPtr& ids) { - auto keys = std::make_unique(); - for (const auto& id : *ids) keys->emplace_back(GetKey(id)); - return keys; +auto GpgKeyGetter::GetKey(const std::string& key_id, bool use_cache) -> GpgKey { + return p_->GetKey(key_id, use_cache); } -GpgFrontend::KeyLinkListPtr GpgFrontend::GpgKeyGetter::GetKeysCopy( - const GpgFrontend::KeyLinkListPtr& keys) { - // get the lock - std::lock_guard lock(ctx_mutex_); - auto keys_copy = std::make_unique(); - for (const auto& key : *keys) keys_copy->emplace_back(key.Copy()); - return keys_copy; +auto GpgKeyGetter::GetPubkey(const std::string& key_id, bool use_cache) + -> GpgKey { + return p_->GetPubkey(key_id, use_cache); } -GpgFrontend::KeyListPtr GpgFrontend::GpgKeyGetter::GetKeysCopy( - const GpgFrontend::KeyListPtr& keys) { - // get the lock - std::lock_guard lock(ctx_mutex_); - auto keys_copy = std::make_unique(); - for (const auto& key : *keys) keys_copy->emplace_back(key.Copy()); - return keys_copy; +void GpgKeyGetter::FlushKeyCache() { p_->FlushKeyCache(); } + +auto GpgKeyGetter::GetKeys(const KeyIdArgsListPtr& ids) -> KeyListPtr { + return p_->GetKeys(ids); } -GpgFrontend::GpgKey GpgFrontend::GpgKeyGetter::get_key_in_cache( - const std::string& id) { - std::lock_guard lock(keys_cache_mutex_); - if (keys_cache_.find(id) != keys_cache_.end()) { - std::lock_guard lock(ctx_mutex_); - // return a copy of the key in cache - return keys_cache_[id].Copy(); - } - // return a bad key - return GpgKey(); +auto GpgKeyGetter::GetKeysCopy(const KeyLinkListPtr& keys) -> KeyLinkListPtr { + return p_->GetKeysCopy(keys); +} + +auto GpgKeyGetter::GetKeysCopy(const KeyListPtr& keys) -> KeyListPtr { + return p_->GetKeysCopy(keys); } + +auto GpgKeyGetter::FetchKey() -> KeyLinkListPtr { return p_->FetchKey(); } + +} // namespace GpgFrontend -- cgit v1.2.3 From 5d7b1d5493df8723259eca0613a9ce0af6077289 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 30 Oct 2023 14:52:29 +0800 Subject: style: improve code style of core --- src/core/function/gpg/GpgKeyGetter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 09c77c28..070699fa 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -96,10 +96,10 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { GpgError err = gpgme_op_keylist_start(ctx_, nullptr, 0); // for debug - assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); + assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); // return when error - if (check_gpg_error_2_err_code(err) != GPG_ERR_NO_ERROR) return; + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) return; { // get the lock @@ -123,10 +123,10 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { static_cast(&keys_cache_), static_cast(this)); // for debug - assert(check_gpg_error_2_err_code(err, GPG_ERR_EOF) == GPG_ERR_EOF); + assert(CheckGpgError2ErrCode(err, GPG_ERR_EOF) == GPG_ERR_EOF); err = gpgme_op_keylist_end(ctx_); - assert(check_gpg_error_2_err_code(err, GPG_ERR_EOF) == GPG_ERR_NO_ERROR); + assert(CheckGpgError2ErrCode(err, GPG_ERR_EOF) == GPG_ERR_NO_ERROR); } auto GetKeys(const KeyIdArgsListPtr& ids) -> KeyListPtr { -- cgit v1.2.3 From 0251f35c93e3f0e0a6853a50fb5bd82c1b9e4187 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 6 Nov 2023 17:17:47 +0800 Subject: refactor: clean up core's codes --- src/core/function/gpg/GpgKeyGetter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 070699fa..4919ce05 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -34,7 +34,7 @@ #include #include "core/GpgConstants.h" -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" namespace GpgFrontend { -- cgit v1.2.3 From 889cb8092381b073a0f4a0411a4ede04cd7bdd14 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 6 Nov 2023 20:49:44 +0800 Subject: refactor: improve the code structure of core --- src/core/function/gpg/GpgKeyGetter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 4919ce05..73d40a0a 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -33,8 +33,8 @@ #include #include -#include "core/GpgConstants.h" #include "core/function/gpg/GpgContext.h" +#include "core/utils/GpgUtils.h" namespace GpgFrontend { -- cgit v1.2.3 From 4dcd2ac8c4f673fc21c4cf0072d6cb648ca64e7e Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 7 Nov 2023 15:57:28 +0800 Subject: refactor: separate typedef and impl --- src/core/function/gpg/GpgKeyGetter.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 73d40a0a..0b1c7c44 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -33,6 +33,7 @@ #include #include +#include "core/GpgModel.h" #include "core/function/gpg/GpgContext.h" #include "core/utils/GpgUtils.h" -- cgit v1.2.3 From 883db05d54510e76b6548e107593187e1306117d Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 04:28:46 -0800 Subject: feat: general improvements of aync execution and memory security --- src/core/function/gpg/GpgKeyGetter.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 0b1c7c44..5215f34b 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -203,6 +203,8 @@ GpgKeyGetter::GpgKeyGetter(int channel) SPDLOG_DEBUG("called channel: {}", channel); } +GpgKeyGetter::~GpgKeyGetter() = default; + auto GpgKeyGetter::GetKey(const std::string& key_id, bool use_cache) -> GpgKey { return p_->GetKey(key_id, use_cache); } -- cgit v1.2.3 From d0602f09564d3d200b20e83a977315134ced842e Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 05:01:27 -0800 Subject: fix: slove issues in key/subkey generation --- src/core/function/gpg/GpgKeyGetter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 5215f34b..cad2d884 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -82,7 +82,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { auto keys_list = std::make_unique(); for (const auto& [key, value] : keys_cache_) { - keys_list->push_back(value.Copy()); + keys_list->push_back(value); } return keys_list; } @@ -140,7 +140,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { // get the lock std::lock_guard lock(ctx_mutex_); auto keys_copy = std::make_unique(); - for (const auto& key : *keys) keys_copy->emplace_back(key.Copy()); + for (const auto& key : *keys) keys_copy->emplace_back(key); return keys_copy; } @@ -148,7 +148,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { // get the lock std::lock_guard lock(ctx_mutex_); auto keys_copy = std::make_unique(); - for (const auto& key : *keys) keys_copy->emplace_back(key.Copy()); + for (const auto& key : *keys) keys_copy->emplace_back(key); return keys_copy; } @@ -189,7 +189,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { if (keys_cache_.find(key_id) != keys_cache_.end()) { std::lock_guard lock(ctx_mutex_); // return a copy of the key in cache - return keys_cache_[key_id].Copy(); + return keys_cache_[key_id]; } // return a bad key -- cgit v1.2.3 From 054e6e28cca2517dda2319ef683314b3318c39a6 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 12:25:21 -0800 Subject: feat: standarized and speed up app env loading process --- src/core/function/gpg/GpgKeyGetter.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index cad2d884..a60b66c9 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -54,7 +54,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } gpgme_key_t p_key = nullptr; - gpgme_get_key(ctx_, fpr.c_str(), &p_key, 1); + gpgme_get_key(ctx_.DefaultContext(), fpr.c_str(), &p_key, 1); if (p_key == nullptr) { SPDLOG_WARN("GpgKeyGetter GetKey Private _p_key Null fpr", fpr); return GetPubkey(fpr, true); @@ -70,7 +70,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } gpgme_key_t p_key = nullptr; - gpgme_get_key(ctx_, fpr.c_str(), &p_key, 0); + gpgme_get_key(ctx_.DefaultContext(), fpr.c_str(), &p_key, 0); if (p_key == nullptr) SPDLOG_WARN("GpgKeyGetter GetKey _p_key Null", fpr); return GpgKey(std::move(p_key)); } @@ -88,13 +88,13 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } void FlushKeyCache() { - SPDLOG_DEBUG("called channel id: {}", GetChannel()); + SPDLOG_DEBUG("flush key channel called, channel: {}", GetChannel()); // clear the keys cache keys_cache_.clear(); // init - GpgError err = gpgme_op_keylist_start(ctx_, nullptr, 0); + GpgError err = gpgme_op_keylist_start(ctx_.DefaultContext(), nullptr, 0); // for debug assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); @@ -106,7 +106,8 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { // get the lock std::lock_guard lock(keys_cache_mutex_); gpgme_key_t key; - while ((err = gpgme_op_keylist_next(ctx_, &key)) == GPG_ERR_NO_ERROR) { + while ((err = gpgme_op_keylist_next(ctx_.DefaultContext(), &key)) == + GPG_ERR_NO_ERROR) { auto gpg_key = GpgKey(std::move(key)); // detect if the key is in a smartcard @@ -120,14 +121,16 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } } - SPDLOG_DEBUG("cache address: {} object address: {}", + SPDLOG_DEBUG("flush key channel cache address: {} object address: {}", static_cast(&keys_cache_), static_cast(this)); // for debug assert(CheckGpgError2ErrCode(err, GPG_ERR_EOF) == GPG_ERR_EOF); - err = gpgme_op_keylist_end(ctx_); + err = gpgme_op_keylist_end(ctx_.DefaultContext()); assert(CheckGpgError2ErrCode(err, GPG_ERR_EOF) == GPG_ERR_NO_ERROR); + + SPDLOG_DEBUG("flush key channel done, channel: {}", GetChannel()); } auto GetKeys(const KeyIdArgsListPtr& ids) -> KeyListPtr { -- cgit v1.2.3 From 33544f343df4e834730d9b0c0916433a51738d55 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 4 Dec 2023 05:43:03 -0800 Subject: feat: add reasons explanation to env loading failed message box --- src/core/function/gpg/GpgKeyGetter.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index a60b66c9..0cd6741c 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -87,7 +87,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { return keys_list; } - void FlushKeyCache() { + auto FlushKeyCache() -> bool { SPDLOG_DEBUG("flush key channel called, channel: {}", GetChannel()); // clear the keys cache @@ -100,7 +100,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); // return when error - if (CheckGpgError(err) != GPG_ERR_NO_ERROR) return; + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) return false; { // get the lock @@ -131,6 +131,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { assert(CheckGpgError2ErrCode(err, GPG_ERR_EOF) == GPG_ERR_NO_ERROR); SPDLOG_DEBUG("flush key channel done, channel: {}", GetChannel()); + return true; } auto GetKeys(const KeyIdArgsListPtr& ids) -> KeyListPtr { @@ -217,7 +218,7 @@ auto GpgKeyGetter::GetPubkey(const std::string& key_id, bool use_cache) return p_->GetPubkey(key_id, use_cache); } -void GpgKeyGetter::FlushKeyCache() { p_->FlushKeyCache(); } +auto GpgKeyGetter::FlushKeyCache() -> bool { return p_->FlushKeyCache(); } auto GpgKeyGetter::GetKeys(const KeyIdArgsListPtr& ids) -> KeyListPtr { return p_->GetKeys(ids); -- cgit v1.2.3 From 37215a895a649345165027971690dfdcd9106a32 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 15 Dec 2023 21:53:03 -0800 Subject: fix: use secure memory management at impl class --- src/core/function/gpg/GpgKeyGetter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 0cd6741c..8ce3c4fa 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -203,7 +203,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { GpgKeyGetter::GpgKeyGetter(int channel) : SingletonFunctionObject(channel), - p_(std::make_unique(channel)) { + p_(SecureCreateUniqueObject(channel)) { SPDLOG_DEBUG("called channel: {}", channel); } -- cgit v1.2.3 From 08dd9b43481d189c60acc58941005dc25a58c77f Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 15 Dec 2023 23:11:32 -0800 Subject: fix: repair test cases --- src/core/function/gpg/GpgKeyGetter.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 8ce3c4fa..de8895d9 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -76,13 +76,18 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } auto FetchKey() -> KeyLinkListPtr { - // get the lock - std::lock_guard lock(keys_cache_mutex_); + if (keys_cache_.empty()) { + FlushKeyCache(); + } auto keys_list = std::make_unique(); - for (const auto& [key, value] : keys_cache_) { - keys_list->push_back(value); + { + // get the lock + std::lock_guard lock(keys_cache_mutex_); + for (const auto& [key, value] : keys_cache_) { + keys_list->push_back(value); + } } return keys_list; } -- cgit v1.2.3 From 644aa4397b03dbef73f8bfedc13925b51cad836b Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 20:55:15 +0800 Subject: feat: integrate logging api to core --- src/core/function/gpg/GpgKeyGetter.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index de8895d9..5c5ed039 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -43,7 +43,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { public: explicit Impl(int channel) : SingletonFunctionObject(channel) { - SPDLOG_DEBUG("called channel: {}", channel); + GF_CORE_LOG_DEBUG("called channel: {}", channel); } auto GetKey(const std::string& fpr, bool use_cache) -> GpgKey { @@ -56,7 +56,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { gpgme_key_t p_key = nullptr; gpgme_get_key(ctx_.DefaultContext(), fpr.c_str(), &p_key, 1); if (p_key == nullptr) { - SPDLOG_WARN("GpgKeyGetter GetKey Private _p_key Null fpr", fpr); + GF_CORE_LOG_WARN("GpgKeyGetter GetKey Private _p_key Null fpr", fpr); return GetPubkey(fpr, true); } return GpgKey(std::move(p_key)); @@ -71,7 +71,8 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { gpgme_key_t p_key = nullptr; gpgme_get_key(ctx_.DefaultContext(), fpr.c_str(), &p_key, 0); - if (p_key == nullptr) SPDLOG_WARN("GpgKeyGetter GetKey _p_key Null", fpr); + if (p_key == nullptr) + GF_CORE_LOG_WARN("GpgKeyGetter GetKey _p_key Null", fpr); return GpgKey(std::move(p_key)); } @@ -93,7 +94,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } auto FlushKeyCache() -> bool { - SPDLOG_DEBUG("flush key channel called, channel: {}", GetChannel()); + GF_CORE_LOG_DEBUG("flush key channel called, channel: {}", GetChannel()); // clear the keys cache keys_cache_.clear(); @@ -126,8 +127,9 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } } - SPDLOG_DEBUG("flush key channel cache address: {} object address: {}", - static_cast(&keys_cache_), static_cast(this)); + GF_CORE_LOG_DEBUG("flush key channel cache address: {} object address: {}", + static_cast(&keys_cache_), + static_cast(this)); // for debug assert(CheckGpgError2ErrCode(err, GPG_ERR_EOF) == GPG_ERR_EOF); @@ -135,7 +137,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { err = gpgme_op_keylist_end(ctx_.DefaultContext()); assert(CheckGpgError2ErrCode(err, GPG_ERR_EOF) == GPG_ERR_NO_ERROR); - SPDLOG_DEBUG("flush key channel done, channel: {}", GetChannel()); + GF_CORE_LOG_DEBUG("flush key channel done, channel: {}", GetChannel()); return true; } @@ -209,7 +211,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { GpgKeyGetter::GpgKeyGetter(int channel) : SingletonFunctionObject(channel), p_(SecureCreateUniqueObject(channel)) { - SPDLOG_DEBUG("called channel: {}", channel); + GF_CORE_LOG_DEBUG("called channel: {}", channel); } GpgKeyGetter::~GpgKeyGetter() = default; -- cgit v1.2.3 From bf538056b24a68b8fd235b1c50991ee8eb46a776 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Jan 2024 14:02:37 +0800 Subject: refactor: use QString instead of std::string and improve threading system --- src/core/function/gpg/GpgKeyGetter.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 5c5ed039..e22979d7 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -46,7 +46,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { GF_CORE_LOG_DEBUG("called channel: {}", channel); } - auto GetKey(const std::string& fpr, bool use_cache) -> GpgKey { + auto GetKey(const QString& fpr, bool use_cache) -> GpgKey { // find in cache first if (use_cache) { auto key = get_key_in_cache(fpr); @@ -54,7 +54,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } gpgme_key_t p_key = nullptr; - gpgme_get_key(ctx_.DefaultContext(), fpr.c_str(), &p_key, 1); + gpgme_get_key(ctx_.DefaultContext(), fpr.toUtf8(), &p_key, 1); if (p_key == nullptr) { GF_CORE_LOG_WARN("GpgKeyGetter GetKey Private _p_key Null fpr", fpr); return GetPubkey(fpr, true); @@ -62,7 +62,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { return GpgKey(std::move(p_key)); } - auto GetPubkey(const std::string& fpr, bool use_cache) -> GpgKey { + auto GetPubkey(const QString& fpr, bool use_cache) -> GpgKey { // find in cache first if (use_cache) { auto key = get_key_in_cache(fpr); @@ -70,7 +70,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } gpgme_key_t p_key = nullptr; - gpgme_get_key(ctx_.DefaultContext(), fpr.c_str(), &p_key, 0); + gpgme_get_key(ctx_.DefaultContext(), fpr.toUtf8(), &p_key, 0); if (p_key == nullptr) GF_CORE_LOG_WARN("GpgKeyGetter GetKey _p_key Null", fpr); return GpgKey(std::move(p_key)); @@ -181,7 +181,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { * @brief cache the keys with key id * */ - std::map keys_cache_; + std::map keys_cache_; /** * @brief shared mutex for the keys cache @@ -195,7 +195,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { * @param id * @return GpgKey */ - auto get_key_in_cache(const std::string& key_id) -> GpgKey { + auto get_key_in_cache(const QString& key_id) -> GpgKey { std::lock_guard lock(keys_cache_mutex_); if (keys_cache_.find(key_id) != keys_cache_.end()) { std::lock_guard lock(ctx_mutex_); @@ -216,12 +216,11 @@ GpgKeyGetter::GpgKeyGetter(int channel) GpgKeyGetter::~GpgKeyGetter() = default; -auto GpgKeyGetter::GetKey(const std::string& key_id, bool use_cache) -> GpgKey { +auto GpgKeyGetter::GetKey(const QString& key_id, bool use_cache) -> GpgKey { return p_->GetKey(key_id, use_cache); } -auto GpgKeyGetter::GetPubkey(const std::string& key_id, bool use_cache) - -> GpgKey { +auto GpgKeyGetter::GetPubkey(const QString& key_id, bool use_cache) -> GpgKey { return p_->GetPubkey(key_id, use_cache); } -- cgit v1.2.3 From 5baef3c4a3b947d3275e9ce44cfb7a68984f6cca Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 19 Jan 2024 21:54:26 +0800 Subject: fix: solve discovered bugs and improve ui operations --- src/core/function/gpg/GpgKeyGetter.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src/core/function/gpg/GpgKeyGetter.cpp') diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index e22979d7..4a35d3cd 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -77,17 +77,16 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { } auto FetchKey() -> KeyLinkListPtr { - if (keys_cache_.empty()) { + if (keys_search_cache_.empty()) { FlushKeyCache(); } auto keys_list = std::make_unique(); - { // get the lock std::lock_guard lock(keys_cache_mutex_); - for (const auto& [key, value] : keys_cache_) { - keys_list->push_back(value); + for (const auto& key : keys_cache_) { + keys_list->push_back(key); } } return keys_list; @@ -98,6 +97,7 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { // clear the keys cache keys_cache_.clear(); + keys_search_cache_.clear(); // init GpgError err = gpgme_op_keylist_start(ctx_.DefaultContext(), nullptr, 0); @@ -123,12 +123,14 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { gpg_key = GetKey(gpg_key.GetId(), false); } - keys_cache_.insert({gpg_key.GetId(), std::move(gpg_key)}); + keys_cache_.push_back(gpg_key); + keys_search_cache_.insert(gpg_key.GetId(), gpg_key); + keys_search_cache_.insert(gpg_key.GetFingerprint(), gpg_key); } } GF_CORE_LOG_DEBUG("flush key channel cache address: {} object address: {}", - static_cast(&keys_cache_), + static_cast(&keys_search_cache_), static_cast(this)); // for debug @@ -181,7 +183,13 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { * @brief cache the keys with key id * */ - std::map keys_cache_; + QMap keys_search_cache_; + + /** + * @brief + * + */ + QList keys_cache_; /** * @brief shared mutex for the keys cache @@ -197,10 +205,10 @@ class GpgKeyGetter::Impl : public SingletonFunctionObject { */ auto get_key_in_cache(const QString& key_id) -> GpgKey { std::lock_guard lock(keys_cache_mutex_); - if (keys_cache_.find(key_id) != keys_cache_.end()) { + if (keys_search_cache_.find(key_id) != keys_search_cache_.end()) { std::lock_guard lock(ctx_mutex_); // return a copy of the key in cache - return keys_cache_[key_id]; + return keys_search_cache_[key_id]; } // return a bad key -- cgit v1.2.3