Skip to content

Commit d19b030

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 fdd4fe4 commit d19b030

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

compat/mingw.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,28 +1836,29 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
18361836
#undef rename
18371837
int mingw_rename(const char *pold, const char *pnew)
18381838
{
1839-
DWORD attrs, gle;
1839+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
18401840
int tries = 0;
18411841
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
18421842
if (xutftowcs_long_path(wpold, pold) < 0 ||
18431843
xutftowcs_long_path(wpnew, pnew) < 0)
18441844
return -1;
18451845

1846-
/*
1847-
* Try native rename() first to get errno right.
1848-
* It is based on MoveFile(), which cannot overwrite existing files.
1849-
*/
1850-
if (!_wrename(wpold, wpnew))
1851-
return 0;
1852-
if (errno != EEXIST)
1853-
return -1;
18541846
repeat:
1855-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1847+
if (MoveFileExW(wpold, wpnew,
1848+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
18561849
return 0;
1857-
/* TODO: translate more errors */
18581850
gle = GetLastError();
1859-
if (gle == ERROR_ACCESS_DENIED &&
1860-
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
1851+
1852+
/* revert file attributes on failure */
1853+
if (attrs != INVALID_FILE_ATTRIBUTES)
1854+
SetFileAttributesW(wpnew, attrs);
1855+
1856+
if (!is_file_in_use_error(gle)) {
1857+
errno = err_win_to_posix(gle);
1858+
return -1;
1859+
}
1860+
1861+
if ((attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
18611862
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
18621863
DWORD attrsold = GetFileAttributesW(wpold);
18631864
if (attrsold == INVALID_FILE_ATTRIBUTES ||
@@ -1868,16 +1869,10 @@ int mingw_rename(const char *pold, const char *pnew)
18681869
return -1;
18691870
}
18701871
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
1871-
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
1872-
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
1873-
return 0;
1874-
gle = GetLastError();
1875-
/* revert file attributes on failure */
1876-
SetFileAttributesW(wpnew, attrs);
1877-
}
1872+
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY))
1873+
goto repeat;
18781874
}
1879-
if (gle == ERROR_ACCESS_DENIED &&
1880-
retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
1875+
if (retry_ask_yes_no(&tries, "Rename from '%s' to '%s' failed. "
18811876
"Should I try again?", pold, pnew))
18821877
goto repeat;
18831878

0 commit comments

Comments
 (0)