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;
14 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
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 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)
24 local lfs = require "lfs";
25 local prosody = prosody;
28 if prosody.platform == "posix" then
29 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask
31 raw_mkdir = lfs.mkdir;
39 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
42 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
46 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end));
51 local function mkdir(path)
52 path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here
53 if not _mkdir[path] then
60 local data_path = (prosody and prosody.paths and prosody.paths.data) or ".";
63 ------- API -------------
65 function set_data_path(path)
66 log("debug", "Setting data path to: %s", path);
70 local function callback(username, host, datastore, data)
71 for _, f in ipairs(callbacks) do
72 username, host, datastore, data = f(username, host, datastore, data);
73 if username == false then break; end
76 return username, host, datastore, data;
78 function add_callback(func)
79 if not callbacks[func] then -- Would you really want to set the same callback more than once?
80 callbacks[func] = true;
81 callbacks[#callbacks+1] = func;
85 function remove_callback(func)
86 if callbacks[func] then
87 for i, f in ipairs(callbacks) do
97 function getpath(username, host, datastore, ext, create)
99 host = (host and encode(host)) or "_global";
100 username = username and encode(username);
102 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
103 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
105 if create then mkdir(mkdir(data_path).."/"..host); end
106 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
108 if create then mkdir(data_path); end
109 return format("%s/%s.%s", data_path, datastore, ext);
113 function load(username, host, datastore)
114 local data, ret = loadfile(getpath(username, host, datastore));
116 local mode = lfs.attributes(getpath(username, host, datastore), "mode");
118 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
120 else -- file exists, but can't be read
121 -- TODO more detailed error checking and logging?
122 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
123 return nil, "Error reading storage";
127 local success, ret = pcall(data);
129 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
130 return nil, "Error reading storage";
135 function store(username, host, datastore, data)
140 username, host, datastore, data = callback(username, host, datastore, data);
141 if username == false then
142 return true; -- Don't save this data at all
145 -- save the datastore
146 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
148 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
149 return nil, "Error saving to storage";
154 if next(data) == nil then -- try to delete empty datastore
155 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
156 os_remove(getpath(username, host, datastore));
158 -- we write data even when we are deleting because lua doesn't have a
159 -- platform independent way of checking for non-exisitng files
163 function list_append(username, host, datastore, data)
164 if not data then return; end
165 if callback(username, host, datastore) == false then return true; end
166 -- save the datastore
167 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
169 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
179 function list_store(username, host, datastore, data)
183 if callback(username, host, datastore) == false then return true; end
184 -- save the datastore
185 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
187 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
190 for _, d in ipairs(data) do
196 if next(data) == nil then -- try to delete empty datastore
197 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
198 os_remove(getpath(username, host, datastore, "list"));
200 -- we write data even when we are deleting because lua doesn't have a
201 -- platform independent way of checking for non-exisitng files
205 function list_load(username, host, datastore)
206 local data, ret = loadfile(getpath(username, host, datastore, "list"));
208 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode");
210 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
212 else -- file exists, but can't be read
213 -- TODO more detailed error checking and logging?
214 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
215 return nil, "Error reading storage";
219 setfenv(data, {item = function(i) t_insert(items, i); end});
220 local success, ret = pcall(data);
222 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
223 return nil, "Error reading storage";