]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - config/unbound/unbound-dhcp-leases-bridge
unbound-dhcp-leases-bridge: Implement atomic file replacement
[ipfire-2.x.git] / config / unbound / unbound-dhcp-leases-bridge
index e9f022affa98a2483ca1f41d267fe443b0f048cf..5d5696af0c50669ea5cfd2179fa7f5a3ad7cbf3b 100644 (file)
@@ -526,16 +526,22 @@ class UnboundConfigWriter(object):
        def write_dhcp_leases(self, leases):
                log.debug("Writing DHCP leases...")
 
-               with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:
+               with tempfile.NamedTemporaryFile(mode="w") as f:
                        for l in leases:
                                for rr in l.rrset:
                                        f.write("local-data: \"%s\"\n" % " ".join(rr))
 
+                       # Flush the file
+                       f.flush()
+
                        # Make file readable for everyone
                        os.fchmod(f.fileno(), stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IROTH)
 
+                       # Remove the old file
+                       os.unlink(self.path)
+
                        # Move the file to its destination
-                       os.rename(f.name, self.path)
+                       os.link(f.name, self.path)
 
        def _control(self, *args):
                command = ["unbound-control"]