Skip to content

Commit c2d4cbf

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 12e6144 commit c2d4cbf

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
@@ -2578,7 +2578,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25782578
int mingw_rename(const char *pold, const char *pnew)
25792579
{
25802580
static int supports_file_rename_info_ex = 1;
2581-
DWORD attrs, gle;
2581+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
25822582
int tries = 0;
25832583
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25842584
int wpnew_len;
@@ -2589,15 +2589,6 @@ int mingw_rename(const char *pold, const char *pnew)
25892589
if (wpnew_len < 0)
25902590
return -1;
25912591

2592-
/*
2593-
* Try native rename() first to get errno right.
2594-
* It is based on MoveFile(), which cannot overwrite existing files.
2595-
*/
2596-
if (!_wrename(wpold, wpnew))
2597-
return 0;
2598-
if (errno != EEXIST)
2599-
return -1;
2600-
26012592
repeat:
26022593
if (supports_file_rename_info_ex) {
26032594
/*
@@ -2671,13 +2662,22 @@ int mingw_rename(const char *pold, const char *pnew)
26712662
* to retry.
26722663
*/
26732664
} else {
2674-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2665+
if (MoveFileExW(wpold, wpnew,
2666+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
26752667
return 0;
26762668
gle = GetLastError();
26772669
}
26782670

2679-
/* TODO: translate more errors */
2680-
if (gle == ERROR_ACCESS_DENIED &&
2671+
/* revert file attributes on failure */
2672+
if (attrs != INVALID_FILE_ATTRIBUTES)
2673+
SetFileAttributesW(wpnew, attrs);
2674+
2675+
if (!is_file_in_use_error(gle)) {
2676+
errno = err_win_to_posix(gle);
2677+
return -1;
2678+
}
2679+
2680+
if (attrs == INVALID_FILE_ATTRIBUTES &&
26812681
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
26822682
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
26832683
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2689,16 +2689,10 @@ int mingw_rename(const char *pold, const char *pnew)
26892689
return -1;
26902690
}
26912691
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2692-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2693-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2694-
return 0;
2695-
gle = GetLastError();
2696-
/* revert file attributes on failure */
2697-
SetFileAttributesW(wpnew, attrs);
2698-
}
2692+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2693+
goto repeat;
26992694
}
2700-
if (gle == ERROR_ACCESS_DENIED &&
2701-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2695+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
27022696
"Should I try again?", pold, pnew))
27032697
goto repeat;
27042698

0 commit comments

Comments
 (0)