Skip to content

Commit d89320e

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 2954d38 commit d89320e

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
@@ -2541,7 +2541,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25412541
int mingw_rename(const char *pold, const char *pnew)
25422542
{
25432543
static int supports_file_rename_info_ex = 1;
2544-
DWORD attrs, gle;
2544+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
25452545
int tries = 0;
25462546
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25472547
int wpnew_len;
@@ -2552,15 +2552,6 @@ int mingw_rename(const char *pold, const char *pnew)
25522552
if (wpnew_len < 0)
25532553
return -1;
25542554

2555-
/*
2556-
* Try native rename() first to get errno right.
2557-
* It is based on MoveFile(), which cannot overwrite existing files.
2558-
*/
2559-
if (!_wrename(wpold, wpnew))
2560-
return 0;
2561-
if (errno != EEXIST)
2562-
return -1;
2563-
25642555
repeat:
25652556
if (supports_file_rename_info_ex) {
25662557
/*
@@ -2634,13 +2625,22 @@ int mingw_rename(const char *pold, const char *pnew)
26342625
* to retry.
26352626
*/
26362627
} else {
2637-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2628+
if (MoveFileExW(wpold, wpnew,
2629+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
26382630
return 0;
26392631
gle = GetLastError();
26402632
}
26412633

2642-
/* TODO: translate more errors */
2643-
if (gle == ERROR_ACCESS_DENIED &&
2634+
/* revert file attributes on failure */
2635+
if (attrs != INVALID_FILE_ATTRIBUTES)
2636+
SetFileAttributesW(wpnew, attrs);
2637+
2638+
if (!is_file_in_use_error(gle)) {
2639+
errno = err_win_to_posix(gle);
2640+
return -1;
2641+
}
2642+
2643+
if (attrs == INVALID_FILE_ATTRIBUTES &&
26442644
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
26452645
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
26462646
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2652,16 +2652,10 @@ int mingw_rename(const char *pold, const char *pnew)
26522652
return -1;
26532653
}
26542654
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2655-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2656-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2657-
return 0;
2658-
gle = GetLastError();
2659-
/* revert file attributes on failure */
2660-
SetFileAttributesW(wpnew, attrs);
2661-
}
2655+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2656+
goto repeat;
26622657
}
2663-
if (gle == ERROR_ACCESS_DENIED &&
2664-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2658+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
26652659
"Should I try again?", pold, pnew))
26662660
goto repeat;
26672661

0 commit comments

Comments
 (0)