Software /
code /
prosody
Annotate
util/datamanager.lua @ 5057:1e44678b5de2
util.datamanager: Add missing mode flag to seek call
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 31 Jul 2012 08:23:55 +0200 |
parent | 5051:71253db26fda |
child | 5065:acfaf771f10e |
child | 5066:4be7093edde9 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1462
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2159
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2159
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
4452
7de17ca4de14
util/datamanager: Use package.config to figure out directory seperator
James Callahan <james@chatid.com>
parents:
4112
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
456
diff
changeset
|
7 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
456
diff
changeset
|
8 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
456
diff
changeset
|
9 |
0 | 10 local format = string.format; |
5051
71253db26fda
util.datamanager: Remove a few unused imports
Kim Alvefur <zash@zash.se>
parents:
5049
diff
changeset
|
11 local setmetatable = setmetatable; |
71253db26fda
util.datamanager: Remove a few unused imports
Kim Alvefur <zash@zash.se>
parents:
5049
diff
changeset
|
12 local ipairs = ipairs; |
0 | 13 local char = string.char; |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4452
diff
changeset
|
14 local pcall = pcall; |
456
27cb85d4059e
Fixed logging in datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
452
diff
changeset
|
15 local log = require "util.logger".init("datamanager"); |
0 | 16 local io_open = io.open; |
206
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
17 local os_remove = os.remove; |
5049
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
18 local os_rename = os.rename; |
5051
71253db26fda
util.datamanager: Remove a few unused imports
Kim Alvefur <zash@zash.se>
parents:
5049
diff
changeset
|
19 local tonumber = tonumber; |
206
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
20 local next = next; |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
21 local t_insert = table.insert; |
5049
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
22 local t_concat = table.concat; |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4452
diff
changeset
|
23 local envloadfile = require"util.envload".envloadfile; |
5045
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
24 local serialize = require "util.serialization".serialize; |
4452
7de17ca4de14
util/datamanager: Use package.config to figure out directory seperator
James Callahan <james@chatid.com>
parents:
4112
diff
changeset
|
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) |
3086
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
26 local lfs = require "lfs"; |
4093
36555949bd16
util.datamanager: Use prosody.paths.data as the initial value for data_path
Matthew Wild <mwild1@gmail.com>
parents:
3723
diff
changeset
|
27 local prosody = prosody; |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
28 local raw_mkdir; |
5045
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
29 local fallocate; |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
30 |
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
31 if prosody.platform == "posix" then |
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
32 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask |
5045
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
33 fallocate = require "util.pposix".fallocate; |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
34 else |
3086
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
35 raw_mkdir = lfs.mkdir; |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
36 end |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
37 |
5045
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
38 if not fallocate then -- Fallback |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
39 function fallocate(f, offset, len) |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
40 -- This assumes that current position == offset |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
41 local fake_data = (" "):rep(len); |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
42 local ok, msg = f:write(fake_data); |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
43 if not ok then |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
44 return ok, msg; |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
45 end |
5057
1e44678b5de2
util.datamanager: Add missing mode flag to seek call
Kim Alvefur <zash@zash.se>
parents:
5051
diff
changeset
|
46 f:seek("set", offset); |
5045
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
47 return true; |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
48 end |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
49 end |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
50 |
0 | 51 module "datamanager" |
52 | |
53 ---- utils ----- | |
54 local encode, decode; | |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
55 do |
0 | 56 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); |
57 | |
58 decode = function (s) | |
59 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); | |
60 end | |
61 | |
62 encode = function (s) | |
628
3712d36b6d25
Fixed URL encoding to generate %0x instead of %x
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
63 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); |
0 | 64 end |
65 end | |
66 | |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
67 local _mkdir = {}; |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
68 local function mkdir(path) |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
69 path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
70 if not _mkdir[path] then |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
71 raw_mkdir(path); |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
72 _mkdir[path] = true; |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
73 end |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
74 return path; |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
75 end |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
76 |
4108
e3e3aa286334
util.datamanager: Handle gracefully the lack of prosody.paths.data
Matthew Wild <mwild1@gmail.com>
parents:
4093
diff
changeset
|
77 local data_path = (prosody and prosody.paths and prosody.paths.data) or "."; |
1381
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
78 local callbacks = {}; |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
79 |
0 | 80 ------- API ------------- |
81 | |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
82 function set_data_path(path) |
1097
c5b33640a5f0
util.datamanager: Lower log level of 'Setting data path' to 'debug'
Matthew Wild <mwild1@gmail.com>
parents:
932
diff
changeset
|
83 log("debug", "Setting data path to: %s", path); |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
84 data_path = path; |
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
85 end |
1381
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
86 |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
87 local function callback(username, host, datastore, data) |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
88 for _, f in ipairs(callbacks) do |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
89 username, host, datastore, data = f(username, host, datastore, data); |
1462
44780b856ce7
datamanager: Fixed incorrect callback result checking
Waqas Hussain <waqas20@gmail.com>
parents:
1381
diff
changeset
|
90 if username == false then break; end |
1381
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
91 end |
4452
7de17ca4de14
util/datamanager: Use package.config to figure out directory seperator
James Callahan <james@chatid.com>
parents:
4112
diff
changeset
|
92 |
1381
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
93 return username, host, datastore, data; |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
94 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
95 function add_callback(func) |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
96 if not callbacks[func] then -- Would you really want to set the same callback more than once? |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
97 callbacks[func] = true; |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
98 callbacks[#callbacks+1] = func; |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
99 return true; |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
100 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
101 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
102 function remove_callback(func) |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
103 if callbacks[func] then |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
104 for i, f in ipairs(callbacks) do |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
105 if f == func then |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
106 callbacks[i] = nil; |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
107 callbacks[f] = nil; |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
108 return true; |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
109 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
110 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
111 end |
932
1ed3e5fe165a
Added: datamanager: Allow a callback to be installed which selectively prevents disk writes
Waqas Hussain <waqas20@gmail.com>
parents:
915
diff
changeset
|
112 end |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
113 |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
114 function getpath(username, host, datastore, ext, create) |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
115 ext = ext or "dat"; |
2328
2804d81206d8
util.datamanager: Store data stores with no host in '_global' folder
Matthew Wild <mwild1@gmail.com>
parents:
2159
diff
changeset
|
116 host = (host and encode(host)) or "_global"; |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
117 username = username and encode(username); |
84
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
118 if username then |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
119 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
120 return format("%s/%s/%s/%s.%s", data_path, host, datastore, username, ext); |
84
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
121 elseif host then |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
122 if create then mkdir(mkdir(data_path).."/"..host); end |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
123 return format("%s/%s/%s.%s", data_path, host, datastore, ext); |
84
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
124 else |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
125 if create then mkdir(data_path); end |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
126 return format("%s/%s.%s", data_path, datastore, ext); |
84
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
127 end |
0 | 128 end |
129 | |
130 function load(username, host, datastore) | |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4452
diff
changeset
|
131 local data, ret = envloadfile(getpath(username, host, datastore), {}); |
117
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
132 if not data then |
3086
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
133 local mode = lfs.attributes(getpath(username, host, datastore), "mode"); |
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
134 if not mode then |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
135 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
3086
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
136 return nil; |
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
137 else -- file exists, but can't be read |
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
138 -- TODO more detailed error checking and logging? |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
139 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
3086
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
140 return nil, "Error reading storage"; |
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
141 end |
117
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
142 end |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4452
diff
changeset
|
143 |
0 | 144 local success, ret = pcall(data); |
117
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
145 if not success then |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
146 log("error", "Unable to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
3086
931acb1188b1
util.datamanager: When failing to load a file, and the file exists, return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
147 return nil, "Error reading storage"; |
117
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
148 end |
0 | 149 return ret; |
150 end | |
151 | |
5049
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
152 local function atomic_store(filename, data) |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
153 local scratch = filename.."~"; |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
154 local f, ok, msg; |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
155 repeat |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
156 f, msg = io_open(scratch, "w"); |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
157 if not f then break end |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
158 |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
159 ok, msg = f:write(data); |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
160 if not ok then break end |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
161 |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
162 ok, msg = f:close(); |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
163 if not ok then break end |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
164 |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
165 return os_rename(scratch, filename); |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
166 until false; |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
167 |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
168 -- Cleanup |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
169 if f then f:close(); end |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
170 os_remove(scratch); |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
171 return nil, msg; |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
172 end |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
173 |
0 | 174 function store(username, host, datastore, data) |
206
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
175 if not data then |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
176 data = {}; |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
177 end |
1381
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
178 |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
179 username, host, datastore, data = callback(username, host, datastore, data); |
1462
44780b856ce7
datamanager: Fixed incorrect callback result checking
Waqas Hussain <waqas20@gmail.com>
parents:
1381
diff
changeset
|
180 if username == false then |
1381
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
181 return true; -- Don't save this data at all |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
182 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
183 |
206
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
184 -- save the datastore |
5049
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
185 local d = "return " .. serialize(data) .. ";\n"; |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
186 local ok, msg = atomic_store(getpath(username, host, datastore, nil, true), d); |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
187 if not ok then |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
188 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); |
3108
df0df92dd58a
util.datamanager: Return an error string when failing to save.
Waqas Hussain <waqas20@gmail.com>
parents:
3086
diff
changeset
|
189 return nil, "Error saving to storage"; |
117
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
190 end |
915
0fe5bf7ab81d
util.datamanager: Don't delete data when first entry in table is 'false'. My favourite bug so far.
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
191 if next(data) == nil then -- try to delete empty datastore |
1732
f1282fad2f99
datamanager: Fixed logging errors on deletion of datastores not owned by a user@host
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
192 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil"); |
206
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
193 os_remove(getpath(username, host, datastore)); |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
194 end |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
195 -- we write data even when we are deleting because lua doesn't have a |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
196 -- platform independent way of checking for non-exisitng files |
0 | 197 return true; |
198 end | |
199 | |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
200 function list_append(username, host, datastore, data) |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
201 if not data then return; end |
1462
44780b856ce7
datamanager: Fixed incorrect callback result checking
Waqas Hussain <waqas20@gmail.com>
parents:
1381
diff
changeset
|
202 if callback(username, host, datastore) == false then return true; end |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
203 -- save the datastore |
5045
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
204 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a"); |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
205 if not f then |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
206 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
207 return; |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
208 end |
5045
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
209 local data = "item(" .. serialize(data) .. ");\n"; |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
210 local pos = f:seek("end"); |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
211 local ok, msg = fallocate(f, pos, #data); |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
212 f:seek("set", pos); |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
213 if ok then |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
214 f:write(data); |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
215 else |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
216 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
217 return ok, msg; |
4ba6940deed0
util.datamanager: Use pposix.fallocate() to make sure appends succeed. Also add a fallback fallocate()
Kim Alvefur <zash@zash.se>
parents:
5038
diff
changeset
|
218 end |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
219 f:close(); |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
220 return true; |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
221 end |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
222 |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
223 function list_store(username, host, datastore, data) |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
224 if not data then |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
225 data = {}; |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
226 end |
1462
44780b856ce7
datamanager: Fixed incorrect callback result checking
Waqas Hussain <waqas20@gmail.com>
parents:
1381
diff
changeset
|
227 if callback(username, host, datastore) == false then return true; end |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
228 -- save the datastore |
5049
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
229 local d = {}; |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
230 for _, item in ipairs(data) do |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
231 d[#d+1] = "item(" .. serialize(item) .. ");\n"; |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
232 end |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
233 local ok, msg = atomic_store(getpath(username, host, datastore, "list", true), t_concat(d)); |
5d685f123332
util.datamanager: Write to a temporary file and atomically move it into place
Kim Alvefur <zash@zash.se>
parents:
5045
diff
changeset
|
234 if not ok then |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
235 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
236 return; |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
237 end |
915
0fe5bf7ab81d
util.datamanager: Don't delete data when first entry in table is 'false'. My favourite bug so far.
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
238 if next(data) == nil then -- try to delete empty datastore |
1732
f1282fad2f99
datamanager: Fixed logging errors on deletion of datastores not owned by a user@host
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
239 log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil"); |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
240 os_remove(getpath(username, host, datastore, "list")); |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
241 end |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
242 -- we write data even when we are deleting because lua doesn't have a |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
243 -- platform independent way of checking for non-exisitng files |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
244 return true; |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
245 end |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
246 |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
247 function list_load(username, host, datastore) |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4452
diff
changeset
|
248 local items = {}; |
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4452
diff
changeset
|
249 local data, ret = envloadfile(getpath(username, host, datastore, "list"), {item = function(i) t_insert(items, i); end}); |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
250 if not data then |
3722
f15b6ed967b7
util.datamanager: When failing to load a list file, and the file exists, log an error, and return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
3108
diff
changeset
|
251 local mode = lfs.attributes(getpath(username, host, datastore, "list"), "mode"); |
f15b6ed967b7
util.datamanager: When failing to load a list file, and the file exists, log an error, and return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
3108
diff
changeset
|
252 if not mode then |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
253 log("debug", "Assuming empty %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
3722
f15b6ed967b7
util.datamanager: When failing to load a list file, and the file exists, log an error, and return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
3108
diff
changeset
|
254 return nil; |
f15b6ed967b7
util.datamanager: When failing to load a list file, and the file exists, log an error, and return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
3108
diff
changeset
|
255 else -- file exists, but can't be read |
f15b6ed967b7
util.datamanager: When failing to load a list file, and the file exists, log an error, and return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
3108
diff
changeset
|
256 -- TODO more detailed error checking and logging? |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
257 log("error", "Failed to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
3722
f15b6ed967b7
util.datamanager: When failing to load a list file, and the file exists, log an error, and return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
3108
diff
changeset
|
258 return nil, "Error reading storage"; |
f15b6ed967b7
util.datamanager: When failing to load a list file, and the file exists, log an error, and return nil, error.
Waqas Hussain <waqas20@gmail.com>
parents:
3108
diff
changeset
|
259 end |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
260 end |
5021
85b2689dbcfe
Eliminate direct setfenv usage
Florian Zeitz <florob@babelmonkeys.de>
parents:
4452
diff
changeset
|
261 |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
262 local success, ret = pcall(data); |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
263 if not success then |
4993
5243b74a4cbb
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents:
4452
diff
changeset
|
264 log("error", "Unable to load %s storage ('%s') for user: %s@%s", datastore, ret, username or "nil", host or "nil"); |
3723
72a917d910a4
util.datamanager: Return an error string when pcall fails on a loaded list file.
Waqas Hussain <waqas20@gmail.com>
parents:
3722
diff
changeset
|
265 return nil, "Error reading storage"; |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
266 end |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
267 return items; |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
268 end |
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
269 |
5032
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
270 function list_stores(username, host) |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
271 if not host then |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
272 return nil, "bad argument #2 to 'list_stores' (string expected, got nothing)"; |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
273 end |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
274 local list = {}; |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
275 local host_dir = format("%s/%s/", data_path, encode(host)); |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
276 for node in lfs.dir(host_dir) do |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
277 if not node:match"^%." then -- dots should be encoded, this is probably . or .. |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
278 local store = decode(node); |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
279 local path = host_dir..node; |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
280 if username == true then |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
281 if lfs.attributes(path, "mode") == "directory" then |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
282 list[#list+1] = store; |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
283 end |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
284 elseif username then |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
285 if lfs.attributes(getpath(username, host, store), "mode") |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
286 or lfs.attributes(getpath(username, host, store, "list"), "mode") then |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
287 list[#list+1] = store; |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
288 end |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
289 elseif lfs.attributes(path, "mode") == "file" then |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
290 list[#list+1] = store:gsub("%.[dalist]+$",""); |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
291 end |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
292 end |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
293 end |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
294 return list; |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
295 end |
c40ea227f8af
util.datamanager: Add function for listing stores
Kim Alvefur <zash@zash.se>
parents:
5024
diff
changeset
|
296 |
5038
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
297 function purge(username, host) |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
298 local host_dir = format("%s/%s/", data_path, encode(host)); |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
299 local deleted = 0; |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
300 for file in lfs.dir(host_dir) do |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
301 if lfs.attributes(host_dir..file, "mode") == "directory" then |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
302 local store = decode(file); |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
303 deleted = deleted + (os_remove(getpath(username, host, store)) and 1 or 0); |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
304 deleted = deleted + (os_remove(getpath(username, host, store, "list")) and 1 or 0); |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
305 -- We this will generate loads of "No such file or directory", but do we care? |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
306 end |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
307 end |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
308 return deleted > 0, deleted; |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
309 end |
242c62ff8e77
util.datamanager: Add function for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents:
5032
diff
changeset
|
310 |
247
681b29aa134f
Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents:
206
diff
changeset
|
311 return _M; |