From c60abdc3494e494c44c6a2a489e9583ac634577e Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 17 Feb 2023 23:41:50 +0100 Subject: [PATCH] Call fsync on patching saves Similar to 1b2a20c call fsync on the newly written patched save before renaming it to prevent data loss in the event of a system crash. --- packages/lib/factorio/patch.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/lib/factorio/patch.js b/packages/lib/factorio/patch.js index 7f92232f..8e0c6a0f 100644 --- a/packages/lib/factorio/patch.js +++ b/packages/lib/factorio/patch.js @@ -251,8 +251,14 @@ async function patch(savePath, modules) { // Write back the save let tempSavePath = `${savePath}.tmp`; let stream = zip.generateNodeStream({ compression: "DEFLATE" }); - let pipe = stream.pipe(fs.createWriteStream(tempSavePath)); - await events.once(pipe, "finish"); + let fd = await fs.open(tempSavePath, "w"); + try { + let pipe = stream.pipe(fs.createWriteStream("", { fd, autoClose: false })); + await events.once(pipe, "finish"); + await fs.fsync(fd); + } finally { + await fs.close(fd); + } await fs.rename(tempSavePath, savePath); }