Skip to content

Commit ea2309a

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 a83531b + 4bcc256 commit ea2309a

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
@@ -2703,7 +2703,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
27032703
int mingw_rename(const char *pold, const char *pnew)
27042704
{
27052705
static int supports_file_rename_info_ex = 1;
2706-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2706+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
27072707
int tries = 0;
27082708
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
27092709
int wpnew_len;
@@ -2793,11 +2793,24 @@ int mingw_rename(const char *pold, const char *pnew)
27932793
gle = GetLastError();
27942794
}
27952795

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

28032816
/* revert file attributes on failure */

0 commit comments

Comments
 (0)