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

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Jun 17 09:37:22 CEST 2019


On 6/14/19 3:37 PM, Alwin Antreich wrote:
> For less and cleaner code, use a hash for de-compressor commands instead
> of the 'long' if-elsif statement.
> 

squash that with 1/3, or better try to pull the decompressor_ method
out in a separate patch, with this already applied, and not mixed in with
the zstd stuff.

> 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;

can get you a "use of undefined variable ..." here, as the if above
can hit this else branch if the defined($comp) evaluates to false..

>      }
>  
>      pop(@$cmd) if !defined($archive);
> 





More information about the pve-devel mailing list