Skip to content

Commit 7ff7f32

Browse files
kbleesvdye
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 d1b4190 commit 7ff7f32

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
@@ -2331,27 +2331,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
23312331
#undef rename
23322332
int mingw_rename(const char *pold, const char *pnew)
23332333
{
2334-
DWORD attrs, gle;
2334+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
23352335
int tries = 0;
23362336
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
23372337
if (xutftowcs_long_path(wpold, pold) < 0 ||
23382338
xutftowcs_long_path(wpnew, pnew) < 0)
23392339
return -1;
23402340

2341-
/*
2342-
* Try native rename() first to get errno right.
2343-
* It is based on MoveFile(), which cannot overwrite existing files.
2344-
*/
2345-
if (!_wrename(wpold, wpnew))
2346-
return 0;
2347-
if (errno != EEXIST)
2348-
return -1;
23492341
repeat:
2350-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2342+
if (MoveFileExW(wpold, wpnew,
2343+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
23512344
return 0;
2352-
/* TODO: translate more errors */
23532345
gle = GetLastError();
2354-
if (gle == ERROR_ACCESS_DENIED &&
2346+
2347+
/* revert file attributes on failure */
2348+
if (attrs != INVALID_FILE_ATTRIBUTES)
2349+
SetFileAttributesW(wpnew, attrs);
2350+
2351+
if (!is_file_in_use_error(gle)) {
2352+
errno = err_win_to_posix(gle);
2353+
return -1;
2354+
}
2355+
2356+
if (attrs == INVALID_FILE_ATTRIBUTES &&
23552357
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
23562358
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
23572359
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2363,16 +2365,10 @@ int mingw_rename(const char *pold, const char *pnew)
23632365
return -1;
23642366
}
23652367
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2366-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2367-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2368-
return 0;
2369-
gle = GetLastError();
2370-
/* revert file attributes on failure */
2371-
SetFileAttributesW(wpnew, attrs);
2372-
}
2368+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2369+
goto repeat;
23732370
}
2374-
if (gle == ERROR_ACCESS_DENIED &&
2375-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2371+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
23762372
"Should I try again?", pold, pnew))
23772373
goto repeat;
23782374

0 commit comments

Comments
 (0)