Skip to content

Commit d0b35df

Browse files
dsl101Git for Windows Build Agent
authored and
Git for Windows Build Agent
committed
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 091cc3a commit d0b35df

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
@@ -2702,7 +2702,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
27022702
int mingw_rename(const char *pold, const char *pnew)
27032703
{
27042704
static int supports_file_rename_info_ex = 1;
2705-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2705+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
27062706
int tries = 0;
27072707
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
27082708
int wpnew_len;
@@ -2792,11 +2792,24 @@ int mingw_rename(const char *pold, const char *pnew)
27922792
gle = GetLastError();
27932793
}
27942794

2795-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2796-
/* Fall back to copy to destination & remove source */
2797-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2798-
return 0;
2799-
gle = GetLastError();
2795+
if (gle == ERROR_ACCESS_DENIED) {
2796+
if (is_inside_windows_container()) {
2797+
/* Fall back to copy to destination & remove source */
2798+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2799+
return 0;
2800+
gle = GetLastError();
2801+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2802+
/* if file is read-only, change and retry */
2803+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2804+
if (MoveFileExW(wpold, wpnew,
2805+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2806+
SetFileAttributesW(wpnew, attrsold);
2807+
return 0;
2808+
}
2809+
gle = GetLastError();
2810+
/* revert attribute change on failure */
2811+
SetFileAttributesW(wpold, attrsold);
2812+
}
28002813
}
28012814

28022815
/* revert file attributes on failure */

0 commit comments

Comments
 (0)