From mboxrd@z Thu Jan 1 00:00:00 1970 From: Michael Tremer To: development@lists.ipfire.org Subject: [PATCH 1/3] unbound-dhcp-leases-bridge: Implement atomic file replacement Date: Fri, 26 Apr 2024 15:09:17 +0000 Message-ID: <20240426150919.3766772-1-michael.tremer@ipfire.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============3328167891573536557==" List-Id: --===============3328167891573536557== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This change no longer renames the file, but removes the old link and creates a new link for the temporary file. That helps us to jump out of the code at any point without worrying about cleaning up the temporary file. Signed-off-by: Michael Tremer --- config/unbound/unbound-dhcp-leases-bridge | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbou= nd-dhcp-leases-bridge index e9f022aff..5d5696af0 100644 --- a/config/unbound/unbound-dhcp-leases-bridge +++ b/config/unbound/unbound-dhcp-leases-bridge @@ -526,16 +526,22 @@ class UnboundConfigWriter(object): def write_dhcp_leases(self, leases): log.debug("Writing DHCP leases...") =20 - with tempfile.NamedTemporaryFile(mode=3D"w", delete=3DFalse) as f: + with tempfile.NamedTemporaryFile(mode=3D"w") as f: for l in leases: for rr in l.rrset: f.write("local-data: \"%s\"\n" % " ".join(rr)) =20 + # 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) =20 + # 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) =20 def _control(self, *args): command =3D ["unbound-control"] --=20 2.39.2 --===============3328167891573536557==--