Skip to content

Commit 4dcfe13

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 d2a5387 commit 4dcfe13

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
@@ -1982,27 +1982,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
19821982
#undef rename
19831983
int mingw_rename(const char *pold, const char *pnew)
19841984
{
1985-
DWORD attrs, gle;
1985+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
19861986
int tries = 0;
19871987
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
19881988
if (xutftowcs_long_path(wpold, pold) < 0 ||
19891989
xutftowcs_long_path(wpnew, pnew) < 0)
19901990
return -1;
19911991

1992-
/*
1993-
* Try native rename() first to get errno right.
1994-
* It is based on MoveFile(), which cannot overwrite existing files.
1995-
*/
1996-
if (!_wrename(wpold, wpnew))
1997-
return 0;
1998-
if (errno != EEXIST)
1999-
return -1;
20001992
repeat:
2001-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1993+
if (MoveFileExW(wpold, wpnew,
1994+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
20021995
return 0;
2003-
/* TODO: translate more errors */
20041996
gle = GetLastError();
2005-
if (gle == ERROR_ACCESS_DENIED &&
1997+
1998+
/* revert file attributes on failure */
1999+
if (attrs != INVALID_FILE_ATTRIBUTES)
2000+
SetFileAttributesW(wpnew, attrs);
2001+
2002+
if (!is_file_in_use_error(gle)) {
2003+
errno = err_win_to_posix(gle);
2004+
return -1;
2005+
}
2006+
2007+
if (attrs == INVALID_FILE_ATTRIBUTES &&
20062008
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
20072009
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
20082010
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2014,16 +2016,10 @@ int mingw_rename(const char *pold, const char *pnew)
20142016
return -1;
20152017
}
20162018
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2017-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2018-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2019-
return 0;
2020-
gle = GetLastError();
2021-
/* revert file attributes on failure */
2022-
SetFileAttributesW(wpnew, attrs);
2023-
}
2019+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2020+
goto repeat;
20242021
}
2025-
if (gle == ERROR_ACCESS_DENIED &&
2026-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2022+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
20272023
"Should I try again?", pold, pnew))
20282024
goto repeat;
20292025

0 commit comments

Comments
 (0)