Software /
code /
prosody
Comparison
util/datamanager.lua @ 2626:39a2e8ff0b0f
Backed out changeset cb1f9266130b: better way to achieve this to be committed Real Soon Now.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 13 Feb 2010 15:34:22 +0000 |
parent | 2583:cb1f9266130b |
child | 2925:692b3c6c5bd2 |
comparison
equal
deleted
inserted
replaced
2583:cb1f9266130b | 2626:39a2e8ff0b0f |
---|---|
58 local data_path = "data"; | 58 local data_path = "data"; |
59 local callbacks = {}; | 59 local callbacks = {}; |
60 | 60 |
61 ------- API ------------- | 61 ------- API ------------- |
62 | 62 |
63 local _set_data_path; | |
64 function set_data_path(path) | 63 function set_data_path(path) |
65 if _set_data_path then return _set_data_path(path); end | |
66 log("debug", "Setting data path to: %s", path); | 64 log("debug", "Setting data path to: %s", path); |
67 data_path = path; | 65 data_path = path; |
68 end | 66 end |
69 | 67 |
70 local function callback(username, host, datastore, data) | 68 local function callback(username, host, datastore, data) |
73 if username == false then break; end | 71 if username == false then break; end |
74 end | 72 end |
75 | 73 |
76 return username, host, datastore, data; | 74 return username, host, datastore, data; |
77 end | 75 end |
78 local _add_callback; | |
79 function add_callback(func) | 76 function add_callback(func) |
80 if _add_callback then return _add_callback(func); end | |
81 if not callbacks[func] then -- Would you really want to set the same callback more than once? | 77 if not callbacks[func] then -- Would you really want to set the same callback more than once? |
82 callbacks[func] = true; | 78 callbacks[func] = true; |
83 callbacks[#callbacks+1] = func; | 79 callbacks[#callbacks+1] = func; |
84 return true; | 80 return true; |
85 end | 81 end |
86 end | 82 end |
87 local _remove_callback; | |
88 function remove_callback(func) | 83 function remove_callback(func) |
89 if _remove_callback then return _remove_callback(func); end | |
90 if callbacks[func] then | 84 if callbacks[func] then |
91 for i, f in ipairs(callbacks) do | 85 for i, f in ipairs(callbacks) do |
92 if f == func then | 86 if f == func then |
93 callbacks[i] = nil; | 87 callbacks[i] = nil; |
94 callbacks[f] = nil; | 88 callbacks[f] = nil; |
96 end | 90 end |
97 end | 91 end |
98 end | 92 end |
99 end | 93 end |
100 | 94 |
101 local _getpath; | |
102 function getpath(username, host, datastore, ext, create) | 95 function getpath(username, host, datastore, ext, create) |
103 if _getpath then return _getpath(username, host, datastore, ext, create); end | |
104 ext = ext or "dat"; | 96 ext = ext or "dat"; |
105 host = (host and encode(host)) or "_global"; | 97 host = (host and encode(host)) or "_global"; |
106 username = username and encode(username); | 98 username = username and encode(username); |
107 if username then | 99 if username then |
108 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end | 100 if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end |
114 if create then mkdir(data_path); end | 106 if create then mkdir(data_path); end |
115 return format("%s/%s.%s", data_path, datastore, ext); | 107 return format("%s/%s.%s", data_path, datastore, ext); |
116 end | 108 end |
117 end | 109 end |
118 | 110 |
119 local _load; | |
120 function load(username, host, datastore) | 111 function load(username, host, datastore) |
121 if _load then return _load(username, host, datastore); end | |
122 local data, ret = loadfile(getpath(username, host, datastore)); | 112 local data, ret = loadfile(getpath(username, host, datastore)); |
123 if not data then | 113 if not data then |
124 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 114 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
125 return nil; | 115 return nil; |
126 end | 116 end |
131 return nil; | 121 return nil; |
132 end | 122 end |
133 return ret; | 123 return ret; |
134 end | 124 end |
135 | 125 |
136 local _store; | |
137 function store(username, host, datastore, data) | 126 function store(username, host, datastore, data) |
138 if _store then return _store(username, host, datastore, data); end | |
139 if not data then | 127 if not data then |
140 data = {}; | 128 data = {}; |
141 end | 129 end |
142 | 130 |
143 username, host, datastore, data = callback(username, host, datastore, data); | 131 username, host, datastore, data = callback(username, host, datastore, data); |
161 -- we write data even when we are deleting because lua doesn't have a | 149 -- we write data even when we are deleting because lua doesn't have a |
162 -- platform independent way of checking for non-exisitng files | 150 -- platform independent way of checking for non-exisitng files |
163 return true; | 151 return true; |
164 end | 152 end |
165 | 153 |
166 local _list_append; | |
167 function list_append(username, host, datastore, data) | 154 function list_append(username, host, datastore, data) |
168 if _list_append then return _list_append(username, host, datastore, data); end | |
169 if not data then return; end | 155 if not data then return; end |
170 if callback(username, host, datastore) == false then return true; end | 156 if callback(username, host, datastore) == false then return true; end |
171 -- save the datastore | 157 -- save the datastore |
172 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); | 158 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); |
173 if not f then | 159 if not f then |
179 f:write(");\n"); | 165 f:write(");\n"); |
180 f:close(); | 166 f:close(); |
181 return true; | 167 return true; |
182 end | 168 end |
183 | 169 |
184 local _list_store; | |
185 function list_store(username, host, datastore, data) | 170 function list_store(username, host, datastore, data) |
186 if _list_store then return _list_store(username, host, datastore, data); end | |
187 if not data then | 171 if not data then |
188 data = {}; | 172 data = {}; |
189 end | 173 end |
190 if callback(username, host, datastore) == false then return true; end | 174 if callback(username, host, datastore) == false then return true; end |
191 -- save the datastore | 175 -- save the datastore |
207 -- we write data even when we are deleting because lua doesn't have a | 191 -- we write data even when we are deleting because lua doesn't have a |
208 -- platform independent way of checking for non-exisitng files | 192 -- platform independent way of checking for non-exisitng files |
209 return true; | 193 return true; |
210 end | 194 end |
211 | 195 |
212 local _list_load; | |
213 function list_load(username, host, datastore) | 196 function list_load(username, host, datastore) |
214 if _list_load then return _list_load(username, host, datastore); end | |
215 local data, ret = loadfile(getpath(username, host, datastore, "list")); | 197 local data, ret = loadfile(getpath(username, host, datastore, "list")); |
216 if not data then | 198 if not data then |
217 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); | 199 log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil")); |
218 return nil; | 200 return nil; |
219 end | 201 end |
225 return nil; | 207 return nil; |
226 end | 208 end |
227 return items; | 209 return items; |
228 end | 210 end |
229 | 211 |
230 function set(t) | |
231 _set_data_path = t.set_data_path; | |
232 _add_callback = t.add_callback; | |
233 _remove_callback = t.remove_callback; | |
234 _getpath = t.getpath; | |
235 _load = t.load; | |
236 _store = t.store; | |
237 _list_append = t.list_append; | |
238 _list_store = t.list_store; | |
239 _list_load = t.list_load; | |
240 end | |
241 function get() | |
242 return { | |
243 set_data_path = _set_data_path; | |
244 add_callback = _add_callback; | |
245 remove_callback = _remove_callback; | |
246 getpath = _getpath; | |
247 load = _load; | |
248 store = _store; | |
249 list_append = _list_append; | |
250 list_store = _list_store; | |
251 list_load = _list_load; | |
252 }; | |
253 end | |
254 | |
255 return _M; | 212 return _M; |