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.
This commit is contained in:
Tamás Bálint Misius 2023-01-29 18:41:39 +01:00
parent bd667dddad
commit b7a6663e08
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
2 changed files with 7 additions and 6 deletions

View File

@ -76,19 +76,20 @@ bool WriteFile(const std::vector<char> &fileData, ByteString filename)
{ {
while (true) 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)) if (!FileExists(writeFileName))
{ {
break; break;
} }
} }
} }
std::ofstream f(writeFileName, std::ios::binary); bool ok = false;
if (f)
{ {
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; std::cerr << "WriteFile: " << filename << ": " << strerror(errno) << std::endl;
if (replace) if (replace)

View File

@ -304,7 +304,7 @@ bool UpdateStart(const std::vector<char> &data)
updName = exeName.substr(0, exeName.length() - 4); updName = exeName.substr(0, exeName.length() - 4);
updName = updName + "_upd.exe"; updName = updName + "_upd.exe";
if (!MoveFile(Platform::WinWiden(exeName).c_str(), Platform::WinWiden(updName).c_str())) if (!RenameFile(exeName, updName))
return false; return false;
if (!WriteFile(data, exeName)) if (!WriteFile(data, exeName))