Skip to content

Commit a15e015

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 f989566 commit a15e015

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
@@ -1892,27 +1892,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
18921892
#undef rename
18931893
int mingw_rename(const char *pold, const char *pnew)
18941894
{
1895-
DWORD attrs, gle;
1895+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
18961896
int tries = 0;
18971897
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
18981898
if (xutftowcs_long_path(wpold, pold) < 0 ||
18991899
xutftowcs_long_path(wpnew, pnew) < 0)
19001900
return -1;
19011901

1902-
/*
1903-
* Try native rename() first to get errno right.
1904-
* It is based on MoveFile(), which cannot overwrite existing files.
1905-
*/
1906-
if (!_wrename(wpold, wpnew))
1907-
return 0;
1908-
if (errno != EEXIST)
1909-
return -1;
19101902
repeat:
1911-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1903+
if (MoveFileExW(wpold, wpnew,
1904+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
19121905
return 0;
1913-
/* TODO: translate more errors */
19141906
gle = GetLastError();
1915-
if (gle == ERROR_ACCESS_DENIED &&
1907+
1908+
/* revert file attributes on failure */
1909+
if (attrs != INVALID_FILE_ATTRIBUTES)
1910+
SetFileAttributesW(wpnew, attrs);
1911+
1912+
if (!is_file_in_use_error(gle)) {
1913+
errno = err_win_to_posix(gle);
1914+
return -1;
1915+
}
1916+
1917+
if (attrs == INVALID_FILE_ATTRIBUTES &&
19161918
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
19171919
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
19181920
DWORD attrsold = GetFileAttributesW(wpold);
@@ -1924,16 +1926,10 @@ int mingw_rename(const char *pold, const char *pnew)
19241926
return -1;
19251927
}
19261928
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1927-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1928-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1929-
return 0;
1930-
gle = GetLastError();
1931-
/* revert file attributes on failure */
1932-
SetFileAttributesW(wpnew, attrs);
1933-
}
1929+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1930+
goto repeat;
19341931
}
1935-
if (gle == ERROR_ACCESS_DENIED &&
1936-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1932+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
19371933
"Should I try again?", pold, pnew))
19381934
goto repeat;
19391935

0 commit comments

Comments
 (0)