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