Skip to content

Commit

Permalink
fix fs::mv() on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed Nov 6, 2023
1 parent 12efa6a commit ecdf5ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/fs_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions unitest/fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ecdf5ee

Please sign in to comment.