summaryrefslogtreecommitdiffstats
path: root/urpm/main_loop.pm
blob: 546c08280f6957a74ada1bdd0c8b880d7b6a4834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package urpm::main_loop;

# $Id$

#- Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA
#- Copyright (C) 2005-2007 Mandriva SA
#-
#- This program is free software; you can redistribute it and/or modify
#- it under the terms of the GNU General Public License as published by
#- the Free Software Foundation; either version 2, or (at your option)
#- any later version.
#-
#- This program is distributed in the hope that it will be useful,
#- but WITHOUT ANY WARRANTY; without even the implied warranty of
#- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#- GNU General Public License for more details.
#-
#- You should have received a copy of the GNU General Public License
#- along with this program; if not, write to the Free Software
#- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use strict;
use urpm;
use urpm::args;
use urpm::msg;
use urpm::install;
use urpm::media;
use urpm::select;
use urpm::orphans;
use urpm::get_pkgs;
use urpm::signature;
use urpm::util qw(untaint difference2 intersection member partition);

# locking is left to callers
sub run {
    my ($urpm, $state, $something_was_to_be_done, $ask_unselect, $_requested, $callbacks) = @_;

    #- global boolean options
    my ($auto_select, $no_install, $install_src, $clean, $noclean, $force, $parallel, $test, $env) =
      ($::auto_select, $::no_install, $::install_src, $::clean, $::noclean, $::force, $::parallel, $::test, $::env);

    urpm::get_pkgs::clean_all_cache($urpm) if $clean;

my ($local_sources, $blists) = urpm::get_pkgs::selected2local_and_blists($urpm,
    $state->{selected},
    clean_other => !$noclean && $urpm->{options}{'pre-clean'},
);
if (!$local_sources && !$blists) {
    $urpm->{fatal}(3, N("unable to get source packages, aborting"));
}

my %sources = %$local_sources;

urpm::removable::try_mounting_non_cdroms($urpm, $blists);

$callbacks->{pre_removable} and $callbacks->{pre_removable}->();
require urpm::cdrom;
urpm::cdrom::copy_packages_of_removable_media($urpm,
    $blists, \%sources,
    $callbacks->{copy_removable});
$callbacks->{post_removable} and $callbacks->{post_removable}->();

#- now create transaction just before installation, this will save user impression of slowness.
#- split of transaction should be disabled if --test is used.
urpm::install::build_transaction_set_($urpm, $state,
			  rpmdb => $env && "$env/rpmdb.cz",
			  nodeps => $urpm->{options}{'allow-nodeps'} || $urpm->{options}{'allow-force'},
			  keep => $urpm->{options}{keep},
			  split_level => $urpm->{options}{'split-level'},
			  split_length => !$test && $urpm->{options}{'split-length'});

    if ($options{debug__do_not_install}) {
	$urpm->{debug} = sub { print STDERR "$_[0]\n" };
    }

$urpm->{debug} and $urpm->{debug}(join("\n", "scheduled sets of transactions:", 
				       urpm::install::transaction_set_to_string($urpm, $state->{transaction} || [])));

$options{debug__do_not_install} and exit 0;

my ($ok, $nok) = (0, 0);
my @errors;
my $exit_code = 0;

my $migrate_back_rpmdb_db_version = 
  $urpm->{root} && urpm::select::should_we_migrate_back_rpmdb_db_version($urpm, $state);

foreach my $set (@{$state->{transaction} || []}) {

    #- put a blank line to separate with previous transaction or user question.
    print "\n" if $options{verbose} >= 0;

    my ($transaction_blists, $transaction_sources) = 
      urpm::install::prepare_transaction($urpm, $set, $blists, \%sources);

    #- first, filter out what is really needed to download for this small transaction.
    my @error_sources;
    urpm::get_pkgs::download_packages_of_distant_media($urpm,
	$transaction_blists,
	$transaction_sources,
	\@error_sources,
	quiet => $options{verbose} < 0,
	callback => $callbacks->{trans_log},
    );
    if (@error_sources) {
	$_->[0] = urpm::download::hide_password($_->[0]) foreach @error_sources;
	if (my @missing = grep { $_->[1] eq 'missing' } @error_sources) {
	    $exit_code = 10;
	    push @errors, map { "missing $_->[0]" } @missing;

	    my $msg = join("\n", map { "    $_->[0]" } @missing);
	    !$urpm->{options}{auto} && $callbacks->{ask_yes_or_no}->(
		N("Installation failed"), 
		N("Installation failed, some files are missing:\n%s\nYou may want to update your urpmi database", $msg)
		  . "\n\n" . N("Try to go on anyway? (y/N) ")) or last;
	}
	if (my @bad = grep { $_->[1] eq 'bad' } @error_sources) {
	    $exit_code = 11;
	    push @errors, map { "bad $_->[0]" } @bad;

	    my $msg = join("\n", map { "    $_->[0]" } @bad);
	    !$urpm->{options}{auto} && $callbacks->{ask_yes_or_no}->(
		N("Installation failed"), 
		N("Installation failed, bad rpms:\n%s", $msg)
		  . "\n\n" . N("Try to go on anyway? (y/N) ")) or last;
	}
    }

    $callbacks->{post_download} and $callbacks->{post_download}->();
    my %transaction_sources_install = %{$urpm->extract_packages_to_install($transaction_sources, $state) || {}};
    $callbacks->{post_extract} and $callbacks->{post_extract}->($set, $transaction_sources, \%transaction_sources_install);

    if (!$force && ($urpm->{options}{'verify-rpm'} || grep { $_->{'verify-rpm'} } @{$urpm->{media}})) {
        $callbacks->{pre_check_sig} and $callbacks->{pre_check_sig}->();
        # CHECK ME: rpmdrake passed "basename => 1" option:
	my @bad_signatures = urpm::signature::check($urpm, \%transaction_sources_install, $transaction_sources,
                                                 callback => $callbacks->{check_sig}
                                             );

	if (@bad_signatures) {
	    my $msg = @bad_signatures == 1 ?
	    	N("The following package has bad signature")
		: N("The following packages have bad signatures");
	    my $msg2 = N("Do you want to continue installation ?");
	    my $p = join "\n", @bad_signatures;
	    $callbacks->{bad_signature}->("$msg:\n$p\n", $msg2) or return 15;
	}
    }

    #- install source package only (whatever the user is root or not, but use rpm for that).
    if ($install_src) {
	if (my @l = grep { /\.src\.rpm$/ } values %transaction_sources_install, values %$transaction_sources) {
	    my $rpm_opt = $options{verbose} >= 0 ? 'vh' : '';
	    system("rpm", "-i$rpm_opt", @l, ($urpm->{root} ? ("--root", $urpm->{root}) : @{[]}));
	    #- Warning : the following message is parsed in urpm::parallel_*
	    if ($?) {
		print N("Installation failed"), "\n";
		++$nok;
	    } elsif ($urpm->{options}{'post-clean'}) {
		if (my @tmp_srpm = grep { urpm::is_temporary_file($urpm, $_) } @l) {
		    $urpm->{log}(N("removing installed rpms (%s)", join(' ', @tmp_srpm)));
		    unlink @tmp_srpm;
		}
	    }
	}
	next;
    }

    next if $no_install;

    #- clean to remove any src package now.
    foreach (\%transaction_sources_install, $transaction_sources) {
	foreach my $id (keys %$_) {
	    my $pkg = $urpm->{depslist}[$id] or next;
	    $pkg->arch eq 'src' and delete $_->{$id};
	}
    }

    if (keys(%transaction_sources_install) || keys(%$transaction_sources) || $set->{remove}) {
	if ($parallel) {
	    print N("distributing %s", join(' ', values %transaction_sources_install, values %$transaction_sources)), "\n";
	    #- no remove are handle here, automatically done by each distant node.
	    $urpm->{log}("starting distributed install");
	    $urpm->{parallel_handler}->parallel_install(
		$urpm,
		[ keys %{$state->{rejected} || {}} ], \%transaction_sources_install, $transaction_sources,
		test => $test,
		excludepath => $urpm->{options}{excludepath}, excludedocs => $urpm->{options}{excludedocs},
	    );
	} else {
	    if ($options{verbose} >= 0) {
	      if (my @packnames = (values %transaction_sources_install, values %$transaction_sources)) {
		(my $common_prefix) = $packnames[0] =~ m!^(.*)/!;
		if (length($common_prefix) && @packnames == grep { m!^\Q$common_prefix/! } @packnames) {
		    #- there's a common prefix, simplify message
		    print N("installing %s from %s", join(' ', map { s!.*/!!; $_ } @packnames), $common_prefix), "\n";
		} else {
		    print N("installing %s", join "\n", @packnames), "\n";
		}
	      }
	    }
	    my $to_remove = $urpm->{options}{'allow-force'} ? [] : $set->{remove} || [];
	    bug_log(scalar localtime(), " ", join(' ', values %transaction_sources_install, values %$transaction_sources), "\n");
	    $urpm->{log}("starting installing packages");
	    my %install_options_common = (
		urpm::install::options($urpm),
		test => $test,
		verbose => $options{verbose},
		oldpackage => $state->{oldpackage},
		justdb => $options{justdb},
		replacepkgs => $options{replacepkgs},
		callback_inst => $callbacks->{inst},
		callback_trans => $callbacks->{trans},
		callback_report_uninst => $callbacks->{callback_report_uninst},
	    );
	    
	    urpm::orphans::add_unrequested($urpm, $state);

	    my @l = urpm::install::install($urpm,
		$to_remove,
		\%transaction_sources_install, $transaction_sources,
		%install_options_common,
	    );
	    if (@l) {
		#- Warning : the following message is parsed in urpm::parallel_*
		my $msg = N("Installation failed:") . "\n" . join("\n",  map { "\t$_" } @l) . "\n";
		if ($urpm->{options}{auto} || !$urpm->{options}{'allow-nodeps'} && !$urpm->{options}{'allow-force'}) {
		    print $msg;
		    ++$nok;
		    ++$urpm->{logger_id};
		    push @errors, @l;
		} else {
		    $callbacks->{ask_yes_or_no}->(N("Installation failed"), 
						  $msg . N("Try installation without checking dependencies? (y/N) ")) or ++$nok, next;
		    $urpm->{log}("starting installing packages without deps");
		    @l = urpm::install::install($urpm,
			$to_remove,
			\%transaction_sources_install, $transaction_sources,
			nodeps => 1,
			%install_options_common,
		    );
		    if (@l) {
			#- Warning : the following message is parsed in urpm::parallel_*
			my $msg = N("Installation failed:") . "\n" . join("\n", map { "\t$_" } @l) . "\n";
			if (!$urpm->{options}{'allow-force'}) {
			    print $msg;
			    ++$nok;
			    ++$urpm->{logger_id};
			    push @errors, @l;
			} else {
			    $callbacks->{ask_yes_or_no}->(N("Installation failed"),
							  $msg . N("Try harder to install (--force)? (y/N) ")) or ++$nok, next;
			    $urpm->{log}("starting force installing packages without deps");
			    @l = urpm::install::install($urpm,
				$to_remove,
				\%transaction_sources_install, $transaction_sources,
				nodeps => 1, force => 1,
				%install_options_common,
			    );
			    if (@l) {
				#- Warning : the following message is parsed in urpm::parallel_*
				print N("Installation failed:") . "\n" . join("\n", map { "\t$_" } @l), "\n";
				++$nok;
				++$urpm->{logger_id};
				push @errors, @l;
			    } else {
				++$ok;
			    }
			}
		    } else {
			++$ok;
		    }
		}
	    } else {
		++$ok;
	    }
	}
    }
}

if ($migrate_back_rpmdb_db_version) {
    urpm::sys::migrate_back_rpmdb_db_version($urpm, $urpm->{root});
}

$callbacks->{completed} and $callbacks->{completed}->();

if ($nok) {
    $callbacks->{trans_error_summary} and $callbacks->{trans_error_summary}->($nok, \@errors);
    print N("Installation failed:"), "\n", map { "\t$_\n" } @errors;
    if ($exit_code) {
	$exit_code = $ok ? 13 : 14;
    } else {
	$exit_code = $ok ? 11 : 12;
    }
} else {
    $callbacks->{success_summary} and $callbacks->{success_summary}->();
    if ($something_was_to_be_done || $auto_select) {
	if (@{$state->{transaction} || []} == 0 && @$ask_unselect == 0) {
	    if ($auto_select) {
		if ($options{verbose} >= 0) {
		    #- Warning : the following message is parsed in urpm::parallel_*
		    print N("Packages are up to date"), "\n";
		}
	    } else {
		if ($callbacks->{already_installed_or_not_installable}) {
		    my $msg = urpm::select::translate_already_installed($state);
		    $callbacks->{already_installed_or_not_installable}->([$msg], []);
		}
	    }
	    $exit_code = 15 if our $expect_install;
	} elsif ($test && $exit_code == 0) {
	    #- Warning : the following message is parsed in urpm::parallel_*
	    print N("Installation is possible"), "\n";
	} elsif (intersection([ keys %{$state->{selected}} ],
			      [ keys %{$urpm->{provides}{'should-restart'}} ])) {
	    if (my $need_restart_formatted = urpm::sys::need_restart_formatted($urpm->{root})) {
		$callbacks->{need_restart}($need_restart_formatted) if $callbacks->{need_restart};

		# need_restart() accesses rpm db, so we need to ensure things are clean:
		urpm::sys::may_clean_rpmdb_shared_regions($urpm, $options{test});
	    }
	}
    }
}
    $exit_code;
}

1;