Changeset

5153:688aeac0012a

mod_storage_internal, datamanager: Add support for iterating over users with data in a store
author Kim Alvefur <zash@zash.se>
date Fri, 21 Sep 2012 17:23:08 +0200
parents 5152:fee5f8d4ec74
children 5154:429c8eeea035
files plugins/mod_storage_internal.lua util/datamanager.lua
diffstat 2 files changed, 25 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_storage_internal.lua	Wed Sep 19 23:29:25 2012 +0200
+++ b/plugins/mod_storage_internal.lua	Fri Sep 21 17:23:08 2012 +0200
@@ -5,8 +5,8 @@
 local driver = {};
 local driver_mt = { __index = driver };
 
-function driver:open(store)
-	return setmetatable({ store = store }, driver_mt);
+function driver:open(store, typ)
+	return setmetatable({ store = store, type = typ }, driver_mt);
 end
 function driver:get(user)
 	return datamanager.load(user, host, self.store);
@@ -20,6 +20,10 @@
 	return datamanager.stores(username, host);
 end
 
+function driver:users()
+	return datamanager.users(host, self.store, self.type);
+end
+
 function driver:purge(user)
 	return datamanager.purge(user, host);
 end
--- a/util/datamanager.lua	Wed Sep 19 23:29:25 2012 +0200
+++ b/util/datamanager.lua	Fri Sep 21 17:23:08 2012 +0200
@@ -282,6 +282,25 @@
 	list = "list";
 }
 
+function users(host, store, typ)
+	typ = type_map[typ or "keyval"];
+	local store_dir = format("%s/%s/%s", data_path, encode(host), encode(store));
+
+	local mode, err = lfs.attributes(store_dir, "mode");
+	if not mode then
+		return function() log("debug", err or (store_dir .. " does not exist")) end
+	end
+	local next, state = lfs.dir(store_dir);
+	return function(state)
+		for node in next, state do
+			local file, ext = node:match("^(.*)%.([dalist]+)$");
+			if file and ext == typ then
+				return decode(file);
+			end
+		end
+	end, state;
+end
+
 function stores(username, host, typ)
 	typ = type_map[typ or "keyval"];
 	local store_dir = format("%s/%s/", data_path, encode(host));