From 8d8985bda1747a18b3b1378ea7c627302a61464a Mon Sep 17 00:00:00 2001 From: Ingo Klöcker Date: Fri, 27 Oct 2023 11:27:29 +0200 Subject: qt: Refactor removal of output file on cancel or error * lang/qt/src/util.h, lang/qt/src/util.cpp (removeFile): New. * lang/qt/src/qgpgmeencryptarchivejob.cpp (encrypt): Move removal of output file from here (encrypt_to_filename): ... to here and use new function. * lang/qt/src/qgpgmesignarchivejob.cpp (sign): Move removal of output file from here (sign_to_filename): ... to here and use new function. * lang/qt/src/qgpgmesignencryptarchivejob.cpp (sign_encrypt): Move removal of output file from here (sign_encrypt_to_filename): ... to here and use new function. -- GnuPG-bug-id: 6721 --- lang/qt/src/qgpgmeencryptarchivejob.cpp | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'lang/qt/src/qgpgmeencryptarchivejob.cpp') diff --git a/lang/qt/src/qgpgmeencryptarchivejob.cpp b/lang/qt/src/qgpgmeencryptarchivejob.cpp index 07095ea1..b8bb4c42 100644 --- a/lang/qt/src/qgpgmeencryptarchivejob.cpp +++ b/lang/qt/src/qgpgmeencryptarchivejob.cpp @@ -44,6 +44,7 @@ #include "encryptarchivejob_p.h" #include "filelistdataprovider.h" #include "qgpgme_debug.h" +#include "util.h" #include @@ -104,20 +105,6 @@ static QGpgMEEncryptArchiveJob::result_type encrypt(Context *ctx, flags = static_cast(flags | Context::EncryptArchive); const auto encryptionResult = ctx->encrypt(recipients, indata, outdata, flags); -#ifdef Q_OS_WIN - const auto outputFileName = QString::fromUtf8(outdata.fileName()); -#else - const auto outputFileName = QFile::decodeName(outdata.fileName()); -#endif - if (!outputFileName.isEmpty() && encryptionResult.error().code()) { - // ensure that the output file is removed if the operation was canceled or failed - if (QFile::exists(outputFileName)) { - qCDebug(QGPGME_LOG) << __func__ << "Removing output file" << outputFileName << "after error or cancel"; - if (!QFile::remove(outputFileName)) { - qCDebug(QGPGME_LOG) << __func__ << "Removing output file" << outputFileName << "failed"; - } - } - } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(encryptionResult, log, ae); @@ -142,18 +129,25 @@ static QGpgMEEncryptArchiveJob::result_type encrypt_to_io_device(Context *ctx, static QGpgMEEncryptArchiveJob::result_type encrypt_to_filename(Context *ctx, const std::vector &recipients, const std::vector &paths, - const QString &outputFile, + const QString &outputFileName, Context::EncryptionFlags flags, const QString &baseDirectory) { Data outdata; #ifdef Q_OS_WIN - outdata.setFileName(outputFile.toUtf8().constData()); + outdata.setFileName(outputFileName.toUtf8().constData()); #else - outdata.setFileName(QFile::encodeName(outputFile).constData()); + outdata.setFileName(QFile::encodeName(outputFileName).constData()); #endif - return encrypt(ctx, recipients, paths, outdata, flags, baseDirectory); + const auto result = encrypt(ctx, recipients, paths, outdata, flags, baseDirectory); + const auto &encryptionResult = std::get<0>(result); + if (encryptionResult.error().code()) { + // ensure that the output file is removed if the operation was canceled or failed + removeFile(outputFileName); + } + + return result; } GpgME::Error QGpgMEEncryptArchiveJob::start(const std::vector &recipients, -- cgit