restore: factor out common early checks into closure

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-02-24 13:29:42 +01:00
parent 9f8ba326ce
commit 36d4bdcb86

View File

@ -6230,8 +6230,15 @@ my $restore_cleanup_oldconf = sub {
my $parse_backup_hints = sub { my $parse_backup_hints = sub {
my ($rpcenv, $user, $storecfg, $fh, $devinfo, $options) = @_; my ($rpcenv, $user, $storecfg, $fh, $devinfo, $options) = @_;
my $virtdev_hash = {}; my $check_storage = sub { # assert if an image can be allocate
my ($storeid, $scfg) = @_;
die "Content type 'images' is not available on storage '$storeid'\n"
if !$scfg->{content}->{images};
$rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace'])
if $user ne 'root@pam';
};
my $virtdev_hash = {};
while (defined(my $line = <$fh>)) { while (defined(my $line = <$fh>)) {
if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) { if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4); my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4);
@ -6253,27 +6260,20 @@ my $parse_backup_hints = sub {
die "Content type 'images' is not available on storage '$storeid'\n" die "Content type 'images' is not available on storage '$storeid'\n"
if !$scfg->{content}->{images}; if !$scfg->{content}->{images};
# check permission on storage $check_storage->($storeid, $scfg); # permission and content type check
my $pool = $options->{pool}; # todo: do we need that?
if ($user ne 'root@pam') {
$rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace']);
}
$virtdev_hash->{$virtdev} = $devinfo->{$devname}; $virtdev_hash->{$virtdev} = $devinfo->{$devname};
} elsif ($line =~ m/^((?:ide|sata|scsi)\d+):\s*(.*)\s*$/) { } elsif ($line =~ m/^((?:ide|sata|scsi)\d+):\s*(.*)\s*$/) {
my $virtdev = $1; my $virtdev = $1;
my $drive = parse_drive($virtdev, $2); my $drive = parse_drive($virtdev, $2);
if (drive_is_cloudinit($drive)) { if (drive_is_cloudinit($drive)) {
my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}); my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
$storeid = $options->{storage} if defined ($options->{storage}); $storeid = $options->{storage} if defined ($options->{storage});
my $scfg = PVE::Storage::storage_config($storecfg, $storeid); my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
my $format = qemu_img_format($scfg, $volname); # has 'raw' fallback my $format = qemu_img_format($scfg, $volname); # has 'raw' fallback
die "Content type 'images' is not available on storage '$storeid'\n" $check_storage->($storeid, $scfg); # permission and content type check
if !$scfg->{content}->{images};
$rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace'])
if $user ne 'root@pam';
$virtdev_hash->{$virtdev} = { $virtdev_hash->{$virtdev} = {
format => $format, format => $format,