Skip to content

Commit 187653a

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 1640750 commit 187653a

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

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

0 commit comments

Comments
 (0)