Skip to content

Commit 4c2333f

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 ecdf9f8 commit 4c2333f

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
/*
@@ -2636,13 +2627,22 @@ int mingw_rename(const char *pold, const char *pnew)
26362627
* to retry.
26372628
*/
26382629
} else {
2639-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2630+
if (MoveFileExW(wpold, wpnew,
2631+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
26402632
return 0;
26412633
gle = GetLastError();
26422634
}
26432635

2644-
/* TODO: translate more errors */
2645-
if (gle == ERROR_ACCESS_DENIED &&
2636+
/* revert file attributes on failure */
2637+
if (attrs != INVALID_FILE_ATTRIBUTES)
2638+
SetFileAttributesW(wpnew, attrs);
2639+
2640+
if (!is_file_in_use_error(gle)) {
2641+
errno = err_win_to_posix(gle);
2642+
return -1;
2643+
}
2644+
2645+
if (attrs == INVALID_FILE_ATTRIBUTES &&
26462646
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
26472647
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
26482648
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2654,16 +2654,10 @@ int mingw_rename(const char *pold, const char *pnew)
26542654
return -1;
26552655
}
26562656
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2657-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2658-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2659-
return 0;
2660-
gle = GetLastError();
2661-
/* revert file attributes on failure */
2662-
SetFileAttributesW(wpnew, attrs);
2663-
}
2657+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2658+
goto repeat;
26642659
}
2665-
if (gle == ERROR_ACCESS_DENIED &&
2666-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2660+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
26672661
"Should I try again?", pold, pnew))
26682662
goto repeat;
26692663

0 commit comments

Comments
 (0)