[pve-devel] [PATCH storage 2/6] Expand archive_info to include ctime, vmid and is_std_name

Fabian Ebner f.ebner at proxmox.com
Thu Jun 4 11:08:34 CEST 2020


where 'is_std_name' shows whether the backup name uses the standard naming
schema and most likely was created by our tools.

Also adds a '^' to the existing filename matching regex, which
should be fine since basename() is used beforehand.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 PVE/Storage.pm            | 17 ++++++++++++++---
 PVE/Storage/Plugin.pm     | 11 ++++-------
 test/archive_info_test.pm | 10 ++++++++++
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index f1e3b19..07a4f53 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -12,6 +12,7 @@ use File::Basename;
 use File::Path;
 use Cwd 'abs_path';
 use Socket;
+use Time::Local qw(timelocal);
 
 use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
@@ -1394,9 +1395,19 @@ sub archive_info {
     my $info;
 
     my $volid = basename($archive);
-    if ($volid =~ /vzdump-(lxc|openvz|qemu)-\d+-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?$/) {
-	$info = decompressor_info($2, $3);
-	$info->{type} = $1;
+    if ($volid =~ /^vzdump-(lxc|openvz|qemu)-\d+-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?$/) {
+	my ($type, $format, $comp) = ($1, $2, $3);
+	my $format_re = defined($comp) ? "$format.$comp" : "$format";
+	$info = decompressor_info($format, $comp);
+	$info->{type} = $type;
+
+	if ($volid =~ /^vzdump-${type}-([1-9][0-9]{2,8})-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.${format_re}$/) {
+	    $info->{vmid} = int($1);
+	    $info->{ctime} = timelocal($7, $6, $5, $4, $3 - 1, $2 - 1900);
+	    $info->{is_std_name} = 1;
+	} else {
+	    $info->{is_std_name} = 0;
+	}
     } else {
 	die "ERROR: couldn't determine archive info from '$archive'\n";
     }
diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index cec136e..595af54 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -8,7 +8,6 @@ use File::chdir;
 use File::Path;
 use File::Basename;
 use File::stat qw();
-use Time::Local qw(timelocal);
 
 use PVE::Tools qw(run_command);
 use PVE::JSONSchema qw(get_standard_option);
@@ -942,21 +941,19 @@ my $get_subdir_files = sub {
 	} elsif ($tt eq 'backup') {
 	    next if defined($vmid) && $fn !~  m/\S+-$vmid-\S+/;
 	    next if $fn !~ m!/([^/]+\.(tgz|(?:(?:tar|vma)(?:\.(${\COMPRESSOR_RE}))?)))$!;
-
 	    my $format = $2;
 	    $fn = $1;
 	    $info = { volid => "$sid:backup/$fn", format => $format };
 
-	    if ($fn =~ m!^vzdump\-(?:lxc|qemu)\-(?:[1-9][0-9]{2,8})\-(\d{4})_(\d{2})_(\d{2})\-(\d{2})_(\d{2})_(\d{2})\.${format}$!) {
-		my $epoch = timelocal($6, $5, $4, $3, $2-1, $1 - 1900);
-		$info->{ctime} = $epoch;
-	    }
+	    my $archive_info = eval { PVE::Storage::archive_info($fn) };
+
+	    $info->{ctime} = $archive_info->{ctime}
+		if defined($archive_info) && defined($archive_info->{ctime});
 
 	    if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
 		$info->{vmid} = $vmid // $1;
 	    }
 
-
 	} elsif ($tt eq 'snippets') {
 
 	    $info = {
diff --git a/test/archive_info_test.pm b/test/archive_info_test.pm
index 2d1405b..5e5ca8a 100644
--- a/test/archive_info_test.pm
+++ b/test/archive_info_test.pm
@@ -26,6 +26,9 @@ my $tests = [
 	    'format'       => 'tar',
 	    'decompressor' => ['tar', '-z'],
 	    'compression'  => 'gz',
+	    'vmid'         => $vmid,
+	    'ctime'        => 1585604370,
+	    'is_std_name'  => 1,
 	},
     },
     {
@@ -36,6 +39,9 @@ my $tests = [
 	    'format'       => 'tar',
 	    'decompressor' => ['tar', '-z'],
 	    'compression'  => 'gz',
+	    'vmid'         => $vmid,
+	    'ctime'        => 1585604370,
+	    'is_std_name'  => 1,
 	},
     },
     {
@@ -46,6 +52,7 @@ my $tests = [
 	    'format'       => 'tar',
 	    'decompressor' => ['tar', '-z'],
 	    'compression'  => 'gz',
+	    'is_std_name'  => 0,
 	},
     },
 ];
@@ -83,6 +90,9 @@ for my $virt (sort keys %$bkp_suffix) {
 		'format'       => "$format",
 		'decompressor' => $decomp->{$suffix},
 		'compression'  => "$suffix",
+		'vmid'         => $vmid,
+		'ctime'        => 1585602760,
+		'is_std_name'  => 1,
 	    },
 	};
     }
-- 
2.20.1





More information about the pve-devel mailing list