Skip to content

Commit edf2f54

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 89314bf commit edf2f54

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
@@ -1712,28 +1712,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
17121712
#undef rename
17131713
int mingw_rename(const char *pold, const char *pnew)
17141714
{
1715-
DWORD attrs, gle;
1715+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
17161716
int tries = 0;
17171717
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
17181718
if (xutftowcs_long_path(wpold, pold) < 0 ||
17191719
xutftowcs_long_path(wpnew, pnew) < 0)
17201720
return -1;
17211721

1722-
/*
1723-
* Try native rename() first to get errno right.
1724-
* It is based on MoveFile(), which cannot overwrite existing files.
1725-
*/
1726-
if (!_wrename(wpold, wpnew))
1727-
return 0;
1728-
if (errno != EEXIST)
1729-
return -1;
17301722
repeat:
1731-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1723+
if (MoveFileExW(wpold, wpnew,
1724+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
17321725
return 0;
1733-
/* TODO: translate more errors */
17341726
gle = GetLastError();
1735-
if (gle == ERROR_ACCESS_DENIED &&
1736-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1727+
1728+
/* revert file attributes on failure */
1729+
if (attrs != INVALID_FILE_ATTRIBUTES)
1730+
SetFileAttributesW(wpnew, attrs);
1731+
1732+
if (!is_file_in_use_error(gle)) {
1733+
errno = err_win_to_posix(gle);
1734+
return -1;
1735+
}
1736+
1737+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
17371738
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
17381739
DWORD attrsold = GetFileAttributesW(wpold);
17391740
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1744,16 +1745,10 @@ int mingw_rename(const char *pold, const char *pnew)
17441745
return -1;
17451746
}
17461747
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1747-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1748-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1749-
return 0;
1750-
gle = GetLastError();
1751-
/* revert file attributes on failure */
1752-
SetFileAttributesW(wpnew, attrs);
1753-
}
1748+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1749+
goto repeat;
17541750
}
1755-
if (gle == ERROR_ACCESS_DENIED &&
1756-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1751+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
17571752
"Should I try again?", pold, pnew))
17581753
goto repeat;
17591754

0 commit comments

Comments
 (0)