Skip to content

Commit 9ee5b9d

Browse files
author
Daniel Kroening
authored
Merge pull request #5261 from diffblue/file-rename2
fix file_rename on Windows
2 parents fb07ce0 + a690071 commit 9ee5b9d

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/util/file_util.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,23 +233,21 @@ void file_rename(const std::string &old_path, const std::string &new_path)
233233
MoveFileW(widen(old_path).c_str(), widen(new_path).c_str());
234234

235235
if(MoveFile_result == 0)
236-
throw system_exceptiont("MoveFile failed");
236+
throw system_exceptiont("MoveFileW failed");
237237
}
238238
else
239239
{
240-
// C++17 requires this to be atomic, which is delivered by
241-
// ReplaceFile(). MoveFile() or rename() do not guarantee this.
240+
// C++17 requires this to be atomic.
241+
// MoveFile, MoveFileEx() or rename() do not guarantee this.
242242
// Any existing file at new_path is to be overwritten.
243-
auto ReplaceFile_result = ReplaceFileW(
244-
widen(new_path).c_str(), // note the ordering
243+
// rename() does not do so on Windows.
244+
auto MoveFileEx_result = MoveFileExW(
245245
widen(old_path).c_str(),
246-
nullptr, // lpBackupFileName
247-
0, // dwReplaceFlags
248-
nullptr, // lpExclude
249-
nullptr); // lpReserved
246+
widen(new_path).c_str(),
247+
MOVEFILE_REPLACE_EXISTING); // flags
250248

251-
if(ReplaceFile_result == 0)
252-
throw system_exceptiont("ReplaceFile failed");
249+
if(MoveFileEx_result == 0)
250+
throw system_exceptiont("MoveFileExW failed");
253251
}
254252
#else
255253
int rename_result = rename(old_path.c_str(), new_path.c_str());

0 commit comments

Comments
 (0)