urpm/main_loop.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« urpm/lock.pm ↑ Summary urpm/media.pm »
1 package urpm::main_loop;
2  
3  
4 #- Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA
5 #- Copyright (C) 2005-2010 Mandriva SA
6 #- Copyright (C) 2011-2020 Mageia
7 #-
8 #- This program is free software; you can redistribute it and/or modify
9 #- it under the terms of the GNU General Public License as published by
10 #- the Free Software Foundation; either version 2, or (at your option)
11 #- any later version.
12 #-
13 #- This program is distributed in the hope that it will be useful,
14 #- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #- GNU General Public License for more details.
17 #-
18 #- You should have received a copy of the GNU General Public License
19 #- along with this program; if not, write to the Free Software
20 #- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 0 ▶use urpm;
Subroutine BEGIN: called CC=1 SCAR=2.6
24 use urpm::args;
25 use urpm::msg;
26 use urpm::install;
27 use urpm::media;
28 use urpm::select;
29 use urpm::orphans;
30 use urpm::get_pkgs;
31 use urpm::signature;
32 use urpm::util qw(find intersection member partition);
33  
34 #- global boolean options
35 my ($auto_select, $no_install, $install_src, $clean, $noclean, $force, $parallel, $test);
36 #- global counters
37 my ($ok, $nok);
38 my $exit_code;
39  
40  
41 =head1 NAME
42  
43 urpm::main_loop - The install/remove main loop for urpm based programs (urpmi, gurpmi, rpmdrake, drakx)
44  
45 =head1 SYNOPSIS
46  
47 =head1 DESCRIPTION
48  
49 =over
50  
51 =cut
52  
53 sub _download_callback {
54 my ($urpm, $callbacks, $raw_msg, $msg) = @_;
55 if (my $download_errors = delete $urpm->{download_errors}) {
56 $raw_msg = join("\n", @$download_errors, '');
57 }
58 $callbacks->{ask_yes_or_no}('', $raw_msg . "\n" . $msg . "\n" . N("Retry?"));
59 }
60  
61 sub _download_packages {
62 my ($urpm, $callbacks, $blists, $sources) = @_;
63 my @error_sources;
64 urpm::get_pkgs::download_packages_of_distant_media(
65 $urpm,
66 $blists,
67 $sources,
68 \@error_sources,
69 quiet => $options{verbose} < 0,
70 callback => $callbacks->{trans_log},
71 ask_retry => !$urpm->{options}{auto} && ($callbacks->{ask_retry} || sub {
72 _download_callback($urpm, $callbacks, @_);
73 }),
74 );
75 my @msgs;
76 if (@error_sources) {
77 $_->[0] = urpm::download::hide_password($_->[0]) foreach @error_sources;
78 my @bad = grep { $_->[1] eq 'bad' } @error_sources;
79 my @missing = grep { $_->[1] eq 'missing' } @error_sources;
80  
81 if (@missing) {
82 push @msgs, N("Installation failed, some files are missing:\n%s",
83 join("\n", map { " $_->[0]" } @missing))
84 . "\n" .
85 N("You may need to update your urpmi database.");
86 $urpm->{nb_install} -= scalar @missing;
87 }
88 if (@bad) {
89 push @msgs, N("Installation failed, bad rpms:\n%s",
90 join("\n", map { " $_->[0]" } @bad));
91 }
92 }
93
94 (\@error_sources, \@msgs);
95 }
96  
97 sub _download_all {
98 my ($urpm, $blists, $sources, $callbacks) = @_;
99 if ($urpm->{options}{'download-all'}) {
100 $urpm->{cachedir} = $urpm->{'urpmi-root'} . $urpm->{options}{'download-all'};
101 urpm::init_dir($urpm, $urpm->{cachedir});
102 }
103 my (undef, $available) = urpm::sys::df("$urpm->{cachedir}/rpms");
104  
105 if (!$urpm->{options}{ignoresize}) {
106 my ($download_size) = urpm::get_pkgs::get_distant_media_filesize($blists, $sources);
107 if ($download_size >= $available*1000) {
108 my $p = N("There is not enough space on your filesystem to download all packages (%s needed, %s available).\nAre you sure you want to continue?", formatXiB($download_size), formatXiB($available*1000));
109 $force || urpm::msg::ask_yes_or_no($p) or return 10;
110 }
111 }
112  
113 #download packages one by one so that we don't try to download them again
114 #and again if the user has to restart urpmi because of some failure
115 my %downloaded_pkgs;
116 foreach my $blist (@$blists) {
117 foreach my $pkg (keys %{$blist->{pkgs}}) {
118 next if $downloaded_pkgs{$pkg};
119 my $blist_one = [{ pkgs => { $pkg => $blist->{pkgs}{$pkg} }, medium => $blist->{medium} }];
120 my ($error_sources) = _download_packages($urpm, $callbacks, $blist_one, $sources);
121 if (@$error_sources) {
122 return 10;
123 }
124 $downloaded_pkgs{$pkg} = 1;
125 }
126 }
127 }
128  
129 sub _verify_rpm {
130 my ($urpm, $callbacks, $transaction_sources_install, $transaction_sources) = @_;
131 $callbacks->{pre_check_sig} and $callbacks->{pre_check_sig}->();
132 my @bad_signatures = urpm::signature::check($urpm, $transaction_sources_install, $transaction_sources,
133 callback => $callbacks->{check_sig}, basename => $options{basename}
134 );
135  
136 if (@bad_signatures) {
137 my $msg = @bad_signatures == 1 ?
138 N("The following package has bad signature")
139 : N("The following packages have bad signatures");
140 my $msg2 = N("Do you want to continue installation ?");
141 my $p = join "\n", @bad_signatures;
142 my $res = $callbacks->{bad_signature}->("$msg:\n$p\n", $msg2);
143 return $res ? 0 : 16;
144 }
145 }
146  
147 sub _install_src {
148 my ($urpm, $transaction_sources_install, $transaction_sources) = @_;
149 if (my @l = grep { /\.src\.rpm$/ } values %$transaction_sources_install, values %$transaction_sources) {
150 my $rpm_opt = $options{verbose} >= 0 ? 'vh' : '';
151 push @l, "--root", $urpm->{root} if $urpm->{root};
152 system("rpm", "-i$rpm_opt", @l);
153 #- Warning : the following message is parsed in urpm::parallel_*
154 if ($?) {
155 $urpm->{print}(N("Installation failed"));
156 ++$nok;
157 } elsif ($urpm->{options}{'post-clean'}) {
158 if (my @tmp_srpm = grep { urpm::is_temporary_file($urpm, $_) } @l) {
159 $urpm->{log}(N("removing installed rpms (%s)", join(' ', @tmp_srpm)));
160 unlink @tmp_srpm;
161 }
162 }
163 }
164 }
165  
166 sub clean_trans_sources_from_src_packages {
167 my ($urpm, $transaction_sources_install, $transaction_sources) = @_;
168 foreach ($transaction_sources_install, $transaction_sources) {
169 foreach my $id (keys %$_) {
170 my $pkg = $urpm->{depslist}[$id] or next;
171 $pkg->arch eq 'src' and delete $_->{$id};
172 }
173 }
174 }
175  
176 sub _continue_on_error {
177 my ($urpm, $callbacks, $msgs, $error_sources, $formatted_errors) = @_;
178 my $go_on;
179 if ($urpm->{options}{auto}) {
180 push @$formatted_errors, @$msgs;
181 } else {
182 my $sub = $callbacks->{ask_for_bad_or_missing} || $callbacks->{ask_yes_or_no};
183 $go_on = $sub->(
184 N("Installation failed"),
185 join("\n\n", @$msgs, N("Try to continue anyway?")));
186 }
187 if (!$go_on) {
188 my @missing = grep { $_->[1] eq 'missing' } @$error_sources;
189 if (@missing) {
190 $exit_code = $ok ? 13 : 14;
191 }
192 return 0;
193 }
194 return 1;
195 }
196  
197 sub _handle_removable_media {
198 my ($urpm, $callbacks, $blists, $sources) = @_;
199 urpm::removable::try_mounting_non_cdroms($urpm, $blists);
200  
201 $callbacks->{pre_removable} and $callbacks->{pre_removable}->();
202 require urpm::cdrom;
203 urpm::cdrom::copy_packages_of_removable_media($urpm,
204 $blists, $sources,
205 $callbacks->{copy_removable});
206 $callbacks->{post_removable} and $callbacks->{post_removable}->();
207 }
208  
209 sub _init_common_options {
210 my ($urpm, $state, $callbacks) = @_;
211 (
212 urpm::install::options($urpm),
213 test => $test,
214 deploops => $options{deploops},
215 verbose => $options{verbose},
216 script_fd => $urpm->{options}{script_fd},
217 oldpackage => $state->{oldpackage},
218 justdb => $options{justdb},
219 replacepkgs => $options{replacepkgs},
220 callback_close_helper => $callbacks->{close_helper},
221 callback_error => $callbacks->{error},
222 callback_inst => $callbacks->{inst},
223 callback_open_helper => $callbacks->{open_helper},
224 callback_trans => $callbacks->{trans},
225 callback_uninst => $callbacks->{uninst},
226 callback_verify => $callbacks->{verify},
227 raw_message => 1,
228 );
229 }
230  
231 sub _log_installing {
232 my ($urpm, $transaction_sources_install, $transaction_sources) = @_;
233 if (my @packnames = (values %$transaction_sources_install, values %$transaction_sources)) {
234 (my $common_prefix) = $packnames[0] =~ m!^(.*)/!;
235 if (length($common_prefix) && @packnames == grep { m!^\Q$common_prefix/! } @packnames) {
236 #- there's a common prefix, simplify message
237 $urpm->{print}(N("installing %s from %s", join(' ', map { s!.*/!!; $_ } @packnames), $common_prefix));
238 } else {
239 $urpm->{print}(N("installing %s", join "\n", @packnames));
240 }
241 }
242 }
243  
244 sub _run_parallel_transaction {
245 my ($urpm, $state, $transaction_sources, $transaction_sources_install) = @_;
246 $urpm->{print}(N("distributing %s", join(' ', values %$transaction_sources_install, values %$transaction_sources)));
247 #- no remove are handle here, automatically done by each distant node.
248 $urpm->{log}("starting distributed install");
249 $urpm->{parallel_handler}->parallel_install(
250 $urpm,
251 [ keys %{$state->{rejected} || {}} ], $transaction_sources_install, $transaction_sources,
252 test => $test,
253 excludepath => $urpm->{options}{excludepath}, excludedocs => $urpm->{options}{excludedocs},
254 );
255 }
256  
257 sub _run_transaction {
258 my ($urpm, $state, $callbacks, $set, $transaction_sources_install, $transaction_sources, $errors) = @_;
259  
260 my $options = $urpm->{options};
261 my $allow_force = $options->{'allow-force'};
262  
263 my $to_remove = $allow_force ? [] : $set->{remove} || [];
264  
265 $urpm->{log}("starting installing packages");
266
267 urpm::orphans::add_unrequested($urpm, $state) if !$test;
268  
269 my %install_options_common = _init_common_options($urpm, $state, $callbacks);
270  
271 install:
272 my @l = urpm::install::install($urpm,
273 $to_remove,
274 $transaction_sources_install, $transaction_sources,
275 %install_options_common,
276 );
277  
278 if (!@l) {
279 ++$ok;
280 return 1;
281 }
282  
283 my ($raw_error, $translated) = partition { /^(badarch|bados|installed|badrelocate|conflicts|installed|diskspace|disknodes|requires|conflicts|unknown)\@/ } @l;
284 @l = @$translated;
285 my $fatal = find { /^disk/ } @$raw_error;
286 my $no_question = $fatal || $options->{auto};
287  
288 #- Warning : the following message is parsed in urpm::parallel_*
289 my $msg = N("Installation failed:") . "\n" . join("\n", map { "\t$_" } @l) . "\n";
290 if (!$no_question && !$install_options_common{nodeps} && ($options->{'allow-nodeps'} || $allow_force)) {
291 if ($callbacks->{ask_yes_or_no}->(N("Installation failed"),
292 $msg . N("Try installation without checking dependencies?"))) {
293 $urpm->{log}("starting installing packages without deps");
294 $install_options_common{nodeps} = 1;
295 # try again:
296 goto install;
297 }
298 } elsif (!$no_question && !$install_options_common{force} && $allow_force) {
299 if ($callbacks->{ask_yes_or_no}->(N("Installation failed"),
300 $msg . N("Try harder to install (--force)?"))) {
301 $urpm->{log}("starting force installing packages without deps");
302 $install_options_common{force} = 1;
303 # try again:
304 goto install;
305 }
306 }
307 $urpm->{log}($msg);
308  
309 ++$nok;
310 push @$errors, @l;
311  
312 !$fatal;
313 }
314  
315 =item run($urpm, $state, $something_was_to_be_done, $ask_unselect, $callbacks)
316  
317 Run the main urpm loop:
318  
319 =over
320  
321 =item * mount removable media if needed
322  
323 =item * split the work in smaller transactions
324  
325 =item * for each transaction:
326  
327 =over
328  
329 =item * prepare the transaction
330  
331 =item * download packages needed for this small transaction
332  
333 =item * verify packages
334  
335 =item * split package that should be installed instead of upgraded,
336  
337 =item * install source package only (whatever the user is root or not, but use rpm for that)
338  
339 =item * install/remove other packages
340  
341 =back
342  
343 =item * migrate the chrooted rpmdb if needed
344  
345 =item * display the final success/error message(s)
346  
347 =back
348  
349 Warning: locking is left to callers...
350  
351 Parameters:
352  
353 =over
354  
355 =item $urpm: the urpm object
356  
357 =item $state: the state object (see L<URPM>)
358  
359 =item $something_was_to_be_done
360  
361 =item $ask_unselect: an array ref of packages that could not be selected
362  
363 =item $callbacks: a hash ref of callbacks :
364  
365 =over
366  
367 =item packages download:
368  
369 =over
370  
371 =item trans_log($mode, $file, $percent, $total, $eta, $speed): called for displaying download progress
372  
373 =item post_download(): called after completing download of packages
374  
375 =back
376  
377 =item interaction with user:
378  
379 =over
380  
381 =item ask_yes_or_no($_title, $msg)
382  
383 =item need_restart($need_restart_formatted) called when restarting urpmi is needed (priority upgrades)
384  
385 =item message($_title, $msg): display a message (with a title for GUIes)
386  
387 =back
388  
389 =item signature management:
390  
391 =over
392  
393 =item pre_check_sig(): signature checking startup (for rpmdrake)
394  
395 =item check_sig(): signature checking progress (for rpmdrake)
396  
397 =item bad_signature($msg, $msg2): called when a package is not/badly signed
398  
399 =back
400  
401 =item removable media management:
402  
403 =over
404  
405 =item pre_removable(): called before handling removable media (for rpmdrake)
406  
407 =item copy_removable($medium_name): called for asking inserting a CD/DVD
408  
409 =item post_extract($set, $transaction_sources, $transaction_sources_install) called after handling removable media (for rpmdrake)
410  
411 =back
412  
413 =item packages installation callbacks (passed to urpm::install::install(), see L<URPM> for parameters)
414  
415 =over
416  
417 =item open_helper(): called when opening a package, must return a fd
418  
419 =item close_helper(): called when package is closed
420  
421 =item inst() called for package opening/progress/end
422  
423 =item trans() called for transaction opening/progress/end
424  
425 =item uninst(): called for erasure progress
426  
427 =item error() called for cpio, script or unpacking errors
428  
429 =back
430  
431 =item finish callbacks (mainly GUI callbacks for rpmdrake/gurpmi/drakx)
432  
433 =over
434  
435 =item completed(): called when everything is completed (for cleanups)
436  
437 =item trans_error_summary($nok, $errors) called to warn than $nok transactions failed with $errors messages
438  
439 =item success_summary() called on success
440  
441 =item already_installed_or_not_installable($msg1, $msg2)
442  
443 =back
444  
445 =back
446  
447 =back
448  
449 =cut
450  
451  
452 sub run {
453 my ($urpm, $state, $something_was_to_be_done, $ask_unselect, $callbacks) = @_;
454  
455 #- global boolean options
456 ($auto_select, $no_install, $install_src, $clean, $noclean, $force, $parallel, $test) =
457 ($::auto_select, $::no_install, $::install_src, $::clean, $::noclean, $::force, $::parallel, $::test);
458  
459 urpm::get_pkgs::clean_all_cache($urpm) if $clean;
460  
461 my $options = $urpm->{options};
462  
463 my ($local_sources, $blists) = urpm::get_pkgs::selected2local_and_blists($urpm,
464 $state->{selected},
465 clean_other => !$noclean && $options->{'pre-clean'},
466 );
467 if (!$local_sources && !$blists) {
468 $urpm->{fatal}(3, N("unable to get source packages, aborting"));
469 }
470  
471 my %sources = %$local_sources;
472  
473 _handle_removable_media($urpm, $callbacks, $blists, \%sources);
474  
475 if (exists $options->{'download-all'}) {
476 _download_all($urpm, $blists, \%sources, $callbacks);
477 }
478  
479 #- now create transaction just before installation, this will save user impression of slowness.
480 #- split of transaction should be disabled if --test is used.
481 urpm::install::build_transaction_set_($urpm, $state,
482 nodeps => $options->{'allow-nodeps'} || $options->{'allow-force'},
483 keep => $options->{keep},
484 split_level => $options->{'split-level'},
485 split_length => !$test && $options->{'split-length'});
486  
487 if ($options{debug__do_not_install}) {
488 $urpm->{debug} = sub { print STDERR "$_[0]\n" };
489 }
490  
491 $urpm->{debug} and $urpm->{debug}(join("\n", "scheduled sets of transactions:",
492 urpm::install::transaction_set_to_string($urpm, $state->{transaction} || [])));
493  
494 $options{debug__do_not_install} and exit 0;
495  
496 ($ok, $nok) = (0, 0);
497 my (@errors, @formatted_errors);
498 $exit_code = 0;
499  
500 urpm::select::migrate_forward_rpmdb_db_if_needed($urpm);
501  
502 my $migrate_back_rpmdb_db_version =
503 $urpm->{root} && urpm::select::should_we_migrate_back_rpmdb_db_version($urpm, $state);
504  
505 #- now process each remove/install transaction
506 foreach my $set (@{$state->{transaction} || []}) {
507  
508 #- put a blank line to separate with previous transaction or user question.
509 $urpm->{print}("\n") if $options{verbose} >= 0;
510  
511 my ($transaction_blists, $transaction_sources) =
512 urpm::install::prepare_transaction($set, $blists, \%sources);
513  
514 #- first, filter out what is really needed to download for this small transaction.
515 my ($error_sources, $msgs) = _download_packages($urpm, $callbacks, $transaction_blists, $transaction_sources);
516 if (@$error_sources) {
517 $nok++;
518 last if !_continue_on_error($urpm, $callbacks, $msgs, $error_sources, \@formatted_errors);
519 }
520  
521 $callbacks->{post_download} and $callbacks->{post_download}->();
522  
523 #- extract packages that should be installed instead of upgraded,
524 my %transaction_sources_install = %{$urpm->extract_packages_to_install($transaction_sources) || {}};
525 $callbacks->{post_extract} and $callbacks->{post_extract}->($set, $transaction_sources, \%transaction_sources_install);
526  
527 #- verify packages
528 if (!$force && ($options->{'verify-rpm'} || grep { $_->{'verify-rpm'} } @{$urpm->{media}})) {
529 my $res = _verify_rpm($urpm, $callbacks, \%transaction_sources_install, $transaction_sources);
530 $res and return $res;
531 }
532  
533 #- install source package only (whatever the user is root or not, but use rpm for that).
534 if ($install_src) {
535 _install_src($urpm, \%transaction_sources_install, $transaction_sources);
536 next;
537 }
538  
539 next if $no_install;
540  
541 #- clean to remove any src package now.
542 clean_trans_sources_from_src_packages($urpm, \%transaction_sources_install, $transaction_sources);
543  
544 #- install/remove other packages
545 if (keys(%transaction_sources_install) || keys(%$transaction_sources) || $set->{remove}) {
546 if ($parallel) {
547 _run_parallel_transaction($urpm, $state, $transaction_sources, \%transaction_sources_install);
548 } else {
549 if ($options{verbose} >= 0) {
550 _log_installing($urpm, \%transaction_sources_install, $transaction_sources);
551 }
552 bug_log(scalar localtime(), " ", join(' ', values %transaction_sources_install, values %$transaction_sources), "\n");
553  
554 _run_transaction($urpm, $state, $callbacks, $set, \%transaction_sources_install, $transaction_sources, \@errors)
555 or last;
556 }
557 }
558  
559 last if $callbacks->{is_canceled} && $callbacks->{is_canceled}->();
560 }
561  
562 #- migrate the chrooted rpmdb if needed
563 if ($migrate_back_rpmdb_db_version) {
564 urpm::sys::migrate_back_rpmdb_db_version($urpm, $urpm->{root});
565 }
566  
567 $callbacks->{completed} and $callbacks->{completed}->();
568  
569 _finish($urpm, $state, $callbacks, \@errors, \@formatted_errors, $ask_unselect, $something_was_to_be_done);
570  
571 $exit_code;
572 }
573  
574 sub _finish {
575 my ($urpm, $state, $callbacks, $errors, $formatted_errors, $ask_unselect, $something_was_to_be_done) = @_;
576  
577 if ($nok) {
578 $callbacks->{trans_error_summary} and $callbacks->{trans_error_summary}->($nok, $errors);
579 if (@$formatted_errors) {
580 $urpm->{print}(join("\n", @$formatted_errors));
581 }
582 if (@$errors) {
583 $urpm->{print}(N("Installation failed:") . join("\n", map { "\t$_" } @$errors));
584 }
585 $exit_code ||= $ok ? 11 : 12;
586 } else {
587 $callbacks->{success_summary} and $callbacks->{success_summary}->();
588 if ($something_was_to_be_done || $auto_select) {
589 if (@{$state->{transaction} || []} == 0 && @$ask_unselect == 0) {
590 if ($auto_select) {
591 if ($options{verbose} >= 0) {
592 #- Warning : the following message is parsed in urpm::parallel_*
593 $urpm->{print}(N("Packages are up to date"));
594 }
595 } else {
596 if ($callbacks->{already_installed_or_not_installable}) {
597 my $msg = urpm::select::translate_already_installed($state);
598 $callbacks->{already_installed_or_not_installable}->([$msg], []);
599 }
600 }
601 $exit_code = 15 if our $expect_install;
602 } elsif ($test && $exit_code == 0) {
603 #- Warning : the following message is parsed in urpm::parallel_*
604 print N("Installation is possible"), "\n";
605 } else {
606 handle_need_restart($urpm, $state, $callbacks);
607 }
608 }
609 }
610 $exit_code;
611 }
612  
613 sub handle_need_restart {
614 my ($urpm, $state, $callbacks) = @_;
615  
616 return if $urpm->{root} && !$ENV{URPMI_TEST_RESTART};
617 return if !$callbacks->{need_restart};
618  
619 if (intersection([ keys %{$state->{selected}} ],
620 [ keys %{$urpm->{provides}{'should-restart'}} ])) {
621 if (my $need_restart_formatted = urpm::sys::need_restart_formatted($urpm->{root})) {
622 $callbacks->{need_restart}($need_restart_formatted);
623  
624 # need_restart() accesses rpm db, so we need to ensure things are clean:
625 urpm::sys::may_clean_rpmdb_shared_regions($urpm, $options{test});
626 }
627 }
628 }
629  
630 1;
631  
632 =back
633  
634 =head1 COPYRIGHT
635  
636 Copyright (C) 1999-2005 MandrakeSoft SA
637  
638 Copyright (C) 2005-2010 Mandriva SA
639  
640 Copyright (C) 2011-2020 Mageia
641  
642 =cut
« urpm/lock.pm ↑ Summary urpm/media.pm »