From ffa65afbb22da45ff972abe59cebaaeec6c7f730 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sun, 26 Aug 2018 10:49:53 +0100 Subject: [PATCH] Catch readonly write errors more cleanly Fail more cleanly when run from a readonly files system without setting readOnly true. --- red.js | 11 +++++++++-- red/runtime/locales/en-US/runtime.json | 1 + red/runtime/storage/localfilesystem/util.js | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/red.js b/red.js index c7dc8b131..9b86357f1 100755 --- a/red.js +++ b/red.js @@ -101,8 +101,15 @@ if (parsedArgs.settings) { var settingsStat = fs.statSync(defaultSettings); if (settingsStat.mtime.getTime() <= settingsStat.ctime.getTime()) { // Default settings file has not been modified - safe to copy - fs.copySync(defaultSettings,userSettingsFile); - settingsFile = userSettingsFile; + try { + fs.copySync(defaultSettings,userSettingsFile); + settingsFile = userSettingsFile; + } + catch (err) { + console.log("Can't copy settings file. Is file system Read Only ?"); + console.log("You may want to set readOnly: true, in settings.js"); + process.exit(1); + } } else { // Use default settings.js as it has been modified settingsFile = defaultSettings; diff --git a/red/runtime/locales/en-US/runtime.json b/red/runtime/locales/en-US/runtime.json index a38f44d08..d5967c98b 100644 --- a/red/runtime/locales/en-US/runtime.json +++ b/red/runtime/locales/en-US/runtime.json @@ -142,6 +142,7 @@ "restore": "Restoring __type__ file backup : __path__", "restore-fail": "Restoring __type__ file backup failed : __message__", "fsync-fail": "Flushing file __path__ to disk failed : __message__", + "fwrite-fail": "Writing backup file __path__ to disk failed.", "projects": { "changing-project": "Setting active project : __project__", "active-project": "Active project : __project__", diff --git a/red/runtime/storage/localfilesystem/util.js b/red/runtime/storage/localfilesystem/util.js index d20c46622..342b4f950 100644 --- a/red/runtime/storage/localfilesystem/util.js +++ b/red/runtime/storage/localfilesystem/util.js @@ -81,7 +81,11 @@ module.exports = { writeFile: function(path,content,backupPath) { if (backupPath) { if (fs.existsSync(path)) { - fs.renameSync(path,backupPath); + try { + fs.renameSync(path,backupPath); + } catch(e) { + log.warn(log._("storage.localfilesystem.fwrite-fail",{path:path})); + } } } return when.promise(function(resolve,reject) {