From b7a6663e08929c471571b709e21eee9d26b7b848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sun, 29 Jan 2023 18:41:39 +0100 Subject: [PATCH] Fix updating not deleting the old exe on windows Also fix WriteFile being unable to overwrite existing files. The rename would fail because the file was still open, and the sanity remove in response to that would also fail for the same reason. --- src/common/platform/Common.cpp | 11 ++++++----- src/common/platform/Windows.cpp | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/common/platform/Common.cpp b/src/common/platform/Common.cpp index 03e8be9ab..af9013e61 100644 --- a/src/common/platform/Common.cpp +++ b/src/common/platform/Common.cpp @@ -76,19 +76,20 @@ bool WriteFile(const std::vector &fileData, ByteString filename) { while (true) { - writeFileName = ByteString::Build(filename, ".temp.", random_gen() % 100000); + writeFileName = ByteString::Build(filename, ".temp.", Format::Width(5), Format::Fill('0'), random_gen() % 100000); if (!FileExists(writeFileName)) { break; } } } - std::ofstream f(writeFileName, std::ios::binary); - if (f) + bool ok = false; { - f.write(&fileData[0], fileData.size()); + std::ofstream f(writeFileName, std::ios::binary); + if (f) f.write(&fileData[0], fileData.size()); + ok = bool(f); } - if (!f) + if (!ok) { std::cerr << "WriteFile: " << filename << ": " << strerror(errno) << std::endl; if (replace) diff --git a/src/common/platform/Windows.cpp b/src/common/platform/Windows.cpp index 80014b15d..9e957ebce 100644 --- a/src/common/platform/Windows.cpp +++ b/src/common/platform/Windows.cpp @@ -304,7 +304,7 @@ bool UpdateStart(const std::vector &data) updName = exeName.substr(0, exeName.length() - 4); updName = updName + "_upd.exe"; - if (!MoveFile(Platform::WinWiden(exeName).c_str(), Platform::WinWiden(updName).c_str())) + if (!RenameFile(exeName, updName)) return false; if (!WriteFile(data, exeName))