Skip to content

Commit c6f8cce

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 847495e commit c6f8cce

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

2321-
/*
2322-
* Try native rename() first to get errno right.
2323-
* It is based on MoveFile(), which cannot overwrite existing files.
2324-
*/
2325-
if (!_wrename(wpold, wpnew))
2326-
return 0;
2327-
if (errno != EEXIST)
2328-
return -1;
23292321
repeat:
2330-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2322+
if (MoveFileExW(wpold, wpnew,
2323+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
23312324
return 0;
2332-
/* TODO: translate more errors */
23332325
gle = GetLastError();
2334-
if (gle == ERROR_ACCESS_DENIED &&
2326+
2327+
/* revert file attributes on failure */
2328+
if (attrs != INVALID_FILE_ATTRIBUTES)
2329+
SetFileAttributesW(wpnew, attrs);
2330+
2331+
if (!is_file_in_use_error(gle)) {
2332+
errno = err_win_to_posix(gle);
2333+
return -1;
2334+
}
2335+
2336+
if (attrs == INVALID_FILE_ATTRIBUTES &&
23352337
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
23362338
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
23372339
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2343,16 +2345,10 @@ int mingw_rename(const char *pold, const char *pnew)
23432345
return -1;
23442346
}
23452347
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2346-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2347-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2348-
return 0;
2349-
gle = GetLastError();
2350-
/* revert file attributes on failure */
2351-
SetFileAttributesW(wpnew, attrs);
2352-
}
2348+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2349+
goto repeat;
23532350
}
2354-
if (gle == ERROR_ACCESS_DENIED &&
2355-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2351+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
23562352
"Should I try again?", pold, pnew))
23572353
goto repeat;
23582354

0 commit comments

Comments
 (0)