Skip to content

fix file_rename on Windows #5261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/util/file_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,21 @@ void file_rename(const std::string &old_path, const std::string &new_path)
MoveFileW(widen(old_path).c_str(), widen(new_path).c_str());

if(MoveFile_result == 0)
throw system_exceptiont("MoveFile failed");
throw system_exceptiont("MoveFileW failed");
}
else
{
// C++17 requires this to be atomic, which is delivered by
// ReplaceFile(). MoveFile() or rename() do not guarantee this.
// C++17 requires this to be atomic.
// MoveFile, MoveFileEx() or rename() do not guarantee this.
// Any existing file at new_path is to be overwritten.
auto ReplaceFile_result = ReplaceFileW(
widen(new_path).c_str(), // note the ordering
// rename() does not do so on Windows.
auto MoveFileEx_result = MoveFileExW(
widen(old_path).c_str(),
nullptr, // lpBackupFileName
0, // dwReplaceFlags
nullptr, // lpExclude
nullptr); // lpReserved
widen(new_path).c_str(),
MOVEFILE_REPLACE_EXISTING); // flags

if(ReplaceFile_result == 0)
throw system_exceptiont("ReplaceFile failed");
if(MoveFileEx_result == 0)
throw system_exceptiont("MoveFileExW failed");
}
#else
int rename_result = rename(old_path.c_str(), new_path.c_str());
Expand Down