From ecdf5eea588c0308d6892077b75e3674391c26b6 Mon Sep 17 00:00:00 2001 From: idealvin Date: Mon, 6 Nov 2023 22:10:01 +0800 Subject: [PATCH] fix fs::mv() on windows --- src/fs_win.cc | 11 ++++++++--- unitest/fs.cc | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/fs_win.cc b/src/fs_win.cc index 52142405e..c5f32209f 100644 --- a/src/fs_win.cc +++ b/src/fs_win.cc @@ -208,7 +208,9 @@ bool mv(const char* from, const char* to) { return MoveFileExW(x, y, MOVEFILE_COPY_ALLOWED); } if (!(b & g_attr_dir)) { - return MoveFileExW(x, y, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED); + DWORD f = MOVEFILE_COPY_ALLOWED; + if (!(a & g_attr_dir)) f |= MOVEFILE_REPLACE_EXISTING; + return MoveFileExW(x, y, f); } const char* p = strrchr(from, '/'); @@ -223,12 +225,15 @@ bool mv(const char* from, const char* to) { if (w == g_bad_attr) { return MoveFileExW(x, y, MOVEFILE_COPY_ALLOWED); } + if (!(w & g_attr_dir)) { - return MoveFileExW(x, y, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED); + DWORD f = MOVEFILE_COPY_ALLOWED; + if (!(a & g_attr_dir)) f |= MOVEFILE_REPLACE_EXISTING; + return MoveFileExW(x, y, f); } if (a & g_attr_dir) RemoveDirectoryW(y); // remove dir y if it is empty - return MoveFileExW(x, y, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED); + return MoveFileExW(x, y, MOVEFILE_COPY_ALLOWED); } bool rename(const char* from, const char* to) { diff --git a/unitest/fs.cc b/unitest/fs.cc index bc05c6804..40d07392d 100644 --- a/unitest/fs.cc +++ b/unitest/fs.cc @@ -123,6 +123,7 @@ DEF_test(fs) { o.open("xxd/xxs", 'w'); o.close(); EXPECT_EQ(fs::mv("xxs", "xxd"), false); + EXPECT_EQ(fs::mv("xxs", "xxd/xxs"), false); } #ifndef _WIN32