Skip to content

Commit 7efdf11

Browse files
kbleesderrickstolee
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 c7065eb commit 7efdf11

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
@@ -2250,27 +2250,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
22502250
#undef rename
22512251
int mingw_rename(const char *pold, const char *pnew)
22522252
{
2253-
DWORD attrs, gle;
2253+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
22542254
int tries = 0;
22552255
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
22562256
if (xutftowcs_long_path(wpold, pold) < 0 ||
22572257
xutftowcs_long_path(wpnew, pnew) < 0)
22582258
return -1;
22592259

2260-
/*
2261-
* Try native rename() first to get errno right.
2262-
* It is based on MoveFile(), which cannot overwrite existing files.
2263-
*/
2264-
if (!_wrename(wpold, wpnew))
2265-
return 0;
2266-
if (errno != EEXIST)
2267-
return -1;
22682260
repeat:
2269-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2261+
if (MoveFileExW(wpold, wpnew,
2262+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
22702263
return 0;
2271-
/* TODO: translate more errors */
22722264
gle = GetLastError();
2273-
if (gle == ERROR_ACCESS_DENIED &&
2265+
2266+
/* revert file attributes on failure */
2267+
if (attrs != INVALID_FILE_ATTRIBUTES)
2268+
SetFileAttributesW(wpnew, attrs);
2269+
2270+
if (!is_file_in_use_error(gle)) {
2271+
errno = err_win_to_posix(gle);
2272+
return -1;
2273+
}
2274+
2275+
if (attrs == INVALID_FILE_ATTRIBUTES &&
22742276
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
22752277
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
22762278
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2282,16 +2284,10 @@ int mingw_rename(const char *pold, const char *pnew)
22822284
return -1;
22832285
}
22842286
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2285-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2286-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2287-
return 0;
2288-
gle = GetLastError();
2289-
/* revert file attributes on failure */
2290-
SetFileAttributesW(wpnew, attrs);
2291-
}
2287+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2288+
goto repeat;
22922289
}
2293-
if (gle == ERROR_ACCESS_DENIED &&
2294-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2290+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
22952291
"Should I try again?", pold, pnew))
22962292
goto repeat;
22972293

0 commit comments

Comments
 (0)