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";
54 ------- API -------------
56 function set_data_path(path)
57 log("info", "Setting data path to: %s", path);
61 function getpath(username, host, datastore, ext, create)
63 host = host and encode(host);
64 username = username and encode(username);
66 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
67 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
69 if create then mkdir(mkdir(data_path).."/"..host); end
70 return format("%s/%s/%s.%s", data_path, host, datastore, ext);
72 if create then mkdir(data_path); end
73 return format("%s/%s.%s", data_path, datastore, ext);
77 function load(username, host, datastore)
78 local data, ret = loadfile(getpath(username, host, datastore));
80 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
84 local success, ret = pcall(data);
86 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
92 function store(username, host, datastore, data)
97 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
99 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
105 if not next(data) then -- try to delete empty datastore
106 os_remove(getpath(username, host, datastore));
108 -- we write data even when we are deleting because lua doesn't have a
109 -- platform independent way of checking for non-exisitng files
113 function list_append(username, host, datastore, data)
114 if not data then return; end
115 -- save the datastore
116 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
118 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
128 function list_store(username, host, datastore, data)
132 -- save the datastore
133 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
135 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
138 for _, d in ipairs(data) do
144 if not next(data) then -- try to delete empty datastore
145 os_remove(getpath(username, host, datastore, "list"));
147 -- we write data even when we are deleting because lua doesn't have a
148 -- platform independent way of checking for non-exisitng files
152 function list_load(username, host, datastore)
153 local data, ret = loadfile(getpath(username, host, datastore, "list"));
155 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
159 setfenv(data, {item = function(i) t_insert(items, i); end});
160 local success, ret = pcall(data);
162 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));