Skip to content

Commit 592fcc7

Browse files
dsl101dscho
authored andcommitted
mingw: work around rename() failing on a read-only file
At least on _some_ APFS network shares, Git fails to rename the object files because they are marked as read-only, because that has the effect of setting the uchg flag on APFS, which then means the file can't be renamed or deleted. To work around that, when a rename failed, and the read-only flag is set, try to turn it off and on again. This fixes #4482 Signed-off-by: David Lomas <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 889cbb1 commit 592fcc7

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
@@ -2678,7 +2678,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
26782678
int mingw_rename(const char *pold, const char *pnew)
26792679
{
26802680
static int supports_file_rename_info_ex = 1;
2681-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2681+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
26822682
int tries = 0;
26832683
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
26842684
int wpnew_len;
@@ -2768,11 +2768,24 @@ int mingw_rename(const char *pold, const char *pnew)
27682768
gle = GetLastError();
27692769
}
27702770

2771-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2772-
/* Fall back to copy to destination & remove source */
2773-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2774-
return 0;
2775-
gle = GetLastError();
2771+
if (gle == ERROR_ACCESS_DENIED) {
2772+
if (is_inside_windows_container()) {
2773+
/* Fall back to copy to destination & remove source */
2774+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2775+
return 0;
2776+
gle = GetLastError();
2777+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2778+
/* if file is read-only, change and retry */
2779+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2780+
if (MoveFileExW(wpold, wpnew,
2781+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2782+
SetFileAttributesW(wpnew, attrsold);
2783+
return 0;
2784+
}
2785+
gle = GetLastError();
2786+
/* revert attribute change on failure */
2787+
SetFileAttributesW(wpold, attrsold);
2788+
}
27762789
}
27772790

27782791
/* revert file attributes on failure */

0 commit comments

Comments
 (0)