Skip to content

Commit 70dfdec

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 1242a2e commit 70dfdec

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
@@ -1772,28 +1772,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
17721772
#undef rename
17731773
int mingw_rename(const char *pold, const char *pnew)
17741774
{
1775-
DWORD attrs, gle;
1775+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
17761776
int tries = 0;
17771777
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
17781778
if (xutftowcs_long_path(wpold, pold) < 0 ||
17791779
xutftowcs_long_path(wpnew, pnew) < 0)
17801780
return -1;
17811781

1782-
/*
1783-
* Try native rename() first to get errno right.
1784-
* It is based on MoveFile(), which cannot overwrite existing files.
1785-
*/
1786-
if (!_wrename(wpold, wpnew))
1787-
return 0;
1788-
if (errno != EEXIST)
1789-
return -1;
17901782
repeat:
1791-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1783+
if (MoveFileExW(wpold, wpnew,
1784+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
17921785
return 0;
1793-
/* TODO: translate more errors */
17941786
gle = GetLastError();
1795-
if (gle == ERROR_ACCESS_DENIED &&
1796-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1787+
1788+
/* revert file attributes on failure */
1789+
if (attrs != INVALID_FILE_ATTRIBUTES)
1790+
SetFileAttributesW(wpnew, attrs);
1791+
1792+
if (!is_file_in_use_error(gle)) {
1793+
errno = err_win_to_posix(gle);
1794+
return -1;
1795+
}
1796+
1797+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
17971798
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
17981799
DWORD attrsold = GetFileAttributesW(wpold);
17991800
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1804,16 +1805,10 @@ int mingw_rename(const char *pold, const char *pnew)
18041805
return -1;
18051806
}
18061807
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1807-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1808-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1809-
return 0;
1810-
gle = GetLastError();
1811-
/* revert file attributes on failure */
1812-
SetFileAttributesW(wpnew, attrs);
1813-
}
1808+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1809+
goto repeat;
18141810
}
1815-
if (gle == ERROR_ACCESS_DENIED &&
1816-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1811+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
18171812
"Should I try again?", pold, pnew))
18181813
goto repeat;
18191814

0 commit comments

Comments
 (0)