core.configmanager: Make components use 'component' module by default if none specified
[prosody.git] / util / datamanager.lua
index 45da17ddfe6913562cbc32b00d1513df2a93cbad..9d3c8592b3293ee8f4fca503190ab467ee0b759e 100644 (file)
@@ -1,20 +1,9 @@
--- Prosody IM v0.2
--- Copyright (C) 2008 Matthew Wild
--- Copyright (C) 2008 Waqas Hussain
+-- Prosody IM v0.4
+-- Copyright (C) 2008-2009 Matthew Wild
+-- Copyright (C) 2008-2009 Waqas Hussain
 -- 
--- This program is free software; you can redistribute it and/or
--- modify it under the terms of the GNU General Public License
--- as published by the Free Software Foundation; either version 2
--- of the License, or (at your option) any later version.
--- 
--- This program is distributed in the hope that it will be useful,
--- but WITHOUT ANY WARRANTY; without even the implied warranty of
--- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--- GNU General Public License for more details.
--- 
--- You should have received a copy of the GNU General Public License
--- along with this program; if not, write to the Free Software
--- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
 --
 
 
@@ -26,11 +15,13 @@ 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
 
 module "datamanager"
 
@@ -48,21 +39,37 @@ do
        end
 end
 
-------- API -------------
+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");
+               _mkdir[path] = true;
+       end
+       return path;
+end
 
 local data_path = "data";
+
+------- API -------------
+
 function set_data_path(path)
-       log("info", "Setting data path to %s", path);
+       log("info", "Setting data path to: %s", path);
        data_path = path;
 end
 
-function getpath(username, host, datastore, ext)
+function getpath(username, host, datastore, ext, create)
        ext = ext or "dat";
+       host = host and encode(host);
+       username = username and encode(username);
        if username then
-               return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
+               if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end
+               return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext);
        elseif host then
-               return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
+               if create then mkdir(mkdir(data_path).."/"..host); end
+               return format("%s/%s/%s.%s", data_path, host, datastore, ext);
        else
+               if create then mkdir(data_path); end
                return format("%s/%s.%s", data_path, datastore, ext);
        end
 end
@@ -70,7 +77,7 @@ end
 function load(username, host, datastore)
        local data, ret = loadfile(getpath(username, host, datastore));
        if not data then
-               log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
+               log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
                return nil;
        end
        setfenv(data, {});
@@ -87,7 +94,7 @@ function store(username, host, datastore, data)
                data = {};
        end
        -- save the datastore
-       local f, msg = io_open(getpath(username, host, datastore), "w+");
+       local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
        if not f then
                log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
                return;
@@ -106,7 +113,7 @@ end
 function list_append(username, host, datastore, data)
        if not data then return; end
        -- save the datastore
-       local f, msg = io_open(getpath(username, host, datastore, "list"), "a+");
+       local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
        if not f then
                log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
                return;
@@ -123,7 +130,7 @@ function list_store(username, host, datastore, data)
                data = {};
        end
        -- save the datastore
-       local f, msg = io_open(getpath(username, host, datastore, "list"), "w+");
+       local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
        if not f then
                log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
                return;
@@ -145,7 +152,7 @@ end
 function list_load(username, host, datastore)
        local data, ret = loadfile(getpath(username, host, datastore, "list"));
        if not data then
-               log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
+               log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
                return nil;
        end
        local items = {};