util.datamanager: Explicit handling of each error condition (see #632)
authorKim Alvefur <zash@zash.se>
Sat, 27 Feb 2016 14:29:56 +0000 (15:29 +0100)
committerKim Alvefur <zash@zash.se>
Sat, 27 Feb 2016 14:29:56 +0000 (15:29 +0100)
util/datamanager.lua

index 07b2fe4aa8ad61a85c9d770bdb37fe4d183b9602..dc98bd13fd3bfd9a0dd0aa9f347dd181bc81a2c9 100644 (file)
@@ -144,24 +144,26 @@ end
 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