From df7f13c6bf12536568b07a18af69bf20f6130954 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 16:20:30 +0000 Subject: [PATCH] Optimize writeFileWithLock to avoid redundant chmod calls - Track if file is newly created to skip redundant chmod - Only call chmod for existing files that need permission changes - Improves performance and reduces unnecessary system calls Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> --- back/shared/utils.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/back/shared/utils.ts b/back/shared/utils.ts index fd8cdf6d..cbb6f747 100644 --- a/back/shared/utils.ts +++ b/back/shared/utils.ts @@ -19,11 +19,13 @@ export async function writeFileWithLock( if (typeof options === 'string') { options = { encoding: options }; } + let isNewFile = false; if (!(await fileExist(filePath))) { // Create the file with the specified mode if provided, otherwise use default const fileMode = options?.mode || 0o666; const fileHandle = await open(filePath, 'w', fileMode); await fileHandle.close(); + isNewFile = true; } const lockfilePath = getUniqueLockPath(filePath); @@ -37,8 +39,8 @@ export async function writeFileWithLock( lockfilePath, }); await writeFile(filePath, content, { encoding: 'utf8', ...options }); - // Ensure the mode is set correctly even if the file already existed - if (options?.mode) { + // Only chmod if the file already existed (not just created with the correct mode) + if (!isNewFile && options?.mode) { await chmod(filePath, options.mode); } await release();