Skip to content

Commit 1625348

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 c397473 commit 1625348

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

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

0 commit comments

Comments
 (0)