X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fdatamanager.lua;h=83f3dd13042d7607c8d5c2144a18f7512b4ec641;hb=6d501b1f0624c4829a787898181bf8a47dc47b35;hp=4a371d2ce7652360ac2be1b43875e2f3c16eb788;hpb=bba1c46fc80d9c6fc5fc10a1d64e018a1b79c4b8;p=prosody.git diff --git a/util/datamanager.lua b/util/datamanager.lua index 4a371d2c..83f3dd13 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -209,22 +209,37 @@ local function store(username, host, datastore, data) return true; end +-- Append a blob of data to a file local function append(username, host, datastore, ext, data) + if type(data) ~= "string" then return; end local filename = getpath(username, host, datastore, ext, true); + + local ok; local f, msg = io_open(filename, "r+"); if not f then + -- File did probably not exist, let's create it f, msg = io_open(filename, "w"); + if not f then + return nil, msg, "open"; + end end - if not f then - return nil, msg; - end + 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; + ok, msg = fallocate(f, pos, #data); + if not ok then + log("warn", "fallocate() failed: %s", tostring(msg)); + -- This doesn't work on every file system + end + + if f:seek() ~= pos then + log("debug", "fallocate() changed file position"); + f:seek("set", pos); + end + + ok, msg = f:write(data); + if not ok then + f:close(); + return ok, msg, "write"; end ok, msg = f:close(); @@ -232,7 +247,7 @@ local function append(username, host, datastore, ext, data) return ok, msg; end - return true; + return true, pos; end local function list_append(username, host, datastore, data) @@ -387,6 +402,7 @@ return { getpath = getpath; load = load; store = store; + append_raw = append; list_append = list_append; list_store = list_store; list_load = list_load;