Skip to content

Commit 08d9b82

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 bec7926 commit 08d9b82

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

compat/mingw.c

+17-21
Original file line numberDiff line numberDiff line change
@@ -2375,27 +2375,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
23752375
#undef rename
23762376
int mingw_rename(const char *pold, const char *pnew)
23772377
{
2378-
DWORD attrs, gle;
2378+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
23792379
int tries = 0;
23802380
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
23812381
if (xutftowcs_long_path(wpold, pold) < 0 ||
23822382
xutftowcs_long_path(wpnew, pnew) < 0)
23832383
return -1;
23842384

2385-
/*
2386-
* Try native rename() first to get errno right.
2387-
* It is based on MoveFile(), which cannot overwrite existing files.
2388-
*/
2389-
if (!_wrename(wpold, wpnew))
2390-
return 0;
2391-
if (errno != EEXIST)
2392-
return -1;
23932385
repeat:
2394-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2386+
if (MoveFileExW(wpold, wpnew,
2387+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
23952388
return 0;
2396-
/* TODO: translate more errors */
23972389
gle = GetLastError();
2398-
if (gle == ERROR_ACCESS_DENIED &&
2390+
2391+
/* revert file attributes on failure */
2392+
if (attrs != INVALID_FILE_ATTRIBUTES)
2393+
SetFileAttributesW(wpnew, attrs);
2394+
2395+
if (!is_file_in_use_error(gle)) {
2396+
errno = err_win_to_posix(gle);
2397+
return -1;
2398+
}
2399+
2400+
if (attrs == INVALID_FILE_ATTRIBUTES &&
23992401
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
24002402
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
24012403
DWORD attrsold = GetFileAttributesW(wpold);
@@ -2407,16 +2409,10 @@ int mingw_rename(const char *pold, const char *pnew)
24072409
return -1;
24082410
}
24092411
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
2410-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
2411-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
2412-
return 0;
2413-
gle = GetLastError();
2414-
/* revert file attributes on failure */
2415-
SetFileAttributesW(wpnew, attrs);
2416-
}
2412+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
2413+
goto repeat;
24172414
}
2418-
if (gle == ERROR_ACCESS_DENIED &&
2419-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
2415+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
24202416
"Should I try again?", pold, pnew))
24212417
goto repeat;
24222418

0 commit comments

Comments
 (0)