Skip to content

Commit 514aca2

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 a299d14 commit 514aca2

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
@@ -2021,27 +2021,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
20212021
#undef rename
20222022
int mingw_rename(const char *pold, const char *pnew)
20232023
{
2024-
DWORD attrs, gle;
2024+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
20252025
int tries = 0;
20262026
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
20272027
if (xutftowcs_long_path(wpold, pold) < 0 ||
20282028
xutftowcs_long_path(wpnew, pnew) < 0)
20292029
return -1;
20302030

2031-
/*
2032-
* Try native rename() first to get errno right.
2033-
* It is based on MoveFile(), which cannot overwrite existing files.
2034-
*/
2035-
if (!_wrename(wpold, wpnew))
2036-
return 0;
2037-
if (errno != EEXIST)
2038-
return -1;
20392031
repeat:
2040-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2032+
if (MoveFileExW(wpold, wpnew,
2033+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
20412034
return 0;
2042-
/* TODO: translate more errors */
20432035
gle = GetLastError();
2044-
if (gle == ERROR_ACCESS_DENIED &&
2036+
2037+
/* revert file attributes on failure */
2038+
if (attrs != INVALID_FILE_ATTRIBUTES)
2039+
SetFileAttributesW(wpnew, attrs);
2040+
2041+
if (!is_file_in_use_error(gle)) {
2042+
errno = err_win_to_posix(gle);
2043+
return -1;
2044+
}
2045+
2046+
if (attrs == INVALID_FILE_ATTRIBUTES &&
20452047
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
20462048
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
20472049
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2053,16 +2055,10 @@ int mingw_rename(const char *pold, const char *pnew)
20532055
return -1;
20542056
}
20552057
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2056-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2057-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2058-
return 0;
2059-
gle = GetLastError();
2060-
/* revert file attributes on failure */
2061-
SetFileAttributesW(wpnew, attrs);
2062-
}
2058+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2059+
goto repeat;
20632060
}
2064-
if (gle == ERROR_ACCESS_DENIED &&
2065-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2061+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
20662062
"Should I try again?", pold, pnew))
20672063
goto repeat;
20682064

0 commit comments

Comments
 (0)