]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/pakfire/lib/functions.pl
pakfire: Prevent from get launched multiple times.
[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
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
e6f4991b
MT
73# Make version
74$Conf::version = &make_version();
75
d6c2e671
SS
76# Pakfire lock file.
77our $lockfile = "/tmp/pakfire_lock";
78
1bd42c89
MT
79sub message {
80 my $message = shift;
35f38a8b 81
1bd42c89 82 logger("$message");
750c1528
MT
83 if ( $enable_colors == 1 ) {
84 if ("$message" =~ /ERROR/) {
85 $message = "$color{'red'}$message$color{'normal'}";
86 } elsif ("$message" =~ /INFO/) {
87 $message = "$color{'cyan'}$message$color{'normal'}";
88 } elsif ("$message" =~ /WARN/) {
89 $message = "$color{'yellow'}$message$color{'normal'}";
90 } elsif ("$message" =~ /RESV/) {
91 $message = "$color{'purple'}$message$color{'normal'}";
92 } elsif ("$message" =~ /INST/) {
93 $message = "$color{'green'}$message$color{'normal'}";
94 } elsif ("$message" =~ /REMV/) {
95 $message = "$color{'lightred'}$message$color{'normal'}";
96 } elsif ("$message" =~ /UPGR/) {
97 $message = "$color{'lightblue'}$message$color{'normal'}";
98 }
35f38a8b
MT
99 }
100 print "$message\n";
101
1bd42c89
MT
102}
103
104sub logger {
105 my $log = shift;
9ced24a8 106 if ($log) {
c506cad0
CS
107 #system("echo \"`date`: $log\" >> /var/log/pakfire.log");
108 system("logger -t pakfire \"$log\"");
9ced24a8 109 }
1bd42c89
MT
110}
111
5b2a12ff 112sub usage {
750c1528 113 &Pakfire::message("Usage: pakfire <install|remove> [options] <pak(s)>");
5b2a12ff 114 &Pakfire::message(" <update> - Contacts the servers for new lists of paks.");
99e6df8e 115 &Pakfire::message(" <upgrade> - Installs the latest version of all paks.");
5b2a12ff 116 &Pakfire::message(" <list> - Outputs a short list with all available paks.");
090af02e 117 &Pakfire::message(" <status> - Outputs a summary about available core upgrades, updates and a required reboot");
99e6df8e 118 &Pakfire::message("");
750c1528
MT
119 &Pakfire::message(" Global options:");
120 &Pakfire::message(" --non-interactive --> Enables the non-interactive mode.");
121 &Pakfire::message(" You won't see any question here.");
122 &Pakfire::message(" -y --> Short for --non-interactive.");
123 &Pakfire::message(" --no-colors --> Turns off the wonderful colors.");
124 &Pakfire::message("");
5b2a12ff
MT
125 exit 1;
126}
127
1bd42c89 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 136
06d55142 137 logger("DOWNLOAD STARTED: $getfile");
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 152
c846ed16 153 $proto = "HTTPS" unless $proto;
4d504812 154
06d55142 155 logger("DOWNLOAD INFO: Host: $host ($proto) - File: $file");
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'}) {
06d55142 165 logger("DOWNLOAD INFO: Upstream proxy: \"$proxysettings{'UPSTREAM_PROXY'}\"");
4b122800 166 if ($proxysettings{'UPSTREAM_USER'}) {
d96d5db6 167 $ua->proxy(["http", "https"], "http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$proxysettings{'UPSTREAM_PROXY'}/");
cf33650e 168 logger("DOWNLOAD INFO: Logging in with \"$proxysettings{'UPSTREAM_USER'}\" against \"$proxysettings{'UPSTREAM_PROXY'}\"");
4b122800 169 } else {
d96d5db6 170 $ua->proxy(["http", "https"], "http://$proxysettings{'UPSTREAM_PROXY'}/");
4b122800
MT
171 }
172 }
a6d327a7
MT
173
174 $final_data = undef;
66a0f364
PM
175
176 my $url;
177 switch ($proto) {
178 case "HTTP" { $url = "http://$host/$file"; }
179 case "HTTPS" { $url = "https://$host/$file"; }
180 else {
181 # skip all lines with unknown protocols
182 logger("DOWNLOAD WARNING: Skipping Host: $host due to unknown protocol ($proto) in mirror database");
183 next;
184 }
185 }
186
06d55142
MT
187 my $result = $ua->head($url);
188 my $remote_headers = $result->headers;
189 $total_size = $remote_headers->content_length;
190 logger("DOWNLOAD INFO: $file has size of $total_size bytes");
a6d327a7 191
06d55142
MT
192 my $response = $ua->get($url, ':content_cb' => \&callback );
193 message("");
4d504812 194
4b122800
MT
195 my $code = $response->code();
196 my $log = $response->status_line;
a6d327a7 197 logger("DOWNLOAD INFO: HTTP-Status-Code: $code - $log");
4b122800
MT
198
199 if ( $code eq "500" ) {
e44b26cf 200 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
201 return 1;
202 }
203
4d504812 204 if ($response->is_success) {
06d55142
MT
205 if (open(FILE, ">$Conf::tmpdir/$bfile")) {
206 print FILE $final_data;
207 close(FILE);
208 logger("DOWNLOAD INFO: File received. Start checking signature...");
209 if (&valid_signature("$Conf::tmpdir/$bfile")) {
210 logger("DOWNLOAD INFO: Signature of $bfile is fine.");
211 move("$Conf::tmpdir/$bfile","$Conf::cachedir/$bfile");
35f38a8b 212 } else {
06d55142
MT
213 message("DOWNLOAD ERROR: The downloaded file ($file) wasn't verified by IPFire.org. Sorry - Exiting...");
214 my $ntp = `ntpdate -q -t 10 pool.ntp.org 2>/dev/null | tail -1`;
215 if ( $ntp !~ /time\ server(.*)offset(.*)/ ){message("TIME ERROR: Unable to get the nettime, this may lead to the verification error.");}
216 else { $ntp =~ /time\ server(.*)offset(.*)/; message("TIME INFO: Time Server$1has$2 offset to localtime.");}
217 exit 1;
186e3d2c 218 }
06d55142
MT
219 logger("DOWNLOAD FINISHED: $file");
220 $allok = 1;
35f38a8b 221 return 0;
06d55142
MT
222 } else {
223 logger("DOWNLOAD ERROR: Could not open $Conf::tmpdir/$bfile for writing.");
4d504812 224 }
06d55142 225 } else {
a6d327a7 226 logger("DOWNLOAD ERROR: $log");
4d504812 227 }
1bd42c89 228 }
a6d327a7 229 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 230 return 1;
1bd42c89
MT
231}
232
233sub getmirrors {
377560fb
MT
234 my $force = shift;
235 my $age;
236
1bd42c89 237 use File::Copy;
1bd42c89 238
2aa6d448
MT
239 if ( -e "$Conf::dbdir/lists/server-list.db" ) {
240 my @stat = stat("$Conf::dbdir/lists/server-list.db");
e3670217
MT
241 my $time = time();
242 $age = $time - $stat[9];
377560fb
MT
243 $force = "force" if ("$age" >= "3600");
244 logger("MIRROR INFO: server-list.db is $age seconds old. - DEBUG: $force");
e3670217
MT
245 } else {
246 # Force an update.
377560fb 247 $force = "force";
e3670217
MT
248 }
249
377560fb 250 if ("$force" eq "force") {
e3670217
MT
251 fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver");
252 move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db");
253 }
1bd42c89
MT
254}
255
2aa6d448 256sub getcoredb {
377560fb
MT
257 my $force = shift;
258 my $age;
259
2aa6d448 260 use File::Copy;
2aa6d448
MT
261
262 if ( -e "$Conf::dbdir/lists/core-list.db" ) {
263 my @stat = stat("$Conf::dbdir/lists/core-list.db");
264 my $time = time();
265 $age = $time - $stat[9];
377560fb
MT
266 $force = "force" if ("$age" >= "3600");
267 logger("CORE INFO: core-list.db is $age seconds old. - DEBUG: $force");
2aa6d448
MT
268 } else {
269 # Force an update.
377560fb 270 $force = "force";
2aa6d448
MT
271 }
272
377560fb 273 if ("$force" eq "force") {
2aa6d448
MT
274 fetchfile("lists/core-list.db", "");
275 move("$Conf::cachedir/core-list.db", "$Conf::dbdir/lists/core-list.db");
276 }
277}
278
3e29608f
MT
279sub valid_signature($) {
280 my $filename = shift;
281
282 open(my $cmd, "gpg --verify --status-fd 1 \"$filename\" 2>/dev/null |");
283 while (<$cmd>) {
284 # Process valid signature lines
285 if (/VALIDSIG ([A-Z0-9]+)/) {
286 # Check if we know the key
287 foreach my $key (@VALID_KEY_FINGERPRINTS) {
288 # Signature is valid
289 return 1 if ($key eq $1);
290 }
291 }
292 }
293 close($cmd);
294
295 # Signature is invalid
296 return 0;
297}
2aa6d448 298
1bd42c89
MT
299sub selectmirror {
300 ### Check if there is a current server list and read it.
301 # If there is no list try to get one.
302 my $count = 0;
3d3b68c5 303 while (!(open(FILE, "<$Conf::dbdir/lists/server-list.db")) && ($count lt 5)) {
1bd42c89 304 $count++;
377560fb 305 getmirrors("noforce");
1bd42c89 306 }
3d3b68c5 307 if ($count == 5) {
a6d327a7 308 message("MIRROR ERROR: Could not find or download a server list");
1bd42c89
MT
309 exit 1;
310 }
311 my @lines = <FILE>;
312 close(FILE);
313
314 ### Count the number of the servers in the list
315 my $scount = 0;
e44b26cf 316 my @newlines;
1bd42c89 317 foreach (@lines) {
e44b26cf
MT
318 if ("$_" =~ /.*;.*;.*;/ ) {
319 push(@newlines,$_);
320 $scount++;
321 }
1bd42c89 322 }
a6d327a7 323 logger("MIRROR INFO: $scount servers found in list");
63efc01c
MT
324
325 if ($scount eq 0) {
326 logger("MIRROR INFO: Could not find any servers. Falling back to main server $Conf::mainserver");
c846ed16 327 return ("HTTPS", $Conf::mainserver, "/$Conf::version");
63efc01c
MT
328 }
329
1bd42c89
MT
330 ### Choose a random server and test if it is online
331 # If the check fails try a new server.
332 # This will never give up.
1bd42c89 333 my $servers = 0;
e32591e7 334 while (1) {
1bd42c89
MT
335 $server = int(rand($scount) + 1);
336 $servers = 0;
337 my ($line, $proto, $path, $host);
338 my @templine;
e44b26cf 339 foreach $line (@newlines) {
1bd42c89
MT
340 $servers++;
341 if ($servers eq $server) {
342 @templine = split(/\;/, $line);
343 $proto = $templine[0];
344 $host = $templine[1];
345 $path = $templine[2];
e32591e7
MT
346
347 return ($proto, $host, $path);
1bd42c89
MT
348 }
349 }
4d504812 350 }
1bd42c89
MT
351}
352
353sub dbgetlist {
354 ### Update the database if the file is older than one day.
355 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
356 # Usage is always with an argument.
357 my $force = shift;
358 my $age;
359
360 use File::Copy;
361
362 if ( -e "$Conf::dbdir/lists/packages_list.db" ) {
363 my @stat = stat("$Conf::dbdir/lists/packages_list.db");
364 my $time = time();
365 $age = $time - $stat[9];
377560fb
MT
366 $force = "force" if ("$age" >= "3600");
367 logger("DB INFO: packages_list.db is $age seconds old. - DEBUG: $force");
1bd42c89
MT
368 } else {
369 # Force an update.
377560fb 370 $force = "force";
1bd42c89
MT
371 }
372
377560fb 373 if ("$force" eq "force") {
1bd42c89
MT
374 fetchfile("lists/packages_list.db", "");
375 move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db");
376 }
ff9545f8
AF
377
378 # Update the meta database if new packages was in the package list
379 my @meta;
380 my $file;
381 my $line;
382 my $prog;
383 my ($name, $version, $release);
384 my @templine;
385
386 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
387 my @db = <FILE>;
388 close(FILE);
389
390 opendir(DIR,"$Conf::dbdir/meta");
391 my @files = readdir(DIR);
392 closedir(DIR);
393 foreach $file (@files) {
394 next if ( $file eq "." );
395 next if ( $file eq ".." );
1af34aa8 396 next if ( $file eq "meta-" );
ff9545f8
AF
397 next if ( $file =~ /^old/ );
398 open(FILE, "<$Conf::dbdir/meta/$file");
399 @meta = <FILE>;
400 close(FILE);
401 foreach $line (@meta) {
402 @templine = split(/\: /,$line);
403 if ("$templine[0]" eq "Name") {
404 $name = $templine[1];
405 chomp($name);
406 } elsif ("$templine[0]" eq "ProgVersion") {
407 $version = $templine[1];
408 chomp($version);
409 } elsif ("$templine[0]" eq "Release") {
410 $release = $templine[1];
411 chomp($release);
412 }
413 }
414 foreach $prog (@db) {
415 @templine = split(/\;/,$prog);
416 if (("$name" eq "$templine[0]") && ("$release" ne "$templine[2]")) {
417 move("$Conf::dbdir/meta/meta-$name","$Conf::dbdir/meta/old_meta-$name");
418 fetchfile("meta/meta-$name", "");
419 move("$Conf::cachedir/meta-$name", "$Conf::dbdir/meta/meta-$name");
420 }
421 }
422 }
1bd42c89
MT
423}
424
425sub dblist {
426 ### This subroutine lists the packages.
427 # You may also pass a filter: &Pakfire::dblist(filter)
428 # Usage is always with two arguments.
429 # filter may be: all, notinstalled, installed
430 my $filter = shift;
431 my $forweb = shift;
4b122800 432 my @meta;
99e6df8e 433 my @updatepaks;
4b122800
MT
434 my $file;
435 my $line;
436 my $prog;
437 my ($name, $version, $release);
438 my @templine;
1bd42c89
MT
439
440 ### Make sure that the list is not outdated.
377560fb 441 #dbgetlist("noforce");
1bd42c89
MT
442
443 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
444 my @db = <FILE>;
445 close(FILE);
4b122800
MT
446
447 if ("$filter" eq "upgrade") {
4638fd92 448 if ("$forweb" ne "forweb" && "$forweb" ne "notice" ) {getcoredb("noforce");}
8a8a50ad 449 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 450 if ("$core_release" > "$Conf::core_mine") {
8a8a50ad
MT
451 if ("$forweb" eq "forweb") {
452 print "<option value=\"core\">Core-Update -- $Conf::version -- Release: $Conf::core_mine -> $core_release</option>\n";
2b38ab5c
JPT
453 }
454 elsif ("$forweb" eq "notice") {
462a5e86 455 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
456 } else {
457 my $command = "Core-Update $Conf::version\nRelease: $Conf::core_mine -> $core_release\n";
458 if ("$Pakfire::enable_colors" eq "1") {
459 print "$color{'lila'}$command$color{'normal'}\n";
460 } else {
461 print "$command\n";
462 }
463 }
464 }
465
5e932bd5 466 opendir(DIR,"$Conf::dbdir/installed");
4b122800
MT
467 my @files = readdir(DIR);
468 closedir(DIR);
469 foreach $file (@files) {
470 next if ( $file eq "." );
471 next if ( $file eq ".." );
3f01107b 472 next if ( $file =~ /^old/ );
5e932bd5 473 open(FILE, "<$Conf::dbdir/installed/$file");
4b122800
MT
474 @meta = <FILE>;
475 close(FILE);
476 foreach $line (@meta) {
477 @templine = split(/\: /,$line);
478 if ("$templine[0]" eq "Name") {
479 $name = $templine[1];
480 chomp($name);
481 } elsif ("$templine[0]" eq "ProgVersion") {
482 $version = $templine[1];
483 chomp($version);
484 } elsif ("$templine[0]" eq "Release") {
485 $release = $templine[1];
486 chomp($release);
487 }
488 }
489 foreach $prog (@db) {
490 @templine = split(/\;/,$prog);
462a5e86 491 if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" && "$forweb" ne "notice")) {
99e6df8e 492 push(@updatepaks,$name);
4b122800
MT
493 if ("$forweb" eq "forweb") {
494 print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n";
495 } else {
e3670217
MT
496 my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n";
497 if ("$Pakfire::enable_colors" eq "1") {
498 print "$color{'lila'}$command$color{'normal'}\n";
499 } else {
500 print "$command\n";
501 }
4b122800
MT
502 }
503 }
504 }
5b2a12ff 505 }
99e6df8e 506 return @updatepaks;
4b122800
MT
507 } else {
508 my $line;
e3670217 509 my $use_color;
4b122800 510 my @templine;
e3670217 511 my $count;
4b122800 512 foreach $line (sort @db) {
06209efc 513 next unless ($line =~ /.*;.*;.*;/ );
e3670217
MT
514 $use_color = "";
515 $count++;
4b122800
MT
516 @templine = split(/\;/,$line);
517 if ("$filter" eq "notinstalled") {
518 next if ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
519 } elsif ("$filter" eq "installed") {
520 next unless ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
521 }
4e17785f
AF
522 if ("$forweb" eq "forweb")
523 {
524 if ("$filter" eq "notinstalled") {
525 print "<option value=\"$templine[0]\">$templine[0]-$templine[1]-$templine[2]</option>\n";
526 } else {
527 print "<option value=\"$templine[0]\">$templine[0]</option>\n";
528 }
4b122800 529 } else {
e3670217
MT
530 if ("$Pakfire::enable_colors" eq "1") {
531 if (&isinstalled("$templine[0]")) {
532 $use_color = "$color{'red'}"
533 } else {
534 $use_color = "$color{'green'}"
535 }
536 }
537 print "${use_color}Name: $templine[0]\nProgVersion: $templine[1]\nRelease: $templine[2]$color{'normal'}\n\n";
4b122800 538 }
1bd42c89 539 }
e3670217 540 print "$count packages total.\n" unless ("$forweb" eq "forweb");
1bd42c89
MT
541 }
542}
543
621dcd86 544sub resolvedeps_one {
1bd42c89
MT
545 my $pak = shift;
546
547 getmetafile("$pak");
548
35f38a8b 549 message("PAKFIRE RESV: $pak: Resolving dependencies...");
1bd42c89
MT
550
551 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
552 my @file = <FILE>;
553 close(FILE);
554
555 my $line;
621dcd86 556 my (@templine, @deps, @all);
1bd42c89
MT
557 foreach $line (@file) {
558 @templine = split(/\: /,$line);
559 if ("$templine[0]" eq "Dependencies") {
560 @deps = split(/ /, $templine[1]);
561 }
562 }
563 chomp (@deps);
564 foreach (@deps) {
565 if ($_) {
186e3d2c
MT
566 my $return = &isinstalled($_);
567 if ($return eq 0) {
35f38a8b 568 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
186e3d2c 569 } else {
35f38a8b 570 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
186e3d2c
MT
571 push(@all,$_);
572 }
1bd42c89
MT
573 }
574 }
e44b26cf 575
621dcd86
MT
576 return @all;
577}
578
579sub resolvedeps {
580 my $pak = shift;
581 my @all;
582
583 # Resolve all not yet installed dependencies of $pak
584 my @deps = &resolvedeps_one($pak);
585 push(@all, @deps);
586
587 # For each dependency, we check if more dependencies exist
588 while (@deps) {
589 my $dep = pop(@deps);
590
591 my @subdeps = &resolvedeps_one($dep);
592 foreach my $subdep (@subdeps) {
593 # Skip the package we are currently resolving for
594 next if ($pak eq $subdep);
595
596 # If the package is not already to be installed,
597 # we add it to the list (@all) and check if it has
598 # more dependencies on its own.
599 unless (grep {$_ eq $subdep} @all) {
600 push(@deps, $subdep);
601 push(@all, $subdep);
1bd42c89
MT
602 }
603 }
604 }
621dcd86 605
186e3d2c 606 return @all;
1bd42c89
MT
607}
608
9f1f68f1 609sub resolvedeps_recursive {
031becc0 610 my @packages = @_;
9f1f68f1
MT
611 my @result = ();
612
613 foreach my $pkg (@packages) {
614 my @deps = &Pakfire::resolvedeps($pkg);
615
616 foreach my $dep (@deps) {
617 push(@result, $dep);
618 }
619 }
620
621 # Sort the result array and remove dupes
622 my %sort = map{ $_, 1 } @result;
623 @result = keys %sort;
624
625 return @result;
626}
627
1bd42c89
MT
628sub cleanup {
629 my $dir = shift;
630 my $path;
631
35f38a8b
MT
632 logger("CLEANUP: $dir");
633
1bd42c89
MT
634 if ( "$dir" eq "meta" ) {
635 $path = "$Conf::dbdir/meta";
636 } elsif ( "$dir" eq "tmp" ) {
637 $path = "$Conf::tmpdir";
638 }
639 chdir("$path");
640 opendir(DIR,".");
641 my @files = readdir(DIR);
642 closedir(DIR);
643 foreach (@files) {
644 unless (($_ eq ".") || ($_ eq "..")) {
645 system("rm -rf $_");
646 }
647 }
648}
649
650sub getmetafile {
651 my $pak = shift;
652
28593a79
CS
653 unless ( -e "$Conf::dbdir/meta/meta-$pak" ) {
654 fetchfile("meta/meta-$pak", "");
655 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
656 }
657
658 if ( -z "$Conf::dbdir/meta/meta-$pak" ) {
1bd42c89
MT
659 fetchfile("meta/meta-$pak", "");
660 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
661 }
662
663 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
664 my @line = <FILE>;
665 close(FILE);
666
667 open(FILE, ">$Conf::dbdir/meta/meta-$pak");
668 foreach (@line) {
669 my $string = $_;
670 $string =~ s/\r\n/\n/g;
671 print FILE $string;
672 }
673 close(FILE);
674 return 1;
675}
676
677sub getsize {
678 my $pak = shift;
679
680 getmetafile("$pak");
681
682 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
683 my @file = <FILE>;
684 close(FILE);
685
686 my $line;
687 my @templine;
688 foreach $line (@file) {
689 @templine = split(/\: /,$line);
690 if ("$templine[0]" eq "Size") {
691 chomp($templine[1]);
692 return $templine[1];
693 }
694 }
4b122800 695 return 0;
1bd42c89
MT
696}
697
698sub decryptpak {
699 my $pak = shift;
700
701 cleanup("tmp");
702
703 my $file = getpak("$pak", "noforce");
704
a6d327a7 705 logger("DECRYPT STARTED: $pak");
35f38a8b 706 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 707 $return %= 255;
a6d327a7 708 logger("DECRYPT FINISHED: $pak - Status: $return");
cde0e116 709 if ($return != 0) { exit 1; }
1bd42c89
MT
710}
711
712sub getpak {
713 my $pak = shift;
714 my $force = shift;
715
716 getmetafile("$pak");
717
718 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
719 my @file = <FILE>;
720 close(FILE);
721
722 my $line;
723 my $file;
724 my @templine;
725 foreach $line (@file) {
726 @templine = split(/\: /,$line);
727 if ("$templine[0]" eq "File") {
728 chomp($templine[1]);
729 $file = $templine[1];
730 }
731 }
732
733 unless ($file) {
1af34aa8 734 message("No filename given in meta-file.");
1bd42c89
MT
735 exit 1;
736 }
737
1bd42c89
MT
738 unless ( "$force" eq "force" ) {
739 if ( -e "$Conf::cachedir/$file" ) {
1bd42c89
MT
740 return $file;
741 }
742 }
743
744 fetchfile("paks/$file", "");
745 return $file;
746}
747
748sub setuppak {
749 my $pak = shift;
750
35f38a8b 751 message("PAKFIRE INST: $pak: Decrypting...");
1bd42c89
MT
752 decryptpak("$pak");
753
35f38a8b 754 message("PAKFIRE INST: $pak: Copying files and running post-installation scripts...");
99e6df8e 755 my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1");
cde0e116 756 $return %= 255;
1bd42c89
MT
757 if ($return == 0) {
758 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
759 cleanup("tmp");
4d504812 760 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
35f38a8b
MT
761 message("PAKFIRE INST: $pak: Finished.");
762 message("");
1bd42c89 763 } else {
35f38a8b 764 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
765 exit $return;
766 }
a08c3a2e 767 return $return;
1bd42c89
MT
768}
769
2aa6d448 770sub upgradecore {
377560fb 771 getcoredb("noforce");
2aa6d448 772 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 773 if ("$core_release" > "$Conf::core_mine") {
712425ff
AF
774 # Safety check for lazy testers:
775 # Before we upgrade to the latest release, we re-install the previous release
776 # to make sure that the tester has always been on the latest version.
777 my $tree = &get_tree();
778 $Conf::core_mine-- if ($tree eq "testing" || $tree eq "unstable");
779
2aa6d448
MT
780 message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release");
781
782 my @seq = `seq $Conf::core_mine $core_release`;
783 shift @seq;
784 my $release;
785 foreach $release (@seq) {
786 chomp($release);
787 getpak("core-upgrade-$release");
788 }
789
790 foreach $release (@seq) {
791 chomp($release);
792 upgradepak("core-upgrade-$release");
793 }
794
795 system("echo $core_release > $Conf::coredir/mine");
796
797 } else {
798 message("CORE ERROR: No new upgrades available. You are on release $Conf::core_mine.");
799 }
800}
801
186e3d2c
MT
802sub isinstalled {
803 my $pak = shift;
804 if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) {
805 close(FILE);
806 return 0;
807 } else {
808 return 1;
809 }
810}
811
99e6df8e 812sub upgradepak {
1bd42c89
MT
813 my $pak = shift;
814
35f38a8b 815 message("PAKFIRE UPGR: $pak: Decrypting...");
1bd42c89
MT
816 decryptpak("$pak");
817
35f38a8b 818 message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts...");
99e6df8e
MT
819 my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1");
820 $return %= 255;
1bd42c89
MT
821 if ($return == 0) {
822 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
823 cleanup("tmp");
4e4b54c5 824 copy("$Conf::dbdir/meta/meta-$pak", "$Conf::dbdir/installed/");
35f38a8b
MT
825 message("PAKFIRE UPGR: $pak: Finished.");
826 message("");
1bd42c89 827 } else {
35f38a8b 828 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
829 exit $return;
830 }
8e58bd37 831 return $return;
1bd42c89
MT
832}
833
834sub removepak {
835 my $pak = shift;
836
35f38a8b 837 message("PAKFIRE REMV: $pak: Decrypting...");
1bd42c89
MT
838 decryptpak("$pak");
839
35f38a8b 840 message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts...");
99e6df8e
MT
841 my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1");
842 $return %= 255;
1bd42c89 843 if ($return == 0) {
1bd42c89 844 unlink("$Conf::dbdir/rootfiles/$pak");
a6d327a7 845 unlink("$Conf::dbdir/installed/meta-$pak");
1bd42c89 846 cleanup("tmp");
35f38a8b
MT
847 message("PAKFIRE REMV: $pak: Finished.");
848 message("");
1bd42c89 849 } else {
35f38a8b 850 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
851 exit $return;
852 }
8e58bd37 853 return $return;
1bd42c89
MT
854}
855
856sub beautifysize {
857 my $size = shift;
35f38a8b 858 #$size = $size / 1024;
a08c3a2e 859 my $unit;
1bd42c89 860
35f38a8b
MT
861 if ($size > 1023*1024) {
862 $size = ($size / (1024*1024));
a08c3a2e 863 $unit = "MB";
35f38a8b
MT
864 } elsif ($size > 1023) {
865 $size = ($size / 1024);
a08c3a2e 866 $unit = "KB";
35f38a8b
MT
867 } else {
868 $unit = "B";
1bd42c89 869 }
a08c3a2e
MT
870 $size = sprintf("%.2f" , $size);
871 my $string = "$size $unit";
872 return $string;
1bd42c89
MT
873}
874
8e58bd37
MT
875sub makeuuid {
876 unless ( -e "$Conf::dbdir/uuid" ) {
8e58bd37
MT
877 open(FILE, "</proc/sys/kernel/random/uuid");
878 my @line = <FILE>;
879 close(FILE);
880
881 open(FILE, ">$Conf::dbdir/uuid");
882 foreach (@line) {
883 print FILE $_;
884 }
885 close(FILE);
886 }
887}
888
a6d327a7
MT
889sub callback {
890 my ($data, $response, $protocol) = @_;
891 $final_data .= $data;
35f38a8b 892 print progress_bar( length($final_data), $total_size, 30, '=' );
a6d327a7
MT
893}
894
895sub progress_bar {
896 my ( $got, $total, $width, $char ) = @_;
897 my $show_bfile;
35f38a8b 898 $width ||= 30; $char ||= '=';
a6d327a7 899 my $len_bfile = length $bfile;
35f38a8b
MT
900 if ("$len_bfile" >= "17") {
901 $show_bfile = substr($bfile,0,17)."...";
a6d327a7
MT
902 } else {
903 $show_bfile = $bfile;
35f38a8b
MT
904 }
905 $progress = sprintf("%.2f%%", 100*$got/+$total);
906 sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got);
a6d327a7
MT
907}
908
090af02e
AK
909sub updates_available {
910 # Get packets with updates available
911 my @upgradepaks = &Pakfire::dblist("upgrade", "noweb");
912
913 # Get the length of the returned array
914 my $updatecount = scalar @upgradepaks;
915
916 return "$updatecount";
917}
918
919sub coreupdate_available {
920 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
921 if ("$core_release" > "$Conf::core_mine") {
922 return "yes ($core_release)";
923 }
924 else {
925 return "no";
926 }
927}
928
929sub reboot_required {
930 if ( -e "/var/run/need_reboot" ) {
931 return "yes";
932 }
933 else {
934 return "no";
935 }
936}
937
938sub status {
939 # General info
8c072c5c
AK
940 my $return = "Core-Version: $Conf::version\n";
941 $return .= "Core-Update-Level: $Conf::core_mine\n";
090af02e
AK
942 $return .= "Last update: " . &General::age("/opt/pakfire/db/core/mine") . " ago\n";
943 $return .= "Last core-list update: " . &General::age("/opt/pakfire/db/lists/core-list.db") . " ago\n";
944 $return .= "Last server-list update: " . &General::age("/opt/pakfire/db/lists/server-list.db") . " ago\n";
945 $return .= "Last packages-list update: " . &General::age("/opt/pakfire/db/lists/packages_list.db") . " ago\n";
946
947 # Get availability of core updates
948 $return .= "Core-Update available: " . &Pakfire::coreupdate_available() . "\n";
949
950 # Get availability of package updates
951 $return .= "Package-Updates available: " . &Pakfire::updates_available() . "\n";
952
953 # Test if reboot is required
954 $return .= "Reboot required: " . &Pakfire::reboot_required() . "\n";
955
956 # Return status text
957 print "$return";
958 exit 1;
959}
960
e6f4991b
MT
961sub get_arch() {
962 # Append architecture
963 my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
964
965 # We only support armv5tel for all 32 bit arches
966 if ($machine =~ m/armv[567]/) {
967 return "armv5tel";
968
969 # We only support i586 for 32 bit x86
970 } elsif ($machine =~ m/i[0-9]86/) {
971 return "i586";
972 }
973
974 return $machine;
975}
976
152378c6
MT
977sub get_tree() {
978 # Return stable if nothing is set
979 return "stable" unless (defined $pakfiresettings{'TREE'});
980
981 return $pakfiresettings{'TREE'};
982}
983
e6f4991b
MT
984sub make_version() {
985 my $version = "";
986
987 # Open /etc/system-release
988 open(RELEASE, "</etc/system-release");
989 my $release = <RELEASE>;
990 close(RELEASE);
991
992 # Add the main relase
993 if ($release =~ m/IPFire ([\d\.]+)/) {
994 $version .= $1;
995 }
996
152378c6
MT
997 # Append suffix for tree
998 my $tree = &get_tree();
999 if ($tree eq "testing") {
1000 $version .= ".1";
1001 } elsif ($tree eq "unstable") {
1002 $version .= ".2";
1003 }
1004
e6f4991b
MT
1005 # Append architecture
1006 my $arch = &get_arch();
1007 if ($arch ne "i586") {
1008 $version .= "-${arch}";
1009 }
1010
1011 return $version;
1012}
1013
1bd42c89 10141;