Skip to content

Commit 36d13bd

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 <johannes.schindelin@gmx.de>
2 parents c1360ad + e5aea4c commit 36d13bd

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

compat/mingw.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
27982798
int mingw_rename(const char *pold, const char *pnew)
27992799
{
28002800
static int supports_file_rename_info_ex = 1;
2801-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2801+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
28022802
int tries = 0;
28032803
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
28042804
int wpnew_len;
@@ -2890,11 +2890,24 @@ int mingw_rename(const char *pold, const char *pnew)
28902890
gle = GetLastError();
28912891
}
28922892

2893-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2894-
/* Fall back to copy to destination & remove source */
2895-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold, 1))
2896-
return 0;
2897-
gle = GetLastError();
2893+
if (gle == ERROR_ACCESS_DENIED) {
2894+
if (is_inside_windows_container()) {
2895+
/* Fall back to copy to destination & remove source */
2896+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold, 1))
2897+
return 0;
2898+
gle = GetLastError();
2899+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2900+
/* if file is read-only, change and retry */
2901+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2902+
if (MoveFileExW(wpold, wpnew,
2903+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2904+
SetFileAttributesW(wpnew, attrsold);
2905+
return 0;
2906+
}
2907+
gle = GetLastError();
2908+
/* revert attribute change on failure */
2909+
SetFileAttributesW(wpold, attrsold);
2910+
}
28982911
}
28992912

29002913
/* revert file attributes on failure */

0 commit comments

Comments
 (0)