Skip to content

Commit f8ec49f

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
mingw: try resetting the read-only bit if rename fails (#4527)
With this patch, Git for Windows works as intended on mounted APFS volumes (where renaming read-only files would fail). Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 5ce629e + 782828d commit f8ec49f

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)