Skip to content

Commit 865520d

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 432e95e commit 865520d

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
@@ -2384,27 +2384,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
23842384
#undef rename
23852385
int mingw_rename(const char *pold, const char *pnew)
23862386
{
2387-
DWORD attrs, gle;
2387+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
23882388
int tries = 0;
23892389
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
23902390
if (xutftowcs_long_path(wpold, pold) < 0 ||
23912391
xutftowcs_long_path(wpnew, pnew) < 0)
23922392
return -1;
23932393

2394-
/*
2395-
* Try native rename() first to get errno right.
2396-
* It is based on MoveFile(), which cannot overwrite existing files.
2397-
*/
2398-
if (!_wrename(wpold, wpnew))
2399-
return 0;
2400-
if (errno != EEXIST)
2401-
return -1;
24022394
repeat:
2403-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2395+
if (MoveFileExW(wpold, wpnew,
2396+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
24042397
return 0;
2405-
/* TODO: translate more errors */
24062398
gle = GetLastError();
2407-
if (gle == ERROR_ACCESS_DENIED &&
2399+
2400+
/* revert file attributes on failure */
2401+
if (attrs != INVALID_FILE_ATTRIBUTES)
2402+
SetFileAttributesW(wpnew, attrs);
2403+
2404+
if (!is_file_in_use_error(gle)) {
2405+
errno = err_win_to_posix(gle);
2406+
return -1;
2407+
}
2408+
2409+
if (attrs == INVALID_FILE_ATTRIBUTES &&
24082410
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
24092411
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
24102412
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2416,16 +2418,10 @@ int mingw_rename(const char *pold, const char *pnew)
24162418
return -1;
24172419
}
24182420
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2419-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2420-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2421-
return 0;
2422-
gle = GetLastError();
2423-
/* revert file attributes on failure */
2424-
SetFileAttributesW(wpnew, attrs);
2425-
}
2421+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2422+
goto repeat;
24262423
}
2427-
if (gle == ERROR_ACCESS_DENIED &&
2428-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2424+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24292425
"Should I try again?", pold, pnew))
24302426
goto repeat;
24312427

0 commit comments

Comments
 (0)