[PATCHv2,07/12] extrahd.cgi: Re-order sanity check logic

Message ID 20230801154839.2373-7-stefan.schantl@ipfire.org
State Staged
Commit 7907c1e068d62a5bcb3275e35aecf678be50b0be
Headers
Series [PATCHv2,01/12] extrahd.cgi: Add various perl functions deal with block devices |

Commit Message

Stefan Schantl Aug. 1, 2023, 3:48 p.m. UTC
  Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 html/cgi-bin/extrahd.cgi | 49 +++++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 23 deletions(-)
  

Comments

Bernhard Bitsch Aug. 2, 2023, 11:57 a.m. UTC | #1
Reviewed-by: Bernhard Bitsch <bbitsch@ipfire.org>


Am 01.08.2023 um 17:48 schrieb Stefan Schantl:
> Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
> ---
>   html/cgi-bin/extrahd.cgi | 49 +++++++++++++++++++++-------------------
>   1 file changed, 26 insertions(+), 23 deletions(-)
> 
> diff --git a/html/cgi-bin/extrahd.cgi b/html/cgi-bin/extrahd.cgi
> index bb84ef77c..de5f26d0e 100644
> --- a/html/cgi-bin/extrahd.cgi
> +++ b/html/cgi-bin/extrahd.cgi
> @@ -98,28 +98,7 @@ $extrahdsettings{'UUID'} = '';
>   ## Add a new device.
>   #
>   if ($extrahdsettings{'ACTION'} eq $Lang::tr{'add'}) {
> -	# Open device file for reading.
> -	open( FILE, "< $devicefile" ) or die "Unable to read $devicefile";
> -	my @devices = <FILE>;
> -	close FILE;
> -
> -	# Loop through the entries line-by-line.
> -	foreach my $entry (sort @devices) {
> -		# Split the line into pieces and assign nice variables.
> -		my ($uuid, $fs, $path) = split( /\;/, $entry );
> -
> -		# Check if the path is allready used.
> -		if ( "$extrahdsettings{'PATH'}" eq "$path" ) {
> -			$errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd because there is already a device mounted'}.";
> -		}
> -
> -		# Check if the uuid is allready used.
> -		if ("$extrahdsettings{'DEVICE'} eq $uuid") {
> -			$errormessage = "$extrahdsettings{'DEVICE'} is allready mounted.";
> -		}
> -	}
> -
> -	# Check if a valid mount path has been choosen.
> +	# Check if a valid mount path has been choosen.
>   	unless(&is_valid_dir("$extrahdsettings{'PATH'}")) {
>   		$errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to root'}.";
>   	}
> @@ -129,7 +108,31 @@ if ($extrahdsettings{'ACTION'} eq $Lang::tr{'add'}) {
>   		$errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd because there is already a device mounted'}.";
>   	}
>   
> -	# Check if there was an error message.
> +	# Check against may previously configured drives.
> +	unless ($errormessage) {
> +		# Open device file for reading.
> +		open( FILE, "< $devicefile" ) or die "Unable to read $devicefile";
> +		my @devices = <FILE>;
> +		close FILE;
> +
> +		# Loop through the entries line-by-line.
> +		foreach my $entry (sort @devices) {
> +			# Split the line into pieces and assign nice variables.
> +			my ($uuid, $fs, $path) = split( /\;/, $entry );
> +
> +			# Check if the path is allready used.
> +			if ( "$extrahdsettings{'PATH'}" eq "$path" ) {
> +				$errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd because there is already a device mounted'}.";
> +			}
> +
> +			# Check if the uuid is allready used.
> +			if ("$extrahdsettings{'DEVICE'} eq $uuid") {
> +				$errormessage = "$extrahdsettings{'DEVICE'} is allready mounted.";
> +			}
> +		}
> +	}
> +
> +	# Go further if there was no error message.
>   	unless($errormessage) {
>   		# Re-open the device file for writing.
>   		open(FILE, ">> $devicefile" ) or die "Unable to write $devicefile";
  

Patch

diff --git a/html/cgi-bin/extrahd.cgi b/html/cgi-bin/extrahd.cgi
index bb84ef77c..de5f26d0e 100644
--- a/html/cgi-bin/extrahd.cgi
+++ b/html/cgi-bin/extrahd.cgi
@@ -98,28 +98,7 @@  $extrahdsettings{'UUID'} = '';
 ## Add a new device.
 #
 if ($extrahdsettings{'ACTION'} eq $Lang::tr{'add'}) {
-	# Open device file for reading.
-	open( FILE, "< $devicefile" ) or die "Unable to read $devicefile";
-	my @devices = <FILE>;
-	close FILE;
-
-	# Loop through the entries line-by-line.
-	foreach my $entry (sort @devices) {
-		# Split the line into pieces and assign nice variables.
-		my ($uuid, $fs, $path) = split( /\;/, $entry );
-
-		# Check if the path is allready used.
-		if ( "$extrahdsettings{'PATH'}" eq "$path" ) {
-			$errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd because there is already a device mounted'}.";
-		}
-
-		# Check if the uuid is allready used.
-		if ("$extrahdsettings{'DEVICE'} eq $uuid") {
-			$errormessage = "$extrahdsettings{'DEVICE'} is allready mounted.";
-		}
-	}
-
-	# Check if a valid mount path has been choosen. 
+	# Check if a valid mount path has been choosen.
 	unless(&is_valid_dir("$extrahdsettings{'PATH'}")) {
 		$errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to root'}.";
 	}
@@ -129,7 +108,31 @@  if ($extrahdsettings{'ACTION'} eq $Lang::tr{'add'}) {
 		$errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd because there is already a device mounted'}.";
 	}
 
-	# Check if there was an error message.
+	# Check against may previously configured drives.
+	unless ($errormessage) {
+		# Open device file for reading.
+		open( FILE, "< $devicefile" ) or die "Unable to read $devicefile";
+		my @devices = <FILE>;
+		close FILE;
+
+		# Loop through the entries line-by-line.
+		foreach my $entry (sort @devices) {
+			# Split the line into pieces and assign nice variables.
+			my ($uuid, $fs, $path) = split( /\;/, $entry );
+
+			# Check if the path is allready used.
+			if ( "$extrahdsettings{'PATH'}" eq "$path" ) {
+				$errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd because there is already a device mounted'}.";
+			}
+
+			# Check if the uuid is allready used.
+			if ("$extrahdsettings{'DEVICE'} eq $uuid") {
+				$errormessage = "$extrahdsettings{'DEVICE'} is allready mounted.";
+			}
+		}
+	}
+
+	# Go further if there was no error message.
 	unless($errormessage) {
 		# Re-open the device file for writing.
 		open(FILE, ">> $devicefile" ) or die "Unable to write $devicefile";