Skip to content

Commit 406123c

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 b9fedd6 commit 406123c

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
@@ -2316,27 +2316,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
23162316
#undef rename
23172317
int mingw_rename(const char *pold, const char *pnew)
23182318
{
2319-
DWORD attrs, gle;
2319+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
23202320
int tries = 0;
23212321
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
23222322
if (xutftowcs_long_path(wpold, pold) < 0 ||
23232323
xutftowcs_long_path(wpnew, pnew) < 0)
23242324
return -1;
23252325

2326-
/*
2327-
* Try native rename() first to get errno right.
2328-
* It is based on MoveFile(), which cannot overwrite existing files.
2329-
*/
2330-
if (!_wrename(wpold, wpnew))
2331-
return 0;
2332-
if (errno != EEXIST)
2333-
return -1;
23342326
repeat:
2335-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2327+
if (MoveFileExW(wpold, wpnew,
2328+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
23362329
return 0;
2337-
/* TODO: translate more errors */
23382330
gle = GetLastError();
2339-
if (gle == ERROR_ACCESS_DENIED &&
2331+
2332+
/* revert file attributes on failure */
2333+
if (attrs != INVALID_FILE_ATTRIBUTES)
2334+
SetFileAttributesW(wpnew, attrs);
2335+
2336+
if (!is_file_in_use_error(gle)) {
2337+
errno = err_win_to_posix(gle);
2338+
return -1;
2339+
}
2340+
2341+
if (attrs == INVALID_FILE_ATTRIBUTES &&
23402342
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
23412343
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
23422344
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2348,16 +2350,10 @@ int mingw_rename(const char *pold, const char *pnew)
23482350
return -1;
23492351
}
23502352
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2351-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2352-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2353-
return 0;
2354-
gle = GetLastError();
2355-
/* revert file attributes on failure */
2356-
SetFileAttributesW(wpnew, attrs);
2357-
}
2353+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2354+
goto repeat;
23582355
}
2359-
if (gle == ERROR_ACCESS_DENIED &&
2360-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2356+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
23612357
"Should I try again?", pold, pnew))
23622358
goto repeat;
23632359

0 commit comments

Comments
 (0)