Comparison

util/datamanager.lua @ 7925:209503ee3aaa

util.datamanager: Ignore ENOENT (no such file) when loading data
author Kim Alvefur <zash@zash.se>
date Wed, 01 Mar 2017 16:44:59 +0100
parent 7734:6a52415ed68a
child 7927:7132abcf669e
comparison
equal deleted inserted replaced
7924:8487fe9fc335 7925:209503ee3aaa
37 return ok, msg; 37 return ok, msg;
38 end 38 end
39 f:seek("set", offset); 39 f:seek("set", offset);
40 return true; 40 return true;
41 end; 41 end;
42 local ENOENT = 2;
42 pcall(function() 43 pcall(function()
43 local pposix = require "util.pposix"; 44 local pposix = require "util.pposix";
44 raw_mkdir = pposix.mkdir or raw_mkdir; -- Doesn't trample on umask 45 raw_mkdir = pposix.mkdir or raw_mkdir; -- Doesn't trample on umask
45 fallocate = pposix.fallocate or fallocate; 46 fallocate = pposix.fallocate or fallocate;
46 end); 47 end);
120 return format("%s/%s/%s.%s", data_path, host, datastore, ext); 121 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
121 end 122 end
122 end 123 end
123 124
124 local function load(username, host, datastore) 125 local function load(username, host, datastore)
125 local data, err = envloadfile(getpath(username, host, datastore), {}); 126 local data, err, errno = envloadfile(getpath(username, host, datastore), {});
126 if not data then 127 if not data then
128 if errno == ENOENT then
129 -- No such file, ok to ignore
130 return nil;
131 end
127 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); 132 local mode = lfs.attributes(getpath(username, host, datastore), "mode");
128 if not mode then 133 if not mode then
129 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil"); 134 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil");
130 return nil; 135 return nil;
131 else -- file exists, but can't be read 136 else -- file exists, but can't be read
143 return ret; 148 return ret;
144 end 149 end
145 150
146 local function atomic_store(filename, data) 151 local function atomic_store(filename, data)
147 local scratch = filename.."~"; 152 local scratch = filename.."~";
148 local f, ok, msg; 153 local f, ok, msg, errno;
149 154
150 f, msg = io_open(scratch, "w"); 155 f, msg, errno = io_open(scratch, "w");
151 if not f then 156 if not f then
152 return nil, msg; 157 return nil, msg;
153 end 158 end
154 159
155 ok, msg = f:write(data); 160 ok, msg = f:write(data);
293 return true; 298 return true;
294 end 299 end
295 300
296 local function list_load(username, host, datastore) 301 local function list_load(username, host, datastore)
297 local items = {}; 302 local items = {};
298 local data, err = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); 303 local data, err, errno = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end});
299 if not data then 304 if not data then
305 if errno == ENOENT then
306 -- No such file, ok to ignore
307 return nil;
308 end
300 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); 309 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode");
301 if not mode then 310 if not mode then
302 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil"); 311 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil");
303 return nil; 312 return nil;
304 else -- file exists, but can't be read 313 else -- file exists, but can't be read