Software /
code /
prosody
Comparison
util/datamanager.lua @ 5130:051d352ed03c
storagemanager, datamanager, mod_storage_{internal,sql}: Replace list_stores() with an iterator version
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 17 Sep 2012 05:42:10 +0200 |
parent | 5118:0dc9e6c128c3 |
child | 5153:688aeac0012a |
comparison
equal
deleted
inserted
replaced
5129:e8253c931166 | 5130:051d352ed03c |
---|---|
275 return nil, "Error reading storage"; | 275 return nil, "Error reading storage"; |
276 end | 276 end |
277 return items; | 277 return items; |
278 end | 278 end |
279 | 279 |
280 function list_stores(username, host) | 280 local type_map = { |
281 if not host then | 281 keyval = "dat"; |
282 return nil, "bad argument #2 to 'list_stores' (string expected, got nothing)"; | 282 list = "list"; |
283 end | 283 } |
284 local list = {}; | 284 |
285 local host_dir = format("%s/%s/", data_path, encode(host)); | 285 function stores(username, host, typ) |
286 for node in lfs.dir(host_dir) do | 286 typ = type_map[typ or "keyval"]; |
287 if not node:match"^%." then -- dots should be encoded, this is probably . or .. | 287 local store_dir = format("%s/%s/", data_path, encode(host)); |
288 local store = decode(node); | 288 |
289 local path = host_dir..node; | 289 local mode, err = lfs.attributes(store_dir, "mode"); |
290 if username == true then | 290 if not mode then |
291 if lfs.attributes(path, "mode") == "directory" then | 291 return function() log("debug", err or (store_dir .. " does not exist")) end |
292 list[#list+1] = store; | 292 end |
293 local next, state = lfs.dir(store_dir); | |
294 return function(state) | |
295 for node in next, state do | |
296 if not node:match"^%." then | |
297 if username == true then | |
298 if lfs.attributes(store_dir..node, "mode") == "directory" then | |
299 return decode(node); | |
300 end | |
301 elseif username then | |
302 local store = decode(node) | |
303 if lfs.attributes(getpath(username, host, store, typ), "mode") then | |
304 return store; | |
305 end | |
306 elseif lfs.attributes(node, "mode") == "file" then | |
307 local file, ext = node:match("^(.*)%.([dalist]+)$"); | |
308 if ext == typ then | |
309 return decode(file) | |
310 end | |
293 end | 311 end |
294 elseif username then | |
295 if lfs.attributes(getpath(username, host, store), "mode") | |
296 or lfs.attributes(getpath(username, host, store, "list"), "mode") then | |
297 list[#list+1] = store; | |
298 end | |
299 elseif lfs.attributes(path, "mode") == "file" then | |
300 list[#list+1] = store:gsub("%.[dalist]+$",""); | |
301 end | 312 end |
302 end | 313 end |
303 end | 314 end, state; |
304 return list; | |
305 end | 315 end |
306 | 316 |
307 local function do_remove(path) | 317 local function do_remove(path) |
308 local ok, err = os_remove(path); | 318 local ok, err = os_remove(path); |
309 if not ok and lfs.attributes(path, "mode") then | 319 if not ok and lfs.attributes(path, "mode") then |