Skip to content

Commit fe11a0a

Browse files
committed
Try to make rename more robust on Windows
1 parent 6121cc7 commit fe11a0a

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/fs.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ bool sizeMatches(const fs::path& path, u64 initialSize) {
1818
return !initialSize || (initialSize == fs::file_size(path, dummy));
1919
}
2020

21-
void removeFile(const fs::path& path) noexcept {
22-
std::error_code dummy;
23-
fs::remove(path, dummy);
24-
}
25-
2621
} // namespace
2722

2823
void fancyRename(const fs::path& src, const fs::path& dst) {
@@ -31,13 +26,12 @@ void fancyRename(const fs::path& src, const fs::path& dst) {
3126
// But this behavior is not obeyed by some Windows implementations (mingw/msys?)
3227
// In that case, we attempt to explicitly remove the destination beforehand
3328

34-
try {
35-
fs::rename(src, dst);
36-
} catch (const fs::filesystem_error& e) {
37-
removeFile(dst);
38-
39-
// Retry the rename following destination removal above.
40-
// If this rename does not succeed, let it out-throw.
29+
std::error_code error{};
30+
fs::rename(src, dst, error);
31+
if (error) {
32+
log("rename %s -> %s error %s, trying remove\n",
33+
src.string().c_str(), dst.string().c_str(), error.message().c_str());
34+
fs::remove(dst);
4135
fs::rename(src, dst);
4236
}
4337
}

0 commit comments

Comments
 (0)