Skip to content

Commit 9ce69b8

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 088f885 commit 9ce69b8

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
@@ -1876,27 +1876,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
18761876
#undef rename
18771877
int mingw_rename(const char *pold, const char *pnew)
18781878
{
1879-
DWORD attrs, gle;
1879+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
18801880
int tries = 0;
18811881
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
18821882
if (xutftowcs_long_path(wpold, pold) < 0 ||
18831883
xutftowcs_long_path(wpnew, pnew) < 0)
18841884
return -1;
18851885

1886-
/*
1887-
* Try native rename() first to get errno right.
1888-
* It is based on MoveFile(), which cannot overwrite existing files.
1889-
*/
1890-
if (!_wrename(wpold, wpnew))
1891-
return 0;
1892-
if (errno != EEXIST)
1893-
return -1;
18941886
repeat:
1895-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1887+
if (MoveFileExW(wpold, wpnew,
1888+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
18961889
return 0;
1897-
/* TODO: translate more errors */
18981890
gle = GetLastError();
1899-
if (gle == ERROR_ACCESS_DENIED &&
1891+
1892+
/* revert file attributes on failure */
1893+
if (attrs != INVALID_FILE_ATTRIBUTES)
1894+
SetFileAttributesW(wpnew, attrs);
1895+
1896+
if (!is_file_in_use_error(gle)) {
1897+
errno = err_win_to_posix(gle);
1898+
return -1;
1899+
}
1900+
1901+
if (attrs == INVALID_FILE_ATTRIBUTES &&
19001902
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
19011903
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
19021904
DWORD attrsold = GetFileAttributesW(wpold);
@@ -1908,16 +1910,10 @@ int mingw_rename(const char *pold, const char *pnew)
19081910
return -1;
19091911
}
19101912
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1911-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1912-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1913-
return 0;
1914-
gle = GetLastError();
1915-
/* revert file attributes on failure */
1916-
SetFileAttributesW(wpnew, attrs);
1917-
}
1913+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1914+
goto repeat;
19181915
}
1919-
if (gle == ERROR_ACCESS_DENIED &&
1920-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1916+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
19211917
"Should I try again?", pold, pnew))
19221918
goto repeat;
19231919

0 commit comments

Comments
 (0)