mirror of
https://github.com/whyour/qinglong.git
synced 2025-11-09 16:16:07 +08:00
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:
parent
62831835a5
commit
df7f13c6bf
|
|
@ -19,11 +19,13 @@ export async function writeFileWithLock(
|
||||||
if (typeof options === 'string') {
|
if (typeof options === 'string') {
|
||||||
options = { encoding: options };
|
options = { encoding: options };
|
||||||
}
|
}
|
||||||
|
let isNewFile = false;
|
||||||
if (!(await fileExist(filePath))) {
|
if (!(await fileExist(filePath))) {
|
||||||
// Create the file with the specified mode if provided, otherwise use default
|
// Create the file with the specified mode if provided, otherwise use default
|
||||||
const fileMode = options?.mode || 0o666;
|
const fileMode = options?.mode || 0o666;
|
||||||
const fileHandle = await open(filePath, 'w', fileMode);
|
const fileHandle = await open(filePath, 'w', fileMode);
|
||||||
await fileHandle.close();
|
await fileHandle.close();
|
||||||
|
isNewFile = true;
|
||||||
}
|
}
|
||||||
const lockfilePath = getUniqueLockPath(filePath);
|
const lockfilePath = getUniqueLockPath(filePath);
|
||||||
|
|
||||||
|
|
@ -37,8 +39,8 @@ export async function writeFileWithLock(
|
||||||
lockfilePath,
|
lockfilePath,
|
||||||
});
|
});
|
||||||
await writeFile(filePath, content, { encoding: 'utf8', ...options });
|
await writeFile(filePath, content, { encoding: 'utf8', ...options });
|
||||||
// Ensure the mode is set correctly even if the file already existed
|
// Only chmod if the file already existed (not just created with the correct mode)
|
||||||
if (options?.mode) {
|
if (!isNewFile && options?.mode) {
|
||||||
await chmod(filePath, options.mode);
|
await chmod(filePath, options.mode);
|
||||||
}
|
}
|
||||||
await release();
|
await release();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user