From 41e1bfb822ebfc4ef41ef7e141e3a053b1bd53c6 Mon Sep 17 00:00:00 2001
From: Rob Swindell <rob@synchro.net>
Date: Wed, 11 Nov 2020 11:20:03 -0800
Subject: [PATCH] Resilience around tickit.ini and freqit.ini file open
 operations.

Throw exceptions rather than just loggin an "Unable to open" error
(easier tracking fo the soruce of the error). Log the description and
errno value associated with the file open failure.

Create the .ini files if they don't already exist (rather than crashing
out with an error).

Replace the deprecated 'e' file open mode flag with 'x'.
---
 exec/load/fidocfg.js | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/exec/load/fidocfg.js b/exec/load/fidocfg.js
index 706b1c2dc1..8e74532d68 100644
--- a/exec/load/fidocfg.js
+++ b/exec/load/fidocfg.js
@@ -65,8 +65,8 @@ function TickITCfg() {
 		}
 	}
 
-	if (!tcfg.open("r"))
-		throw new Error("Unable to open '"+tcfg.name+"'");
+	if (!tcfg.open(tcfg.exists ? "r" : "w+"))
+		throw new Error(tcfg.error + " (" + strerror(tcfg.error) + ") opening '" + tcfg.name + "'");
 	this.gcfg = tcfg.iniGetObject();
 	lcprops(this.gcfg);
 	if (this.gcfg.handler !== undefined) {
@@ -147,9 +147,8 @@ TickITCfg.prototype.get_next_tic_filename = function()
 		val = f.readBin();
 	}
 	else {
-		if (!f.open("web+")) {
-			log(LOG_ERROR, "Unable to open file "+f.name+"!");
-			return undefined;
+		if (!f.open("wxb+")) {
+			throw new Error(f.error + " (" + strerror(f.error) + ") opening '" + f.name + "'");
 		}
 		val = -1;
 	}
@@ -233,7 +232,7 @@ TickITCfg.prototype.save = function()
 	}
 
 	if (!tcfg.open(tcfg.exists ? 'r+':'w+'))
-		return "Unable to open '"+tcfg.name+"'";
+		throw new Error(tcfg.error + " (" + strerror(tcfg.error) + ") opening '" + tcfg.name + "'");
 
 	writesect(null, this.gcfg);
 	sects = tcfg.iniGetSections().map(function(v){return v.toLowerCase();});
@@ -269,9 +268,8 @@ function FREQITCfg()
 	var key;
 	var sects;
 
-	if (!f.open('r')) {
-		log(LOG_ERROR, "Unable to open '"+f.name+"'");
-		return;
+	if (!f.open(f.exists ? 'r' : 'w+')) {
+		throw new Error(f.error + " (" + strerror(f.error) + ") opening '" + f.name + "'");
 	}
 	// TODO: Support requiring passwords for specific files/dirs
 	this.dirs = [];
-- 
GitLab