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 io_popen = io.popen;
30 local tostring, tonumber = tostring, tonumber;
33 local t_insert = table.insert;
34 local append = require "util.serialization".append;
35 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
42 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
45 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
49 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end));
54 local function mkdir(path)
55 path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here
56 if not _mkdir[path] then
57 local x = io_popen("mkdir \""..path.."\" 2>&1"):read("*a");
63 local data_path = "data";
65 ------- API -------------
67 function set_data_path(path)
68 log("info", "Setting data path to: %s", path);
72 function getpath(username, host, datastore, ext, create)
74 host = host and encode(host);
75 username = username and encode(username);
77 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
78 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
80 if create then mkdir(mkdir(data_path).."/"..host); end
81 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
83 if create then mkdir(data_path); end
84 return format("%s/%s.%s", data_path, datastore, ext);
88 function load(username, host, datastore)
89 local data, ret = loadfile(getpath(username, host, datastore));
91 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
95 local success, ret = pcall(data);
97 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
103 function store(username, host, datastore, data)
107 -- save the datastore
108 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
110 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
116 if not next(data) then -- try to delete empty datastore
117 os_remove(getpath(username, host, datastore));
119 -- we write data even when we are deleting because lua doesn't have a
120 -- platform independent way of checking for non-exisitng files
124 function list_append(username, host, datastore, data)
125 if not data then return; end
126 -- save the datastore
127 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
129 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
139 function list_store(username, host, datastore, data)
143 -- save the datastore
144 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
146 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
149 for _, d in ipairs(data) do
155 if not next(data) then -- try to delete empty datastore
156 os_remove(getpath(username, host, datastore, "list"));
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_load(username, host, datastore)
164 local data, ret = loadfile(getpath(username, host, datastore, "list"));
166 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
170 setfenv(data, {item = function(i) t_insert(items, i); end});
171 local success, ret = pcall(data);
173 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));