Skip to content

Commit e8328f0

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 f656a8c commit e8328f0

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
@@ -2412,27 +2412,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
24122412
#undef rename
24132413
int mingw_rename(const char *pold, const char *pnew)
24142414
{
2415-
DWORD attrs, gle;
2415+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
24162416
int tries = 0;
24172417
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
24182418
if (xutftowcs_long_path(wpold, pold) < 0 ||
24192419
xutftowcs_long_path(wpnew, pnew) < 0)
24202420
return -1;
24212421

2422-
/*
2423-
* Try native rename() first to get errno right.
2424-
* It is based on MoveFile(), which cannot overwrite existing files.
2425-
*/
2426-
if (!_wrename(wpold, wpnew))
2427-
return 0;
2428-
if (errno != EEXIST)
2429-
return -1;
24302422
repeat:
2431-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2423+
if (MoveFileExW(wpold, wpnew,
2424+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
24322425
return 0;
2433-
/* TODO: translate more errors */
24342426
gle = GetLastError();
2435-
if (gle == ERROR_ACCESS_DENIED &&
2427+
2428+
/* revert file attributes on failure */
2429+
if (attrs != INVALID_FILE_ATTRIBUTES)
2430+
SetFileAttributesW(wpnew, attrs);
2431+
2432+
if (!is_file_in_use_error(gle)) {
2433+
errno = err_win_to_posix(gle);
2434+
return -1;
2435+
}
2436+
2437+
if (attrs == INVALID_FILE_ATTRIBUTES &&
24362438
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
24372439
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
24382440
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2444,16 +2446,10 @@ int mingw_rename(const char *pold, const char *pnew)
24442446
return -1;
24452447
}
24462448
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2447-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2448-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2449-
return 0;
2450-
gle = GetLastError();
2451-
/* revert file attributes on failure */
2452-
SetFileAttributesW(wpnew, attrs);
2453-
}
2449+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2450+
goto repeat;
24542451
}
2455-
if (gle == ERROR_ACCESS_DENIED &&
2456-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2452+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24572453
"Should I try again?", pold, pnew))
24582454
goto repeat;
24592455

0 commit comments

Comments
 (0)