From 2845475e3f47a0c8ba43a511bc4eb41481a02139 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 20 Mar 2018 00:04:36 +0000 Subject: [PATCH] Keep backup of .config.json --- .../storage/localfilesystem/projects/index.js | 13 ++----------- red/runtime/storage/localfilesystem/settings.js | 4 +++- red/runtime/storage/localfilesystem/util.js | 11 ++++++++++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/red/runtime/storage/localfilesystem/projects/index.js b/red/runtime/storage/localfilesystem/projects/index.js index 781c8434b..7c24845ab 100644 --- a/red/runtime/storage/localfilesystem/projects/index.js +++ b/red/runtime/storage/localfilesystem/projects/index.js @@ -514,11 +514,6 @@ function saveFlows(flows) { } flowsFileExists = true; - - try { - fs.renameSync(flowsFullPath,flowsFileBackup); - } catch(err) { - } var flowData; @@ -527,7 +522,7 @@ function saveFlows(flows) { } else { flowData = JSON.stringify(flows); } - return util.writeFile(flowsFullPath, flowData); + return util.writeFile(flowsFullPath, flowData, flowsFileBackup); } function getCredentials() { @@ -539,17 +534,13 @@ function saveCredentials(credentials) { return when.resolve(); } - try { - fs.renameSync(credentialsFile,credentialsFileBackup); - } catch(err) { - } var credentialData; if (settings.flowFilePretty) { credentialData = JSON.stringify(credentials,null,4); } else { credentialData = JSON.stringify(credentials); } - return util.writeFile(credentialsFile, credentialData); + return util.writeFile(credentialsFile, credentialData, credentialsFileBackup); } function getFlowFilename() { diff --git a/red/runtime/storage/localfilesystem/settings.js b/red/runtime/storage/localfilesystem/settings.js index 7f3fc22ff..17ece58d7 100644 --- a/red/runtime/storage/localfilesystem/settings.js +++ b/red/runtime/storage/localfilesystem/settings.js @@ -22,12 +22,14 @@ var log = require("../../log"); var util = require("./util"); var globalSettingsFile; +var globalSettingsBackup; var settings; module.exports = { init: function(_settings) { settings = _settings; globalSettingsFile = fspath.join(settings.userDir,".config.json"); + globalSettingsBackup = fspath.join(settings.userDir,".config.json.backup"); }, getSettings: function() { return when.promise(function(resolve,reject) { @@ -47,6 +49,6 @@ module.exports = { if (settings.readOnly) { return when.resolve(); } - return util.writeFile(globalSettingsFile,JSON.stringify(newSettings,null,1)); + return util.writeFile(globalSettingsFile,JSON.stringify(newSettings,null,1),globalSettingsBackup); } } diff --git a/red/runtime/storage/localfilesystem/util.js b/red/runtime/storage/localfilesystem/util.js index 08f821df8..6fb75d255 100644 --- a/red/runtime/storage/localfilesystem/util.js +++ b/red/runtime/storage/localfilesystem/util.js @@ -78,7 +78,16 @@ module.exports = { * This forces a fsync before completing to ensure * the write hits disk. */ - writeFile: function(path,content) { + writeFile: function(path,content,backupPath) { + if (backupPath) { + try { + console.log(path); + console.log(backupPath); + 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) {