[pve-devel] [PATCH storage v2 2/3] storage: merge archive format/compressor

Alwin Antreich a.antreich at proxmox.com
Fri Jan 31 17:01:03 CET 2020


detection into a separate function to reduce code duplication and allow
for easier modification.

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

diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index 1688077..390b343 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -1265,6 +1265,52 @@ sub foreach_volid {
     }
 }
 
+sub archive_info {
+    my ($archive, $comp, $format) = @_;
+    my $type;
+
+    if (!defined($comp) || !defined($format)) {
+	my $volid = basename($archive);
+	if ($volid =~ /vzdump-(lxc|openvz|qemu)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
+	    $type = $1;
+
+	    if ($8 eq 'tgz') {
+		$format = 'tar';
+		$comp = 'gz';
+	    } else {
+		$format = $10;
+		$comp = $12 if defined($12);
+	    }
+	} else {
+	    die "ERROR: couldn't determine format and compression type\n";
+	}
+    }
+
+    my $decompressor = {
+	gz  => {
+	    'vma' => [ "zcat", $archive ],
+	    'tar' => [ "tar", "-z", $archive ],
+	},
+	lzo => {
+	    'vma' => [ "lzop", "-d", "-c", $archive ],
+	    'tar' => [ "tar", "--lzop", $archive ],
+	},
+    };
+
+    my $info;
+    $info->{'format'} = $format;
+    $info->{'type'} = $type;
+    $info->{'compression'} = $comp;
+
+    if (defined($comp) && defined($format)) {
+	my $dcomp = $decompressor->{$comp}->{$format};
+	pop(@$dcomp) if !defined($archive);
+	$info->{'decompressor'} = $dcomp;
+    }
+
+    return $info;
+}
+
 sub extract_vzdump_config_tar {
     my ($archive, $conf_re) = @_;
 
@@ -1310,16 +1356,12 @@ sub extract_vzdump_config_vma {
     };
 
 
+    my $info = archive_info($archive);
+    $comp //= $info->{compression};
+    my $decompressor = $info->{decompressor};
+
     if ($comp) {
-	my $uncomp;
-	if ($comp eq 'gz') {
-	    $uncomp = ["zcat", $archive];
-	} elsif ($comp eq 'lzo') {
-	    $uncomp = ["lzop", "-d", "-c", $archive];
-	} else {
-	    die "unknown compression method '$comp'\n";
-	}
-	$cmd = [$uncomp, ["vma", "config", "-"]];
+	$cmd = [ $decompressor, ["vma", "config", "-"] ];
 
 	# in some cases, lzop/zcat exits with 1 when its stdout pipe is
 	# closed early by vma, detect this and ignore the exit code later
@@ -1360,20 +1402,14 @@ sub extract_vzdump_config {
     my ($cfg, $volid) = @_;
 
     my $archive = abs_filesystem_path($cfg, $volid);
+    my $info = archive_info($archive);
+    my $format = $info->{format};
+    my $comp = $info->{compression};
+    my $type = $info->{type};
 
-    if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
+    if ($type eq 'lxc' || $type eq 'openvz') {
 	return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
-    } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
-	my $format;
-	my $comp;
-	if ($7 eq 'tgz') {
-	    $format = 'tar';
-	    $comp = 'gz';
-	} else {
-	    $format = $9;
-	    $comp = $11 if defined($11);
-	}
-
+    } elsif ($type eq 'qemu') {
 	if ($format eq 'tar') {
 	    return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!);
 	} else {
-- 
2.20.1




More information about the pve-devel mailing list