Dpkg/Source/Archive.pm

filter: Statementstmt 63.0% 94 / 149 Branchbran 31.8% 21 / 66 Conditioncond 20.5% 7 / 34 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 79.1% 19 / 24 Podpod 0.0% 0 / 5 totaltotal 47.8% 142 / 297 CC 47 SCAR 28.5
CC
47
Cov
49%
extract
31.9
create
30.0
_add_entry
20.0
CRAP 17.2
« Dpkg/OpenPGP/Backend/SOP.pm ↑ Summary Dpkg/Control/FieldsCore.pm »
1 # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Dpkg::Source::Archive - source tarball archive support
21  
22 =head1 DESCRIPTION
23  
24 This module provides a class that implements support for handling
25 source tarballs.
26  
27 B<Note>: This is a private module, its API can change at any time.
28  
29 =cut
30  
31 package Dpkg::Source::Archive 0.01;
32  
33 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use Fcntl qw(:mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use File::stat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 ▶use Dpkg ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use Dpkg::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use Dpkg::Source::Functions qw(erasedir fixperms);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 2 ▶use parent qw(Dpkg::Compression::FileHandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 0 ▶sub create {
Subroutine create: not called CC=5 SCAR=34.0
Pod undocumented
54 0 my ($self, %opts) = @_;
55 0 ▶ $opts{options} //= [];
Condition0%0/2
$opts{"options"} //= []
l!l
00
MC/DC0%0/1
$opts{"options"} //= []
$opts{"options"}
Truth table0%0/2
A
A= $opts{"options"}
Aresult
11
00
56 0 my %spawn_opts;
57 # Possibly run tar from another directory.
58 0 ▶ if ($opts{chdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"chdir"}) 0 0 0
59 0 $spawn_opts{chdir} = $opts{chdir};
60 0 *$self->{chdir} = $opts{chdir};
61 }
62 # Redirect input/output appropriately.
63 0 $self->ensure_open('w');
64 0 $spawn_opts{to_handle} = $self->get_filehandle();
65 0 $spawn_opts{from_pipe} = \*$self->{tar_input};
66 # Try to use a deterministic mtime.
67 0 ▶ my $mtime = $opts{source_date} // $ENV{SOURCE_DATE_EPOCH} || time;
Condition0%0/5
($opts{'source_date'} // $ENV{'SOURCE_DATE_EPOCH'}) || time
l!l
00
$opts{"source_date"} // $ENV{"SOURCE_DATE_EPOCH"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($opts{'source_date'} // $ENV{'SOURCE_DATE_EPOCH'}) || time
$opts{"source_date"} $ENV{"SOURCE_DATE_EPOCH"}
Truth table0%0/3
A // B
A= $opts{"source_date"}B= $ENV{"SOURCE_DATE_EPOCH"}
ABresult
1X1
011
000
68 # Call tar creation process.
69 $spawn_opts{delete_env} = [
70 0 'TAR_OPTIONS',
71 ];
72 $spawn_opts{exec} = [
73 $Dpkg::PROGTAR,
74 '-cf', '-',
75 '--format=gnu',
76 '--sort=name',
77 '--mtime', "\@$mtime",
78 '--clamp-mtime',
79 '--null',
80 '--numeric-owner', '--owner=0', '--group=0',
81 0 @{$opts{options}},
82 '-T', '-',
83 ];
84 0 *$self->{pid} = spawn(%spawn_opts);
85 0 *$self->{cwd} = getcwd();
86 }
87  
88 0 ▶sub _add_entry {
Subroutine _add_entry: not called CC=4 SCAR=30.0
89 0 my ($self, $file) = @_;
90 0 my $cwd = *$self->{cwd};
91 0 ▶ croak 'call create() first' unless *$self->{tar_input};
Branch: 1 branch
BranchTrueFalseTotal
unless *$self->{"tar_input"} 0 0 0
92 0 ▶ if ($file =~ m{^\Q$cwd\E/(.+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[^\Q$cwd\E/(.+)$]u) 0 0 0
93 # Make pathname relative to the source root directory.
94 0 $file = $1;
95 }
96 0 ▶ print({ *$self->{tar_input} } "$file\0")
Branch: 1 branch
BranchTrueFalseTotal
print {*$self->{"tar_input"};} "$file\000" or syserr(g_("cannot write into tar input")) 0 0 0
97 or syserr(g_('cannot write into tar input'));
98 }
99  
100 0 ▶sub add_file {
Subroutine add_file: not called CC=4 SCAR=30.0
Pod undocumented
101 0 my ($self, $file) = @_;
102 0 my $testfile = $file;
103 0 ▶ if (*$self->{chdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (*$self->{"chdir"}) 0 0 0
104 0 $testfile = File::Spec->catfile(*$self->{chdir}, $file);
105 }
106 0 ▶ croak 'add_file() does not handle directories'
Branch: 1 branch
BranchTrueFalseTotal
if not -l $testfile and -d _ 0 0 0
Condition0%0/3
not -l $testfile and -d _
!ll&&!rl&&r
000
MC/DC0%0/2
not -l $testfile and -d _
not -l $testfile -d _
Truth table0%0/3
not(A) and B
A= not -l $testfileB= -d _
ABresult
0X0
100
111
107 if not -l $testfile and -d _;
108 0 $self->_add_entry($file);
109 }
110  
111 0 ▶sub add_directory {
Subroutine add_directory: not called CC=4 SCAR=30.0
Pod undocumented
112 0 my ($self, $file) = @_;
113 0 my $testfile = $file;
114 0 ▶ if (*$self->{chdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (*$self->{"chdir"}) 0 0 0
115 0 $testfile = File::Spec->catdir(*$self->{chdir}, $file);
116 }
117 0 ▶ croak 'add_directory() only handles directories'
Branch: 1 branch
BranchTrueFalseTotal
if -l $testfile or not -d _ 0 0 0
Condition0%0/3
-l $testfile or not -d _
l!l&&r!l&&!r
000
MC/DC0%0/2
-l $testfile or not -d _
-l $testfile not -d _
Truth table0%0/3
A or not(B)
A= -l $testfileB= not -d _
ABresult
1X1
011
000
118 if -l $testfile or not -d _;
119 0 $self->_add_entry($file);
120 }
121  
122 0 ▶sub finish {
Subroutine finish: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my $self = shift;
124  
125 0 ▶ close(*$self->{tar_input}) or syserr(g_('cannot close tar input'));
Branch: 1 branch
BranchTrueFalseTotal
close *$self->{"tar_input"} or syserr(g_("cannot close tar input")) 0 0 0
126 0 wait_child(*$self->{pid}, cmdline => "$Dpkg::PROGTAR -cf -");
127 0 delete *$self->{pid};
128 0 delete *$self->{tar_input};
129 0 delete *$self->{cwd};
130 0 delete *$self->{chdir};
131 0 $self->close();
132 }
133  
134 2 ▶sub extract {
Subroutine extract: called CC=15 SCAR=34.6
Pod undocumented
135 2 my ($self, $dest, %opts) = @_;
136 2 ▶ $opts{options} //= [];
Condition50%1/2
$opts{"options"} //= []
l!l
02
MC/DC0%0/1
$opts{"options"} //= []
$opts{"options"}
Truth table50%1/2
A
A= $opts{"options"}
Aresult
11
00
137 2 ▶ $opts{in_place} //= 0;
Condition50%1/2
$opts{"in_place"} //= 0
l!l
20
MC/DC0%0/1
$opts{"in_place"} //= 0
$opts{"in_place"}
Truth table50%1/2
A
A= $opts{"in_place"}
Aresult
11
00
138 2 ▶ $opts{no_fixperms} //= 0;
Condition50%1/2
$opts{"no_fixperms"} //= 0
l!l
02
MC/DC0%0/1
$opts{"no_fixperms"} //= 0
$opts{"no_fixperms"}
Truth table50%1/2
A
A= $opts{"no_fixperms"}
Aresult
11
00
139 2 my %spawn_opts = (
140 wait_child => 1,
141 );
142  
143 # Prepare destination.
144 2 ▶ unless (-e $dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dest) 0 2 2
145 # Kludge so that realpath works.
146 0 ▶ mkdir($dest) or syserr(g_('cannot create directory %s'), $dest);
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dest or syserr(g_("cannot create directory %s"), $dest) 0 0 0
147 }
148 2 my $tmpdir = File::Temp->newdir(
149 TEMPLATE => basename($self->get_filename()) . '.tmp-extract.XXXXX',
150 DIR => Cwd::realpath("$dest/.."),
151 );
152 2 $spawn_opts{chdir} = $tmpdir;
153  
154 # Prepare stuff that handles the input of tar.
155 2 $self->ensure_open('r',
156 delete_sig => [
157 'PIPE',
158 ],
159 );
160 2 $spawn_opts{from_handle} = $self->get_filehandle();
161  
162 # Call tar extraction process.
163 $spawn_opts{delete_env} = [
164 2 'TAR_OPTIONS',
165 ];
166 $spawn_opts{exec} = [
167 $Dpkg::PROGTAR,
168 '-xf', '-',
169 '--no-same-permissions',
170 '--no-same-owner',
171 2 @{$opts{options}},
172 ];
173 2 spawn(%spawn_opts);
174 1 $self->close();
175  
176 # Fix permissions on extracted files because tar insists on applying
177 # our umask _to the original permissions_ rather than mostly-ignoring
178 # the original permissions.
179 # We still need --no-same-permissions because otherwise tar might
180 # extract directory setgid (which we want inherited, not
181 # extracted); we need --no-same-owner because putting the owner
182 # back is tedious - in particular, correct group ownership would
183 # have to be calculated using mount options and other madness.
184 1 ▶ fixperms($tmpdir) unless $opts{no_fixperms};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"no_fixperms"} 1 0 1
185  
186 # If we are extracting "in-place" do not remove the destination directory.
187 1 ▶ if ($opts{in_place}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'in_place'}) { } 1 0 1
188 1 my $canon_basedir = Cwd::realpath($dest);
189 # On Solaris /dev/null points to /devices/pseudo/mm@0:null.
190 1 my $canon_devnull = Cwd::realpath('/dev/null');
191 6 ▶ my $check_symlink = sub {
Subroutine __ANON__: called CC=6 SCAR=20.5
192 6 my $pathname = shift;
193 6 my $canon_pathname = Cwd::realpath($pathname);
194 6 ▶ if (not defined $canon_pathname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $canon_pathname) 0 6 6
195 0 ▶ return if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 0 0 0
196  
197 0 syserr(g_("cannot canonicalize pathname '%s'"), $pathname);
198 }
199 6 ▶ return if $canon_pathname eq $canon_devnull;
Branch: 1 branch
BranchTrueFalseTotal
if $canon_pathname eq $canon_devnull 0 6 6
200 6 ▶ return if $canon_pathname eq $canon_basedir;
Branch: 1 branch
BranchTrueFalseTotal
if $canon_pathname eq $canon_basedir 0 6 6
201 6 ▶ return if $canon_pathname =~ m{^\Q$canon_basedir/\E};
Branch: 1 branch
BranchTrueFalseTotal
if $canon_pathname =~ m[^\Q$canon_basedir/\E]u 4 2 6
202 2 warning(g_("pathname '%s' points outside source root (to '%s')"),
203 $pathname, $canon_pathname);
204 1 };
205  
206 11 ▶ my $move_in_place = sub {
Subroutine __ANON__: called CC=14 SCAR=29.3
207 11 my $relpath = File::Spec->abs2rel($File::Find::name, $tmpdir);
208 11 my $destpath = File::Spec->catfile($dest, $relpath);
209  
210 11 ▶ my $src_st = File::stat::lstat($File::Find::name)
Branch: 1 branch
BranchTrueFalseTotal
my $src_st = &File::stat::lstat($File::Find::name) or syserr(g_("cannot get source pathname %s metadata"), $File::Find::name) 11 0 11
211 or syserr(g_('cannot get source pathname %s metadata'), $File::Find::name);
212 11 my $dst_st = File::stat::lstat($destpath);
213 11 ▶ if (! $dst_st && $! != ENOENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dst_st and $! != 2) 0 11 11
Condition66%2/3
not $dst_st and $! != 2
!ll&&!rl&&r
920
MC/DC0%0/2
not $dst_st and $! != 2
not $dst_st $! != 2
Truth table66%2/3
not(A) and B
A= not $dst_stB= $! != 2
ABresult
0X0
100
111
214 0 syserr(g_('cannot get target pathname %s metadata'), $destpath);
215 }
216  
217 11 ▶ if (defined $dst_st) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dst_st) 9 2 11
218 9 ▶ if (-d $dst_st && -d $src_st) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $dst_st and -d $src_st) { } 3 6 9
elsif (-d $dst_st) { } 0 6 6
Condition66%2/3
-d $dst_st and -d $src_st
!ll&&!rl&&r
603
MC/DC50%1/2
-d $dst_st and -d $src_st
-d $dst_st -d $src_st
Truth table66%2/3
A and B
A= -d $dst_stB= -d $src_st
ABresult
0X0
100
111
219 # Refresh the destination directory attributes with the
220 # ones from the tarball.
221 3 ▶ chmod S_IMODE($src_st->mode), $destpath
Branch: 1 branch
BranchTrueFalseTotal
chmod S_IMODE($src_st->mode), $destpath or syserr(g_("cannot change directory %s mode"), $File::Find::name) 3 0 3
222 or syserr(g_('cannot change directory %s mode'), $File::Find::name);
223 3 ▶ utime $src_st->atime, $src_st->mtime, $destpath
Branch: 1 branch
BranchTrueFalseTotal
utime $src_st->atime, $src_st->mtime, $destpath or syserr(g_("cannot change directory %s times"), $File::Find::name) 3 0 3
224 or syserr(g_('cannot change directory %s times'), $File::Find::name);
225  
226 # We should do nothing, and just walk further tree.
227 3 return;
228 } elsif (-d $dst_st) {
229 0 ▶ rmdir $destpath
Branch: 1 branch
BranchTrueFalseTotal
rmdir $destpath or syserr(g_("cannot remove destination directory %s"), $destpath) 0 0 0
230 or syserr(g_('cannot remove destination directory %s'), $destpath);
231 } else {
232 6 $check_symlink->($destpath);
233 6 ▶ unlink $destpath
Branch: 1 branch
BranchTrueFalseTotal
unlink $destpath or syserr(g_("cannot remove destination file %s"), $destpath) 6 0 6
234 or syserr(g_('cannot remove destination file %s'), $destpath);
235 }
236 }
237 # If we are moving a directory, we do not need to walk it.
238 8 ▶ if (-d $src_st) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $src_st) 5 3 8
239 5 $File::Find::prune = 1;
240 }
241 8 ▶ rename $File::Find::name, $destpath
Branch: 1 branch
BranchTrueFalseTotal
rename $File::Find::name, $destpath or syserr(g_("cannot move %s to %s"), $File::Find::name, $destpath) 8 0 8
242 or syserr(g_('cannot move %s to %s'), $File::Find::name, $destpath);
243 1 };
244  
245 1 my $scan_move_in_place = {
246 wanted => $move_in_place,
247 no_chdir => 1,
248 dangling_symlinks => 0,
249 };
250 1 find($scan_move_in_place, $tmpdir);
251 } else {
252 # Rename extracted directory.
253 0 ▶ opendir my $dir_dh, $tmpdir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir_dh, $tmpdir or syserr(g_("cannot open directory %s"), $tmpdir) 0 0 0
254 or syserr(g_('cannot open directory %s'), $tmpdir);
255 0 ▶ my @entries = grep { $_ ne '.' && $_ ne '..' } readdir($dir_dh);
Condition0%0/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
256 0 closedir($dir_dh);
257  
258 0 erasedir($dest);
259  
260 0 ▶ if (scalar(@entries) == 1 && ! -l "$tmpdir/$entries[0]" && -d _) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @entries == 1 and not -l "$tmpdir/$entries[0]" and -d _) { } 0 0 0
Condition0%0/6
scalar @entries == 1 and not -l "$tmpdir/$entries[0]" and -d _
!ll&&!rl&&r
000
scalar @entries == 1 and not -l "$tmpdir/$entries[0]"
!ll&&!rl&&r
000
MC/DC0%0/3
scalar @entries == 1 and not -l "$tmpdir/$entries[0]" and -d _
scalar @entries == 1 not -l "$tmpdir/$entries[0]" -d _
Truth table0%0/4
A and not(B) and C
A= scalar @entries == 1B= not -l "$tmpdir/$entries[0]"C= -d _
ABCresult
0XX0
10X0
1100
1111
261 0 ▶ rename "$tmpdir/$entries[0]", $dest
Branch: 1 branch
BranchTrueFalseTotal
rename "$tmpdir/$entries[0]", $dest or syserr(g_("cannot rename %s to %s"), "$tmpdir/$entries[0]", $dest) 0 0 0
262 or syserr(g_('cannot rename %s to %s'),
263 "$tmpdir/$entries[0]", $dest);
264 } else {
265 0 ▶ rename $tmpdir, $dest
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpdir, $dest or syserr(g_("cannot rename %s to %s"), $tmpdir, $dest) 0 0 0
266 or syserr(g_('cannot rename %s to %s'), $tmpdir, $dest);
267 }
268 }
269 1 erasedir($tmpdir);
270 }
271  
272 =head1 CHANGES
273  
274 =head2 Version 0.xx
275  
276 This is a private module.
277  
278 =cut
279  
280 2 1;
« Dpkg/OpenPGP/Backend/SOP.pm ↑ Summary Dpkg/Control/FieldsCore.pm »