aboutsummaryrefslogtreecommitdiff
path: root/advtrains/nodedb.lua
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2020-10-07 14:41:57 +0200
committerorwell96 <orwell@bleipb.de>2020-10-07 14:41:57 +0200
commitb892d793ac22a00ee8708b4697d83c0d0ca06743 (patch)
treec03c9e2f82f33d76809d69b07205c2f4fa53b75b /advtrains/nodedb.lua
parent1a743c2dd67df10e4e15191b2aeb8532158b28ad (diff)
downloadadvtrains-b892d793ac22a00ee8708b4697d83c0d0ca06743.tar.gz
advtrains-b892d793ac22a00ee8708b4697d83c0d0ca06743.tar.bz2
advtrains-b892d793ac22a00ee8708b4697d83c0d0ca06743.zip
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.
Diffstat (limited to 'advtrains/nodedb.lua')
-rw-r--r--advtrains/nodedb.lua27
1 files 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