File Coverage

blib/lib/urpm/main_loop.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
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-2017 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     1   1711 use strict;
  1         2  
  1         27  
23 1     1   25 use urpm;
  0            
  0            
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             raw_message => 1,
227             );
228             }
229              
230             sub _log_installing {
231             my ($urpm, $transaction_sources_install, $transaction_sources) = @_;
232             if (my @packnames = (values %$transaction_sources_install, values %$transaction_sources)) {
233             (my $common_prefix) = $packnames[0] =~ m!^(.*)/!;
234             if (length($common_prefix) && @packnames == grep { m!^\Q$common_prefix/! } @packnames) {
235             #- there's a common prefix, simplify message
236             $urpm->{print}(N("installing %s from %s", join(' ', map { s!.*/!!; $_ } @packnames), $common_prefix));
237             } else {
238             $urpm->{print}(N("installing %s", join "\n", @packnames));
239             }
240             }
241             }
242              
243             sub _run_parallel_transaction {
244             my ($urpm, $state, $transaction_sources, $transaction_sources_install) = @_;
245             $urpm->{print}(N("distributing %s", join(' ', values %$transaction_sources_install, values %$transaction_sources)));
246             #- no remove are handle here, automatically done by each distant node.
247             $urpm->{log}("starting distributed install");
248             $urpm->{parallel_handler}->parallel_install(
249             $urpm,
250             [ keys %{$state->{rejected} || {}} ], $transaction_sources_install, $transaction_sources,
251             test => $test,
252             excludepath => $urpm->{options}{excludepath}, excludedocs => $urpm->{options}{excludedocs},
253             );
254             }
255              
256             sub _run_transaction {
257             my ($urpm, $state, $callbacks, $set, $transaction_sources_install, $transaction_sources, $errors) = @_;
258              
259             my $options = $urpm->{options};
260             my $allow_force = $options->{'allow-force'};
261              
262             my $to_remove = $allow_force ? [] : $set->{remove} || [];
263              
264             $urpm->{log}("starting installing packages");
265            
266             urpm::orphans::add_unrequested($urpm, $state) if !$test;
267              
268             my %install_options_common = _init_common_options($urpm, $state, $callbacks);
269              
270             install:
271             my @l = urpm::install::install($urpm,
272             $to_remove,
273             $transaction_sources_install, $transaction_sources,
274             %install_options_common,
275             );
276              
277             if (!@l) {
278             ++$ok;
279             return 1;
280             }
281              
282             my ($raw_error, $translated) = partition { /^(badarch|bados|installed|badrelocate|conflicts|installed|diskspace|disknodes|requires|conflicts|unknown)\@/ } @l;
283             @l = @$translated;
284             my $fatal = find { /^disk/ } @$raw_error;
285             my $no_question = $fatal || $options->{auto};
286              
287             #- Warning : the following message is parsed in urpm::parallel_*
288             my $msg = N("Installation failed:") . "\n" . join("\n", map { "\t$_" } @l) . "\n";
289             if (!$no_question && !$install_options_common{nodeps} && ($options->{'allow-nodeps'} || $allow_force)) {
290             if ($callbacks->{ask_yes_or_no}->(N("Installation failed"),
291             $msg . N("Try installation without checking dependencies?"))) {
292             $urpm->{log}("starting installing packages without deps");
293             $install_options_common{nodeps} = 1;
294             # try again:
295             goto install;
296             }
297             } elsif (!$no_question && !$install_options_common{force} && $allow_force) {
298             if ($callbacks->{ask_yes_or_no}->(N("Installation failed"),
299             $msg . N("Try harder to install (--force)?"))) {
300             $urpm->{log}("starting force installing packages without deps");
301             $install_options_common{force} = 1;
302             # try again:
303             goto install;
304             }
305             }
306             $urpm->{log}($msg);
307              
308             ++$nok;
309             push @$errors, @l;
310              
311             !$fatal;
312             }
313              
314             =item run($urpm, $state, $something_was_to_be_done, $ask_unselect, $callbacks)
315              
316             Run the main urpm loop:
317              
318             =over
319              
320             =item * mount removable media if needed
321              
322             =item * split the work in smaller transactions
323              
324             =item * for each transaction:
325              
326             =over
327              
328             =item * prepare the transaction
329              
330             =item * download packages needed for this small transaction
331              
332             =item * verify packages
333              
334             =item * split package that should be installed instead of upgraded,
335              
336             =item * install source package only (whatever the user is root or not, but use rpm for that)
337              
338             =item * install/remove other packages
339              
340             =back
341              
342             =item * migrate the chrooted rpmdb if needed
343              
344             =item * display the final success/error message(s)
345              
346             =back
347              
348             Warning: locking is left to callers...
349              
350             Parameters:
351              
352             =over
353              
354             =item $urpm: the urpm object
355              
356             =item $state: the state object (see L)
357              
358             =item $something_was_to_be_done
359              
360             =item $ask_unselect: an array ref of packages that could not be selected
361              
362             =item $callbacks: a hash ref of callbacks :
363              
364             =over
365              
366             =item packages download:
367              
368             =over
369              
370             =item trans_log($mode, $file, $percent, $total, $eta, $speed): called for displaying download progress
371              
372             =item post_download(): called after completing download of packages
373              
374             =back
375              
376             =item interaction with user:
377              
378             =over
379              
380             =item ask_yes_or_no($_title, $msg)
381              
382             =item need_restart($need_restart_formatted) called when restarting urpmi is needed (priority upgrades)
383              
384             =item message($_title, $msg): display a message (with a title for GUIes)
385              
386             =back
387              
388             =item signature management:
389              
390             =over
391              
392             =item pre_check_sig(): signature checking startup (for rpmdrake)
393              
394             =item check_sig(): signature checking progress (for rpmdrake)
395              
396             =item bad_signature($msg, $msg2): called when a package is not/badly signed
397              
398             =back
399              
400             =item removable media management:
401              
402             =over
403              
404             =item pre_removable(): called before handling removable media (for rpmdrake)
405              
406             =item copy_removable($medium_name): called for asking inserting a CD/DVD
407              
408             =item post_extract($set, $transaction_sources, $transaction_sources_install) called after handling removable media (for rpmdrake)
409              
410             =back
411              
412             =item packages installation callbacks (passed to urpm::install::install(), see L for parameters)
413              
414             =over
415              
416             =item open_helper(): called when opening a package, must return a fd
417              
418             =item close_helper(): called when package is closed
419              
420             =item inst() called for package opening/progress/end
421              
422             =item trans() called for transaction opening/progress/end
423              
424             =item uninst(): called for erasure progress
425              
426             =item error() called for cpio, script or unpacking errors
427              
428             =back
429              
430             =item finish callbacks (mainly GUI callbacks for rpmdrake/gurpmi/drakx)
431              
432             =over
433              
434             =item completed(): called when everything is completed (for cleanups)
435              
436             =item trans_error_summary($nok, $errors) called to warn than $nok transactions failed with $errors messages
437              
438             =item success_summary() called on success
439              
440             =item already_installed_or_not_installable($msg1, $msg2)
441              
442             =back
443              
444             =back
445              
446             =back
447              
448             =cut
449              
450              
451             sub run {
452             my ($urpm, $state, $something_was_to_be_done, $ask_unselect, $callbacks) = @_;
453              
454             #- global boolean options
455             ($auto_select, $no_install, $install_src, $clean, $noclean, $force, $parallel, $test) =
456             ($::auto_select, $::no_install, $::install_src, $::clean, $::noclean, $::force, $::parallel, $::test);
457              
458             urpm::get_pkgs::clean_all_cache($urpm) if $clean;
459              
460             my $options = $urpm->{options};
461              
462             my ($local_sources, $blists) = urpm::get_pkgs::selected2local_and_blists($urpm,
463             $state->{selected},
464             clean_other => !$noclean && $options->{'pre-clean'},
465             );
466             if (!$local_sources && !$blists) {
467             $urpm->{fatal}(3, N("unable to get source packages, aborting"));
468             }
469              
470             my %sources = %$local_sources;
471              
472             _handle_removable_media($urpm, $callbacks, $blists, \%sources);
473              
474             if (exists $options->{'download-all'}) {
475             _download_all($urpm, $blists, \%sources, $callbacks);
476             }
477              
478             #- now create transaction just before installation, this will save user impression of slowness.
479             #- split of transaction should be disabled if --test is used.
480             urpm::install::build_transaction_set_($urpm, $state,
481             nodeps => $options->{'allow-nodeps'} || $options->{'allow-force'},
482             keep => $options->{keep},
483             split_level => $options->{'split-level'},
484             split_length => !$test && $options->{'split-length'});
485              
486             if ($options{debug__do_not_install}) {
487             $urpm->{debug} = sub { print STDERR "$_[0]\n" };
488             }
489              
490             $urpm->{debug} and $urpm->{debug}(join("\n", "scheduled sets of transactions:",
491             urpm::install::transaction_set_to_string($urpm, $state->{transaction} || [])));
492              
493             $options{debug__do_not_install} and exit 0;
494              
495             ($ok, $nok) = (0, 0);
496             my (@errors, @formatted_errors);
497             $exit_code = 0;
498              
499             my $migrate_back_rpmdb_db_version =
500             $urpm->{root} && urpm::select::should_we_migrate_back_rpmdb_db_version($urpm, $state);
501              
502             #- now process each remove/install transaction
503             foreach my $set (@{$state->{transaction} || []}) {
504              
505             #- put a blank line to separate with previous transaction or user question.
506             $urpm->{print}("\n") if $options{verbose} >= 0;
507              
508             my ($transaction_blists, $transaction_sources) =
509             urpm::install::prepare_transaction($set, $blists, \%sources);
510              
511             #- first, filter out what is really needed to download for this small transaction.
512             my ($error_sources, $msgs) = _download_packages($urpm, $callbacks, $transaction_blists, $transaction_sources);
513             if (@$error_sources) {
514             $nok++;
515             last if !_continue_on_error($urpm, $callbacks, $msgs, $error_sources, \@formatted_errors);
516             }
517              
518             $callbacks->{post_download} and $callbacks->{post_download}->();
519              
520             #- extract packages that should be installed instead of upgraded,
521             my %transaction_sources_install = %{$urpm->extract_packages_to_install($transaction_sources) || {}};
522             $callbacks->{post_extract} and $callbacks->{post_extract}->($set, $transaction_sources, \%transaction_sources_install);
523              
524             #- verify packages
525             if (!$force && ($options->{'verify-rpm'} || grep { $_->{'verify-rpm'} } @{$urpm->{media}})) {
526             my $res = _verify_rpm($urpm, $callbacks, \%transaction_sources_install, $transaction_sources);
527             $res and return $res;
528             }
529              
530             #- install source package only (whatever the user is root or not, but use rpm for that).
531             if ($install_src) {
532             _install_src($urpm, \%transaction_sources_install, $transaction_sources);
533             next;
534             }
535              
536             next if $no_install;
537              
538             #- clean to remove any src package now.
539             clean_trans_sources_from_src_packages($urpm, \%transaction_sources_install, $transaction_sources);
540              
541             #- install/remove other packages
542             if (keys(%transaction_sources_install) || keys(%$transaction_sources) || $set->{remove}) {
543             if ($parallel) {
544             _run_parallel_transaction($urpm, $state, $transaction_sources, \%transaction_sources_install);
545             } else {
546             if ($options{verbose} >= 0) {
547             _log_installing($urpm, \%transaction_sources_install, $transaction_sources);
548             }
549             bug_log(scalar localtime(), " ", join(' ', values %transaction_sources_install, values %$transaction_sources), "\n");
550              
551             _run_transaction($urpm, $state, $callbacks, $set, \%transaction_sources_install, $transaction_sources, \@errors)
552             or last;
553             }
554             }
555              
556             last if $callbacks->{is_canceled} && $callbacks->{is_canceled}->();
557             }
558              
559             #- migrate the chrooted rpmdb if needed
560             if ($migrate_back_rpmdb_db_version) {
561             urpm::sys::migrate_back_rpmdb_db_version($urpm, $urpm->{root});
562             }
563              
564             $callbacks->{completed} and $callbacks->{completed}->();
565              
566             _finish($urpm, $state, $callbacks, \@errors, \@formatted_errors, $ask_unselect, $something_was_to_be_done);
567              
568             $exit_code;
569             }
570              
571             sub _finish {
572             my ($urpm, $state, $callbacks, $errors, $formatted_errors, $ask_unselect, $something_was_to_be_done) = @_;
573              
574             if ($nok) {
575             $callbacks->{trans_error_summary} and $callbacks->{trans_error_summary}->($nok, $errors);
576             if (@$formatted_errors) {
577             $urpm->{print}(join("\n", @$formatted_errors));
578             }
579             if (@$errors) {
580             $urpm->{print}(N("Installation failed:") . join("\n", map { "\t$_" } @$errors));
581             }
582             $exit_code ||= $ok ? 11 : 12;
583             } else {
584             $callbacks->{success_summary} and $callbacks->{success_summary}->();
585             if ($something_was_to_be_done || $auto_select) {
586             if (@{$state->{transaction} || []} == 0 && @$ask_unselect == 0) {
587             if ($auto_select) {
588             if ($options{verbose} >= 0) {
589             #- Warning : the following message is parsed in urpm::parallel_*
590             $urpm->{print}(N("Packages are up to date"));
591             }
592             } else {
593             if ($callbacks->{already_installed_or_not_installable}) {
594             my $msg = urpm::select::translate_already_installed($state);
595             $callbacks->{already_installed_or_not_installable}->([$msg], []);
596             }
597             }
598             $exit_code = 15 if our $expect_install;
599             } elsif ($test && $exit_code == 0) {
600             #- Warning : the following message is parsed in urpm::parallel_*
601             print N("Installation is possible"), "\n";
602             } else {
603             handle_need_restart($urpm, $state, $callbacks);
604             }
605             }
606             }
607             $exit_code;
608             }
609              
610             sub handle_need_restart {
611             my ($urpm, $state, $callbacks) = @_;
612              
613             return if $urpm->{root} && !$ENV{URPMI_TEST_RESTART};
614             return if !$callbacks->{need_restart};
615              
616             if (intersection([ keys %{$state->{selected}} ],
617             [ keys %{$urpm->{provides}{'should-restart'}} ])) {
618             if (my $need_restart_formatted = urpm::sys::need_restart_formatted($urpm->{root})) {
619             $callbacks->{need_restart}($need_restart_formatted);
620              
621             # need_restart() accesses rpm db, so we need to ensure things are clean:
622             urpm::sys::may_clean_rpmdb_shared_regions($urpm, $options{test});
623             }
624             }
625             }
626              
627             1;
628              
629             =back
630              
631             =head1 COPYRIGHT
632              
633             Copyright (C) 1999-2005 MandrakeSoft SA
634              
635             Copyright (C) 2005-2010 Mandriva SA
636              
637             Copyright (C) 2011-2017 Mageia
638              
639             =cut