Skip to content

Commit 90b30de

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 2ab9aeb commit 90b30de

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
@@ -2577,7 +2577,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25772577
#undef rename
25782578
int mingw_rename(const char *pold, const char *pnew)
25792579
{
2580-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2580+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
25812581
int tries = 0;
25822582
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25832583
if (xutftowcs_long_path(wpold, pold) < 0 ||
@@ -2590,11 +2590,24 @@ int mingw_rename(const char *pold, const char *pnew)
25902590
return 0;
25912591
gle = GetLastError();
25922592

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

26002613
/* revert file attributes on failure */

0 commit comments

Comments
 (0)