Comparison

util/datamanager.lua @ 6992:0622f2820d1d

util.datamanager: Factor out code for appending bytes to a file
author Kim Alvefur <zash@zash.se>
date Fri, 11 Dec 2015 20:07:22 +0100
parent 6777:5de6b93d0190
child 6993:dc0c6b8dc638
comparison
equal deleted inserted replaced
6987:06696882d972 6992:0622f2820d1d
207 -- platform independent way of checking for non-exisitng files 207 -- platform independent way of checking for non-exisitng files
208 until ok; 208 until ok;
209 return true; 209 return true;
210 end 210 end
211 211
212 local function list_append(username, host, datastore, data) 212 local function append(username, host, datastore, ext, data)
213 if not data then return; end 213 local f, msg = io_open(getpath(username, host, datastore, ext, true), "r+");
214 if callback(username, host, datastore) == false then return true; end
215 -- save the datastore
216 local f, msg = io_open(getpath(username, host, datastore, "list", true), "r+");
217 if not f then 214 if not f then
218 f, msg = io_open(getpath(username, host, datastore, "list", true), "w"); 215 f, msg = io_open(getpath(username, host, datastore, ext, true), "w");
219 end 216 end
220 if not f then 217 if not f then
221 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); 218 return nil, msg;
222 return; 219 end
223 end
224 local data = "item(" .. serialize(data) .. ");\n";
225 local pos = f:seek("end"); 220 local pos = f:seek("end");
226 local ok, msg = fallocate(f, pos, #data); 221 local ok, msg = fallocate(f, pos, #data);
227 f:seek("set", pos); 222 f:seek("set", pos);
228 if ok then 223 if ok then
229 f:write(data); 224 f:write(data);
230 else 225 else
226 return ok, msg;
227 end
228 f:close();
229 return true;
230 end
231
232 local function list_append(username, host, datastore, data)
233 if not data then return; end
234 if callback(username, host, datastore) == false then return true; end
235 -- save the datastore
236
237 local data = "item(" .. serialize(data) .. ");\n";
238 local ok, msg = append(username, host, datastore, "list", data);
239 if not ok then
231 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); 240 log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil");
232 return ok, msg; 241 return ok, msg;
233 end 242 end
234 f:close();
235 return true; 243 return true;
236 end 244 end
237 245
238 local function list_store(username, host, datastore, data) 246 local function list_store(username, host, datastore, data)
239 if not data then 247 if not data then