mirror of
https://github.com/Jeuners/ECC.git
synced 2026-09-09 15:02:30 +02:00
fix: make saveAliases atomic on Unix by skipping unnecessary unlink before rename
On Unix/macOS, rename(2) atomically replaces the destination file. The previous code ran unlinkSync before renameSync on all platforms, creating an unnecessary non-atomic window where a crash could lose data. Now the delete-before-rename is gated behind process.platform === 'win32', where rename cannot overwrite an existing file.
This commit is contained in:
parent
0daa5cb070
commit
dc11fc2fd8
2 changed files with 29 additions and 2 deletions
|
|
@ -110,8 +110,10 @@ function saveAliases(aliases) {
|
|||
// Atomic write: write to temp file, then rename
|
||||
fs.writeFileSync(tempPath, content, 'utf8');
|
||||
|
||||
// On Windows, we need to delete the target file before renaming
|
||||
if (fs.existsSync(aliasesPath)) {
|
||||
// On Windows, rename fails with EEXIST if destination exists, so delete first.
|
||||
// On Unix/macOS, rename(2) atomically replaces the destination — skip the
|
||||
// delete to avoid an unnecessary non-atomic window between unlink and rename.
|
||||
if (process.platform === 'win32' && fs.existsSync(aliasesPath)) {
|
||||
fs.unlinkSync(aliasesPath);
|
||||
}
|
||||
fs.renameSync(tempPath, aliasesPath);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue