Skip to content

Commit 301ceca

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 6b290a6 commit 301ceca

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
@@ -2417,27 +2417,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
24172417
#undef rename
24182418
int mingw_rename(const char *pold, const char *pnew)
24192419
{
2420-
DWORD attrs, gle;
2420+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
24212421
int tries = 0;
24222422
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
24232423
if (xutftowcs_long_path(wpold, pold) < 0 ||
24242424
xutftowcs_long_path(wpnew, pnew) < 0)
24252425
return -1;
24262426

2427-
/*
2428-
* Try native rename() first to get errno right.
2429-
* It is based on MoveFile(), which cannot overwrite existing files.
2430-
*/
2431-
if (!_wrename(wpold, wpnew))
2432-
return 0;
2433-
if (errno != EEXIST)
2434-
return -1;
24352427
repeat:
2436-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2428+
if (MoveFileExW(wpold, wpnew,
2429+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
24372430
return 0;
2438-
/* TODO: translate more errors */
24392431
gle = GetLastError();
2440-
if (gle == ERROR_ACCESS_DENIED &&
2432+
2433+
/* revert file attributes on failure */
2434+
if (attrs != INVALID_FILE_ATTRIBUTES)
2435+
SetFileAttributesW(wpnew, attrs);
2436+
2437+
if (!is_file_in_use_error(gle)) {
2438+
errno = err_win_to_posix(gle);
2439+
return -1;
2440+
}
2441+
2442+
if (attrs == INVALID_FILE_ATTRIBUTES &&
24412443
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
24422444
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
24432445
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2449,16 +2451,10 @@ int mingw_rename(const char *pold, const char *pnew)
24492451
return -1;
24502452
}
24512453
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2452-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2453-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2454-
return 0;
2455-
gle = GetLastError();
2456-
/* revert file attributes on failure */
2457-
SetFileAttributesW(wpnew, attrs);
2458-
}
2454+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2455+
goto repeat;
24592456
}
2460-
if (gle == ERROR_ACCESS_DENIED &&
2461-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2457+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24622458
"Should I try again?", pold, pnew))
24632459
goto repeat;
24642460

0 commit comments

Comments
 (0)