From 1b1b5f46dbf6fb0345b880bfc9a3cee3344a619c Mon Sep 17 00:00:00 2001 From: Michael Schroeder Date: Thu, 9 Jan 2025 10:57:31 +0100 Subject: [PATCH] Refactor apk package query And also fix the handling of install_if. --- Build/Apk.pm | 67 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/Build/Apk.pm b/Build/Apk.pm index 25ea6515..5688e9f1 100644 --- a/Build/Apk.pm +++ b/Build/Apk.pm @@ -423,6 +423,35 @@ sub parse { return $ret; } +my %pkginfo_array = ( + 'depend' => 1, + 'replaces' => 1, + 'provides' => 1, +); + +sub queryvars { + my ($handle, $withhdrmd5) = @_; + my $tar = Archive::Tar->new; + my @read = $tar->read($handle, 1, {'filter' => '^\.PKGINFO$', 'limit' => 1}); + die("$handle: not an apk package file\n") unless @read == 1; + my $pkginfo = $read[0]->get_content; + die("$handle: not an apk package file\n") unless defined $pkginfo; + my @lines = split("\n", $pkginfo); + my %q; + while (@lines) { + my $l = shift @lines; + next if $l eq '' || substr($l, 0, 1) eq '#'; + next unless $l =~ /^(.+?) = (.*)$/; + if ($pkginfo_array{$1}) { + push @{$q{$1}}, $2; + } else { + $q{$1} = $2; + } + } + $q{'hdrmd5'} = Digest::MD5::md5_hex($pkginfo) if $withhdrmd5; + $q{'install_if'} = [ split(' ', $q{'install_if'}) ] if $q{'install_if'}; + return \%q; +} my %pkginfomap = ( 'pkgname' => 'name', @@ -433,38 +462,27 @@ my %pkginfomap = ( 'arch' => 'arch', 'license' => 'license', 'origin' => 'source', - 'depend' => [ 'requires' ], -# 'replaces' => [ 'obsoletes' ], - 'provides' => [ 'provides' ], - 'install_if' => [ 'install_if' ], + 'depend' => 'requires', +# 'replaces' => 'obsoletes', + 'provides' => 'provides', 'datahash' => 'apkdatachksum', + 'hdrmd5' => 'hdrmd5', ); sub query { my ($handle, %opts) = @_; - my $tar = Archive::Tar->new; - my @read = $tar->read($handle, 1, {'filter' => '^\.PKGINFO$', 'limit' => 1}); - die("$handle: not an apk package file\n") unless @read == 1; - my $pkginfo = $read[0]->get_content; - die("$handle: not an apk package file\n") unless defined $pkginfo; - my @lines = split("\n", $pkginfo); + my $qq = queryvars($handle, 1); my %q; - while (@lines) { - my $l = shift @lines; - next if $l eq '' || substr($l, 0, 1) eq '#'; - next unless $l =~ /^(.+?) = (.*)$/; - my $m = $pkginfomap{$1}; - if (ref($m)) { - push @{$q{$m->[0]}}, $2; - } elsif ($m) { - $q{$m} = $2; - } + for (keys %$qq) { + my $m = $pkginfomap{$_}; + $q{$m} = $qq->{$_} if $m; } my @conflicts = grep {/^\!/} @{$q{'requires'} || []}; if (@conflicts) { substr($_, 0, 1, '') for @conflicts; $q{'conflicts'} = \@conflicts; $q{'requires'} = [ grep {!/^\!/} @{$q{'requires'} || []} ]; + delete $q{'requires'} unless @{$q{'requires'}}; } if ($q{'name'} && $opts{'addselfprovides'}) { my $selfprovides = $q{'name'}; @@ -472,15 +490,12 @@ sub query { push @{$q{'provides'}}, $selfprovides unless @{$q{'provides'} || []} && $q{'provides'}->[-1] eq $selfprovides; } if ($opts{'normalizedeps'}) { - s/^([a-zA-Z0-9\._+-]+)~/$1=~/ for @{$q{'requires'} || []}, @{$q{'conflicts'} || []}; + s/^([a-zA-Z0-9\._+-]+)~/$1=~/ for @{$q{'requires'} || []}, @{$q{'conflicts'} || []}, @{$qq->{'install_if'} || []}; } $q{'version'} = 0 unless defined $q{'version'}; $q{'release'} = $1 if $q{'version'} =~ s/-([^-]*)$//; - $q{'hdrmd5'} = Digest::MD5::md5_hex($pkginfo); $q{'source'} ||= $q{'name'} if defined $q{'name'}; - my $install_if = delete $q{'install_if'}; - $q{'supplements'} = [ join(' & ', @$install_if) ] if @{$install_if || []} && $opts{'weakdeps'}; - delete $q{'supplements'} unless $opts{'weakdeps'}; + $q{'supplements'} = [ join(' & ', @{$qq->{'install_if'}}) ] if $opts{'weakdeps'} && $qq->{'install_if'}; delete $q{'buildtime'} unless $opts{'buildtime'}; delete $q{'apkdatachksum'} unless $opts{'apkdatachksum'}; return \%q; @@ -600,7 +615,7 @@ sub calcapkchksum { $status = $z->inflate($input, $output); } if ($type eq 'raw') { - $ctx .= substr($oldinput, 0, length($oldinput) - length($input)); + $ctx .= substr($oldinput, 0, length($oldinput) - length($input)) if $sec == $section; } else { $ctx->add(substr($oldinput, 0, length($oldinput) - length($input))) if $sec == $section; }