Skip to content

Commit e571860

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 c431fcc commit e571860

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
@@ -1788,28 +1788,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
17881788
#undef rename
17891789
int mingw_rename(const char *pold, const char *pnew)
17901790
{
1791-
DWORD attrs, gle;
1791+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
17921792
int tries = 0;
17931793
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
17941794
if (xutftowcs_long_path(wpold, pold) < 0 ||
17951795
xutftowcs_long_path(wpnew, pnew) < 0)
17961796
return -1;
17971797

1798-
/*
1799-
* Try native rename() first to get errno right.
1800-
* It is based on MoveFile(), which cannot overwrite existing files.
1801-
*/
1802-
if (!_wrename(wpold, wpnew))
1803-
return 0;
1804-
if (errno != EEXIST)
1805-
return -1;
18061798
repeat:
1807-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1799+
if (MoveFileExW(wpold, wpnew,
1800+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
18081801
return 0;
1809-
/* TODO: translate more errors */
18101802
gle = GetLastError();
1811-
if (gle == ERROR_ACCESS_DENIED &&
1812-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1803+
1804+
/* revert file attributes on failure */
1805+
if (attrs != INVALID_FILE_ATTRIBUTES)
1806+
SetFileAttributesW(wpnew, attrs);
1807+
1808+
if (!is_file_in_use_error(gle)) {
1809+
errno = err_win_to_posix(gle);
1810+
return -1;
1811+
}
1812+
1813+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
18131814
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
18141815
DWORD attrsold = GetFileAttributesW(wpold);
18151816
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1820,16 +1821,10 @@ int mingw_rename(const char *pold, const char *pnew)
18201821
return -1;
18211822
}
18221823
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1823-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1824-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1825-
return 0;
1826-
gle = GetLastError();
1827-
/* revert file attributes on failure */
1828-
SetFileAttributesW(wpnew, attrs);
1829-
}
1824+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1825+
goto repeat;
18301826
}
1831-
if (gle == ERROR_ACCESS_DENIED &&
1832-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1827+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
18331828
"Should I try again?", pold, pnew))
18341829
goto repeat;
18351830

0 commit comments

Comments
 (0)