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>
This commit is contained in:
copilot-swe-agent[bot] 2025-11-07 16:20:30 +00:00
parent 62831835a5
commit df7f13c6bf

View File

@ -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();