From 40ca3d58963884a876c826dcf5c32673b8ddc084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Wed, 16 Aug 2023 14:23:15 +0200 Subject: 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 --- lang/qt/tests/run-encryptarchivejob.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lang/qt/tests/run-encryptarchivejob.cpp') diff --git a/lang/qt/tests/run-encryptarchivejob.cpp b/lang/qt/tests/run-encryptarchivejob.cpp index afbb13c0..c1d617fb 100644 --- a/lang/qt/tests/run-encryptarchivejob.cpp +++ b/lang/qt/tests/run-encryptarchivejob.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -63,6 +64,7 @@ struct CommandLineOptions { bool sign = false; QString archiveName; QString baseDirectory; + std::chrono::seconds cancelTimeout{0}; std::vector 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) { -- cgit v1.2.3