Software / code / prosody
Comparison
util/datamanager.lua @ 5024:d25e1b9332cc
Merge with Florob
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 28 Jul 2012 01:14:31 +0100 |
| parent | 5021:85b2689dbcfe |
| parent | 4993:5243b74a4cbb |
| child | 5032:c40ea227f8af |
comparison
equal
deleted
inserted
replaced
| 5023:dcc8e789df36 | 5024:d25e1b9332cc |
|---|---|
| 114 function load(username, host, datastore) | 114 function load(username, host, datastore) |
| 115 local data, ret = envloadfile(getpath(username, host, datastore), {}); | 115 local data, ret = envloadfile(getpath(username, host, datastore), {}); |
| 116 if not data then | 116 if not data then |
| 117 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); | 117 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); |
| 118 if not mode then | 118 if not mode then |
| 119 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 119 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
| 120 return nil; | 120 return nil; |
| 121 else -- file exists, but can't be read | 121 else -- file exists, but can't be read |
| 122 -- TODO more detailed error checking and logging? | 122 -- TODO more detailed error checking and logging? |
| 123 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 123 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
| 124 return nil, "Error reading storage"; | 124 return nil, "Error reading storage"; |
| 125 end | 125 end |
| 126 end | 126 end |
| 127 | 127 |
| 128 local success, ret = pcall(data); | 128 local success, ret = pcall(data); |
| 129 if not success then | 129 if not success then |
| 130 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 130 log("error", "Unable to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
| 131 return nil, "Error reading storage"; | 131 return nil, "Error reading storage"; |
| 132 end | 132 end |
| 133 return ret; | 133 return ret; |
| 134 end | 134 end |
| 135 | 135 |
| 144 end | 144 end |
| 145 | 145 |
| 146 -- save the datastore | 146 -- save the datastore |
| 147 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+"); | 147 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+"); |
| 148 if not f then | 148 if not f then |
| 149 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); | 149 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); |
| 150 return nil, "Error saving to storage"; | 150 return nil, "Error saving to storage"; |
| 151 end | 151 end |
| 152 f:write("return "); | 152 f:write("return "); |
| 153 append(f, data); | 153 append(f, data); |
| 154 f:close(); | 154 f:close(); |
| 165 if not data then return; end | 165 if not data then return; end |
| 166 if callback(username, host, datastore) == false then return true; end | 166 if callback(username, host, datastore) == false then return true; end |
| 167 -- save the datastore | 167 -- save the datastore |
| 168 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); | 168 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); |
| 169 if not f then | 169 if not f then |
| 170 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); | 170 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); |
| 171 return; | 171 return; |
| 172 end | 172 end |
| 173 f:write("item("); | 173 f:write("item("); |
| 174 append(f, data); | 174 append(f, data); |
| 175 f:write(");\n"); | 175 f:write(");\n"); |
| 183 end | 183 end |
| 184 if callback(username, host, datastore) == false then return true; end | 184 if callback(username, host, datastore) == false then return true; end |
| 185 -- save the datastore | 185 -- save the datastore |
| 186 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); | 186 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); |
| 187 if not f then | 187 if not f then |
| 188 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); | 188 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); |
| 189 return; | 189 return; |
| 190 end | 190 end |
| 191 for _, d in ipairs(data) do | 191 for _, d in ipairs(data) do |
| 192 f:write("item("); | 192 f:write("item("); |
| 193 append(f, d); | 193 append(f, d); |
| 207 local items = {}; | 207 local items = {}; |
| 208 local data, ret = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); | 208 local data, ret = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); |
| 209 if not data then | 209 if not data then |
| 210 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); | 210 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); |
| 211 if not mode then | 211 if not mode then |
| 212 log("debug", "Assuming empty "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 212 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
| 213 return nil; | 213 return nil; |
| 214 else -- file exists, but can't be read | 214 else -- file exists, but can't be read |
| 215 -- TODO more detailed error checking and logging? | 215 -- TODO more detailed error checking and logging? |
| 216 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 216 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
| 217 return nil, "Error reading storage"; | 217 return nil, "Error reading storage"; |
| 218 end | 218 end |
| 219 end | 219 end |
| 220 | 220 |
| 221 local success, ret = pcall(data); | 221 local success, ret = pcall(data); |
| 222 if not success then | 222 if not success then |
| 223 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 223 log("error", "Unable to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
| 224 return nil, "Error reading storage"; | 224 return nil, "Error reading storage"; |
| 225 end | 225 end |
| 226 return items; | 226 return items; |
| 227 end | 227 end |
| 228 | 228 |