Skip to content

Commit 2d72190

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 a6e9c81 commit 2d72190

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
@@ -1883,27 +1883,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
18831883
#undef rename
18841884
int mingw_rename(const char *pold, const char *pnew)
18851885
{
1886-
DWORD attrs, gle;
1886+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
18871887
int tries = 0;
18881888
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
18891889
if (xutftowcs_long_path(wpold, pold) < 0 ||
18901890
xutftowcs_long_path(wpnew, pnew) < 0)
18911891
return -1;
18921892

1893-
/*
1894-
* Try native rename() first to get errno right.
1895-
* It is based on MoveFile(), which cannot overwrite existing files.
1896-
*/
1897-
if (!_wrename(wpold, wpnew))
1898-
return 0;
1899-
if (errno != EEXIST)
1900-
return -1;
19011893
repeat:
1902-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1894+
if (MoveFileExW(wpold, wpnew,
1895+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
19031896
return 0;
1904-
/* TODO: translate more errors */
19051897
gle = GetLastError();
1906-
if (gle == ERROR_ACCESS_DENIED &&
1898+
1899+
/* revert file attributes on failure */
1900+
if (attrs != INVALID_FILE_ATTRIBUTES)
1901+
SetFileAttributesW(wpnew, attrs);
1902+
1903+
if (!is_file_in_use_error(gle)) {
1904+
errno = err_win_to_posix(gle);
1905+
return -1;
1906+
}
1907+
1908+
if (attrs == INVALID_FILE_ATTRIBUTES &&
19071909
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
19081910
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
19091911
DWORD attrsold = GetFileAttributesW(wpold);
@@ -1915,16 +1917,10 @@ int mingw_rename(const char *pold, const char *pnew)
19151917
return -1;
19161918
}
19171919
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1918-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1919-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1920-
return 0;
1921-
gle = GetLastError();
1922-
/* revert file attributes on failure */
1923-
SetFileAttributesW(wpnew, attrs);
1924-
}
1920+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1921+
goto repeat;
19251922
}
1926-
if (gle == ERROR_ACCESS_DENIED &&
1927-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1923+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
19281924
"Should I try again?", pold, pnew))
19291925
goto repeat;
19301926

0 commit comments

Comments
 (0)