]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/pakfire/lib/functions.pl
3 require "/opt/pakfire/etc/pakfire.conf" ;
4 require "/var/ipfire/general-functions.pl" ;
14 my %pakfiresettings = ();
15 & General
:: readhash
( "${General::swroot}/pakfire/settings" , \
%pakfiresettings );
25 system ( "logger -t pakfire \" $log \" " );
29 & Pakfire
:: message
( "Usage: pakfire <install|remove> <pak(s)>" );
30 & Pakfire
:: message
( " <update> - Contacts the servers for new lists of paks." );
31 & Pakfire
:: message
( " <upgrade> - Installs the latest version of all paks." );
32 & Pakfire
:: message
( " <list> - Outputs a short list with all available paks." );
33 & Pakfire
:: message
( "" );
40 $p = Net
:: Ping
-> new ();
41 if ( $p -> ping ( $host )) {
42 logger
( " $host is alive." );
45 logger
( " $host is dead." );
54 my ( @server , $host , $proto , $file , $allok , $i );
57 $bfile = basename
( " $getfile " );
60 while (( $allok == 0 ) && $i < 5 ) {
63 if ( " $gethost " eq "" ) {
64 @server = selectmirror
();
67 $file = " $server [2]/ $getfile " ;
73 $proto = "HTTP" unless $proto ;
75 logger
( "Trying to get $file from $host ( $proto )." );
77 my $ua = LWP
:: UserAgent
-> new ;
78 $ua -> agent ( "Pakfire/ $Conf ::version" );
82 & General
:: readhash
( "${General::swroot}/proxy/advanced/settings" , \
%proxysettings );
84 if ( $proxysettings { 'UPSTREAM_PROXY' }) {
85 logger
( "Using upstream proxy: \" $proxysettings {'UPSTREAM_PROXY'} \" " );
86 if ( $proxysettings { 'UPSTREAM_USER' }) {
87 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_USER'}: $proxysettings {'UPSTREAM_PASSWORD'}@" . " $proxysettings {'UPSTREAM_PROXY'}/" );
88 logger
( " Logging in with: \" $proxysettings {'UPSTREAM_USER'} \" - \" $proxysettings {'UPSTREAM_PASSWORD'} \" " );
90 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_PROXY'}/" );
94 my $response = $ua -> get ( "http:// $host / $file " );
96 my $code = $response -> code ();
97 my $log = $response -> status_line ;
98 logger
( "HTTP-Status-Code: $code - $log " );
100 if ( $code eq "500" ) {
101 message
( "Giving up: There was no chance to get the file \" $getfile \" from any available server. \n There was an error on the way. Please fix it." );
105 if ( $response -> is_success ) {
106 if ( open ( FILE
, "> $Conf ::tmpdir/ $bfile " )) {
107 print FILE
$response -> content ;
109 unless ( $bfile =~ /^counter\?.*/ ) { # Don't check out counterfile cause it's empty
110 logger
( "File received. Start checking signature..." );
111 if ( system ( "gpg --verify \" $Conf ::tmpdir/ $bfile \" &>/dev/null" ) eq 0 ) {
112 logger
( "Signature of $bfile is fine." );
113 move
( " $Conf ::tmpdir/ $bfile " , " $Conf ::cachedir/ $bfile " );
115 message
( "The downloaded file ( $file ) wasn't verified by IPFire.org. Sorry - Exiting..." );
119 logger
( "Download successfully done from $host (file: $file )." );
123 logger
( "Could not open $Conf ::cachedir/ $bfile for writing." );
126 logger
( "Download $file failed from $host ( $proto ): $log " );
129 message
( "Giving up: There was no chance to get the file \" $getfile \" from any available server. \n May be you should run \" pakfire update \" to get some new servers." );
136 logger
( "Try to get a mirror list." );
138 fetchfile
( " $Conf ::version/lists/server-list.db" , " $Conf ::mainserver" );
139 move
( " $Conf ::cachedir/server-list.db" , " $Conf ::dbdir/lists/server-list.db" );
143 ### Check if there is a current server list and read it.
144 # If there is no list try to get one.
146 while (!( open ( FILE
, "< $Conf ::dbdir/lists/server-list.db" )) && ( $count lt 5 )) {
151 message
( "Could not find or download a server list." );
157 ### Count the number of the servers in the list
161 if ( " $_ " =~ /.*;.*;.*;/ ) {
166 logger
( " $scount servers found in list." );
168 ### Choose a random server and test if it is online
169 # If the check fails try a new server.
170 # This will never give up.
173 while ( $found == 0 ) {
174 $server = int ( rand ( $scount ) + 1 );
176 my ( $line , $proto , $path , $host );
178 foreach $line ( @newlines ) {
180 if ( $servers eq $server ) {
181 @templine = split ( /\;/ , $line );
182 $proto = $templine [ 0 ];
183 $host = $templine [ 1 ];
184 $path = $templine [ 2 ];
185 if ( pinghost
( " $host " )) {
187 return ( $proto , $host , $path );
195 ### Update the database if the file is older than one day.
196 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
197 # Usage is always with an argument.
203 if ( - e
" $Conf ::dbdir/lists/packages_list.db" ) {
204 my @stat = stat ( " $Conf ::dbdir/lists/packages_list.db" );
206 $age = $time - $stat [ 9 ];
212 if (( " $age " gt 86400 ) || ( " $force " eq "force" )) {
214 fetchfile
( "lists/packages_list.db" , "" );
215 move
( " $Conf ::cachedir/packages_list.db" , " $Conf ::dbdir/lists/packages_list.db" );
220 ### This subroutine lists the packages.
221 # You may also pass a filter: &Pakfire::dblist(filter)
222 # Usage is always with two arguments.
223 # filter may be: all, notinstalled, installed
231 my ( $name , $version , $release );
234 ### Make sure that the list is not outdated.
235 dbgetlist
( "noforce" );
237 open ( FILE
, "< $Conf ::dbdir/lists/packages_list.db" );
241 if ( " $filter " eq "upgrade" ) {
242 opendir ( DIR
, " $Conf ::dbdir/meta" );
243 my @files = readdir ( DIR
);
245 foreach $file ( @files ) {
246 next if ( $file eq "." );
247 next if ( $file eq ".." );
248 open ( FILE
, "< $Conf ::dbdir/meta/ $file " );
251 foreach $line ( @meta ) {
252 @templine = split ( /\: / , $line );
253 if ( " $templine [0]" eq "Name" ) {
254 $name = $templine [ 1 ];
256 } elsif ( " $templine [0]" eq "ProgVersion" ) {
257 $version = $templine [ 1 ];
259 } elsif ( " $templine [0]" eq "Release" ) {
260 $release = $templine [ 1 ];
264 foreach $prog ( @db ) {
265 @templine = split ( /\;/ , $prog );
266 if (( " $name " eq " $templine [0]" ) && ( " $release " < " $templine [2]" )) {
267 push ( @updatepaks , $name );
268 if ( " $forweb " eq "forweb" ) {
269 print "<option value= \" $name \" >Update: $name -- Version: $version -> $templine [1] -- Release: $release -> $templine [2]</option> \n " ;
271 print "Update: $name \n Version: $version -> $templine [1] \n Release: $release -> $templine [2] \n\n " ;
280 foreach $line ( sort @db ) {
281 next unless ( $line =~ /.*;.*;.*;/ );
282 @templine = split ( /\;/ , $line );
283 if ( " $filter " eq "notinstalled" ) {
284 next if ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
285 } elsif ( " $filter " eq "installed" ) {
286 next unless ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
288 if ( " $forweb " eq "forweb" ) {
289 print "<option value= \" $templine [0] \" > $templine [0]- $templine [1]- $templine [2]</option> \n " ;
291 print "Name: $templine [0] \n ProgVersion: $templine [1] \n Release: $templine [2] \n\n " ;
303 message
( "## Resolving dependencies for $pak ..." );
305 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
310 my ( @templine , @deps , @tempdeps , @all );
311 foreach $line ( @file ) {
312 @templine = split ( /\: / , $line );
313 if ( " $templine [0]" eq "Dependencies" ) {
314 @deps = split ( / / , $templine [ 1 ]);
320 my $return = & isinstalled
( $_ );
322 message
( "### Dependency is already installed: $_ " );
324 message
( "### Need to install dependency: $_ " );
331 foreach ( @tempdeps ) {
333 my @newdeps = resolvedeps
( " $_ " );
335 unless (( $_ eq " " ) || ( $_ eq "" )) {
336 my $return = & isinstalled
( $_ );
338 message
( "### Dependency is already installed: $_ " );
340 message
( "### Need to install dependency: $_ " );
355 if ( " $dir " eq "meta" ) {
356 $path = " $Conf ::dbdir/meta" ;
357 } elsif ( " $dir " eq "tmp" ) {
358 $path = " $Conf ::tmpdir" ;
362 my @files = readdir ( DIR
);
365 unless (( $_ eq "." ) || ( $_ eq ".." )) {
374 logger
( "Going to download meta- $pak ." );
376 unless ( - e
" $Conf ::dbdir/meta/meta- $pak " ) {
377 fetchfile
( "meta/meta- $pak " , "" );
378 move
( " $Conf ::cachedir/meta- $pak " , " $Conf ::dbdir/meta/meta- $pak " );
381 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
385 open ( FILE
, "> $Conf ::dbdir/meta/meta- $pak " );
388 $string =~ s/\r\n/\n/g ;
400 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
406 foreach $line ( @file ) {
407 @templine = split ( /\: / , $line );
408 if ( " $templine [0]" eq "Size" ) {
421 my $file = getpak
( " $pak " , "noforce" );
423 my $return = system ( "cd $Conf ::tmpdir/ && gpg -d < $Conf ::cachedir/ $file | tar x >/dev/null 2>&1" );
425 logger
( "Decryption process returned the following: $return " );
426 if ( $return != 0 ) { exit 1 ; }
435 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
442 foreach $line ( @file ) {
443 @templine = split ( /\: / , $line );
444 if ( " $templine [0]" eq "File" ) {
446 $file = $templine [ 1 ];
451 message
( "No filename given in meta-file. Please phone the developers." );
455 unless ( " $force " eq "force" ) {
456 if ( - e
" $Conf ::cachedir/ $file " ) {
461 fetchfile
( "paks/ $file " , "" );
468 message
( "################################################################################" );
469 message
( "# --> Installing: $pak " );
470 message
( "################################################################################" );
474 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./install.sh >> $Conf ::logdir/install- $pak .log 2>&1" );
476 if ( $pakfiresettings { 'UUID' } ne "off" ) {
477 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&ipak= $pak &return= $return " , " $Conf ::mainserver" );
480 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
482 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
483 message
( "Setup completed. Congratulations!" );
484 message
( "################################################################################" );
486 message
( "Setup returned: $return . Sorry. Please search our forum to find a solution for this problem." );
494 if ( open ( FILE
, "< $Conf ::dbdir/installed/meta- $pak " ) ) {
505 message
( "We are going to upgrade: $pak " );
509 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./update.sh >> $Conf ::logdir/update- $pak .log 2>&1" );
511 if ( $pakfiresettings { 'UUID' } ne "off" ) {
512 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&upak= $pak &return= $return " , " $Conf ::mainserver" );
515 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
517 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
518 message
( "Upgrade completed. Congratulations!" );
520 message
( "Setup returned: $return . Sorry. Please search our forum to find a solution for this problem." );
529 message
( "We are going to uninstall: $pak " );
533 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./uninstall.sh >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
535 if ( $pakfiresettings { 'UUID' } ne "off" ) {
536 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&dpak= $pak &return= $return " , " $Conf ::mainserver" );
539 open ( FILE
, "< $Conf ::dbdir/rootfiles/ $pak " );
545 system ( "echo \" Removing: $line \" >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
546 system ( "cd / && rm -rf $line >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
548 unlink ( " $Conf ::dbdir/rootfiles/ $pak " );
550 message
( "Uninstall completed. Congratulations!" );
552 message
( "Setup returned: $return . Sorry. Please search our forum to find a solution for this problem." );
560 $size = $size / 1024 ;
564 $size = ( $size / 1024 );
569 $size = sprintf ( "%.2f" , $size );
570 my $string = " $size $unit " ;
575 unless ( - e
" $Conf ::dbdir/uuid" ) {
576 open ( FILE
, "</proc/sys/kernel/random/uuid" );
580 open ( FILE
, "> $Conf ::dbdir/uuid" );
589 if ( $pakfiresettings { 'UUID' } ne "off" ) {
590 unless ( " $Conf ::uuid" ) {
591 $Conf :: uuid
= `cat $Conf ::dbdir/uuid` ;
593 logger
( "Sending my uuid: $Conf ::uuid" );
594 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid" , " $Conf ::mainserver" );
595 system ( "rm -f $Conf ::tmpdir/counter* 2>/dev/null" );
600 my $myid = "64D96617" ; # Our own gpg-key
601 my $trustid = "65D0FD58" ; # Id of CaCert
602 my $ret = system ( "gpg --list-keys | grep -q $myid " );
603 unless ( " $ret " eq "0" ) {
604 message
( "The GnuPG isn't configured corectly. Trying now to fix this." );
605 system ( "gpg --keyserver wwwkeys.de.pgp.net --always-trust --recv-key $myid " );
606 system ( "gpg --keyserver wwwkeys.de.pgp.net --always-trust --recv-key $trustid " );