Skip to content

Commit 4f8f4ac

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 528f0fc commit 4f8f4ac

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

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

0 commit comments

Comments
 (0)