urpm/install.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/get_pkgs.pm ↑ Summary urpm/lock.pm »
1 package urpm::install;
2  
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use urpm;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use urpm::msg;
7 use urpm::util qw(cat_utf8 member);
8  
9  
10 =head1 NAME
11  
12 urpm::install - Package installation transaction routines for urpmi
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 =over
19  
20 =cut
21  
22 # size of the installation progress bar
23 my $progress_size = 45;
24 if (-t *STDOUT) {
25 eval {
26 require Term::ReadKey;
27 ($progress_size) = Term::ReadKey::GetTerminalSize();
28 $progress_size -= 35;
29 $progress_size < 5 and $progress_size = 5;
30 };
31 }
32  
33  
34 sub _hash_intersect_list {
35 my ($h, $l) = @_;
36 my %h;
37 foreach (@$l) {
38 exists $h->{$_} and $h{$_} = $h->{$_};
39 }
40 \%h;
41 }
42  
43 =item prepare_transaction($set, $blists, $sources)
44  
45 =cut
46  
47  
48 sub prepare_transaction {
49 my ($set, $blists, $sources) = @_;
50  
51 my @blists_subset = map {
52 +{ %$_, pkgs => _hash_intersect_list($_->{pkgs}, $set->{upgrade}) };
53 } @$blists;
54  
55 \@blists_subset, _hash_intersect_list($sources, $set->{upgrade});
56 }
57  
58 sub build_transaction_set_ {
59 my ($urpm, $state, %options) = @_;
60  
61 if ($urpm->{parallel_handler} || !$options{split_length} ||
62 keys %{$state->{selected}} < $options{split_level}) {
63 #- build simplest transaction (no split).
64 $urpm->build_transaction_set(undef, $state, split_length => 0);
65 } else {
66 my $db = urpm::db_open_or_die_($urpm);
67  
68 my $sig_handler = sub { undef $db; exit 3 };
69 local $SIG{INT} = $sig_handler;
70 local $SIG{QUIT} = $sig_handler;
71  
72 #- build transaction set...
73 $urpm->build_transaction_set($db, $state, split_length => $options{split_length}, keep => $options{keep});
74 }
75 }
76  
77 sub transaction_set_to_string {
78 my ($urpm, $set) = @_;
79  
80 my $format_list = sub { int(@_) . '=' . join(',', @_) };
81 map {
82 sprintf('remove=%s update=%s',
83 $format_list->(@{$_->{remove} || []}),
84 $format_list->(map { $urpm->{depslist}[$_]->name } @{$_->{upgrade} || []}));
85 } @$set;
86 }
87  
88 =item install_logger($urpm, $type, $id, $subtype, $amount, $total)
89  
90 Standard logger for transactions
91  
92 See L<URPM> for parameters
93  
94 =cut
95  
96 # install logger callback
97 my ($erase_logger, $index, $total_pkg, $uninst_count, $current_pkg, $rpm_version);
98 sub install_logger {
99 my ($urpm, $type, undef, $subtype, $amount, $total) = @_;
100 local $| = 1;
101  
102 if ($subtype eq 'start') {
103 $urpm->{logger_progress} = 0;
104 if ($type eq 'trans') {
105 $total_pkg = $urpm->{nb_install};
106 $urpm->{logger_count} ||= 0;
107 $uninst_count = 0;
108 my $p = N("Preparing...");
109 print $p, " " x (33 - length $p);
110 } else {
111 my $pname;
112 my $cnt;
113 if ($type eq 'uninst') {
114 $total_pkg = $urpm->{trans}->NElements - $index if !$uninst_count;
115 $cnt = ++$uninst_count;
116 $pname = N("removing %s", $current_pkg);
117 $erase_logger->($urpm, undef, undef, $subtype);
118 } else {
119 $pname = $urpm->{trans}->Element_name($index);
120 ++$urpm->{logger_count} if $pname;
121 $cnt = $pname ? $urpm->{logger_count} : '-';
122 }
123 require urpm::select;
124 $index++ if $rpm_version lt 4.13.0;
125 my $s = sprintf("%9s: %-22s", $cnt . "/" . $total_pkg, $pname);
126 print $s;
127 $s =~ / $/ or printf "\n%9s %-22s", '', '';
128 }
129 } elsif ($subtype eq 'stop') {
130 if ($urpm->{logger_progress} < $progress_size) {
131 $urpm->{print}('#' x ($progress_size - $urpm->{logger_progress}));
132 $urpm->{logger_progress} = 0;
133 }
134 } elsif ($subtype eq 'progress') {
135 my $new_progress = $total > 0 ? int($progress_size * $amount / $total) : $progress_size;
136 if ($new_progress > $urpm->{logger_progress}) {
137 print '#' x ($new_progress - $urpm->{logger_progress});
138 $urpm->{logger_progress} = $new_progress;
139 $urpm->{logger_progress} == $progress_size and print "\n";
140 }
141 }
142 }
143  
144 =item get_README_files($urpm, $trans, $pkg)
145  
146 =cut
147  
148 sub get_README_files {
149 my ($urpm, $trans, $pkg) = @_;
150  
151 foreach my $file ($pkg->doc_files) {
152 my ($kind) = $file =~ m!/README([^/]*)\.urpmi$! or next;
153 my $valid;
154 if ($kind eq '') {
155 $valid = 1;
156 } elsif ($kind eq '.install' && !$pkg->flag_installed) {
157 $valid = 1;
158 } elsif ($kind =~ /(.*)\.(upgrade|update)$/ && $pkg->flag_installed) {
159 if (!$1) {
160 $valid = 1;
161 } else {
162 my $version = $1;
163 foreach my $i (0 .. $trans->NElements - 1) {
164 $trans->Element_name($i) eq $pkg->name or next;
165  
166 # handle README.<version>-<release>.upgrade.urpmi:
167 # the content is displayed when upgrading from rpm older than <version>
168 my $vr = $trans->Element_version($i) . '-' . $trans->Element_release($i);
169 if (URPM::ranges_overlap("== $vr", "< $version")) {
170 $valid = 1;
171 last;
172 }
173 }
174 }
175 }
176 $valid and $urpm->{readmes}{$file} = $pkg->fullname;
177 }
178 }
179  
180 sub options {
181 my ($urpm) = @_;
182  
183 (
184 excludepath => $urpm->{options}{excludepath},
185 excludedocs => $urpm->{options}{excludedocs},
186 post_clean_cache => $urpm->{options}{'post-clean'},
187 nosize => $urpm->{options}{ignoresize},
188 ignorearch => $urpm->{options}{ignorearch},
189 noscripts => $urpm->{options}{noscripts},
190 replacefiles => $urpm->{options}{replacefiles},
191 );
192 }
193  
194 sub _schedule_packages_for_erasing {
195 my ($urpm, $trans, $remove) = @_;
196 foreach (@$remove) {
197 if ($trans->remove($_)) {
198 $urpm->{debug} and $urpm->{debug}("trans: scheduling removal of $_");
199 } else {
200 $urpm->{error}("unable to remove package " . $_);
201 }
202 }
203 }
204  
205 sub _apply_delta_rpm {
206 my ($urpm, $path, $mode, $pkg) = @_;
207 my $true_rpm = urpm::sys::apply_delta_rpm($path, "$urpm->{cachedir}/rpms", $pkg);
208 my $true_pkg;
209 if ($true_rpm) {
210 if (my ($id) = $urpm->parse_rpm($true_rpm)) {
211 $true_pkg = defined $id && $urpm->{depslist}[$id];
212 $mode->{$id} = $true_rpm;
213 } else {
214 $urpm->{error}("Failed to parse $true_pkg");
215 }
216 } else {
217 $urpm->{error}(N("unable to extract rpm from delta-rpm package %s", $path));
218 }
219 $true_rpm, $true_pkg;
220 }
221  
222 sub _schedule_packages {
223 my ($urpm, $trans, $install, $upgrade, %options) = @_;
224 my $update = 0;
225 my (@trans_pkgs, @produced_deltas);
226 foreach my $mode ($install, $upgrade) {
227 foreach (keys %$mode) {
228 my $pkg = $urpm->{depslist}[$_];
229 $pkg->update_header($mode->{$_}, keep_all_tags => 1);
230 my ($true_rpm, $true_pkg);
231 if ($pkg->payload_format eq 'drpm') { #- handle deltarpms
232 ($true_rpm, $true_pkg) = _apply_delta_rpm($urpm, $mode->{$_}, $mode, $pkg);
233 push @produced_deltas, ($mode->{$_} = $true_rpm); #- fix path
234 }
235 if ($urpm->{options}{reinstall} ? $trans->addReinstall($true_pkg || $pkg) :
236 $trans->add($true_pkg || $pkg, update => $update,
237 $options{excludepath} ? (excludepath => [ split /,/, $options{excludepath} ]) : ())) {
238 $urpm->{debug} and $urpm->{debug}(
239 sprintf('trans: scheduling %s of %s (id=%d, file=%s)',
240 $update ? 'update' : 'install',
241 scalar($pkg->fullname), $_, $mode->{$_}));
242 push @trans_pkgs, $pkg;
243  
244 } else {
245 $urpm->{error}(N("unable to install package %s", $mode->{$_}));
246 my $cachefile = "$urpm->{cachedir}/rpms/" . $pkg->filename;
247 if (-e $cachefile) {
248 $urpm->{error}(N("removing bad rpm (%s) from %s", $pkg->name, "$urpm->{cachedir}/rpms"));
249 unlink $cachefile or $urpm->{fatal}(1, N("removing %s failed: %s", $cachefile, $!));
250 }
251 }
252 }
253 ++$update;
254 }
255 \@produced_deltas, @trans_pkgs;
256 }
257  
258 sub _get_callbacks {
259 my ($urpm, $db, $trans, $options, $install, $upgrade, $have_pkgs) = @_;
260 $index = 0;
261 my $fh;
262  
263 my $is_test = $options->{test}; # fix circular reference
264 #- assume default value for some parameter.
265 $options->{delta} ||= 1000;
266  
267 #- ensure perl does not create a circular reference below, otherwise all this won't be collected,
268 # and rpmdb won't be closed:
269 my ($callback_open_helper, $callback_close_helper) = ($options->{callback_open_helper}, $options->{callback_close_helper});
270 $options->{callback_open} = sub {
271 my ($_data, $_type, $id) = @_;
272 $callback_open_helper and $callback_open_helper->(@_);
273 $fh = urpm::sys::open_safe($urpm, '<', $install->{$id} || $upgrade->{$id});
274 $fh ? fileno $fh : undef;
275 };
276 $options->{callback_close} = sub {
277 my ($urpm, undef, $pkgid) = @_;
278 return unless defined $pkgid;
279 $callback_close_helper and $callback_close_helper->($db, @_);
280 get_README_files($urpm, $trans, $urpm->{depslist}[$pkgid]) if !$is_test;
281 close $fh if defined $fh;
282 };
283  
284 #- ensure perl does not create a circular reference below, otherwise all this won't be collected,
285 # and rpmdb won't be closed
286 my $verbose = $options->{verbose};
287 $erase_logger = sub {
288 my ($urpm, undef, undef, $subtype) = @_;
289  
290 if ($subtype eq 'start') {
291 my $name = $trans->Element_name($index);
292 my @previous = map { $trans->Element_name($_) } 0 .. ($index - 1);
293 # looking at previous packages in transaction
294 # we should be looking only at installed packages, but it should not give a different result
295 if (member($name, @previous)) {
296 $urpm->{log}("removing upgraded package $current_pkg");
297 } else {
298 $urpm->{print}(N("removing package %s", $current_pkg)) if $verbose >= 0;
299 }
300 }
301 };
302  
303 $options->{callback_uninst} ||= $options->{verbose} >= 0 ? \&install_logger : $erase_logger;
304  
305 $options->{callback_elem} ||= sub {
306 my (undef, undef, undef, undef, $idx, undef) = @_;
307 $index = $idx;
308 $current_pkg = $trans->Element_fullname($idx);
309 };
310 $options->{callback_error} ||= sub {
311 my ($urpm, undef, undef, $subtype, undef, undef) = @_;
312 $urpm->{error}("ERROR: '$subtype' failed for $current_pkg");
313 };
314  
315 if ($options->{verbose} >= 0 && $have_pkgs) {
316 $options->{callback_inst} ||= \&install_logger;
317 $options->{callback_trans} ||= \&install_logger;
318 }
319 }
320  
321 =item install($urpm, $remove, $install, $upgrade, %options)
322  
323 Install packages according to each hash (remove, install or upgrade).
324  
325 options:
326 test, excludepath, nodeps, noorder (unused), delta,
327 callback_inst, callback_trans, callback_uninst,
328 callback_open_helper, callback_close_helper,
329 post_clean_cache, verbose
330 (more options for trans->run)
331 excludedocs, nosize, noscripts, oldpackage, replacepkgs, justdb, ignorearch
332  
333 See L<URPM> for callback parameters
334  
335 There's 2 callbacks that are specific to C<urpm::install> though:
336  
337 =over
338  
339 =item * open_helper($data, $type, $id)
340  
341 =item * close_helper($db, $data, $type, $id)
342  
343 =back
344  
345 Those are called when opening/closing a package file, whether when rpmlib verify or install packages.
346  
347 The C<close_helper> callback receives an extra $db parameter used in eg: drakx to check whether a package really was installed through a DB query.
348  
349 =cut
350  
351 #- side-effects: uses a $urpm->{readmes}
352 sub install {
353 my ($urpm, $remove, $install, $upgrade, %options) = @_;
354 $options{translate_message} = 1;
355  
356 my $db = urpm::db_open_or_die_($urpm, !$options{test}); #- open in read/write mode unless testing installation.
357  
358 my $trans = $db->create_transaction;
359 if ($trans) {
360 my ($rm_count, $inst_count, $up_count) = (scalar(@{$remove || []}), scalar(values %$install), scalar(values %$upgrade));
361 sys_log("transaction on %s (remove=%d, install=%d, upgrade=%d)", $urpm->{root} || '/', $rm_count, $inst_count, $up_count);
362 $urpm->{log}(N("created transaction for installing on %s (remove=%d, install=%d, upgrade=%d)", $urpm->{root} || '/',
363 $rm_count, $inst_count, $up_count));
364 } else {
365 return N("unable to create transaction");
366 }
367  
368 $trans->set_script_fd($options{script_fd}) if $options{script_fd};
369  
370 my @errors;
371  
372 _schedule_packages_for_erasing($urpm, $trans, $remove);
373  
374 my ($produced_deltas, @trans_pkgs) = _schedule_packages($urpm, $trans, $install, $upgrade, %options);
375  
376 if (!$options{nodeps} && (@errors = $trans->check(%options))) {
377 } elsif (!$options{noorder} && (@errors = $trans->order(%options))) {
378 } else {
379 $urpm->{readmes} = {};
380 $rpm_version ||= urpm::select::_rpm_version();
381  
382 _get_callbacks($urpm, $db, $trans, \%options, $install, $upgrade, scalar @trans_pkgs);
383  
384 local $ENV{LD_PRELOAD}; # fix eatmydata & co
385 local $urpm->{trans} = $trans;
386 @errors = $trans->run($urpm, %options);
387 delete $urpm->{trans};
388 undef $erase_logger;
389  
390 #- don't clear cache if transaction failed. We might want to retry.
391 if (!@errors && !$options{test} && $options{post_clean_cache}) {
392 #- examine the local cache to delete packages which were part of this transaction
393 my $cachedir = "$urpm->{cachedir}/rpms";
394 my @pkgs = grep { -e "$cachedir/$_" } map { $_->filename } @trans_pkgs;
395 $urpm->{log}(N("removing installed rpms (%s) from %s", join(' ', @pkgs), $cachedir)) if @pkgs;
396 foreach (@pkgs) {
397 unlink "$cachedir/$_" or $urpm->{fatal}(1, N("removing %s failed: %s", $_, $!));
398 }
399 }
400  
401 if ($options{verbose} >= 0 && !$options{justdb}) {
402 foreach (keys %{$urpm->{readmes}}) {
403 $urpm->{print}("-" x 70 . "\n" .
404 N("More information on package %s", $urpm->{readmes}{$_}));
405 $urpm->{print}(scalar cat_utf8(($urpm->{root} || '') . $_));
406 $urpm->{print}("-" x 70);
407 }
408 }
409 }
410  
411 unlink @$produced_deltas;
412  
413 urpm::sys::may_clean_rpmdb_shared_regions($urpm, $options{test});
414  
415 # explicitely close the RPM DB (needed for drakx -- looks like refcount has hard work):
416 undef $db;
417 undef $trans;
418  
419 @errors;
420 }
421  
422 1;
423  
424 =back
425  
426 =head1 COPYRIGHT
427  
428 Copyright (C) 1999-2005 MandrakeSoft SA
429  
430 Copyright (C) 2005-2010 Mandriva SA
431  
432 Copyright (C) 2011-2020 Mageia
433  
434 =cut
« urpm/get_pkgs.pm ↑ Summary urpm/lock.pm »