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 = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
24 local lfs = require "lfs";
27 if prosody.platform == "posix" then
28 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask
30 raw_mkdir = lfs.mkdir;
38 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
41 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
45 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end));
50 local function mkdir(path)
51 path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here
52 if not _mkdir[path] then
59 local data_path = "data";
62 ------- API -------------
64 function set_data_path(path)
65 log("debug", "Setting data path to: %s", path);
69 local function callback(username, host, datastore, data)
70 for _, f in ipairs(callbacks) do
71 username, host, datastore, data = f(username, host, datastore, data);
72 if username == false then break; end
75 return username, host, datastore, data;
77 function add_callback(func)
78 if not callbacks[func] then -- Would you really want to set the same callback more than once?
79 callbacks[func] = true;
80 callbacks[#callbacks+1] = func;
84 function remove_callback(func)
85 if callbacks[func] then
86 for i, f in ipairs(callbacks) do
96 function getpath(username, host, datastore, ext, create)
98 host = (host and encode(host)) or "_global";
99 username = username and encode(username);
101 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
102 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
104 if create then mkdir(mkdir(data_path).."/"..host); end
105 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
107 if create then mkdir(data_path); end
108 return format("%s/%s.%s", data_path, datastore, ext);
112 function load(username, host, datastore)
113 local data, ret = loadfile(getpath(username, host, datastore));
115 local mode = lfs.attributes(getpath(username, host, datastore), "mode");
117 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
119 else -- file exists, but can't be read
120 -- TODO more detailed error checking and logging?
121 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
122 return nil, "Error reading storage";
126 local success, ret = pcall(data);
128 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
129 return nil, "Error reading storage";
134 function store(username, host, datastore, data)
139 username, host, datastore, data = callback(username, host, datastore, data);
140 if username == false then
141 return true; -- Don't save this data at all
144 -- save the datastore
145 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
147 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
148 return nil, "Error saving to storage";
153 if next(data) == nil then -- try to delete empty datastore
154 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
155 os_remove(getpath(username, host, datastore));
157 -- we write data even when we are deleting because lua doesn't have a
158 -- platform independent way of checking for non-exisitng files
162 function list_append(username, host, datastore, data)
163 if not data then return; end
164 if callback(username, host, datastore) == false then return true; end
165 -- save the datastore
166 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
168 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
178 function list_store(username, host, datastore, data)
182 if callback(username, host, datastore) == false then return true; end
183 -- save the datastore
184 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
186 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
189 for _, d in ipairs(data) do
195 if next(data) == nil then -- try to delete empty datastore
196 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
197 os_remove(getpath(username, host, datastore, "list"));
199 -- we write data even when we are deleting because lua doesn't have a
200 -- platform independent way of checking for non-exisitng files
204 function list_load(username, host, datastore)
205 local data, ret = loadfile(getpath(username, host, datastore, "list"));
207 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
211 setfenv(data, {item = function(i) t_insert(items, i); end});
212 local success, ret = pcall(data);
214 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));