From da708adcbd855f9ab74312297beacc546bb31c89 Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Mon, 6 Nov 2017 10:19:13 -0800 Subject: [PATCH] Call fsync() before closing file --- red/runtime/locales/en-US/runtime.json | 3 ++- red/runtime/storage/localfilesystem.js | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/red/runtime/locales/en-US/runtime.json b/red/runtime/locales/en-US/runtime.json index 37c82dc14..168e461ba 100644 --- a/red/runtime/locales/en-US/runtime.json +++ b/red/runtime/locales/en-US/runtime.json @@ -137,7 +137,8 @@ "empty": "Existing __type__ file is empty", "invalid": "Existing __type__ file is not valid json", "restore": "Restoring __type__ file backup : __path__", - "restore-fail": "Restoring __type__ file backup failed : __message__" + "restore-fail": "Restoring __type__ file backup failed : __message__", + "fsync-fail": "Flushing file __path__ to disk failed : __message__" } } } diff --git a/red/runtime/storage/localfilesystem.js b/red/runtime/storage/localfilesystem.js index 32c0415f8..ecb5b55ab 100644 --- a/red/runtime/storage/localfilesystem.js +++ b/red/runtime/storage/localfilesystem.js @@ -114,8 +114,13 @@ function writeFile(path,content) { return when.promise(function(resolve,reject) { var stream = fs.createWriteStream(path); stream.on('open',function(fd) { - stream.end(content,'utf8',function() { - fs.fsync(fd,resolve); + stream.write(content,'utf8',function() { + fs.fsync(fd,function(err) { + if (err) { + log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()})); + } + stream.end(resolve); + }); }); }); stream.on('error',function(err) {