Skip to content

Commit 259e325

Browse files
kbleesdscho
authored andcommitted
Win32: mingw_rename: support renaming symlinks
MSVCRT's _wrename() cannot rename symlinks over existing files: it returns success without doing anything. Newer MSVCR*.dll versions probably do not have this problem: according to CRT sources, they just call MoveFileEx() with the MOVEFILE_COPY_ALLOWED flag. Get rid of _wrename() and call MoveFileEx() with proper error handling. Signed-off-by: Karsten Blees <[email protected]>
1 parent 4c1110a commit 259e325

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

compat/mingw.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,27 +2019,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
20192019
#undef rename
20202020
int mingw_rename(const char *pold, const char *pnew)
20212021
{
2022-
DWORD attrs, gle;
2022+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
20232023
int tries = 0;
20242024
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
20252025
if (xutftowcs_long_path(wpold, pold) < 0 ||
20262026
xutftowcs_long_path(wpnew, pnew) < 0)
20272027
return -1;
20282028

2029-
/*
2030-
* Try native rename() first to get errno right.
2031-
* It is based on MoveFile(), which cannot overwrite existing files.
2032-
*/
2033-
if (!_wrename(wpold, wpnew))
2034-
return 0;
2035-
if (errno != EEXIST)
2036-
return -1;
20372029
repeat:
2038-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2030+
if (MoveFileExW(wpold, wpnew,
2031+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
20392032
return 0;
2040-
/* TODO: translate more errors */
20412033
gle = GetLastError();
2042-
if (gle == ERROR_ACCESS_DENIED &&
2034+
2035+
/* revert file attributes on failure */
2036+
if (attrs != INVALID_FILE_ATTRIBUTES)
2037+
SetFileAttributesW(wpnew, attrs);
2038+
2039+
if (!is_file_in_use_error(gle)) {
2040+
errno = err_win_to_posix(gle);
2041+
return -1;
2042+
}
2043+
2044+
if (attrs == INVALID_FILE_ATTRIBUTES &&
20432045
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
20442046
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
20452047
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2051,16 +2053,10 @@ int mingw_rename(const char *pold, const char *pnew)
20512053
return -1;
20522054
}
20532055
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2054-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2055-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2056-
return 0;
2057-
gle = GetLastError();
2058-
/* revert file attributes on failure */
2059-
SetFileAttributesW(wpnew, attrs);
2060-
}
2056+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2057+
goto repeat;
20612058
}
2062-
if (gle == ERROR_ACCESS_DENIED &&
2063-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2059+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
20642060
"Should I try again?", pold, pnew))
20652061
goto repeat;
20662062

0 commit comments

Comments
 (0)