]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/pakfire/lib/functions.pl
Kernel 2.6.22.1 - leider ohne OpenSwan.
[people/pmueller/ipfire-2.x.git] / src / pakfire / lib / functions.pl
1 #!/usr/bin/perl -w
2
3 require "/opt/pakfire/etc/pakfire.conf";
4 require "/var/ipfire/general-functions.pl";
5
6 use File::Basename;
7 use File::Copy;
8 use LWP::UserAgent;
9 use HTTP::Response;
10 use HTTP::Headers;
11 use HTTP::Message;
12 use HTTP::Request;
13 use Net::Ping;
14
15 package Pakfire;
16
17 # GPG Keys
18 my $myid = "64D96617"; # Our own gpg-key paks@ipfire.org
19 my $trustid = "65D0FD58"; # gpg-key of CaCert
20
21 # A small color-hash :D
22 my %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";
40 our $enable_colors = 1;
41
42 my $final_data;
43 my $total_size;
44 my $bfile;
45
46 my %pakfiresettings = ();
47 &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
48
49 sub message {
50 my $message = shift;
51
52 logger("$message");
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 }
69 }
70 print "$message\n";
71
72 }
73
74 sub logger {
75 my $log = shift;
76 if ($log) {
77 system("echo \"`date`: $log\" >> /var/log/pakfire.log");
78 #system("logger -t pakfire \"$log\"");
79 }
80 }
81
82 sub usage {
83 &Pakfire::message("Usage: pakfire <install|remove> [options] <pak(s)>");
84 &Pakfire::message(" <update> - Contacts the servers for new lists of paks.");
85 &Pakfire::message(" <upgrade> - Installs the latest version of all paks.");
86 &Pakfire::message(" <list> - Outputs a short list with all available paks.");
87 &Pakfire::message("");
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("");
94 exit 1;
95 }
96
97 sub pinghost {
98 my $host = shift;
99
100 $p = Net::Ping->new();
101 if ($p->ping($host)) {
102 logger("PING INFO: $host is alive");
103 return 1;
104 } else {
105 logger("PING INFO: $host is unreachable");
106 return 0;
107 }
108 $p->close();
109 }
110
111 sub fetchfile {
112 my $getfile = shift;
113 my $gethost = shift;
114 my (@server, $host, $proto, $file, $allok, $i);
115
116 logger("DOWNLOAD STARTED: $getfile") unless ($bfile =~ /^counter\?.*/);
117 use File::Basename;
118 $bfile = basename("$getfile");
119
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";
129 } else {
130 $host = $gethost;
131 $file = $getfile;
132 }
133
134 $proto = "HTTP" unless $proto;
135
136 unless ($bfile =~ /^counter\?.*/) {
137 logger("DOWNLOAD INFO: Host: $host ($proto) - File: $file");
138 }
139
140 my $ua = LWP::UserAgent->new;
141 $ua->agent("Pakfire/$Conf::version");
142 $ua->timeout(5);
143
144 my %proxysettings=();
145 &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings);
146
147 if ($proxysettings{'UPSTREAM_PROXY'}) {
148 logger("DOWNLOAD INFO: Upstream proxy: \"$proxysettings{'UPSTREAM_PROXY'}\"") unless ($bfile =~ /^counter\?.*/);
149 if ($proxysettings{'UPSTREAM_USER'}) {
150 $ua->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$proxysettings{'UPSTREAM_PROXY'}/");
151 logger("DOWNLOAD INFO: Logging in with: \"$proxysettings{'UPSTREAM_USER'}\" - \"$proxysettings{'UPSTREAM_PASSWORD'}\"") unless ($bfile =~ /^counter\?.*/);
152 } else {
153 $ua->proxy("http","http://$proxysettings{'UPSTREAM_PROXY'}/");
154 }
155 }
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 }
172
173 my $code = $response->code();
174 my $log = $response->status_line;
175 logger("DOWNLOAD INFO: HTTP-Status-Code: $code - $log");
176
177 if ( $code eq "500" ) {
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.");
179 return 1;
180 }
181
182 if ($response->is_success) {
183 unless ($bfile =~ /^counter\?.*/) {
184 if (open(FILE, ">$Conf::tmpdir/$bfile")) {
185 print FILE $final_data;
186 close(FILE);
187 logger("DOWNLOAD INFO: File received. Start checking signature...");
188 if (system("gpg --verify \"$Conf::tmpdir/$bfile\" &>/dev/null") eq 0) {
189 logger("DOWNLOAD INFO: Signature of $bfile is fine.");
190 move("$Conf::tmpdir/$bfile","$Conf::cachedir/$bfile");
191 } else {
192 message("DOWNLOAD ERROR: The downloaded file ($file) wasn't verified by IPFire.org. Sorry - Exiting...");
193 exit 1;
194 }
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.");
200 }
201 } else {
202 return 0;
203 }
204 } else {
205 logger("DOWNLOAD ERROR: $log");
206 }
207 }
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.");
209 return 1;
210 }
211
212 sub getmirrors {
213 use File::Copy;
214
215 logger("MIRROR: Trying to get a mirror list.");
216
217 fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver");
218 move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db");
219 }
220
221 sub 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;
225 while (!(open(FILE, "<$Conf::dbdir/lists/server-list.db")) && ($count lt 5)) {
226 $count++;
227 getmirrors();
228 }
229 if ($count == 5) {
230 message("MIRROR ERROR: Could not find or download a server list");
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;
238 my @newlines;
239 foreach (@lines) {
240 if ("$_" =~ /.*;.*;.*;/ ) {
241 push(@newlines,$_);
242 $scount++;
243 }
244 }
245 logger("MIRROR INFO: $scount servers found in list");
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;
257 foreach $line (@newlines) {
258 $servers++;
259 if ($servers eq $server) {
260 @templine = split(/\;/, $line);
261 $proto = $templine[0];
262 $host = $templine[1];
263 $path = $templine[2];
264 if (pinghost("$host")) {
265 $found = 1;
266 return ($proto, $host, $path);
267 }
268 }
269 }
270 }
271 }
272
273 sub 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
291 if (("$age" gt "86400") || ("$force" eq "force")) {
292 fetchfile("lists/packages_list.db", "");
293 move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db");
294 }
295 }
296
297 sub 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;
304 my @meta;
305 my @updatepaks;
306 my $file;
307 my $line;
308 my $prog;
309 my ($name, $version, $release);
310 my @templine;
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);
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]" )) {
345 push(@updatepaks,$name);
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 }
353 }
354 return @updatepaks;
355 } else {
356 my $line;
357 my @templine;
358 foreach $line (sort @db) {
359 next unless ($line =~ /.*;.*;.*;/ );
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 }
371 }
372 }
373 }
374
375 sub resolvedeps {
376 my $pak = shift;
377
378 getmetafile("$pak");
379
380 message("PAKFIRE RESV: $pak: Resolving dependencies...");
381
382 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
383 my @file = <FILE>;
384 close(FILE);
385
386 my $line;
387 my (@templine, @deps, @tempdeps, @all);
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 ($_) {
397 my $return = &isinstalled($_);
398 if ($return eq 0) {
399 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
400 } else {
401 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
402 push(@tempdeps,$_);
403 push(@all,$_);
404 }
405 }
406 }
407
408 foreach (@tempdeps) {
409 if ($_) {
410 my @newdeps = resolvedeps("$_");
411 foreach(@newdeps) {
412 unless (($_ eq " ") || ($_ eq "")) {
413 my $return = &isinstalled($_);
414 if ($return eq 0) {
415 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
416 } else {
417 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
418 push(@all,$_);
419 }
420 }
421 }
422 }
423 }
424 message("");
425 chomp (@all);
426 return @all;
427 }
428
429 sub cleanup {
430 my $dir = shift;
431 my $path;
432
433 logger("CLEANUP: $dir");
434
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
451 sub getmetafile {
452 my $pak = shift;
453
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
473 sub 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 }
491 return 0;
492 }
493
494 sub decryptpak {
495 my $pak = shift;
496
497 cleanup("tmp");
498
499 my $file = getpak("$pak", "noforce");
500
501 logger("DECRYPT STARTED: $pak");
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");
503 $return %= 255;
504 logger("DECRYPT FINISHED: $pak - Status: $return");
505 if ($return != 0) { exit 1; }
506 }
507
508 sub 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
534 unless ( "$force" eq "force" ) {
535 if ( -e "$Conf::cachedir/$file" ) {
536 return $file;
537 }
538 }
539
540 fetchfile("paks/$file", "");
541 return $file;
542 }
543
544 sub setuppak {
545 my $pak = shift;
546
547 message("PAKFIRE INST: $pak: Decrypting...");
548 decryptpak("$pak");
549
550 message("PAKFIRE INST: $pak: Copying files and running post-installation scripts...");
551 my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1");
552 $return %= 255;
553 if ($pakfiresettings{'UUID'} ne "off") {
554 fetchfile("cgi-bin/counter?ver=$Conf::version&uuid=$Conf::uuid&ipak=$pak&return=$return", "$Conf::mainserver");
555 }
556 if ($return == 0) {
557 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
558 cleanup("tmp");
559 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
560 message("PAKFIRE INST: $pak: Finished.");
561 message("");
562 } else {
563 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
564 exit $return;
565 }
566 return $return;
567 }
568
569 sub 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
579 sub upgradepak {
580 my $pak = shift;
581
582 message("PAKFIRE UPGR: $pak: Decrypting...");
583 decryptpak("$pak");
584
585 message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts...");
586 my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1");
587 $return %= 255;
588 if ($pakfiresettings{'UUID'} ne "off") {
589 fetchfile("cgi-bin/counter?ver=$Conf::version&uuid=$Conf::uuid&upak=$pak&return=$return", "$Conf::mainserver");
590 }
591 if ($return == 0) {
592 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
593 cleanup("tmp");
594 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
595 message("PAKFIRE UPGR: $pak: Finished.");
596 message("");
597 } else {
598 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
599 exit $return;
600 }
601 return $return;
602 }
603
604 sub removepak {
605 my $pak = shift;
606
607 message("PAKFIRE REMV: $pak: Decrypting...");
608 decryptpak("$pak");
609
610 message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts...");
611 my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1");
612 $return %= 255;
613 if ($pakfiresettings{'UUID'} ne "off") {
614 fetchfile("cgi-bin/counter?ver=$Conf::version&uuid=$Conf::uuid&dpak=$pak&return=$return", "$Conf::mainserver");
615 }
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");
627 unlink("$Conf::dbdir/installed/meta-$pak");
628 cleanup("tmp");
629 message("PAKFIRE REMV: $pak: Finished.");
630 message("");
631 } else {
632 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
633 exit $return;
634 }
635 return $return;
636 }
637
638 sub beautifysize {
639 my $size = shift;
640 #$size = $size / 1024;
641 my $unit;
642
643 if ($size > 1023*1024) {
644 $size = ($size / (1024*1024));
645 $unit = "MB";
646 } elsif ($size > 1023) {
647 $size = ($size / 1024);
648 $unit = "KB";
649 } else {
650 $unit = "B";
651 }
652 $size = sprintf("%.2f" , $size);
653 my $string = "$size $unit";
654 return $string;
655 }
656
657 sub makeuuid {
658 unless ( -e "$Conf::dbdir/uuid" ) {
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
671 sub senduuid {
672 if ($pakfiresettings{'UUID'} ne "off") {
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");
678 system("rm -f $Conf::tmpdir/counter* 2>/dev/null");
679 }
680 }
681
682 sub checkcryptodb {
683 logger("CRYPTO INFO: Checking GnuPG Database");
684 my $ret = system("gpg --list-keys | grep -q $myid");
685 unless ( "$ret" eq "0" ) {
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.");
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");
690 } else {
691 logger("CRYPTO INFO: Database is okay");
692 }
693 }
694
695 sub callback {
696 my ($data, $response, $protocol) = @_;
697 $final_data .= $data;
698 print progress_bar( length($final_data), $total_size, 30, '=' );
699 }
700
701 sub progress_bar {
702 my ( $got, $total, $width, $char ) = @_;
703 my $show_bfile;
704 $width ||= 30; $char ||= '=';
705 my $len_bfile = length $bfile;
706 if ("$len_bfile" >= "17") {
707 $show_bfile = substr($bfile,0,17)."...";
708 } else {
709 $show_bfile = $bfile;
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);
713 }
714
715 1;