[3/3] unbound-dhcp-leases-bridge: Make comparison work if old file does not exist

Message ID 20240426150919.3766772-3-michael.tremer@ipfire.org
State Staged
Commit 4bf50efa84559278b06c158105247d51c3c0212f
Headers
Series [1/3] unbound-dhcp-leases-bridge: Implement atomic file replacement |

Commit Message

Michael Tremer April 26, 2024, 3:09 p.m. UTC
  This patch catches any errors if the file did not previously exist and
therefore skips the comparison.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 config/unbound/unbound-dhcp-leases-bridge | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
  

Comments

Nick Howitt April 30, 2024, 10:41 a.m. UTC | #1
On 26/04/2024 16:09, Michael Tremer wrote:
> This patch catches any errors if the file did not previously exist and
> therefore skips the comparison.
>
> Signed-off-by: Michael Tremer<michael.tremer@ipfire.org>
> ---
>   config/unbound/unbound-dhcp-leases-bridge | 17 +++++++++++------
>   1 file changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge
> index 80c8267e8..7f89f620a 100644
> --- a/config/unbound/unbound-dhcp-leases-bridge
> +++ b/config/unbound/unbound-dhcp-leases-bridge
> @@ -535,17 +535,22 @@ class UnboundConfigWriter(object):
>   			f.flush()
>   
>   			# Compare if the new leases file has changed from the previous version
> -			if filecmp.cmp(f.name, self.path, shallow=False):
> -				log.debug("The generated leases file has not changed")
> +			try:
> +				if filecmp.cmp(f.name, self.path, shallow=False):
> +					log.debug("The generated leases file has not changed")
>   
> -				return False
> +					return False
> +
> +				# Remove the old file
> +				os.unlink(self.path)
> +
> +			# If the previous file did not exist, just keep falling through
> +			except FileNotFoundError:
> +				pass
>   
>   			# 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.link(f.name, self.path)
>   
|Signed-off-by: Nick Howitt <nick@howitts.co.uk>|
  

Patch

diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge
index 80c8267e8..7f89f620a 100644
--- a/config/unbound/unbound-dhcp-leases-bridge
+++ b/config/unbound/unbound-dhcp-leases-bridge
@@ -535,17 +535,22 @@  class UnboundConfigWriter(object):
 			f.flush()
 
 			# Compare if the new leases file has changed from the previous version
-			if filecmp.cmp(f.name, self.path, shallow=False):
-				log.debug("The generated leases file has not changed")
+			try:
+				if filecmp.cmp(f.name, self.path, shallow=False):
+					log.debug("The generated leases file has not changed")
 
-				return False
+					return False
+
+				# Remove the old file
+				os.unlink(self.path)
+
+			# If the previous file did not exist, just keep falling through
+			except FileNotFoundError:
+				pass
 
 			# 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.link(f.name, self.path)