Skip to content

Commit b9fe1c2

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 b8879c7 commit b9fe1c2

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

compat/mingw.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,28 +1678,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
16781678
#undef rename
16791679
int mingw_rename(const char *pold, const char *pnew)
16801680
{
1681-
DWORD attrs, gle;
1681+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
16821682
int tries = 0;
16831683
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
16841684
if (xutftowcs_long_path(wpold, pold) < 0 ||
16851685
xutftowcs_long_path(wpnew, pnew) < 0)
16861686
return -1;
16871687

1688-
/*
1689-
* Try native rename() first to get errno right.
1690-
* It is based on MoveFile(), which cannot overwrite existing files.
1691-
*/
1692-
if (!_wrename(wpold, wpnew))
1693-
return 0;
1694-
if (errno != EEXIST)
1695-
return -1;
16961688
repeat:
1697-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1689+
if (MoveFileExW(wpold, wpnew,
1690+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
16981691
return 0;
1699-
/* TODO: translate more errors */
17001692
gle = GetLastError();
1701-
if (gle == ERROR_ACCESS_DENIED &&
1702-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1693+
1694+
/* revert file attributes on failure */
1695+
if (attrs != INVALID_FILE_ATTRIBUTES)
1696+
SetFileAttributesW(wpnew, attrs);
1697+
1698+
if (!is_file_in_use_error(gle)) {
1699+
errno = err_win_to_posix(gle);
1700+
return -1;
1701+
}
1702+
1703+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
17031704
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
17041705
DWORD attrsold = GetFileAttributesW(wpold);
17051706
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1710,16 +1711,10 @@ int mingw_rename(const char *pold, const char *pnew)
17101711
return -1;
17111712
}
17121713
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1713-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1714-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1715-
return 0;
1716-
gle = GetLastError();
1717-
/* revert file attributes on failure */
1718-
SetFileAttributesW(wpnew, attrs);
1719-
}
1714+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1715+
goto repeat;
17201716
}
1721-
if (gle == ERROR_ACCESS_DENIED &&
1722-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1717+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
17231718
"Should I try again?", pold, pnew))
17241719
goto repeat;
17251720

0 commit comments

Comments
 (0)