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
This commit is contained in:
Aaron Miller 2023-04-26 17:57:25 -07:00 committed by AT
parent ee5c58c26c
commit 18fa61c025

View File

@ -286,6 +286,14 @@ void HashAndSaveFile::hashAndSave(const QString &expectedHash, const QString &sa
// The file save needs the tempFile closed // The file save needs the tempFile closed
tempFile->close(); 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 // Reopen the tempFile for copying
if (!tempFile->open()) { if (!tempFile->open()) {
qWarning() << "ERROR: Could not open temp file at finish:" qWarning() << "ERROR: Could not open temp file at finish:"