[10/12] rules.pl: Check if an ipset db file exists before call to restore it.

Message ID 20220214184257.2406-10-stefan.schantl@ipfire.org
State Accepted
Commit 50e43059554a6a1c9ca8579b5347a9f98bc99ffb
Headers
Series [01/12] location-functions.pl: Rename and set the location for exported databases to "/var/lib/location/ipset/". |

Commit Message

Stefan Schantl Feb. 14, 2022, 6:42 p.m. UTC
  Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 config/firewall/rules.pl | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
  

Comments

Peter Müller Feb. 14, 2022, 9:06 p.m. UTC | #1
Reviewed-by: Peter Müller <peter.mueller@ipfire.org>

> Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
> ---
>  config/firewall/rules.pl | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/config/firewall/rules.pl b/config/firewall/rules.pl
> index 29990ee67..162781f7a 100644
> --- a/config/firewall/rules.pl
> +++ b/config/firewall/rules.pl
> @@ -927,6 +927,12 @@ sub firewall_is_in_subnet {
>  sub ipset_restore ($) {
>  	my ($ccode) = @_;
>  
> -	# Run ipset and restore the list of the given country code.
> -	run("$IPSET restore < $Location::Functions::ipset_db_directory/$ccode.ipset4");
> +	my $file_prefix = "ipset4";
> +	my $db_file = "$Location::Functions::ipset_db_directory/$ccode.$file_prefix";
> +
> +	# Check if the generated file exists.
> +	if (-f $db_file) {
> +		# Run ipset and restore the list of the given country code.
> +		run("$IPSET restore < $db_file");
> +	}
>  }
  
Michael Tremer Feb. 15, 2022, 12:38 p.m. UTC | #2
Reviewed-by: Michael Tremer <michael.tremer@ipfire.org>

> On 14 Feb 2022, at 21:06, Peter Müller <peter.mueller@ipfire.org> wrote:
> 
> Reviewed-by: Peter Müller <peter.mueller@ipfire.org>
> 
>> Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
>> ---
>> config/firewall/rules.pl | 10 ++++++++--
>> 1 file changed, 8 insertions(+), 2 deletions(-)
>> 
>> diff --git a/config/firewall/rules.pl b/config/firewall/rules.pl
>> index 29990ee67..162781f7a 100644
>> --- a/config/firewall/rules.pl
>> +++ b/config/firewall/rules.pl
>> @@ -927,6 +927,12 @@ sub firewall_is_in_subnet {
>> sub ipset_restore ($) {
>> 	my ($ccode) = @_;
>> 
>> -	# Run ipset and restore the list of the given country code.
>> -	run("$IPSET restore < $Location::Functions::ipset_db_directory/$ccode.ipset4");
>> +	my $file_prefix = "ipset4";
>> +	my $db_file = "$Location::Functions::ipset_db_directory/$ccode.$file_prefix";
>> +
>> +	# Check if the generated file exists.
>> +	if (-f $db_file) {
>> +		# Run ipset and restore the list of the given country code.
>> +		run("$IPSET restore < $db_file");
>> +	}
>> }
  

Patch

diff --git a/config/firewall/rules.pl b/config/firewall/rules.pl
index 29990ee67..162781f7a 100644
--- a/config/firewall/rules.pl
+++ b/config/firewall/rules.pl
@@ -927,6 +927,12 @@  sub firewall_is_in_subnet {
 sub ipset_restore ($) {
 	my ($ccode) = @_;
 
-	# Run ipset and restore the list of the given country code.
-	run("$IPSET restore < $Location::Functions::ipset_db_directory/$ccode.ipset4");
+	my $file_prefix = "ipset4";
+	my $db_file = "$Location::Functions::ipset_db_directory/$ccode.$file_prefix";
+
+	# Check if the generated file exists.
+	if (-f $db_file) {
+		# Run ipset and restore the list of the given country code.
+		run("$IPSET restore < $db_file");
+	}
 }