util.datamanager: Explicit handling of each error condition (see #632)
authorKim Alvefur <zash@zash.se>
Sat, 27 Feb 2016 15:29:56 +0100
changeset 7205 5bf0ff3882aa
parent 7204 7a8cffafeff0
child 7206 be8b88ad35a3
util.datamanager: Explicit handling of each error condition (see #632)
util/datamanager.lua
--- a/util/datamanager.lua	Thu Feb 25 22:37:41 2016 +0100
+++ b/util/datamanager.lua	Sat Feb 27 15:29:56 2016 +0100
@@ -144,24 +144,26 @@
 local function atomic_store(filename, data)
 	local scratch = filename.."~";
 	local f, ok, msg;
-	repeat
-		f, msg = io_open(scratch, "w");
-		if not f then break end
 
-		ok, msg = f:write(data);
-		if not ok then break end
+	f, msg = io_open(scratch, "w");
+	if not f then
+		return nil, msg;
+	end
 
-		ok, msg = f:close();
-		f = nil; -- no longer valid
-		if not ok then break end
+	ok, msg = f:write(data);
+	if not ok then
+		f:close();
+		os_remove(scratch);
+		return nil, msg;
+	end
 
-		return os_rename(scratch, filename);
-	until false;
+	ok, msg = f:close();
+	if not ok then
+		os_remove(scratch);
+		return nil, msg;
+	end
 
-	-- Cleanup
-	if f then f:close(); end
-	os_remove(scratch);
-	return nil, msg;
+	return os_rename(scratch, filename);
 end
 
 if prosody and prosody.platform ~= "posix" then