Software /
code /
prosody
Annotate
util/datamanager.lua @ 3327:b447682f2a8d
net.dns: Make timeout configurable (default 15s)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 05 Jul 2010 12:09:50 +0100 |
parent | 3108:df0df92dd58a |
child | 3722:f15b6ed967b7 |
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 |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
456
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; |
11 local setmetatable, type = setmetatable, type; | |
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
|
12 local pairs, ipairs = pairs, ipairs; |
0 | 13 local char = string.char; |
14 local loadfile, setfenv, pcall = loadfile, setfenv, 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; |
267 | 18 local tostring, tonumber = tostring, tonumber; |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
129
diff
changeset
|
19 local error = error; |
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; |
550
56bdfae9e4ea
Changed util.datamanager to use util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
22 local append = require "util.serialization".append; |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
23 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end |
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
|
24 local lfs = require "lfs"; |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
25 local raw_mkdir; |
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
26 |
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
27 if prosody.platform == "posix" then |
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
28 raw_mkdir = require "util.pposix".mkdir; -- Doesn't trample on umask |
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
29 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
|
30 raw_mkdir = lfs.mkdir; |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
31 end |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
32 |
0 | 33 module "datamanager" |
34 | |
35 ---- utils ----- | |
36 local encode, decode; | |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
37 do |
0 | 38 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); |
39 | |
40 decode = function (s) | |
41 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); | |
42 end | |
43 | |
44 encode = function (s) | |
628
3712d36b6d25
Fixed URL encoding to generate %0x instead of %x
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
45 return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); |
0 | 46 end |
47 end | |
48 | |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
49 local _mkdir = {}; |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
50 local function mkdir(path) |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
51 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
|
52 if not _mkdir[path] then |
2444
267d6482bac6
util.datamanager: Use pposix.mkdir if available
Matthew Wild <mwild1@gmail.com>
parents:
2328
diff
changeset
|
53 raw_mkdir(path); |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
54 _mkdir[path] = true; |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
55 end |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
56 return path; |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
57 end |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
58 |
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
59 local data_path = "data"; |
1381
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
60 local callbacks = {}; |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
61 |
0 | 62 ------- API ------------- |
63 | |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
64 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
|
65 log("debug", "Setting data path to: %s", path); |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
66 data_path = path; |
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
67 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
|
68 |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
69 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
|
70 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
|
71 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
|
72 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
|
73 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
74 |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
75 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
|
76 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
83 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
92 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
93 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
|
94 end |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
95 |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
96 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
|
97 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
|
98 host = (host and encode(host)) or "_global"; |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
99 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
|
100 if username then |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
101 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
|
102 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
|
103 elseif host then |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
104 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
|
105 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
|
106 else |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
107 if create then mkdir(data_path); end |
452
613c5c6bdce4
Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents:
267
diff
changeset
|
108 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
|
109 end |
0 | 110 end |
111 | |
112 function load(username, host, datastore) | |
113 local data, ret = loadfile(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
|
114 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
|
115 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
|
116 if not mode then |
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
|
117 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "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
|
118 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
|
119 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
|
120 -- TODO more detailed error checking and logging? |
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
|
121 log("error", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "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
|
122 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
|
123 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
|
124 end |
0 | 125 setfenv(data, {}); |
126 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
|
127 if not success then |
182
f5cb6b5a6eb7
Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents:
177
diff
changeset
|
128 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(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
|
129 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
|
130 end |
0 | 131 return ret; |
132 end | |
133 | |
134 function store(username, host, datastore, data) | |
206
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
135 if not data then |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
136 data = {}; |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
137 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
|
138 |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 end |
46a58df8557d
util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
Matthew Wild <mwild1@gmail.com>
parents:
1097
diff
changeset
|
143 |
206
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
144 -- save the datastore |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
145 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+"); |
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
|
146 if not f then |
182
f5cb6b5a6eb7
Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents:
177
diff
changeset
|
147 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(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
|
148 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
|
149 end |
0 | 150 f:write("return "); |
550
56bdfae9e4ea
Changed util.datamanager to use util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
151 append(f, data); |
0 | 152 f:close(); |
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
|
153 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
|
154 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
|
155 os_remove(getpath(username, host, datastore)); |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
156 end |
e30d0e30a0ff
Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents:
182
diff
changeset
|
157 -- 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
|
158 -- platform independent way of checking for non-exisitng files |
0 | 159 return true; |
160 end | |
161 | |
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
|
162 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
|
163 if not data then return; end |
1462
44780b856ce7
datamanager: Fixed incorrect callback result checking
Waqas Hussain <waqas20@gmail.com>
parents:
1381
diff
changeset
|
164 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
|
165 -- save the datastore |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
166 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
|
167 if not f 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
|
168 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); |
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
|
169 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
|
170 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
|
171 f:write("item("); |
550
56bdfae9e4ea
Changed util.datamanager to use util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
172 append(f, data); |
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
|
173 f:write(");\n"); |
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
|
174 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
|
175 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
|
176 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
|
177 |
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
|
178 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
|
179 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
|
180 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
|
181 end |
1462
44780b856ce7
datamanager: Fixed incorrect callback result checking
Waqas Hussain <waqas20@gmail.com>
parents:
1381
diff
changeset
|
182 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
|
183 -- save the datastore |
643
8ff454831f7d
Moved directory auto-creation to datamanager
Waqas Hussain <waqas20@gmail.com>
parents:
628
diff
changeset
|
184 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); |
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
|
185 if not f 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
|
186 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); |
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
|
187 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
|
188 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
|
189 for _, d in ipairs(data) do |
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
|
190 f:write("item("); |
550
56bdfae9e4ea
Changed util.datamanager to use util.serialization
Waqas Hussain <waqas20@gmail.com>
parents:
519
diff
changeset
|
191 append(f, d); |
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
|
192 f:write(");\n"); |
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
|
193 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
|
194 f:close(); |
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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 -- 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
|
200 -- 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
|
201 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
|
202 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
|
203 |
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
|
204 function list_load(username, host, datastore) |
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 local data, ret = loadfile(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
|
206 if not data then |
706
ce772e283992
datamanager: Change log message from type "warn" to "debug" when loading a file fails
Waqas Hussain <waqas20@gmail.com>
parents:
643
diff
changeset
|
207 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(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
|
208 return nil; |
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
|
209 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
|
210 local 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
|
211 setfenv(data, {item = function(i) t_insert(items, i); 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
|
212 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
|
213 if not success 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
|
214 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
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
|
215 return nil; |
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
|
216 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
|
217 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
|
218 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
|
219 |
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 _M; |