aboutsummaryrefslogtreecommitdiffstats
path: root/util/datamanager.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2015-12-11 20:07:22 +0100
committerKim Alvefur <zash@zash.se>2015-12-11 20:07:22 +0100
commit3e9c86d509060611bdebd819a2fe61960b213e19 (patch)
treece4b92e40aca5e320011820ad3cf382c098bbdc8 /util/datamanager.lua
parent7f8d1c0995b51435b4d6a4f5c7a5d5ed7ab5d8e2 (diff)
downloadprosody-3e9c86d509060611bdebd819a2fe61960b213e19.tar.gz
prosody-3e9c86d509060611bdebd819a2fe61960b213e19.zip
util.datamanager: Factor out code for appending bytes to a file
Diffstat (limited to 'util/datamanager.lua')
-rw-r--r--util/datamanager.lua28
1 files changed, 18 insertions, 10 deletions
diff --git a/util/datamanager.lua b/util/datamanager.lua
index 4b722851..13aed78f 100644
--- a/util/datamanager.lua
+++ b/util/datamanager.lua
@@ -209,32 +209,40 @@ local function store(username, host, datastore, data)
return true;
end
-local function list_append(username, host, datastore, data)
- if not data then return; 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), "r+");
+local function append(username, host, datastore, ext, data)
+ local f, msg = io_open(getpath(username, host, datastore, ext, true), "r+");
if not f then
- f, msg = io_open(getpath(username, host, datastore, "list", true), "w");
+ f, msg = io_open(getpath(username, host, datastore, ext, true), "w");
end
if not f then
- log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
- return;
+ return nil, msg;
end
- local data = "item(" .. serialize(data) .. ");\n";
local pos = f:seek("end");
local ok, msg = fallocate(f, pos, #data);
f:seek("set", pos);
if ok then
f:write(data);
else
- log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
return ok, msg;
end
f:close();
return true;
end
+local function list_append(username, host, datastore, data)
+ if not data then return; end
+ if callback(username, host, datastore) == false then return true; end
+ -- save the datastore
+
+ local data = "item(" .. serialize(data) .. ");\n";
+ local ok, msg = append(username, host, datastore, "list", data);
+ 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 ok, msg;
+ end
+ return true;
+end
+
local function list_store(username, host, datastore, data)
if not data then
data = {};