Skip to content

Commit 27567cd

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 7ddbe60 commit 27567cd

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
@@ -2360,27 +2360,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
23602360
#undef rename
23612361
int mingw_rename(const char *pold, const char *pnew)
23622362
{
2363-
DWORD attrs, gle;
2363+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
23642364
int tries = 0;
23652365
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
23662366
if (xutftowcs_long_path(wpold, pold) < 0 ||
23672367
xutftowcs_long_path(wpnew, pnew) < 0)
23682368
return -1;
23692369

2370-
/*
2371-
* Try native rename() first to get errno right.
2372-
* It is based on MoveFile(), which cannot overwrite existing files.
2373-
*/
2374-
if (!_wrename(wpold, wpnew))
2375-
return 0;
2376-
if (errno != EEXIST)
2377-
return -1;
23782370
repeat:
2379-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2371+
if (MoveFileExW(wpold, wpnew,
2372+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
23802373
return 0;
2381-
/* TODO: translate more errors */
23822374
gle = GetLastError();
2383-
if (gle == ERROR_ACCESS_DENIED &&
2375+
2376+
/* revert file attributes on failure */
2377+
if (attrs != INVALID_FILE_ATTRIBUTES)
2378+
SetFileAttributesW(wpnew, attrs);
2379+
2380+
if (!is_file_in_use_error(gle)) {
2381+
errno = err_win_to_posix(gle);
2382+
return -1;
2383+
}
2384+
2385+
if (attrs == INVALID_FILE_ATTRIBUTES &&
23842386
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
23852387
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
23862388
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2392,16 +2394,10 @@ int mingw_rename(const char *pold, const char *pnew)
23922394
return -1;
23932395
}
23942396
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2395-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2396-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2397-
return 0;
2398-
gle = GetLastError();
2399-
/* revert file attributes on failure */
2400-
SetFileAttributesW(wpnew, attrs);
2401-
}
2397+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2398+
goto repeat;
24022399
}
2403-
if (gle == ERROR_ACCESS_DENIED &&
2404-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2400+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24052401
"Should I try again?", pold, pnew))
24062402
goto repeat;
24072403

0 commit comments

Comments
 (0)