Skip to content

Commit a15cc39

Browse files
kbleesmjcheetham
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 2294f8e commit a15cc39

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
@@ -2393,27 +2393,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
23932393
#undef rename
23942394
int mingw_rename(const char *pold, const char *pnew)
23952395
{
2396-
DWORD attrs, gle;
2396+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
23972397
int tries = 0;
23982398
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
23992399
if (xutftowcs_long_path(wpold, pold) < 0 ||
24002400
xutftowcs_long_path(wpnew, pnew) < 0)
24012401
return -1;
24022402

2403-
/*
2404-
* Try native rename() first to get errno right.
2405-
* It is based on MoveFile(), which cannot overwrite existing files.
2406-
*/
2407-
if (!_wrename(wpold, wpnew))
2408-
return 0;
2409-
if (errno != EEXIST)
2410-
return -1;
24112403
repeat:
2412-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2404+
if (MoveFileExW(wpold, wpnew,
2405+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
24132406
return 0;
2414-
/* TODO: translate more errors */
24152407
gle = GetLastError();
2416-
if (gle == ERROR_ACCESS_DENIED &&
2408+
2409+
/* revert file attributes on failure */
2410+
if (attrs != INVALID_FILE_ATTRIBUTES)
2411+
SetFileAttributesW(wpnew, attrs);
2412+
2413+
if (!is_file_in_use_error(gle)) {
2414+
errno = err_win_to_posix(gle);
2415+
return -1;
2416+
}
2417+
2418+
if (attrs == INVALID_FILE_ATTRIBUTES &&
24172419
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
24182420
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
24192421
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2425,16 +2427,10 @@ int mingw_rename(const char *pold, const char *pnew)
24252427
return -1;
24262428
}
24272429
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2428-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2429-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2430-
return 0;
2431-
gle = GetLastError();
2432-
/* revert file attributes on failure */
2433-
SetFileAttributesW(wpnew, attrs);
2434-
}
2430+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2431+
goto repeat;
24352432
}
2436-
if (gle == ERROR_ACCESS_DENIED &&
2437-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2433+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24382434
"Should I try again?", pold, pnew))
24392435
goto repeat;
24402436

0 commit comments

Comments
 (0)