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