From 71227f61d80a4a3143101124015fda293e8b9e4e Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 20 Apr 2024 20:46:10 +0100 Subject: [PATCH] Use MoveFileExW to implement FioRenameFile on Windows This is to allow renaming over an existing file --- src/fileio.cpp | 2 +- src/sl/saveload.cpp | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/fileio.cpp b/src/fileio.cpp index d61aef58b5..163a20dc86 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -383,7 +383,7 @@ void FioCreateDirectory(const std::string &name) bool FioRenameFile(const std::string &oldname, const std::string &newname) { #if defined(_WIN32) - return _wrename(OTTD2FS(oldname).c_str(), OTTD2FS(newname).c_str()) == 0; + return MoveFileExW(OTTD2FS(oldname).c_str(), OTTD2FS(newname).c_str(), MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0; #else return rename(oldname.c_str(), newname.c_str()) == 0; #endif diff --git a/src/sl/saveload.cpp b/src/sl/saveload.cpp index 0b3238b2c3..563bcf632f 100644 --- a/src/sl/saveload.cpp +++ b/src/sl/saveload.cpp @@ -2908,11 +2908,6 @@ struct FileWriter : SaveFilter { SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE, stdstr_fmt("Temporary save file does not have expected file size: " PRINTF_SIZE " != " PRINTF_SIZE, (size_t)st.st_size, save_size)); } -#if defined(_WIN32) - /* Renaming over an existing file is not supported on Windows, manually unlink the target filename first */ - unlink(this->target_name.c_str()); -#endif - if (!FioRenameFile(this->temp_name, this->target_name)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE, "Failed to rename temporary save file to target name"); this->temp_name.clear(); // Now no need to unlink temporary name }