Mercurial > prosody-modules
diff mod_storage_xmlarchive/mod_storage_xmlarchive.lua @ 1740:11f7fb2f927f
mod_storage_xmlarchive: Code is annoying to read when every other line is 'if not ok then return'
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 18 May 2015 02:44:41 +0200 |
parents | 940a4ab75cec |
children | 07ceaf5f6f0d |
line wrap: on
line diff
--- a/mod_storage_xmlarchive/mod_storage_xmlarchive.lua Mon May 18 02:43:34 2015 +0200 +++ b/mod_storage_xmlarchive/mod_storage_xmlarchive.lua Mon May 18 02:44:41 2015 +0200 @@ -14,6 +14,7 @@ end return f:seek("set", offset); end; + pcall(function() local pposix = require "util.pposix"; fallocate = pposix.fallocate or fallocate; @@ -27,26 +28,27 @@ module:log("error", "Attempt to store non-stanza object, traceback: %s", debug.traceback()); return nil, "unsupported-datatype"; end + username = username or "@"; data = tostring(data) .. "\n"; + local day = dt.date(when); local filename = dm.getpath(username.."@"..day, module.host, self.store, "xml", true); + local ok, err; local f = io.open(filename, "r+"); if not f then - f, err = io.open(filename, "w"); - if not f then return nil, err; end + f, err = io.open(filename, "w"); if not f then return nil, err; end ok, err = dm.list_append(username, module.host, self.store, day); if not ok then return nil, err; end end - local offset = f and f:seek("end"); - ok, err = fallocate(f, offset, #data); - if not ok then return nil, err; end - f:seek("set", offset); - ok, err = f:write(data); - if not ok then return nil, err; end - ok, err = f:close(); - if not ok then return nil, err; end + + local offset = f:seek("end"); -- Seek to the end and collect current file length + -- then make sure there is enough free space for what we're about to add + ok, err = fallocate(f, offset, #data); if not ok then return nil, err; end + ok, err = f:write(data); if not ok then return nil, err; end + ok, err = f:close(); if not ok then return nil, err; end + local id = day .. "-" .. hmac_sha256(username.."@"..day.."+"..offset, data, true):sub(-16); ok, err = dm.list_append(username.."@"..day, module.host, self.store, { id = id, when = when, with = with, offset = offset, length = #data }); if not ok then return nil, err; end