urpm/util.pm

filter: Statementstmt 5.4% 9 / 164 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 5.5% 2 / 36 Podpod 17.6% 6 / 34 totaltotal 5.1% 17 / 331 CC 56 SCAR 29.2
CC
56
Cov
4%
reduce_pathname
72.0
output_safe_
56.0
add2hash_
20.0
CRAP 18.5
« urpm/prompt.pm ↑ Summary gurpm/RPMProgressDialog.pm »
1 package urpm::util;
2  
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our @ISA = 'Exporter';
7 2 our @EXPORT = qw(add2hash_
8 any
9 append_to_file
10 basename
11 begins_with
12 cat_
13 cat_utf8
14 copy_and_own
15 difference2
16 dirname
17 file2absolute_file
18 file_size
19 find
20 formatList
21 intersection
22 max
23 member
24 min
25 offset_pathname
26 output_safe
27 partition
28 put_in_hash
29 quotespace
30 reduce_pathname
31 same_size_and_mtime
32 uefi_type
33 uniq
34 uniq_
35 unquotespace
36 untaint
37 wc_l
38 );
39  
40  
41 =head1 NAME
42  
43 urpm::util - Misc. utilities subs for urpmi
44  
45 Mostly a subset of L<MDK::Common>
46  
47 See C<perldoc MDK::Common> for more documentation
48  
49 =head1 SYNOPSIS
50  
51 =head1 DESCRIPTION
52  
53 =head1 FUNCTIONS
54  
55 =over
56  
57 =cut
58  
59 0 ▶sub min { my $n = shift; $_ < $n and $n = $_ foreach @_; $n }
Subroutine min: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_ < $n and $n = $_ 0 0 0
60 0 ▶sub max { my $n = shift; $_ > $n and $n = $_ foreach @_; $n }
Subroutine max: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_ > $n and $n = $_ 0 0 0
61  
62 =item quotespace($str)
63  
64 =item unquotespace($str)
65  
66 Quoting/unquoting a string that may be containing space chars.
67  
68 =cut
69  
70 0 ▶sub quotespace { my $x = $_[0] || ''; $x =~ s/(\s)/\\$1/g; $x }
Subroutine quotespace: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0] || ''
l!l
00
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
71 0 ▶sub unquotespace { my $x = $_[0] || ''; $x =~ s/\\(\s)/$1/g; $x }
Subroutine unquotespace: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0] || ''
l!l
00
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
72  
73 0 ▶sub dirname { local $_ = shift; s|[^/]*/*\s*$||; s|(.)/*$|$1|; $_ || '.' }
Subroutine dirname: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_ or "." 0 0 0
74 0 ▶sub basename { local $_ = shift; s|/*\s*$||; s|.*/||; $_ }
Subroutine basename: not called CC=1 SCAR=6.9
Pod undocumented
75  
76 0 ▶sub file2absolute_file {
Subroutine file2absolute_file: not called CC=2 SCAR=17.9
Pod undocumented
77 0 my ($f) = @_;
78  
79 0 ▶ if ($f !~ m!^/!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f =~ m[^/]) 0 0 0
80 0 require File::Spec;
81 0 $f = File::Spec->rel2abs($f);
82 }
83 0 $f;
84 }
85  
86 =item reduce_pathname($url)
87  
88 Reduce pathname by removing <something>/.. each time it appears (or . too).
89  
90 =cut
91  
92 0 ▶sub reduce_pathname {
Subroutine reduce_pathname: not called CC=8 SCAR=42.8
Pod documented
93 0 my ($url) = @_;
94  
95 #- clean url to remove any macro (which cannot be solved now).
96 #- take care if this is a true url and not a simple pathname.
97 0 my ($host, $dir) = $url =~ m|([^:/]*://[^/]*/)?(.*)|;
98 0 ▶ $host //= '';
Condition0%0/2
$host //= ""
l!l
00
MC/DC0%0/1
$host //= ""
$host
Truth table0%0/2
A
A= $host
Aresult
11
00
99  
100 #- remove any multiple /s or trailing /.
101 #- then split all components of pathname.
102 0 $dir =~ s|/+|/|g; $dir =~ s|/$||;
103 0 my @paths = split '/', $dir;
104  
105 #- reset $dir, recompose it, and clean trailing / added by algorithm.
106 0 $dir = '';
107 0 foreach (@paths) {
108 0 ▶ if ($_ eq '..') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq '..') { } 0 0 0
elsif ($_ ne '.') { } 0 0 0
109 0 ▶ if ($dir =~ s|([^/]+)/$||) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ s[([^/]+)/$][]) { } 0 0 0
110 0 ▶ if ($1 eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "..") 0 0 0
111 0 $dir .= "../../";
112 }
113 } else {
114 0 $dir .= "../";
115 }
116 } elsif ($_ ne '.') {
117 0 $dir .= "$_/";
118 }
119 }
120 0 $dir =~ s|/$||;
121 0 ▶ $dir ||= '/';
Condition0%0/2
$dir ||= "/"
l!l
00
MC/DC0%0/1
$dir ||= "/"
$dir
Truth table0%0/2
A
A= $dir
Aresult
11
00
122  
123 0 $host . $dir;
124 }
125  
126 =item offset_pathname($url, $offset)
127  
128 Offset pathname by returning the right things to add to a relative directory
129 to make no change. url is needed to resolve going before to top base.
130  
131 =cut
132  
133 0 ▶sub offset_pathname {
Subroutine offset_pathname: not called CC=3 SCAR=24.8
Pod documented
134 0 my ($url, $offset) = map { reduce_pathname($_) } @_;
135  
136 #- clean url to remove any macro (which cannot be solved now).
137 #- take care if this is a true url and not a simple pathname.
138 0 my (undef, $dir) = $url =~ m|([^:/]*://[^/]*/)?(.*)|;
139 0 my @paths = split '/', $dir;
140 0 my @offpaths = reverse split '/', $offset;
141 0 my @corrections;
142 0 my $result = '';
143  
144 0 foreach (@offpaths) {
145 0 ▶ if ($_ eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq '..') { } 0 0 0
146 0 push @corrections, pop @paths;
147 } else {
148 0 $result .= '../';
149 }
150 }
151 0 $result . join('/', reverse @corrections);
152 }
153  
154 0 ▶sub untaint {
Subroutine untaint: not called CC=2 SCAR=17.9
Pod undocumented
155 0 my @r = map { /(.*)/ } @_;
156 0 ▶ @r == 1 ? $r[0] : @r;
Branch: 1 branch
BranchTrueFalseTotal
@r == 1 ? : 0 0 0
157 }
158  
159 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod undocumented
160 0 my ($file, $dest) = @_;
161 0 !system("/bin/cp", "-p", "-L", "-R", $file, $dest);
162 }
163 0 ▶sub copy_and_own {
Subroutine copy_and_own: not called CC=2 SCAR=17.9
Pod undocumented
164 0 my ($file, $dest_file) = @_;
165 0 ▶ copy($file, $dest_file) && chown(0, 0, $dest_file) == 1;
Branch: 1 branch
BranchTrueFalseTotal
copy($file, $dest_file) and chown(0, 0, $dest_file) == 1 0 0 0
166 }
167  
168 0 ▶sub move {
Subroutine move: not called CC=2 SCAR=17.9
Pod undocumented
169 0 my ($file, $dest) = @_;
170 0 ▶ rename($file, $dest) || !system("/bin/mv", "-f", $file, $dest);
Branch: 1 branch
BranchTrueFalseTotal
rename $file, $dest or not system("/bin/mv", "-f", $file, $dest) 0 0 0
171 }
172  
173 =item file_size($file)
174  
175 It is useful to write file_size(...) > 32 without having warnings if file doesn't exist
176  
177 =cut
178  
179 0 ▶sub file_size {
Subroutine file_size: not called CC=2 SCAR=17.9
Pod documented
180 0 my ($file) = @_;
181 0 ▶ -s $file || 0;
Branch: 1 branch
BranchTrueFalseTotal
-s $file or 0 0 0 0
182 }
183  
184 0 ▶sub same_size_and_mtime {
Subroutine same_size_and_mtime: not called CC=2 SCAR=17.9
Pod undocumented
185 0 my ($f1, $f2) = @_;
186  
187 0 my @sstat = stat $f1;
188 0 my @lstat = stat $f2;
189 0 ▶ $sstat[7] == $lstat[7] && $sstat[9] == $lstat[9];
Branch: 1 branch
BranchTrueFalseTotal
$sstat[7] == $lstat[7] and $sstat[9] == $lstat[9] 0 0 0
190 }
191  
192 0 ▶sub partition(&@) {
Subroutine partition: not called CC=3 SCAR=24.8
Pod undocumented
193 0 my $f = shift;
194 0 my (@a, @b);
195 0 foreach (@_) {
196 0 ▶ $f->($_) ? push(@a, $_) : push(@b, $_);
Branch: 1 branch
BranchTrueFalseTotal
&$f($_) ? : 0 0 0
197 }
198 0 \@a, \@b;
199 }
200  
201 0 ▶sub begins_with {
Subroutine begins_with: not called CC=1 SCAR=6.9
Pod undocumented
202 0 my ($s, $prefix) = @_;
203 0 index($s, $prefix) == 0;
204 }
205 0 ▶sub formatList {
Subroutine formatList: not called CC=2 SCAR=17.9
Pod undocumented
206 0 my $nb = shift;
207 0 ▶ join(", ", @_ <= $nb ? @_ : (@_[0..$nb-1], '...'));
Branch: 1 branch
BranchTrueFalseTotal
@_ <= $nb ? : 0 0 0
208 }
209  
210 0 ▶sub add2hash_ { my ($a, $b) = @_; while (my ($k, $v) = each %{$b || {}}) { exists $a->{$k} or $a->{$k} = $v } $a }
Subroutine add2hash_: not called CC=4 SCAR=30.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$b or {};}) 0 0 0
exists $a->{$k} or $a->{$k} = $v 0 0 0
Condition0%0/2
$b || {}
l!l
00
MC/DC0%0/1
$b || {}
$b
Truth table0%0/2
A
A= $b
Aresult
11
00
211 0 ▶sub put_in_hash { my ($a, $b) = @_; while (my ($k, $v) = each %{$b || {}}) { $a->{$k} = $v } $a }
Subroutine put_in_hash: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$b or {};}) 0 0 0
Condition0%0/2
$b || {}
l!l
00
MC/DC0%0/1
$b || {}
$b
Truth table0%0/2
A
A= $b
Aresult
11
00
212 0 ▶sub uniq { my %l; $l{$_} = 1 foreach @_; grep { delete $l{$_} } @_ }
Subroutine uniq: not called CC=2 SCAR=17.9
Pod undocumented
213 0 ▶sub difference2 { my %l; @l{@{$_[1]}} = (); grep { !exists $l{$_} } @{$_[0]} }
Subroutine difference2: not called CC=1 SCAR=6.9
Pod undocumented
214 0 ▶sub intersection { my (%l, @m); @l{@{shift @_}} = (); foreach (@_) { @m = grep { exists $l{$_} } @$_; %l = (); @l{@m} = () } keys %l }
Subroutine intersection: not called CC=2 SCAR=17.9
Pod undocumented
215 0 ▶sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 }
Subroutine member: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$e eq $_ and return 1 0 0 0
216 0 ▶sub cat_ { my @l = map { my $F; open($F, '<', $_) ? <$F> : () } @_; wantarray() ? @l : join '', @l }
Subroutine cat_: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
open($F, '<', $_) ? : 0 0 0
wantarray ? : 0 0 0
217 0 ▶sub cat_utf8 { my @l = map { my $F; open($F, '<:utf8', $_) ? <$F> : () } @_; wantarray() ? @l : join '', @l }
Subroutine cat_utf8: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
open($F, '<:utf8', $_) ? : 0 0 0
wantarray ? : 0 0 0
218 0 ▶sub wc_l { my $F; open($F, '<', $_[0]) or return; my $count = 0; while (<$F>) { $count++ } $count }
Subroutine wc_l: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
open $F, "<", $_[0] or return 0 0 0
if (defined($_ = readline $F)) 0 0 0
219  
220 0 ▶sub uniq_(&@) {
Subroutine uniq_: not called CC=2 SCAR=17.9
Pod undocumented
221 0 my $f = shift;
222 0 my %l;
223 0 $l{$f->($_)} = 1 foreach @_;
224 0 grep { delete $l{$f->($_)} } @_;
225 }
226  
227 0 ▶sub output_safe {
Subroutine output_safe: not called CC=1 SCAR=6.9
Pod undocumented
228 0 my ($file, $content, $o_backup_ext) = @_;
229  
230 #- The file must be world-readable, else mgaapplet and urpm* commands run as
231 #- a normal user won't be able to read it. We enforce umask here in the case
232 #- where the msec security level is set to 'secure' (which means umask 077)
233 #- or where we are run from a gdm-x-session (mga#24636)
234 0 my $old_umask = umask 0022;
235 0 my $retval = output_safe_($file, $content, $o_backup_ext);
236 0 umask $old_umask;
237 0 $retval;
238 }
239  
240 0 ▶sub output_safe_ {
Subroutine output_safe_: not called CC=7 SCAR=40.3
Pod undocumented
241 0 my ($file, $content, $o_backup_ext) = @_;
242  
243 0 ▶ open(my $f, '>', "$file.new") or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $f, ">", "$file.new" or return 0 0 0
244 0 ▶ print $f $content or return;
Branch: 1 branch
BranchTrueFalseTotal
print $f $content or return 0 0 0
245 0 ▶ close $f or return;
Branch: 1 branch
BranchTrueFalseTotal
close $f or return 0 0 0
246  
247 0 ▶ rename($file, "$file$o_backup_ext") or return if $o_backup_ext;
Branch: 1 branch
BranchTrueFalseTotal
if $o_backup_ext 0 0 0
Condition0%0/5
$o_backup_ext && (rename($file, "$file$o_backup_ext") || (return))
!ll&&!rl&&r
000
rename $file, "$file$o_backup_ext" or return
l!l
00
MC/DC0%0/2
$o_backup_ext && (rename($file, "$file$o_backup_ext") || (return))
$o_backup_ext rename $file, "$file$o_backup_ext"
Truth table0%0/3
A && B
A= $o_backup_extB= rename $file, "$file$o_backup_ext"
ABresult
0X0
100
111
248 0 ▶ rename("$file.new", $file) or return;
Branch: 1 branch
BranchTrueFalseTotal
rename "$file.new", $file or return 0 0 0
249 0 1;
250 }
251  
252 0 ▶sub find(&@) {
Subroutine find: not called CC=3 SCAR=24.8
Pod undocumented
253 0 my $f = shift;
254 0 ▶ $f->($_) and return $_ foreach @_;
Branch: 1 branch
BranchTrueFalseTotal
&$f($_) and return $_ 0 0 0
255 0 undef;
256 }
257  
258 0 ▶sub any(&@) {
Subroutine any: not called CC=3 SCAR=24.8
Pod undocumented
259 0 my $f = shift;
260 0 ▶ $f->($_) and return 1 foreach @_;
Branch: 1 branch
BranchTrueFalseTotal
&$f($_) and return 1 0 0 0
261 0 0;
262 }
263  
264 0 ▶sub append_to_file {
Subroutine append_to_file: not called CC=3 SCAR=24.8
Pod undocumented
265 0 my $f = shift;
266 0 ▶ open(my $F, '>>', $f) or die "writing to file $f failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $F, ">>", $f or die "writing to file $f failed: $!\n" 0 0 0
267 0 print $F $_ foreach @_;
268 0 1;
269 }
270  
271 =item uefi_type()
272  
273 Return the UEFI machine type short name
274  
275 =cut
276  
277 # FIXME: move into urpm::sys ?
278 0 ▶sub uefi_type() {
Subroutine uefi_type: not called CC=3 SCAR=24.8
Pod documented
279 0 ▶ if (-e '/sys/firmware/efi/fw_platform_size') {
Branch: 1 branch
BranchTrueFalseTotal
if (-e '/sys/firmware/efi/fw_platform_size') { } 0 0 0
280 # No support for ARM yet
281 0 ▶ cat_('/sys/firmware/efi/fw_platform_size') =~ /32/ ? 'ia32' : 'x64';
Branch: 1 branch
BranchTrueFalseTotal
cat_('/sys/firmware/efi/fw_platform_size') =~ /32/ ? : 0 0 0
282 } else {
283 0 'none';
284 }
285 }
286  
287 2 1;
288  
289 =back
290  
291 =head1 COPYRIGHT
292  
293 Copyright (C) 2005 MandrakeSoft SA
294  
295 Copyright (C) 2005-2010 Mandriva SA
296  
297 Copyright (C) 2011-2020 Mageia
298  
299 =cut
« urpm/prompt.pm ↑ Summary gurpm/RPMProgressDialog.pm »