]>
git.ipfire.org Git - 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 a pak." );
32 & Pakfire
:: message
( " <list> - Outputs a short list with all available paks." );
39 $p = Net
:: Ping
-> new ();
40 if ( $p -> ping ( $host )) {
41 logger
( " $host is alive." );
44 logger
( " $host is dead." );
53 my ( @server , $host , $proto , $file , $allok , $i );
56 $bfile = basename
( " $getfile " );
59 while (( $allok == 0 ) && $i < 5 ) {
62 if ( " $gethost " eq "" ) {
63 @server = selectmirror
();
66 $file = " $server [2]/ $getfile " ;
72 $proto = "HTTP" unless $proto ;
74 logger
( "Trying to get $file from $host ( $proto )." );
76 my $ua = LWP
:: UserAgent
-> new ;
77 $ua -> agent ( "Pakfire/ $Conf ::version" );
81 & General
:: readhash
( "${General::swroot}/proxy/advanced/settings" , \
%proxysettings );
83 if ( $_ = $proxysettings { 'UPSTREAM_PROXY' }) {
84 my ( $peer , $peerport ) = ( /^(?:[a-zA-Z ]+\:\/ \
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/ .* ?
) ?
$/);
85 if ( $proxysettings { 'UPSTREAM_USER' }) {
86 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_USER'}: $proxysettings {'UPSTREAM_PASSWORD'}@" . " $peer : $peerport /" );
88 $ua -> proxy ( "http" , "http:// $peer : $peerport /" );
92 my $response = $ua -> get ( "http:// $host / $file " );
94 my $code = $response -> code ();
95 my $log = $response -> status_line ;
96 logger
( "HTTP-Status-Code: $code - $log " );
98 if ( $code eq "500" ) {
99 message
( "Giving up: There was no chance to get teh file \" $getfile \" from any available server. \n There was an error on the way. Please fix it." );
103 if ( $response -> is_success ) {
104 if ( open ( FILE
, "> $Conf ::cachedir/ $bfile " )) {
105 print FILE
$response -> content ;
107 logger
( "Download successfully done from $host (file: $file )." );
111 logger
( "Could not open $Conf ::cachedir/ $bfile for writing." );
114 logger
( "Download $file failed from $host ( $proto ): $log " );
117 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." );
124 logger
( "Try to get a mirror list." );
126 fetchfile
( "lists/ $Conf ::version-server-list" , " $Conf ::mainserver" );
127 move
( " $Conf ::cachedir/ $Conf ::version-server-list" , " $Conf ::dbdir/lists/ $Conf ::version-server-list" );
131 ### Check if there is a current server list and read it.
132 # If there is no list try to get one.
134 while (!( open ( FILE
, "< $Conf ::dbdir/lists/ $Conf ::version-server-list" )) && ( $count lt 5 )) {
139 message
( "Could not find or download a server list." );
145 ### Count the number of the servers in the list
150 logger
( " $scount servers found in list." );
152 ### Choose a random server and test if it is online
153 # If the check fails try a new server.
154 # This will never give up.
157 while ( $found == 0 ) {
158 $server = int ( rand ( $scount ) + 1 );
160 my ( $line , $proto , $path , $host );
162 foreach $line ( @lines ) {
164 if ( $servers eq $server ) {
165 @templine = split ( /\;/ , $line );
166 $proto = $templine [ 0 ];
167 $host = $templine [ 1 ];
168 $path = $templine [ 2 ];
169 if ( pinghost
( " $host " )) {
171 return ( $proto , $host , $path );
179 ### Update the database if the file is older than one day.
180 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
181 # Usage is always with an argument.
187 if ( - e
" $Conf ::dbdir/lists/packages_list.db" ) {
188 my @stat = stat ( " $Conf ::dbdir/lists/packages_list.db" );
190 $age = $time - $stat [ 9 ];
196 if (( " $age " gt 86400 ) || ( " $force " eq "force" )) {
198 fetchfile
( "lists/packages_list.db" , "" );
199 move
( " $Conf ::cachedir/packages_list.db" , " $Conf ::dbdir/lists/packages_list.db" );
204 ### This subroutine lists the packages.
205 # You may also pass a filter: &Pakfire::dblist(filter)
206 # Usage is always with two arguments.
207 # filter may be: all, notinstalled, installed
214 my ( $name , $version , $release );
217 ### Make sure that the list is not outdated.
218 dbgetlist
( "noforce" );
220 open ( FILE
, "< $Conf ::dbdir/lists/packages_list.db" );
224 if ( " $filter " eq "upgrade" ) {
225 opendir ( DIR
, " $Conf ::dbdir/meta" );
226 my @files = readdir ( DIR
);
228 foreach $file ( @files ) {
229 next if ( $file eq "." );
230 next if ( $file eq ".." );
231 open ( FILE
, "< $Conf ::dbdir/meta/ $file " );
234 foreach $line ( @meta ) {
235 @templine = split ( /\: / , $line );
236 if ( " $templine [0]" eq "Name" ) {
237 $name = $templine [ 1 ];
239 } elsif ( " $templine [0]" eq "ProgVersion" ) {
240 $version = $templine [ 1 ];
242 } elsif ( " $templine [0]" eq "Release" ) {
243 $release = $templine [ 1 ];
247 foreach $prog ( @db ) {
248 @templine = split ( /\;/ , $prog );
249 if (( " $name " eq " $templine [0]" ) && ( " $release " < " $templine [2]" )) {
250 if ( " $forweb " eq "forweb" ) {
251 print "<option value= \" $name \" >Update: $name -- Version: $version -> $templine [1] -- Release: $release -> $templine [2]</option> \n " ;
253 print "Update: $name \n Version: $version -> $templine [1] \n Release: $release -> $templine [2] \n\n " ;
261 foreach $line ( sort @db ) {
262 @templine = split ( /\;/ , $line );
263 if ( " $filter " eq "notinstalled" ) {
264 next if ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
265 } elsif ( " $filter " eq "installed" ) {
266 next unless ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
268 if ( " $forweb " eq "forweb" ) {
269 print "<option value= \" $templine [0] \" > $templine [0]- $templine [1]- $templine [2]</option> \n " ;
271 print "Name: $templine [0] \n ProgVersion: $templine [1] \n Release: $templine [2] \n\n " ;
282 message
( " \n ## Resolving dependencies for $pak ..." );
284 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
289 my ( @templine , @deps , @tempdeps );
290 foreach $line ( @file ) {
291 @templine = split ( /\: / , $line );
292 if ( " $templine [0]" eq "Dependencies" ) {
293 @deps = split ( / / , $templine [ 1 ]);
299 message
( "### Found dependency: $_ " );
304 #my @tempdeps = @deps;
305 foreach ( @tempdeps ) {
307 my @newdeps = resolvedeps
( " $_ " );
309 unless (( $_ eq " " ) || ( $_ eq "" )) {
310 message
( "### Found dependency: $_ " );
324 if ( " $dir " eq "meta" ) {
325 $path = " $Conf ::dbdir/meta" ;
326 } elsif ( " $dir " eq "tmp" ) {
327 $path = " $Conf ::tmpdir" ;
331 my @files = readdir ( DIR
);
334 unless (( $_ eq "." ) || ( $_ eq ".." )) {
343 logger
( "Going to download meta- $pak ." );
345 unless ( - e
" $Conf ::dbdir/meta/meta- $pak " ) {
346 fetchfile
( "meta/meta- $pak " , "" );
347 move
( " $Conf ::cachedir/meta- $pak " , " $Conf ::dbdir/meta/meta- $pak " );
350 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
354 open ( FILE
, "> $Conf ::dbdir/meta/meta- $pak " );
357 $string =~ s/\r\n/\n/g ;
369 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
375 foreach $line ( @file ) {
376 @templine = split ( /\: / , $line );
377 if ( " $templine [0]" eq "Size" ) {
390 my $file = getpak
( " $pak " , "noforce" );
392 my $return = system ( "cd $Conf ::tmpdir/ && gpg -d < $Conf ::cachedir/ $file | cpio -i >/dev/null 2>&1" );
394 logger
( "Decryption process returned the following: $return " );
395 if ( $return != 0 ) { exit 1 ; }
404 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
411 foreach $line ( @file ) {
412 @templine = split ( /\: / , $line );
413 if ( " $templine [0]" eq "File" ) {
415 $file = $templine [ 1 ];
420 message
( "No filename given in meta-file. Please phone the developers." );
424 unless ( " $force " eq "force" ) {
425 if ( - e
" $Conf ::cachedir/ $file " ) {
430 fetchfile
( "paks/ $file " , "" );
437 message
( "We are going to install: $pak " );
441 my $return = system ( "cd $Conf ::tmpdir && ./install.sh >> $Conf ::logdir/install- $pak .log 2>&1" );
444 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
446 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
447 message
( "Setup completed. Congratulations!" );
449 message
( "Setup returned: $return . Sorry. Please search our forum to find a solution for this problem." );
458 message
( "We are going to update: $pak " );
462 my $return = system ( "cd $Conf ::tmpdir && ./update.sh >> $Conf ::logdir/update- $pak .log 2>&1" );
464 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
466 message
( "Update completed. Congratulations!" );
468 message
( "Setup returned: $return . Sorry. Please search our forum to find a solution for this problem." );
477 message
( "We are going to uninstall: $pak " );
481 my $return = system ( "cd $Conf ::tmpdir && ./uninstall.sh >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
483 open ( FILE
, "< $Conf ::dbdir/rootfiles/ $pak " );
489 system ( "echo \" Removing: $line \" >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
490 system ( "cd / && rm -rf $line >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
492 unlink ( " $Conf ::dbdir/rootfiles/ $pak " );
494 message
( "Uninstall completed. Congratulations!" );
496 message
( "Setup returned: $return . Sorry. Please search our forum to find a solution for this problem." );
504 $size = $size / 1024 ;
508 $size = ( $size / 1024 );
513 $size = sprintf ( "%.2f" , $size );
514 my $string = " $size $unit " ;
519 unless ( - e
" $Conf ::dbdir/uuid" ) {
520 open ( FILE
, "</proc/sys/kernel/random/uuid" );
524 open ( FILE
, "> $Conf ::dbdir/uuid" );
533 if ( $pakfiresettings { 'UUID' } eq "on" ) {
534 unless ( " $Conf ::uuid" ) {
535 $Conf :: uuid
= `cat $Conf ::dbdir/uuid` ;
537 logger
( "Sending my uuid: $Conf ::uuid" );
538 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid" , " $Conf ::mainserver" );
539 system ( "rm -f $Conf ::cachedir/counter* 2>/dev/null" );
545 if ( " $status " eq "on" ) {
546 system ( "touch /opt/pakfire/pakfire.lock" );
547 system ( "chmod 777 /opt/pakfire/pakfire.lock" );
548 logger
( "Created lock" );
550 if ( system ( "rm -f /opt/pakfire/pakfire.lock >/dev/null 2>&1" )) {
551 logger
( "Successfully removed lock." );
553 logger
( "Couldn't remove lock." );
560 my $myid = "64D96617" ; # Our own gpg-key
561 my $trustid = "65D0FD58" ; # Id of CaCert
562 my $ret = system ( "gpg --list-keys | grep -q $myid " );
563 unless ( " $ret " eq "0" ) {
564 message
( "The GnuPG isn't configured corectly. Trying now to fix this." );
565 system ( "gpg --keyserver wwwkeys.de.pgp.net --always-trust --recv-key $myid " );
566 system ( "gpg --keyserver wwwkeys.de.pgp.net --always-trust --recv-key $trustid " );