urpm.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« urpm/parallel_ssh.pm ↑ Summary urpm/cfg.pm »
1 package urpm;
2  
3  
4 1 ▶no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use urpm::msg;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use urpm::download;
9 use urpm::util qw(basename begins_with cat_ cat_utf8 dirname file2absolute_file member);
10 use urpm::sys;
11 use urpm::cfg;
12 use urpm::md5sum;
13 # perl_checker: require urpm::args
14 # perl_checker: require urpm::media
15 # perl_checker: require urpm::parallel
16  
17 our $VERSION = 'v8.133.1';
18 our @ISA = qw(URPM Exporter);
19 our @EXPORT_OK = ('file_from_local_url', 'file_from_local_medium', 'is_local_medium');
20  
21 # Prepare exit code. If you change this, the exiting with a failure and the message given will be postponed to the end of the overall processing.
22 our $postponed_msg = N("While some packages may have been installed, there were failures.\n");
23 our $postponed_code = 0;
24  
25 use URPM;
26 use URPM::Resolve;
27  
28  
29 =head1 NAME
30  
31 urpm - Mageia perl tools to handle the urpmi database
32  
33 =head1 DESCRIPTION
34  
35 C<urpm> is used by urpmi executables to manipulate packages and media
36 on a Mageia Linux distribution.
37  
38 It rely on L<URPM> for the lower level librpm bindings.
39  
40 =head2 The urpm class
41  
42 =over 4
43  
44 =cut
45  
46 #- this violently overrides is_arch_compat() to always return true.
47 sub shunt_ignorearch {
48 eval q( sub URPM::Package::is_arch_compat { 1 } );
49 }
50  
51 sub xml_info_policies() { qw(never on-demand update-only always) }
52  
53 sub default_options {
54 {
55 'split-level' => 1,
56 'split-length' => 50,
57 'verify-rpm' => 1,
58 'post-clean' => 1,
59 'xml-info' => 'on-demand',
60 'max-round-robin-tries' => 5,
61 'max-round-robin-probes' => 2,
62 'days-between-mirrorlist-update' => 5,
63 'nb-of-new-unrequested-pkgs-between-auto-select-orphans-check' => 10,
64 };
65 }
66  
67 =item urpm->new()
68  
69 The constructor creates a new urpm object. It's a blessed hash that
70 contains fields from L<URPM>, and also the following fields:
71  
72 B<source>: { id => src_rpm_file|spec_file }
73  
74 B<media>: [ {
75 start => int, end => int, name => string, url => string,
76 virtual => bool, media_info_dir => string, with_synthesis => string,
77 no-media-info => bool,
78 iso => string, downloader => string,
79 ignore => bool, update => bool, modified => bool, really_modified => bool,
80 unknown_media_info => bool,
81 } ],
82  
83 B<options>: hashref of urpm options
84  
85 several paths:
86  
87 =over
88  
89 B<config>: path of urpmi.cfg (/etc/urpmi/urpmi.cfg)
90  
91 B<mediacfgdir>: path of mediacfg.d (/etc/urpmi/mediacfg.d)
92  
93 B<skiplist>: path of skip.list (/etc/urpmi/skip.list),
94  
95 B<instlist>: path of inst.list (/etc/urpmi/inst.list),
96  
97 B<prefer_list>: path of prefer.list (/etc/urpmi/prefer.list),
98  
99 B<prefer_vendor_list>: path of prefer.vendor.list (/etc/urpmi/prefer.vendor.list),
100  
101 B<private_netrc>: path of netrc (/etc/urpmi/netrc),
102  
103 B<statedir>: state directory (/var/lib/urpmi),
104  
105 B<cachedir>: cache directory (/var/cache/urpmi),
106  
107 B<root>: path of the rooted system (when using global urpmi config),
108  
109 B<urpmi_root>: path of the rooted system (when both urpmi & rpmdb are chrooted)
110  
111 =back
112  
113 Several subs:
114  
115 =over
116  
117 B<fatal>: sub for relaying fatal errors (should popup in GUIes)
118  
119 B<error>: sub for relaying other errors
120  
121 B<log>: sub for relaying messages if --verbose
122  
123 B<print>: sub for always displayed messages, enable to redirect output for eg: installer
124  
125 B<info>: sub for messages displayed unless --quiet
126  
127 =back
128  
129 All C<URPM> methods are available on an urpm object.
130  
131 =cut
132  
133 sub new {
134 my ($class) = @_;
135 my $self;
136 $self = bless {
137 # from URPM
138 depslist => [],
139 provides => {},
140 obsoletes => {},
141  
142 media => undef,
143 options => {},
144  
145 fatal => sub { printf STDERR "%s\n", $_[1]; exit($_[0]) },
146 error => sub { printf STDERR "%s\n", $_[0] },
147 info => sub { printf "%s\n", $_[0] }, #- displayed unless --quiet
148 log => sub { printf "%s\n", $_[0] }, #- displayed if --verbose
149 print => sub { printf "%s\n", $_[0] }, #- always displayed, enable to redirect output for eg: installer
150 }, $class;
151  
152 set_files($self, '');
153 $self->set_nofatal(1);
154 $self;
155 }
156  
157 =item urpm->new_parse_cmdline()
158  
159 Like urpm->new but also parse the command line and parse the configuration file.
160  
161 =cut
162  
163 sub new_parse_cmdline {
164 my ($class) = @_;
165 my $urpm = $class->new;
166 urpm::args::parse_cmdline(urpm => $urpm);
167 get_global_options($urpm);
168 $urpm;
169 }
170  
171 sub _add2hash { my ($a, $b) = @_; while (my ($k, $v) = each %{$b || {}}) { defined $a->{$k} or $a->{$k} = $v } $a }
172  
173 sub get_global_options {
174 my ($urpm) = @_;
175  
176 my $config = urpm::cfg::load_config($urpm->{config})
177 or $urpm->{fatal}(6, $urpm::cfg::err);
178  
179 if (my $global = $config->{global}) {
180 _add2hash($urpm->{options}, $global);
181 }
182 #- remember global options for write_config
183 $urpm->{global_config} = $config->{global};
184  
185 _add2hash($urpm->{options}, default_options());
186 }
187  
188 sub prefer_rooted {
189 my ($root, $file) = @_;
190 -e "$root$file" ? "$root$file" : $file;
191 }
192  
193 sub check_dir {
194 my ($urpm, $dir) = @_;
195 -d $dir && ! -l $dir or $urpm->{fatal}(1, N("fail to create directory %s", $dir));
196 -o $dir && -w $dir or $urpm->{fatal}(1, N("invalid owner for directory %s", $dir));
197 }
198  
199 sub init_dir {
200 my ($urpm, $dir) = @_;
201  
202 mkdir $dir, 0755; # try to create it
203  
204 check_dir($urpm, $dir);
205  
206 mkdir "$dir/partial";
207 mkdir "$dir/rpms";
208  
209 $dir;
210 }
211  
212 sub userdir_prefix {
213 my ($_urpm) = @_;
214 '/tmp/.urpmi-';
215 }
216  
217 sub valid_statedir {
218 my ($urpm) = @_;
219 $< or return;
220  
221 my $dir = ($urpm->{urpmi_root} || '') . userdir_prefix($urpm) . $< . "/lib";
222 init_dir($urpm, $dir);
223 }
224  
225 sub userdir {
226 #mdkonline uses userdir because it runs as user
227 my ($urpm) = @_;
228 $< or return;
229  
230 my $dir = ($urpm->{urpmi_root} || '') . userdir_prefix($urpm) . $<;
231 init_dir($urpm, $dir);
232 }
233  
234 sub ensure_valid_cachedir {
235 my ($urpm) = @_;
236 if (my $dir = userdir($urpm)) {
237 $urpm->{cachedir} = $dir;
238 }
239 -w "$urpm->{cachedir}/partial" or $urpm->{fatal}(1, N("Can not download packages into %s", "$urpm->{cachedir}/partial"));
240 }
241  
242 sub valid_cachedir {
243 my ($urpm) = @_;
244 userdir($urpm) || $urpm->{cachedir};
245 }
246  
247 sub is_temporary_file {
248 my ($urpm, $f) = @_;
249  
250 begins_with($f, $urpm->{cachedir});
251 }
252  
253 sub set_env {
254 my ($urpm, $env) = @_;
255 -d $env or $urpm->{fatal}(8, N("Environment directory %s does not exist", $env));
256 print N("using specific environment on %s\n", $env);
257 #- setting new environment.
258 $urpm->{config} = "$env/urpmi.cfg";
259 if (cat_($urpm->{config}) =~ /^\s*virtual\s*$/m) {
260 print "dropping virtual from $urpm->{config}\n";
261 system(q(perl -pi -e 's/^\s*virtual\s*$//' ) . $urpm->{config});
262 }
263 $urpm->{mediacfgdir} = "$env/mediacfg.d";
264 $urpm->{skiplist} = "$env/skip.list";
265 $urpm->{instlist} = "$env/inst.list";
266 $urpm->{prefer_list} = "$env/prefer.list";
267 $urpm->{prefer_vendor_list} = "$env/prefer.vendor.list";
268 $urpm->{statedir} = $env;
269 $urpm->{env_rpmdb} = "$env/rpmdb.cz";
270 $urpm->{env_dir} = $env;
271 }
272  
273 sub set_files {
274 my ($urpm, $urpmi_root) = @_;
275  
276 $urpmi_root and $urpmi_root = file2absolute_file($urpmi_root);
277  
278 my %h = (
279 config => "$urpmi_root/etc/urpmi/urpmi.cfg",
280 mediacfgdir => "$urpmi_root/etc/urpmi/mediacfg.d",
281 skiplist => prefer_rooted($urpmi_root, '/etc/urpmi/skip.list'),
282 instlist => prefer_rooted($urpmi_root, '/etc/urpmi/inst.list'),
283 prefer_list => prefer_rooted($urpmi_root, '/etc/urpmi/prefer.list'),
284 prefer_vendor_list =>
285 prefer_rooted($urpmi_root, '/etc/urpmi/prefer.vendor.list'),
286 private_netrc => "$urpmi_root/etc/urpmi/netrc",
287 statedir => "$urpmi_root/var/lib/urpmi",
288 cachedir => "$urpmi_root/var/cache/urpmi",
289 root => $urpmi_root,
290 $urpmi_root ? (urpmi_root => $urpmi_root) : @{[]},
291 );
292 $urpm->{$_} = $h{$_} foreach keys %h;
293  
294 create_var_lib_rpm($urpm, %h);
295  
296 # policy is too use chroot environment only for --urpmi-root, not for --root:
297 if ($urpmi_root && -e "$urpmi_root/etc/rpm/macros") {
298 URPM::loadmacrosfile("$urpmi_root/etc/rpm/macros");
299 }
300 }
301  
302 sub create_var_lib_rpm {
303 my ($urpm, %h) = @_;
304 require File::Path;
305 File::Path::mkpath([ $h{statedir},
306 (map { "$h{cachedir}/$_" } qw(partial rpms)),
307 dirname($h{config}),
308 "$urpm->{root}/var/lib/rpm",
309 "$urpm->{root}/var/tmp",
310 ]);
311 }
312  
313 sub modify_rpm_macro {
314 my ($name, $to_remove, $to_add) = @_;
315  
316 my $val = URPM::expand('%' . $name);
317 $val =~ s/$to_remove/$to_add/ or $val = join(' ', grep { $_ } $val, $to_add);
318 URPM::add_macro("$name $val");
319 }
320  
321 sub set_tune_rpm {
322 my ($urpm, $para) = @_;
323  
324 my %h = map { $_ => 1 } map {
325 if ($_ eq 'all') {
326 ('nofsync', 'private');
327 } else {
328 $_;
329 }
330 } split(',', $para);
331  
332 $urpm->{tune_rpm} = \%h;
333 }
334  
335 sub tune_rpm {
336 my ($urpm) = @_;
337  
338 if ($urpm->{tune_rpm}{nofsync}) {
339 modify_rpm_macro('__dbi_other', 'fsync', 'nofsync');
340 }
341 if ($urpm->{tune_rpm}{private}) {
342 urpm::sys::clean_rpmdb_shared_regions($urpm->{root});
343 modify_rpm_macro('__dbi_other', 'usedbenv', 'private');
344 }
345 }
346  
347 sub _blist_pkg_to_urls {
348 my ($blist, @pkgs) = @_;
349 my $base_url = $blist->{medium}{url} . '/';
350 map { $base_url . $_->filename } @pkgs;
351 }
352 sub blist_pkg_to_url {
353 my ($blist, $pkg) = @_;
354 my ($url) = _blist_pkg_to_urls($blist, $pkg);
355 $url;
356 }
357 sub blist_to_urls {
358 my ($blist) = @_;
359 _blist_pkg_to_urls($blist, values %{$blist->{pkgs}});
360 }
361 sub blist_to_filenames {
362 my ($blist) = @_;
363 map { $_->filename } values %{$blist->{pkgs}};
364 }
365  
366 sub protocol_from_url {
367 my ($url) = @_;
368 $url =~ m!^(\w+)(_[^:]*)?:! && $1;
369 }
370 sub file_from_local_url {
371 my ($url) = @_;
372 $url =~ m!^(?:removable[^:]*:/|file:/)?(/.*)! && $1;
373 }
374 sub file_from_local_medium {
375 my ($medium, $o_url) = @_;
376 my $url = $o_url || $medium->{url};
377 if ($url =~ m!^cdrom://(.*)!) {
378 my $rel = $1;
379 $medium->{mntpoint} or do { require Carp; Carp::confess("cdrom is not mounted yet!\n") };
380 "$medium->{mntpoint}/$rel";
381 } else {
382 file_from_local_url($url);
383 }
384 }
385 sub is_local_url {
386 my ($url) = @_;
387 file_from_local_url($url) || is_cdrom_url($url);
388 }
389 sub is_local_medium {
390 my ($medium) = @_;
391 is_local_url($medium->{url});
392 }
393 sub is_cdrom_url {
394 my ($url) = @_;
395 protocol_from_url($url) eq 'cdrom';
396 }
397  
398 =item db_open_or_die($urpm, $b_write_perm)
399  
400 Open RPM database (RW or not) and die if it fails
401  
402 =cut
403  
404 sub db_open_or_die_ {
405 my ($urpm, $b_write_perm) = @_;
406 my $db;
407 if ($urpm->{env_rpmdb}) {
408 #- URPM has same methods as URPM::DB and empty URPM will be seen as empty URPM::DB.
409 $db = URPM->new;
410 $db->parse_synthesis($urpm->{env_rpmdb});
411 } else {
412 $db = db_open_or_die($urpm, $urpm->{root}, $b_write_perm);
413 }
414 $db;
415 }
416  
417 # please use higher level function db_open_or_die_()
418 sub db_open_or_die {
419 my ($urpm, $root, $b_write_perm) = @_;
420  
421 $urpm->{debug} and $urpm->{debug}("opening rpmdb (root=$root, write=$b_write_perm)");
422  
423 my $db = URPM::DB::open($root, $b_write_perm || 0)
424 or $urpm->{fatal}(9, N("unable to open rpmdb"));
425  
426 $db;
427 }
428  
429 =item register_rpms($urpm, @files)
430  
431 Register local packages for being installed, keep track of source.
432  
433 =cut
434  
435 sub register_rpms {
436 my ($urpm, @files) = @_;
437 my ($start, $id, $error, %requested);
438  
439 #- examine each rpm and build the depslist for them using current
440 #- depslist and provides environment.
441 $start = @{$urpm->{depslist}};
442 foreach (@files) {
443 /\.(?:rpm|spec)$/ or $error = 1, $urpm->{error}(N("invalid rpm file name [%s]", $_)), next;
444  
445 #- if that's an URL, download.
446 if (protocol_from_url($_)) {
447 my $basename = basename($_);
448 unlink "$urpm->{cachedir}/partial/$basename";
449 $urpm->{log}(N("retrieving rpm file [%s] ...", $_));
450 if (urpm::download::sync_url($urpm, $_, quiet => 1)) {
451 $urpm->{log}(N("...retrieving done"));
452 $_ = "$urpm->{cachedir}/partial/$basename";
453 } else {
454 $urpm->{error}(N("...retrieving failed: %s", $@));
455 unlink "$urpm->{cachedir}/partial/$basename";
456 next;
457 }
458 } else {
459 -r $_ or $error = 1, $urpm->{error}(N("unable to access rpm file [%s]", $_)), next;
460 }
461  
462 if (/\.spec$/) {
463 my $pkg = URPM::spec2srcheader($_)
464 or $error = 1, $urpm->{error}(N("unable to parse spec file %s [%s]", $_, $!)), next;
465 $id = @{$urpm->{depslist}};
466 $urpm->{depslist}[$id] = $pkg;
467 $pkg->set_id($id); #- sets internal id to the depslist id.
468 $urpm->{source}{$id} = $_;
469 } else {
470 ($id) = $urpm->parse_rpm($_);
471 my $pkg = defined $id && $urpm->{depslist}[$id];
472 $pkg or $error = 1, $urpm->{error}(N("unable to register rpm file")), next;
473 $pkg->arch eq 'src' || $pkg->is_arch_compat
474 or $error = 1, $urpm->{error}(N("Incompatible architecture for rpm [%s]", $_)), next;
475 $urpm->{source}{$id} = $_;
476 }
477 }
478 $error and $urpm->{fatal}(2, N("error registering local packages"));
479 defined $id && $start <= $id and @requested{($start .. $id)} = (1) x ($id-$start+1);
480  
481 #- distribute local packages to distant nodes directly in cache of each machine.
482 if (@files && $urpm->{parallel_handler}) {
483 $urpm->{parallel_handler}->parallel_register_rpms($urpm, @files);
484 }
485  
486 %requested;
487 }
488  
489 =item is_delta_installable($urpm, $pkg, $root)
490  
491 checks whether the delta RPM represented by $pkg is installable wrt the
492 RPM DB on $root. For this, it extracts the rpm version to which the
493 delta applies from the delta rpm filename itself. So naming conventions
494 do matter :)
495  
496 =cut
497  
498 sub is_delta_installable {
499 my ($urpm, $pkg, $root) = @_;
500 $pkg->flag_installed or return 0;
501 my $f = $pkg->filename;
502 my $n = $pkg->name;
503 my ($v_match) = $f =~ /^\Q$n\E-(.*)_.+\.delta\.rpm$/;
504 my $db = db_open_or_die($urpm, $root);
505 my $v_installed;
506 $db->traverse(sub {
507 my ($p) = @_;
508 $p->name eq $n and $v_installed = $p->version . '-' . $p->release;
509 });
510 $v_match eq $v_installed;
511 }
512  
513  
514 =item extract_packages_to_install($urpm, $sources)
515  
516 Extract package that should be installed instead of upgraded,
517 installing instead of upgrading is useful
518 - for inst.list (cf flag disable_obsolete)
519  
520 Sources is a hash of id -> source rpm filename.
521  
522 =cut
523  
524 sub extract_packages_to_install {
525 my ($urpm, $sources) = @_;
526 my %inst;
527  
528 foreach (keys %$sources) {
529 my $pkg = $urpm->{depslist}[$_] or next;
530 $pkg->flag_disable_obsolete
531 and $inst{$pkg->id} = delete $sources->{$pkg->id};
532 }
533  
534 \%inst;
535 }
536  
537 #- deprecated, use find_candidate_packages_() directly
538 #-
539 #- side-effects: none
540 sub find_candidate_packages_ {
541 my ($urpm, $id_prop) = @_;
542  
543 my %packages;
544 foreach ($urpm->find_candidate_packages($id_prop)) {
545 push @{$packages{$_->name}}, $_;
546 }
547 values %packages;
548 }
549  
550 =item get_updates_description($urpm, @update_medias)
551  
552 Get reason of update for packages to be updated.
553 Use all update medias if none given.
554  
555 =cut
556  
557 sub get_updates_description {
558 my ($urpm, @update_medias) = @_;
559 my %update_descr;
560 my ($cur, $section);
561  
562 @update_medias or @update_medias = urpm::media::non_ignored_media($urpm, 'update');
563  
564 foreach my $medium (@update_medias) {
565 # fix not taking into account the last %package token of each descrptions file: '%package dummy'
566 foreach (cat_utf8(urpm::media::statedir_descriptions($urpm, $medium)),
567 ($::env ? cat_utf8("$::env/descriptions") : ()), '%package dummy') {
568 /^%package +(.+)/ and do {
569 # fixes not parsing descriptions file when MU adds itself the security source:
570 if (exists $cur->{importance} && !member($cur->{importance}, qw(security bugfix))) {
571 $cur->{importance} = 'normal';
572 }
573 $update_descr{$medium->{name}}{$_} = $cur foreach @{$cur->{pkgs} || []};
574 $cur = { pkgs => [ split /\s/, $1 ] };
575 $section = 'pkg';
576 next;
577 };
578 /^Updated?: +(.+)/ && $section eq 'pkg' and do { $cur->{updated} = $1; next };
579 /^Importance: +(.+)/ && $section eq 'pkg' and do { $cur->{importance} = $1; next };
580 /^(ID|URL): +(.+)/ && $section eq 'pkg' and do { $cur->{$1} = $2; next };
581 /^%(pre|description)/ and do { $section = $1; next };
582 $section =~ /^(pre|description)\z/ and $cur->{$1} .= $_;
583 }
584 }
585 \%update_descr;
586 }
587  
588 sub error_restricted ($) {
589 my ($urpm) = @_;
590 $urpm->{fatal}(2, N("This operation is forbidden while running in restricted mode"));
591 }
592  
593 sub DESTROY {}
594  
595 1;
596  
597  
598 =back
599  
600 =head1 SEE ALSO
601  
602 The L<URPM> package is used to manipulate at a lower level synthesis and rpm
603 files.
604  
605 The L<rpmtools> package provides commands to generate synthesis and hdlist
606 files (as well as other metadata): L<genhdlist2> and L<gendistrib> (a
607 gendhlist2 wrapper handling a whole media set) from L<rpmtools> package.
608  
609 See also submodules: L<gurpmi>, L<urpm::args>, L<urpm::bug_report>,
610 L<urpm::cdrom>, L<urpm::cfg>, L<urpm::download>, L<urpm::get_pkgs>,
611 L<urpm::install>, L<urpm::ldap>, L<urpm::lock>, L<urpm::main_loop>,
612 L<urpm::md5sum>, L<urpm::media>, L<urpm::mirrors>, L<urpm::msg>,
613 L<urpm::orphans>, L<urpm::parallel_ka_run>, L<urpm::parallel>,
614 L<urpm::parallel_ssh>, L<urpm::prompt>, L<urpm::removable>,
615 L<urpm::select>, L<urpm::signature>, L<urpm::sys>, L<urpm::util>,
616 L<urpm::xml_info_pkg>, L<urpm::xml_info>
617  
618 =head1 COPYRIGHT
619  
620 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA
621  
622 Copyright (C) 2005-2010 Mandriva SA
623  
624 Copyright (C) 2011-2020 Mageia
625  
626 This program is free software; you can redistribute it and/or modify
627 it under the terms of the GNU General Public License as published by
628 the Free Software Foundation; either version 2, or (at your option)
629 any later version.
630  
631 This program is distributed in the hope that it will be useful,
632 but WITHOUT ANY WARRANTY; without even the implied warranty of
633 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
634 GNU General Public License for more details.
635  
636 You should have received a copy of the GNU General Public License
637 along with this program; if not, write to the Free Software
638 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
639  
640 =cut
641  
642 # ex: set ts=8 sts=4 sw=4 noet:
« urpm/parallel_ssh.pm ↑ Summary urpm/cfg.pm »