Dpkg/Path.pm

filter: Statementstmt 81.8% 113 / 138 Branchbran 56.7% 42 / 74 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 90.9% 20 / 22 Podpod 100% 10 / 10 totaltotal 73.3% 190 / 259 CC 44 SCAR 21.9
CC
44
Cov
72%
get_control_path
30.0
find_build_file
30.0
guess_pkg_root_dir
7.4
CRAP 8.9
« Dpkg/Changelog/Entry/Debian.pm ↑ Summary Dpkg/Shlibs/Symbol.pm »
1 # Copyright © 2007-2011 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2011 Linaro Limited
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Dpkg::Path - some common path handling functions
22  
23 =head1 DESCRIPTION
24  
25 It provides some functions to handle various path.
26  
27 =cut
28  
29 package Dpkg::Path 1.05;
30  
31 9 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 9 our @EXPORT_OK = qw(
34 canonpath
35 resolve_symlink
36 check_files_are_the_same
37 check_directory_traversal
38 find_command
39 find_build_file
40 get_control_path
41 get_pkg_root_dir
42 guess_pkg_root_dir
43 relative_to_pkg_root
44 );
45  
46 9 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 9 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 9 ▶use File::stat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 9 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 9 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 9 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 9 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 9 ▶use Dpkg::Arch qw(get_host_arch debarch_to_debtuple);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 9 ▶use Dpkg::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 =head1 FUNCTIONS
59  
60 =over 8
61  
62 =item $pathname = get_pkg_root_dir($file)
63  
64 This function will scan upwards the hierarchy of directory to find out
65 the directory which contains the "DEBIAN" sub-directory and it will return
66 its path. This directory is the root directory of a package being built.
67  
68 If no DEBIAN subdirectory is found, it will return undef.
69  
70 =cut
71  
72 6 ▶sub get_pkg_root_dir {
Subroutine get_pkg_root_dir: called CC=5 SCAR=16.2
Pod documented
73 6 my $file = shift;
74 6 $file =~ s{/+$}{};
75 6 ▶ $file =~ s{/+[^/]+$}{} if not -d $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $file 0 6 6
76 6 ▶ while ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 21 0 21
77 21 ▶ return $file if -d "$file/DEBIAN";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$file/DEBIAN" 3 18 21
78 18 ▶ last if $file !~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ m[/]u 3 15 18
79 15 $file =~ s{/+[^/]+$}{};
80 }
81 3 return;
82 }
83  
84 =item $pathname = relative_to_pkg_root($file)
85  
86 Returns the filename relative to get_pkg_root_dir($file).
87  
88 =cut
89  
90 2 ▶sub relative_to_pkg_root {
Subroutine relative_to_pkg_root: called CC=3 SCAR=11.0
Pod documented
91 2 my $file = shift;
92 2 my $pkg_root = get_pkg_root_dir($file);
93 2 ▶ if (defined $pkg_root) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pkg_root) 1 1 2
94 1 $pkg_root .= '/';
95 1 ▶ return $file if ($file =~ s/^\Q$pkg_root\E//);
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ s/^\Q$pkg_root\E//u 1 0 1
96 }
97 1 return;
98 }
99  
100 =item $pathname = guess_pkg_root_dir($file)
101  
102 This function tries to guess the root directory of the package build tree.
103 It will first use get_pkg_root_dir(), but it will fallback to a more
104 imprecise check: namely it will use the parent directory that is a
105 sub-directory of the debian directory.
106  
107 It can still return undef if a file outside of the debian sub-directory is
108 provided.
109  
110 =cut
111  
112 2 ▶sub guess_pkg_root_dir {
Subroutine guess_pkg_root_dir: called CC=7 SCAR=20.0
Pod documented
113 2 my $file = shift;
114 2 my $root = get_pkg_root_dir($file);
115 2 ▶ return $root if defined $root;
Branch: 1 branch
BranchTrueFalseTotal
if defined $root 1 1 2
116  
117 1 $file =~ s{/+$}{};
118 1 ▶ $file =~ s{/+[^/]+$}{} if not -d $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $file 0 1 1
119 1 my $parent = $file;
120 1 ▶ while ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 3 0 3
121 3 $parent =~ s{/+[^/]+$}{};
122 3 ▶ last if not -d $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $parent 0 3 3
123 3 ▶ return $file if check_files_are_the_same('debian', $parent);
Branch: 1 branch
BranchTrueFalseTotal
if check_files_are_the_same("debian", $parent) 1 2 3
124 2 $file = $parent;
125 2 ▶ last if $file !~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ m[/]u 0 2 2
126 }
127 0 return;
128 }
129  
130 =item $bool = check_files_are_the_same($file1, $file2, $resolve_symlink)
131  
132 This function verifies that both files are the same by checking that the device
133 numbers and the inode numbers returned by stat()/lstat() are the same. If
134 $resolve_symlink is true then stat() is used, otherwise lstat() is used.
135  
136 =cut
137  
138 5 ▶sub check_files_are_the_same {
Subroutine check_files_are_the_same: called CC=6 SCAR=19.7
Pod documented
139 5 my ($file1, $file2, $resolve_symlink) = @_;
140  
141 5 ▶ return 1 if $file1 eq $file2;
Branch: 1 branch
BranchTrueFalseTotal
if $file1 eq $file2 1 4 5
142 4 my ($st1, $st2);
143 4 ▶ if ($resolve_symlink) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve_symlink) { } 0 4 4
144 0 $st1 = File::stat::stat($file1);
145 0 $st2 = File::stat::stat($file2);
146 } else {
147 4 $st1 = File::stat::lstat($file1);
148 4 $st2 = File::stat::lstat($file2);
149 }
150 4 ▶ return 0 if ! defined $st1 || ! defined $st2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $st1 and defined $st2 0 4 4
Condition33%1/3
defined $st1 and defined $st2
!ll&&!rl&&r
004
MC/DC0%0/2
defined $st1 and defined $st2
defined $st1 defined $st2
Truth table33%1/3
A and B
A= defined $st1B= defined $st2
ABresult
0X0
100
111
151 4 ▶ return $st1->dev == $st2->dev && $st1->ino == $st2->ino;
Condition66%2/3
$st1->dev == $st2->dev && $st1->ino == $st2->ino
!ll&&!rl&&r
031
MC/DC50%1/2
$st1->dev == $st2->dev && $st1->ino == $st2->ino
$st1->dev == $st2->dev $st1->ino == $st2->ino
Truth table66%2/3
A && B
A= $st1->dev == $st2->devB= $st1->ino == $st2->ino
ABresult
0X0
100
111
152 }
153  
154  
155 =item $pathname = canonpath($file)
156  
157 This function returns a cleaned path. It simplifies double //, and remove
158 /./ and /../ intelligently. For /../ it simplifies the path only if the
159 previous element is not a symlink. Thus it should only be used on real
160 filenames.
161  
162 =cut
163  
164 7 ▶sub canonpath {
Subroutine canonpath: called CC=7 SCAR=19.9
Pod documented
165 7 my $path = shift;
166 7 $path = File::Spec->canonpath($path);
167 7 my ($v, $dirs, $file) = File::Spec->splitpath($path);
168 7 my @dirs = File::Spec->splitdir($dirs);
169 7 my @new;
170 7 foreach my $d (@dirs) {
171 38 ▶ if ($d eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq '..') { } 5 33 38
172 5 ▶ if (scalar(@new) > 0 and $new[-1] ne '..') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @new > 0 and $new[-1] ne '..') { } 5 0 5
Condition33%1/3
scalar @new > 0 and $new[-1] ne ".."
!ll&&!rl&&r
005
MC/DC0%0/2
scalar @new > 0 and $new[-1] ne ".."
scalar @new > 0 $new[-1] ne ".."
Truth table33%1/3
A and B
A= scalar @new > 0B= $new[-1] ne ".."
ABresult
0X0
100
111
173 # Root directory has no parent.
174 5 ▶ next if $new[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $new[-1] eq "" 0 5 5
175 5 my $parent = File::Spec->catpath($v,
176 File::Spec->catdir(@new), '');
177 5 ▶ if (not -l $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -l $parent) { } 4 1 5
178 4 pop @new;
179 } else {
180 1 push @new, $d;
181 }
182 } else {
183 0 push @new, $d;
184 }
185 } else {
186 33 push @new, $d;
187 }
188 }
189 7 return File::Spec->catpath($v, File::Spec->catdir(@new), $file);
190 }
191  
192 =item $pathname = resolve_symlink($symlink)
193  
194 Return the filename of the file pointed by the symlink. The new name is
195 canonicalized by canonpath().
196  
197 =cut
198  
199 3 ▶sub resolve_symlink {
Subroutine resolve_symlink: called CC=3 SCAR=11.0
Pod documented
200 3 my $symlink = shift;
201 3 my $content = readlink($symlink);
202 3 ▶ return unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 3 3
203 3 ▶ if (File::Spec->file_name_is_absolute($content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($content)) { } 1 2 3
204 1 return canonpath($content);
205 } else {
206 2 my ($link_v, $link_d, $link_f) = File::Spec->splitpath($symlink);
207 2 my ($cont_v, $cont_d, $cont_f) = File::Spec->splitpath($content);
208 2 my $new = File::Spec->catpath($link_v, $link_d . '/' . $cont_d, $cont_f);
209 2 return canonpath($new);
210 }
211 }
212  
213 =item check_directory_traversal($basedir, $dir)
214  
215 This function verifies that the directory $dir does not contain any symlink
216 that goes beyond $basedir (which should be either equal or a parent of $dir).
217  
218 =cut
219  
220 18 ▶sub check_directory_traversal {
Subroutine check_directory_traversal: called CC=1 SCAR=0.0
Pod documented
221 18 my ($basedir, $dir) = @_;
222  
223 18 my $canon_basedir = realpath($basedir);
224 # On Solaris /dev/null points to /devices/pseudo/mm@0:null.
225 18 my $canon_devnull = realpath('/dev/null');
226 81 ▶ my $check_symlinks = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
227 81 my $canon_pathname = realpath($_);
228 81 ▶ if (not defined $canon_pathname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $canon_pathname) 1 80 81
229 1 ▶ return if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 0 1 1
230  
231 1 syserr(g_("cannot canonicalize pathname '%s'"), $_);
232 }
233 80 ▶ return if $canon_pathname eq $canon_devnull;
Branch: 1 branch
BranchTrueFalseTotal
if $canon_pathname eq $canon_devnull 1 79 80
234 79 ▶ return if $canon_pathname eq $canon_basedir;
Branch: 1 branch
BranchTrueFalseTotal
if $canon_pathname eq $canon_basedir 0 79 79
235 79 ▶ return if $canon_pathname =~ m{^\Q$canon_basedir/\E};
Branch: 1 branch
BranchTrueFalseTotal
if $canon_pathname =~ m[^\Q$canon_basedir/\E]u 69 10 79
236  
237 10 error(g_("pathname '%s' points outside source root (to '%s')"),
238 $_, $canon_pathname);
239 18 };
240  
241 18 my $scan_symlinks = {
242 wanted => $check_symlinks,
243 no_chdir => 1,
244 follow => 1,
245 follow_skip => 2,
246 };
247 18 find($scan_symlinks, $dir);
248  
249 7 return;
250 }
251  
252 =item $pathname = find_command($command)
253  
254 Return the path of the command if defined and available on an absolute or
255 relative path or on the $PATH, undef otherwise.
256  
257 =cut
258  
259 110 ▶sub find_command {
Subroutine find_command: called CC=6 SCAR=19.9
Pod documented
260 110 my $cmd = shift;
261  
262 110 ▶ return if not $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 0 110 110
263 110 ▶ if ($cmd =~ m{/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ m[/]u) { } 0 110 110
264 0 ▶ return "$cmd" if -x "$cmd";
Branch: 1 branch
BranchTrueFalseTotal
if -x "$cmd" 0 0 0
265 } else {
266 110 foreach my $dir (split(/:/, $ENV{PATH})) {
267 1078 ▶ return "$dir/$cmd" if -x "$dir/$cmd";
Branch: 1 branch
BranchTrueFalseTotal
if -x "$dir/$cmd" 11 1067 1078
268 }
269 }
270 99 return;
271 }
272  
273 =item $pathname = get_control_path($pkg, $filetype)
274  
275 Return the path of the control file of type $filetype for the given
276 package.
277  
278 =item @pathname_list = get_control_path($pkg)
279  
280 Return the path of all available control files for the given package.
281  
282 =cut
283  
284 0 ▶sub get_control_path {
Subroutine get_control_path: not called CC=5 SCAR=34.0
Pod documented
285 0 my ($pkg, $filetype) = @_;
286 0 my $control_file;
287 0 my @exec = ('dpkg-query', '--control-path', $pkg);
288 0 ▶ push @exec, $filetype if defined $filetype;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filetype 0 0 0
289 0 spawn(
290 exec => \@exec,
291 wait_child => 1,
292 to_string => \$control_file,
293 );
294 0 chomp($control_file);
295 0 ▶ if (defined $filetype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filetype) 0 0 0
296 0 ▶ return if $control_file eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $control_file eq "" 0 0 0
297 0 return $control_file;
298 }
299 0 ▶ return () if $control_file eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $control_file eq "" 0 0 0
300 0 return split(/\n/, $control_file);
301 }
302  
303 =item $file = find_build_file($basename)
304  
305 Selects the right variant of the given file: the arch-specific variant
306 ("$basename.$arch") has priority over the OS-specific variant
307 ("$basename.$os") which has priority over the default variant
308 ("$basename"). If none of the files exists, then it returns undef.
309  
310 =item @files = find_build_file($basename)
311  
312 Return the available variants of the given file. Returns an empty
313 list if none of the files exists.
314  
315 =cut
316  
317 0 ▶sub find_build_file {
Subroutine find_build_file: not called CC=5 SCAR=34.0
Pod documented
318 0 my $base = shift;
319 0 my $host_arch = get_host_arch();
320 0 my ($abi, $libc, $host_os, $cpu) = debarch_to_debtuple($host_arch);
321 0 my @files;
322 0 foreach my $fn ("$base.$host_arch", "$base.$host_os", "$base") {
323 0 ▶ push @files, $fn if -f $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -f $fn 0 0 0
324 }
325 0 ▶ return @files if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
326 0 ▶ return $files[0] if scalar @files;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @files 0 0 0
327 0 return;
328 }
329  
330 =back
331  
332 =head1 CHANGES
333  
334 =head2 Version 1.05 (dpkg 1.20.4)
335  
336 New function: check_directory_traversal().
337  
338 =head2 Version 1.04 (dpkg 1.17.11)
339  
340 Update semantics: find_command() now handles an empty or undef argument.
341  
342 =head2 Version 1.03 (dpkg 1.16.1)
343  
344 New function: find_build_file()
345  
346 =head2 Version 1.02 (dpkg 1.16.0)
347  
348 New function: get_control_path()
349  
350 =head2 Version 1.01 (dpkg 1.15.8)
351  
352 New function: find_command()
353  
354 =head2 Version 1.00 (dpkg 1.15.6)
355  
356 Mark the module as public.
357  
358 =cut
359  
360 9 1;
« Dpkg/Changelog/Entry/Debian.pm ↑ Summary Dpkg/Shlibs/Symbol.pm »