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