Skip to content

Commit 5957111

Browse files
committed
mingw: try resetting the read-only bit if rename fails (#4527)
With this patch, Git for Windows works as intended on mounted APFS volumes (where renaming read-only files would fail). Signed-off-by: Johannes Schindelin <[email protected]>
2 parents e6969fc + 90b30de commit 5957111

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

compat/mingw.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,7 +2577,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25772577
#undef rename
25782578
int mingw_rename(const char *pold, const char *pnew)
25792579
{
2580-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2580+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
25812581
int tries = 0;
25822582
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25832583
if (xutftowcs_long_path(wpold, pold) < 0 ||
@@ -2590,11 +2590,24 @@ int mingw_rename(const char *pold, const char *pnew)
25902590
return 0;
25912591
gle = GetLastError();
25922592

2593-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2594-
/* Fall back to copy to destination & remove source */
2595-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2596-
return 0;
2597-
gle = GetLastError();
2593+
if (gle == ERROR_ACCESS_DENIED) {
2594+
if (is_inside_windows_container()) {
2595+
/* Fall back to copy to destination & remove source */
2596+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2597+
return 0;
2598+
gle = GetLastError();
2599+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2600+
/* if file is read-only, change and retry */
2601+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2602+
if (MoveFileExW(wpold, wpnew,
2603+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2604+
SetFileAttributesW(wpnew, attrsold);
2605+
return 0;
2606+
}
2607+
gle = GetLastError();
2608+
/* revert attribute change on failure */
2609+
SetFileAttributesW(wpold, attrsold);
2610+
}
25982611
}
25992612

26002613
/* revert file attributes on failure */

0 commit comments

Comments
 (0)