diff options
author | Kim Alvefur <zash@zash.se> | 2015-12-11 20:07:22 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2015-12-11 20:07:22 +0100 |
commit | 429ea15d8b1470965fa09707e38d1c66977c20c5 (patch) | |
tree | ce4b92e40aca5e320011820ad3cf382c098bbdc8 /util | |
parent | 47fd3f55f22d92f1fa2d25097545a6b4a17d3564 (diff) | |
download | prosody-429ea15d8b1470965fa09707e38d1c66977c20c5.tar.gz prosody-429ea15d8b1470965fa09707e38d1c66977c20c5.zip |
util.datamanager: Factor out code for appending bytes to a file
Diffstat (limited to 'util')
-rw-r--r-- | util/datamanager.lua | 28 |
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 = {}; |