Merge Tobias's fancy SASL branch->trunk
[prosody.git] / util / datamanager.lua
index 8e4b782866b776d94ae68fd59f1d6da86c902b4c..01c7aab2210f85f817f7e6c63620d5d37cab7937 100644 (file)
@@ -1,6 +1,6 @@
--- Prosody IM v0.4
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
 -- 
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
@@ -15,19 +15,25 @@ local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
 local log = require "util.logger".init("datamanager");
 local io_open = io.open;
 local os_remove = os.remove;
-local io_popen = io.popen;
 local tostring, tonumber = tostring, tonumber;
 local error = error;
 local next = next;
 local t_insert = table.insert;
 local append = require "util.serialization".append;
 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
+local raw_mkdir;
+
+if prosody.platform == "posix" then
+       raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask
+else
+       raw_mkdir = require "lfs".mkdir;
+end
 
 module "datamanager"
 
 ---- utils -----
 local encode, decode;
-do 
+do
        local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
 
        decode = function (s)
@@ -43,7 +49,7 @@ local _mkdir = {};
 local function mkdir(path)
        path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here
        if not _mkdir[path] then
-               local x = io_popen("mkdir \""..path.."\" 2>&1"):read("*a");
+               raw_mkdir(path);
                _mkdir[path] = true;
        end
        return path;
@@ -88,7 +94,7 @@ end
 
 function getpath(username, host, datastore, ext, create)
        ext = ext or "dat";
-       host = host and encode(host);
+       host = (host and encode(host)) or "_global";
        username = username and encode(username);
        if username then
                if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
@@ -137,7 +143,7 @@ function store(username, host, datastore, data)
        append(f, data);
        f:close();
        if next(data) == nil then -- try to delete empty datastore
-               log("debug", "Removing empty %s datastore for user %s@%s", datastore, username, host);
+               log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
                os_remove(getpath(username, host, datastore));
        end
        -- we write data even when we are deleting because lua doesn't have a
@@ -179,7 +185,7 @@ function list_store(username, host, datastore, data)
        end
        f:close();
        if next(data) == nil then -- try to delete empty datastore
-               log("debug", "Removing empty %s datastore for user %s@%s", datastore, username, host);
+               log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil");
                os_remove(getpath(username, host, datastore, "list"));
        end
        -- we write data even when we are deleting because lua doesn't have a