diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/datamanager.lua | 24 | ||||
-rw-r--r-- | util/envload.lua | 14 |
2 files changed, 27 insertions, 11 deletions
diff --git a/util/datamanager.lua b/util/datamanager.lua index 585bb83d..2b47c3c4 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -39,10 +39,12 @@ local function fallocate(f, offset, len) f:seek("set", offset); return true; end; +local ENOENT = 2; pcall(function() local pposix = require "util.pposix"; raw_mkdir = pposix.mkdir or raw_mkdir; -- Doesn't trample on umask fallocate = pposix.fallocate or fallocate; + ENOENT = pposix.ENOENT or ENOENT; end); local _ENV = nil; @@ -122,8 +124,12 @@ local function getpath(username, host, datastore, ext, create) end local function load(username, host, datastore) - local data, err = envloadfile(getpath(username, host, datastore), {}); + local data, err, errno = envloadfile(getpath(username, host, datastore), {}); if not data then + if errno == ENOENT then + -- No such file, ok to ignore + return nil; + end local mode = lfs.attributes(getpath(username, host, datastore), "mode"); if not mode then log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil"); @@ -145,9 +151,9 @@ end local function atomic_store(filename, data) local scratch = filename.."~"; - local f, ok, msg; + local f, ok, msg, errno; - f, msg = io_open(scratch, "w"); + f, msg, errno = io_open(scratch, "w"); if not f then return nil, msg; end @@ -222,11 +228,8 @@ local function append(username, host, datastore, ext, data) local ok; local f, msg = io_open(filename, "r+"); if not f then + return atomic_store(filename, data); -- File did probably not exist, let's create it - f, msg = io_open(filename, "w"); - if not f then - return nil, msg, "open"; - end end local pos = f:seek("end"); @@ -295,8 +298,12 @@ end local function list_load(username, host, datastore) local items = {}; - local data, err = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); + local data, err, errno = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); if not data then + if errno == ENOENT then + -- No such file, ok to ignore + return nil; + end local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); if not mode then log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil"); @@ -408,6 +415,7 @@ return { load = load; store = store; append_raw = append; + store_raw = atomic_store; list_append = list_append; list_store = list_store; list_load = list_load; diff --git a/util/envload.lua b/util/envload.lua index 3c5190df..926f20c0 100644 --- a/util/envload.lua +++ b/util/envload.lua @@ -6,7 +6,8 @@ -- -- luacheck: ignore 113/setfenv -local load, loadstring, loadfile, setfenv = load, loadstring, loadfile, setfenv; +local load, loadstring, setfenv = load, loadstring, setfenv; +local io_open = io.open; local envload; local envloadfile; @@ -18,7 +19,10 @@ if setfenv then end function envloadfile(file, env) - local f, err = loadfile(file); + local fh, err, errno = io_open(file); + if not fh then return fh, err, errno; end + local f, err = load(function () return fh:read(2048); end, "@"..file); + fh:close(); if f and env then setfenv(f, env); end return f, err; end @@ -28,7 +32,11 @@ else end function envloadfile(file, env) - return loadfile(file, nil, env); + local fh, err, errno = io_open(file); + if not fh then return fh, err, errno; end + local f, err = load(fh:lines(2048), "@"..file, nil, env); + fh:close(); + return f, err; end end |