Software /
code /
prosody-modules
Diff
mod_storage_xmlarchive/mod_storage_xmlarchive.lua @ 1969:e63dba236a2a
mod_storage_xmlarchive: Use datamanager.append_raw (had that code duplicated here)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 12 Dec 2015 03:30:06 +0100 |
parent | 1831:004d3bfc05ea |
child | 2038:a85b5c3791dc |
line wrap: on
line diff
--- a/mod_storage_xmlarchive/mod_storage_xmlarchive.lua Sat Dec 12 03:25:57 2015 +0100 +++ b/mod_storage_xmlarchive/mod_storage_xmlarchive.lua Sat Dec 12 03:30:06 2015 +0100 @@ -12,21 +12,6 @@ local new_stream = require "util.xmppstream".new; local empty = {}; -local function fallocate(f, offset, len) - -- This assumes that current position == offset - local fake_data = (" "):rep(len); - local ok, msg = f:write(fake_data); - if not ok then - return ok, msg; - end - return f:seek("set", offset); -end; - -pcall(function() - local pposix = require "util.pposix"; - fallocate = pposix.fallocate or fallocate; -end); - local archive = {}; local archive_mt = { __index = archive }; @@ -43,25 +28,23 @@ 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 - ok, err = dm.list_append(username, module.host, self.store, day); - if not ok then return nil, err; end + local ok, err = dm.append_raw(username.."@"..day, module.host, self.store, "xml", data); + 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 offset = ok and err; 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 = dt.datetime(when), with = with, offset = offset, length = #data }); - if not ok then return nil, err; end + if offset == 0 then + -- means the message is at the beginnig of the file, so it's a new day + -- so we add this new day to the "index" + dm.list_append(username, module.host, self.store, day); + end + if not ok then + return nil, err; + end return id; end