Skip to content

Commit b140540

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 393164b commit b140540

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
@@ -2423,27 +2423,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
24232423
#undef rename
24242424
int mingw_rename(const char *pold, const char *pnew)
24252425
{
2426-
DWORD attrs, gle;
2426+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
24272427
int tries = 0;
24282428
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
24292429
if (xutftowcs_long_path(wpold, pold) < 0 ||
24302430
xutftowcs_long_path(wpnew, pnew) < 0)
24312431
return -1;
24322432

2433-
/*
2434-
* Try native rename() first to get errno right.
2435-
* It is based on MoveFile(), which cannot overwrite existing files.
2436-
*/
2437-
if (!_wrename(wpold, wpnew))
2438-
return 0;
2439-
if (errno != EEXIST)
2440-
return -1;
24412433
repeat:
2442-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2434+
if (MoveFileExW(wpold, wpnew,
2435+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
24432436
return 0;
2444-
/* TODO: translate more errors */
24452437
gle = GetLastError();
2446-
if (gle == ERROR_ACCESS_DENIED &&
2438+
2439+
/* revert file attributes on failure */
2440+
if (attrs != INVALID_FILE_ATTRIBUTES)
2441+
SetFileAttributesW(wpnew, attrs);
2442+
2443+
if (!is_file_in_use_error(gle)) {
2444+
errno = err_win_to_posix(gle);
2445+
return -1;
2446+
}
2447+
2448+
if (attrs == INVALID_FILE_ATTRIBUTES &&
24472449
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
24482450
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
24492451
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2455,16 +2457,10 @@ int mingw_rename(const char *pold, const char *pnew)
24552457
return -1;
24562458
}
24572459
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2458-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2459-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2460-
return 0;
2461-
gle = GetLastError();
2462-
/* revert file attributes on failure */
2463-
SetFileAttributesW(wpnew, attrs);
2464-
}
2460+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2461+
goto repeat;
24652462
}
2466-
if (gle == ERROR_ACCESS_DENIED &&
2467-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2463+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24682464
"Should I try again?", pold, pnew))
24692465
goto repeat;
24702466

0 commit comments

Comments
 (0)