Software /
code /
prosody
Comparison
util/datamanager.lua @ 3086:931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 22 May 2010 05:49:21 +0500 |
parent | 2925:692b3c6c5bd2 |
child | 3108:df0df92dd58a |
comparison
equal
deleted
inserted
replaced
3085:cf01b0725272 | 3086:931acb1188b1 |
---|---|
19 local error = error; | 19 local error = error; |
20 local next = next; | 20 local next = next; |
21 local t_insert = table.insert; | 21 local t_insert = table.insert; |
22 local append = require "util.serialization".append; | 22 local append = require "util.serialization".append; |
23 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end | 23 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end |
24 local lfs = require "lfs"; | |
24 local raw_mkdir; | 25 local raw_mkdir; |
25 | 26 |
26 if prosody.platform == "posix" then | 27 if prosody.platform == "posix" then |
27 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask | 28 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask |
28 else | 29 else |
29 raw_mkdir = require "lfs".mkdir; | 30 raw_mkdir = lfs.mkdir; |
30 end | 31 end |
31 | 32 |
32 module "datamanager" | 33 module "datamanager" |
33 | 34 |
34 ---- utils ----- | 35 ---- utils ----- |
109 end | 110 end |
110 | 111 |
111 function load(username, host, datastore) | 112 function load(username, host, datastore) |
112 local data, ret = loadfile(getpath(username, host, datastore)); | 113 local data, ret = loadfile(getpath(username, host, datastore)); |
113 if not data then | 114 if not data then |
114 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 115 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); |
115 return nil; | 116 if not mode then |
117 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | |
118 return nil; | |
119 else -- file exists, but can't be read | |
120 -- TODO more detailed error checking and logging? | |
121 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | |
122 return nil, "Error reading storage"; | |
123 end | |
116 end | 124 end |
117 setfenv(data, {}); | 125 setfenv(data, {}); |
118 local success, ret = pcall(data); | 126 local success, ret = pcall(data); |
119 if not success then | 127 if not success then |
120 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 128 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
121 return nil; | 129 return nil, "Error reading storage"; |
122 end | 130 end |
123 return ret; | 131 return ret; |
124 end | 132 end |
125 | 133 |
126 function store(username, host, datastore, data) | 134 function store(username, host, datastore, data) |