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