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/GpgFileOpera.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 30678cf0..cb919042 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.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 5175b3ae6687839afa2cdfe01f2fd70d714024ed Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 17 Oct 2023 04:19:26 +0800 Subject: refactor: use c++17 features and piml tech to rewrite DataObject and Task --- src/core/function/gpg/GpgFileOpera.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index cb919042..de50de62 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -55,7 +55,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( throw std::runtime_error("read file error"); } - std::unique_ptr out_buffer = nullptr; + ByteArrayPtr out_buffer = nullptr; auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( std::move(keys), in_buffer, out_buffer, result); @@ -85,7 +85,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } - std::unique_ptr out_buffer; + ByteArrayPtr out_buffer; auto err = GpgBasicOperator::GetInstance().Decrypt(in_buffer, out_buffer, result); @@ -119,7 +119,7 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } - std::unique_ptr out_buffer; + ByteArrayPtr out_buffer; auto err = GpgBasicOperator::GetInstance(_channel).Sign( std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); @@ -149,7 +149,7 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile( if (!FileOperator::ReadFileStd(data_path_std, in_buffer)) { throw std::runtime_error("read file error"); } - std::unique_ptr sign_buffer = nullptr; + ByteArrayPtr sign_buffer = nullptr; if (!sign_path.empty()) { std::string sign_buffer_str; if (!FileOperator::ReadFileStd(sign_path_std, sign_buffer_str)) { @@ -180,7 +180,7 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } - std::unique_ptr out_buffer = nullptr; + ByteArrayPtr out_buffer = nullptr; auto err = GpgBasicOperator::GetInstance(_channel).EncryptSign( std::move(keys), std::move(signer_keys), in_buffer, out_buffer, encr_res, @@ -212,7 +212,7 @@ gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( throw std::runtime_error("read file error"); } - std::unique_ptr out_buffer = nullptr; + ByteArrayPtr out_buffer = nullptr; auto err = GpgBasicOperator::GetInstance().DecryptVerify( in_buffer, out_buffer, decr_res, verify_res); @@ -241,7 +241,7 @@ unsigned int GpgFrontend::GpgFileOpera::EncryptFileSymmetric( throw std::runtime_error("read file error"); } - std::unique_ptr out_buffer; + ByteArrayPtr out_buffer; auto err = GpgBasicOperator::GetInstance(_channel).EncryptSymmetric( in_buffer, out_buffer, result); -- 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/GpgFileOpera.cpp | 62 +++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index de50de62..199cf3d7 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -27,19 +27,15 @@ */ #include "GpgFileOpera.h" -#include -#include - -#include "GpgBasicOperator.h" -#include "GpgConstants.h" -#include "function/FileOperator.h" - -GpgFrontend::GpgFileOpera::GpgFileOpera(int channel) - : SingletonFunctionObject(channel) {} - -GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( - KeyListPtr keys, const std::string& in_path, const std::string& out_path, - GpgEncrResult& result, int _channel) { +#include "core/GpgConstants.h" +#include "core/function/FileOperator.h" +#include "core/function/gpg/GpgBasicOperator.h" + +auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, + const std::string& in_path, + const std::string& out_path, + GpgEncrResult& result, int _channel) + -> GpgFrontend::GpgError { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -68,9 +64,10 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( return err; } -GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( - const std::string& in_path, const std::string& out_path, - GpgDecrResult& result) { +auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, + const std::string& out_path, + GpgDecrResult& result) + -> GpgFrontend::GpgError { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -100,11 +97,11 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( return err; } -gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, - const std::string& in_path, - const std::string& out_path, - GpgSignResult& result, - int _channel) { +auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, + const std::string& in_path, + const std::string& out_path, + GpgSignResult& result, int _channel) + -> gpgme_error_t { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -132,9 +129,10 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, return err; } -gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile( - const std::string& data_path, const std::string& sign_path, - GpgVerifyResult& result, int _channel) { +auto GpgFrontend::GpgFileOpera::VerifyFile(const std::string& data_path, + const std::string& sign_path, + GpgVerifyResult& result, + int _channel) -> gpgme_error_t { #ifdef WINDOWS auto data_path_std = std::filesystem::path(QString::fromStdString(data_path).toStdU16String()); @@ -162,10 +160,10 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile( return err; } -gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( +auto GpgFrontend::GpgFileOpera::EncryptSignFile( KeyListPtr keys, KeyListPtr signer_keys, const std::string& in_path, const std::string& out_path, GpgEncrResult& encr_res, - GpgSignResult& sign_res, int _channel) { + GpgSignResult& sign_res, int _channel) -> gpg_error_t { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -194,9 +192,11 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( return err; } -gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( - const std::string& in_path, const std::string& out_path, - GpgDecrResult& decr_res, GpgVerifyResult& verify_res) { +auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, + const std::string& out_path, + GpgDecrResult& decr_res, + GpgVerifyResult& verify_res) + -> gpg_error_t { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -223,9 +223,9 @@ gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( return err; } -unsigned int GpgFrontend::GpgFileOpera::EncryptFileSymmetric( +auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( const std::string& in_path, const std::string& out_path, - GpgFrontend::GpgEncrResult& result, int _channel) { + GpgFrontend::GpgEncrResult& result, int _channel) -> unsigned int { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); -- 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/GpgFileOpera.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 199cf3d7..bf416880 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -56,9 +56,9 @@ auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( std::move(keys), in_buffer, out_buffer, result); - if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("write_buffer_to_file error"); + throw std::runtime_error("WriteBufferToFile error"); }; return err; @@ -87,11 +87,11 @@ auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, auto err = GpgBasicOperator::GetInstance().Decrypt(in_buffer, out_buffer, result); - assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); + assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); - if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("write_buffer_to_file error"); + throw std::runtime_error("WriteBufferToFile error"); }; return err; @@ -121,9 +121,9 @@ auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, auto err = GpgBasicOperator::GetInstance(_channel).Sign( std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); - if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("write_buffer_to_file error"); + throw std::runtime_error("WriteBufferToFile error"); }; return err; @@ -184,9 +184,9 @@ auto GpgFrontend::GpgFileOpera::EncryptSignFile( std::move(keys), std::move(signer_keys), in_buffer, out_buffer, encr_res, sign_res); - if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("write_buffer_to_file error"); + throw std::runtime_error("WriteBufferToFile error"); }; return err; @@ -216,7 +216,7 @@ auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, auto err = GpgBasicOperator::GetInstance().DecryptVerify( in_buffer, out_buffer, decr_res, verify_res); - if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("write file error"); }; @@ -245,9 +245,9 @@ auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( auto err = GpgBasicOperator::GetInstance(_channel).EncryptSymmetric( in_buffer, out_buffer, result); - if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("write_buffer_to_file error"); + throw std::runtime_error("WriteBufferToFile error"); }; return err; -- cgit v1.2.3 From b219d1c9d0f6e9542b0b4f3f62c4dd368413ddec Mon Sep 17 00:00:00 2001 From: saturneric Date: Thu, 2 Nov 2023 09:59:40 +0800 Subject: feat: add callback function to event --- src/core/function/gpg/GpgFileOpera.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index bf416880..f1b1d1c9 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -245,10 +245,11 @@ auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( auto err = GpgBasicOperator::GetInstance(_channel).EncryptSymmetric( in_buffer, out_buffer, result); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); - }; + } + }; return err; } -- 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/GpgFileOpera.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index f1b1d1c9..8d5e3369 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -27,9 +27,9 @@ */ #include "GpgFileOpera.h" -#include "core/GpgConstants.h" -#include "core/function/FileOperator.h" #include "core/function/gpg/GpgBasicOperator.h" +#include "core/utils/GpgUtils.h" +#include "core/utils/IOUtils.h" auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, const std::string& in_path, @@ -47,7 +47,7 @@ auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } @@ -57,7 +57,7 @@ auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, std::move(keys), in_buffer, out_buffer, result); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); }; @@ -79,7 +79,7 @@ auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } ByteArrayPtr out_buffer; @@ -90,7 +90,7 @@ auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); }; @@ -113,7 +113,7 @@ auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } ByteArrayPtr out_buffer; @@ -122,7 +122,7 @@ auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); }; @@ -144,13 +144,13 @@ auto GpgFrontend::GpgFileOpera::VerifyFile(const std::string& data_path, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(data_path_std, in_buffer)) { + if (!ReadFileStd(data_path_std, in_buffer)) { throw std::runtime_error("read file error"); } ByteArrayPtr sign_buffer = nullptr; if (!sign_path.empty()) { std::string sign_buffer_str; - if (!FileOperator::ReadFileStd(sign_path_std, sign_buffer_str)) { + if (!ReadFileStd(sign_path_std, sign_buffer_str)) { throw std::runtime_error("read file error"); } sign_buffer = std::make_unique(sign_buffer_str); @@ -175,7 +175,7 @@ auto GpgFrontend::GpgFileOpera::EncryptSignFile( #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } ByteArrayPtr out_buffer = nullptr; @@ -185,7 +185,7 @@ auto GpgFrontend::GpgFileOpera::EncryptSignFile( sign_res); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); }; @@ -208,7 +208,7 @@ auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } @@ -217,7 +217,7 @@ auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, in_buffer, out_buffer, decr_res, verify_res); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("write file error"); }; @@ -237,7 +237,7 @@ auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } @@ -246,7 +246,7 @@ auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( in_buffer, out_buffer, result); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); } }; -- cgit v1.2.3 From 2fc77533c039ac2e77240dc4b434c84109c6e643 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 4 Dec 2023 07:08:39 -0800 Subject: feat: convert basic opera to async style --- src/core/function/gpg/GpgFileOpera.cpp | 52 +++++++++++++++++----------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 8d5e3369..bce3fcd8 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -36,32 +36,32 @@ auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, const std::string& out_path, GpgEncrResult& result, int _channel) -> GpgFrontend::GpgError { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { - throw std::runtime_error("read file error"); - } - - ByteArrayPtr out_buffer = nullptr; - - auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( - std::move(keys), in_buffer, out_buffer, result); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("WriteBufferToFile error"); - }; - - return err; + // #ifdef WINDOWS + // auto in_path_std = + // std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); + // auto out_path_std = + // std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); + // #else + // auto in_path_std = std::filesystem::path(in_path); + // auto out_path_std = std::filesystem::path(out_path); + // #endif + + // std::string in_buffer; + // if (!ReadFileStd(in_path_std, in_buffer)) { + // throw std::runtime_error("read file error"); + // } + + // ByteArrayPtr out_buffer = nullptr; + + // auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( + // std::move(keys), in_buffer, out_buffer, result); + + // if (CheckGpgError(err) == GPG_ERR_NO_ERROR) + // if (!WriteFileStd(out_path_std, *out_buffer)) { + // throw std::runtime_error("WriteBufferToFile error"); + // }; + + // return err; } auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, -- cgit v1.2.3 From 4f29479ec7c128ab035d76e32006d8b0c7d0b9f1 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sat, 23 Dec 2023 20:07:26 +0800 Subject: fix: solve all issues of test cases on macos m1 --- src/core/function/gpg/GpgFileOpera.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index bce3fcd8..32f8c3ab 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -84,8 +84,8 @@ auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, } ByteArrayPtr out_buffer; - auto err = - GpgBasicOperator::GetInstance().Decrypt(in_buffer, out_buffer, result); + auto err = GpgBasicOperator::GetInstance().Decrypt(GFBuffer(in_buffer), + out_buffer, result); assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); -- cgit v1.2.3 From 956c5ed3a8931bcbfa07fbe2b1af8090188b822b Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 24 Dec 2023 01:36:40 +0800 Subject: feat: improve core interfaces of encrypt and decrypt --- src/core/function/gpg/GpgFileOpera.cpp | 106 +++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 46 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 32f8c3ab..f1e9a3c1 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -27,47 +27,20 @@ */ #include "GpgFileOpera.h" +#include + #include "core/function/gpg/GpgBasicOperator.h" +#include "core/model/GFBuffer.h" +#include "core/model/GpgEncryptResult.h" +#include "core/model/GpgKey.h" #include "core/utils/GpgUtils.h" #include "core/utils/IOUtils.h" -auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, +void GpgFrontend::GpgFileOpera::EncryptFile(std::vector keys, const std::string& in_path, const std::string& out_path, - GpgEncrResult& result, int _channel) - -> GpgFrontend::GpgError { - // #ifdef WINDOWS - // auto in_path_std = - // std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - // auto out_path_std = - // std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); - // #else - // auto in_path_std = std::filesystem::path(in_path); - // auto out_path_std = std::filesystem::path(out_path); - // #endif - - // std::string in_buffer; - // if (!ReadFileStd(in_path_std, in_buffer)) { - // throw std::runtime_error("read file error"); - // } - - // ByteArrayPtr out_buffer = nullptr; - - // auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( - // std::move(keys), in_buffer, out_buffer, result); - - // if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - // if (!WriteFileStd(out_path_std, *out_buffer)) { - // throw std::runtime_error("WriteBufferToFile error"); - // }; - - // return err; -} - -auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, - const std::string& out_path, - GpgDecrResult& result) - -> GpgFrontend::GpgError { + bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -78,23 +51,64 @@ auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, auto out_path_std = std::filesystem::path(out_path); #endif - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr out_buffer; - auto err = GpgBasicOperator::GetInstance().Decrypt(GFBuffer(in_buffer), - out_buffer, result); + GpgBasicOperator::GetInstance().Encrypt( + std::move(keys), std::get<1>(read_result), ascii, + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto result = ExtractParams(data_object, 0); + auto buffer = ExtractParams(data_object, 1); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + cb(err, TransferParams(result)); + return; + } + + if (!WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + }); +} - assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); +void GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, + const std::string& out_path, + const GpgOperationCallback& cb) { +#ifdef WINDOWS + auto in_path_std = + std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); + auto out_path_std = + std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); +#else + auto in_path_std = std::filesystem::path(in_path); + auto out_path_std = std::filesystem::path(out_path); +#endif - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("WriteBufferToFile error"); - }; + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { + throw std::runtime_error("read file error"); + } - return err; + GpgBasicOperator::GetInstance().Decrypt( + std::get<1>(read_result), + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto result = ExtractParams(data_object, 0); + auto buffer = ExtractParams(data_object, 1); + + if (CheckGpgError(err) == GPG_ERR_NO_ERROR && + !WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + + cb(err, TransferParams(result)); + }); } auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, -- cgit v1.2.3 From e52215cd31237bcfe343754bdfcfdde65e804432 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 24 Dec 2023 18:16:45 +0800 Subject: fix: use the new async encrypt symmetric api at gui app --- src/core/function/gpg/GpgFileOpera.cpp | 36 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index f1e9a3c1..cbfbc8b1 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -237,9 +237,9 @@ auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, return err; } -auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( - const std::string& in_path, const std::string& out_path, - GpgFrontend::GpgEncrResult& result, int _channel) -> unsigned int { +void GpgFrontend::GpgFileOpera::EncryptFileSymmetric( + const std::string& in_path, const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -250,20 +250,26 @@ auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( auto out_path_std = std::filesystem::path(out_path); #endif - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr out_buffer; - auto err = GpgBasicOperator::GetInstance(_channel).EncryptSymmetric( - in_buffer, out_buffer, result); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("WriteBufferToFile error"); - } - }; + GpgBasicOperator::GetInstance().EncryptSymmetric( + std::get<1>(read_result), ascii, + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto result = ExtractParams(data_object, 0); + auto buffer = ExtractParams(data_object, 1); + if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { + cb(err, TransferParams(result)); + return; + } - return err; + if (!WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + }); } -- cgit v1.2.3 From 5260d7a5634279408b3eb36a5f26fe71fb6f9e69 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 25 Dec 2023 20:51:01 +0800 Subject: feat: upgrade basical operations fully to async style and update test cases --- src/core/function/gpg/GpgFileOpera.cpp | 190 +++++++++++++++++++-------------- 1 file changed, 107 insertions(+), 83 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index cbfbc8b1..1c577997 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -31,16 +31,20 @@ #include "core/function/gpg/GpgBasicOperator.h" #include "core/model/GFBuffer.h" +#include "core/model/GpgDecryptResult.h" #include "core/model/GpgEncryptResult.h" #include "core/model/GpgKey.h" +#include "core/model/GpgSignResult.h" +#include "core/model/GpgVerifyResult.h" #include "core/utils/GpgUtils.h" #include "core/utils/IOUtils.h" -void GpgFrontend::GpgFileOpera::EncryptFile(std::vector keys, - const std::string& in_path, - const std::string& out_path, - bool ascii, - const GpgOperationCallback& cb) { +namespace GpgFrontend { + +void GpgFileOpera::EncryptFile(std::vector keys, + const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -64,20 +68,18 @@ void GpgFrontend::GpgFileOpera::EncryptFile(std::vector keys, } auto result = ExtractParams(data_object, 0); auto buffer = ExtractParams(data_object, 1); - if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { - cb(err, TransferParams(result)); - return; - } - - if (!WriteFileGFBuffer(out_path_std, buffer)) { - throw std::runtime_error("write buffer to file error"); + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + if (!WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } } + cb(err, TransferParams(result)); }); } -void GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, - const std::string& out_path, - const GpgOperationCallback& cb) { +void GpgFileOpera::DecryptFile(const std::string& in_path, + const std::string& out_path, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -96,10 +98,10 @@ void GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, GpgBasicOperator::GetInstance().Decrypt( std::get<1>(read_result), [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check()) { + if (!data_object->Check()) { throw std::runtime_error("data object transfers wrong arguments"); } - auto result = ExtractParams(data_object, 0); + auto result = ExtractParams(data_object, 0); auto buffer = ExtractParams(data_object, 1); if (CheckGpgError(err) == GPG_ERR_NO_ERROR && @@ -111,11 +113,9 @@ void GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, }); } -auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, - const std::string& in_path, - const std::string& out_path, - GpgSignResult& result, int _channel) - -> gpgme_error_t { +void GpgFileOpera::SignFile(KeyArgsList keys, const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -126,27 +126,31 @@ auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, auto out_path_std = std::filesystem::path(out_path); #endif - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr out_buffer; - auto err = GpgBasicOperator::GetInstance(_channel).Sign( - std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("WriteBufferToFile error"); - }; + GpgBasicOperator::GetInstance().Sign( + std::move(keys), std::get<1>(read_result), GPGME_SIG_MODE_DETACH, ascii, + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto result = ExtractParams(data_object, 0); + auto buffer = ExtractParams(data_object, 1); - return err; + if (CheckGpgError(err) == GPG_ERR_NO_ERROR && + !WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + cb(err, TransferParams(result)); + }); } -auto GpgFrontend::GpgFileOpera::VerifyFile(const std::string& data_path, - const std::string& sign_path, - GpgVerifyResult& result, - int _channel) -> gpgme_error_t { +void GpgFileOpera::VerifyFile(const std::string& data_path, + const std::string& sign_path, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto data_path_std = std::filesystem::path(QString::fromStdString(data_path).toStdU16String()); @@ -157,27 +161,35 @@ auto GpgFrontend::GpgFileOpera::VerifyFile(const std::string& data_path, auto sign_path_std = std::filesystem::path(sign_path); #endif - std::string in_buffer; - if (!ReadFileStd(data_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(data_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr sign_buffer = nullptr; + + GFBuffer sign_buffer; if (!sign_path.empty()) { - std::string sign_buffer_str; - if (!ReadFileStd(sign_path_std, sign_buffer_str)) { + auto read_result = ReadFileGFBuffer(sign_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - sign_buffer = std::make_unique(sign_buffer_str); + sign_buffer = std::get<1>(read_result); } - auto err = GpgBasicOperator::GetInstance(_channel).Verify( - in_buffer, sign_buffer, result); - return err; + + GpgBasicOperator::GetInstance().Verify( + std::get<1>(read_result), sign_buffer, + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto result = ExtractParams(data_object, 0); + cb(err, TransferParams(result)); + }); } -auto GpgFrontend::GpgFileOpera::EncryptSignFile( - KeyListPtr keys, KeyListPtr signer_keys, const std::string& in_path, - const std::string& out_path, GpgEncrResult& encr_res, - GpgSignResult& sign_res, int _channel) -> gpg_error_t { +void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, + const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -188,29 +200,32 @@ auto GpgFrontend::GpgFileOpera::EncryptSignFile( auto out_path_std = std::filesystem::path(out_path); #endif - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr out_buffer = nullptr; - - auto err = GpgBasicOperator::GetInstance(_channel).EncryptSign( - std::move(keys), std::move(signer_keys), in_buffer, out_buffer, encr_res, - sign_res); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("WriteBufferToFile error"); - }; - return err; + GpgBasicOperator::GetInstance().EncryptSign( + std::move(keys), std::move(signer_keys), std::get<1>(read_result), ascii, + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object->Check()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto encrypt_result = ExtractParams(data_object, 0); + auto sign_result = ExtractParams(data_object, 1); + auto buffer = ExtractParams(data_object, 2); + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + if (!WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + } + cb(err, TransferParams(encrypt_result, sign_result)); + }); } -auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, - const std::string& out_path, - GpgDecrResult& decr_res, - GpgVerifyResult& verify_res) - -> gpg_error_t { +void GpgFileOpera::DecryptVerifyFile(const std::string& in_path, + const std::string& out_path, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -221,25 +236,32 @@ auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, auto out_path_std = std::filesystem::path(out_path); #endif - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { + auto read_result = ReadFileGFBuffer(in_path_std); + if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } - ByteArrayPtr out_buffer = nullptr; - auto err = GpgBasicOperator::GetInstance().DecryptVerify( - in_buffer, out_buffer, decr_res, verify_res); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("write file error"); - }; - - return err; + GpgBasicOperator::GetInstance().DecryptVerify( + std::get<1>(read_result), + [=](GpgError err, const DataObjectPtr& data_object) { + if (!data_object + ->Check()) { + throw std::runtime_error("data object transfers wrong arguments"); + } + auto decrypt_result = ExtractParams(data_object, 0); + auto verify_result = ExtractParams(data_object, 1); + auto buffer = ExtractParams(data_object, 2); + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + if (!WriteFileGFBuffer(out_path_std, buffer)) { + throw std::runtime_error("write buffer to file error"); + } + } + cb(err, TransferParams(decrypt_result, verify_result)); + }); } -void GpgFrontend::GpgFileOpera::EncryptFileSymmetric( - const std::string& in_path, const std::string& out_path, bool ascii, - const GpgOperationCallback& cb) { +void GpgFileOpera::EncryptFileSymmetric(const std::string& in_path, + const std::string& out_path, bool ascii, + const GpgOperationCallback& cb) { #ifdef WINDOWS auto in_path_std = std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); @@ -273,3 +295,5 @@ void GpgFrontend::GpgFileOpera::EncryptFileSymmetric( } }); } + +} // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From f8733b9b2be66d0d8dd8a220ed972af8520192b4 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 26 Dec 2023 17:07:49 +0800 Subject: fix: add test cases and test file basical operations --- src/core/function/gpg/GpgFileOpera.cpp | 131 +++++++++------------------------ 1 file changed, 33 insertions(+), 98 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 1c577997..45286df2 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -42,20 +42,10 @@ namespace GpgFrontend { void GpgFileOpera::EncryptFile(std::vector keys, - const std::string& in_path, - const std::string& out_path, bool ascii, + const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -69,7 +59,7 @@ void GpgFileOpera::EncryptFile(std::vector keys, auto result = ExtractParams(data_object, 0); auto buffer = ExtractParams(data_object, 1); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path_std, buffer)) { + if (!WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } } @@ -77,20 +67,10 @@ void GpgFileOpera::EncryptFile(std::vector keys, }); } -void GpgFileOpera::DecryptFile(const std::string& in_path, - const std::string& out_path, +void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -105,7 +85,7 @@ void GpgFileOpera::DecryptFile(const std::string& in_path, auto buffer = ExtractParams(data_object, 1); if (CheckGpgError(err) == GPG_ERR_NO_ERROR && - !WriteFileGFBuffer(out_path_std, buffer)) { + !WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } @@ -113,20 +93,11 @@ void GpgFileOpera::DecryptFile(const std::string& in_path, }); } -void GpgFileOpera::SignFile(KeyArgsList keys, const std::string& in_path, - const std::string& out_path, bool ascii, +void GpgFileOpera::SignFile(KeyArgsList keys, + const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -141,34 +112,24 @@ void GpgFileOpera::SignFile(KeyArgsList keys, const std::string& in_path, auto buffer = ExtractParams(data_object, 1); if (CheckGpgError(err) == GPG_ERR_NO_ERROR && - !WriteFileGFBuffer(out_path_std, buffer)) { + !WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } cb(err, TransferParams(result)); }); } -void GpgFileOpera::VerifyFile(const std::string& data_path, - const std::string& sign_path, +void GpgFileOpera::VerifyFile(const std::filesystem::path& data_path, + const std::filesystem::path& sign_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto data_path_std = - std::filesystem::path(QString::fromStdString(data_path).toStdU16String()); - auto sign_path_std = - std::filesystem::path(QString::fromStdString(sign_path).toStdU16String()); -#else - auto data_path_std = std::filesystem::path(data_path); - auto sign_path_std = std::filesystem::path(sign_path); -#endif - - auto read_result = ReadFileGFBuffer(data_path_std); + auto read_result = ReadFileGFBuffer(data_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } GFBuffer sign_buffer; if (!sign_path.empty()) { - auto read_result = ReadFileGFBuffer(sign_path_std); + auto read_result = ReadFileGFBuffer(sign_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -187,20 +148,11 @@ void GpgFileOpera::VerifyFile(const std::string& data_path, } void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, - const std::string& in_path, - const std::string& out_path, bool ascii, + const std::filesystem::path& in_path, + bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -215,7 +167,7 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, auto sign_result = ExtractParams(data_object, 1); auto buffer = ExtractParams(data_object, 2); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path_std, buffer)) { + if (!WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } } @@ -223,20 +175,10 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, }); } -void GpgFileOpera::DecryptVerifyFile(const std::string& in_path, - const std::string& out_path, +void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -252,27 +194,18 @@ void GpgFileOpera::DecryptVerifyFile(const std::string& in_path, auto verify_result = ExtractParams(data_object, 1); auto buffer = ExtractParams(data_object, 2); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path_std, buffer)) { + if (!WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } } cb(err, TransferParams(decrypt_result, verify_result)); }); } -void GpgFileOpera::EncryptFileSymmetric(const std::string& in_path, - const std::string& out_path, bool ascii, +void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path, + bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -290,9 +223,11 @@ void GpgFileOpera::EncryptFileSymmetric(const std::string& in_path, return; } - if (!WriteFileGFBuffer(out_path_std, buffer)) { + if (!WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } + + cb(err, TransferParams(result)); }); } -- cgit v1.2.3 From 566d5e41a34e08b06a5c84fd4ad9f944b6b31feb Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 27 Dec 2023 11:20:11 +0800 Subject: feat: avoid reading entire file to memory --- src/core/function/gpg/GpgFileOpera.cpp | 296 ++++++++++++++++----------------- 1 file changed, 148 insertions(+), 148 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 45286df2..7c247b95 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -31,120 +31,129 @@ #include "core/function/gpg/GpgBasicOperator.h" #include "core/model/GFBuffer.h" +#include "core/model/GpgData.h" #include "core/model/GpgDecryptResult.h" #include "core/model/GpgEncryptResult.h" #include "core/model/GpgKey.h" #include "core/model/GpgSignResult.h" #include "core/model/GpgVerifyResult.h" +#include "core/utils/AsyncUtils.h" #include "core/utils/GpgUtils.h" #include "core/utils/IOUtils.h" namespace GpgFrontend { +auto PathPreCheck(const std::filesystem::path& path, bool read) + -> std::tuple { + QFileInfo const file_info(path); + QFileInfo const path_info(file_info.absolutePath()); + + if (!path_info.exists()) { + return {false, _("")}; + } + if (read ? !file_info.isReadable() : !path_info.isWritable()) { + return {false, _("")}; + } + return {true, _("")}; +} + +GpgFileOpera::GpgFileOpera(int channel) + : SingletonFunctionObject(channel) {} + void GpgFileOpera::EncryptFile(std::vector keys, const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + std::vector recipients(keys.begin(), keys.end()); - GpgBasicOperator::GetInstance().Encrypt( - std::move(keys), std::get<1>(read_result), ascii, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams(data_object, 0); - auto buffer = ExtractParams(data_object, 1); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - } - cb(err, TransferParams(result)); - }); + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); + data_object->Swap({GpgEncryptResult(gpgme_op_encrypt_result(ctx))}); + + return err; + }, + cb, "gpgme_op_encrypt", "2.1.0"); } void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - - GpgBasicOperator::GetInstance().Decrypt( - std::get<1>(read_result), - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams(data_object, 0); - auto buffer = ExtractParams(data_object, 1); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR && - !WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - - cb(err, TransferParams(result)); - }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto err = CheckGpgError( + gpgme_op_decrypt(ctx_.DefaultContext(), data_in, data_out)); + data_object->Swap( + {GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext()))}); + + return err; + }, + cb, "gpgme_op_decrypt", "2.1.0"); } void GpgFileOpera::SignFile(KeyArgsList keys, const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - - GpgBasicOperator::GetInstance().Sign( - std::move(keys), std::get<1>(read_result), GPGME_SIG_MODE_DETACH, ascii, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams(data_object, 0); - auto buffer = ExtractParams(data_object, 1); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR && - !WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - cb(err, TransferParams(result)); - }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + + // Set Singers of this opera + GpgBasicOperator::GetInstance().SetSigners(keys, ascii); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError( + gpgme_op_sign(ctx, data_in, data_out, GPGME_SIG_MODE_DETACH)); + + data_object->Swap({ + GpgSignResult(gpgme_op_sign_result(ctx)), + }); + return err; + }, + cb, "gpgme_op_sign", "2.1.0"); } void GpgFileOpera::VerifyFile(const std::filesystem::path& data_path, const std::filesystem::path& sign_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(data_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + + GpgData data_in(data_path, true); + GpgData data_out; + if (!sign_path.empty()) { + GpgData sig_data(sign_path, true); + err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), sig_data, + data_in, nullptr)); + } else { + err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), data_in, + nullptr, data_out)); + } - GFBuffer sign_buffer; - if (!sign_path.empty()) { - auto read_result = ReadFileGFBuffer(sign_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - sign_buffer = std::get<1>(read_result); - } + data_object->Swap({ + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + }); - GpgBasicOperator::GetInstance().Verify( - std::get<1>(read_result), sign_buffer, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams(data_object, 0); - cb(err, TransferParams(result)); - }); + return err; + }, + cb, "gpgme_op_verify", "2.1.0"); } void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, @@ -152,83 +161,74 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - - GpgBasicOperator::GetInstance().EncryptSign( - std::move(keys), std::move(signer_keys), std::get<1>(read_result), ascii, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto encrypt_result = ExtractParams(data_object, 0); - auto sign_result = ExtractParams(data_object, 1); - auto buffer = ExtractParams(data_object, 2); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - } - cb(err, TransferParams(encrypt_result, sign_result)); - }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + std::vector recipients(keys.begin(), keys.end()); + + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgBasicOperator::GetInstance().SetSigners(signer_keys, ascii); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError(gpgme_op_encrypt_sign(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); + + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + GpgSignResult(gpgme_op_sign_result(ctx)), + }); + return err; + }, + cb, "gpgme_op_encrypt_sign", "2.1.0"); } void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; - GpgBasicOperator::GetInstance().DecryptVerify( - std::get<1>(read_result), - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object - ->Check()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto decrypt_result = ExtractParams(data_object, 0); - auto verify_result = ExtractParams(data_object, 1); - auto buffer = ExtractParams(data_object, 2); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - } - cb(err, TransferParams(decrypt_result, verify_result)); - }); + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + err = CheckGpgError( + gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); + + data_object->Swap({ + GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext())), + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + }); + + return err; + }, + cb, "gpgme_op_decrypt_verify", "2.1.0"); } + void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - - GpgBasicOperator::GetInstance().EncryptSymmetric( - std::get<1>(read_result), ascii, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams(data_object, 0); - auto buffer = ExtractParams(data_object, 1); - if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { - cb(err, TransferParams(result)); - return; - } - - if (!WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - - cb(err, TransferParams(result)); - }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt( + ctx, nullptr, GPGME_ENCRYPT_SYMMETRIC, data_in, data_out)); + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + }); + + return err; + }, + cb, "gpgme_op_encrypt_symmetric", "2.1.0"); } - } // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From 300e55bf5bddc393de050c2ca9a0356fce9a8a9d Mon Sep 17 00:00:00 2001 From: saturneric Date: Thu, 28 Dec 2023 14:32:49 +0800 Subject: feat: add simple archiving functions for encrypt and decrypt --- src/core/function/gpg/GpgFileOpera.cpp | 91 ++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 15 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 7c247b95..929c02ea 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -27,8 +27,9 @@ */ #include "GpgFileOpera.h" -#include +#include +#include "core/function/ArchiveFileOperator.h" #include "core/function/gpg/GpgBasicOperator.h" #include "core/model/GFBuffer.h" #include "core/model/GpgData.h" @@ -43,20 +44,6 @@ namespace GpgFrontend { -auto PathPreCheck(const std::filesystem::path& path, bool read) - -> std::tuple { - QFileInfo const file_info(path); - QFileInfo const path_info(file_info.absolutePath()); - - if (!path_info.exists()) { - return {false, _("")}; - } - if (read ? !file_info.isReadable() : !path_info.isWritable()) { - return {false, _("")}; - } - return {true, _("")}; -} - GpgFileOpera::GpgFileOpera(int channel) : SingletonFunctionObject(channel) {} @@ -85,6 +72,44 @@ void GpgFileOpera::EncryptFile(std::vector keys, cb, "gpgme_op_encrypt", "2.1.0"); } +void GpgFileOpera::EncryptDirectory(std::vector keys, + const std::filesystem::path& in_path, + bool ascii, + const std::filesystem::path& out_path, + const GpgOperationCallback& cb) { + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + std::array pipe_fds; + if (pipe(pipe_fds.data()) != 0) { + SPDLOG_ERROR( + "cannot create pipe for directory archive and encryt process"); + return GPG_ERR_EPIPE; + } + + ArchiveFileOperator::NewArchive2Fd( + in_path, pipe_fds[1], [](GFError err, const DataObjectPtr&) { + SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err); + }); + + std::vector recipients(keys.begin(), keys.end()); + + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgData data_in(pipe_fds[0]); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); + data_object->Swap({GpgEncryptResult(gpgme_op_encrypt_result(ctx))}); + + return err; + }, + cb, "gpgme_op_encrypt", "2.1.0"); +} + void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { @@ -103,6 +128,42 @@ void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, cb, "gpgme_op_decrypt", "2.1.0"); } +void GpgFileOpera::DecryptArchive(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, + const GpgOperationCallback& cb) { + SPDLOG_DEBUG("decrypt archive start, cuurent thread: {}", + QThread::currentThread()->currentThreadId()); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + std::array pipe_fds; + if (pipe(pipe_fds.data()) != 0) { + SPDLOG_ERROR( + "cannot create pipe for directory archive and encryt process"); + return GPG_ERR_EPIPE; + } + + SPDLOG_DEBUG("decrypt archive processing, cuurent thread: {}", + QThread::currentThread()->currentThreadId()); + ArchiveFileOperator::ExtractArchiveFromFd( + pipe_fds[0], out_path, [](GFError err, const DataObjectPtr&) { + SPDLOG_DEBUG("extract archive from fd operation, err: {}", err); + }); + + GpgData data_in(in_path, true); + GpgData data_out(pipe_fds[1]); + + SPDLOG_DEBUG("start to decrypt archive: {}", in_path.string()); + auto err = CheckGpgError( + gpgme_op_decrypt(ctx_.DefaultContext(), data_in, data_out)); + SPDLOG_DEBUG("decryption of archive done: {}", in_path.string()); + + data_object->Swap( + {GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext()))}); + return err; + }, + cb, "gpgme_op_decrypt", "2.1.0"); +} + void GpgFileOpera::SignFile(KeyArgsList keys, const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, -- cgit v1.2.3 From 9f7593eab9b2d6f6fa6d76a303975b355b6ab458 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sat, 30 Dec 2023 19:55:20 +0800 Subject: feat: do not load entire data to memory in libarchive operations --- src/core/function/gpg/GpgFileOpera.cpp | 53 ++++++++++++---------------------- 1 file changed, 19 insertions(+), 34 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 929c02ea..a8e33725 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -27,8 +27,6 @@ */ #include "GpgFileOpera.h" -#include - #include "core/function/ArchiveFileOperator.h" #include "core/function/gpg/GpgBasicOperator.h" #include "core/model/GFBuffer.h" @@ -77,37 +75,35 @@ void GpgFileOpera::EncryptDirectory(std::vector keys, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { + std::shared_ptr ex = std::make_shared(8192); + RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { - std::array pipe_fds; - if (pipe(pipe_fds.data()) != 0) { - SPDLOG_ERROR( - "cannot create pipe for directory archive and encryt process"); - return GPG_ERR_EPIPE; - } - - ArchiveFileOperator::NewArchive2Fd( - in_path, pipe_fds[1], [](GFError err, const DataObjectPtr&) { - SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err); - }); - std::vector recipients(keys.begin(), keys.end()); // Last entry data_in array has to be nullptr recipients.emplace_back(nullptr); - GpgData data_in(pipe_fds[0]); + GpgData data_in(ex); GpgData data_out(out_path, false); + SPDLOG_DEBUG("encrypt directory start"); + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); auto err = CheckGpgError(gpgme_op_encrypt(ctx, recipients.data(), GPGME_ENCRYPT_ALWAYS_TRUST, data_in, data_out)); data_object->Swap({GpgEncryptResult(gpgme_op_encrypt_result(ctx))}); + SPDLOG_DEBUG("encrypt directory finished, err: {}", err); return err; }, cb, "gpgme_op_encrypt", "2.1.0"); + + ArchiveFileOperator::NewArchive2DataExchanger( + in_path, ex, [=](GFError err, const DataObjectPtr&) { + SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err); + }); } void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, @@ -131,31 +127,20 @@ void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, void GpgFileOpera::DecryptArchive(const std::filesystem::path& in_path, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - SPDLOG_DEBUG("decrypt archive start, cuurent thread: {}", - QThread::currentThread()->currentThreadId()); - RunGpgOperaAsync( - [=](const DataObjectPtr& data_object) -> GpgError { - std::array pipe_fds; - if (pipe(pipe_fds.data()) != 0) { - SPDLOG_ERROR( - "cannot create pipe for directory archive and encryt process"); - return GPG_ERR_EPIPE; - } + std::shared_ptr ex = std::make_shared(8192); - SPDLOG_DEBUG("decrypt archive processing, cuurent thread: {}", - QThread::currentThread()->currentThreadId()); - ArchiveFileOperator::ExtractArchiveFromFd( - pipe_fds[0], out_path, [](GFError err, const DataObjectPtr&) { - SPDLOG_DEBUG("extract archive from fd operation, err: {}", err); - }); + ArchiveFileOperator::ExtractArchiveFromDataExchanger( + ex, out_path, [](GFError err, const DataObjectPtr&) { + SPDLOG_DEBUG("extract archive from fd operation, err: {}", err); + }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { GpgData data_in(in_path, true); - GpgData data_out(pipe_fds[1]); + GpgData data_out(ex); - SPDLOG_DEBUG("start to decrypt archive: {}", in_path.string()); auto err = CheckGpgError( gpgme_op_decrypt(ctx_.DefaultContext(), data_in, data_out)); - SPDLOG_DEBUG("decryption of archive done: {}", in_path.string()); data_object->Swap( {GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext()))}); -- cgit v1.2.3 From 5030380bc5e155252655e2ad71e63c15271054c4 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 31 Dec 2023 14:10:31 +0800 Subject: feat: fully test basical operations of archive and file --- src/core/function/gpg/GpgFileOpera.cpp | 103 ++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index a8e33725..ecb292e0 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -42,6 +42,8 @@ namespace GpgFrontend { +constexpr ssize_t kDataExchangerSize = 8192; + GpgFileOpera::GpgFileOpera(int channel) : SingletonFunctionObject(channel) {} @@ -75,7 +77,7 @@ void GpgFileOpera::EncryptDirectory(std::vector keys, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - std::shared_ptr ex = std::make_shared(8192); + auto ex = std::make_shared(kDataExchangerSize); RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -127,7 +129,7 @@ void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, void GpgFileOpera::DecryptArchive(const std::filesystem::path& in_path, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - std::shared_ptr ex = std::make_shared(8192); + auto ex = std::make_shared(kDataExchangerSize); ArchiveFileOperator::ExtractArchiveFromDataExchanger( ex, out_path, [](GFError err, const DataObjectPtr&) { @@ -234,6 +236,46 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, cb, "gpgme_op_encrypt_sign", "2.1.0"); } +void GpgFileOpera::EncryptSignDirectory(KeyArgsList keys, + KeyArgsList signer_keys, + const std::filesystem::path& in_path, + bool ascii, + const std::filesystem::path& out_path, + const GpgOperationCallback& cb) { + auto ex = std::make_shared(kDataExchangerSize); + + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + std::vector recipients(keys.begin(), keys.end()); + + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgBasicOperator::GetInstance().SetSigners(signer_keys, ascii); + + GpgData data_in(ex); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError(gpgme_op_encrypt_sign(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); + + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + GpgSignResult(gpgme_op_sign_result(ctx)), + }); + return err; + }, + cb, "gpgme_op_encrypt_sign", "2.1.0"); + + ArchiveFileOperator::NewArchive2DataExchanger( + in_path, ex, [=](GFError err, const DataObjectPtr&) { + SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err); + }); +} + void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { @@ -257,6 +299,36 @@ void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path, cb, "gpgme_op_decrypt_verify", "2.1.0"); } +void GpgFileOpera::DecryptVerifyArchive(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, + const GpgOperationCallback& cb) { + auto ex = std::make_shared(kDataExchangerSize); + + ArchiveFileOperator::ExtractArchiveFromDataExchanger( + ex, out_path, [](GFError err, const DataObjectPtr&) { + SPDLOG_DEBUG("extract archive from ex operation, err: {}", err); + }); + + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + + GpgData data_in(in_path, true); + GpgData data_out(ex); + + err = CheckGpgError( + gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); + + data_object->Swap({ + GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext())), + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + }); + + return err; + }, + cb, "gpgme_op_decrypt_verify", "2.1.0"); +} + void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, @@ -277,4 +349,31 @@ void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path, }, cb, "gpgme_op_encrypt_symmetric", "2.1.0"); } + +void GpgFileOpera::EncryptDerectorySymmetric( + const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { + auto ex = std::make_shared(kDataExchangerSize); + + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgData data_in(ex); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt( + ctx, nullptr, GPGME_ENCRYPT_SYMMETRIC, data_in, data_out)); + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + }); + + return err; + }, + cb, "gpgme_op_encrypt_symmetric", "2.1.0"); + + ArchiveFileOperator::NewArchive2DataExchanger( + in_path, ex, [=](GFError err, const DataObjectPtr&) { + SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err); + }); +} } // namespace GpgFrontend \ No newline at end of file -- 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/GpgFileOpera.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index ecb292e0..53cc6099 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -89,7 +89,7 @@ void GpgFileOpera::EncryptDirectory(std::vector keys, GpgData data_in(ex); GpgData data_out(out_path, false); - SPDLOG_DEBUG("encrypt directory start"); + GF_CORE_LOG_DEBUG("encrypt directory start"); auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); auto err = CheckGpgError(gpgme_op_encrypt(ctx, recipients.data(), @@ -97,14 +97,14 @@ void GpgFileOpera::EncryptDirectory(std::vector keys, data_in, data_out)); data_object->Swap({GpgEncryptResult(gpgme_op_encrypt_result(ctx))}); - SPDLOG_DEBUG("encrypt directory finished, err: {}", err); + GF_CORE_LOG_DEBUG("encrypt directory finished, err: {}", err); return err; }, cb, "gpgme_op_encrypt", "2.1.0"); ArchiveFileOperator::NewArchive2DataExchanger( in_path, ex, [=](GFError err, const DataObjectPtr&) { - SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err); + GF_CORE_LOG_DEBUG("new archive 2 fd operation, err: {}", err); }); } @@ -133,7 +133,7 @@ void GpgFileOpera::DecryptArchive(const std::filesystem::path& in_path, ArchiveFileOperator::ExtractArchiveFromDataExchanger( ex, out_path, [](GFError err, const DataObjectPtr&) { - SPDLOG_DEBUG("extract archive from fd operation, err: {}", err); + GF_CORE_LOG_DEBUG("extract archive from fd operation, err: {}", err); }); RunGpgOperaAsync( @@ -272,7 +272,7 @@ void GpgFileOpera::EncryptSignDirectory(KeyArgsList keys, ArchiveFileOperator::NewArchive2DataExchanger( in_path, ex, [=](GFError err, const DataObjectPtr&) { - SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err); + GF_CORE_LOG_DEBUG("new archive 2 fd operation, err: {}", err); }); } @@ -306,7 +306,7 @@ void GpgFileOpera::DecryptVerifyArchive(const std::filesystem::path& in_path, ArchiveFileOperator::ExtractArchiveFromDataExchanger( ex, out_path, [](GFError err, const DataObjectPtr&) { - SPDLOG_DEBUG("extract archive from ex operation, err: {}", err); + GF_CORE_LOG_DEBUG("extract archive from ex operation, err: {}", err); }); RunGpgOperaAsync( @@ -373,7 +373,7 @@ void GpgFileOpera::EncryptDerectorySymmetric( ArchiveFileOperator::NewArchive2DataExchanger( in_path, ex, [=](GFError err, const DataObjectPtr&) { - SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err); + GF_CORE_LOG_DEBUG("new archive 2 fd operation, err: {}", err); }); } } // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From 6983b5c1dd82d159236ebd06cf17f071cc9c1ee9 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Jan 2024 23:08:38 +0800 Subject: refactor: remove boost and use QString instead of std::filesystem::path --- src/core/function/gpg/GpgFileOpera.cpp | 59 ++++++++++++++-------------------- 1 file changed, 25 insertions(+), 34 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 53cc6099..a4663150 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -29,7 +29,6 @@ #include "core/function/ArchiveFileOperator.h" #include "core/function/gpg/GpgBasicOperator.h" -#include "core/model/GFBuffer.h" #include "core/model/GpgData.h" #include "core/model/GpgDecryptResult.h" #include "core/model/GpgEncryptResult.h" @@ -38,7 +37,6 @@ #include "core/model/GpgVerifyResult.h" #include "core/utils/AsyncUtils.h" #include "core/utils/GpgUtils.h" -#include "core/utils/IOUtils.h" namespace GpgFrontend { @@ -47,9 +45,8 @@ constexpr ssize_t kDataExchangerSize = 8192; GpgFileOpera::GpgFileOpera(int channel) : SingletonFunctionObject(channel) {} -void GpgFileOpera::EncryptFile(std::vector keys, - const std::filesystem::path& in_path, bool ascii, - const std::filesystem::path& out_path, +void GpgFileOpera::EncryptFile(std::vector keys, const QString& in_path, + bool ascii, const QString& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -73,9 +70,8 @@ void GpgFileOpera::EncryptFile(std::vector keys, } void GpgFileOpera::EncryptDirectory(std::vector keys, - const std::filesystem::path& in_path, - bool ascii, - const std::filesystem::path& out_path, + const QString& in_path, bool ascii, + const QString& out_path, const GpgOperationCallback& cb) { auto ex = std::make_shared(kDataExchangerSize); @@ -108,8 +104,7 @@ void GpgFileOpera::EncryptDirectory(std::vector keys, }); } -void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, - const std::filesystem::path& out_path, +void GpgFileOpera::DecryptFile(const QString& in_path, const QString& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -126,8 +121,8 @@ void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, cb, "gpgme_op_decrypt", "2.1.0"); } -void GpgFileOpera::DecryptArchive(const std::filesystem::path& in_path, - const std::filesystem::path& out_path, +void GpgFileOpera::DecryptArchive(const QString& in_path, + const QString& out_path, const GpgOperationCallback& cb) { auto ex = std::make_shared(kDataExchangerSize); @@ -151,9 +146,8 @@ void GpgFileOpera::DecryptArchive(const std::filesystem::path& in_path, cb, "gpgme_op_decrypt", "2.1.0"); } -void GpgFileOpera::SignFile(KeyArgsList keys, - const std::filesystem::path& in_path, bool ascii, - const std::filesystem::path& out_path, +void GpgFileOpera::SignFile(KeyArgsList keys, const QString& in_path, + bool ascii, const QString& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -177,8 +171,8 @@ void GpgFileOpera::SignFile(KeyArgsList keys, cb, "gpgme_op_sign", "2.1.0"); } -void GpgFileOpera::VerifyFile(const std::filesystem::path& data_path, - const std::filesystem::path& sign_path, +void GpgFileOpera::VerifyFile(const QString& data_path, + const QString& sign_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -186,7 +180,7 @@ void GpgFileOpera::VerifyFile(const std::filesystem::path& data_path, GpgData data_in(data_path, true); GpgData data_out; - if (!sign_path.empty()) { + if (!sign_path.isEmpty()) { GpgData sig_data(sign_path, true); err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), sig_data, data_in, nullptr)); @@ -205,9 +199,8 @@ void GpgFileOpera::VerifyFile(const std::filesystem::path& data_path, } void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, - const std::filesystem::path& in_path, - bool ascii, - const std::filesystem::path& out_path, + const QString& in_path, bool ascii, + const QString& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -238,9 +231,8 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, void GpgFileOpera::EncryptSignDirectory(KeyArgsList keys, KeyArgsList signer_keys, - const std::filesystem::path& in_path, - bool ascii, - const std::filesystem::path& out_path, + const QString& in_path, bool ascii, + const QString& out_path, const GpgOperationCallback& cb) { auto ex = std::make_shared(kDataExchangerSize); @@ -276,8 +268,8 @@ void GpgFileOpera::EncryptSignDirectory(KeyArgsList keys, }); } -void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path, - const std::filesystem::path& out_path, +void GpgFileOpera::DecryptVerifyFile(const QString& in_path, + const QString& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -299,8 +291,8 @@ void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path, cb, "gpgme_op_decrypt_verify", "2.1.0"); } -void GpgFileOpera::DecryptVerifyArchive(const std::filesystem::path& in_path, - const std::filesystem::path& out_path, +void GpgFileOpera::DecryptVerifyArchive(const QString& in_path, + const QString& out_path, const GpgOperationCallback& cb) { auto ex = std::make_shared(kDataExchangerSize); @@ -329,9 +321,8 @@ void GpgFileOpera::DecryptVerifyArchive(const std::filesystem::path& in_path, cb, "gpgme_op_decrypt_verify", "2.1.0"); } -void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path, - bool ascii, - const std::filesystem::path& out_path, +void GpgFileOpera::EncryptFileSymmetric(const QString& in_path, bool ascii, + const QString& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { @@ -350,9 +341,9 @@ void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path, cb, "gpgme_op_encrypt_symmetric", "2.1.0"); } -void GpgFileOpera::EncryptDerectorySymmetric( - const std::filesystem::path& in_path, bool ascii, - const std::filesystem::path& out_path, const GpgOperationCallback& cb) { +void GpgFileOpera::EncryptDerectorySymmetric(const QString& in_path, bool ascii, + const QString& out_path, + const GpgOperationCallback& cb) { auto ex = std::make_shared(kDataExchangerSize); RunGpgOperaAsync( -- cgit v1.2.3 From e352e8e6b8d03a24ef5d52eef3e4d370807b5bbd Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 17 Jan 2024 19:39:47 +0800 Subject: fix: find and slove some bugs --- src/core/function/gpg/GpgFileOpera.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index a4663150..e3d47cf6 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -100,7 +100,8 @@ void GpgFileOpera::EncryptDirectory(std::vector keys, ArchiveFileOperator::NewArchive2DataExchanger( in_path, ex, [=](GFError err, const DataObjectPtr&) { - GF_CORE_LOG_DEBUG("new archive 2 fd operation, err: {}", err); + GF_CORE_LOG_DEBUG("new archive 2 data exchanger operation, err: {}", + err); }); } @@ -128,7 +129,8 @@ void GpgFileOpera::DecryptArchive(const QString& in_path, ArchiveFileOperator::ExtractArchiveFromDataExchanger( ex, out_path, [](GFError err, const DataObjectPtr&) { - GF_CORE_LOG_DEBUG("extract archive from fd operation, err: {}", err); + GF_CORE_LOG_DEBUG( + "extract archive from data exchanger operation, err: {}", err); }); RunGpgOperaAsync( -- cgit v1.2.3 From e0d2c4021483e6e1366c1050ccb5a30e53e172bb Mon Sep 17 00:00:00 2001 From: saturneric Date: Thu, 18 Jan 2024 20:58:37 +0800 Subject: fix: add test cases and solve discovered issues --- src/core/function/gpg/GpgFileOpera.cpp | 212 ++++++++++++++++++++++++++++++++- 1 file changed, 206 insertions(+), 6 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index e3d47cf6..94a08c76 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -45,7 +45,7 @@ constexpr ssize_t kDataExchangerSize = 8192; GpgFileOpera::GpgFileOpera(int channel) : SingletonFunctionObject(channel) {} -void GpgFileOpera::EncryptFile(std::vector keys, const QString& in_path, +void GpgFileOpera::EncryptFile(const KeyArgsList& keys, const QString& in_path, bool ascii, const QString& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( @@ -69,7 +69,32 @@ void GpgFileOpera::EncryptFile(std::vector keys, const QString& in_path, cb, "gpgme_op_encrypt", "2.1.0"); } -void GpgFileOpera::EncryptDirectory(std::vector keys, +auto GpgFileOpera::EncryptFileSync(const KeyArgsList& keys, + const QString& in_path, bool ascii, + const QString& out_path) + -> std::tuple { + return RunGpgOperaSync( + [=](const DataObjectPtr& data_object) -> GpgError { + std::vector recipients(keys.begin(), keys.end()); + + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); + data_object->Swap({GpgEncryptResult(gpgme_op_encrypt_result(ctx))}); + + return err; + }, + "gpgme_op_encrypt", "2.1.0"); +} + +void GpgFileOpera::EncryptDirectory(const KeyArgsList& keys, const QString& in_path, bool ascii, const QString& out_path, const GpgOperationCallback& cb) { @@ -122,6 +147,24 @@ void GpgFileOpera::DecryptFile(const QString& in_path, const QString& out_path, cb, "gpgme_op_decrypt", "2.1.0"); } +auto GpgFileOpera::DecryptFileSync(const QString& in_path, + const QString& out_path) + -> std::tuple { + return RunGpgOperaSync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto err = CheckGpgError( + gpgme_op_decrypt(ctx_.DefaultContext(), data_in, data_out)); + data_object->Swap( + {GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext()))}); + + return err; + }, + "gpgme_op_decrypt", "2.1.0"); +} + void GpgFileOpera::DecryptArchive(const QString& in_path, const QString& out_path, const GpgOperationCallback& cb) { @@ -148,7 +191,7 @@ void GpgFileOpera::DecryptArchive(const QString& in_path, cb, "gpgme_op_decrypt", "2.1.0"); } -void GpgFileOpera::SignFile(KeyArgsList keys, const QString& in_path, +void GpgFileOpera::SignFile(const KeyArgsList& keys, const QString& in_path, bool ascii, const QString& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( @@ -173,6 +216,31 @@ void GpgFileOpera::SignFile(KeyArgsList keys, const QString& in_path, cb, "gpgme_op_sign", "2.1.0"); } +auto GpgFileOpera::SignFileSync(const KeyArgsList& keys, const QString& in_path, + bool ascii, const QString& out_path) + -> std::tuple { + return RunGpgOperaSync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + + // Set Singers of this opera + GpgBasicOperator::GetInstance().SetSigners(keys, ascii); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError( + gpgme_op_sign(ctx, data_in, data_out, GPGME_SIG_MODE_DETACH)); + + data_object->Swap({ + GpgSignResult(gpgme_op_sign_result(ctx)), + }); + return err; + }, + "gpgme_op_sign", "2.1.0"); +} + void GpgFileOpera::VerifyFile(const QString& data_path, const QString& sign_path, const GpgOperationCallback& cb) { @@ -200,7 +268,35 @@ void GpgFileOpera::VerifyFile(const QString& data_path, cb, "gpgme_op_verify", "2.1.0"); } -void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, +auto GpgFileOpera::VerifyFileSync(const QString& data_path, + const QString& sign_path) + -> std::tuple { + return RunGpgOperaSync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + + GpgData data_in(data_path, true); + GpgData data_out; + if (!sign_path.isEmpty()) { + GpgData sig_data(sign_path, true); + err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), sig_data, + data_in, nullptr)); + } else { + err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), data_in, + nullptr, data_out)); + } + + data_object->Swap({ + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + }); + + return err; + }, + "gpgme_op_verify", "2.1.0"); +} + +void GpgFileOpera::EncryptSignFile(const KeyArgsList& keys, + const KeyArgsList& signer_keys, const QString& in_path, bool ascii, const QString& out_path, const GpgOperationCallback& cb) { @@ -231,8 +327,40 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, cb, "gpgme_op_encrypt_sign", "2.1.0"); } -void GpgFileOpera::EncryptSignDirectory(KeyArgsList keys, - KeyArgsList signer_keys, +auto GpgFileOpera::EncryptSignFileSync(const KeyArgsList& keys, + const KeyArgsList& signer_keys, + const QString& in_path, bool ascii, + const QString& out_path) + -> std::tuple { + return RunGpgOperaSync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + std::vector recipients(keys.begin(), keys.end()); + + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgBasicOperator::GetInstance().SetSigners(signer_keys, ascii); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError(gpgme_op_encrypt_sign(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); + + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + GpgSignResult(gpgme_op_sign_result(ctx)), + }); + return err; + }, + "gpgme_op_encrypt_sign", "2.1.0"); +} + +void GpgFileOpera::EncryptSignDirectory(const KeyArgsList& keys, + const KeyArgsList& signer_keys, const QString& in_path, bool ascii, const QString& out_path, const GpgOperationCallback& cb) { @@ -293,6 +421,29 @@ void GpgFileOpera::DecryptVerifyFile(const QString& in_path, cb, "gpgme_op_decrypt_verify", "2.1.0"); } +auto GpgFileOpera::DecryptVerifyFileSync(const QString& in_path, + const QString& out_path) + -> std::tuple { + return RunGpgOperaSync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + err = CheckGpgError( + gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); + + data_object->Swap({ + GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext())), + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + }); + + return err; + }, + "gpgme_op_decrypt_verify", "2.1.0"); +} + void GpgFileOpera::DecryptVerifyArchive(const QString& in_path, const QString& out_path, const GpgOperationCallback& cb) { @@ -343,6 +494,26 @@ void GpgFileOpera::EncryptFileSymmetric(const QString& in_path, bool ascii, cb, "gpgme_op_encrypt_symmetric", "2.1.0"); } +auto GpgFileOpera::EncryptFileSymmetricSync(const QString& in_path, bool ascii, + const QString& out_path) + -> std::tuple { + return RunGpgOperaSync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt( + ctx, nullptr, GPGME_ENCRYPT_SYMMETRIC, data_in, data_out)); + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + }); + + return err; + }, + "gpgme_op_encrypt_symmetric", "2.1.0"); +} + void GpgFileOpera::EncryptDerectorySymmetric(const QString& in_path, bool ascii, const QString& out_path, const GpgOperationCallback& cb) { @@ -369,4 +540,33 @@ void GpgFileOpera::EncryptDerectorySymmetric(const QString& in_path, bool ascii, GF_CORE_LOG_DEBUG("new archive 2 fd operation, err: {}", err); }); } + +auto GpgFileOpera::EncryptDerectorySymmetricSync(const QString& in_path, + bool ascii, + const QString& out_path) + -> std::tuple { + auto ex = std::make_shared(kDataExchangerSize); + + ArchiveFileOperator::NewArchive2DataExchanger( + in_path, ex, [=](GFError err, const DataObjectPtr&) { + GF_CORE_LOG_DEBUG("new archive 2 fd operation, err: {}", err); + }); + + return RunGpgOperaSync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgData data_in(ex); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt( + ctx, nullptr, GPGME_ENCRYPT_SYMMETRIC, data_in, data_out)); + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + }); + + return err; + }, + "gpgme_op_encrypt_symmetric", "2.1.0"); +} + } // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From f22ceca734868a4cb946c232f661aad72da01ded Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 19 Jan 2024 20:10:17 +0800 Subject: fix: slove discovered faults and bugs --- src/core/function/gpg/GpgFileOpera.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 94a08c76..5abf3159 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -452,6 +452,7 @@ void GpgFileOpera::DecryptVerifyArchive(const QString& in_path, ArchiveFileOperator::ExtractArchiveFromDataExchanger( ex, out_path, [](GFError err, const DataObjectPtr&) { GF_CORE_LOG_DEBUG("extract archive from ex operation, err: {}", err); + GF_CORE_LOG_INFO("//////////////"); }); RunGpgOperaAsync( @@ -461,8 +462,10 @@ void GpgFileOpera::DecryptVerifyArchive(const QString& in_path, GpgData data_in(in_path, true); GpgData data_out(ex); + GF_CORE_LOG_INFO("-----------------------"); err = CheckGpgError( gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); + GF_CORE_LOG_INFO("++++++++++++++++++++++++"); data_object->Swap({ GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext())), -- 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/GpgFileOpera.cpp | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/core/function/gpg/GpgFileOpera.cpp') diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 5abf3159..94a08c76 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -452,7 +452,6 @@ void GpgFileOpera::DecryptVerifyArchive(const QString& in_path, ArchiveFileOperator::ExtractArchiveFromDataExchanger( ex, out_path, [](GFError err, const DataObjectPtr&) { GF_CORE_LOG_DEBUG("extract archive from ex operation, err: {}", err); - GF_CORE_LOG_INFO("//////////////"); }); RunGpgOperaAsync( @@ -462,10 +461,8 @@ void GpgFileOpera::DecryptVerifyArchive(const QString& in_path, GpgData data_in(in_path, true); GpgData data_out(ex); - GF_CORE_LOG_INFO("-----------------------"); err = CheckGpgError( gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); - GF_CORE_LOG_INFO("++++++++++++++++++++++++"); data_object->Swap({ GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext())), -- cgit v1.2.3