Skip to content

Commit e333dec

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 f558a61 commit e333dec

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

compat/mingw.c

+19-6
Original file line numberDiff line numberDiff line change
@@ -2727,7 +2727,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
27272727
int mingw_rename(const char *pold, const char *pnew)
27282728
{
27292729
static int supports_file_rename_info_ex = 1;
2730-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2730+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
27312731
int tries = 0;
27322732
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
27332733
int wpnew_len;
@@ -2815,11 +2815,24 @@ int mingw_rename(const char *pold, const char *pnew)
28152815
gle = GetLastError();
28162816
}
28172817

2818-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2819-
/* Fall back to copy to destination & remove source */
2820-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2821-
return 0;
2822-
gle = GetLastError();
2818+
if (gle == ERROR_ACCESS_DENIED) {
2819+
if (is_inside_windows_container()) {
2820+
/* Fall back to copy to destination & remove source */
2821+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2822+
return 0;
2823+
gle = GetLastError();
2824+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2825+
/* if file is read-only, change and retry */
2826+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2827+
if (MoveFileExW(wpold, wpnew,
2828+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2829+
SetFileAttributesW(wpnew, attrsold);
2830+
return 0;
2831+
}
2832+
gle = GetLastError();
2833+
/* revert attribute change on failure */
2834+
SetFileAttributesW(wpold, attrsold);
2835+
}
28232836
}
28242837

28252838
/* revert file attributes on failure */

0 commit comments

Comments
 (0)