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