From 6fa0d671c0db371ccbd280d6c30ff0dee69e1918 Mon Sep 17 00:00:00 2001 From: Nathan Allen Date: Thu, 3 May 2018 04:40:51 -0400 Subject: [PATCH] Fix ENOENT error on first start when no user dir (#1711) * Fix ENOENT error on first start when no user dir Write backup using `copySync` and move it below the `fsync` to ensure file is present when backup is made. * Check for path to exist before attempting backup --- red/runtime/storage/localfilesystem/util.js | 42 ++++++++++----------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/red/runtime/storage/localfilesystem/util.js b/red/runtime/storage/localfilesystem/util.js index f677bdd38..d20c46622 100644 --- a/red/runtime/storage/localfilesystem/util.js +++ b/red/runtime/storage/localfilesystem/util.js @@ -80,28 +80,26 @@ module.exports = { */ writeFile: function(path,content,backupPath) { if (backupPath) { - try { - fs.renameSync(path,backupPath); - } catch(err) { - console.log(err); - } - } - return when.promise(function(resolve,reject) { - var stream = fs.createWriteStream(path); - stream.on('open',function(fd) { - 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) { - reject(err); - }); - }); + if (fs.existsSync(path)) { + fs.renameSync(path,backupPath); + } + } + return when.promise(function(resolve,reject) { + var stream = fs.createWriteStream(path); + stream.on('open',function(fd) { + 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) { + reject(err); + }); + }); }, readFile: readFile,