Skip to content

Commit eaf8a32

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 de17edc commit eaf8a32

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
@@ -2738,7 +2738,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
27382738
int mingw_rename(const char *pold, const char *pnew)
27392739
{
27402740
static int supports_file_rename_info_ex = 1;
2741-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2741+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
27422742
int tries = 0;
27432743
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
27442744
int wpnew_len;
@@ -2828,11 +2828,24 @@ int mingw_rename(const char *pold, const char *pnew)
28282828
gle = GetLastError();
28292829
}
28302830

2831-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2832-
/* Fall back to copy to destination & remove source */
2833-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2834-
return 0;
2835-
gle = GetLastError();
2831+
if (gle == ERROR_ACCESS_DENIED) {
2832+
if (is_inside_windows_container()) {
2833+
/* Fall back to copy to destination & remove source */
2834+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2835+
return 0;
2836+
gle = GetLastError();
2837+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2838+
/* if file is read-only, change and retry */
2839+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2840+
if (MoveFileExW(wpold, wpnew,
2841+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2842+
SetFileAttributesW(wpnew, attrsold);
2843+
return 0;
2844+
}
2845+
gle = GetLastError();
2846+
/* revert attribute change on failure */
2847+
SetFileAttributesW(wpold, attrsold);
2848+
}
28362849
}
28372850

28382851
/* revert file attributes on failure */

0 commit comments

Comments
 (0)