]> git.proxmox.com Git - pve-common.git/commitdiff
fix #4849: download file from url: add opt parameter for a decompression command
authorPhilipp Hufnagl <p.hufnagl@proxmox.com>
Tue, 1 Aug 2023 14:46:01 +0000 (16:46 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 4 Aug 2023 10:45:35 +0000 (12:45 +0200)
Signed-off-by: Philipp Hufnagl <p.hufnagl@proxmox.com>
src/PVE/Tools.pm

index 9ffac12100f0bec79be13d1052d5b04d8aa43134..159ec8259fac6e9a7dc25c90901cc749748e5c44 100644 (file)
@@ -2013,10 +2013,13 @@ sub download_file_from_url {
        }
     }
 
-    my $tmpdest = "$dest.tmp.$$";
+    my $tmp_download = "$dest.tmp_dwnl.$$";
+    my $tmp_decomp = "$dest.tmp_dcom.$$";
     eval {
        local $SIG{INT} = sub {
-           unlink $tmpdest or warn "could not cleanup temporary file: $!";
+           unlink $tmp_download or warn "could not cleanup temporary file: $!";
+           unlink $tmp_decomp or warn "could not cleanup temporary file: $!"
+               if $opts->{decompression_command};
            die "got interrupted by signal\n";
        };
 
@@ -2029,7 +2032,7 @@ sub download_file_from_url {
                $ENV{https_proxy} = $opts->{https_proxy};
            }
 
-           my $cmd = ['wget', '--progress=dot:giga', '-O', $tmpdest, $url];
+           my $cmd = ['wget', '--progress=dot:giga', '-O', $tmp_download, $url];
 
            if (!($opts->{verify_certificates} // 1)) { # default to true
                push @$cmd, '--no-check-certificate';
@@ -2041,7 +2044,7 @@ sub download_file_from_url {
        if ($checksum_algorithm) {
            print "calculating checksum...";
 
-           my $checksum_got = get_file_hash($checksum_algorithm, $tmpdest);
+           my $checksum_got = get_file_hash($checksum_algorithm, $tmp_download);
 
            if (lc($checksum_got) eq lc($checksum_expected)) {
                print "OK, checksum verified\n";
@@ -2051,10 +2054,26 @@ sub download_file_from_url {
            }
        }
 
-       rename($tmpdest, $dest) or die "unable to rename temporary file: $!\n";
+    if (my $cmd = $opts->{decompression_command}) {
+       push @$cmd, $tmp_download;
+    my $fh;
+    if (!open($fh, ">", "$tmp_decomp")) {
+       die "cant open temporary file $tmp_decomp for decompresson: $!\n";
+    }
+       print "decompressing $tmp_download to $tmp_decomp\n";
+       eval { run_command($cmd, output => '>&'.fileno($fh)); };
+       my $err = $@;
+       unlink $tmp_download;
+       die "$err\n" if $err;
+       rename($tmp_decomp, $dest) or die "unable to rename temporary file: $!\n";
+    } else {
+       rename($tmp_download, $dest) or die "unable to rename temporary file: $!\n";
+    }
     };
     if (my $err = $@) {
-       unlink $tmpdest or warn "could not cleanup temporary file: $!";
+       unlink $tmp_download or warn "could not cleanup temporary file: $!";
+       unlink $tmp_decomp or warn "could not cleanup temporary file: $!"
+           if $opts->{decompression_command};
        die $err;
     }