From 01bbeced65b82f5041462cd7a6c9016274acade4 Mon Sep 17 00:00:00 2001 From: Chris Bannister Date: Wed, 27 Nov 2024 09:42:30 +0000 Subject: [PATCH] fix(server/settings): write settings to a temp file then move to avoid corruption (#1067) When writing the settings.json file ensure that the file is fully written by writing it to temporary file before renaming it to the final settings path. This should prevent issues where the config gets lost due to the file being corrupted. --- server/lib/settings/index.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/lib/settings/index.ts b/server/lib/settings/index.ts index 50933034..5f13b650 100644 --- a/server/lib/settings/index.ts +++ b/server/lib/settings/index.ts @@ -688,10 +688,9 @@ class Settings { } public async save(): Promise { - await fs.writeFile( - SETTINGS_PATH, - JSON.stringify(this.data, undefined, ' ') - ); + const tmp = SETTINGS_PATH + '.tmp'; + await fs.writeFile(tmp, JSON.stringify(this.data, undefined, ' ')); + await fs.rename(tmp, SETTINGS_PATH); } }