Skip to content

Commit 9be895c

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 db44028 commit 9be895c

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
@@ -2567,7 +2567,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25672567
int mingw_rename(const char *pold, const char *pnew)
25682568
{
25692569
static int supports_file_rename_info_ex = 1;
2570-
DWORD attrs, gle;
2570+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
25712571
int tries = 0;
25722572
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25732573
int wpnew_len;
@@ -2578,15 +2578,6 @@ int mingw_rename(const char *pold, const char *pnew)
25782578
if (wpnew_len < 0)
25792579
return -1;
25802580

2581-
/*
2582-
* Try native rename() first to get errno right.
2583-
* It is based on MoveFile(), which cannot overwrite existing files.
2584-
*/
2585-
if (!_wrename(wpold, wpnew))
2586-
return 0;
2587-
if (errno != EEXIST)
2588-
return -1;
2589-
25902581
repeat:
25912582
if (supports_file_rename_info_ex) {
25922583
/*
@@ -2658,13 +2649,22 @@ int mingw_rename(const char *pold, const char *pnew)
26582649
* to retry.
26592650
*/
26602651
} else {
2661-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2652+
if (MoveFileExW(wpold, wpnew,
2653+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
26622654
return 0;
26632655
gle = GetLastError();
26642656
}
26652657

2666-
/* TODO: translate more errors */
2667-
if (gle == ERROR_ACCESS_DENIED &&
2658+
/* revert file attributes on failure */
2659+
if (attrs != INVALID_FILE_ATTRIBUTES)
2660+
SetFileAttributesW(wpnew, attrs);
2661+
2662+
if (!is_file_in_use_error(gle)) {
2663+
errno = err_win_to_posix(gle);
2664+
return -1;
2665+
}
2666+
2667+
if (attrs == INVALID_FILE_ATTRIBUTES &&
26682668
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
26692669
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
26702670
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2676,16 +2676,10 @@ int mingw_rename(const char *pold, const char *pnew)
26762676
return -1;
26772677
}
26782678
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2679-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2680-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2681-
return 0;
2682-
gle = GetLastError();
2683-
/* revert file attributes on failure */
2684-
SetFileAttributesW(wpnew, attrs);
2685-
}
2679+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2680+
goto repeat;
26862681
}
2687-
if (gle == ERROR_ACCESS_DENIED &&
2688-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2682+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
26892683
"Should I try again?", pold, pnew))
26902684
goto repeat;
26912685

0 commit comments

Comments
 (0)