From 2dc35092fe78c77373d6fa5c24b9b30e3528518f Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows 11)" <rob@synchro.net>
Date: Fri, 22 Sep 2023 11:53:42 -0700
Subject: [PATCH] Fix "error 13 opening" files when using init-fidonet.js

I finally reproduced this issue myself on a fresh install on Windows.

We needed explicit file closing here as going out of scope doesn't immediately
finalize (and close) a File object. That may not happen until garbage
collection and thus prevent subsequent re-opens of the same files, so always
close() your files.

init-fidonet.js calls install-binkit.js which uses cfglib.js, so that was
a source of these errors too.
---
 exec/init-fidonet.js | 4 ++++
 exec/load/cfglib.js  | 4 ++++
 2 files changed, 8 insertions(+)

diff --git a/exec/init-fidonet.js b/exec/init-fidonet.js
index 28f2c380ba..9969b5ba29 100644
--- a/exec/init-fidonet.js
+++ b/exec/init-fidonet.js
@@ -251,6 +251,7 @@ function get_domain(zone)
 				continue;
 			if(typeof zones == 'number') {
 				if(zone == zones) {
+					file.close();
 					return netname;
 				}
 				continue;
@@ -258,6 +259,7 @@ function get_domain(zone)
 			zones = zones.split(',');
 			for(var j = 0; j < zones.length; j++) {
 				if(zone == zones[j]) {
+					file.close();
 					return netname;
 				}
 			}
@@ -265,6 +267,7 @@ function get_domain(zone)
 		file.close();
 		return result;
 	}
+	file.close();
 	return "";
 }
 
@@ -638,6 +641,7 @@ if(!msgs_ini.iniGetObject("grp:" + netname)
 			? (netname.toUpperCase() + "_") : network.areatag_prefix
 	});
 }
+msgs_ini.close();
 
 /*********************/
 /* DOWNLOAD ECHOLIST */
diff --git a/exec/load/cfglib.js b/exec/load/cfglib.js
index 08c42c8ccd..dd4c97cf2f 100644
--- a/exec/load/cfglib.js
+++ b/exec/load/cfglib.js
@@ -19,6 +19,7 @@ function read_main_ini(filename)
 	obj.mqtt = f.iniGetObject("MQTT");
 	obj.module = f.iniGetObject("module");
 	obj.shell = read_sections(f, "shell:");
+	f.close();
 	return obj;
 }
 
@@ -33,6 +34,7 @@ function read_file_ini(filename)
 	obj.viewer = read_sections(f, "viewer:");
 	obj.tester = read_sections(f, "tester:");
 	obj.protocol = read_sections(f, "protocol:");
+	f.close();
 	return obj;
 }
 
@@ -69,6 +71,7 @@ function read_xtrn_ini(filename)
 			item.code = item.code.substring(i + 1);
 		}
 	}
+	f.close();
 	return obj;
 }
 
@@ -101,6 +104,7 @@ function write_xtrn_ini(filename, obj)
 	write_sections(f, obj.sec, "sec:");
 	write_sections(f, obj.prog, "prog:");
 	write_sections(f, obj.native, "native:");
+	f.close();
 	return true;
 }
 
-- 
GitLab