Skip to content

Commit 6fdd1c8

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 68ccaa5 + ec4fa5d commit 6fdd1c8

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
@@ -2650,7 +2650,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
26502650
int mingw_rename(const char *pold, const char *pnew)
26512651
{
26522652
static int supports_file_rename_info_ex = 1;
2653-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2653+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
26542654
int tries = 0;
26552655
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
26562656
int wpnew_len;
@@ -2738,11 +2738,24 @@ int mingw_rename(const char *pold, const char *pnew)
27382738
gle = GetLastError();
27392739
}
27402740

2741-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2742-
/* Fall back to copy to destination & remove source */
2743-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2744-
return 0;
2745-
gle = GetLastError();
2741+
if (gle == ERROR_ACCESS_DENIED) {
2742+
if (is_inside_windows_container()) {
2743+
/* Fall back to copy to destination & remove source */
2744+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2745+
return 0;
2746+
gle = GetLastError();
2747+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2748+
/* if file is read-only, change and retry */
2749+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2750+
if (MoveFileExW(wpold, wpnew,
2751+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2752+
SetFileAttributesW(wpnew, attrsold);
2753+
return 0;
2754+
}
2755+
gle = GetLastError();
2756+
/* revert attribute change on failure */
2757+
SetFileAttributesW(wpold, attrsold);
2758+
}
27462759
}
27472760

27482761
/* revert file attributes on failure */

0 commit comments

Comments
 (0)