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