From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 8F2EB1FF163 for ; Thu, 7 Nov 2024 17:54:47 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 93B72354AD; Thu, 7 Nov 2024 17:52:56 +0100 (CET) From: Fiona Ebner To: pve-devel@lists.proxmox.com Date: Thu, 7 Nov 2024 17:51:42 +0100 Message-Id: <20241107165146.125935-31-f.ebner@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241107165146.125935-1-f.ebner@proxmox.com> References: <20241107165146.125935-1-f.ebner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.056 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [RFC container v3 30/34] create: factor out compression option helper X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" In preparation to re-use it for checking potentially untrusted archives. Signed-off-by: Fiona Ebner --- New in v3. src/PVE/LXC/Create.pm | 51 +++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm index 719f372..d2f675e 100644 --- a/src/PVE/LXC/Create.pm +++ b/src/PVE/LXC/Create.pm @@ -78,15 +78,38 @@ sub restore_proxmox_backup_archive { $scfg, $storeid, $cmd, $param, userns_cmd => $userns_cmd); } +my sub tar_compression_option { + my ($archive) = @_; + + my %compression_map = ( + '.gz' => '-z', + '.bz2' => '-j', + '.xz' => '-J', + '.lzo' => '--lzop', + '.zst' => '--zstd', + ); + if ($archive =~ /\.tar(\.[^.]+)?$/) { + if (defined($1)) { + die "unrecognized compression format: $1\n" if !defined($compression_map{$1}); + return $compression_map{$1}; + } + return; + } else { + die "file does not look like a template archive: $archive\n"; + } +} + my sub restore_tar_archive_command { - my ($conf, $opts, $rootdir, $bwlimit) = @_; + my ($conf, $compression_opt, $rootdir, $bwlimit) = @_; my ($id_map, $root_uid, $root_gid) = PVE::LXC::parse_id_maps($conf); my $userns_cmd = PVE::LXC::userns_command($id_map); - my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', $opts->@*, '--totals', - @PVE::Storage::Plugin::COMMON_TAR_FLAGS, - '-C', $rootdir]; + my $cmd = [@$userns_cmd, 'tar', 'xpf', '-']; + push $cmd->@*, $compression_opt if $compression_opt; + push $cmd->@*, '--totals'; + push $cmd->@*, @PVE::Storage::Plugin::COMMON_TAR_FLAGS; + push $cmd->@*, '-C', $rootdir; # skip-old-files doesn't have anything to do with time (old/new), but is # simply -k (annoyingly also called --keep-old-files) without the 'treat @@ -108,24 +131,10 @@ sub restore_tar_archive { my $archive_fh; my $tar_input = '<&STDIN'; - my @compression_opt; + my $compression_opt; if ($archive ne '-') { # GNU tar refuses to autodetect this... *sigh* - my %compression_map = ( - '.gz' => '-z', - '.bz2' => '-j', - '.xz' => '-J', - '.lzo' => '--lzop', - '.zst' => '--zstd', - ); - if ($archive =~ /\.tar(\.[^.]+)?$/) { - if (defined($1)) { - die "unrecognized compression format: $1\n" if !defined($compression_map{$1}); - @compression_opt = $compression_map{$1}; - } - } else { - die "file does not look like a template archive: $archive\n"; - } + $compression_opt = tar_compression_option($archive); sysopen($archive_fh, $archive, O_RDONLY) or die "failed to open '$archive': $!\n"; my $flags = $archive_fh->fcntl(Fcntl::F_GETFD(), 0); @@ -133,7 +142,7 @@ sub restore_tar_archive { $tar_input = '<&'.fileno($archive_fh); } - my $cmd = restore_tar_archive_command($conf, [@compression_opt], $rootdir, $bwlimit); + my $cmd = restore_tar_archive_command($conf, $compression_opt, $rootdir, $bwlimit); if ($archive eq '-') { print "extracting archive from STDIN\n"; -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel