Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset READONLY if rename fails #4527

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -2700,7 +2700,7 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
#undef rename
int mingw_rename(const char *pold, const char *pnew)
{
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle;
DWORD attrs = INVALID_FILE_ATTRIBUTES, gle, attrsold;
int tries = 0;
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
if (xutftowcs_long_path(wpold, pold) < 0 ||
Expand All @@ -2713,11 +2713,24 @@ int mingw_rename(const char *pold, const char *pnew)
return 0;
gle = GetLastError();

if (gle == ERROR_ACCESS_DENIED && is_inside_windows_container()) {
/* Fall back to copy to destination & remove source */
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
return 0;
gle = GetLastError();
if (gle == ERROR_ACCESS_DENIED) {
if (is_inside_windows_container()) {
/* Fall back to copy to destination & remove source */
if (CopyFileW(wpold, wpnew, FALSE) && !mingw_unlink(pold))
return 0;
gle = GetLastError();
} else if ((attrsold = GetFileAttributesW(wpold)) & FILE_ATTRIBUTE_READONLY) {
/* if file is read-only, change and retry */
SetFileAttributesW(wpold, attrsold & ~FILE_ATTRIBUTE_READONLY);
if (MoveFileExW(wpold, wpnew,
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)) {
SetFileAttributesW(wpnew, attrsold);
return 0;
}
gle = GetLastError();
/* revert attribute change on failure */
SetFileAttributesW(wpold, attrsold);
}
}

/* revert file attributes on failure */
Expand Down