urpm/sys.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/msg.pm ↑ Summary urpm/args.pm »
1 package urpm::sys;
2  
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use urpm::util 'cat_';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use urpm::msg;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use POSIX ();
9  
10  
11 =head1 NAME
12  
13 urpm::sys - OS-related routines for urpmi
14  
15 =head1 SYNOPSIS
16  
17 =head1 DESCRIPTION
18  
19 =over
20  
21 =cut
22  
23 =item get_packages_list($file, $o_extra)
24  
25 Get the list of packages that should not be upgraded or installed,
26 typically from the inst.list or skip.list files.
27  
28 =cut
29  
30 sub get_packages_list {
31 my ($file, $o_extra) = @_;
32 my @l = split(/,/, $o_extra || '');
33 push @l, cat_($file);
34 [ grep { $_ } map {
35 chomp; s/#.*$//; s/^\s*//; s/\s*$//;
36 $_;
37 } @l ];
38 }
39  
40 sub _read_fstab_or_mtab {
41 my ($file) = @_;
42  
43 my @l;
44 foreach (cat_($file)) {
45 next if /^\s*#/;
46 my ($device, $mntpoint, $fstype, $_options) = m!^\s*(\S+)\s+(/\S+)\s+(\S+)\s+(\S+)!
47 or next;
48 $mntpoint =~ s,/+,/,g; $mntpoint =~ s,/$,,;
49 push @l, { mntpoint => $mntpoint, device => $device, fs => $fstype };
50 }
51 @l;
52 }
53  
54 =item find_a_mntpoint($dir)
55  
56 Find used mount point from a pathname
57  
58 =cut
59  
60 sub find_a_mntpoint {
61 my ($dir) = @_;
62 _find_a_mntpoint($dir, {});
63 }
64  
65 sub read_mtab() { _read_fstab_or_mtab('/etc/mtab') }
66  
67 #- find used mount point from a pathname
68 sub _find_a_mntpoint {
69 my ($dir, $infos) = @_;
70  
71 #- read /etc/fstab and check for existing mount point.
72 foreach (_read_fstab_or_mtab("/etc/fstab")) {
73 $infos->{$_->{mntpoint}} = { mounted => 0, %$_ };
74 }
75 foreach (read_mtab()) {
76 $infos->{$_->{mntpoint}} = { mounted => 1, %$_ };
77 }
78  
79 #- try to follow symlink, too complex symlink graph may not be seen.
80 #- check the possible mount point.
81 my @paths = split '/', $dir;
82 my $pdir = '';
83 while (@paths) {
84 my $path = shift @paths;
85 length($path) or next;
86 $pdir .= "/$path";
87 $pdir =~ s,/+,/,g; $pdir =~ s,/$,,;
88 if (exists($infos->{$pdir})) {
89 #- following symlinks may be useless or dangerous for supermounted devices.
90 #- this means it is assumed no symlink inside a removable device
91 #- will go outside the device itself (or at least will go into
92 #- regular already mounted device like /).
93 #- for simplification we refuse also any other device and stop here.
94 return $infos->{$pdir};
95 } elsif (-l $pdir) {
96 unshift @paths, split '/', _expand_symlink($pdir);
97 $pdir = '';
98 }
99 }
100 undef;
101 }
102  
103 =item df($mntpoint)
104  
105 Return the size of the partition and its free space in KiB
106  
107 =cut
108  
109 sub df {
110 my ($mntpoint) = @_;
111 require Filesys::Df;
112 my $df = Filesys::Df::df($mntpoint || "/", 1024); # ask 1kb values
113 @$df{qw(blocks bfree)};
114 }
115  
116 sub _expand_symlink {
117 my ($pdir) = @_;
118  
119 while (my $v = readlink $pdir) {
120 if ($pdir =~ m|^/|) {
121 $pdir = $v;
122 } else {
123 while ($v =~ s!^\.\./!!) {
124 $pdir =~ s!/[^/]+/*$!!;
125 }
126 $pdir .= "/$v";
127 }
128 }
129 $pdir;
130 }
131  
132 sub whereis_binary {
133 my ($prog, $o_prefix) = @_;
134 if ($prog =~ m!/!) {
135 warn qq(don't call whereis_binary with a name containing a "/" (the culprit is: $prog)\n);
136 return;
137 }
138 my $prefix = $o_prefix || '';
139 foreach (split(':', $ENV{PATH})) {
140 my $f = "$_/$prog";
141 -x "$prefix$f" and return $f;
142 }
143 }
144  
145 sub may_clean_rpmdb_shared_regions {
146 my ($urpm, $test) = @_;
147  
148 if ($urpm->{root} && !$test || $urpm->{tune_rpm}{private}) {
149 $urpm->{root} && $urpm->{debug} and $urpm->{debug}("workaround bug in rpmlib by removing $urpm->{root}/var/lib/rpm/__db*");
150 clean_rpmdb_shared_regions($urpm->{root});
151 }
152 }
153  
154 sub clean_rpmdb_shared_regions {
155 my ($prefix) = @_;
156 unlink glob("$prefix/var/lib/rpm/__db.*");
157 }
158  
159 sub proc_mounts() {
160 my @l = cat_('/proc/mounts') or warn "Can't read /proc/mounts: $!\n";
161 @l;
162 }
163  
164 sub proc_self_mountinfo() {
165 my @l = cat_('/proc/self/mountinfo') or warn "Can't read /proc/self/mountinfo: $!\n";
166 @l;
167 }
168  
169 sub trim_until_d {
170 my ($dir) = @_;
171 foreach (proc_mounts()) {
172 #- fail if an iso is already mounted
173 m!^/dev/loop! and return $dir;
174 }
175 while ($dir && !-d $dir) { $dir =~ s,/[^/]*$,, }
176 $dir;
177 }
178  
179 =item check_fs_writable()
180  
181 Checks if the main filesystems are writable for urpmi to install files in
182  
183 =cut
184  
185 sub check_fs_writable () {
186 foreach (proc_self_mountinfo()) {
187 (undef, undef, undef, undef, our $mountpoint, my $opts) = split ' ';
188 if ($opts =~ /(?:^|,)ro(?:,|$)/ && $mountpoint =~ m!^(/|/usr|/s?bin)\z!) {
189 return 0;
190 }
191 }
192 1;
193 }
194  
195 sub _launched_time {
196 my ($component) = @_;
197  
198 if ($component eq N_("system")) {
199 my ($uptime) = cat_('/proc/uptime') =~ /(\S+)/;
200 time() - $uptime;
201 } else {
202 1; # TODO
203 }
204 }
205  
206 sub need_restart {
207 my ($root) = @_;
208 my $rpm_qf = '%{name} %{installtime} [%{provides}:%{Provideversion} ]\n';
209 my $options = ($root ? "--root $root " : '') . "-q --whatprovides should-restart --qf '$rpm_qf'";
210 open(my $F, "rpm $options | uniq |");
211  
212 my (%need_restart, %launched_time);
213 while (my $line = <$F>) {
214 my ($name, $installtime, $s) = $line =~ /(\S+)\s+(\S+)\s+(.*)/;
215
216 my @should_restart = $s =~ /should-restart:(\S+)/g;
217 foreach my $component (@should_restart) {
218 $launched_time{$component} ||= _launched_time($component);
219  
220 if ($launched_time{$component} < $installtime) {
221 push @{$need_restart{$component}}, $name;
222 }
223 }
224 }
225 %need_restart && \%need_restart;
226 }
227  
228 sub need_restart_formatted {
229 my ($root) = @_;
230 my $need_restart = need_restart($root) or return;
231  
232 foreach (keys %$need_restart) {
233 my $packages = join(', ', sort @{$need_restart->{$_}});
234 if ($_ eq 'system') {
235 $need_restart->{$_} = N("You should restart your computer for %s", $packages);
236 } elsif ($_ eq 'session') {
237 $need_restart->{$_} = N("You should restart your session for %s", $packages);
238 } else {
239 $need_restart->{$_} = N("You should restart %s for %s", translate($_), $packages);
240 }
241 }
242 $need_restart;
243 }
244  
245 # useful on command-line: perl -Murpm::sys -e 'urpm::sys::print_need_restart'
246 sub print_need_restart() {
247 my $h = need_restart_formatted('');
248 print "$_\n" foreach values %$h;
249 }
250  
251 =item migrate_back_rpmdb_db_to_4_6($urpm, $root)
252  
253 Downgrade rpmdb from external libdb-4.6.so to internal DB 4.3.27 (mdv2008.1 -> mdv2009.0)
254  
255 =cut
256  
257 sub migrate_back_rpmdb_db_to_hash_8 {
258 my ($urpm, $root) = @_;
259  
260 $urpm->{info}("migrating back the created rpm db from Hash version 9 to Hash version 8");
261  
262 foreach my $db_file (glob("$root/var/lib/rpm/[A-Z]*")) {
263 rename $db_file, "$db_file.";
264 system("db_dump $db_file. | db42_load $db_file");
265 if (-e $db_file) {
266 unlink "$db_file.";
267 } else {
268 rename "$db_file.", $db_file;
269 $urpm->{error}("rpm db migration failed on $db_file. You will not be able to run rpm chrooted");
270 return;
271 }
272 }
273 }
274  
275 =item migrate_back_rpmdb_db_to_4_6($urpm, $root)
276  
277 Downgrade rpmdb from rpm-4.9 to 4.8.1 (both linked with db4.8.30) db (eg: when installing mga1 in a chroot from mga2+, mga#4590)
278  
279 =cut
280  
281 sub migrate_back_rpmdb_db_to_4_6 {
282 my ($urpm, $root) = @_;
283 $urpm->{info}("migrating back the created rpm db from rpm-4.9 to rpm-4.6/4.8");
284 if (system('chroot', $root, 'rpm', '--rebuilddb') == 0) {
285 $urpm->{log}("rpm db downgraded successfully");
286 } else {
287 $urpm->{error}("rpm db downgrade failed. You will not be able to run rpm chrooted");
288 }
289 }
290  
291 =item migrate_back_rpmdb_db_version($urpm, $root)
292  
293 Check if we need to migrate back rpmdb to an older libdb after completing the upgrade/installation.
294  
295 =cut
296  
297 sub migrate_back_rpmdb_db_version {
298 my ($urpm, $root) = @_;
299  
300 if ($urpm->{need_migrate_rpmdb} eq '4.6') {
301 migrate_back_rpmdb_db_to_hash_8($urpm, $root);
302 } elsif ($urpm->{need_migrate_rpmdb} eq '4.8') {
303 migrate_back_rpmdb_db_to_4_6($urpm, $root);
304 }
305  
306 clean_rpmdb_shared_regions($root);
307 }
308  
309 =item migrate_rpmdb_to_sqlite($urpm)
310  
311 Migrate rpmdb to the new sqlite backend.
312  
313 =cut
314  
315 sub migrate_rpmdb_to_sqlite {
316 my ($urpm) = @_;
317 $urpm->{info}("migrating db from bdb to sqlite (rpm >= 4.16)");
318 # Whatever is the default backend:
319 URPM::add_macro('_db_backend sqlite');
320 if (system('rpm', '--rebuilddb', ($urpm->{root} ? ('--dbpath', "$urpm->{root}/var/lib/rpm") : @{[]})) == 0) {
321 $urpm->{log}("rpm db converted to sqlite successfully");
322 } else {
323 $urpm->{error}("rpm db conversion failed. You will not be able to run rpm >= 4.17");
324 }
325 }
326  
327 =item migrate_forward_rpmdb_db_version($urpm)
328  
329 Check if we need to migrate rpmdb to a new backend prior to use it
330 and do it if needed.
331  
332 =cut
333  
334 sub migrate_forward_rpmdb_db_version {
335 my ($urpm) = @_;
336  
337 if ($urpm->{need_migrate_rpmdb_now} eq '4.16') {
338 migrate_rpmdb_to_sqlite($urpm);
339 }
340 }
341  
342  
343 =item apply_delta_rpm($deltarpm, $o_dir, $o_pkg)
344  
345 Create a plain rpm from an installed rpm and a delta rpm (in the current directory)
346 Returns the new rpm filename in case of success.
347 Params :
348  
349 =over
350  
351 =item * $deltarpm : full pathname of the deltarpm
352  
353 =item * $o_dir : directory where to put the produced rpm (optional)
354  
355 =item * $o_pkg : URPM::Package object corresponding to the deltarpm (optional)
356  
357 =back
358  
359 =cut
360  
361 our $APPLYDELTARPM = '/usr/bin/applydeltarpm';
362 sub apply_delta_rpm {
363 my ($deltarpm, $o_dir, $o_pkg) = @_;
364 -x $APPLYDELTARPM or return 0;
365 -e $deltarpm or return 0;
366 my $rpm;
367 if ($o_pkg) {
368 require URPM; #- help perl_checker
369 $rpm = $o_pkg->fullname . '.rpm';
370 } else {
371 $rpm = `rpm -qp --qf '%{name}-%{version}-%{release}.%{arch}.rpm' '$deltarpm'`;
372 }
373 $rpm or return 0;
374 $rpm = $o_dir . '/' . $rpm;
375 unlink $rpm;
376 system($APPLYDELTARPM, $deltarpm, $rpm);
377 -e $rpm ? $rpm : '';
378 }
379  
380 our $tempdir_template = '/tmp/urpm.XXXXXX';
381 sub mktempdir() {
382 my $tmpdir;
383 eval { require File::Temp };
384 if ($@) {
385 #- fall back to external command (File::Temp not in perl-base)
386 $tmpdir = `mktemp -d $tempdir_template`;
387 chomp $tmpdir;
388 } else {
389 $tmpdir = File::Temp::tempdir($tempdir_template);
390 }
391 return $tmpdir;
392 }
393  
394 # temporary hack used by urpmi when restarting itself.
395 sub fix_fd_leak() {
396 opendir my $dirh, "/proc/$$/fd" or return undef;
397 my @fds = grep { /^(\d+)$/ && $1 > 2 } readdir $dirh;
398 closedir $dirh;
399 foreach (@fds) {
400 my $link = readlink("/proc/$$/fd/$_");
401 $link or next;
402 next if $link =~ m!^/(usr|dev)/! || $link !~ m!^/!;
403 POSIX::close($_);
404 }
405 }
406  
407 sub clean_dir {
408 my ($dir) = @_;
409  
410 require File::Path;
411 File::Path::rmtree([$dir]);
412 }
413  
414 sub empty_dir {
415 my ($dir) = @_;
416 clean_dir($dir);
417 mkdir $dir, 0755;
418 }
419  
420 sub syserror {
421 my ($urpm, $msg, $info) = @_;
422 $urpm->{error}("$msg [$info] [$!]");
423 }
424  
425 sub open_safe {
426 my ($urpm, $sense, $filename) = @_;
427 open my $f, $sense, $filename
428 or syserror($urpm, $sense eq '>' ? N("Can't write file") : N("Can't open file"), $filename), return undef;
429 return $f;
430 }
431  
432 sub opendir_safe {
433 my ($urpm, $dirname) = @_;
434 opendir my $d, $dirname
435 or syserror($urpm, "Can't open directory", $dirname), return undef;
436 return $d;
437 }
438  
439 sub move_or_die {
440 my ($urpm, $file, $dest) = @_;
441 urpm::util::move($file, $dest) or $urpm->{fatal}(1, N("Can't move file %s to %s", $file, $dest));
442 }
443  
444 1;
445  
446  
447 =back
448  
449 =head1 COPYRIGHT
450  
451 Copyright (C) 2005 MandrakeSoft SA
452  
453 Copyright (C) 2005-2010 Mandriva SA
454  
455 Copyright (C) 2011-2020 Mageia
456  
457 =cut
« urpm/msg.pm ↑ Summary urpm/args.pm »