Software /
code /
prosody
Comparison
util/datamanager.lua @ 7009:61f5f8435e0b
Merge
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 16 Dec 2015 16:44:40 +0000 |
parent | 7002:9ab0d5e69c41 |
child | 7201:7a8cffafeff0 |
comparison
equal
deleted
inserted
replaced
7007:e28fbe6dd424 | 7009:61f5f8435e0b |
---|---|
207 -- platform independent way of checking for non-exisitng files | 207 -- platform independent way of checking for non-exisitng files |
208 until ok; | 208 until ok; |
209 return true; | 209 return true; |
210 end | 210 end |
211 | 211 |
212 -- Append a blob of data to a file | |
213 local function append(username, host, datastore, ext, data) | |
214 if type(data) ~= "string" then return; end | |
215 local filename = getpath(username, host, datastore, ext, true); | |
216 | |
217 local ok; | |
218 local f, msg = io_open(filename, "r+"); | |
219 if not f then | |
220 -- File did probably not exist, let's create it | |
221 f, msg = io_open(filename, "w"); | |
222 if not f then | |
223 return nil, msg, "open"; | |
224 end | |
225 end | |
226 | |
227 local pos = f:seek("end"); | |
228 ok, msg = fallocate(f, pos, #data); | |
229 if not ok then | |
230 log("warn", "fallocate() failed: %s", tostring(msg)); | |
231 -- This doesn't work on every file system | |
232 end | |
233 | |
234 if f:seek() ~= pos then | |
235 log("debug", "fallocate() changed file position"); | |
236 f:seek("set", pos); | |
237 end | |
238 | |
239 ok, msg = f:write(data); | |
240 if not ok then | |
241 f:close(); | |
242 return ok, msg, "write"; | |
243 end | |
244 | |
245 ok, msg = f:close(); | |
246 if not ok then | |
247 return ok, msg; | |
248 end | |
249 | |
250 return true, pos; | |
251 end | |
252 | |
212 local function list_append(username, host, datastore, data) | 253 local function list_append(username, host, datastore, data) |
213 if not data then return; end | 254 if not data then return; end |
214 if callback(username, host, datastore) == false then return true; end | 255 if callback(username, host, datastore) == false then return true; end |
215 -- save the datastore | 256 -- save the datastore |
216 local f, msg = io_open(getpath(username, host, datastore, "list", true), "r+"); | 257 |
217 if not f then | 258 data = "item(" .. serialize(data) .. ");\n"; |
218 f, msg = io_open(getpath(username, host, datastore, "list", true), "w"); | 259 local ok, msg = append(username, host, datastore, "list", data); |
219 end | 260 if not ok then |
220 if not f then | |
221 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); | |
222 return; | |
223 end | |
224 local data = "item(" .. serialize(data) .. ");\n"; | |
225 local pos = f:seek("end"); | |
226 local ok, msg = fallocate(f, pos, #data); | |
227 f:seek("set", pos); | |
228 if ok then | |
229 f:write(data); | |
230 else | |
231 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); | 261 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); |
232 return ok, msg; | 262 return ok, msg; |
233 end | 263 end |
234 f:close(); | |
235 return true; | 264 return true; |
236 end | 265 end |
237 | 266 |
238 local function list_store(username, host, datastore, data) | 267 local function list_store(username, host, datastore, data) |
239 if not data then | 268 if not data then |
371 add_callback = add_callback; | 400 add_callback = add_callback; |
372 remove_callback = remove_callback; | 401 remove_callback = remove_callback; |
373 getpath = getpath; | 402 getpath = getpath; |
374 load = load; | 403 load = load; |
375 store = store; | 404 store = store; |
405 append_raw = append; | |
376 list_append = list_append; | 406 list_append = list_append; |
377 list_store = list_store; | 407 list_store = list_store; |
378 list_load = list_load; | 408 list_load = list_load; |
379 users = users; | 409 users = users; |
380 stores = stores; | 410 stores = stores; |