Skip to content

Commit d27bed2

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 3a56953 commit d27bed2

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

2429-
/*
2430-
* Try native rename() first to get errno right.
2431-
* It is based on MoveFile(), which cannot overwrite existing files.
2432-
*/
2433-
if (!_wrename(wpold, wpnew))
2434-
return 0;
2435-
if (errno != EEXIST)
2436-
return -1;
24372429
repeat:
2438-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2430+
if (MoveFileExW(wpold, wpnew,
2431+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
24392432
return 0;
2440-
/* TODO: translate more errors */
24412433
gle = GetLastError();
2442-
if (gle == ERROR_ACCESS_DENIED &&
2434+
2435+
/* revert file attributes on failure */
2436+
if (attrs != INVALID_FILE_ATTRIBUTES)
2437+
SetFileAttributesW(wpnew, attrs);
2438+
2439+
if (!is_file_in_use_error(gle)) {
2440+
errno = err_win_to_posix(gle);
2441+
return -1;
2442+
}
2443+
2444+
if (attrs == INVALID_FILE_ATTRIBUTES &&
24432445
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
24442446
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
24452447
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2451,16 +2453,10 @@ int mingw_rename(const char *pold, const char *pnew)
24512453
return -1;
24522454
}
24532455
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2454-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2455-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2456-
return 0;
2457-
gle = GetLastError();
2458-
/* revert file attributes on failure */
2459-
SetFileAttributesW(wpnew, attrs);
2460-
}
2456+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2457+
goto repeat;
24612458
}
2462-
if (gle == ERROR_ACCESS_DENIED &&
2463-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2459+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24642460
"Should I try again?", pold, pnew))
24652461
goto repeat;
24662462

0 commit comments

Comments
 (0)