Use MoveFileExW to implement FioRenameFile on Windows

This is to allow renaming over an existing file
pull/688/head
Jonathan G Rennison 1 month ago
parent 083d91a582
commit 71227f61d8

@ -383,7 +383,7 @@ void FioCreateDirectory(const std::string &name)
bool FioRenameFile(const std::string &oldname, const std::string &newname) bool FioRenameFile(const std::string &oldname, const std::string &newname)
{ {
#if defined(_WIN32) #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 #else
return rename(oldname.c_str(), newname.c_str()) == 0; return rename(oldname.c_str(), newname.c_str()) == 0;
#endif #endif

@ -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)); 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"); 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 this->temp_name.clear(); // Now no need to unlink temporary name
} }

Loading…
Cancel
Save