Comparison

util/datamanager.lua @ 7673:177d569307fd

util.datamanager: Rename variables to avoid name clashes [luacheck]
author Kim Alvefur <zash@zash.se>
date Sat, 24 Sep 2016 18:31:00 +0200
parent 7432:92f721226753
child 7674:8027eecc750f
comparison
equal deleted inserted replaced
7672:27f5db07bec9 7673:177d569307fd
120 return format("%s/%s/%s.%s", data_path, host, datastore, ext); 120 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
121 end 121 end
122 end 122 end
123 123
124 local function load(username, host, datastore) 124 local function load(username, host, datastore)
125 local data, ret = envloadfile(getpath(username, host, datastore), {}); 125 local data, err = envloadfile(getpath(username, host, datastore), {});
126 if not data then 126 if not data then
127 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); 127 local mode = lfs.attributes(getpath(username, host, datastore), "mode");
128 if not mode then 128 if not mode then
129 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); 129 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil");
130 return nil; 130 return nil;
131 else -- file exists, but can't be read 131 else -- file exists, but can't be read
132 -- TODO more detailed error checking and logging? 132 -- TODO more detailed error checking and logging?
133 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); 133 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil");
134 return nil, "Error reading storage"; 134 return nil, "Error reading storage";
135 end 135 end
136 end 136 end
137 137
138 local success, ret = pcall(data); 138 local success, ret = pcall(data);
293 return true; 293 return true;
294 end 294 end
295 295
296 local function list_load(username, host, datastore) 296 local function list_load(username, host, datastore)
297 local items = {}; 297 local items = {};
298 local data, ret = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); 298 local data, err = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end});
299 if not data then 299 if not data then
300 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); 300 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode");
301 if not mode then 301 if not mode then
302 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); 302 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil");
303 return nil; 303 return nil;
304 else -- file exists, but can't be read 304 else -- file exists, but can't be read
305 -- TODO more detailed error checking and logging? 305 -- TODO more detailed error checking and logging?
306 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); 306 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, err, username or "nil", host or "nil");
307 return nil, "Error reading storage"; 307 return nil, "Error reading storage";
308 end 308 end
309 end 309 end
310 310
311 local success, ret = pcall(data); 311 local success, ret = pcall(data);
355 if username == true then 355 if username == true then
356 if lfs.attributes(store_dir..node, "mode") == "directory" then 356 if lfs.attributes(store_dir..node, "mode") == "directory" then
357 return decode(node); 357 return decode(node);
358 end 358 end
359 elseif username then 359 elseif username then
360 local store = decode(node) 360 local store_name = decode(node);
361 if lfs.attributes(getpath(username, host, store, typ), "mode") then 361 if lfs.attributes(getpath(username, host, store_name, typ), "mode") then
362 return store; 362 return store_name;
363 end 363 end
364 elseif lfs.attributes(node, "mode") == "file" then 364 elseif lfs.attributes(node, "mode") == "file" then
365 local file, ext = node:match("^(.*)%.([dalist]+)$"); 365 local file, ext = node:match("^(.*)%.([dalist]+)$");
366 if ext == typ then 366 if ext == typ then
367 return decode(file) 367 return decode(file)
387 return ok, iter; 387 return ok, iter;
388 end 388 end
389 local errs = {}; 389 local errs = {};
390 for file in iter, state, var do 390 for file in iter, state, var do
391 if lfs.attributes(host_dir..file, "mode") == "directory" then 391 if lfs.attributes(host_dir..file, "mode") == "directory" then
392 local store = decode(file); 392 local store_name = decode(file);
393 local ok, err = do_remove(getpath(username, host, store)); 393 local ok, err = do_remove(getpath(username, host, store_name));
394 if not ok then errs[#errs+1] = err; end 394 if not ok then errs[#errs+1] = err; end
395 395
396 local ok, err = do_remove(getpath(username, host, store, "list")); 396 local ok, err = do_remove(getpath(username, host, store_name, "list"));
397 if not ok then errs[#errs+1] = err; end 397 if not ok then errs[#errs+1] = err; end
398 end 398 end
399 end 399 end
400 return #errs == 0, t_concat(errs, ", "); 400 return #errs == 0, t_concat(errs, ", ");
401 end 401 end