Skip to content

Commit b3d997d

Browse files
kbleesGit for Windows Build Agent
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 c6db534 commit b3d997d

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

compat/mingw.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,7 +2543,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25432543
int mingw_rename(const char *pold, const char *pnew)
25442544
{
25452545
static int supports_file_rename_info_ex = 1;
2546-
DWORD attrs, gle;
2546+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
25472547
int tries = 0;
25482548
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25492549
int wpnew_len;
@@ -2554,15 +2554,6 @@ int mingw_rename(const char *pold, const char *pnew)
25542554
if (wpnew_len < 0)
25552555
return -1;
25562556

2557-
/*
2558-
* Try native rename() first to get errno right.
2559-
* It is based on MoveFile(), which cannot overwrite existing files.
2560-
*/
2561-
if (!_wrename(wpold, wpnew))
2562-
return 0;
2563-
if (errno != EEXIST)
2564-
return -1;
2565-
25662557
repeat:
25672558
if (supports_file_rename_info_ex) {
25682559
/*
@@ -2638,13 +2629,22 @@ int mingw_rename(const char *pold, const char *pnew)
26382629
* to retry.
26392630
*/
26402631
} else {
2641-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2632+
if (MoveFileExW(wpold, wpnew,
2633+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
26422634
return 0;
26432635
gle = GetLastError();
26442636
}
26452637

2646-
/* TODO: translate more errors */
2647-
if (gle == ERROR_ACCESS_DENIED &&
2638+
/* revert file attributes on failure */
2639+
if (attrs != INVALID_FILE_ATTRIBUTES)
2640+
SetFileAttributesW(wpnew, attrs);
2641+
2642+
if (!is_file_in_use_error(gle)) {
2643+
errno = err_win_to_posix(gle);
2644+
return -1;
2645+
}
2646+
2647+
if (attrs == INVALID_FILE_ATTRIBUTES &&
26482648
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
26492649
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
26502650
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2656,16 +2656,10 @@ int mingw_rename(const char *pold, const char *pnew)
26562656
return -1;
26572657
}
26582658
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2659-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2660-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2661-
return 0;
2662-
gle = GetLastError();
2663-
/* revert file attributes on failure */
2664-
SetFileAttributesW(wpnew, attrs);
2665-
}
2659+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2660+
goto repeat;
26662661
}
2667-
if (gle == ERROR_ACCESS_DENIED &&
2668-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2662+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
26692663
"Should I try again?", pold, pnew))
26702664
goto repeat;
26712665

0 commit comments

Comments
 (0)