Skip to content

Commit ca0339f

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 4aa2727 commit ca0339f

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

1763-
/*
1764-
* Try native rename() first to get errno right.
1765-
* It is based on MoveFile(), which cannot overwrite existing files.
1766-
*/
1767-
if (!_wrename(wpold, wpnew))
1768-
return 0;
1769-
if (errno != EEXIST)
1770-
return -1;
17711763
repeat:
1772-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1764+
if (MoveFileExW(wpold, wpnew,
1765+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
17731766
return 0;
1774-
/* TODO: translate more errors */
17751767
gle = GetLastError();
1776-
if (gle == ERROR_ACCESS_DENIED &&
1777-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1768+
1769+
/* revert file attributes on failure */
1770+
if (attrs != INVALID_FILE_ATTRIBUTES)
1771+
SetFileAttributesW(wpnew, attrs);
1772+
1773+
if (!is_file_in_use_error(gle)) {
1774+
errno = err_win_to_posix(gle);
1775+
return -1;
1776+
}
1777+
1778+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
17781779
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
17791780
DWORD attrsold = GetFileAttributesW(wpold);
17801781
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1785,16 +1786,10 @@ int mingw_rename(const char *pold, const char *pnew)
17851786
return -1;
17861787
}
17871788
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1788-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1789-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1790-
return 0;
1791-
gle = GetLastError();
1792-
/* revert file attributes on failure */
1793-
SetFileAttributesW(wpnew, attrs);
1794-
}
1789+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1790+
goto repeat;
17951791
}
1796-
if (gle == ERROR_ACCESS_DENIED &&
1797-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1792+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
17981793
"Should I try again?", pold, pnew))
17991794
goto repeat;
18001795

0 commit comments

Comments
 (0)