Skip to content

Commit 04116c6

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 3a21276 commit 04116c6

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
@@ -2307,27 +2307,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
23072307
#undef rename
23082308
int mingw_rename(const char *pold, const char *pnew)
23092309
{
2310-
DWORD attrs, gle;
2310+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
23112311
int tries = 0;
23122312
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
23132313
if (xutftowcs_long_path(wpold, pold) < 0 ||
23142314
xutftowcs_long_path(wpnew, pnew) < 0)
23152315
return -1;
23162316

2317-
/*
2318-
* Try native rename() first to get errno right.
2319-
* It is based on MoveFile(), which cannot overwrite existing files.
2320-
*/
2321-
if (!_wrename(wpold, wpnew))
2322-
return 0;
2323-
if (errno != EEXIST)
2324-
return -1;
23252317
repeat:
2326-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2318+
if (MoveFileExW(wpold, wpnew,
2319+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
23272320
return 0;
2328-
/* TODO: translate more errors */
23292321
gle = GetLastError();
2330-
if (gle == ERROR_ACCESS_DENIED &&
2322+
2323+
/* revert file attributes on failure */
2324+
if (attrs != INVALID_FILE_ATTRIBUTES)
2325+
SetFileAttributesW(wpnew, attrs);
2326+
2327+
if (!is_file_in_use_error(gle)) {
2328+
errno = err_win_to_posix(gle);
2329+
return -1;
2330+
}
2331+
2332+
if (attrs == INVALID_FILE_ATTRIBUTES &&
23312333
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
23322334
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
23332335
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2339,16 +2341,10 @@ int mingw_rename(const char *pold, const char *pnew)
23392341
return -1;
23402342
}
23412343
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2342-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2343-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2344-
return 0;
2345-
gle = GetLastError();
2346-
/* revert file attributes on failure */
2347-
SetFileAttributesW(wpnew, attrs);
2348-
}
2344+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2345+
goto repeat;
23492346
}
2350-
if (gle == ERROR_ACCESS_DENIED &&
2351-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2347+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
23522348
"Should I try again?", pold, pnew))
23532349
goto repeat;
23542350

0 commit comments

Comments
 (0)