qt: Clean up after failure or cancel of sign/encrypt archive operation

* lang/qt/src/qgpgmeencryptarchivejob.cpp (encrypt): Remove output file
if operation was canceled or failed.
* lang/qt/src/qgpgmesignarchivejob.cpp (sign): Ditto.
* lang/qt/src/qgpgmesignencryptarchivejob.cpp (sign_encrypt): Ditto.

* lang/qt/tests/run-encryptarchivejob.cpp (CommandLineOptions): Add
field cancelTimeout.
(parseCommandLine): Add option --cancel-after. Parse option value.
(main): Check for invalid cancel timeout. Start timer for canceling
the job.
* lang/qt/tests/run-signarchivejob.cpp (CommandLineOptions): Add
field cancelTimeout.
(parseCommandLine): Add option --cancel-after. Parse option value.
(main): Check for invalid cancel timeout. Start timer for canceling
the job.
--

This change ensures that the output file is removed if the creation of
a signed or encrypted archive was canceled or failed. The new option
of the test runners enables testing the cancelation of the jobs.

GnuPG-bug-id: 6584
This commit is contained in:
Ingo Klöcker 2023-08-16 14:23:15 +02:00
parent c3171d0cf1
commit 40ca3d5896
No known key found for this signature in database
GPG Key ID: F5A5D1692277A1E9
5 changed files with 87 additions and 5 deletions

View File

@ -43,6 +43,7 @@
#include "dataprovider.h"
#include "encryptarchivejob_p.h"
#include "filelistdataprovider.h"
#include "qgpgme_debug.h"
#include <QFile>
@ -101,10 +102,21 @@ static QGpgMEEncryptArchiveJob::result_type encrypt(Context *ctx,
}
flags = static_cast<Context::EncryptionFlags>(flags | Context::EncryptArchive);
const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, flags);
const auto encryptionResult = ctx->encrypt(recipients, indata, outdata, flags);
const auto outputFileName = QFile::decodeName(outdata.fileName());
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(res, log, ae);
return std::make_tuple(encryptionResult, log, ae);
}
static QGpgMEEncryptArchiveJob::result_type encrypt_to_io_device(Context *ctx,

View File

@ -43,6 +43,7 @@
#include "dataprovider.h"
#include "signarchivejob_p.h"
#include "filelistdataprovider.h"
#include "qgpgme_debug.h"
#include <QFile>
@ -108,10 +109,21 @@ static QGpgMESignArchiveJob::result_type sign(Context *ctx,
}
}
const SigningResult res = ctx->sign(indata, outdata, GpgME::SignArchive);
const auto signingResult = ctx->sign(indata, outdata, GpgME::SignArchive);
const auto outputFileName = QFile::decodeName(outdata.fileName());
if (!outputFileName.isEmpty() && signingResult.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(res, log, ae);
return std::make_tuple(signingResult, log, ae);
}
static QGpgMESignArchiveJob::result_type sign_to_io_device(Context *ctx,

View File

@ -43,6 +43,7 @@
#include "dataprovider.h"
#include "signencryptarchivejob_p.h"
#include "filelistdataprovider.h"
#include "qgpgme_debug.h"
#include <QFile>
@ -112,9 +113,22 @@ static QGpgMESignEncryptArchiveJob::result_type sign_encrypt(Context *ctx,
encryptionFlags = static_cast<Context::EncryptionFlags>(encryptionFlags | Context::EncryptArchive);
const auto res = ctx->signAndEncrypt(recipients, indata, outdata, encryptionFlags);
const auto &signingResult = res.first;
const auto &encryptionResult = res.second;
const auto outputFileName = QFile::decodeName(outdata.fileName());
if (!outputFileName.isEmpty() && (signingResult.error().code() || 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(res.first, res.second, log, ae);
return std::make_tuple(signingResult, encryptionResult, log, ae);
}
static QGpgMESignEncryptArchiveJob::result_type sign_encrypt_to_io_device(Context *ctx,

View File

@ -44,6 +44,7 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTimer>
#include <context.h>
#include <encryptionresult.h>
@ -63,6 +64,7 @@ struct CommandLineOptions {
bool sign = false;
QString archiveName;
QString baseDirectory;
std::chrono::seconds cancelTimeout{0};
std::vector<QString> filesAndDirectories;
};
@ -78,6 +80,7 @@ CommandLineOptions parseCommandLine(const QStringList &arguments)
{{"o", "output"}, "Write output to FILE.", "FILE"},
{{"a", "armor"}, "Create ASCII armored output."},
{{"C", "directory"}, "Change to DIRECTORY before creating the archive.", "DIRECTORY"},
{"cancel-after", "Cancel the running job after SECONDS seconds.", "SECONDS"},
});
parser.addPositionalArgument("files", "Files and directories to add to the archive", "[files] [directories]");
@ -92,6 +95,13 @@ CommandLineOptions parseCommandLine(const QStringList &arguments)
options.sign = parser.isSet("sign");
options.archiveName = parser.value("output");
options.baseDirectory = parser.value("directory");
if (parser.isSet("cancel-after")) {
bool ok;
options.cancelTimeout = std::chrono::seconds{parser.value("cancel-after").toInt(&ok)};
if (!ok) {
options.cancelTimeout = std::chrono::seconds{-1};
}
}
std::copy(args.begin(), args.end(), std::back_inserter(options.filesAndDirectories));
return options;
@ -115,6 +125,9 @@ int main(int argc, char **argv)
app.setApplicationName("run-encryptarchivejob");
const auto options = parseCommandLine(app.arguments());
if (options.cancelTimeout.count() < 0) {
std::cerr << "Ignoring invalid timeout for cancel." << std::endl;
}
if ((options.sign && !QGpgME::SignEncryptArchiveJob::isSupported())
|| (!options.sign && !QGpgME::EncryptArchiveJob::isSupported())) {
@ -147,6 +160,12 @@ int main(int argc, char **argv)
std::cerr << "Encryption Result: " << encryptionResult << std::endl;
qApp->quit();
});
if (options.cancelTimeout.count() > 0) {
QTimer::singleShot(options.cancelTimeout, job, [job]() {
std::cerr << "Canceling job" << std::endl;
job->slotCancel();
});
}
GpgME::Error err;
if (output) {
@ -172,6 +191,12 @@ int main(int argc, char **argv)
std::cerr << "Result: " << result << std::endl;
qApp->quit();
});
if (options.cancelTimeout.count() > 0) {
QTimer::singleShot(options.cancelTimeout, job, [job]() {
std::cerr << "Canceling job" << std::endl;
job->slotCancel();
});
}
GpgME::Error err;
if (output) {

View File

@ -43,6 +43,7 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTimer>
#include <context.h>
#include <signingresult.h>
@ -60,6 +61,7 @@ struct CommandLineOptions {
bool armor;
QString archiveName;
QString baseDirectory;
std::chrono::seconds cancelTimeout{0};
std::vector<QString> filesAndDirectories;
};
@ -74,6 +76,7 @@ CommandLineOptions parseCommandLine(const QStringList &arguments)
{{"o", "output"}, "Write output to FILE.", "FILE"},
{{"a", "armor"}, "Create ASCII armored output."},
{{"C", "directory"}, "Change to DIRECTORY before creating the archive.", "DIRECTORY"},
{"cancel-after", "Cancel the running job after SECONDS seconds.", "SECONDS"},
});
parser.addPositionalArgument("files", "Files and directories to add to the archive", "[files] [directories]");
@ -87,6 +90,13 @@ CommandLineOptions parseCommandLine(const QStringList &arguments)
options.armor = parser.isSet("armor");
options.archiveName = parser.value("output");
options.baseDirectory = parser.value("directory");
if (parser.isSet("cancel-after")) {
bool ok;
options.cancelTimeout = std::chrono::seconds{parser.value("cancel-after").toInt(&ok)};
if (!ok) {
options.cancelTimeout = std::chrono::seconds{-1};
}
}
std::copy(args.begin(), args.end(), std::back_inserter(options.filesAndDirectories));
return options;
@ -110,6 +120,9 @@ int main(int argc, char **argv)
app.setApplicationName("run-signarchivejob");
const auto options = parseCommandLine(app.arguments());
if (options.cancelTimeout.count() < 0) {
std::cerr << "Ignoring invalid timeout for cancel." << std::endl;
}
if (!QGpgME::SignArchiveJob::isSupported()) {
std::cerr << "Error: Signing archives is not supported by your version of gpg." << std::endl;
@ -139,6 +152,12 @@ int main(int argc, char **argv)
std::cerr << "Result: " << result << std::endl;
qApp->quit();
});
if (options.cancelTimeout.count() > 0) {
QTimer::singleShot(options.cancelTimeout, job, [job]() {
std::cerr << "Canceling job" << std::endl;
job->slotCancel();
});
}
GpgME::Error err;
if (output) {