Skip to content

Commit 9271297

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 261adda commit 9271297

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
/*
@@ -2669,13 +2660,22 @@ int mingw_rename(const char *pold, const char *pnew)
26692660
* to retry.
26702661
*/
26712662
} else {
2672-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2663+
if (MoveFileExW(wpold, wpnew,
2664+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
26732665
return 0;
26742666
gle = GetLastError();
26752667
}
26762668

2677-
/* TODO: translate more errors */
2678-
if (gle == ERROR_ACCESS_DENIED &&
2669+
/* revert file attributes on failure */
2670+
if (attrs != INVALID_FILE_ATTRIBUTES)
2671+
SetFileAttributesW(wpnew, attrs);
2672+
2673+
if (!is_file_in_use_error(gle)) {
2674+
errno = err_win_to_posix(gle);
2675+
return -1;
2676+
}
2677+
2678+
if (attrs == INVALID_FILE_ATTRIBUTES &&
26792679
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
26802680
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
26812681
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2687,16 +2687,10 @@ int mingw_rename(const char *pold, const char *pnew)
26872687
return -1;
26882688
}
26892689
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2690-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2691-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2692-
return 0;
2693-
gle = GetLastError();
2694-
/* revert file attributes on failure */
2695-
SetFileAttributesW(wpnew, attrs);
2696-
}
2690+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2691+
goto repeat;
26972692
}
2698-
if (gle == ERROR_ACCESS_DENIED &&
2699-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2693+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
27002694
"Should I try again?", pold, pnew))
27012695
goto repeat;
27022696

0 commit comments

Comments
 (0)