Changeset

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
parents 1968:95ad6e68e203
children 1970:5ea6f4e6fa8c
files mod_storage_xmlarchive/README.markdown mod_storage_xmlarchive/mod_storage_xmlarchive.lua
diffstat 2 files changed, 15 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/mod_storage_xmlarchive/README.markdown	Sat Dec 12 03:25:57 2015 +0100
+++ b/mod_storage_xmlarchive/README.markdown	Sat Dec 12 03:30:06 2015 +0100
@@ -23,7 +23,7 @@
 }
 ```
 
-To use it with [mod\_mam\_muc] or [mod_http_muc_log]:
+To use it with [mod\_mam\_muc] or [mod\_http\_muc\_log]:
 
 ``` lua
 storage = {
@@ -31,8 +31,8 @@
 }
 ```
 
-Refer to [Prosodys data storage
-documentation](https://prosody.im/doc/storage) for more information.
+Refer to [Prosodys data storage documentation][doc:storage] for more
+information.
 
 Note that this module does not implement the "keyval" storage method and
 can't be used by anything other than archives.
--- 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