2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 local format = string.format;
22 local setmetatable, type = setmetatable, type;
23 local pairs, ipairs = pairs, ipairs;
24 local char = string.char;
25 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
26 local log = require "util.logger".init("datamanager");
27 local io_open = io.open;
28 local os_remove = os.remove;
29 local tostring, tonumber = tostring, tonumber;
32 local t_insert = table.insert;
33 local append = require "util.serialization".append;
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("%%%x", c:byte()); end));
51 ------- API -------------
53 local data_path = "data";
54 function set_data_path(path)
58 function getpath(username, host, datastore, ext)
61 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
63 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
65 return format("%s/%s.%s", data_path, datastore, ext);
69 function load(username, host, datastore)
70 local data, ret = loadfile(getpath(username, host, datastore));
72 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
76 local success, ret = pcall(data);
78 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
84 function store(username, host, datastore, data)
89 local f, msg = io_open(getpath(username, host, datastore), "w+");
91 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
97 if not next(data) then -- try to delete empty datastore
98 os_remove(getpath(username, host, datastore));
100 -- we write data even when we are deleting because lua doesn't have a
101 -- platform independent way of checking for non-exisitng files
105 function list_append(username, host, datastore, data)
106 if not data then return; end
107 -- save the datastore
108 local f, msg = io_open(getpath(username, host, datastore, "list"), "a+");
110 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
120 function list_store(username, host, datastore, data)
124 -- save the datastore
125 local f, msg = io_open(getpath(username, host, datastore, "list"), "w+");
127 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
130 for _, d in ipairs(data) do
136 if not next(data) then -- try to delete empty datastore
137 os_remove(getpath(username, host, datastore, "list"));
139 -- we write data even when we are deleting because lua doesn't have a
140 -- platform independent way of checking for non-exisitng files
144 function list_load(username, host, datastore)
145 local data, ret = loadfile(getpath(username, host, datastore, "list"));
147 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
151 setfenv(data, {item = function(i) t_insert(items, i); end});
152 local success, ret = pcall(data);
154 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));