fileutils: force owner write while copying read-only sources

Opening the destination at the source's exact mode breaks when the
source lacks owner write (e.g. r-xr-xr-x): the create still works but
the overwrite path can't reopen such a destination for writing. Force
owner read+write while copying and let the trailing Chmod() put the
source mode back, which only ever adds owner bits so the group/other
bits stay as restrictive as the source throughout. Add a regression
test covering the restricted-mode and read-only-source cases.
This commit is contained in:
dxbjavid
2026-06-09 09:53:57 +05:30
parent 1701edca5b
commit 01678166a8
2 changed files with 60 additions and 1 deletions
+5 -1
View File
@@ -265,7 +265,11 @@ bool CFile::Copy(const CString& sOldFileName, const CString& sNewFileName,
iMode = st.st_mode & 07777;
}
if (!NewFile.Open(O_WRONLY | O_CREAT | O_TRUNC, iMode)) {
// Force owner read+write while copying so the write still works for a
// source that lacks them (e.g. r-xr-xr-x); the trailing Chmod() puts
// the exact source mode back. This only ever adds owner bits, so the
// group/other bits stay as restrictive as the source the whole time.
if (!NewFile.Open(O_WRONLY | O_CREAT | O_TRUNC, iMode | S_IRUSR | S_IWUSR)) {
return false;
}