aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2012-07-29 02:28:25 +0100
committerMatthew Wild <mwild1@gmail.com>2012-07-29 02:28:25 +0100
commitabc359a28fc9a70348da270c5cbc8b169f2495cf (patch)
treed297759efe7397d10a00b7b941221a17f5701cd1
parent30f0c3a465a1488d553fe8b40bbfbe593be8a656 (diff)
parent79e7a7cac4f67fa812b522c8db1e0c4652a51ccd (diff)
downloadprosody-abc359a28fc9a70348da270c5cbc8b169f2495cf.tar.gz
prosody-abc359a28fc9a70348da270c5cbc8b169f2495cf.zip
Merge with Zash
-rwxr-xr-xconfigure2
-rw-r--r--util/datamanager.lua47
2 files changed, 35 insertions, 14 deletions
diff --git a/configure b/configure
index dd6af49c..209fdccb 100755
--- a/configure
+++ b/configure
@@ -96,6 +96,7 @@ do
LUA_SUFFIX_SET=yes
LUA_INCDIR=/usr/include/lua5.1;
LUA_INCDIR_SET=yes
+ CFLAGS="$CFLAGS -D_GNU_SOURCE"
fi
if [ "$OSTYPE" = "macosx" ]
then LUA_INCDIR=/usr/local/include;
@@ -110,6 +111,7 @@ do
LUA_LIBDIR=/usr/local/lib
LUA_LIBDIR_SET=yes
CFLAGS="-Wall -fPIC"
+ CFLAGS="$CFLAGS -D_GNU_SOURCE"
LDFLAGS="-shared"
fi
if [ "$OSTYPE" = "freebsd" -o "$OSTYPE" = "openbsd" ]
diff --git a/util/datamanager.lua b/util/datamanager.lua
index 29a5b994..5d3074a3 100644
--- a/util/datamanager.lua
+++ b/util/datamanager.lua
@@ -15,11 +15,12 @@ local pcall = pcall;
local log = require "util.logger".init("datamanager");
local io_open = io.open;
local os_remove = os.remove;
+local os_rename = os.rename;
local tostring, tonumber = tostring, tonumber;
local error = error;
local next = next;
local t_insert = table.insert;
-local append = require "util.serialization".append;
+local t_concat = table.concat;
local envloadfile = require"util.envload".envloadfile;
local serialize = require "util.serialization".serialize;
local path_separator = assert ( package.config:match ( "^([^\n]+)" ) , "package.config not in standard form" ) -- Extract directory seperator from package.config (an undocumented string that comes with lua)
@@ -149,6 +150,28 @@ function load(username, host, datastore)
return ret;
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
+
+ ok, msg = f:close();
+ if not ok then break end
+
+ return os_rename(scratch, filename);
+ until false;
+
+ -- Cleanup
+ if f then f:close(); end
+ os_remove(scratch);
+ return nil, msg;
+end
+
function store(username, host, datastore, data)
if not data then
data = {};
@@ -160,14 +183,12 @@ function store(username, host, datastore, data)
end
-- save the datastore
- local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
- if not f then
+ local d = "return " .. serialize(data) .. ";\n";
+ local ok, msg = atomic_store(getpath(username, host, datastore, nil, true), d);
+ if not ok then
log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
return nil, "Error saving to storage";
end
- f:write("return ");
- 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 or "nil", host or "nil");
os_remove(getpath(username, host, datastore));
@@ -206,17 +227,15 @@ function list_store(username, host, datastore, data)
end
if callback(username, host, datastore) == false then return true; end
-- save the datastore
- local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
- if not f then
+ local d = {};
+ for _, item in ipairs(data) do
+ d[#d+1] = "item(" .. serialize(item) .. ");\n";
+ end
+ local ok, msg = atomic_store(getpath(username, host, datastore, "list", true), t_concat(d));
+ if not ok then
log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
return;
end
- for _, d in ipairs(data) do
- f:write("item(");
- append(f, d);
- f:write(");\n");
- 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 or "nil", host or "nil");
os_remove(getpath(username, host, datastore, "list"));