diff --git a/src/backend/BSContar.pm b/src/backend/BSContar.pm index 06e66054c1f..071d0e13bc4 100644 --- a/src/backend/BSContar.pm +++ b/src/backend/BSContar.pm @@ -302,12 +302,8 @@ sub checksum_tar { } sub normalize_container { - my ($tarfd, $recompress, $repotags, $tmpdir) = @_; - my @tarstat = stat($tarfd); - die("stat: $!\n") unless @tarstat; - my $mtime = $tarstat[9]; - my $tar = BSTar::list($tarfd); - $_->{'file'} = $tarfd for @$tar; + my ($file, $recompress, $repotags, $tmpdir) = @_; + my ($tar, $mtime) = open_container_tar($file); my %tar = map {$_->{'name'} => $_} @$tar; my ($manifest_ent, $manifest, $layercomp) = get_manifest(\%tar); my ($config_ent, $config) = get_config(\%tar, $manifest); @@ -414,6 +410,21 @@ sub create_dist_manifest_list { return $json; } +sub open_container_tar { + my ($file) = @_; + my $tarfd; + if (ref($file)) { + $tarfd = $file; + } else { + open($tarfd, '<', $file) || die("$file: $!\n"); + } + my @s = stat($tarfd); + die("$file: $!\n") unless @s; + my $tar = BSTar::list($tarfd); + $_->{'file'} = $tarfd for @$tar; + return ($tar, $s[9]); +} + sub container_from_helm { my ($chartfile, $config_json, $repotags) = @_; my $fd; diff --git a/src/backend/BSPublisher/Container.pm b/src/backend/BSPublisher/Container.pm index b3911025291..65be0b76ddb 100644 --- a/src/backend/BSPublisher/Container.pm +++ b/src/backend/BSPublisher/Container.pm @@ -318,17 +318,13 @@ sub open_container_tar { } elsif (($containerinfo->{'type'} || '') eq 'helm') { ($tar, $mtime, $layer_compression) = BSContar::container_from_helm($file, $containerinfo->{'config_json'}, $containerinfo->{'tags'}); } elsif ($file =~ /\.tar$/) { - my $tarfd; - open($tarfd, '<', $file) || die("$file: $!\n"); - $tar = BSTar::list($tarfd); - $_->{'file'} = $tarfd for @$tar; + ($tar, $mtime) = BSContar::open_container_tar($file); } else { my $tmpfile = decompress_container($file); my $tarfd; open($tarfd, '<', $tmpfile) || die("$tmpfile: $!\n"); unlink($tmpfile); - $tar = BSTar::list($tarfd); - $_->{'file'} = $tarfd for @$tar; + ($tar, $mtime) = BSContar::open_container_tar($tarfd); } die("incomplete containerinfo\n") unless $tar; return ($tar, $mtime, $layer_compression); diff --git a/src/backend/BSPublisher/Registry.pm b/src/backend/BSPublisher/Registry.pm index 079f494f91c..96356af9319 100644 --- a/src/backend/BSPublisher/Registry.pm +++ b/src/backend/BSPublisher/Registry.pm @@ -562,9 +562,7 @@ sub open_container_tar { } elsif (($containerinfo->{'type'} || '') eq 'helm') { ($tar, $mtime, $layer_compression) = BSContar::container_from_helm($file, $containerinfo->{'config_json'}, $containerinfo->{'tags'}); } else { - my $tarfd; - open($tarfd, '<', $file) || die("$file: $!\n"); - ($tar, $mtime, undef, undef, $layer_compression) = BSContar::normalize_container($tarfd, 1); + ($tar, $mtime, undef, undef, $layer_compression) = BSContar::normalize_container($file, 1); } die("incomplete containerinfo\n") unless $tar; return ($tar, $mtime, $layer_compression); diff --git a/src/backend/bs_worker b/src/backend/bs_worker index d7c49b97755..32e8a2c2002 100755 --- a/src/backend/bs_worker +++ b/src/backend/bs_worker @@ -3495,15 +3495,11 @@ sub createreport { sub normalize_container { my ($container, $recompress, $tmpdir) = @_; - local *TAR; - open(TAR, '<', $container) || die("$container: $!\n"); - my ($tar, $mtime, undef, undef, $layercomp) = BSContar::normalize_container(\*TAR, $recompress, undef, $tmpdir); + my ($tar, $mtime, undef, undef, $layercomp) = BSContar::normalize_container($container, $recompress, undef, $tmpdir); if (grep {$_ && $_ ne 'gzip'} @{$layercomp || []}) { - close(TAR); die("non-gzip layer compression, not normalizing container\n"); } BSTar::writetarfile("$container.new", undef, $tar, 'mtime' => $mtime); - close(TAR); if (-f "$container.sha256") { open(NEWTAR, '<', "$container.new") || die("$container.new: $!\n"); my $ctx = Digest::SHA->new(256);