Skip to content

Commit fb95cda

Browse files
committed
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 effdf2f commit fb95cda

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
@@ -1683,28 +1683,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
16831683
#undef rename
16841684
int mingw_rename(const char *pold, const char *pnew)
16851685
{
1686-
DWORD attrs, gle;
1686+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
16871687
int tries = 0;
16881688
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
16891689
if (xutftowcs_long_path(wpold, pold) < 0 ||
16901690
xutftowcs_long_path(wpnew, pnew) < 0)
16911691
return -1;
16921692

1693-
/*
1694-
* Try native rename() first to get errno right.
1695-
* It is based on MoveFile(), which cannot overwrite existing files.
1696-
*/
1697-
if (!_wrename(wpold, wpnew))
1698-
return 0;
1699-
if (errno != EEXIST)
1700-
return -1;
17011693
repeat:
1702-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1694+
if (MoveFileExW(wpold, wpnew,
1695+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
17031696
return 0;
1704-
/* TODO: translate more errors */
17051697
gle = GetLastError();
1706-
if (gle == ERROR_ACCESS_DENIED &&
1707-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1698+
1699+
/* revert file attributes on failure */
1700+
if (attrs != INVALID_FILE_ATTRIBUTES)
1701+
SetFileAttributesW(wpnew, attrs);
1702+
1703+
if (!is_file_in_use_error(gle)) {
1704+
errno = err_win_to_posix(gle);
1705+
return -1;
1706+
}
1707+
1708+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
17081709
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
17091710
DWORD attrsold = GetFileAttributesW(wpold);
17101711
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1715,16 +1716,10 @@ int mingw_rename(const char *pold, const char *pnew)
17151716
return -1;
17161717
}
17171718
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1718-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1719-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1720-
return 0;
1721-
gle = GetLastError();
1722-
/* revert file attributes on failure */
1723-
SetFileAttributesW(wpnew, attrs);
1724-
}
1719+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1720+
goto repeat;
17251721
}
1726-
if (gle == ERROR_ACCESS_DENIED &&
1727-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1722+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
17281723
"Should I try again?", pold, pnew))
17291724
goto repeat;
17301725

0 commit comments

Comments
 (0)