util.datamanager: Factor out code for appending bytes to a file
authorKim Alvefur <zash@zash.se>
Fri, 11 Dec 2015 19:07:22 +0000 (20:07 +0100)
committerKim Alvefur <zash@zash.se>
Fri, 11 Dec 2015 19:07:22 +0000 (20:07 +0100)
util/datamanager.lua

index 4b722851233db27c4776469716a702e491e843bb..13aed78f7ce894ebc5d832290b6b2de76b87988e 100644 (file)
@@ -209,32 +209,40 @@ local function store(username, host, datastore, data)
        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
-               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
 
+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
+       return true;
+end
+
 local function list_store(username, host, datastore, data)
        if not data then
                data = {};