Skip to content

Commit d3aa1e3

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 ac17b65 commit d3aa1e3

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

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

0 commit comments

Comments
 (0)