Skip to content

Commit 7d7b851

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 a8e6d38 commit 7d7b851

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
@@ -2518,7 +2518,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25182518
int mingw_rename(const char *pold, const char *pnew)
25192519
{
25202520
static int supports_file_rename_info_ex = 1;
2521-
DWORD attrs, gle;
2521+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
25222522
int tries = 0;
25232523
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25242524
int wpnew_len;
@@ -2529,15 +2529,6 @@ int mingw_rename(const char *pold, const char *pnew)
25292529
if (wpnew_len < 0)
25302530
return -1;
25312531

2532-
/*
2533-
* Try native rename() first to get errno right.
2534-
* It is based on MoveFile(), which cannot overwrite existing files.
2535-
*/
2536-
if (!_wrename(wpold, wpnew))
2537-
return 0;
2538-
if (errno != EEXIST)
2539-
return -1;
2540-
25412532
repeat:
25422533
if (supports_file_rename_info_ex) {
25432534
/*
@@ -2611,13 +2602,22 @@ int mingw_rename(const char *pold, const char *pnew)
26112602
* to retry.
26122603
*/
26132604
} else {
2614-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2605+
if (MoveFileExW(wpold, wpnew,
2606+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
26152607
return 0;
26162608
gle = GetLastError();
26172609
}
26182610

2619-
/* TODO: translate more errors */
2620-
if (gle == ERROR_ACCESS_DENIED &&
2611+
/* revert file attributes on failure */
2612+
if (attrs != INVALID_FILE_ATTRIBUTES)
2613+
SetFileAttributesW(wpnew, attrs);
2614+
2615+
if (!is_file_in_use_error(gle)) {
2616+
errno = err_win_to_posix(gle);
2617+
return -1;
2618+
}
2619+
2620+
if (attrs == INVALID_FILE_ATTRIBUTES &&
26212621
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
26222622
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
26232623
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2629,16 +2629,10 @@ int mingw_rename(const char *pold, const char *pnew)
26292629
return -1;
26302630
}
26312631
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2632-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2633-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2634-
return 0;
2635-
gle = GetLastError();
2636-
/* revert file attributes on failure */
2637-
SetFileAttributesW(wpnew, attrs);
2638-
}
2632+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2633+
goto repeat;
26392634
}
2640-
if (gle == ERROR_ACCESS_DENIED &&
2641-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2635+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
26422636
"Should I try again?", pold, pnew))
26432637
goto repeat;
26442638

0 commit comments

Comments
 (0)