util.datamanager: Import tostring and type (fix global access)
[prosody.git] / util / datamanager.lua
index e57d5fdc5ecdae39ed86598a0022cba47d78c7d6..fb9ba3a4ce6283f6eb33abce53a218f692cc96c6 100644 (file)
@@ -17,7 +17,9 @@ local io_open = io.open;
 local os_remove = os.remove;
 local os_rename = os.rename;
 local tonumber = tonumber;
+local tostring = tostring;
 local next = next;
+local type = type;
 local t_insert = table.insert;
 local t_concat = table.concat;
 local envloadfile = require"util.envload".envloadfile;
@@ -144,23 +146,26 @@ end
 local function atomic_store(filename, data)
        local scratch = filename.."~";
        local f, ok, msg;
-       repeat
-               f, msg = io_open(scratch, "w");
-               if not f then break end
 
-               ok, msg = f:write(data);
-               if not ok then break end
+       f, msg = io_open(scratch, "w");
+       if not f then
+               return nil, msg;
+       end
 
-               ok, msg = f:close();
-               if not ok then break end
+       ok, msg = f:write(data);
+       if not ok then
+               f:close();
+               os_remove(scratch);
+               return nil, msg;
+       end
 
-               return os_rename(scratch, filename);
-       until false;
+       ok, msg = f:close();
+       if not ok then
+               os_remove(scratch);
+               return nil, msg;
+       end
 
-       -- Cleanup
-       if f then f:close(); end
-       os_remove(scratch);
-       return nil, msg;
+       return os_rename(scratch, filename);
 end
 
 if prosody and prosody.platform ~= "posix" then
@@ -220,7 +225,7 @@ local function append(username, host, datastore, ext, data)
                -- File did probably not exist, let's create it
                f, msg = io_open(filename, "w");
                if not f then
-                       return nil, msg;
+                       return nil, msg, "open";
                end
        end
 
@@ -239,7 +244,7 @@ local function append(username, host, datastore, ext, data)
        ok, msg = f:write(data);
        if not ok then
                f:close();
-               return ok, msg;
+               return ok, msg, "write";
        end
 
        ok, msg = f:close();
@@ -247,7 +252,7 @@ local function append(username, host, datastore, ext, data)
                return ok, msg;
        end
 
-       return true;
+       return true, pos;
 end
 
 local function list_append(username, host, datastore, data)
@@ -402,6 +407,7 @@ return {
        getpath = getpath;
        load = load;
        store = store;
+       append_raw = append;
        list_append = list_append;
        list_store = list_store;
        list_load = list_load;