Skip to content

Commit 5dcdda3

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 a05ee44 commit 5dcdda3

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
@@ -2490,7 +2490,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
24902490
int mingw_rename(const char *pold, const char *pnew)
24912491
{
24922492
static int supports_file_rename_info_ex = 1;
2493-
DWORD attrs, gle;
2493+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
24942494
int tries = 0;
24952495
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
24962496
int wpnew_len;
@@ -2501,15 +2501,6 @@ int mingw_rename(const char *pold, const char *pnew)
25012501
if (wpnew_len < 0)
25022502
return -1;
25032503

2504-
/*
2505-
* Try native rename() first to get errno right.
2506-
* It is based on MoveFile(), which cannot overwrite existing files.
2507-
*/
2508-
if (!_wrename(wpold, wpnew))
2509-
return 0;
2510-
if (errno != EEXIST)
2511-
return -1;
2512-
25132504
repeat:
25142505
if (supports_file_rename_info_ex) {
25152506
/*
@@ -2581,13 +2572,22 @@ int mingw_rename(const char *pold, const char *pnew)
25812572
* to retry.
25822573
*/
25832574
} else {
2584-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2575+
if (MoveFileExW(wpold, wpnew,
2576+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
25852577
return 0;
25862578
gle = GetLastError();
25872579
}
25882580

2589-
/* TODO: translate more errors */
2590-
if (gle == ERROR_ACCESS_DENIED &&
2581+
/* revert file attributes on failure */
2582+
if (attrs != INVALID_FILE_ATTRIBUTES)
2583+
SetFileAttributesW(wpnew, attrs);
2584+
2585+
if (!is_file_in_use_error(gle)) {
2586+
errno = err_win_to_posix(gle);
2587+
return -1;
2588+
}
2589+
2590+
if (attrs == INVALID_FILE_ATTRIBUTES &&
25912591
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
25922592
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
25932593
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2599,16 +2599,10 @@ int mingw_rename(const char *pold, const char *pnew)
25992599
return -1;
26002600
}
26012601
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2602-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2603-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2604-
return 0;
2605-
gle = GetLastError();
2606-
/* revert file attributes on failure */
2607-
SetFileAttributesW(wpnew, attrs);
2608-
}
2602+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2603+
goto repeat;
26092604
}
2610-
if (gle == ERROR_ACCESS_DENIED &&
2611-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2605+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
26122606
"Should I try again?", pold, pnew))
26132607
goto repeat;
26142608

0 commit comments

Comments
 (0)