Skip to content

Commit 800b97c

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 <johannes.schindelin@gmx.de>
2 parents ad7afd7 + f54de32 commit 800b97c

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

compat/mingw.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,7 +2794,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
27942794
int mingw_rename(const char *pold, const char *pnew)
27952795
{
27962796
static int supports_file_rename_info_ex = 1;
2797-
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
2797+
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
27982798
int tries = 0;
27992799
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
28002800
int wpnew_len;
@@ -2886,11 +2886,24 @@ int mingw_rename(const char *pold, const char *pnew)
28862886
gle = GetLastError();
28872887
}
28882888

2889-
if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
2890-
/* Fall back to copy to destination & remove source */
2891-
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
2892-
return 0;
2893-
gle = GetLastError();
2889+
if (gle == ERROR_ACCESS_DENIED) {
2890+
if (is_inside_windows_container()) {
2891+
/* Fall back to copy to destination & remove source */
2892+
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold, 1))
2893+
return 0;
2894+
gle = GetLastError();
2895+
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
2896+
/* if file is read-only, change and retry */
2897+
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
2898+
if (MoveFileExW(wpold, wpnew,
2899+
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
2900+
SetFileAttributesW(wpnew, attrsold);
2901+
return 0;
2902+
}
2903+
gle = GetLastError();
2904+
/* revert attribute change on failure */
2905+
SetFileAttributesW(wpold, attrsold);
2906+
}
28942907
}
28952908

28962909
/* revert file attributes on failure */

0 commit comments

Comments
 (0)