From mboxrd@z Thu Jan 1 00:00:00 1970 From: Robin Roevens To: development@lists.ipfire.org Subject: [PATCH] pakfire: implement function to parse meta files Date: Fri, 25 Jun 2021 01:30:05 +0200 Message-ID: <20210624233005.32149-2-robin.roevens@disroot.org> In-Reply-To: <20210624233005.32149-1-robin.roevens@disroot.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0557576851639392839==" List-Id: --===============0557576851639392839== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Removing a lot of duplicate code parsing meta files, now replaced by simple function calls, resulting in all metadata in one hash. Signed-off-by: Robin Roevens --- src/pakfire/lib/functions.pl | 129 +++++++++++++---------------------- 1 file changed, 49 insertions(+), 80 deletions(-) diff --git a/src/pakfire/lib/functions.pl b/src/pakfire/lib/functions.pl index f9a19b60d..79ef7a12d 100644 --- a/src/pakfire/lib/functions.pl +++ b/src/pakfire/lib/functions.pl @@ -2,7 +2,7 @@ ############################################################################= ### # = # # IPFire.org - A linux based firewall = # -# Copyright (C) 2007-2015 IPFire Team = # +# Copyright (C) 2007-2021 IPFire Team = # # = # # This program is free software: you can redistribute it and/or modify = # # it under the terms of the GNU General Public License as published by = # @@ -376,11 +376,10 @@ sub dbgetlist { } =20 # Update the meta database if new packages was in the package list - my @meta; my $file; my $line; my $prog; - my ($name, $version, $release); + my %metadata; my @templine; =20 open(FILE, "<$Conf::dbdir/lists/packages_list.db"); @@ -395,28 +394,14 @@ sub dbgetlist { next if ( $file eq ".." ); next if ( $file eq "meta-" ); next if ( $file =3D~ /^old/ ); - open(FILE, "<$Conf::dbdir/meta/$file"); - @meta =3D ; - close(FILE); - foreach $line (@meta) { - @templine =3D split(/\: /,$line); - if ("$templine[0]" eq "Name") { - $name =3D $templine[1]; - chomp($name); - } elsif ("$templine[0]" eq "ProgVersion") { - $version =3D $templine[1]; - chomp($version); - } elsif ("$templine[0]" eq "Release") { - $release =3D $templine[1]; - chomp($release); - } - } + %metadata =3D parsemetafile("$Conf::dbdir/meta/$file"); + foreach $prog (@db) { @templine =3D split(/\;/,$prog); - if (("$name" eq "$templine[0]") && ("$release" ne "$templine[2]")) { - move("$Conf::dbdir/meta/meta-$name","$Conf::dbdir/meta/old_meta-$name"); - fetchfile("meta/meta-$name", ""); - move("$Conf::cachedir/meta-$name", "$Conf::dbdir/meta/meta-$name"); + if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" ne= "$templine[2]")) { + move("$Conf::dbdir/meta/meta-$metadata{'Name'}","$Conf::dbdir/meta/old_m= eta-$metadata{'Name'}"); + fetchfile("meta/meta-$metadata{'Name'}", ""); + move("$Conf::cachedir/meta-$metadata{'Name'}", "$Conf::dbdir/meta/meta-$= metadata{'Name'}"); } } } @@ -429,12 +414,11 @@ sub dblist { # filter may be: all, notinstalled, installed my $filter =3D shift; my $forweb =3D shift; - my @meta; my @updatepaks; my $file; my $line; my $prog; - my ($name, $version, $release); + my %metadata; my @templine; =09 ### Make sure that the list is not outdated.=20 @@ -470,30 +454,16 @@ sub dblist { next if ( $file eq "." ); next if ( $file eq ".." ); next if ( $file =3D~ /^old/ ); - open(FILE, "<$Conf::dbdir/installed/$file"); - @meta =3D ; - close(FILE); - foreach $line (@meta) { - @templine =3D split(/\: /,$line); - if ("$templine[0]" eq "Name") { - $name =3D $templine[1]; - chomp($name); - } elsif ("$templine[0]" eq "ProgVersion") { - $version =3D $templine[1]; - chomp($version); - } elsif ("$templine[0]" eq "Release") { - $release =3D $templine[1]; - chomp($release); - } - } + %metadata =3D parsemetafile("$Conf::dbdir/installed/$file"); + foreach $prog (@db) { @templine =3D split(/\;/,$prog); - if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" && "$for= web" ne "notice")) { - push(@updatepaks,$name); + if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" <= "$templine[2]" && "$forweb" ne "notice")) { + push(@updatepaks,$metadata{'Name'}); if ("$forweb" eq "forweb") { - print "\n"; + print "\n"; } else { - my $command =3D "Update: $name\nVersion: $version -> $templine[1]\nRel= ease: $release -> $templine[2]\n"; + my $command =3D "Update: $metadata{'Name'}\nVersion: $metadata{'ProgVe= rsion'} -> $templine[1]\nRelease: $metadata{'Release'} -> $templine[2]\n"; if ("$Pakfire::enable_colors" eq "1") { print "$color{'lila'}$command$color{'normal'}\n"; } else { @@ -548,18 +518,9 @@ sub resolvedeps_one { =09 message("PAKFIRE RESV: $pak: Resolving dependencies..."); =09 - open(FILE, "<$Conf::dbdir/meta/meta-$pak"); - my @file =3D ; - close(FILE); -=09 - my $line; - my (@templine, @deps, @all); - foreach $line (@file) { - @templine =3D split(/\: /,$line); - if ("$templine[0]" eq "Dependencies") { - @deps =3D split(/ /, $templine[1]); - } - } + my %metadata =3D parsemetafile("$Conf::dbdir/meta/meta-$pak"); + my @all; + my @deps =3D split(/ /, $metadata{'Dependencies'}); chomp (@deps); foreach (@deps) { if ($_) { @@ -679,20 +640,40 @@ sub getsize { =09 getmetafile("$pak"); =09 - open(FILE, "<$Conf::dbdir/meta/meta-$pak"); - my @file =3D ; + if (my %metadata =3D parsemetafile("$Conf::dbdir/meta/meta-$pak")) { + return $metadata{'Size'}; + } + return 0; +} + +sub parsemetafile { + ### This subroutine returns a hash with the contents of a meta- file + # Pass path to metafile as argument: Pakfire::parsemetafile("$Conf::dbdir= /meta/meta-$pak") + # Usage is always with an argument. + my $metafile =3D shift; + + my %metadata =3D (); + + my @templine; + my @file; + + if (! -e $metafile ) { + return 0; + } + + open(FILE, "<$metafile"); + @file =3D ; close(FILE); =09 - my $line; - my @templine; - foreach $line (@file) { - @templine =3D split(/\: /,$line); - if ("$templine[0]" eq "Size") { + foreach (@file) { + @templine =3D split(/\: /,$_); + if ($templine[1]) { chomp($templine[1]); - return $templine[1]; + $metadata{"$templine[0]"} =3D $templine[1]; } } - return 0; + + return %metadata; } =20 sub decryptpak { @@ -715,20 +696,8 @@ sub getpak { =20 getmetafile("$pak"); =09 - open(FILE, "<$Conf::dbdir/meta/meta-$pak"); - my @file =3D ; - close(FILE); -=09 - my $line; - my $file; - my @templine; - foreach $line (@file) { - @templine =3D split(/\: /,$line); - if ("$templine[0]" eq "File") { - chomp($templine[1]); - $file =3D $templine[1]; - } - } + my %metadata =3D parsemetafile("$Conf::dbdir/meta/meta-$pak"); + my $file =3D $metadata{'File'}; =09 unless ($file) { message("No filename given in meta-file."); --=20 2.31.1 --=20 Dit bericht is gescanned op virussen en andere gevaarlijke inhoud door MailScanner en lijkt schoon te zijn. --===============0557576851639392839==--