From 1701edca5b88b93cba7a2bc87c9f5bb674488123 Mon Sep 17 00:00:00 2001 From: dxbjavid Date: Mon, 25 May 2026 13:08:43 +0530 Subject: [PATCH] fileutils: open Copy() destination at the source's mode --- src/FileUtils.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/FileUtils.cpp b/src/FileUtils.cpp index fd98ed85..bf434431 100644 --- a/src/FileUtils.cpp +++ b/src/FileUtils.cpp @@ -256,7 +256,16 @@ bool CFile::Copy(const CString& sOldFileName, const CString& sNewFileName, return false; } - if (!NewFile.Open(O_WRONLY | O_CREAT | O_TRUNC)) { + // Stat the source up front so the new file is created with the + // same mode instead of appearing at the default 0644 for the + // duration of the copy. + struct stat st; + mode_t iMode = 0600; + if (GetInfo(sOldFileName, st) == 0) { + iMode = st.st_mode & 07777; + } + + if (!NewFile.Open(O_WRONLY | O_CREAT | O_TRUNC, iMode)) { return false; } @@ -280,9 +289,9 @@ bool CFile::Copy(const CString& sOldFileName, const CString& sNewFileName, OldFile.Close(); NewFile.Close(); - struct stat st; - GetInfo(sOldFileName, st); - Chmod(sNewFileName, st.st_mode); + // open(O_CREAT|O_TRUNC) doesn't update the mode of an existing + // file, so apply it here for the overwrite path. + Chmod(sNewFileName, iMode); return true; }