2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 io_popen = io.popen;
19 local tostring, tonumber = tostring, tonumber;
22 local t_insert = table.insert;
23 local append = require "util.serialization".append;
24 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
31 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
34 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
38 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end));
43 local function mkdir(path)
44 path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here
45 if not _mkdir[path] then
46 local x = io_popen("mkdir \""..path.."\" 2>&1"):read("*a");
52 local data_path = "data";
55 ------- API -------------
57 function set_data_path(path)
58 log("debug", "Setting data path to: %s", path);
62 local function callback(username, host, datastore, data)
63 for _, f in ipairs(callbacks) do
64 username, host, datastore, data = f(username, host, datastore, data);
65 if username == false then break; end
68 return username, host, datastore, data;
70 function add_callback(func)
71 if not callbacks[func] then -- Would you really want to set the same callback more than once?
72 callbacks[func] = true;
73 callbacks[#callbacks+1] = func;
77 function remove_callback(func)
78 if callbacks[func] then
79 for i, f in ipairs(callbacks) do
89 function getpath(username, host, datastore, ext, create)
91 host = host and encode(host);
92 username = username and encode(username);
94 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
95 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
97 if create then mkdir(mkdir(data_path).."/"..host); end
98 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
100 if create then mkdir(data_path); end
101 return format("%s/%s.%s", data_path, datastore, ext);
105 function load(username, host, datastore)
106 local data, ret = loadfile(getpath(username, host, datastore));
108 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
112 local success, ret = pcall(data);
114 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
120 function store(username, host, datastore, data)
125 username, host, datastore, data = callback(username, host, datastore, data);
126 if username == false then
127 return true; -- Don't save this data at all
130 -- save the datastore
131 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
133 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
139 if next(data) == nil then -- try to delete empty datastore
140 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
141 os_remove(getpath(username, host, datastore));
143 -- we write data even when we are deleting because lua doesn't have a
144 -- platform independent way of checking for non-exisitng files
148 function list_append(username, host, datastore, data)
149 if not data then return; end
150 if callback(username, host, datastore) == false then return true; end
151 -- save the datastore
152 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
154 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
164 function list_store(username, host, datastore, data)
168 if callback(username, host, datastore) == false then return true; end
169 -- save the datastore
170 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
172 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
175 for _, d in ipairs(data) do
181 if next(data) == nil then -- try to delete empty datastore
182 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
183 os_remove(getpath(username, host, datastore, "list"));
185 -- we write data even when we are deleting because lua doesn't have a
186 -- platform independent way of checking for non-exisitng files
190 function list_load(username, host, datastore)
191 local data, ret = loadfile(getpath(username, host, datastore, "list"));
193 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
197 setfenv(data, {item = function(i) t_insert(items, i); end});
198 local success, ret = pcall(data);
200 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));