From 9673be17cbd5f0aa094eb8685226714be1d4f7e3 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 15 Sep 2021 23:17:24 +0800 Subject: [PATCH] Improve handling when writing to temporary files Let QTemporaryFile remove incomplete written file when error occurs. "XXXXXX" template is not strictly required according to Qt doc. --- src/base/net/downloadhandlerimpl.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/base/net/downloadhandlerimpl.cpp b/src/base/net/downloadhandlerimpl.cpp index 654245e05..93a2c33eb 100644 --- a/src/base/net/downloadhandlerimpl.cpp +++ b/src/base/net/downloadhandlerimpl.cpp @@ -46,14 +46,13 @@ namespace if (!filePath.isEmpty()) return Utils::IO::saveToFile(filePath, replyData).has_value(); - QTemporaryFile tmpfile {Utils::Fs::tempPath() + "XXXXXX"}; - tmpfile.setAutoRemove(false); - - if (!tmpfile.open()) + QTemporaryFile file {Utils::Fs::tempPath()}; + if (!file.open() || (file.write(replyData) != replyData.length()) || !file.flush()) return false; - filePath = tmpfile.fileName(); - return (tmpfile.write(replyData) == replyData.length()); + file.setAutoRemove(false); + filePath = file.fileName(); + return true; } }