comparison 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
comparison
equal deleted inserted replaced
1739:940a4ab75cec 1740:11f7fb2f927f
12 if not ok then 12 if not ok then
13 return ok, msg; 13 return ok, msg;
14 end 14 end
15 return f:seek("set", offset); 15 return f:seek("set", offset);
16 end; 16 end;
17
17 pcall(function() 18 pcall(function()
18 local pposix = require "util.pposix"; 19 local pposix = require "util.pposix";
19 fallocate = pposix.fallocate or fallocate; 20 fallocate = pposix.fallocate or fallocate;
20 end); 21 end);
21 22
25 function archive:append(username, _, when, with, data) 26 function archive:append(username, _, when, with, data)
26 if getmetatable(data) ~= st.stanza_mt then 27 if getmetatable(data) ~= st.stanza_mt then
27 module:log("error", "Attempt to store non-stanza object, traceback: %s", debug.traceback()); 28 module:log("error", "Attempt to store non-stanza object, traceback: %s", debug.traceback());
28 return nil, "unsupported-datatype"; 29 return nil, "unsupported-datatype";
29 end 30 end
31
30 username = username or "@"; 32 username = username or "@";
31 data = tostring(data) .. "\n"; 33 data = tostring(data) .. "\n";
34
32 local day = dt.date(when); 35 local day = dt.date(when);
33 local filename = dm.getpath(username.."@"..day, module.host, self.store, "xml", true); 36 local filename = dm.getpath(username.."@"..day, module.host, self.store, "xml", true);
37
34 local ok, err; 38 local ok, err;
35 local f = io.open(filename, "r+"); 39 local f = io.open(filename, "r+");
36 if not f then 40 if not f then
37 f, err = io.open(filename, "w"); 41 f, err = io.open(filename, "w"); if not f then return nil, err; end
38 if not f then return nil, err; end
39 ok, err = dm.list_append(username, module.host, self.store, day); 42 ok, err = dm.list_append(username, module.host, self.store, day);
40 if not ok then return nil, err; end 43 if not ok then return nil, err; end
41 end 44 end
42 local offset = f and f:seek("end"); 45
43 ok, err = fallocate(f, offset, #data); 46 local offset = f:seek("end"); -- Seek to the end and collect current file length
44 if not ok then return nil, err; end 47 -- then make sure there is enough free space for what we're about to add
45 f:seek("set", offset); 48 ok, err = fallocate(f, offset, #data); if not ok then return nil, err; end
46 ok, err = f:write(data); 49 ok, err = f:write(data); if not ok then return nil, err; end
47 if not ok then return nil, err; end 50 ok, err = f:close(); if not ok then return nil, err; end
48 ok, err = f:close(); 51
49 if not ok then return nil, err; end
50 local id = day .. "-" .. hmac_sha256(username.."@"..day.."+"..offset, data, true):sub(-16); 52 local id = day .. "-" .. hmac_sha256(username.."@"..day.."+"..offset, data, true):sub(-16);
51 ok, err = dm.list_append(username.."@"..day, module.host, self.store, { id = id, when = when, with = with, offset = offset, length = #data }); 53 ok, err = dm.list_append(username.."@"..day, module.host, self.store, { id = id, when = when, with = with, offset = offset, length = #data });
52 if not ok then return nil, err; end 54 if not ok then return nil, err; end
53 return id; 55 return id;
54 end 56 end