2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local format = string.format;
11 local setmetatable, type = setmetatable, type;
12 local pairs, ipairs = pairs, ipairs;
13 local char = string.char;
15 local log = require "util.logger".init("datamanager");
16 local io_open = io.open;
17 local os_remove = os.remove;
18 local tostring, tonumber = tostring, tonumber;
21 local t_insert = table.insert;
22 local append = require "util.serialization".append;
23 local envloadfile = require"util.envload".envloadfile;
24 local path_separator = assert ( package.config:match ( "^([^\n]+)" ) , "package.config not in standard form" ) -- Extract directory seperator from package.config (an undocumented string that comes with lua)
25 local lfs = require "lfs";
26 local prosody = prosody;
29 if prosody.platform == "posix" then
30 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask
32 raw_mkdir = lfs.mkdir;
40 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
43 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
47 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end));
52 local function mkdir(path)
53 path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here
54 if not _mkdir[path] then
61 local data_path = (prosody and prosody.paths and prosody.paths.data) or ".";
64 ------- API -------------
66 function set_data_path(path)
67 log("debug", "Setting data path to: %s", path);
71 local function callback(username, host, datastore, data)
72 for _, f in ipairs(callbacks) do
73 username, host, datastore, data = f(username, host, datastore, data);
74 if username == false then break; end
77 return username, host, datastore, data;
79 function add_callback(func)
80 if not callbacks[func] then -- Would you really want to set the same callback more than once?
81 callbacks[func] = true;
82 callbacks[#callbacks+1] = func;
86 function remove_callback(func)
87 if callbacks[func] then
88 for i, f in ipairs(callbacks) do
98 function getpath(username, host, datastore, ext, create)
100 host = (host and encode(host)) or "_global";
101 username = username and encode(username);
103 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
104 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
106 if create then mkdir(mkdir(data_path).."/"..host); end
107 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
109 if create then mkdir(data_path); end
110 return format("%s/%s.%s", data_path, datastore, ext);
114 function load(username, host, datastore)
115 local data, ret = envloadfile(getpath(username, host, datastore), {});
117 local mode = lfs.attributes(getpath(username, host, datastore), "mode");
119 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
121 else -- file exists, but can't be read
122 -- TODO more detailed error checking and logging?
123 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
124 return nil, "Error reading storage";
128 local success, ret = pcall(data);
130 log("error", "Unable to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
131 return nil, "Error reading storage";
136 function store(username, host, datastore, data)
141 username, host, datastore, data = callback(username, host, datastore, data);
142 if username == false then
143 return true; -- Don't save this data at all
146 -- save the datastore
147 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
149 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
150 return nil, "Error saving to storage";
155 if next(data) == nil then -- try to delete empty datastore
156 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
157 os_remove(getpath(username, host, datastore));
159 -- we write data even when we are deleting because lua doesn't have a
160 -- platform independent way of checking for non-exisitng files
164 function list_append(username, host, datastore, data)
165 if not data then return; end
166 if callback(username, host, datastore) == false then return true; end
167 -- save the datastore
168 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
170 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
180 function list_store(username, host, datastore, data)
184 if callback(username, host, datastore) == false then return true; end
185 -- save the datastore
186 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
188 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
191 for _, d in ipairs(data) do
197 if next(data) == nil then -- try to delete empty datastore
198 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
199 os_remove(getpath(username, host, datastore, "list"));
201 -- we write data even when we are deleting because lua doesn't have a
202 -- platform independent way of checking for non-exisitng files
206 function list_load(username, host, datastore)
208 local data, ret = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end});
210 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode");
212 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
214 else -- file exists, but can't be read
215 -- TODO more detailed error checking and logging?
216 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
217 return nil, "Error reading storage";
221 local success, ret = pcall(data);
223 log("error", "Unable to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil");
224 return nil, "Error reading storage";
229 function list_stores(username, host)
231 return nil, "bad argument #2 to 'list_stores' (string expected, got nothing)";
234 local host_dir = format("%s/%s/", data_path, encode(host));
235 for node in lfs.dir(host_dir) do
236 if not node:match"^%." then -- dots should be encoded, this is probably . or ..
237 local store = decode(node);
238 local path = host_dir..node;
239 if username == true then
240 if lfs.attributes(path, "mode") == "directory" then
241 list[#list+1] = store;
244 if lfs.attributes(getpath(username, host, store), "mode")
245 or lfs.attributes(getpath(username, host, store, "list"), "mode") then
246 list[#list+1] = store;
248 elseif lfs.attributes(path, "mode") == "file" then
249 list[#list+1] = store:gsub("%.[dalist]+$","");