Mercurial > prosody-hg
comparison util/datamanager.lua @ 5021:85b2689dbcfe
Eliminate direct setfenv usage
| author | Florian Zeitz <florob@babelmonkeys.de> |
|---|---|
| date | Fri, 08 Jun 2012 05:04:38 +0200 |
| parents | 7de17ca4de14 |
| children | d25e1b9332cc |
comparison
equal
deleted
inserted
replaced
| 5020:ef1eb65acbba | 5021:85b2689dbcfe |
|---|---|
| 9 | 9 |
| 10 local format = string.format; | 10 local format = string.format; |
| 11 local setmetatable, type = setmetatable, type; | 11 local setmetatable, type = setmetatable, type; |
| 12 local pairs, ipairs = pairs, ipairs; | 12 local pairs, ipairs = pairs, ipairs; |
| 13 local char = string.char; | 13 local char = string.char; |
| 14 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; | 14 local pcall = pcall; |
| 15 local log = require "util.logger".init("datamanager"); | 15 local log = require "util.logger".init("datamanager"); |
| 16 local io_open = io.open; | 16 local io_open = io.open; |
| 17 local os_remove = os.remove; | 17 local os_remove = os.remove; |
| 18 local tostring, tonumber = tostring, tonumber; | 18 local tostring, tonumber = tostring, tonumber; |
| 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 envloadfile = require"util.envload".envloadfile; | |
| 23 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) | 24 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) |
| 24 local lfs = require "lfs"; | 25 local lfs = require "lfs"; |
| 25 local prosody = prosody; | 26 local prosody = prosody; |
| 26 local raw_mkdir; | 27 local raw_mkdir; |
| 27 | 28 |
| 109 return format("%s/%s.%s", data_path, datastore, ext); | 110 return format("%s/%s.%s", data_path, datastore, ext); |
| 110 end | 111 end |
| 111 end | 112 end |
| 112 | 113 |
| 113 function load(username, host, datastore) | 114 function load(username, host, datastore) |
| 114 local data, ret = loadfile(getpath(username, host, datastore)); | 115 local data, ret = envloadfile(getpath(username, host, datastore), {}); |
| 115 if not data then | 116 if not data then |
| 116 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); | 117 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); |
| 117 if not mode then | 118 if not mode then |
| 118 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 119 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 119 return nil; | 120 return nil; |
| 121 -- TODO more detailed error checking and logging? | 122 -- TODO more detailed error checking and logging? |
| 122 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 123 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 123 return nil, "Error reading storage"; | 124 return nil, "Error reading storage"; |
| 124 end | 125 end |
| 125 end | 126 end |
| 126 setfenv(data, {}); | 127 |
| 127 local success, ret = pcall(data); | 128 local success, ret = pcall(data); |
| 128 if not success then | 129 if not success then |
| 129 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 130 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 130 return nil, "Error reading storage"; | 131 return nil, "Error reading storage"; |
| 131 end | 132 end |
| 201 -- platform independent way of checking for non-exisitng files | 202 -- platform independent way of checking for non-exisitng files |
| 202 return true; | 203 return true; |
| 203 end | 204 end |
| 204 | 205 |
| 205 function list_load(username, host, datastore) | 206 function list_load(username, host, datastore) |
| 206 local data, ret = loadfile(getpath(username, host, datastore, "list")); | 207 local items = {}; |
| 208 local data, ret = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); | |
| 207 if not data then | 209 if not data then |
| 208 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); | 210 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); |
| 209 if not mode then | 211 if not mode then |
| 210 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 212 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 211 return nil; | 213 return nil; |
| 213 -- TODO more detailed error checking and logging? | 215 -- TODO more detailed error checking and logging? |
| 214 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 216 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 215 return nil, "Error reading storage"; | 217 return nil, "Error reading storage"; |
| 216 end | 218 end |
| 217 end | 219 end |
| 218 local items = {}; | 220 |
| 219 setfenv(data, {item = function(i) t_insert(items, i); end}); | |
| 220 local success, ret = pcall(data); | 221 local success, ret = pcall(data); |
| 221 if not success then | 222 if not success then |
| 222 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 223 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
| 223 return nil, "Error reading storage"; | 224 return nil, "Error reading storage"; |
| 224 end | 225 end |
