From b892d793ac22a00ee8708b4697d83c0d0ca06743 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 7 Oct 2020 14:41:57 +0200 Subject: Add windows compatibility in nodedb saving (H#153) Note: it does not simply add os.delete() but reverts to the "old" behavior of directly overwriting the file, because this did work before. --- advtrains/nodedb.lua | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/advtrains/nodedb.lua b/advtrains/nodedb.lua index 01bf5a4..03a5a2d 100644 --- a/advtrains/nodedb.lua +++ b/advtrains/nodedb.lua @@ -94,10 +94,19 @@ function ndb.load_data(data) ndb_ver = 1 end +local windows_compat = false --save function ndb.save_data() local tmppath = path.."~" - local file, err = io.open(tmppath, "wb") + local file, err + if windows_compat then + -- open ndb file directly + file, err = io.open(path, "wb") + else + -- open another file next to it, then replace atomically + file, err = io.open(tmppath, "wb") + end + if not file then atwarn("Couldn't save the node database: ", err or "Unknown Error") else @@ -113,7 +122,21 @@ function ndb.save_data() end file:close() end - os.rename(tmppath, path) + + if not windows_compat then + local success, msg = os.rename(tmppath, path) + --local success, msg = nil, "test" + -- for windows, this fails if the file already exists. Enable windows compatibility and directly write to path. + if not success then + atlog("Replacing the nodedb file atomically failed:",msg) + atlog("Switching to Windows mode (will directly overwrite the nodedb file from now on)") + windows_compat = true + os.remove(tmppath) + -- try again + ndb.save_data() + end + end + return {nodeids = ndb_nodeids, ver = ndb_ver} end -- cgit v1.2.3