Skip to content

Commit ee130a5

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 65e200e + 9372700 commit ee130a5

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
@@ -2578,7 +2578,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25782578
#undef rename
25792579
int mingw_rename(const char *pold, const char *pnew)
25802580
{
2581-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2581+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
25822582
int tries = 0;
25832583
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25842584
if (xutftowcs_long_path(wpold, pold) < 0 ||
@@ -2591,11 +2591,24 @@ int mingw_rename(const char *pold, const char *pnew)
25912591
return 0;
25922592
gle = GetLastError();
25932593

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

26012614
/* revert file attributes on failure */

0 commit comments

Comments
 (0)