Skip to content

Commit e24f541

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 1064dda commit e24f541

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

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

0 commit comments

Comments
 (0)