Skip to content

Commit 095c058

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 33f94e9 commit 095c058

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
@@ -1756,28 +1756,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
17561756
#undef rename
17571757
int mingw_rename(const char *pold, const char *pnew)
17581758
{
1759-
DWORD attrs, gle;
1759+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
17601760
int tries = 0;
17611761
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
17621762
if (xutftowcs_long_path(wpold, pold) < 0 ||
17631763
xutftowcs_long_path(wpnew, pnew) < 0)
17641764
return -1;
17651765

1766-
/*
1767-
* Try native rename() first to get errno right.
1768-
* It is based on MoveFile(), which cannot overwrite existing files.
1769-
*/
1770-
if (!_wrename(wpold, wpnew))
1771-
return 0;
1772-
if (errno != EEXIST)
1773-
return -1;
17741766
repeat:
1775-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1767+
if (MoveFileExW(wpold, wpnew,
1768+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
17761769
return 0;
1777-
/* TODO: translate more errors */
17781770
gle = GetLastError();
1779-
if (gle == ERROR_ACCESS_DENIED &&
1780-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1771+
1772+
/* revert file attributes on failure */
1773+
if (attrs != INVALID_FILE_ATTRIBUTES)
1774+
SetFileAttributesW(wpnew, attrs);
1775+
1776+
if (!is_file_in_use_error(gle)) {
1777+
errno = err_win_to_posix(gle);
1778+
return -1;
1779+
}
1780+
1781+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
17811782
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
17821783
DWORD attrsold = GetFileAttributesW(wpold);
17831784
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1788,16 +1789,10 @@ int mingw_rename(const char *pold, const char *pnew)
17881789
return -1;
17891790
}
17901791
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1791-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1792-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1793-
return 0;
1794-
gle = GetLastError();
1795-
/* revert file attributes on failure */
1796-
SetFileAttributesW(wpnew, attrs);
1797-
}
1792+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1793+
goto repeat;
17981794
}
1799-
if (gle == ERROR_ACCESS_DENIED &&
1800-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1795+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
18011796
"Should I try again?", pold, pnew))
18021797
goto repeat;
18031798

0 commit comments

Comments
 (0)