Diff

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
line wrap: on
line diff
--- 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