Skip to content

Commit

Permalink
[backend] refactor container tar opening into BSContar::open_containe…
Browse files Browse the repository at this point in the history
…r_tar
  • Loading branch information
mlschroe committed Feb 1, 2024
1 parent 271ef23 commit 529933f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
23 changes: 17 additions & 6 deletions src/backend/BSContar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions src/backend/BSPublisher/Container.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/backend/BSPublisher/Registry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions src/backend/bs_worker
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 529933f

Please sign in to comment.