Skip to content

Commit a439f05

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 832f589 commit a439f05

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
@@ -2558,7 +2558,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
25582558
#undef rename
25592559
int mingw_rename(const char *pold, const char *pnew)
25602560
{
2561-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2561+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
25622562
int tries = 0;
25632563
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
25642564
if (xutftowcs_long_path(wpold, pold) < 0 ||
@@ -2571,11 +2571,24 @@ int mingw_rename(const char *pold, const char *pnew)
25712571
return 0;
25722572
gle = GetLastError();
25732573

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

25812594
/* revert file attributes on failure */

0 commit comments

Comments
 (0)