]> git.ipfire.org Git - ipfire-2.x.git/blame - src/pakfire/lib/functions.pl
pakfire: Remove mirror health check
[ipfire-2.x.git] / src / pakfire / lib / functions.pl
CommitLineData
1bd42c89 1#!/usr/bin/perl -w
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
9f1f68f1 5# Copyright (C) 2007-2015 IPFire Team <info@ipfire.org> #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
1bd42c89
MT
21
22require "/opt/pakfire/etc/pakfire.conf";
4b122800 23require "/var/ipfire/general-functions.pl";
1bd42c89
MT
24
25use File::Basename;
26use File::Copy;
27use LWP::UserAgent;
4d504812 28use HTTP::Response;
a6d327a7
MT
29use HTTP::Headers;
30use HTTP::Message;
31use HTTP::Request;
1bd42c89
MT
32use Net::Ping;
33
66a0f364
PM
34use Switch;
35
1bd42c89
MT
36package Pakfire;
37
3e29608f
MT
38my @VALID_KEY_FINGERPRINTS = (
39 # 2018
40 "3ECA8AA4478208B924BB96206FEF7A8ED713594B",
41 # 2007
42 "179740DC4D8C47DC63C099C74BDE364C64D96617",
43);
44
35f38a8b
MT
45# A small color-hash :D
46my %color;
47 $color{'normal'} = "\033[0m";
48 $color{'black'} = "\033[0;30m";
49 $color{'darkgrey'} = "\033[1;30m";
50 $color{'blue'} = "\033[0;34m";
51 $color{'lightblue'} = "\033[1;34m";
52 $color{'green'} = "\033[0;32m";
53 $color{'lightgreen'} = "\033[1;32m";
54 $color{'cyan'} = "\033[0;36m";
55 $color{'lightcyan'} = "\033[1;36m";
56 $color{'red'} = "\033[0;31m";
57 $color{'lightred'} = "\033[1;31m";
58 $color{'purple'} = "\033[0;35m";
59 $color{'lightpurple'} = "\033[1;35m";
60 $color{'brown'} = "\033[0;33m";
61 $color{'lightgrey'} = "\033[0;37m";
62 $color{'yellow'} = "\033[1;33m";
63 $color{'white'} = "\033[1;37m";
750c1528 64our $enable_colors = 1;
35f38a8b 65
a6d327a7
MT
66my $final_data;
67my $total_size;
68my $bfile;
69
4b122800
MT
70my %pakfiresettings = ();
71&General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
72
1bd42c89
MT
73sub message {
74 my $message = shift;
35f38a8b 75
1bd42c89 76 logger("$message");
750c1528
MT
77 if ( $enable_colors == 1 ) {
78 if ("$message" =~ /ERROR/) {
79 $message = "$color{'red'}$message$color{'normal'}";
80 } elsif ("$message" =~ /INFO/) {
81 $message = "$color{'cyan'}$message$color{'normal'}";
82 } elsif ("$message" =~ /WARN/) {
83 $message = "$color{'yellow'}$message$color{'normal'}";
84 } elsif ("$message" =~ /RESV/) {
85 $message = "$color{'purple'}$message$color{'normal'}";
86 } elsif ("$message" =~ /INST/) {
87 $message = "$color{'green'}$message$color{'normal'}";
88 } elsif ("$message" =~ /REMV/) {
89 $message = "$color{'lightred'}$message$color{'normal'}";
90 } elsif ("$message" =~ /UPGR/) {
91 $message = "$color{'lightblue'}$message$color{'normal'}";
92 }
35f38a8b
MT
93 }
94 print "$message\n";
95
1bd42c89
MT
96}
97
98sub logger {
99 my $log = shift;
9ced24a8 100 if ($log) {
c506cad0
CS
101 #system("echo \"`date`: $log\" >> /var/log/pakfire.log");
102 system("logger -t pakfire \"$log\"");
9ced24a8 103 }
1bd42c89
MT
104}
105
5b2a12ff 106sub usage {
750c1528 107 &Pakfire::message("Usage: pakfire <install|remove> [options] <pak(s)>");
5b2a12ff 108 &Pakfire::message(" <update> - Contacts the servers for new lists of paks.");
99e6df8e 109 &Pakfire::message(" <upgrade> - Installs the latest version of all paks.");
5b2a12ff 110 &Pakfire::message(" <list> - Outputs a short list with all available paks.");
99e6df8e 111 &Pakfire::message("");
750c1528
MT
112 &Pakfire::message(" Global options:");
113 &Pakfire::message(" --non-interactive --> Enables the non-interactive mode.");
114 &Pakfire::message(" You won't see any question here.");
115 &Pakfire::message(" -y --> Short for --non-interactive.");
116 &Pakfire::message(" --no-colors --> Turns off the wonderful colors.");
117 &Pakfire::message("");
5b2a12ff
MT
118 exit 1;
119}
120
1bd42c89 121sub fetchfile {
4d504812
MT
122 my $getfile = shift;
123 my $gethost = shift;
377560fb
MT
124 my (@server, $host, $proto, $file, $i);
125 my $allok = 0;
1bd42c89
MT
126
127 use File::Basename;
4d504812 128 $bfile = basename("$getfile");
377560fb
MT
129
130 logger("DOWNLOAD STARTED: $getfile") unless ($bfile =~ /^counter\?.*/);
1bd42c89 131
4d504812
MT
132 $i = 0;
133 while (($allok == 0) && $i < 5) {
134 $i++;
135
136 if ("$gethost" eq "") {
137 @server = selectmirror();
138 $proto = $server[0];
139 $host = $server[1];
140 $file = "$server[2]/$getfile";
1bd42c89 141 } else {
4d504812 142 $host = $gethost;
afabe9f7 143 $file = $getfile;
1bd42c89 144 }
4d504812
MT
145
146 $proto = "HTTP" unless $proto;
147
a6d327a7
MT
148 unless ($bfile =~ /^counter\?.*/) {
149 logger("DOWNLOAD INFO: Host: $host ($proto) - File: $file");
a6d327a7 150 }
1bd42c89 151
4d504812
MT
152 my $ua = LWP::UserAgent->new;
153 $ua->agent("Pakfire/$Conf::version");
3d3b68c5 154 $ua->timeout(20);
4b122800
MT
155
156 my %proxysettings=();
157 &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings);
158
99e6df8e 159 if ($proxysettings{'UPSTREAM_PROXY'}) {
4c7fa778 160 logger("DOWNLOAD INFO: Upstream proxy: \"$proxysettings{'UPSTREAM_PROXY'}\"") unless ($bfile =~ /^counter.py\?.*/);
4b122800 161 if ($proxysettings{'UPSTREAM_USER'}) {
4b072d64 162 $ua->proxy([["http", "https"] => "http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$proxysettings{'UPSTREAM_PROXY'}/"]);
4c7fa778 163 logger("DOWNLOAD INFO: Logging in with: \"$proxysettings{'UPSTREAM_USER'}\" - \"$proxysettings{'UPSTREAM_PASSWORD'}\"") unless ($bfile =~ /^counter.py\?.*/);
4b122800 164 } else {
4b072d64 165 $ua->proxy([["http", "https"] => "http://$proxysettings{'UPSTREAM_PROXY'}/"]);
4b122800
MT
166 }
167 }
a6d327a7
MT
168
169 $final_data = undef;
66a0f364
PM
170
171 my $url;
172 switch ($proto) {
173 case "HTTP" { $url = "http://$host/$file"; }
174 case "HTTPS" { $url = "https://$host/$file"; }
175 else {
176 # skip all lines with unknown protocols
177 logger("DOWNLOAD WARNING: Skipping Host: $host due to unknown protocol ($proto) in mirror database");
178 next;
179 }
180 }
181
a6d327a7
MT
182 my $response;
183
4c7fa778 184 unless ($bfile =~ /^counter.py\?.*/) {
a6d327a7
MT
185 my $result = $ua->head($url);
186 my $remote_headers = $result->headers;
187 $total_size = $remote_headers->content_length;
188 logger("DOWNLOAD INFO: $file has size of $total_size bytes");
189
190 $response = $ua->get($url, ':content_cb' => \&callback );
191 message("");
192 } else {
193 $response = $ua->get($url);
194 }
4d504812 195
4b122800
MT
196 my $code = $response->code();
197 my $log = $response->status_line;
a6d327a7 198 logger("DOWNLOAD INFO: HTTP-Status-Code: $code - $log");
4b122800
MT
199
200 if ( $code eq "500" ) {
e44b26cf 201 message("Giving up: There was no chance to get the file \"$getfile\" from any available server.\nThere was an error on the way. Please fix it.");
4b122800
MT
202 return 1;
203 }
204
4d504812 205 if ($response->is_success) {
4c7fa778 206 unless ($bfile =~ /^counter.py\?.*/) {
35f38a8b
MT
207 if (open(FILE, ">$Conf::tmpdir/$bfile")) {
208 print FILE $final_data;
209 close(FILE);
a6d327a7 210 logger("DOWNLOAD INFO: File received. Start checking signature...");
3e29608f 211 if (&valid_signature("$Conf::tmpdir/$bfile")) {
a6d327a7 212 logger("DOWNLOAD INFO: Signature of $bfile is fine.");
06209efc
MT
213 move("$Conf::tmpdir/$bfile","$Conf::cachedir/$bfile");
214 } else {
a6d327a7 215 message("DOWNLOAD ERROR: The downloaded file ($file) wasn't verified by IPFire.org. Sorry - Exiting...");
28593a79
CS
216 my $ntp = `ntpdate -q -t 10 pool.ntp.org 2>/dev/null | tail -1`;
217 if ( $ntp !~ /time\ server(.*)offset(.*)/ ){message("TIME ERROR: Unable to get the nettime, this may lead to the verification error.");}
218 else { $ntp =~ /time\ server(.*)offset(.*)/; message("TIME INFO: Time Server$1has$2 offset to localtime.");}
06209efc
MT
219 exit 1;
220 }
35f38a8b
MT
221 logger("DOWNLOAD FINISHED: $file");
222 $allok = 1;
223 return 0;
224 } else {
4a979780 225 logger("DOWNLOAD ERROR: Could not open $Conf::tmpdir/$bfile for writing.");
186e3d2c 226 }
4d504812 227 } else {
35f38a8b 228 return 0;
4d504812
MT
229 }
230 } else {
a6d327a7 231 logger("DOWNLOAD ERROR: $log");
4d504812 232 }
1bd42c89 233 }
a6d327a7 234 message("DOWNLOAD ERROR: There was no chance to get the file \"$getfile\" from any available server.\nMay be you should run \"pakfire update\" to get some new servers.");
4d504812 235 return 1;
1bd42c89
MT
236}
237
238sub getmirrors {
377560fb
MT
239 my $force = shift;
240 my $age;
241
1bd42c89 242 use File::Copy;
1bd42c89 243
2aa6d448
MT
244 if ( -e "$Conf::dbdir/lists/server-list.db" ) {
245 my @stat = stat("$Conf::dbdir/lists/server-list.db");
e3670217
MT
246 my $time = time();
247 $age = $time - $stat[9];
377560fb
MT
248 $force = "force" if ("$age" >= "3600");
249 logger("MIRROR INFO: server-list.db is $age seconds old. - DEBUG: $force");
e3670217
MT
250 } else {
251 # Force an update.
377560fb 252 $force = "force";
e3670217
MT
253 }
254
377560fb 255 if ("$force" eq "force") {
e3670217
MT
256 fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver");
257 move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db");
258 }
1bd42c89
MT
259}
260
2aa6d448 261sub getcoredb {
377560fb
MT
262 my $force = shift;
263 my $age;
264
2aa6d448 265 use File::Copy;
2aa6d448
MT
266
267 if ( -e "$Conf::dbdir/lists/core-list.db" ) {
268 my @stat = stat("$Conf::dbdir/lists/core-list.db");
269 my $time = time();
270 $age = $time - $stat[9];
377560fb
MT
271 $force = "force" if ("$age" >= "3600");
272 logger("CORE INFO: core-list.db is $age seconds old. - DEBUG: $force");
2aa6d448
MT
273 } else {
274 # Force an update.
377560fb 275 $force = "force";
2aa6d448
MT
276 }
277
377560fb 278 if ("$force" eq "force") {
2aa6d448
MT
279 fetchfile("lists/core-list.db", "");
280 move("$Conf::cachedir/core-list.db", "$Conf::dbdir/lists/core-list.db");
281 }
282}
283
3e29608f
MT
284sub valid_signature($) {
285 my $filename = shift;
286
287 open(my $cmd, "gpg --verify --status-fd 1 \"$filename\" 2>/dev/null |");
288 while (<$cmd>) {
289 # Process valid signature lines
290 if (/VALIDSIG ([A-Z0-9]+)/) {
291 # Check if we know the key
292 foreach my $key (@VALID_KEY_FINGERPRINTS) {
293 # Signature is valid
294 return 1 if ($key eq $1);
295 }
296 }
297 }
298 close($cmd);
299
300 # Signature is invalid
301 return 0;
302}
2aa6d448 303
1bd42c89
MT
304sub selectmirror {
305 ### Check if there is a current server list and read it.
306 # If there is no list try to get one.
307 my $count = 0;
3d3b68c5 308 while (!(open(FILE, "<$Conf::dbdir/lists/server-list.db")) && ($count lt 5)) {
1bd42c89 309 $count++;
377560fb 310 getmirrors("noforce");
1bd42c89 311 }
3d3b68c5 312 if ($count == 5) {
a6d327a7 313 message("MIRROR ERROR: Could not find or download a server list");
1bd42c89
MT
314 exit 1;
315 }
316 my @lines = <FILE>;
317 close(FILE);
318
319 ### Count the number of the servers in the list
320 my $scount = 0;
e44b26cf 321 my @newlines;
1bd42c89 322 foreach (@lines) {
e44b26cf
MT
323 if ("$_" =~ /.*;.*;.*;/ ) {
324 push(@newlines,$_);
325 $scount++;
326 }
1bd42c89 327 }
a6d327a7 328 logger("MIRROR INFO: $scount servers found in list");
63efc01c
MT
329
330 if ($scount eq 0) {
331 logger("MIRROR INFO: Could not find any servers. Falling back to main server $Conf::mainserver");
332 return ("HTTP", $Conf::mainserver, "/$Conf::version");
333 }
334
1bd42c89
MT
335 ### Choose a random server and test if it is online
336 # If the check fails try a new server.
337 # This will never give up.
1bd42c89 338 my $servers = 0;
e32591e7 339 while (1) {
1bd42c89
MT
340 $server = int(rand($scount) + 1);
341 $servers = 0;
342 my ($line, $proto, $path, $host);
343 my @templine;
e44b26cf 344 foreach $line (@newlines) {
1bd42c89
MT
345 $servers++;
346 if ($servers eq $server) {
347 @templine = split(/\;/, $line);
348 $proto = $templine[0];
349 $host = $templine[1];
350 $path = $templine[2];
e32591e7
MT
351
352 return ($proto, $host, $path);
1bd42c89
MT
353 }
354 }
4d504812 355 }
1bd42c89
MT
356}
357
358sub dbgetlist {
359 ### Update the database if the file is older than one day.
360 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
361 # Usage is always with an argument.
362 my $force = shift;
363 my $age;
364
365 use File::Copy;
366
367 if ( -e "$Conf::dbdir/lists/packages_list.db" ) {
368 my @stat = stat("$Conf::dbdir/lists/packages_list.db");
369 my $time = time();
370 $age = $time - $stat[9];
377560fb
MT
371 $force = "force" if ("$age" >= "3600");
372 logger("DB INFO: packages_list.db is $age seconds old. - DEBUG: $force");
1bd42c89
MT
373 } else {
374 # Force an update.
377560fb 375 $force = "force";
1bd42c89
MT
376 }
377
377560fb 378 if ("$force" eq "force") {
1bd42c89
MT
379 fetchfile("lists/packages_list.db", "");
380 move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db");
381 }
ff9545f8
AF
382
383 # Update the meta database if new packages was in the package list
384 my @meta;
385 my $file;
386 my $line;
387 my $prog;
388 my ($name, $version, $release);
389 my @templine;
390
391 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
392 my @db = <FILE>;
393 close(FILE);
394
395 opendir(DIR,"$Conf::dbdir/meta");
396 my @files = readdir(DIR);
397 closedir(DIR);
398 foreach $file (@files) {
399 next if ( $file eq "." );
400 next if ( $file eq ".." );
1af34aa8 401 next if ( $file eq "meta-" );
ff9545f8
AF
402 next if ( $file =~ /^old/ );
403 open(FILE, "<$Conf::dbdir/meta/$file");
404 @meta = <FILE>;
405 close(FILE);
406 foreach $line (@meta) {
407 @templine = split(/\: /,$line);
408 if ("$templine[0]" eq "Name") {
409 $name = $templine[1];
410 chomp($name);
411 } elsif ("$templine[0]" eq "ProgVersion") {
412 $version = $templine[1];
413 chomp($version);
414 } elsif ("$templine[0]" eq "Release") {
415 $release = $templine[1];
416 chomp($release);
417 }
418 }
419 foreach $prog (@db) {
420 @templine = split(/\;/,$prog);
421 if (("$name" eq "$templine[0]") && ("$release" ne "$templine[2]")) {
422 move("$Conf::dbdir/meta/meta-$name","$Conf::dbdir/meta/old_meta-$name");
423 fetchfile("meta/meta-$name", "");
424 move("$Conf::cachedir/meta-$name", "$Conf::dbdir/meta/meta-$name");
425 }
426 }
427 }
1bd42c89
MT
428}
429
430sub dblist {
431 ### This subroutine lists the packages.
432 # You may also pass a filter: &Pakfire::dblist(filter)
433 # Usage is always with two arguments.
434 # filter may be: all, notinstalled, installed
435 my $filter = shift;
436 my $forweb = shift;
4b122800 437 my @meta;
99e6df8e 438 my @updatepaks;
4b122800
MT
439 my $file;
440 my $line;
441 my $prog;
442 my ($name, $version, $release);
443 my @templine;
1bd42c89
MT
444
445 ### Make sure that the list is not outdated.
377560fb 446 #dbgetlist("noforce");
1bd42c89
MT
447
448 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
449 my @db = <FILE>;
450 close(FILE);
4b122800
MT
451
452 if ("$filter" eq "upgrade") {
4638fd92 453 if ("$forweb" ne "forweb" && "$forweb" ne "notice" ) {getcoredb("noforce");}
8a8a50ad 454 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 455 if ("$core_release" > "$Conf::core_mine") {
8a8a50ad
MT
456 if ("$forweb" eq "forweb") {
457 print "<option value=\"core\">Core-Update -- $Conf::version -- Release: $Conf::core_mine -> $core_release</option>\n";
2b38ab5c
JPT
458 }
459 elsif ("$forweb" eq "notice") {
462a5e86 460 print "<br /><br /><br /><a href='pakfire.cgi'>$Lang::tr{'core notice 1'} $Conf::core_mine $Lang::tr{'core notice 2'} $core_release $Lang::tr{'core notice 3'}</a>";
8a8a50ad
MT
461 } else {
462 my $command = "Core-Update $Conf::version\nRelease: $Conf::core_mine -> $core_release\n";
463 if ("$Pakfire::enable_colors" eq "1") {
464 print "$color{'lila'}$command$color{'normal'}\n";
465 } else {
466 print "$command\n";
467 }
468 }
469 }
470
5e932bd5 471 opendir(DIR,"$Conf::dbdir/installed");
4b122800
MT
472 my @files = readdir(DIR);
473 closedir(DIR);
474 foreach $file (@files) {
475 next if ( $file eq "." );
476 next if ( $file eq ".." );
3f01107b 477 next if ( $file =~ /^old/ );
5e932bd5 478 open(FILE, "<$Conf::dbdir/installed/$file");
4b122800
MT
479 @meta = <FILE>;
480 close(FILE);
481 foreach $line (@meta) {
482 @templine = split(/\: /,$line);
483 if ("$templine[0]" eq "Name") {
484 $name = $templine[1];
485 chomp($name);
486 } elsif ("$templine[0]" eq "ProgVersion") {
487 $version = $templine[1];
488 chomp($version);
489 } elsif ("$templine[0]" eq "Release") {
490 $release = $templine[1];
491 chomp($release);
492 }
493 }
494 foreach $prog (@db) {
495 @templine = split(/\;/,$prog);
462a5e86 496 if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" && "$forweb" ne "notice")) {
99e6df8e 497 push(@updatepaks,$name);
4b122800
MT
498 if ("$forweb" eq "forweb") {
499 print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n";
500 } else {
e3670217
MT
501 my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n";
502 if ("$Pakfire::enable_colors" eq "1") {
503 print "$color{'lila'}$command$color{'normal'}\n";
504 } else {
505 print "$command\n";
506 }
4b122800
MT
507 }
508 }
509 }
5b2a12ff 510 }
99e6df8e 511 return @updatepaks;
4b122800
MT
512 } else {
513 my $line;
e3670217 514 my $use_color;
4b122800 515 my @templine;
e3670217 516 my $count;
4b122800 517 foreach $line (sort @db) {
06209efc 518 next unless ($line =~ /.*;.*;.*;/ );
e3670217
MT
519 $use_color = "";
520 $count++;
4b122800
MT
521 @templine = split(/\;/,$line);
522 if ("$filter" eq "notinstalled") {
523 next if ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
524 } elsif ("$filter" eq "installed") {
525 next unless ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
526 }
4e17785f
AF
527 if ("$forweb" eq "forweb")
528 {
529 if ("$filter" eq "notinstalled") {
530 print "<option value=\"$templine[0]\">$templine[0]-$templine[1]-$templine[2]</option>\n";
531 } else {
532 print "<option value=\"$templine[0]\">$templine[0]</option>\n";
533 }
4b122800 534 } else {
e3670217
MT
535 if ("$Pakfire::enable_colors" eq "1") {
536 if (&isinstalled("$templine[0]")) {
537 $use_color = "$color{'red'}"
538 } else {
539 $use_color = "$color{'green'}"
540 }
541 }
542 print "${use_color}Name: $templine[0]\nProgVersion: $templine[1]\nRelease: $templine[2]$color{'normal'}\n\n";
4b122800 543 }
1bd42c89 544 }
e3670217 545 print "$count packages total.\n" unless ("$forweb" eq "forweb");
1bd42c89
MT
546 }
547}
548
621dcd86 549sub resolvedeps_one {
1bd42c89
MT
550 my $pak = shift;
551
552 getmetafile("$pak");
553
35f38a8b 554 message("PAKFIRE RESV: $pak: Resolving dependencies...");
1bd42c89
MT
555
556 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
557 my @file = <FILE>;
558 close(FILE);
559
560 my $line;
621dcd86 561 my (@templine, @deps, @all);
1bd42c89
MT
562 foreach $line (@file) {
563 @templine = split(/\: /,$line);
564 if ("$templine[0]" eq "Dependencies") {
565 @deps = split(/ /, $templine[1]);
566 }
567 }
568 chomp (@deps);
569 foreach (@deps) {
570 if ($_) {
186e3d2c
MT
571 my $return = &isinstalled($_);
572 if ($return eq 0) {
35f38a8b 573 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
186e3d2c 574 } else {
35f38a8b 575 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
186e3d2c
MT
576 push(@all,$_);
577 }
1bd42c89
MT
578 }
579 }
e44b26cf 580
621dcd86
MT
581 return @all;
582}
583
584sub resolvedeps {
585 my $pak = shift;
586 my @all;
587
588 # Resolve all not yet installed dependencies of $pak
589 my @deps = &resolvedeps_one($pak);
590 push(@all, @deps);
591
592 # For each dependency, we check if more dependencies exist
593 while (@deps) {
594 my $dep = pop(@deps);
595
596 my @subdeps = &resolvedeps_one($dep);
597 foreach my $subdep (@subdeps) {
598 # Skip the package we are currently resolving for
599 next if ($pak eq $subdep);
600
601 # If the package is not already to be installed,
602 # we add it to the list (@all) and check if it has
603 # more dependencies on its own.
604 unless (grep {$_ eq $subdep} @all) {
605 push(@deps, $subdep);
606 push(@all, $subdep);
1bd42c89
MT
607 }
608 }
609 }
621dcd86 610
186e3d2c 611 return @all;
1bd42c89
MT
612}
613
9f1f68f1 614sub resolvedeps_recursive {
031becc0 615 my @packages = @_;
9f1f68f1
MT
616 my @result = ();
617
618 foreach my $pkg (@packages) {
619 my @deps = &Pakfire::resolvedeps($pkg);
620
621 foreach my $dep (@deps) {
622 push(@result, $dep);
623 }
624 }
625
626 # Sort the result array and remove dupes
627 my %sort = map{ $_, 1 } @result;
628 @result = keys %sort;
629
630 return @result;
631}
632
1bd42c89
MT
633sub cleanup {
634 my $dir = shift;
635 my $path;
636
35f38a8b
MT
637 logger("CLEANUP: $dir");
638
1bd42c89
MT
639 if ( "$dir" eq "meta" ) {
640 $path = "$Conf::dbdir/meta";
641 } elsif ( "$dir" eq "tmp" ) {
642 $path = "$Conf::tmpdir";
643 }
644 chdir("$path");
645 opendir(DIR,".");
646 my @files = readdir(DIR);
647 closedir(DIR);
648 foreach (@files) {
649 unless (($_ eq ".") || ($_ eq "..")) {
650 system("rm -rf $_");
651 }
652 }
653}
654
655sub getmetafile {
656 my $pak = shift;
657
28593a79
CS
658 unless ( -e "$Conf::dbdir/meta/meta-$pak" ) {
659 fetchfile("meta/meta-$pak", "");
660 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
661 }
662
663 if ( -z "$Conf::dbdir/meta/meta-$pak" ) {
1bd42c89
MT
664 fetchfile("meta/meta-$pak", "");
665 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
666 }
667
668 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
669 my @line = <FILE>;
670 close(FILE);
671
672 open(FILE, ">$Conf::dbdir/meta/meta-$pak");
673 foreach (@line) {
674 my $string = $_;
675 $string =~ s/\r\n/\n/g;
676 print FILE $string;
677 }
678 close(FILE);
679 return 1;
680}
681
682sub getsize {
683 my $pak = shift;
684
685 getmetafile("$pak");
686
687 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
688 my @file = <FILE>;
689 close(FILE);
690
691 my $line;
692 my @templine;
693 foreach $line (@file) {
694 @templine = split(/\: /,$line);
695 if ("$templine[0]" eq "Size") {
696 chomp($templine[1]);
697 return $templine[1];
698 }
699 }
4b122800 700 return 0;
1bd42c89
MT
701}
702
703sub decryptpak {
704 my $pak = shift;
705
706 cleanup("tmp");
707
708 my $file = getpak("$pak", "noforce");
709
a6d327a7 710 logger("DECRYPT STARTED: $pak");
35f38a8b 711 my $return = system("cd $Conf::tmpdir/ && gpg -d --batch --quiet --no-verbose --status-fd 2 --output - < $Conf::cachedir/$file 2>/dev/null | tar x");
99e6df8e 712 $return %= 255;
a6d327a7 713 logger("DECRYPT FINISHED: $pak - Status: $return");
cde0e116 714 if ($return != 0) { exit 1; }
1bd42c89
MT
715}
716
717sub getpak {
718 my $pak = shift;
719 my $force = shift;
720
721 getmetafile("$pak");
722
723 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
724 my @file = <FILE>;
725 close(FILE);
726
727 my $line;
728 my $file;
729 my @templine;
730 foreach $line (@file) {
731 @templine = split(/\: /,$line);
732 if ("$templine[0]" eq "File") {
733 chomp($templine[1]);
734 $file = $templine[1];
735 }
736 }
737
738 unless ($file) {
1af34aa8 739 message("No filename given in meta-file.");
1bd42c89
MT
740 exit 1;
741 }
742
1bd42c89
MT
743 unless ( "$force" eq "force" ) {
744 if ( -e "$Conf::cachedir/$file" ) {
1bd42c89
MT
745 return $file;
746 }
747 }
748
749 fetchfile("paks/$file", "");
750 return $file;
751}
752
753sub setuppak {
754 my $pak = shift;
755
35f38a8b 756 message("PAKFIRE INST: $pak: Decrypting...");
1bd42c89
MT
757 decryptpak("$pak");
758
35f38a8b 759 message("PAKFIRE INST: $pak: Copying files and running post-installation scripts...");
99e6df8e 760 my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1");
cde0e116 761 $return %= 255;
e44b26cf 762 if ($pakfiresettings{'UUID'} ne "off") {
4c7fa778 763 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&ipak=$pak&return=$return", "$Conf::mainserver");
e44b26cf 764 }
1bd42c89
MT
765 if ($return == 0) {
766 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
767 cleanup("tmp");
4d504812 768 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
35f38a8b
MT
769 message("PAKFIRE INST: $pak: Finished.");
770 message("");
1bd42c89 771 } else {
35f38a8b 772 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
773 exit $return;
774 }
a08c3a2e 775 return $return;
1bd42c89
MT
776}
777
2aa6d448 778sub upgradecore {
377560fb 779 getcoredb("noforce");
2aa6d448 780 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 781 if ("$core_release" > "$Conf::core_mine") {
2aa6d448
MT
782 message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release");
783
784 my @seq = `seq $Conf::core_mine $core_release`;
785 shift @seq;
786 my $release;
787 foreach $release (@seq) {
788 chomp($release);
789 getpak("core-upgrade-$release");
790 }
791
792 foreach $release (@seq) {
793 chomp($release);
794 upgradepak("core-upgrade-$release");
795 }
796
797 system("echo $core_release > $Conf::coredir/mine");
798
799 } else {
800 message("CORE ERROR: No new upgrades available. You are on release $Conf::core_mine.");
801 }
802}
803
186e3d2c
MT
804sub isinstalled {
805 my $pak = shift;
806 if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) {
807 close(FILE);
808 return 0;
809 } else {
810 return 1;
811 }
812}
813
99e6df8e 814sub upgradepak {
1bd42c89
MT
815 my $pak = shift;
816
35f38a8b 817 message("PAKFIRE UPGR: $pak: Decrypting...");
1bd42c89
MT
818 decryptpak("$pak");
819
35f38a8b 820 message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts...");
99e6df8e
MT
821 my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1");
822 $return %= 255;
e44b26cf 823 if ($pakfiresettings{'UUID'} ne "off") {
4c7fa778 824 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&upak=$pak&return=$return", "$Conf::mainserver");
e44b26cf 825 }
1bd42c89
MT
826 if ($return == 0) {
827 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
828 cleanup("tmp");
4e4b54c5 829 copy("$Conf::dbdir/meta/meta-$pak", "$Conf::dbdir/installed/");
35f38a8b
MT
830 message("PAKFIRE UPGR: $pak: Finished.");
831 message("");
1bd42c89 832 } else {
35f38a8b 833 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
834 exit $return;
835 }
8e58bd37 836 return $return;
1bd42c89
MT
837}
838
839sub removepak {
840 my $pak = shift;
841
35f38a8b 842 message("PAKFIRE REMV: $pak: Decrypting...");
1bd42c89
MT
843 decryptpak("$pak");
844
35f38a8b 845 message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts...");
99e6df8e
MT
846 my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1");
847 $return %= 255;
e44b26cf 848 if ($pakfiresettings{'UUID'} ne "off") {
4c7fa778 849 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&dpak=$pak&return=$return", "$Conf::mainserver");
e44b26cf 850 }
1bd42c89 851 if ($return == 0) {
1bd42c89 852 unlink("$Conf::dbdir/rootfiles/$pak");
a6d327a7 853 unlink("$Conf::dbdir/installed/meta-$pak");
1bd42c89 854 cleanup("tmp");
35f38a8b
MT
855 message("PAKFIRE REMV: $pak: Finished.");
856 message("");
1bd42c89 857 } else {
35f38a8b 858 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
859 exit $return;
860 }
8e58bd37 861 return $return;
1bd42c89
MT
862}
863
864sub beautifysize {
865 my $size = shift;
35f38a8b 866 #$size = $size / 1024;
a08c3a2e 867 my $unit;
1bd42c89 868
35f38a8b
MT
869 if ($size > 1023*1024) {
870 $size = ($size / (1024*1024));
a08c3a2e 871 $unit = "MB";
35f38a8b
MT
872 } elsif ($size > 1023) {
873 $size = ($size / 1024);
a08c3a2e 874 $unit = "KB";
35f38a8b
MT
875 } else {
876 $unit = "B";
1bd42c89 877 }
a08c3a2e
MT
878 $size = sprintf("%.2f" , $size);
879 my $string = "$size $unit";
880 return $string;
1bd42c89
MT
881}
882
8e58bd37
MT
883sub makeuuid {
884 unless ( -e "$Conf::dbdir/uuid" ) {
8e58bd37
MT
885 open(FILE, "</proc/sys/kernel/random/uuid");
886 my @line = <FILE>;
887 close(FILE);
888
889 open(FILE, ">$Conf::dbdir/uuid");
890 foreach (@line) {
891 print FILE $_;
892 }
893 close(FILE);
894 }
895}
896
897sub senduuid {
99e6df8e 898 if ($pakfiresettings{'UUID'} ne "off") {
4b122800
MT
899 unless("$Conf::uuid") {
900 $Conf::uuid = `cat $Conf::dbdir/uuid`;
901 }
902 logger("Sending my uuid: $Conf::uuid");
4c7fa778 903 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid", "$Conf::mainserver");
06209efc 904 system("rm -f $Conf::tmpdir/counter* 2>/dev/null");
8e58bd37 905 }
8e58bd37 906}
1bd42c89 907
a6d327a7
MT
908sub callback {
909 my ($data, $response, $protocol) = @_;
910 $final_data .= $data;
35f38a8b 911 print progress_bar( length($final_data), $total_size, 30, '=' );
a6d327a7
MT
912}
913
914sub progress_bar {
915 my ( $got, $total, $width, $char ) = @_;
916 my $show_bfile;
35f38a8b 917 $width ||= 30; $char ||= '=';
a6d327a7 918 my $len_bfile = length $bfile;
35f38a8b
MT
919 if ("$len_bfile" >= "17") {
920 $show_bfile = substr($bfile,0,17)."...";
a6d327a7
MT
921 } else {
922 $show_bfile = $bfile;
35f38a8b
MT
923 }
924 $progress = sprintf("%.2f%%", 100*$got/+$total);
925 sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got);
a6d327a7
MT
926}
927
1bd42c89 9281;