Skip to content

Commit 30c5ad3

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 0f1397a commit 30c5ad3

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
@@ -1748,28 +1748,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
17481748
#undef rename
17491749
int mingw_rename(const char *pold, const char *pnew)
17501750
{
1751-
DWORD attrs, gle;
1751+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
17521752
int tries = 0;
17531753
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
17541754
if (xutftowcs_long_path(wpold, pold) < 0 ||
17551755
xutftowcs_long_path(wpnew, pnew) < 0)
17561756
return -1;
17571757

1758-
/*
1759-
* Try native rename() first to get errno right.
1760-
* It is based on MoveFile(), which cannot overwrite existing files.
1761-
*/
1762-
if (!_wrename(wpold, wpnew))
1763-
return 0;
1764-
if (errno != EEXIST)
1765-
return -1;
17661758
repeat:
1767-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1759+
if (MoveFileExW(wpold, wpnew,
1760+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
17681761
return 0;
1769-
/* TODO: translate more errors */
17701762
gle = GetLastError();
1771-
if (gle == ERROR_ACCESS_DENIED &&
1772-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1763+
1764+
/* revert file attributes on failure */
1765+
if (attrs != INVALID_FILE_ATTRIBUTES)
1766+
SetFileAttributesW(wpnew, attrs);
1767+
1768+
if (!is_file_in_use_error(gle)) {
1769+
errno = err_win_to_posix(gle);
1770+
return -1;
1771+
}
1772+
1773+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
17731774
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
17741775
DWORD attrsold = GetFileAttributesW(wpold);
17751776
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1780,16 +1781,10 @@ int mingw_rename(const char *pold, const char *pnew)
17801781
return -1;
17811782
}
17821783
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1783-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1784-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1785-
return 0;
1786-
gle = GetLastError();
1787-
/* revert file attributes on failure */
1788-
SetFileAttributesW(wpnew, attrs);
1789-
}
1784+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1785+
goto repeat;
17901786
}
1791-
if (gle == ERROR_ACCESS_DENIED &&
1792-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1787+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
17931788
"Should I try again?", pold, pnew))
17941789
goto repeat;
17951790

0 commit comments

Comments
 (0)