Skip to content

Commit 1f3edba

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 60ba999 commit 1f3edba

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

compat/mingw.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,27 +1885,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
18851885
#undef rename
18861886
int mingw_rename(const char *pold, const char *pnew)
18871887
{
1888-
DWORD attrs, gle;
1888+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
18891889
int tries = 0;
18901890
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
18911891
if (xutftowcs_long_path(wpold, pold) < 0 ||
18921892
xutftowcs_long_path(wpnew, pnew) < 0)
18931893
return -1;
18941894

1895-
/*
1896-
* Try native rename() first to get errno right.
1897-
* It is based on MoveFile(), which cannot overwrite existing files.
1898-
*/
1899-
if (!_wrename(wpold, wpnew))
1900-
return 0;
1901-
if (errno != EEXIST)
1902-
return -1;
19031895
repeat:
1904-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1896+
if (MoveFileExW(wpold, wpnew,
1897+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
19051898
return 0;
1906-
/* TODO: translate more errors */
19071899
gle = GetLastError();
1908-
if (gle == ERROR_ACCESS_DENIED &&
1900+
1901+
/* revert file attributes on failure */
1902+
if (attrs != INVALID_FILE_ATTRIBUTES)
1903+
SetFileAttributesW(wpnew, attrs);
1904+
1905+
if (!is_file_in_use_error(gle)) {
1906+
errno = err_win_to_posix(gle);
1907+
return -1;
1908+
}
1909+
1910+
if (attrs == INVALID_FILE_ATTRIBUTES &&
19091911
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
19101912
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
19111913
DWORD attrsold = GetFileAttributesW(wpold);
@@ -1917,16 +1919,10 @@ int mingw_rename(const char *pold, const char *pnew)
19171919
return -1;
19181920
}
19191921
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1920-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1921-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1922-
return 0;
1923-
gle = GetLastError();
1924-
/* revert file attributes on failure */
1925-
SetFileAttributesW(wpnew, attrs);
1926-
}
1922+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1923+
goto repeat;
19271924
}
1928-
if (gle == ERROR_ACCESS_DENIED &&
1929-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1925+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
19301926
"Should I try again?", pold, pnew))
19311927
goto repeat;
19321928

0 commit comments

Comments
 (0)