Comparison

util/datamanager.lua @ 5118:0dc9e6c128c3

util.datamanager: Make the util.pposix dependency optional.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 12 Sep 2012 22:03:06 +0500
parent 5103:5a1488369c35
child 5130:051d352ed03c
comparison
equal deleted inserted replaced
5117:2c7e1ce8f482 5118:0dc9e6c128c3
23 local envloadfile = require"util.envload".envloadfile; 23 local envloadfile = require"util.envload".envloadfile;
24 local serialize = require "util.serialization".serialize; 24 local serialize = require "util.serialization".serialize;
25 local path_separator = assert ( package.config:match ( "^([^\n]+)" ) , "package.config not in standard form" ) -- Extract directory seperator from package.config (an undocumented string that comes with lua) 25 local path_separator = assert ( package.config:match ( "^([^\n]+)" ) , "package.config not in standard form" ) -- Extract directory seperator from package.config (an undocumented string that comes with lua)
26 local lfs = require "lfs"; 26 local lfs = require "lfs";
27 local prosody = prosody; 27 local prosody = prosody;
28 local raw_mkdir; 28
29 local fallocate; 29 local raw_mkdir = lfs.mkdir;
30 30 local function fallocate(f, offset, len)
31 if prosody.platform == "posix" then 31 -- This assumes that current position == offset
32 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask 32 local fake_data = (" "):rep(len);
33 fallocate = require "util.pposix".fallocate; 33 local ok, msg = f:write(fake_data);
34 else 34 if not ok then
35 raw_mkdir = lfs.mkdir; 35 return ok, msg;
36 end 36 end
37 37 f:seek("set", offset);
38 if not fallocate then -- Fallback 38 return true;
39 function fallocate(f, offset, len) 39 end;
40 -- This assumes that current position == offset 40 pcall(function()
41 local fake_data = (" "):rep(len); 41 local pposix = require "util.pposix";
42 local ok, msg = f:write(fake_data); 42 raw_mkdir = pposix.mkdir or raw_mkdir; -- Doesn't trample on umask
43 if not ok then 43 fallocate = pposix.fallocate or fallocate;
44 return ok, msg; 44 end);
45 end
46 f:seek("set", offset);
47 return true;
48 end
49 end
50 45
51 module "datamanager" 46 module "datamanager"
52 47
53 ---- utils ----- 48 ---- utils -----
54 local encode, decode; 49 local encode, decode;