[pve-devel] [PATCH storage 3/3] decompress: use hash instead of if statement

Alwin Antreich a.antreich at proxmox.com
Fri Jun 14 15:37:34 CEST 2019


For less and cleaner code, use a hash for de-compressor commands instead
of the 'long' if-elsif statement.

Signed-off-by: Alwin Antreich <a.antreich at proxmox.com>
---
 PVE/Storage.pm | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index 6953b3f..537259a 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -1380,19 +1380,17 @@ sub decompressor_info {
 	}
     }
 
+    my $decompressor = {
+	'gz'   => ["zcat", $archive],
+	'lzo'  => ["lzop", "-d", "-c", $archive],
+	'zst'  => ["zstd", "-q", "-d", "-c", $archive],
+    };
+
     my $cmd;
-    if (defined($comp)) {
-	if ($comp eq 'gz') {
-	    $cmd = ["zcat", $archive];
-	} elsif ($comp eq 'lzo') {
-	    $cmd = ["lzop", "-d", "-c", $archive];
-	} elsif ($comp eq 'zst') {
-	    $cmd = ["zstd", "-q", "-d", "-c", $archive];
-	} else {
-	    die "unknown compression method '$comp'\n" if !$noerr;
-	}
+    if (defined($comp) && $decompressor->{$comp}) {
+	$cmd = $decompressor->{$comp};
     } else {
-	die "compression type not set\n" if !$noerr;
+	die "compression method unknown, '$comp'\n" if !$noerr;
     }
 
     pop(@$cmd) if !defined($archive);
-- 
2.11.0





More information about the pve-devel mailing list