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