aboutsummaryrefslogtreecommitdiffstats
path: root/lang/qt/src/qgpgmeencryptarchivejob.cpp
diff options
context:
space:
mode:
authorIngo Klöcker <[email protected]>2023-10-27 14:07:16 +0000
committerIngo Klöcker <[email protected]>2023-10-27 14:07:34 +0000
commit46f5d5eeb3b1d0586106b33cecf600ab66170b45 (patch)
tree58b4e222157b0770eb0603174906f55fba5bf525 /lang/qt/src/qgpgmeencryptarchivejob.cpp
parentqt: Refactor removal of output file on cancel or error (diff)
downloadgpgme-46f5d5eeb3b1d0586106b33cecf600ab66170b45.tar.gz
gpgme-46f5d5eeb3b1d0586106b33cecf600ab66170b45.zip
qt: Use temporary .part file names when creating archives
* lang/qt/src/util.h, lang/qt/src/util.cpp (class PartialFileGuard): New. * lang/qt/src/util.cpp (getRandomCharacters, createPartFileName): New. * lang/qt/src/qgpgmeencryptarchivejob.cpp (encrypt_to_filename): Use PartialFileGuard. * lang/qt/src/qgpgmesignarchivejob.cpp (sign_to_filename): Ditto. * lang/qt/src/qgpgmesignencryptarchivejob.cpp (sign_encrypt_to_filename): Ditto. -- When creating signed and/or encrypted archives, gpgtar now writes the result to a temporary file name. On success, the archive is renamed to the final file name. Otherwise, the (partially written) temporary file is removed (if possible). GnuPG-bug-id: 6721
Diffstat (limited to '')
-rw-r--r--lang/qt/src/qgpgmeencryptarchivejob.cpp15
1 files changed, 10 insertions, 5 deletions
diff --git a/lang/qt/src/qgpgmeencryptarchivejob.cpp b/lang/qt/src/qgpgmeencryptarchivejob.cpp
index b8bb4c42..b3586d80 100644
--- a/lang/qt/src/qgpgmeencryptarchivejob.cpp
+++ b/lang/qt/src/qgpgmeencryptarchivejob.cpp
@@ -133,18 +133,23 @@ static QGpgMEEncryptArchiveJob::result_type encrypt_to_filename(Context *ctx,
Context::EncryptionFlags flags,
const QString &baseDirectory)
{
+ PartialFileGuard partFileGuard{outputFileName};
+ if (partFileGuard.tempFileName().isEmpty()) {
+ return std::make_tuple(EncryptionResult{Error::fromCode(GPG_ERR_EEXIST)}, QString{}, Error{});
+ }
+
Data outdata;
#ifdef Q_OS_WIN
- outdata.setFileName(outputFileName.toUtf8().constData());
+ outdata.setFileName(partFileGuard.tempFileName().toUtf8().constData());
#else
- outdata.setFileName(QFile::encodeName(outputFileName).constData());
+ outdata.setFileName(QFile::encodeName(partFileGuard.tempFileName()).constData());
#endif
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);
+ if (!encryptionResult.error().code()) {
+ // the operation succeeded -> save the result under the requested file name
+ partFileGuard.commit();
}
return result;