From 1f371be3959d1a9d322845e69eb1fb925c464cff Mon Sep 17 00:00:00 2001 From: Aaron Miller Date: Wed, 26 Apr 2023 17:57:25 -0700 Subject: [PATCH] download: atomically move tempfile when possible should save unnecessary time and I/O and eliminate the possibility of the file being improperly truncated when the temp file is on the same filesystem as the destination path --- download.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/download.cpp b/download.cpp index 0fb1d2e3..80dab3e2 100644 --- a/download.cpp +++ b/download.cpp @@ -286,6 +286,14 @@ void HashAndSaveFile::hashAndSave(const QString &expectedHash, const QString &sa // The file save needs the tempFile closed tempFile->close(); + // Attempt to *move* the verified tempfile into place - this should be atomic + // but will only work if the destination is on the same filesystem + if (tempFile->rename(saveFilePath)) { + tempFile->setAutoRemove(false); + emit hashAndSaveFinished(true, tempFile, modelReply); + return; + } + // Reopen the tempFile for copying if (!tempFile->open()) { qWarning() << "ERROR: Could not open temp file at finish:"