Skip to content

Commit 2d3aa8b

Browse files
kbleesGit for Windows Build Agent
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 ea15722 commit 2d3aa8b

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
@@ -2200,27 +2200,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
22002200
#undef rename
22012201
int mingw_rename(const char *pold, const char *pnew)
22022202
{
2203-
DWORD attrs, gle;
2203+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
22042204
int tries = 0;
22052205
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
22062206
if (xutftowcs_long_path(wpold, pold) < 0 ||
22072207
xutftowcs_long_path(wpnew, pnew) < 0)
22082208
return -1;
22092209

2210-
/*
2211-
* Try native rename() first to get errno right.
2212-
* It is based on MoveFile(), which cannot overwrite existing files.
2213-
*/
2214-
if (!_wrename(wpold, wpnew))
2215-
return 0;
2216-
if (errno != EEXIST)
2217-
return -1;
22182210
repeat:
2219-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2211+
if (MoveFileExW(wpold, wpnew,
2212+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
22202213
return 0;
2221-
/* TODO: translate more errors */
22222214
gle = GetLastError();
2223-
if (gle == ERROR_ACCESS_DENIED &&
2215+
2216+
/* revert file attributes on failure */
2217+
if (attrs != INVALID_FILE_ATTRIBUTES)
2218+
SetFileAttributesW(wpnew, attrs);
2219+
2220+
if (!is_file_in_use_error(gle)) {
2221+
errno = err_win_to_posix(gle);
2222+
return -1;
2223+
}
2224+
2225+
if (attrs == INVALID_FILE_ATTRIBUTES &&
22242226
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
22252227
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
22262228
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2232,16 +2234,10 @@ int mingw_rename(const char *pold, const char *pnew)
22322234
return -1;
22332235
}
22342236
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2235-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2236-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2237-
return 0;
2238-
gle = GetLastError();
2239-
/* revert file attributes on failure */
2240-
SetFileAttributesW(wpnew, attrs);
2241-
}
2237+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2238+
goto repeat;
22422239
}
2243-
if (gle == ERROR_ACCESS_DENIED &&
2244-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2240+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
22452241
"Should I try again?", pold, pnew))
22462242
goto repeat;
22472243

0 commit comments

Comments
 (0)