Skip to content

Commit cf25bbc

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 492cfa7 commit cf25bbc

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
@@ -2583,7 +2583,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25832583
#undef rename
25842584
int mingw_rename(const char *pold, const char *pnew)
25852585
{
2586-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2586+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
25872587
int tries = 0;
25882588
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25892589
if (xutftowcs_long_path(wpold, pold) < 0 ||
@@ -2596,11 +2596,24 @@ int mingw_rename(const char *pold, const char *pnew)
25962596
return 0;
25972597
gle = GetLastError();
25982598

2599-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2600-
/* Fall back to copy to destination & remove source */
2601-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2602-
return 0;
2603-
gle = GetLastError();
2599+
if (gle == ERROR_ACCESS_DENIED) {
2600+
if (is_inside_windows_container()) {
2601+
/* Fall back to copy to destination & remove source */
2602+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2603+
return 0;
2604+
gle = GetLastError();
2605+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2606+
/* if file is read-only, change and retry */
2607+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2608+
if (MoveFileExW(wpold, wpnew,
2609+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2610+
SetFileAttributesW(wpnew, attrsold);
2611+
return 0;
2612+
}
2613+
gle = GetLastError();
2614+
/* revert attribute change on failure */
2615+
SetFileAttributesW(wpold, attrsold);
2616+
}
26042617
}
26052618

26062619
/* revert file attributes on failure */

0 commit comments

Comments
 (0)