# HG changeset patch # User Kim Alvefur # Date 1449860842 -3600 # Node ID 0622f2820d1dbf12ea5eb6f8cca23ab7e85d542b # Parent 06696882d972d997d88d531eacdd971f28876276 util.datamanager: Factor out code for appending bytes to a file diff -r 06696882d972 -r 0622f2820d1d util/datamanager.lua --- a/util/datamanager.lua Thu Dec 10 18:00:08 2015 +0000 +++ b/util/datamanager.lua Fri Dec 11 20:07:22 2015 +0100 @@ -209,29 +209,37 @@ return true; end -local function list_append(username, host, datastore, data) - if not data then return; end - if callback(username, host, datastore) == false then return true; end - -- save the datastore - local f, msg = io_open(getpath(username, host, datastore, "list", true), "r+"); +local function append(username, host, datastore, ext, data) + local f, msg = io_open(getpath(username, host, datastore, ext, true), "r+"); if not f then - f, msg = io_open(getpath(username, host, datastore, "list", true), "w"); + f, msg = io_open(getpath(username, host, datastore, ext, true), "w"); end if not f then - log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); - return; + return nil, msg; end - local data = "item(" .. serialize(data) .. ");\n"; local pos = f:seek("end"); local ok, msg = fallocate(f, pos, #data); f:seek("set", pos); if ok then f:write(data); else + return ok, msg; + end + f:close(); + return true; +end + +local function list_append(username, host, datastore, data) + if not data then return; end + if callback(username, host, datastore) == false then return true; end + -- save the datastore + + local data = "item(" .. serialize(data) .. ");\n"; + local ok, msg = append(username, host, datastore, "list", data); + if not ok then log("error", "Unable to write to %s storage ('%s') for user: %s@%s", datastore, msg, username or "nil", host or "nil"); return ok, msg; end - f:close(); return true; end