Skip to content

Commit 26dc5c1

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 03d14da commit 26dc5c1

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

compat/mingw.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,27 +2398,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
23982398
#undef rename
23992399
int mingw_rename(const char *pold, const char *pnew)
24002400
{
2401-
DWORD attrs, gle;
2401+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
24022402
int tries = 0;
24032403
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
24042404
if (xutftowcs_long_path(wpold, pold) < 0 ||
24052405
xutftowcs_long_path(wpnew, pnew) < 0)
24062406
return -1;
24072407

2408-
/*
2409-
* Try native rename() first to get errno right.
2410-
* It is based on MoveFile(), which cannot overwrite existing files.
2411-
*/
2412-
if (!_wrename(wpold, wpnew))
2413-
return 0;
2414-
if (errno != EEXIST)
2415-
return -1;
24162408
repeat:
2417-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2409+
if (MoveFileExW(wpold, wpnew,
2410+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
24182411
return 0;
2419-
/* TODO: translate more errors */
24202412
gle = GetLastError();
2421-
if (gle == ERROR_ACCESS_DENIED &&
2413+
2414+
/* revert file attributes on failure */
2415+
if (attrs != INVALID_FILE_ATTRIBUTES)
2416+
SetFileAttributesW(wpnew, attrs);
2417+
2418+
if (!is_file_in_use_error(gle)) {
2419+
errno = err_win_to_posix(gle);
2420+
return -1;
2421+
}
2422+
2423+
if (attrs == INVALID_FILE_ATTRIBUTES &&
24222424
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
24232425
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
24242426
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2430,16 +2432,10 @@ int mingw_rename(const char *pold, const char *pnew)
24302432
return -1;
24312433
}
24322434
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2433-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2434-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2435-
return 0;
2436-
gle = GetLastError();
2437-
/* revert file attributes on failure */
2438-
SetFileAttributesW(wpnew, attrs);
2439-
}
2435+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2436+
goto repeat;
24402437
}
2441-
if (gle == ERROR_ACCESS_DENIED &&
2442-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2438+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24432439
"Should I try again?", pold, pnew))
24442440
goto repeat;
24452441

0 commit comments

Comments
 (0)