Skip to content

Commit 3f13555

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 f49949c commit 3f13555

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
@@ -2282,27 +2282,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
22822282
#undef rename
22832283
int mingw_rename(const char *pold, const char *pnew)
22842284
{
2285-
DWORD attrs, gle;
2285+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
22862286
int tries = 0;
22872287
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
22882288
if (xutftowcs_long_path(wpold, pold) < 0 ||
22892289
xutftowcs_long_path(wpnew, pnew) < 0)
22902290
return -1;
22912291

2292-
/*
2293-
* Try native rename() first to get errno right.
2294-
* It is based on MoveFile(), which cannot overwrite existing files.
2295-
*/
2296-
if (!_wrename(wpold, wpnew))
2297-
return 0;
2298-
if (errno != EEXIST)
2299-
return -1;
23002292
repeat:
2301-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2293+
if (MoveFileExW(wpold, wpnew,
2294+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
23022295
return 0;
2303-
/* TODO: translate more errors */
23042296
gle = GetLastError();
2305-
if (gle == ERROR_ACCESS_DENIED &&
2297+
2298+
/* revert file attributes on failure */
2299+
if (attrs != INVALID_FILE_ATTRIBUTES)
2300+
SetFileAttributesW(wpnew, attrs);
2301+
2302+
if (!is_file_in_use_error(gle)) {
2303+
errno = err_win_to_posix(gle);
2304+
return -1;
2305+
}
2306+
2307+
if (attrs == INVALID_FILE_ATTRIBUTES &&
23062308
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
23072309
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
23082310
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2314,16 +2316,10 @@ int mingw_rename(const char *pold, const char *pnew)
23142316
return -1;
23152317
}
23162318
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2317-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2318-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2319-
return 0;
2320-
gle = GetLastError();
2321-
/* revert file attributes on failure */
2322-
SetFileAttributesW(wpnew, attrs);
2323-
}
2319+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2320+
goto repeat;
23242321
}
2325-
if (gle == ERROR_ACCESS_DENIED &&
2326-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2322+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
23272323
"Should I try again?", pold, pnew))
23282324
goto repeat;
23292325

0 commit comments

Comments
 (0)