Skip to content

Commit 7d083b0

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 1d3fe46 + 77800b1 commit 7d083b0

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
@@ -2701,7 +2701,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
27012701
int mingw_rename(const char *pold, const char *pnew)
27022702
{
27032703
static int supports_file_rename_info_ex = 1;
2704-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2704+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
27052705
int tries = 0;
27062706
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
27072707
int wpnew_len;
@@ -2791,11 +2791,24 @@ int mingw_rename(const char *pold, const char *pnew)
27912791
gle = GetLastError();
27922792
}
27932793

2794-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2795-
/* Fall back to copy to destination & remove source */
2796-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2797-
return 0;
2798-
gle = GetLastError();
2794+
if (gle == ERROR_ACCESS_DENIED) {
2795+
if (is_inside_windows_container()) {
2796+
/* Fall back to copy to destination & remove source */
2797+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold, 1))
2798+
return 0;
2799+
gle = GetLastError();
2800+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2801+
/* if file is read-only, change and retry */
2802+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2803+
if (MoveFileExW(wpold, wpnew,
2804+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2805+
SetFileAttributesW(wpnew, attrsold);
2806+
return 0;
2807+
}
2808+
gle = GetLastError();
2809+
/* revert attribute change on failure */
2810+
SetFileAttributesW(wpold, attrsold);
2811+
}
27992812
}
28002813

28012814
/* revert file attributes on failure */

0 commit comments

Comments
 (0)