Skip to content

Commit 444d7d3

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 3a7b998 commit 444d7d3

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
@@ -2563,7 +2563,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25632563
#undef rename
25642564
int mingw_rename(const char *pold, const char *pnew)
25652565
{
2566-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2566+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
25672567
int tries = 0;
25682568
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25692569
if (xutftowcs_long_path(wpold, pold) < 0 ||
@@ -2576,11 +2576,24 @@ int mingw_rename(const char *pold, const char *pnew)
25762576
return 0;
25772577
gle = GetLastError();
25782578

2579-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2580-
/* Fall back to copy to destination & remove source */
2581-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2582-
return 0;
2583-
gle = GetLastError();
2579+
if (gle == ERROR_ACCESS_DENIED) {
2580+
if (is_inside_windows_container()) {
2581+
/* Fall back to copy to destination & remove source */
2582+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2583+
return 0;
2584+
gle = GetLastError();
2585+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2586+
/* if file is read-only, change and retry */
2587+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2588+
if (MoveFileExW(wpold, wpnew,
2589+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2590+
SetFileAttributesW(wpnew, attrsold);
2591+
return 0;
2592+
}
2593+
gle = GetLastError();
2594+
/* revert attribute change on failure */
2595+
SetFileAttributesW(wpold, attrsold);
2596+
}
25842597
}
25852598

25862599
/* revert file attributes on failure */

0 commit comments

Comments
 (0)