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