lib/File/Unpack2.pm

filter: Statementstmt 36.5% 434 / 1186 Branchbran 21.4% 176 / 822 Conditioncond 22.4% 85 / 378 MC/DCmcdc 8.7% 19 / 217 Subroutinesub 65.2% 47 / 72 Podpod 100% 14 / 14 totaltotal 28.8% 775 / 2689 CC 593 SCAR 77.4
CC
593
Cov
29%
mime
7798.0
_run_mime_helper
5550.0
run
2352.0
CRAP 2309.1
↑ Summary script/file_unpack2 »
1 #
2 # File::Unpack2 - aggressively unpack any archive, recursively, by mime type.
3 #
4 # (C) 2010-2014 Juergen Weigert, (C) 2023-2026 Sebastian Riedel.
5 # Distributed under the same terms as Perl itself.
6 #
7  
8 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package File::Unpack2;
12  
13 BEGIN
14 16 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # Requires: shared-mime-info
16 0 ▶ eval 'use File::LibMagic;'; # only needed in mime(); mime() dies, if missing
Subroutine BEGIN: called CC=1 SCAR=1.2
17 16 ▶ eval 'use File::MimeInfo::Magic;'; # only needed in mime(); okay, if missing.
Subroutine BEGIN: called CC=1 SCAR=0.0
18 # unless builtin!
19 0 ▶ eval 'use Compress::Raw::Lzma;'; # only needed in mime(); for finding lzma.
Subroutine BEGIN: called CC=1 SCAR=1.2
20 16 ▶ eval 'use Compress::Raw::Bzip2;'; # only needed in mime(); for finding second level types
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶ eval 'use Compress::Raw::Zlib;'; # only needed in mime(); for finding second level types
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶ eval 'use BSD::Resource;'; # setrlimit
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶ eval 'use Filesys::Statvfs;'; # statvfs();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 }
25  
26 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 16 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 16 ▶use File::Temp (); # tempdir() in _run_mime_helper.
Subroutine BEGIN: called CC=1 SCAR=0.0
29 16 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 16 ▶use File::Compare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 16 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 16 ▶use String::ShellQuote; # used in _prep_configdir
Subroutine BEGIN: called CC=1 SCAR=0.0
33 16 ▶use IPC::Run; # implements File::Unpack2::run()
Subroutine BEGIN: called CC=1 SCAR=0.0
34 16 ▶use Text::Sprintf::Named; # used to parse @builtin_mime_helpers
Subroutine BEGIN: called CC=1 SCAR=0.0
35 16 ▶use Cwd 'getcwd'; # run(), moves us there and back.
Subroutine BEGIN: called CC=1 SCAR=0.0
36 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 16 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =head1 NAME
40  
41 File::Unpack2 - Aggressively unpack any archive, recursively, by mime type
42  
43 =head1 VERSION
44  
45 Version 2.04
46  
47 =cut
48  
49 # Please run perl Makefile.PL after changing the version here.
50 16 our $VERSION = '2.04';
51  
52 16 POSIX::setlocale(&POSIX::LC_ALL, 'C');
53 16 $ENV{PATH} = '/usr/bin:/bin';
54 16 $ENV{SHELL} = '/bin/sh';
55 16 delete $ENV{ENV};
56  
57 # Optional safeguard: the prctl(2) syscall number for arming PR_SET_PDEATHSIG on mime helpers (see
58 # run()'s init hook), resolved once from the system's own syscall.ph. Deliberately NOT a hardcoded
59 # arch-specific map - it is left undef where syscall.ph is not installed (e.g. slimmed CI perls),
60 # and the safeguard is then simply skipped. It is a belt to the stall watchdog's braces, not
61 # load-bearing; production perls ship syscall.ph, so it is armed there.
62 16 our $SYS_PRCTL;
63 {
64 # Load syscall.ph in its OWN throwaway package: it defines hundreds of SYS_* constant subs into the
65 # current package, which would pollute File::Unpack2 (and break Pod::Coverage). We only want one.
66 16 ▶ no warnings; local $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 0 ▶ if (eval { package File::Unpack2::_Syscall; require 'syscall.ph'; 1 } and defined &File::Unpack2::_Syscall::SYS_prctl)
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { package File::Unpack2::_Syscall; require "syscall.ph"; 1 } } and defined &File::Unpack2::_Syscall::SYS_prctl) 0 16 16
Condition33%1/3
eval { do { package File::Unpack2::_Syscall; require "syscall.ph"; 1 } } and defined &File::Unpack2::_Syscall::SYS_prctl
!ll&&!rl&&r
1600
MC/DC0%0/2
eval { do { package File::Unpack2::_Syscall; require "syscall.ph"; 1 } } and defined &File::Unpack2::_Syscall::SYS_prctl
eval { do { package File::Unpack2::_Syscall; require "syscall.ph"; 1 } } defined &File::Unpack2::_Syscall::SYS_prctl
Truth table33%1/3
A and B
A= eval { do { package File::Unpack2::_Syscall; require "syscall.ph"; 1 } }B= defined &File::Unpack2::_Syscall::SYS_prctl
ABresult
0X0
100
111
68 {
69 0 $SYS_PRCTL = File::Unpack2::_Syscall::SYS_prctl();
70 }
71 }
72  
73 # what we name the temporary directories, while helpers are working.
74 16 my $TMPDIR_TEMPL = '_fu_XXXXX';
75  
76 # Compress::Raw::Bunzip2 needs several 100k of input data, we special case this.
77 # File::LibMagic wants to read ca. 70k of input data, before it says application/vnd.ms-excel
78 # Anything else works with 1024.
79 16 my $UNCOMP_BUFSZ = 1024;
80  
81 # unpack will give up, after unpacking that many levels. It is more likely we
82 # got into a loop by then, than really have that many levels.
83 16 my $RECURSION_LIMIT = 50;
84  
85 # External MIME helpers are an optional extension: File::Unpack2 ships none of its
86 # own. A caller enables directory-based helpers by pointing the FILE_UNPACK2_HELPER_DIR
87 # environment variable (or the helper_dir constructor parameter) at a directory of
88 # executable helper scripts; helpers can also be registered programmatically with
89 # mime_helper(). Returns undef when no directory is configured, so nothing is scanned.
90 21 ▶sub _default_helper_dir { $ENV{FILE_UNPACK2_HELPER_DIR} }
Subroutine _default_helper_dir: called CC=1 SCAR=0.0
91  
92 # we use '=' in the mime_name, this expands to '/(x\-|ANY\+)?'
93 ##
94 ## Caution: always use (?: ... ) below for grouping, so that no extra capturing clauses are created.
95  
96 16 my @builtin_mime_helpers = (
97 # mimetype pattern # suffix_re # command with redirects, as defined with IPC::Run::run
98  
99 # Requires: xz bzip2 gzip unzip lzip
100 [ 'application=x-lzip', qr{(?:lz)}, [qw(/usr/bin/lzip -dc %(src)s)], qw(> %(destfile)s) ],
101 [ 'application=xz', qr{(?:xz|lz(ma)?)}, [qw(/usr/bin/lzcat --memlimit-decompress=60%)], qw(< %(src)s > %(destfile)s) ],
102 [ 'application=xz', qr{(?:xz|lz(ma)?)}, [qw(/usr/bin/xz -dc --memlimit-decompress=60% %(src)s)], qw(> %(destfile)s) ],
103 [ 'application=lzma', qr{(?:xz|lz(ma)?)}, [qw(/usr/bin/lzcat --memlimit-decompress=60%)], qw(< %(src)s > %(destfile)s) ],
104 [ 'application=lzma', qr{(?:xz|lz(ma)?)}, [qw(/usr/bin/xz -dc --memlimit-decompress=60% %(src)s)], qw(> %(destfile)s) ],
105 [ 'application=bzip2', qr{bz2}, [qw(/usr/bin/bunzip2 -dc -f %(src)s)], qw(> %(destfile)s) ],
106 [ 'application=gzip', qr{(?:gz|Z)}, [qw(/usr/bin/gzip -dc -f %(src)s)], qw(> %(destfile)s) ],
107 [ 'application=compress', qr{(?:gz|Z)}, [qw(/usr/bin/gzip -dc -f %(src)s)], qw(> %(destfile)s) ],
108  
109 # Requires: sharutils
110 [ 'text=uuencode', qr{uu}, [qw(/usr/bin/uudecode -o %(destfile)s %(src)s)] ],
111  
112 # Requires: upx
113 [ 'application=upx', qr{(?:upx\.exe|upx)}, [qw(/usr/bin/upx -q -q -q -d -o%(destfile)s %(lsrc)s) ] ],
114  
115 # xml.summary.Mono.Security.Authenticode is twice inside of monodoc-1.0.4.tar.gz/Mono.zip/ -> use -o
116 [ 'application=zip', qr{(?:zip|jar|sar)}, [qw(/usr/bin/unzip -P no_pw -q -o %(src)s)] ],
117  
118 # Requires: unrar
119 [ 'application=rar', qr{rar}, [qw(/usr/bin/unrar x -o- -p- -inul -kb -y %(src)s)] ],
120 # Requires: lha
121 [ 'application=x-lha', qr{lha}, [qw(/usr/bin/lha x -q %(src)s)] ],
122  
123 # Requires: binutils
124 [ 'application=archive', qr{(?:a|ar|deb)}, [qw(/usr/bin/ar x %(src)s)] ],
125 [ 'application=x-deb', qr{deb}, [qw(/usr/bin/ar x %(src)s)] ],
126 [ 'application=x-debian-package', qr{deb}, [qw(/usr/bin/ar x %(src)s)] ],
127  
128 # Requires: cabextract
129 [ 'application/vnd.ms-cab-compressed', qr{cab}, [qw(/usr/bin/cabextract -q %(src)s)] ],
130  
131 # Requires: p7zip
132 [ 'application/x-7z-compressed', qr{7z}, [qw(/usr/bin/7z x -pPass -y %(src)s)] ],
133  
134 # Requires: tar rpm cpio
135 [ 'application=tar', qr{(?:tar|gem)}, [\&_locate_tar, qw(-xf %(src)s)] ],
136 [ 'application=tar+bzip2', qr{(?:tar\.bz2|tbz)}, [\&_locate_tar, qw(-jxf %(src)s)] ],
137 [ 'application=tar+gzip', qr{t(?:ar\.gz|gz)}, [\&_locate_tar, qw(-zxf %(src)s)] ],
138 [ 'application=tar+lzma', qr{tar\.(?:xz|lzma|lz)}, [qw(/usr/bin/lzcat --memlimit-decompress=60%)], qw(< %(src)s |), [\&_locate_tar, qw(-xf -)] ],
139 [ 'application=tar+lzma', qr{tar\.(?:xz|lzma|lz)}, [qw(/usr/bin/xz -dc --memlimit-decompress=60% -f %(src)s)], '|', [\&_locate_tar, qw(-xf -)] ],
140 [ 'application=rpm', qr{(?:src\.r|s|r)pm}, [qw(/usr/bin/rpm2cpio %(src)s)], '|', [\&_locate_cpio_i] ],
141 [ 'application=cpio', qr{cpio}, [\&_locate_cpio_i], qw(< %(src)s) ],
142  
143 # Requires: poppler-tools
144 [ 'application=pdf', qr{pdf}, [qw(/usr/bin/pdftotext %(src)s %(destfile)s.txt)], '&', [qw(/usr/bin/pdfimages -j %(src)s pdfimages)] ],
145 );
146  
147 ## CAUTION keep _my_shell_quote in sync with all _locate_* functions.
148 sub _locate_tar
149 0 ▶{
Subroutine _locate_tar: not called CC=6 SCAR=37.4
150 0 my $self = shift;
151 0 ▶ return @{$self->{_locate_tar}} if defined $self->{_locate_tar};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_locate_tar"} 0 0 0
152  
153 # cannot use tar -C %(destdir)s, we rely on being chdir'ed inside already :-)
154 # E: /bin/tar: /tmp/xxx/_VASn/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_: Cannot chdir: Permission denied
155  
156 0 ▶ my @tar = (-f '/bin/tar' ? '/bin/tar' : '/usr/bin/tar' );
Branch: 1 branch
BranchTrueFalseTotal
-f '/bin/tar' ? : 0 0 0
157 ## osc co loves to create directories with : in them.
158 ## Tell tar to accept such directories as directores.
159 0 ▶ push @tar, "--force-local"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->run([@tar, "--force-local", "--help"], {"out_err", "/dev/null"}) 0 0 0
160 unless $self->run([@tar, "--force-local", "--help"], { out_err => '/dev/null' });
161 0 ▶ push @tar, "--no-unquote"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->run([@tar, "--no-unquote", "--help"], {"out_err", "/dev/null"}) 0 0 0
162 unless $self->run([@tar, "--no-unquote", "--help"], { out_err => '/dev/null'});
163 ## Archives not in tree order (npm's pngjs-5.0.0.tgz) would get a u-x directory mode applied before its last member
164 ## is written. unpack() makes each directory readable before descending, so the late modes are harmless.
165 0 ▶ push @tar, "--delay-directory-restore"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->run([@tar, "--delay-directory-restore", "--help"], {"out_err", "/dev/null"}) 0 0 0
166 unless $self->run([@tar, "--delay-directory-restore", "--help"], { out_err => '/dev/null'});
167  
168 0 $self->{_locate_tar} = \@tar;
169 0 return @tar;
170 }
171  
172 sub _locate_cpio_i
173 0 ▶{
Subroutine _locate_cpio_i: not called CC=6 SCAR=37.4
174 0 my $self = shift;
175 0 ▶ return @{$self->{_locate_cpio_i}} if defined $self->{_locate_cpio_i};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_locate_cpio_i"} 0 0 0
176  
177 0 my @cpio_i = ('/usr/bin/cpio', '-idm');
178 0 ▶ $cpio_i[1] .= 'u'
Branch: 1 branch
BranchTrueFalseTotal
unless run(["/usr/bin/cpio", "-idmu", "--usage"], {"out_err", "/dev/null"}) 0 0 0
179 unless run(['/usr/bin/cpio', '-idmu', '--usage'], {out_err => '/dev/null'});
180 0 ▶ push @cpio_i, '--sparse'
Branch: 1 branch
BranchTrueFalseTotal
unless run([@cpio_i, "--sparse", "--usage"], {"out_err", "/dev/null"}) 0 0 0
181 unless run([@cpio_i, '--sparse', '--usage'], {out_err => '/dev/null'});
182 0 ▶ push @cpio_i, '--no-absolute-filenames'
Branch: 1 branch
BranchTrueFalseTotal
unless run([@cpio_i, "--no-absolute-filenames", "--usage"], {"out_err", "/dev/null"}) 0 0 0
183 unless run([@cpio_i, '--no-absolute-filenames', '--usage'], {out_err => '/dev/null'});
184 0 ▶ push @cpio_i, '--force-local'
Branch: 1 branch
BranchTrueFalseTotal
unless run([@cpio_i, "--force-local", "--usage"], {"out_err", "/dev/null"}) 0 0 0
185 unless run([@cpio_i, '--force-local', '--usage'], {out_err => '/dev/null'});
186  
187 0 $self->{_locate_cpio_i} = \@cpio_i;
188 0 return @cpio_i;
189 }
190  
191 =head1 SYNOPSIS
192  
193 use File::Unpack2;
194  
195 # Recursively unpack an archive into a destination directory, capturing a
196 # JSON log of everything that was produced.
197 my $log;
198 my $u = File::Unpack2->new(logfile => \$log, destdir => '/tmp/out');
199 $u->unpack('inputfile.tar.bz2');
200 print "$1\n" while $log =~ m{^\s*"(.*?)":}g; # every unpacked file
201  
202 # Just identify a file's mime type (not fooled by the suffix).
203 my $m = $u->mime('/etc/init.d/rc');
204 print "$m->[0]; charset=$m->[1]\n"; # text/x-shellscript; charset=us-ascii
205  
206 # List the mime helpers that are wired up.
207 print "$_->{name}\n" for @{$u->mime_helper()};
208  
209 The distribution also ships a command line front-end, C<file_unpack2>; run
210 C<file_unpack2 --help> for its options.
211  
212 =head1 DESCRIPTION
213  
214 File::Unpack2 unpacks archives and compressed files of essentially any common
215 type - C<bz2>, C<gz>, C<xz>/C<lzma>, C<zip>, C<tar>, C<cpio>, C<iso>, C<rpm>,
216 C<deb>, C<cab>, C<7z>, C<rar>, and more, down to C<pdf> and C<odf> - and it does
217 so I<aggressively>: it identifies each file by its B<mime type> rather than its
218 name, and it recursively descends into everything it produces, so a
219 tar-inside-zip-inside-rpm is peeled apart to the last layer without being fooled
220 by misleading (or absent) file suffixes.
221  
222 It was built for, and is primarily developed as a dependency of,
223 L<Cavil|https://github.com/openSUSE/cavil>, openSUSE's legal review system, whose
224 job is to extract every scrap of readable text from a source package so it can be
225 scanned for license information. That goal - I<expose as much readable payload as
226 possible> - shapes the whole design: unpacking continues as deep as it can, and
227 individual failures are logged and stepped over rather than aborting the run.
228  
229 Unpacking is dispatched to per-mime-type B<helpers>. Many are built in (wrapping
230 the usual command line tools such as C<tar>, C<unzip> and C<rpm2cpio>). Support for
231 further formats is an optional extension: register a helper command programmatically
232 with C<mime_helper>, or point C<mime_helper_dir> (or the C<FILE_UNPACK2_HELPER_DIR>
233 environment variable) at a directory of executable helper scripts named after the
234 mime types they handle. File::Unpack2 itself ships no external helpers. See
235 L</unpack> for the helper calling protocol.
236  
237 Because Cavil feeds it whole distributions - including binaries, deliberately
238 malformed samples and archive "bombs" - File::Unpack2 is hardened against hostile
239 input: it enforces optional caps on file count, total bytes and per-helper
240 runtime, watches helpers for stalls, and passes memory limits to the C<xz>/C<lzma>
241 decoders. See L</new> for the relevant knobs.
242  
243 A logfile (JSON, or a plain listing) precisely records every mime type detected
244 and every unpack action taken.
245  
246 See the C<docs/Architecture.md> document in the distribution for a prose overview
247 of how it all fits together.
248  
249 =head1 SUBROUTINES/METHODS
250  
251 =head2 new
252  
253 my $u = new(destdir => '.', logfile => \*STDOUT, maxfilesize => '2G', verbose => 1,
254 world_readable => 0, one_shot => 0, no_op => 0, archive_name_as_dir => 0,
255 follow_file_symlinks => 0,
256 log_params => {}, log_type => 'JSON');
257  
258 Creates an unpacker instance. The parameter C<destdir> must be a writable location; all output
259 files and directories are placed inside this destdir. Subdirectories will be
260 created in an attempt to reflect the structure of the input. Destdir defaults
261 to the current directory; relative paths are resolved immediatly, so that
262 chdir() after calling new is harmless.
263  
264 The parameter C<logfile> can be a reference to a scalar, a filename, or a filedescriptor.
265 The logfile starts with a JSON formatted prolog, where all lines start
266 with printable characters.
267 For each file unpacked, a one line record is appended, starting with a single
268 whitespace ' ', and terminated by "\n". The format is a JSON-encoded C<< "key":
269 {value},\n >> pair, where key is the filename, and value is a hash including 'mime',
270 'size', and other information.
271 The logfile is terminated by an epilog, where each line starts with a printable character.
272 As part of the epilog, a dummy file named "\" with an empty hash is added to the list.
273 It should be ignored while parsing.
274 Per default, the logfile is sent to STDOUT.
275  
276 The parameter C<maxfilesize> is a safeguard against compressed sparse files and
277 test-files for archivers. Such files could easily fill up any available disk
278 space when unpacked. Files hitting this limit will be silently truncated.
279 Check the logfile records or epilog to see if this has happened. BSD::Resource
280 is used manipulate RLIMIT_FSIZE.
281  
282 The parameter C<one_shot> can optionally be set to non-zero, to limit unpacking
283 to one step of unpacking. Unpacking of well known compressed archives like
284 e.g. '.tar.bz2' is considered one step only. If uncompressing an archive is
285 considered an extra step before unpacking the archive depends on the configured
286 mime helpers.
287  
288 The parameter C<no_op> causes unpack() to only print one shell command to
289 STDOUT and exit. This implies one_shot=1.
290  
291 The parameter C<world_readable> causes unpack() change all directories to 0755,
292 and all files to 444. Otherwise 0700 and 0400 (user readable) is asserted.
293  
294 The parameter C<follow_file_symlinks> causes some or all symlinks to files
295 to be included.
296 A value of 1 follows symlinks that exist in the input directory and point to a file.
297 This has no effect if the input is an archive file. A value of 2 also follows symlinks
298 that were extracted from archives. CAUTION: This may cause unpack() to visit
299 files or archives elsewhere in the local filesystem.
300 Directory symlinks are always excluded.
301  
302 The parameter C<archive_name_as_dir> causes the unpacker to store all unpacked
303 files inside a directory with the same name as their archive.
304  
305 The default depends on how many files are unpacked from the archive: If exactly one
306 file (or one toplevel directory) is unpacked, then no extra directory is used.
307 E.g. F<foo.tar.gz> would unpack to F<foo.tar> or
308 F<foo-1.0.zip> would unpack to F<foo-1.0/*> and no files outside this directory.
309 If multiple files (or directories) are unpacked, and the suffix of the archive can
310 be removed with the C<suffix_re> of its C<mime_helper>, then the
311 shortened name is used as a directory. E.g. F<foo.tar> would unpack to
312 F<foo/*>. Otherwise F<._> is appended to the archive name. E.g. F<foo.tar> would unpack to
313 F<foo.tar._/*>.
314  
315 In any case, the suffix F<._> or F<._B<NNN>> is used to avoid conflicts with
316 already existing names where B<NNN> is a numeric value.
317  
318 =head2 exclude
319  
320 exclude(add => ['.svn', '*.orig' ], del => '.svn', force => 1, follow_file_symlinks => 0)
321  
322 Defines the exclude-list for unpacking. This list is advisory for the MIME helpers.
323 The exclude-list items are shell glob patterns, where '*' or '?' never match '/'.
324  
325 You can use force to have any of these removed after unpacking.
326 Use (vcs => 1) to exclude a long list of known version control system directories, use (vcs => 0) to remove them.
327 The default is C<< exclude(empty => 1) >>, which is the same as C<< exclude(empty_file => 1, empty_dir => 1) >> --
328 having the obvious meaning.
329  
330 (re => 1) returns the active exclude-list as a regexp pattern.
331 Otherwise C<exclude> always returns the list as an array ref.
332  
333 Some symbolic links are included if {follow_file_symlinks} is nonzero. For details see C<<new()>>.
334  
335 If exclude patterns were effective, or if symlinks, fifos, sockets, ... were encountered during unpack(),
336 the logfile contains an additional 'skipped' keyword with statistics.
337  
338 =cut
339  
340 sub _glob_list_re
341 6 ▶{
Subroutine _glob_list_re: called CC=3 SCAR=11.0
342 6 my @re;
343 6 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 6 6
344 6 for my $text (@_)
345 {
346 # Taken from pdb2perl:glob2re() and adapted, to not match slashes in wildcards.
347 # This should be kept compatible with tar --exclude .
348  
349 47 $text =~ s{([\.\(\)\[\]\{\}])}{\\$1}g; ## protect magic re characters.
350 47 $text =~ s{\*}{[^/]*}g; ## * -> [^/]*
351 47 $text =~ s{\?}{[^/]}g; ## ? -> [^/]
352 47 push @re, $text;
353 }
354 6 return '(/|^)(' . join('|', @re) . ')(/|$)';
355 }
356  
357 sub _not_excluded
358 148 ▶{
Subroutine _not_excluded: called CC=5 SCAR=16.2
359 148 my $self = shift;
360 148 my ($dir, $file) = @_;
361  
362 148 ▶ return 1 unless my $re = $self->{exclude}{re};
Branch: 1 branch
BranchTrueFalseTotal
unless my $re = $self->{"exclude"}{"re"} 17 131 148
363  
364 131 ▶ $dir ||= '';
Condition50%1/2
$dir ||= ""
l!l
1310
MC/DC0%0/1
$dir ||= ""
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
365 131 ▶ $dir .= '/' unless $dir =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $dir =~ m[/$] 0 131 131
366 131 $file = $dir . $file;
367  
368 131 ▶ return 0 if $file =~ m{$re};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /$re/ 57 74 131
369 74 return 1;
370 }
371  
372 sub exclude
373 6 ▶{
Subroutine exclude: called CC=17 SCAR=30.8
Pod documented
374 6 my $self = shift;
375 6 ▶ my %opt = $#_ ? @_ : (add => $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
$#_ ? : 6 0 6
376  
377 # ADD to this list from: https://build.opensuse.org/project/show?project=devel%3Atools%3Ascm
378 6 my @vcs = qw(SCCS RCS CVS .svn .git .hg .osc);
379  
380 6 ▶ $opt{add} = [ $opt{add} ] unless ref $opt{add};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt{"add"} 2 4 6
381 6 ▶ $opt{del} = [ $opt{del} ] unless ref $opt{del};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt{"del"} 6 0 6
382  
383 6 ▶ push @{$opt{add}}, @vcs if defined $opt{vcs} and $opt{vcs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"vcs"} and $opt{"vcs"} 6 0 6
Condition33%1/3
defined $opt{"vcs"} and $opt{"vcs"}
!ll&&!rl&&r
006
MC/DC0%0/2
defined $opt{"vcs"} and $opt{"vcs"}
defined $opt{"vcs"} $opt{"vcs"}
Truth table33%1/3
A and B
A= defined $opt{"vcs"}B= $opt{"vcs"}
ABresult
0X0
100
111
384 0 ▶ push @{$opt{del}}, @vcs if defined $opt{vcs} and !$opt{vcs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"vcs"} and not $opt{"vcs"} 0 6 6
Condition33%1/3
defined $opt{"vcs"} and not $opt{"vcs"}
!ll&&!rl&&r
060
MC/DC0%0/2
defined $opt{"vcs"} and not $opt{"vcs"}
defined $opt{"vcs"} not $opt{"vcs"}
Truth table33%1/3
A and not(B)
A= defined $opt{"vcs"}B= not $opt{"vcs"}
ABresult
0X0
100
111
385  
386  
387 6 for my $a (@{$opt{add}})
388 {
389 49 ▶ $self->{exclude}{list}{$a}++ if defined $a;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a 47 2 49
390 }
391  
392 6 for my $a (@{$opt{del}})
393 {
394 6 ▶ delete $self->{exclude}{list}{$a} if defined $a;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a 0 6 6
395 }
396  
397 6 my @list = sort keys %{$self->{exclude}{list}};
398 6 $self->{exclude}{re} = _glob_list_re(@list);
399  
400 6 ▶ $opt{empty_dir} = $opt{empty_file} = $opt{empty} if defined $opt{empty};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"empty"} 0 6 6
401  
402 6 for my $o (qw(empty_file empty_dir force))
403 {
404 18 ▶ $self->{exclude}{$o} = $opt{$o} if defined $opt{$o};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{$o} 0 18 18
405 }
406  
407 $self->{follow_file_symlinks} = $opt{follow_file_symlinks}
408 6 ▶ if defined $opt{follow_file_symlinks};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"follow_file_symlinks"} 0 6 6
409  
410 6 ▶ return $opt{re} ? $self->{exclude}{re} : \@list;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'re'} ? : 1 5 6
411 }
412  
413 =head2 Internal logging helpers
414  
415 These are used internally by C<unpack> and are not normally called directly.
416  
417 =over 4
418  
419 =item log
420  
421 Send text to the logfile.
422  
423 =item logf
424  
425 Take a filename and a hash and log a JSON-formatted line. The trailing newline of
426 a line is delayed and emitted by the next call (with a leading comma from the
427 second call onward, so the accumulated result is valid JSON).
428  
429 =item loggable_pathname
430  
431 Shorten a path to be relative to either C<< $self->{destdir} >> or
432 C<< $self->{input} >>, unless C<< $self->{log_fullpath} >> is true.
433  
434 =back
435  
436 =cut
437 sub log
438 114 ▶{
Subroutine log: called CC=5 SCAR=16.6
Pod documented
439 114 my ($self, $text) = @_;
440 114 ▶ if (my $fp = $self->{lfp})
Branch: 1 branch
BranchTrueFalseTotal
if (my $fp = $self->{"lfp"}) 114 0 114
441 {
442 114 my $oldpos = eval { $fp->tell; }; # old perl at SLES11 has no IO::Handle::tell()
443 114 ▶ $fp->write($text) or die "log($self->{logfile}): write failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$fp->write($text) or die qq[log($self->{"logfile"}): write failed: $!\n] 114 0 114
444 114 my $r = eval { $fp->tell - $oldpos; };
445  
446 ## We do not expect any multibyte utf8 issues in here. It is plain 7-bit JSON.
447 ## E.g. /dev/null is not seekable. Be forgiving.
448 114 ▶ die "$oldpos,$r=log($self->{logfile}): write failed: $text\n" if $r and $r != length($text);
Branch: 1 branch
BranchTrueFalseTotal
if $r and $r != length $text 0 114 114
Condition66%2/3
$r and $r != length $text
!ll&&!rl&&r
24900
MC/DC0%0/2
$r and $r != length $text
$r $r != length $text
Truth table66%2/3
A and B
A= $rB= $r != length $text
ABresult
0X0
100
111
449 114 $self->{lfp_printed}++;
450 }
451 }
452  
453 sub loggable_pathname
454 90 ▶{
Subroutine loggable_pathname: called CC=6 SCAR=24.3
Pod documented
455 90 my ($self, $file, $hash) = @_;
456  
457 90 ▶ unless ($self->{log_fullpath})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"log_fullpath"}) 90 0 90
458 {
459 # very frequently, files are inside the destdir
460 90 ▶ unless ($file =~ s{^\Q$self->{destdir}\E/}{})
Branch: 1 branch
BranchTrueFalseTotal
unless ($file =~ s[^\Q$self->{"destdir"}\E/][]) 90 0 90
461 {
462 # less frequently, archives are logged inside the input dir
463 90 ▶ if ($self->{input})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"input"}) 0 90 90
464 {
465 0 ▶ if ($file =~ s{^\Q$self->{input}\E/}{\./input/./})
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ s[^\Q$self->{"input"}\E/][./input/./]) 0 0 0
466 {
467 0 ▶ $hash->{srcdir} = 'input' if ref $hash eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash eq "HASH" 0 0 0
468 }
469 }
470 }
471 }
472 90 return $file;
473 }
474  
475 sub logf
476 90 ▶{
Subroutine logf: called CC=10 SCAR=29.1
Pod documented
477 90 my ($self,$file,$hash,$suff) = @_;
478 90 ▶ $suff = "" unless defined $suff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suff 90 0 90
479 90 ▶ my $json = $self->{json} ||= JSON->new()->ascii(1);
Condition33%1/3
$self->{"json"} ||= "JSON"->new->ascii(1)
l!l&&r!l&&!r
9000
MC/DC0%0/2
$self->{"json"} ||= "JSON"->new->ascii(1)
$self->{"json"} "JSON"->new->ascii(1)
Truth table33%1/3
A ||= B
A= $self->{"json"}B= "JSON"->new->ascii(1)
ABresult
1X1
011
000
480 90 $file = $self->loggable_pathname($file, $hash);
481 90 ▶ if (my $fp = $self->{lfp})
Branch: 1 branch
BranchTrueFalseTotal
if (my $fp = $self->{"lfp"}) 90 0 90
482 {
483 90 ▶ if ($self->{log_type} eq 'plain')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log_type'} eq 'plain') { } 0 90 90
484 {
485 0 my $str = $file . ' (';
486 0 ▶ $str .= $hash->{mime} if defined $hash->{mime};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash->{"mime"} 0 0 0
487 0 $str .= ')';
488 0 ▶ $str = "# $str -> " . $hash->{unpacked} if $hash->{unpacked};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"unpacked"} 0 0 0
489 0 $str .= "\n";
490 0 $self->log($str);
491 }
492 else
493 {
494 $self->log(qq[{ "oops": "logf used before prolog??",\n"unpacked_files":{\n])
495 90 ▶ unless $self->{lfp_printed}; # sysseek($fp, 0, 1); # }} there is no systell() ...
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lfp_printed"} 0 90 90
496 90 my $str = $json->encode({$file => $hash});
497 90 $str =~ s{^\{}{}s;
498 90 $str =~ s{\}$}{}s;
499 90 my $pre = " ";
500 90 ▶ $pre = ",\n " if $self->{logf_continuation}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"logf_continuation"}++ 82 8 90
501 90 ▶ die "logf failed to encode newline char: $str\n" if $str =~ m{(?:\n|\r)};
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /(?:\n|\r)/ 0 90 90
502 90 $self->log("$pre$str$suff");
503 }
504 }
505 }
506  
507 $SIG{'XFSZ'} = sub
508 0 ▶{
Subroutine __ANON__: not called CC=1 SCAR=6.9
509 0 print STDERR "soft RLIMIT_FSIZE exceeded. SIGXFSZ recieved. Exiting\n";
510 0 exit;
511 16 };
512  
513 # if this returns 0, we test again and call it again, possibly.
514 # if this returns nonzero, we just continue.
515 sub _default_fs_warn
516 0 ▶{
Subroutine _default_fs_warn: not called CC=1 SCAR=6.9
517 0 carp "Filesystem (@_) is almost full.\n $0 paused for 30 sec.\n";
518 0 sleep(30);
519 0 return 0;
520 }
521  
522 ## returns 1, if enough space free.
523 ## returns 0, if warn-method was called, and returned nonzero
524 ## returns -1, if no warn method
525 ## or does not return at all, and rechecks the status
526 ## with at least on second delay, if warn-method returns 0.
527 sub _fs_check
528 0 ▶{
Subroutine _fs_check: not called CC=13 SCAR=52.0
529 0 my ($self, $needed_b, $needed_i, $needed_p) = @_;
530 0 ▶ $needed_b = '1M' unless defined $needed_b; # bytes
Branch: 1 branch
BranchTrueFalseTotal
unless defined $needed_b 0 0 0
531 0 ▶ $needed_i = 100 unless defined $needed_i; # inodes
Branch: 1 branch
BranchTrueFalseTotal
unless defined $needed_i 0 0 0
532 0 ▶ $needed_p = 1.0 unless defined $needed_p; # percent
Branch: 1 branch
BranchTrueFalseTotal
unless defined $needed_p 0 0 0
533 0 $needed_b = _bytes_unit($needed_b);
534  
535 0 my $DIR;
536 open $DIR, "<", $self->{destdir} or
537 0 ▶ opendir $DIR, $self->{destdir} or return;
Branch: 1 branch
BranchTrueFalseTotal
open $DIR, "<", $self->{"destdir"} or opendir $DIR, $self->{"destdir"} or return 0 0 0
Condition0%0/3
open $DIR, "<", $self->{"destdir"} or opendir $DIR, $self->{"destdir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
open $DIR, "<", $self->{"destdir"} or opendir $DIR, $self->{"destdir"}
open $DIR, "<", $self->{"destdir"} opendir $DIR, $self->{"destdir"}
Truth table0%0/3
A or B
A= open $DIR, "<", $self->{"destdir"}B= opendir $DIR, $self->{"destdir"}
ABresult
1X1
011
000
538 ## fileno() does not work with opendir() handles.
539 0 ▶ my $fd = fileno($DIR); return unless defined $fd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd 0 0 0
540  
541 0 for (;;)
542 {
543 0 my $st = eval { [ fstatvfs($fd) ] };
544 0 my $total_b = $st->[1] * $st->[2]; # f_frsize * f_blocks
545 0 my $free_b = $st->[0] * $st->[4]; # f_bsize * f_bavail
546 0 my $free_i = $st->[7]; # f_favail
547 0 ▶ my $perc = 100.0 * ($total_b - $free_b) / ($total_b||1);
Condition0%0/2
$total_b || 1
l!l
00
MC/DC0%0/1
$total_b || 1
$total_b
Truth table0%0/2
A
A= $total_b
Aresult
11
00
548  
549 0 ▶ return 1 if $free_b >= $needed_b &&
Branch: 1 branch
BranchTrueFalseTotal
if $free_b >= $needed_b and $free_i >= $needed_i and 100 - $perc > $needed_p 0 0 0
Condition0%0/6
$free_b >= $needed_b and $free_i >= $needed_i and 100 - $perc > $needed_p
!ll&&!rl&&r
000
$free_b >= $needed_b and $free_i >= $needed_i
!ll&&!rl&&r
000
MC/DC0%0/3
$free_b >= $needed_b and $free_i >= $needed_i and 100 - $perc > $needed_p
$free_b >= $needed_b $free_i >= $needed_i 100 - $perc > $needed_p
Truth table0%0/4
A and B and C
A= $free_b >= $needed_bB= $free_i >= $needed_iC= 100 - $perc > $needed_p
ABCresult
0XX0
10X0
1100
1111
550 $free_i >= $needed_i &&
551 (100-$perc > $needed_p);
552  
553 0 ▶ return -1 unless $self->{fs_warn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fs_warn"} 0 0 0
554 0 my $w = $self->{fs_warn}->($self->{destdir}, $perc, $free_b, $free_i);
555 0 ▶ return 0 if $w;
Branch: 1 branch
BranchTrueFalseTotal
if $w 0 0 0
556 0 sleep 1;
557 }
558 }
559  
560 sub new
561 20 ▶{
Subroutine new: called CC=45 SCAR=46.6
Pod documented
562 20 my $self = shift;
563 20 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
564 0 ▶ my %obj = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 20 20
565  
566 20 ▶ $obj{verbose} = 1 unless defined $obj{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj{"verbose"} 4 16 20
567 20 ▶ $obj{destdir} ||= '.';
Condition100%2/2
$obj{"destdir"} ||= "."
l!l
155
MC/DC100%1/1
$obj{"destdir"} ||= "."
$obj{"destdir"}
Truth table100%2/2
A
A= $obj{"destdir"}
Aresult
11
00
568 20 ▶ $obj{logfile} ||= \*STDOUT;
Condition100%2/2
$obj{"logfile"} ||= \*STDOUT
l!l
164
MC/DC100%1/1
$obj{"logfile"} ||= \*STDOUT
$obj{"logfile"}
Truth table100%2/2
A
A= $obj{"logfile"}
Aresult
11
00
569 20 ▶ $obj{log_type} ||= 'json'; # or 'plain'
Condition100%2/2
$obj{"log_type"} ||= "json"
l!l
218
MC/DC100%1/1
$obj{"log_type"} ||= "json"
$obj{"log_type"}
Truth table100%2/2
A
A= $obj{"log_type"}
Aresult
11
00
570 20 $obj{log_type} = lc $obj{log_type};
571 20 ▶ $obj{maxfilesize} = $ENV{'FILE_UNPACK2_MAXFILESIZE'}||'2.5G' unless defined $obj{maxfilesize};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj{"maxfilesize"} 18 2 20
Condition100%2/2
$ENV{'FILE_UNPACK2_MAXFILESIZE'} || '2.5G'
l!l
810
MC/DC100%1/1
$ENV{'FILE_UNPACK2_MAXFILESIZE'} || '2.5G'
$ENV{'FILE_UNPACK2_MAXFILESIZE'}
Truth table100%2/2
A
A= $ENV{'FILE_UNPACK2_MAXFILESIZE'}
Aresult
11
00
572 20 $obj{maxfilesize} = _bytes_unit($obj{maxfilesize});
573 20 $ENV{'FILE_UNPACK2_MAXFILESIZE'} = $obj{maxfilesize}; # so that children see the same.
574  
575 # Kill a mime-helper that makes no I/O progress for this many seconds, so a stuck helper
576 # (blocked on a fifo, deadlocked pipe, ...) can never hang unpacking forever. 0 disables.
577 # An env value of "0" must disable it, so we cannot use the ||= idiom here.
578 20 ▶ unless (defined $obj{stall_timeout})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj{"stall_timeout"}) 18 2 20
579 {
580 $obj{stall_timeout} = defined $ENV{'FILE_UNPACK2_STALL_TIMEOUT'}
581 18 ▶ ? $ENV{'FILE_UNPACK2_STALL_TIMEOUT'} : 120;
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'FILE_UNPACK2_STALL_TIMEOUT'} ? : 7 11 18
582 }
583 20 $obj{stall_timeout} += 0; # numeric seconds
584 20 $ENV{'FILE_UNPACK2_STALL_TIMEOUT'} = $obj{stall_timeout}; # so that children see the same.
585  
586 # Opt-in absolute caps against breadth/slow bombs. All default OFF (0), because legitimate inputs
587 # can be huge (e.g. a 30G chromium tarball) and these would clip them. Enable per-deployment.
588 # max_files - stop after this many files have been unpacked in one top-level run
589 # max_total_bytes - stop after this many bytes have been processed in one top-level run
590 # helper_timeout - absolute wall-clock per mime helper (catches "slow bombs" that keep
591 # progressing and so never trip the no-progress stall watchdog)
592 # Read env for opt-in defaults, but (unlike maxfilesize/RLIMIT) do NOT write it back: these caps
593 # are enforced in-process during the recursion, so a process-global env value would leak between
594 # File::Unpack2 instances (e.g. across packages in a long-lived worker).
595 20 for my $cap (qw(max_files max_total_bytes helper_timeout))
596 {
597 60 my $env = 'FILE_UNPACK2_' . uc($cap);
598 60 ▶ $obj{$cap} = defined $ENV{$env} ? $ENV{$env} : 0 unless defined $obj{$cap};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $obj{$cap} 57 3 60
defined $ENV{$env} ? : 0 57 57
599 }
600 # Only max_total_bytes takes a size suffix (1G/500M/...). max_files (a count) and helper_timeout
601 # (seconds) are plain numbers - do NOT run them through _bytes_unit, or "30m" would be read as
602 # 30 megabytes/megaseconds instead of what the user meant.
603 20 $obj{max_total_bytes} = _bytes_unit($obj{max_total_bytes});
604 20 $obj{max_files} += 0;
605 20 $obj{helper_timeout} += 0;
606  
607 20 mkpath($obj{destdir}); # abs_path is unreliable if destdir does not exist
608 20 $obj{destdir} = Cwd::fast_abs_path($obj{destdir});
609 20 $obj{destdir} =~ s{(.)/+$}{$1}; # assert no trailing '/'.
610  
611 # used in unpack() to jail mime_helpers deep inside destdir:
612 20 ▶ $obj{dot_dot_safeguard} = 20 unless defined $obj{dot_dot_safeguard};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj{"dot_dot_safeguard"} 20 0 20
613 20 ▶ $obj{jail_chmod0} ||= 0;
Condition50%1/2
$obj{"jail_chmod0"} ||= 0
l!l
020
MC/DC0%0/1
$obj{"jail_chmod0"} ||= 0
$obj{"jail_chmod0"}
Truth table50%1/2
A
A= $obj{"jail_chmod0"}
Aresult
11
00
614 # used in unpack, print only:
615 20 ▶ $obj{no_op} ||= 0;
Condition50%1/2
$obj{"no_op"} ||= 0
l!l
020
MC/DC0%0/1
$obj{"no_op"} ||= 0
$obj{"no_op"}
Truth table50%1/2
A
A= $obj{"no_op"}
Aresult
11
00
616 # used in unpack, blocks recursion after archive unpacking:
617 20 ▶ $obj{one_shot} ||= $obj{no_op};
Condition66%2/3
$obj{"one_shot"} ||= $obj{"no_op"}
l!l&&r!l&&!r
1019
MC/DC50%1/2
$obj{"one_shot"} ||= $obj{"no_op"}
$obj{"one_shot"} $obj{"no_op"}
Truth table66%2/3
A ||= B
A= $obj{"one_shot"}B= $obj{"no_op"}
ABresult
1X1
011
000
618  
619 # With $self->{within_archives} we know the difference between symlinks found in
620 # the given repository or symlinks that were unpacked from an archive.
621 # Those from an archive are followed only with follow_file_symlinks == 2.
622 20 ▶ $obj{follow_file_symlinks} ||= 0;
Condition50%1/2
$obj{"follow_file_symlinks"} ||= 0
l!l
020
MC/DC0%0/1
$obj{"follow_file_symlinks"} ||= 0
$obj{"follow_file_symlinks"}
Truth table50%1/2
A
A= $obj{"follow_file_symlinks"}
Aresult
11
00
623  
624 20 ▶ warn "WARNING: We are running as root: Malicious archives may clobber your filesystem.\n" if $obj{verbose} and !$>;
Branch: 1 branch
BranchTrueFalseTotal
if $obj{"verbose"} and not $> 0 20 20
Condition66%2/3
$obj{"verbose"} and not $>
!ll&&!rl&&r
1640
MC/DC0%0/2
$obj{"verbose"} and not $>
$obj{"verbose"} not $>
Truth table66%2/3
A and not(B)
A= $obj{"verbose"}B= not $>
ABresult
0X0
100
111
625  
626 20 ▶ if (ref $obj{logfile} eq 'SCALAR' or !(ref $obj{logfile}))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj{'logfile'} eq 'SCALAR' or not ref $obj{'logfile'}) { } 16 4 20
Condition100%3/3
ref $obj{"logfile"} eq "SCALAR" or not ref $obj{"logfile"}
l!l&&r!l&&!r
2144
MC/DC100%2/2
ref $obj{"logfile"} eq "SCALAR" or not ref $obj{"logfile"}
ref $obj{"logfile"} eq "SCALAR" not ref $obj{"logfile"}
Truth table100%3/3
A or not(B)
A= ref $obj{"logfile"} eq "SCALAR"B= not ref $obj{"logfile"}
ABresult
1X1
011
000
627 {
628 16 ▶ open $obj{lfp}, ">", $obj{logfile} or croak "open logfile $obj{logfile} failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $obj{"lfp"}, ">", $obj{"logfile"} or croak(qq[open logfile $obj{"logfile"} failed: $!\n]) 16 0 16
629 }
630 else
631 {
632 4 $obj{lfp} = $obj{logfile};
633 }
634 # make $obj{lfp} unbuffered, so that other processes can read line by line...
635 20 $obj{lfp}->autoflush(1);
636 20 $obj{lfp_printed} = 0;
637  
638 20 $obj{readable_file_modes} = [ 0400 ];
639 20 $obj{readable_dir_modes} = [ 0700, 0500 ];
640  
641 20 ▶ if ($obj{world_readable})
Branch: 1 branch
BranchTrueFalseTotal
if ($obj{"world_readable"}) 0 20 20
642 {
643 0 unshift @{$obj{readable_file_modes}}, 0444;
644 0 unshift @{$obj{readable_dir_modes}}, 0755;
645 }
646  
647 20 ▶ if ($obj{maxfilesize})
Branch: 1 branch
BranchTrueFalseTotal
if ($obj{"maxfilesize"}) 20 0 20
648 {
649 eval
650 20 {
651 16 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
652 # helper/application=x-shellscript calls File::Unpack2->new(), with defaults...
653 20 my @have = BSD::Resource::getrlimit(RLIMIT_FSIZE);
654 20 ▶ if ($have[0] == RLIM_INFINITY or $have[0] > $obj{maxfilesize})
Branch: 1 branch
BranchTrueFalseTotal
if ($have[0] == RLIM_INFINITY() or $have[0] > $obj{"maxfilesize"}) 0 20 20
Condition33%1/3
$have[0] == RLIM_INFINITY() or $have[0] > $obj{"maxfilesize"}
l!l&&r!l&&!r
0020
MC/DC0%0/2
$have[0] == RLIM_INFINITY() or $have[0] > $obj{"maxfilesize"}
$have[0] == RLIM_INFINITY() $have[0] > $obj{"maxfilesize"}
Truth table33%1/3
A or B
A= $have[0] == RLIM_INFINITY()B= $have[0] > $obj{"maxfilesize"}
ABresult
1X1
011
000
655 {
656 # if RLIM_INFINITY is seen as an attempt to increase limits, we would fail. Ignore this.
657 BSD::Resource::setrlimit(RLIMIT_FSIZE, $obj{maxfilesize}, RLIM_INFINITY) or
658 0 ▶ BSD::Resource::setrlimit(RLIMIT_FSIZE, $obj{maxfilesize}, $obj{maxfilesize}) or
Branch: 1 branch
BranchTrueFalseTotal
&BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, scalar RLIM_INFINITY()) or &BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, $obj{"maxfilesize"}) or warn qq[RLIMIT_FSIZE($obj{"maxfilesize"}), limit=($have[0],$have[1]) failed\n] 0 0 0
Condition0%0/3
&BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, scalar RLIM_INFINITY()) or &BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, $obj{"maxfilesize"})
l!l&&r!l&&!r
000
MC/DC0%0/2
&BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, scalar RLIM_INFINITY()) or &BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, $obj{"maxfilesize"})
&BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, scalar RLIM_INFINITY()) &BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, $obj{"maxfilesize"})
Truth table0%0/3
A or B
A= &BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, scalar RLIM_INFINITY())B= &BSD::Resource::setrlimit(scalar RLIMIT_FSIZE(), $obj{"maxfilesize"}, $obj{"maxfilesize"})
ABresult
1X1
011
000
659 warn "RLIMIT_FSIZE($obj{maxfilesize}), limit=($have[0],$have[1]) failed\n";
660 }
661 };
662 20 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 20 20
663 {
664 0 carp "WARNING maxfilesize=$obj{maxfilesize} ignored:\n $@ $!\n Maybe package perl-BSD-Resource is not installed??\n\n";
665 }
666 }
667  
668 20 ▶ $obj{minfree}{factor} = 10 unless defined $obj{minfree}{factor};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj{"minfree"}{"factor"} 20 0 20
669 20 ▶ $obj{minfree}{bytes} = '1M' unless defined $obj{minfree}{bytes};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj{"minfree"}{"bytes"} 20 0 20
670 20 ▶ $obj{minfree}{percent} = '1%' unless defined $obj{minfree}{percent};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj{"minfree"}{"percent"} 20 0 20
671 20 ▶ minfree(\%obj, warning => $obj{fs_warn}||\&_default_fs_warn);
Condition50%1/2
$obj{'fs_warn'} || \&File::Unpack2::_default_fs_warn
l!l
020
MC/DC0%0/1
$obj{'fs_warn'} || \&File::Unpack2::_default_fs_warn
$obj{'fs_warn'}
Truth table50%1/2
A
A= $obj{'fs_warn'}
Aresult
11
00
672  
673 20 ▶ $obj{exclude}{empty_dir} = 1 unless defined $obj{exclude}{empty_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj{"exclude"}{"empty_dir"} 20 0 20
674 20 ▶ $obj{exclude}{empty_file} = 1 unless defined $obj{exclude}{empty_file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj{"exclude"}{"empty_file"} 20 0 20
675  
676 20 $self = bless \%obj, $class;
677  
678 20 for my $h (@builtin_mime_helpers)
679 {
680 520 $self->mime_helper(@$h);
681 }
682 20 ▶ $obj{helper_dir} = _default_helper_dir unless exists $obj{helper_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj{"helper_dir"} 20 0 20
683 20 ▶ $self->mime_helper_dir($obj{helper_dir}) if defined $obj{helper_dir} and -d $obj{helper_dir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $obj{"helper_dir"} and -d $obj{"helper_dir"} 0 20 20
Condition33%1/3
defined $obj{"helper_dir"} and -d $obj{"helper_dir"}
!ll&&!rl&&r
2000
MC/DC0%0/2
defined $obj{"helper_dir"} and -d $obj{"helper_dir"}
defined $obj{"helper_dir"} -d $obj{"helper_dir"}
Truth table33%1/3
A and B
A= defined $obj{"helper_dir"}B= -d $obj{"helper_dir"}
ABresult
0X0
100
111
684  
685 20 ▶ unless ($ENV{PERL5LIB})
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"PERL5LIB"}) 0 20 20
686 {
687 # in case we are using non-standard perl lib dirs, put them into the environment,
688 # so that any helper scripts see them too. They might need them, if written in perl.
689  
690 16 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
691 0 my $pat = qr{^(?:\Q$Config{vendorlib}\E|\Q$Config{sitelib}\E|\Q$Config{privlib}\E)\b};
692 0 my @add; # all dirs, that come before the standard dirs.
693 0 for my $i (@INC)
694 {
695 0 ▶ last if $i =~ m{$pat};
Branch: 1 branch
BranchTrueFalseTotal
if $i =~ /$pat/ 0 0 0
696 0 push @add, $i;
697 }
698 0 ▶ $ENV{PERL5LIB} = join ':', @add if @add;
Branch: 1 branch
BranchTrueFalseTotal
if @add 0 0 0
699 }
700  
701 20 return $self;
702 }
703  
704 sub DESTROY
705 20 ▶{
Subroutine DESTROY: called CC=14 SCAR=27.7
706 20 my $self = shift;
707 # when unpack() processes an input, it should delete {lfp} afterwards.
708 # Added some 'or' cases, as $self->{input} might be empty, although we had processed an input.
709 #
710 # We rather catch an error, than produce incomplete output.
711 # This happens with ksh/ast-base.2012-08-01.tar.bz2 after unpack('.../ast-base.2012-08-01/src/cmd/pax/data/a'): no such file or directory
712 #
713 20 ▶ if (($self->{input} or
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"input"} || ($self->{"lfp_printed"} || 0) || ($self->{"recursion_level"} || 0) and $self->{"lfp"}) 3 17 20
Condition84%11/13
$self->{"input"} || ($self->{"lfp_printed"} || 0) || ($self->{"recursion_level"} || 0) and $self->{"lfp"}
!ll&&!rl&&r
1703
$self->{"input"} or ($self->{"lfp_printed"} or 0) or ($self->{"recursion_level"} or 0)
l!l&&r!l&&!r
1217
$self->{"input"} or ($self->{"lfp_printed"} or 0)
l!l&&r!l&&!r
0119
$self->{"lfp_printed"} or 0
l!l
119
$self->{"recursion_level"} or 0
l!l
217
MC/DC50%2/4
$self->{"input"} || ($self->{"lfp_printed"} || 0) || ($self->{"recursion_level"} || 0) and $self->{"lfp"}
$self->{"input"} $self->{"lfp_printed"} $self->{"recursion_level"} $self->{"lfp"}
Truth table42%3/7
A or B or C and D
A= $self->{"input"}B= $self->{"lfp_printed"}C= $self->{"recursion_level"}D= $self->{"lfp"}
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
714 ($self->{lfp_printed}||0) or
715 ($self->{recursion_level}||0)) and $self->{lfp})
716 {
717 3 ▶ if ($self->{log_type} eq 'plain')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log_type'} eq 'plain') { } 0 3 3
718 {
719 # pass
720 }
721 else
722 {
723 3 ▶ $self->log(sprintf(qq[{"pid":"%d", "unpacked":{], $$)) unless $self->{lfp_printed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lfp_printed"} 2 1 3
724 }
725 3 ▶ my $r = $self->{recursion_level}||0;
Condition50%1/2
$self->{'recursion_level'} || 0
l!l
30
MC/DC0%0/1
$self->{'recursion_level'} || 0
$self->{'recursion_level'}
Truth table50%1/2
A
A= $self->{'recursion_level'}
Aresult
11
00
726  
727 # this should never happen.
728 # always delete $self->{lfp} manually, when done.
729 ## {{
730 3 my $msg = "unexpected destructor seen";
731 3 ▶ $msg = join('; ', @{$self->{error}}) if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 1 2 3
732 3 ▶ if ($self->{log_type} eq 'plain')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log_type'} eq 'plain') { } 0 3 3
733 {
734 0 $self->log("# error: (l=$self->{lfp_printed},r=$r): $msg\n");
735 }
736 else
737 {
738 3 $self->log(qq[\n}, "error":"(l=$self->{lfp_printed},r=$r): $msg"}\n]);
739 }
740 3 ▶ close $self->{lfp} if $self->{lfp} ne $self->{logfile};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lfp"} ne $self->{"logfile"} 3 0 3
741 3 delete $self->{lfp};
742 3 delete $self->{lfp_printed};
743 }
744 20 ▶ if ($self->{configdir})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"configdir"}) 0 20 20
745 {
746 0 rmtree($self->{configdir});
747 0 delete $self->{configdir};
748 }
749 }
750  
751 =head2 unpack
752  
753 $u->unpack($archive, [$destdir])
754  
755 Determines the contents of an archive and recursivly extracts its files.
756 An archive may be the pathname of a file or directory. The extracted contents will be
757 stored in F<destdir/$subdir/$dest_name>, where dest_name is the filename
758 component of archive without any leading pathname components, and possibly
759 stripped or added suffix. (Subdir defaults to ''.) If archive is a directory,
760 then dest_name will also be a directory. If archive is a file, the type of
761 dest_name depends on the type of packing: If the archive expands to multiple
762 files, dest_name will be a directory, otherwise it will be a file. If a file of
763 the same name already exists in the destination subdir, an additional subdir
764 component is created to avoid any conflicts.
765  
766 For each extracted file, a record is written to the logfile.
767 When unpacking is finished, the logfile contains one valid JSON structure.
768 Unpack achieves this by writing suitable prolog and epilog lines to the logfile.
769 The logfile can also be parsed line by line. All file records is one line and start
770 with a ' ' whitespace, and end in a ',' comma. Everything else is prolog or epilog.
771  
772 The actual unpacking is dispatched to MIME type specific helpers,
773 selected using C<mime>. A MIME helper can be built-in code, a command registered
774 with C<mime_helper>, or an external executable script found in a directory
775 registered with C<mime_helper_dir>. Such a directory can be supplied with the
776 environment variable F<FILE_UNPACK2_HELPER_DIR> or the C<new> parameter
777 C<helper_dir>; File::Unpack2 ships no external helpers of its own.
778  
779 The naming of helper scripts is described under C<mime_helper()>.
780  
781 A MIME helper must have executable permission and is called with 6 parameters:
782 source_path, destfile, destination_path, mimetype, description, and config_dir.
783 Note, that destination_path is a freshly created empty working directory, even
784 if the unpacker is expected to unpack only a single file. The unpacker is
785 called after chdir into destination_path, so you usually do not need to
786 evaluate the third parameter.
787  
788 The directory C<config_dir> contains unpack configuration in .sh, .js and possibly
789 other formats. A MIME helper may use this information, but need not.
790 All data passed into C<new> is reflected there, as well as the active exclude-list.
791 Using the config information can help a MIME helper to skip unwanted
792 work or otherwise optimize unpacking.
793  
794 C<unpack> monitors the available filesystem space in destdir. If there is less space
795 than configured with C<minfree>, a warning can be printed and unpacking is
796 optionally paused. It also monitors the MIME helpers progress reading the archive
797 at source_path and reports percentages to STDERR (if verbose is 1 or more).
798  
799 After the MIME helper is finished, C<unpack> examines the files it created.
800 If it created no files in F<destdir>, an error is reported, and the
801 F<source_path> may be passed to other unpackers, or finally be added to the log as is.
802  
803 If the MIME helper wants to express that F<source_path> is already unpacked as far as possible
804 and should be added to the log without any error messages, it creates a symbolic link
805 F<destdir> pointing to F<source_path>.
806  
807  
808 The system considers replacing the
809 directory with a file, if all of the following conditions are met:
810  
811 =over
812  
813 =item *
814  
815 There is exactly one file in the directory.
816  
817 =item *
818  
819 The file name is identical with the directory name,
820 except for one changed or removed
821 suffix-word. (*.tar.gz -> *.tar; or *.tgz -> *.tar)
822  
823 =item *
824  
825 The file must not already exist in the parent directory.
826  
827 =back
828  
829 C<unpack> prepares 20 empty subdirectory levels and chdirs the unpacker
830 in there. This number can be adjusted using C<< new(dot_dot_safeguard => 20) >>.
831 A directory 20 levels up from the current working dir has mode 0 while
832 the MIME helper runs. C<unpack> can optionally chmod(0) the parent of the subdirectory
833 after it chdirs the unpacker inside. Use C<< new(jail_chmod0 => 1) >> for this, default
834 is off. If enabled, a MIME helper trying to place files outside of the specified
835 destination_path may receive 'permission denied' conditions.
836  
837 These are special hacks to keep badly constructed
838 tar-balls, cpio-, or zip-archives at bay.
839  
840 Please note, that this can help against archives containing relative paths
841 (like starting with '../../../foo'), but will be ineffective with absolute paths
842 (starting with '/foo').
843 It is the responsibility of MIME helpers to not create absolute paths;
844 C<unpack> should not be run as the root user, to minimize the risk of
845 compromising the root filesystem.
846  
847 A missing MIME helper is skipped, and subsequent helpers may take effect. A
848 MIME helper is expected to return an exit status of 0 upon success. If it runs
849 into a problem, it should print lines
850 starting with the affected filenames to stderr.
851 Such errors are recorded in the log with the unpacked archive, and as far as
852 files were created, also with these files.
853  
854 Symbolic links are ignored while unpacking.
855  
856 Currently you can call C<unpack> only once.
857  
858 =cut
859  
860 sub unpack
861 166 ▶{
Subroutine unpack: called CC=105 SCAR=77.3
Pod documented
862 ## as long as $archive is outside $self->{destdir}, we construct our destdir by
863 ## replacing $self->{input_dir} with $self->{destdir}.
864 ## This $self->{input_dir} must be created and kept constant at the earliest
865 ## possible call.
866 ## When the $archive is inside $self->{destdir}, we do not use $self->{input_dir},
867 ## we then use the current $in_dir as destdir.
868 ##
869 ## Whenever an archive path outside $self->{destdir} is found,
870 ## it is first passed through Cwd::fast_abs_path before any other processing occurs.
871 ##
872 166 my ($self, $archive, $destdir) = @_;
873 166 ▶ $destdir = $self->{destdir} unless defined $destdir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $destdir 12 154 166
874  
875 166 ▶ $destdir = $1 if $destdir =~ m{^(.*)$}s; # brute force untaint
Branch: 1 branch
BranchTrueFalseTotal
if $destdir =~ /^(.*)$/s 166 0 166
876  
877 166 ▶ if (($self->{recursion_level}||0) > $RECURSION_LIMIT)
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"recursion_level"} || 0) > $RECURSION_LIMIT) 1 165 166
Condition100%2/2
$self->{'recursion_level'} || 0
l!l
15610
MC/DC100%1/1
$self->{'recursion_level'} || 0
$self->{'recursion_level'}
Truth table100%2/2
A
A= $self->{'recursion_level'}
Aresult
11
00
878 {
879 1 warn "unpack('$archive','$destdir'): recursion limit $RECURSION_LIMIT";
880 ## this is only an emergency stop.
881 1 return 1;
882 }
883  
884 # Opt-in breadth-bomb caps (default off). Stop descending once a limit is reached, but do it
885 # gracefully - log once, then quietly skip the rest so the run still finishes with a valid log.
886 165 ▶ if ( ($self->{max_files} and ($self->{file_count}||0) >= $self->{max_files})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"max_files"} and ($self->{"file_count"} || 0) >= $self->{"max_files"} or $self->{"max_total_bytes"} and ($self->{"total_bytes"} || 0) >= $self->{"max_total_bytes"}) 4 161 165
Condition76%10/13
$self->{"max_files"} and ($self->{"file_count"} || 0) >= $self->{"max_files"} or $self->{"max_total_bytes"} and ($self->{"total_bytes"} || 0) >= $self->{"max_total_bytes"}
l!l&&r!l&&!r
04161
$self->{"max_files"} and ($self->{"file_count"} || 0) >= $self->{"max_files"}
!ll&&!rl&&r
15870
$self->{'file_count'} || 0
l!l
07
$self->{"max_total_bytes"} and ($self->{"total_bytes"} || 0) >= $self->{"max_total_bytes"}
!ll&&!rl&&r
15834
$self->{'total_bytes'} || 0
l!l
52
MC/DC50%2/4
$self->{"max_files"} and ($self->{"file_count"} || 0) >= $self->{"max_files"} or $self->{"max_total_bytes"} and ($self->{"total_bytes"} || 0) >= $self->{"max_total_bytes"}
$self->{"max_files"} $self->{'file_count'} $self->{"max_total_bytes"} $self->{'total_bytes'}
Truth table57%4/7
A and B or C and D
A= $self->{"max_files"}B= $self->{'file_count'}C= $self->{"max_total_bytes"}D= $self->{'total_bytes'}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
887 or ($self->{max_total_bytes} and ($self->{total_bytes}||0) >= $self->{max_total_bytes}))
888 {
889 4 ▶ unless ($self->{_capped}++)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_capped"}++) 1 3 4
890 {
891 my $why = ($self->{max_files} and ($self->{file_count}||0) >= $self->{max_files})
892 1 ▶ ? "max_files=$self->{max_files}" : "max_total_bytes=$self->{max_total_bytes}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'max_files'} && ($self->{'file_count'} || 0) >= $self->{'max_files'} ? : 0 1 1
Condition20%1/5
$self->{"max_files"} and ($self->{"file_count"} || 0) >= $self->{"max_files"}
!ll&&!rl&&r
100
$self->{'file_count'} || 0
l!l
00
MC/DC0%0/2
$self->{"max_files"} and ($self->{"file_count"} || 0) >= $self->{"max_files"}
$self->{"max_files"} $self->{'file_count'}
Truth table33%1/3
A and B
A= $self->{"max_files"}B= $self->{'file_count'}
ABresult
0X0
100
111
893 1 warn "File::Unpack2: $why reached - skipping remaining files\n";
894 1 push @{$self->{error}}, "resource cap reached ($why); ";
895 }
896 4 return 1;
897 }
898  
899 161 ▶ if ($archive !~ m{^/} or $archive !~ m{^\Q$self->{destdir}\E/})
Branch: 1 branch
BranchTrueFalseTotal
unless ($archive =~ m[^/] and $archive =~ m[^\Q$self->{"destdir"}\E/]) 161 0 161
Condition66%2/3
$archive =~ m[^/] and $archive =~ m[^\Q$self->{"destdir"}\E/]
!ll&&!rl&&r
41570
MC/DC0%0/2
$archive =~ m[^/] and $archive =~ m[^\Q$self->{"destdir"}\E/]
$archive =~ m[^/] $archive =~ m[^\Q$self->{"destdir"}\E/]
Truth table66%2/3
A and B
A= $archive =~ m[^/]B= $archive =~ m[^\Q$self->{"destdir"}\E/]
ABresult
0X0
100
111
900 {
901 # Cwd::fast_abs_path($archive) not only makes nice absolute paths, but it also expands
902 # file symlinks. This is a bad idea for two reasons:
903 # * when we allow {follow_file_symlinks} the link destination gets into the log file,
904 # rather than the (expected) link itself.
905 # * Also, this could easily trigger "path escaped" below .
906 ######
907 161 ▶ if ($self->{follow_file_symlinks} && $archive =~ m{^(.*)/(.*?)$})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'follow_file_symlinks'} and $archive =~ m[^(.*)/(.*?)$]) { } 0 161 161
Condition33%1/3
$self->{"follow_file_symlinks"} and $archive =~ m[^(.*)/(.*?)$]
!ll&&!rl&&r
16100
MC/DC0%0/2
$self->{"follow_file_symlinks"} and $archive =~ m[^(.*)/(.*?)$]
$self->{"follow_file_symlinks"} $archive =~ m[^(.*)/(.*?)$]
Truth table33%1/3
A and B
A= $self->{"follow_file_symlinks"}B= $archive =~ m[^(.*)/(.*?)$]
ABresult
0X0
100
111
908 {
909 # we solve both issues by doing this:
910 # chop off the filename; expand the path; re-add the filename.
911 0 my ($a_path, $a_file) = ($1,$2);
912 0 ▶ $a_path = Cwd::fast_abs_path($a_path) if -e $a_path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $a_path 0 0 0
913 0 $archive = $a_path . '/' . $a_file;
914 }
915 else
916 {
917 161 ▶ $archive = Cwd::fast_abs_path($archive) if -e $archive;
Branch: 1 branch
BranchTrueFalseTotal
if -e $archive 160 1 161
918 }
919 }
920  
921 161 ▶ unless (-e $archive)
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $archive) 1 160 161
922 {
923 # contstucted $archive wrongly, or it disappeared between when the
924 # caller stat'd it and now (e.g. a background mime-helper writing into
925 # the same destdir).
926 # Check this BEFORE incrementing recursion_level: an early return that
927 # leaves the counter incremented makes the outer call's
928 # --recursion_level == 0 check miss, so the JSON epilog never runs and
929 # the log file is left as an unclosed object — invalid JSON.
930 1 push @{$self->{error}}, "unpack('$archive'): no such file or directory; ";
931 1 return 1;
932 }
933  
934 160 my $start_time = time;
935 160 ▶ if ($self->{recursion_level}++ == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"recursion_level"}++ == 0) 10 150 160
936 {
937 10 ▶ print STDERR "unpack: starting...\n" if $self->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 10 10
938 ## State that needs to be reset when (re)starting goes in here.
939 #
940 # CAUTION: recursion_level decrements again, as we return from unpack()
941 # how do we assert, that this code only runs at the start,
942 # and not once again at the end?
943 10 $self->{inside_archives} = 0;
944 10 $self->{total_bytes} = 0; # for the opt-in max_total_bytes cap
945 10 $self->{_capped} = 0;
946 10 ▶ $self->{json} ||= JSON->new()->ascii(1); # used often, create it unconditionally here and once.
Condition33%1/3
$self->{"json"} ||= "JSON"->new->ascii(1)
l!l&&r!l&&!r
0100
MC/DC0%0/2
$self->{"json"} ||= "JSON"->new->ascii(1)
$self->{"json"} "JSON"->new->ascii(1)
Truth table33%1/3
A ||= B
A= $self->{"json"}B= "JSON"->new->ascii(1)
ABresult
1X1
011
000
947 10 $self->{iput} = $archive;
948 10 $self->{progress_tstamp} = $start_time;
949 10 ▶ ($self->{input_dir}, $self->{input_file}) = ($1, $2) if $archive =~ m{^(.*)/([^/]*)$};
Branch: 1 branch
BranchTrueFalseTotal
if $archive =~ m[^(.*)/([^/]*)$] 10 0 10
950  
951 10 ▶ if ($self->{log_type} eq 'plain')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log_type'} eq 'plain') { } 0 10 10
952 {
953 # pass
954 }
955 else
956 {
957 # logfile prolog
958 10 my $prolog = {destdir=>$self->{destdir}, fu=>$VERSION, pid=>$$, input => $archive, start => scalar localtime};
959 10 ▶ $prolog->{params} = $self->{log_params} if keys %{$self->{log_params}};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"log_params"};} 0 10 10
960 10 my $s = $self->{json}->encode($prolog);
961 10 $s =~ s@}$@, "unpacked":{\n@;
962 10 $self->log($s);
963 }
964 }
965  
966 160 ▶ unless ($self->{input_dir})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"input_dir"}) 0 160 160
967 {
968 0 push @{$self->{error}}, "unpack('$archive'); internal error: no {input_dir}";
969 0 $self->{recursion_level}--;
970 0 return 1;
971 }
972  
973 160 my ($in_dir, $in_file) = ('/', '');
974 160 ▶ ($in_dir, $in_file) = ($1, $2) if $archive =~ m{^(.*/)([^/]*)$};
Branch: 1 branch
BranchTrueFalseTotal
if $archive =~ m[^(.*/)([^/]*)$] 160 0 160
975  
976 160 my $inside_destdir = 1;
977 160 my $subdir = $in_dir; # remainder after stripping $orig_archive_prefix / $self->{destdir}
978 160 ▶ unless ($subdir =~ s{^\Q$self->{destdir}\E/+}{})
Branch: 1 branch
BranchTrueFalseTotal
unless ($subdir =~ s[^\Q$self->{"destdir"}\E/+][]) 160 0 160
979 {
980 160 $inside_destdir = 0;
981 160 ▶ die "$archive path escaped. Neither inside original $self->{input_dir} nor inside destdir='$self->{destdir}'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $subdir =~ s[^\Q$self->{"input_dir"}\E/+][] 0 160 160
982 unless $subdir =~ s{^\Q$self->{input_dir}\E/+}{};
983 }
984  
985 160 ▶ print STDERR "unpack: r=$self->{recursion_level} in_dir=$in_dir, in_file=$in_file, destdir=$destdir\n" if $self->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 160 160
986  
987 160 my @missing_unpacker;
988  
989 160 ▶ if ($self->{progress_tstamp} + 10 < $start_time)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"progress_tstamp"} + 10 < $start_time) 0 160 160
990 {
991 0 ▶ printf "T: %d files ...\n", $self->{file_count}||0;
Condition0%0/2
$self->{'file_count'} || 0
l!l
00
MC/DC0%0/1
$self->{'file_count'} || 0
$self->{'file_count'}
Truth table0%0/2
A
A= $self->{'file_count'}
Aresult
11
00
992 0 $self->{progress_tstamp} = $start_time;
993 }
994  
995 160 ▶ if (-d $archive)
Branch: 2 branches
BranchTrueFalseTotal
if (-d $archive) { } 12 148 160
elsif (-f $archive) { } 148 0 148
996 {
997 12 $self->_chmod_add($archive, @{$self->{readable_dir_modes}});
998 12 ▶ if (opendir DIR, $archive)
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, $archive) { } 12 0 12
999 {
1000 12 ▶ my @f = sort grep { $_ ne '.' && $_ ne '..' } readdir DIR;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
1212154
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
1001 12 closedir DIR;
1002 12 ▶ print STDERR "dir = @f\n" if $self->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 12 12
1003 12 for my $f (@f)
1004 {
1005 154 ▶ if ($self->{exclude}{re} && $f =~ m{$self->{exclude}{re}})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exclude"}{"re"} and $f =~ /$self->{"exclude"}{"re"}/) 19 135 154
Condition100%3/3
$self->{"exclude"}{"re"} and $f =~ /$self->{"exclude"}{"re"}/
!ll&&!rl&&r
2011519
MC/DC100%2/2
$self->{"exclude"}{"re"} and $f =~ /$self->{"exclude"}{"re"}/
$self->{"exclude"}{"re"} $f =~ /$self->{"exclude"}{"re"}/
Truth table100%3/3
A and B
A= $self->{"exclude"}{"re"}B= $f =~ /$self->{"exclude"}{"re"}/
ABresult
0X0
100
111
1006 {
1007 19 $self->{skipped}{exclude}++;
1008 }
1009 154 my $new_in = "$archive/$f";
1010 ## if $archive is $inside_destdir, then $archive is normally indentical to $destdir.
1011 ## ($inside_destdir means inside $self->{destdir}, actually)
1012 154 ▶ my $new_destdir = $destdir; $new_destdir .= "/$f" if -d $new_in;
Branch: 1 branch
BranchTrueFalseTotal
if -d $new_in 3 151 154
1013 154 my $symlink_to_skip = -l $new_in;
1014 154 ▶ my $dangeous_symlink = $self->{inside_archives} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'inside_archives'} ? : 0 154 154
1015 154 ▶ if ($symlink_to_skip and ($self->{follow_file_symlinks} > $dangeous_symlink))
Branch: 1 branch
BranchTrueFalseTotal
if ($symlink_to_skip and $self->{"follow_file_symlinks"} > $dangeous_symlink) 0 154 154
Condition33%1/3
$symlink_to_skip and $self->{"follow_file_symlinks"} > $dangeous_symlink
!ll&&!rl&&r
15400
MC/DC0%0/2
$symlink_to_skip and $self->{"follow_file_symlinks"} > $dangeous_symlink
$symlink_to_skip $self->{"follow_file_symlinks"} > $dangeous_symlink
Truth table33%1/3
A and B
A= $symlink_to_skipB= $self->{"follow_file_symlinks"} > $dangeous_symlink
ABresult
0X0
100
111
1016 {
1017 0 ▶ $symlink_to_skip = 0 if -f $new_in;
Branch: 1 branch
BranchTrueFalseTotal
if -f $new_in 0 0 0
1018 # directory and dead symlinks we always skip.
1019 # directory symlinks could cause us to recurse out of the current tree.
1020 }
1021  
1022 154 ▶ if ($symlink_to_skip)
Branch: 2 branches
BranchTrueFalseTotal
if ($symlink_to_skip) { } 0 154 154
elsif (-f $new_in or -d _) { } 154 0 154
1023 {
1024 # test -l first, as -f could be also true here...
1025 0 ▶ print STDERR "symlink $new_in: skipped\n" if $self->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
1026 0 $self->{skipped}{symlink}++;
1027 }
1028 ▶ elsif (-f $new_in or -d _)
Condition66%2/3
-f $new_in or -d _
l!l&&r!l&&!r
15130
MC/DC0%0/2
-f $new_in or -d _
-f $new_in -d _
Truth table66%2/3
A or B
A= -f $new_inB= -d _
ABresult
1X1
011
000
1029 {
1030 154 $self->_safe_unpack($new_in, $new_destdir);
1031 }
1032 else
1033 {
1034 0 ▶ print STDERR "special file $new_in: skipped\n" if $self->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
1035 0 $self->{skipped}{device_node}++;
1036 }
1037 154 $self->{progress_tstamp} = time;
1038 }
1039 }
1040 else
1041 {
1042 0 push @{$self->{error}}, "unpack dir ($archive) failed: $!";
1043 }
1044 }
1045 elsif (-f $archive)
1046 {
1047 148 ▶ if ($self->_not_excluded($subdir, $in_file) and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_not_excluded($subdir, $in_file) and not defined $self->{"done"}{$archive}) 91 57 148
Condition66%2/3
$self->_not_excluded($subdir, $in_file) and not defined $self->{"done"}{$archive}
!ll&&!rl&&r
57091
MC/DC50%1/2
$self->_not_excluded($subdir, $in_file) and not defined $self->{"done"}{$archive}
$self->_not_excluded($subdir, $in_file) not defined $self->{"done"}{$archive}
Truth table66%2/3
A and not(B)
A= $self->_not_excluded($subdir, $in_file)B= not defined $self->{"done"}{$archive}
ABresult
0X0
100
111
1048 !defined($self->{done}{$archive}))
1049 {
1050 91 $self->_chmod_add($archive, @{$self->{readable_file_modes}});
1051 91 ▶ $self->{total_bytes} += (-s $archive || 0); # for the opt-in max_total_bytes cap
Condition50%1/2
-s $archive || 0
l!l
910
MC/DC0%0/1
-s $archive || 0
-s $archive
Truth table50%1/2
A
A= -s $archive
Aresult
11
00
1052  
1053 91 my $m = $self->mime($archive);
1054 0 my ($h, $more) = $self->find_mime_helper($m);
1055 0 my $data = { mime => $m->[0] };
1056 0 ▶ if ($more)
Branch: 1 branch
BranchTrueFalseTotal
if ($more) 0 0 0
1057 {
1058 0 $data->{found} = $more;
1059 0 ▶ push @missing_unpacker, @{$more->{missing}} if $more->{missing};
Branch: 1 branch
BranchTrueFalseTotal
if $more->{"missing"} 0 0 0
1060 }
1061  
1062 0 ▶ if ($m->[0] eq 'text/plain' or !$h)
Branch: 1 branch
BranchTrueFalseTotal
if ($m->[0] eq 'text/plain' or not $h) { } 0 0 0
Condition0%0/3
$m->[0] eq "text/plain" or not $h
l!l&&r!l&&!r
000
MC/DC0%0/2
$m->[0] eq "text/plain" or not $h
$m->[0] eq "text/plain" not $h
Truth table0%0/3
A or not(B)
A= $m->[0] eq "text/plain"B= not $h
ABresult
1X1
011
000
1063 {
1064 # not really an archive.
1065 0 ▶ unless ($archive =~ m{^\Q$self->{destdir}\E/})
Branch: 1 branch
BranchTrueFalseTotal
if ($archive =~ m[^\Q$self->{'destdir'}\E/]) { } 0 0 0
1066 {
1067 0 ▶ mkpath($destdir) unless $self->{no_op};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_op"} 0 0 0
1068 0 my $destdir_in_file;
1069 0 ▶ $destdir_in_file = $1 if "$destdir/$in_file" =~ m{^(.*)$}s; # brute force untaint
Branch: 1 branch
BranchTrueFalseTotal
if ("$destdir/$in_file") =~ /^(.*)$/s 0 0 0
1070  
1071 0 ▶ if (-e "$destdir_in_file")
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$destdir_in_file") 0 0 0
1072 {
1073 0 ▶ print STDERR "unpack copy in: $destdir_in_file already exists, " if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1074 0 $destdir = File::Temp::tempdir($TMPDIR_TEMPL, DIR => $destdir);
1075 0 ▶ $destdir_in_file = $1 if "$destdir/$in_file" =~ m{^(.*)$}s; # brute force untaint
Branch: 1 branch
BranchTrueFalseTotal
if ("$destdir/$in_file") =~ /^(.*)$/s 0 0 0
1076 0 ▶ print STDERR "using $destdir_in_file instead.\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1077 }
1078 0 ▶ $data->{error} = "copy($archive): $!" unless File::Copy::copy($archive, $destdir_in_file);
Branch: 1 branch
BranchTrueFalseTotal
unless File::Copy::copy($archive, $destdir_in_file) 0 0 0
1079 0 $self->logf($destdir_in_file => $data);
1080 }
1081 else
1082 {
1083 0 $self->logf($archive => $data);
1084 }
1085 0 $self->{file_count}++;
1086 }
1087 else
1088 {
1089 # really an archive.
1090 0 ▶ if ($self->{archive_name_as_dir})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"archive_name_as_dir"}) 0 0 0
1091 {
1092 0 ▶ print STDERR "archive_name_as_dir: expanding destdir $destdir\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1093 0 $destdir = _unused_pathname($destdir, $in_file);
1094 0 ▶ print STDERR "archive_name_as_dir: to $destdir\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1095 }
1096 0 ▶ mkpath($destdir) unless $self->{no_op};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_op"} 0 0 0
1097 0 ▶ $self->{configdir} = $self->_prep_configdir() unless exists $self->{configdir};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"configdir"} 0 0 0
1098  
1099 ## new_name is a suggestion for the mime_helper only.
1100 0 my $new_name = $in_file;
1101  
1102 # Either shorten the name from e.g. foo.txt.bz2 to foo.txt or append
1103 # something: foo.pdf to foo.pdf._;
1104 # Normally a suffix is appended by '.', but we also see '-' or '_' in real life.
1105 0 ▶ unless ($h->{suffix_re} and $new_name =~ s{[\._-]$h->{suffix_re}(?:\._\d*)?$}{}i)
Branch: 1 branch
BranchTrueFalseTotal
unless ($h->{"suffix_re"} and $new_name =~ s/[\._-]$h->{"suffix_re"}(?:\._\d*)?$//i) 0 0 0
Condition0%0/3
$h->{"suffix_re"} and $new_name =~ s/[\._-]$h->{"suffix_re"}(?:\._\d*)?$//i
!ll&&!rl&&r
000
MC/DC0%0/2
$h->{"suffix_re"} and $new_name =~ s/[\._-]$h->{"suffix_re"}(?:\._\d*)?$//i
$h->{"suffix_re"} $new_name =~ s/[\._-]$h->{"suffix_re"}(?:\._\d*)?$//i
Truth table0%0/3
A and B
A= $h->{"suffix_re"}B= $new_name =~ s/[\._-]$h->{"suffix_re"}(?:\._\d*)?$//i
ABresult
0X0
100
111
1106 {
1107 # avoid unary notation of recursion couning. There may be a 256 char limit per
1108 # directory entry. Start counting in decimal, if two or more.
1109 # Hmm, the /e modifier is not mentioned in perlre, but it works. Is it deprecated??
1110 0 $new_name .= "._";
1111 0 $new_name =~ s{\._\._$}{\._2};
1112 0 $new_name =~ s{\._(\d+)\._$}{ "._".($1+1) }e;
1113 }
1114  
1115 ## if consumer of logf wants to do progress indication himself,
1116 ## then tell him what we do before we start. (Our timer tick code may be an alternative...)
1117 #
1118 # if ($archive =~ m{^\Q$self->{destdir}\E})
1119 # {
1120 # $self->logf($archive => { unpacking => $h->{fmt_p} });
1121 # }
1122  
1123 my ($unpacked, $diag) =
1124 $self->_run_mime_helper($h, $archive, $new_name, $destdir,
1125 0 $m->[0], $m->[2], $self->{configdir});
1126  
1127 0 ▶ unless (ref $unpacked or -e $unpacked or readlink($unpacked))
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $unpacked or -e $unpacked or readlink $unpacked) 0 0 0
Condition0%0/6
ref $unpacked or -e $unpacked or readlink $unpacked
l!l&&r!l&&!r
000
ref $unpacked or -e $unpacked
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $unpacked or -e $unpacked or readlink $unpacked
ref $unpacked -e $unpacked readlink $unpacked
Truth table0%0/4
A or B or C
A= ref $unpackedB= -e $unpackedC= readlink $unpacked
ABCresult
1XX1
01X1
0011
0000
1128 {
1129 0 warn("archive=$archive, new_name=$new_name\n");
1130 0 die("assert -e '$unpacked'")
1131 }
1132  
1133 0 ▶ return 0 if $self->{no_op};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_op"} 0 0 0
1134 0 ▶ if (ref $unpacked)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $unpacked) { } 0 0 0
elsif (readlink $unpacked or '' eq $archive) { } 0 0 0
1135 {
1136 # a ref here means, something went wrong.
1137 0 $data->{failed} = $h->{fmt_p};
1138 0 $data->{error} = $unpacked->{error};
1139 0 ▶ $data->{stderr} = $unpacked->{stderr} if defined $unpacked->{stderr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $unpacked->{"stderr"} 0 0 0
1140 0 $self->logf($archive => $data);
1141 0 $self->{file_count}++;
1142 0 $self->{helper_errors}++;
1143 }
1144 ▶ elsif (readlink($unpacked)||'' eq $archive)
Condition0%0/3
readlink $unpacked or "" eq $archive
l!l&&r!l&&!r
000
MC/DC0%0/2
readlink $unpacked or "" eq $archive
readlink $unpacked "" eq $archive
Truth table0%0/3
A or B
A= readlink $unpackedB= "" eq $archive
ABresult
1X1
011
000
1145 {
1146 # a symlink backwards means, there is nothing to unpack here. take it as is.
1147 0 unlink $unpacked;
1148 0 ▶ rmdir $destdir if $self->{archive_name_as_dir}; # now an empty dir.
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"archive_name_as_dir"} 0 0 0
1149 0 $data->{passed} = $h->{name};
1150 0 $data->{input} = $self->loggable_pathname($archive);
1151 0 $data->{cmd} = $h->{fmt_p};
1152 {
1153 0 local $Data::Dumper::Terse = 1;
1154 0 local $Data::Dumper::Indent = 0;
1155 0 ▶ $data->{diag} = Dumper $diag if $diag;
Branch: 1 branch
BranchTrueFalseTotal
if $diag 0 0 0
1156 }
1157  
1158 0 ▶ if ($archive =~ m{^\Q$self->{destdir}\E})
Branch: 1 branch
BranchTrueFalseTotal
if ($archive =~ /^\Q$self->{'destdir'}\E/) { } 0 0 0
1159 {
1160 # if inside, we just flag it done and log it.
1161 0 $self->{done}{$archive} = $archive;
1162 0 $self->logf($archive => $data);
1163 }
1164 else
1165 {
1166 # if the archive itself was outside destdir,
1167 # we copy it in, flag it done there, and log it here.
1168 0 ▶ if (File::Copy::copy($archive, $unpacked))
Branch: 1 branch
BranchTrueFalseTotal
if (File::Copy::copy($archive, $unpacked)) { } 0 0 0
1169 {
1170 0 $self->{done}{$archive} = $unpacked;
1171 0 $self->logf($unpacked => $data);
1172 }
1173 else
1174 {
1175 0 $data->{error} = "copy($archive, $unpacked): $!";
1176 0 $self->logf($archive => $data);
1177 }
1178 }
1179 0 $self->{file_count}++;
1180 }
1181 else
1182 {
1183 # normal case: mime helper placed all
1184 # in a directory (or file) called $unpacked
1185  
1186 0 $self->{done}{$archive} = $unpacked;
1187 0 $data->{cmd} = $h->{fmt_p};
1188 0 $data->{unpacked} = $self->loggable_pathname($unpacked);
1189 0 $self->logf($archive => $data);
1190 0 $self->{file_count}++;
1191 0 $self->{inside_archives}++;
1192  
1193 0 my $newdestdir = $unpacked;
1194 0 ▶ $newdestdir =~ s{/+[^/]+}{} unless -d $newdestdir; # make sure it is a directory
Branch: 1 branch
BranchTrueFalseTotal
unless -d $newdestdir 0 0 0
1195 0 ▶ $newdestdir = $destdir unless $newdestdir =~ m{^\Q$self->{destdir}\E/}; # make sure it does not escape
Branch: 1 branch
BranchTrueFalseTotal
unless $newdestdir =~ m[^\Q$self->{"destdir"}\E/] 0 0 0
1196 0 ▶ if ($self->{one_shot})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'one_shot'}) { } 0 0 0
1197 {
1198 0 local $self->{mime_orcish};
1199 0 local $self->{mime_helper};
1200  
1201 0 $self->_safe_unpack($unpacked, $newdestdir);
1202 }
1203 else
1204 {
1205 0 $self->_safe_unpack($unpacked, $newdestdir);
1206 }
1207 0 $self->{progress_tstamp} = time;
1208 0 $self->{inside_archives}--;
1209  
1210 # If the archive was an intermediate file created inside destdir
1211 # (e.g. foo.tar produced by unpacking foo.tar.gz), remove it now
1212 # that its contents have been recursively unpacked.
1213 0 ▶ if ($archive =~ m{^\Q$self->{destdir}\E/} and -f $archive and !-l $archive)
Branch: 1 branch
BranchTrueFalseTotal
if ($archive =~ m[^\Q$self->{"destdir"}\E/] and -f $archive and not -l $archive) 0 0 0
Condition0%0/6
$archive =~ m[^\Q$self->{"destdir"}\E/] and -f $archive and not -l $archive
!ll&&!rl&&r
000
$archive =~ m[^\Q$self->{"destdir"}\E/] and -f $archive
!ll&&!rl&&r
000
MC/DC0%0/3
$archive =~ m[^\Q$self->{"destdir"}\E/] and -f $archive and not -l $archive
$archive =~ m[^\Q$self->{"destdir"}\E/] -f $archive not -l $archive
Truth table0%0/4
A and B and not(C)
A= $archive =~ m[^\Q$self->{"destdir"}\E/]B= -f $archiveC= not -l $archive
ABCresult
0XX0
10X0
1100
1111
1214 {
1215 0 unlink $archive;
1216 }
1217 }
1218 }
1219 }
1220 }
1221 else
1222 {
1223 0 $self->logf($archive => { "skipped" => "special file"});
1224 0 $self->{file_count}++;
1225 }
1226  
1227 69 ▶ if (--$self->{recursion_level} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"recursion_level"} == 0) 9 60 69
1228 {
1229 9 ▶ if ($self->{log_type} eq 'plain')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log_type'} eq 'plain') { } 0 9 9
1230 {
1231 0 for my $m (@missing_unpacker)
1232 {
1233 0 $self->log("# missing unpacker: $m\n");
1234 }
1235 0 for my $s (sort keys %{$self->{skipped}})
1236 {
1237 0 $self->log("# skipped: $s: $self->{skipped}{$s}\n");
1238 }
1239 0 ▶ $self->log("# error: ".join('; ', @{$self->{error}})."\n") if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
1240 }
1241 else
1242 {
1243 9 my $epilog = {end => scalar localtime, sec => time-$start_time };
1244 9 ▶ $epilog->{skipped} = $self->{skipped} if $self->{skipped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"skipped"} 3 6 9
1245 9 ▶ $epilog->{error}{msg} = $self->{error} if $self->{error}; # just in case some errors were non-fatal.
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 8 1 9
1246 9 ▶ $epilog->{error}{helper} = $self->{helper_errors} if $self->{helper_errors}; # counting
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"helper_errors"} 0 9 9
1247 9 ▶ $epilog->{missing_unpacker} = \@missing_unpacker if @missing_unpacker;
Branch: 1 branch
BranchTrueFalseTotal
if @missing_unpacker 0 9 9
1248 9 my $s = $self->{json}->encode($epilog);
1249  
1250 9 $s =~ s@^{@\n},@;
1251 9 $self->log($s . "\n");
1252 }
1253  
1254 9 ▶ if ($self->{lfp} ne $self->{logfile})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lfp"} ne $self->{"logfile"}) 9 0 9
1255 {
1256 9 ▶ close $self->{lfp} or carp "logfile write ($self->{logfile}) failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $self->{"lfp"} or carp(qq[logfile write ($self->{"logfile"}) failed: $!\n]) 9 0 9
1257 }
1258 9 delete $self->{lfp};
1259 9 delete $self->{lfp_printed};
1260 }
1261  
1262 # FIXME: should return nonzero if we had any unrecoverable errors.
1263 69 ▶ return $self->{error} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'error'} ? : 53 16 69
1264 }
1265  
1266 # Try a few modes to add to the current permission bits.
1267 # The first mode that succeeds ends the list.
1268 sub _chmod_add
1269 103 ▶{
Subroutine _chmod_add: called CC=4 SCAR=14.3
1270 103 my ($self, $file, @modes) = @_;
1271 103 ▶ $file = $1 if $file =~ m{^(.*)$}m;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^(.*)$/m 103 0 103
1272 103 my $perm = (stat $file)[2] & 07777;
1273 103 for my $m (@modes)
1274 {
1275 103 ▶ last if chmod($perm|$m, $file); # may or may not succeed. Harmless here.
Branch: 1 branch
BranchTrueFalseTotal
if chmod $perm | $m, $file 103 0 103
1276 }
1277 }
1278  
1279 =head2 run
1280  
1281 $u->run([argv0, ...], @redir, ... { init => sub ..., in, out, err, watch, every, prog, ... })
1282  
1283 A general purpose fork-exec wrapper, based on IPC::Run. STDIN is closed, unless you specify
1284 an C<< in => >> as described in IPC::Run. STDERR and STDOUT are both printed to
1285 STDOUT, prefixed with 'E: ' and 'O: ' respectively, unless you specify C<< out => >>,
1286 C<< err => >>, or C<< out_err => >> ... for both.
1287  
1288 Using redirection operators in @redir takes precedence over the above in/out/err
1289 redirections. See also L<IPC::Run>. If you use the options in/out/err, you should
1290 restrict your redirection operators to the forms '<', '0<', '1>', '2>', or '>&' due
1291 to limitations in the precedence logic. Piping via '|' is properly recognized,
1292 but background execution '&' may confuse the precedence logic.
1293  
1294 This C<run> method is completly independent of the rest of File::Unpack2. It works both
1295 as a static function and as a method call.
1296 It is used internally by C<unpack>, but is exported to be of use elsewhere.
1297  
1298 Init is run after construction of redirects. Calling chdir() in init thus has no
1299 effect on redirects with relative paths.
1300  
1301 Return value in scalar context is the first nonzero result code, if any. In list context
1302 all return values are returned.
1303 =cut
1304  
1305 sub run
1306 0 ▶{
Subroutine run: not called CC=48 SCAR=77.6
Pod documented
1307 0 ▶ shift if ref $_[0] ne 'ARRAY'; # toss $self object handle.
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "ARRAY" 0 0 0
1308 0 my (@cmd) = @_;
1309 0 my $opt;
1310 0 ▶ $opt = pop @cmd if ref $cmd[-1] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $cmd[-1] eq "HASH" 0 0 0
1311  
1312 0 my $cmdname = $cmd[0][0]; $cmdname =~ s{^.*/}{};
1313  
1314 # run the command with
1315 # - STDIN closed, unless you specify an { in => ... }
1316 # - STDERR and STDOUT printed prefixed with 'E: ', 'O: ' to STDOUT,
1317 # unless you specify out =>, err =>, or out_err => ... for both.
1318 0 ▶ $opt->{in} ||= \undef;
Condition0%0/2
$opt->{"in"} ||= \(undef)
l!l
00
MC/DC0%0/1
$opt->{"in"} ||= \(undef)
$opt->{"in"}
Truth table0%0/2
A
A= $opt->{"in"}
Aresult
11
00
1319 0 ▶ $opt->{out} ||= $opt->{out_err};
Condition0%0/3
$opt->{"out"} ||= $opt->{"out_err"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"out"} ||= $opt->{"out_err"}
$opt->{"out"} $opt->{"out_err"}
Truth table0%0/3
A ||= B
A= $opt->{"out"}B= $opt->{"out_err"}
ABresult
1X1
011
000
1320 0 ▶ $opt->{err} ||= $opt->{out_err};
Condition0%0/3
$opt->{"err"} ||= $opt->{"out_err"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"err"} ||= $opt->{"out_err"}
$opt->{"err"} $opt->{"out_err"}
Truth table0%0/3
A ||= B
A= $opt->{"err"}B= $opt->{"out_err"}
ABresult
1X1
011
000
1321 0 ▶ $opt->{out} ||= sub { print "O: ($cmdname) @_\n"; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$opt->{"out"} ||= sub { print "O: ($cmdname) @_\n"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"out"} ||= sub { print "O: ($cmdname) @_\n"; }
$opt->{"out"} sub { print "O: ($cmdname) @_\n"; }
Truth table0%0/3
A ||= B
A= $opt->{"out"}B= sub { print "O: ($cmdname) @_\n"; }
ABresult
1X1
011
000
1322 0 ▶ $opt->{err} ||= sub { print "E: ($cmdname) @_\n"; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$opt->{"err"} ||= sub { print "E: ($cmdname) @_\n"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"err"} ||= sub { print "E: ($cmdname) @_\n"; }
$opt->{"err"} sub { print "E: ($cmdname) @_\n"; }
Truth table0%0/3
A ||= B
A= $opt->{"err"}B= sub { print "E: ($cmdname) @_\n"; }
ABresult
1X1
011
000
1323  
1324 # When a watchdog is active: (1) treat ANY helper output as progress - pipes/sockets have no fd
1325 # "pos" for the stall detector to see, so a helper streaming to a pipe would otherwise look
1326 # stalled and be killed; (2) run each helper in its own process group so _kill_family can reap the
1327 # whole family (including grandchildren) instead of leaving a pipe-holder that hangs finish().
1328 0 ▶ if ($opt->{stall_timeout} or $opt->{helper_timeout})
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"stall_timeout"} or $opt->{"helper_timeout"}) 0 0 0
Condition0%0/3
$opt->{"stall_timeout"} or $opt->{"helper_timeout"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"stall_timeout"} or $opt->{"helper_timeout"}
$opt->{"stall_timeout"} $opt->{"helper_timeout"}
Truth table0%0/3
A or B
A= $opt->{"stall_timeout"}B= $opt->{"helper_timeout"}
ABresult
1X1
011
000
1329 {
1330 0 for my $ch (qw(out err))
1331 {
1332 0 ▶ next unless ref $opt->{$ch} eq 'CODE'; # leave file/handle redirects (e.g. '/dev/null') alone
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt->{$ch} eq "CODE" 0 0 0
1333 0 my $orig = $opt->{$ch};
1334 0 ▶ $opt->{$ch} = sub { $opt->{last_progress} = time; $orig->(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1335 }
1336 0 ▶ $opt->{init} ||= sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1337 # Optional safeguard (armed only where syscall.ph gave us the prctl number - see $SYS_PRCTL):
1338 # PR_SET_PDEATHSIG(SIGKILL) makes the kernel kill this helper the instant its parent worker
1339 # dies, so a stuck helper (e.g. a malicious cyclic-hardlink tar spinning on stat()/linkat())
1340 # is not orphaned to ppid==1 to spin forever when the worker is force-stopped mid-unpack
1341 # (Minion "stop job", a hard restart, a crash) - the case the stall watchdog can't cover
1342 # because it dies with the worker. PR_SET_PDEATHSIG is 1 on every Linux arch. Where the
1343 # syscall number was unresolved this is skipped; then that narrow orphan window remains and
1344 # such orphans must be cleaned up out of band (or the worker stopped via systemd
1345 # KillMode=control-group, which reaps the whole cgroup).
1346 0 ▶ eval { syscall($SYS_PRCTL, 1, POSIX::SIGKILL()) if $SYS_PRCTL; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $File::Unpack2::SYS_PRCTL 0 0 0
1347 # ... and put each helper in its own process group so _kill_family can reap the whole family
1348 # (grandchildren included) while we are alive.
1349 0 setpgrp(0, 0); # builtin; POSIX::setpgrp is unimplemented on modern perl
1350 0 ▶ };
Condition0%0/3
$opt->{"init"} ||= sub { eval { do { syscall $SYS_PRCTL, 1, 9 if $SYS_PRCTL; '???' } }; setpgrp 0, 0; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"init"} ||= sub { eval { do { syscall $SYS_PRCTL, 1, 9 if $SYS_PRCTL; '???' } }; setpgrp 0, 0; }
$opt->{"init"} sub { eval { do { syscall $SYS_PRCTL, 1, 9 if $SYS_PRCTL; '???' } }; setpgrp 0, 0; }
Truth table0%0/3
A ||= B
A= $opt->{"init"}B= sub { eval { do { syscall $SYS_PRCTL, 1, 9 if $SYS_PRCTL; '???' } }; setpgrp 0, 0; }
ABresult
1X1
011
000
1351 }
1352  
1353 0 my $has_i_redir = 0;
1354 0 my $has_o_redir = 0;
1355 0 my $has_e_redir = 0;
1356  
1357 ## The ugly truth is, there might be multiple commands with pipes.
1358 ## We need to provide all of them with the proper redirects.
1359 ## A command that pipes somewhere else, has_o_redir outbound through the pipe.
1360 ## A command that is piped into, has_i_redir inbound from the pipe.
1361 0 my @run = ();
1362  
1363  
1364 0 for my $c (@cmd)
1365 {
1366 0 ▶ if (ref $c)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) 0 0 0
1367 {
1368 0 push @run, $c;
1369  
1370 # put init early, so that it is run, before any IO redirects access relative paths.
1371 0 ▶ push @run, init => $opt->{init} if $opt->{init};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"init"} 0 0 0
1372 0 next; # don't look into argvs, but
1373 }
1374 # look only into redirection operators
1375 0 ▶ $has_i_redir++ if $c =~ m{^0?<};
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /^0?</ 0 0 0
1376 0 ▶ $has_o_redir++ if $c =~ m{^1?>};
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /^1?>/ 0 0 0
1377 0 ▶ $has_e_redir++ if $c =~ m{^(?:2>|>&$)};
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /^(?:2>|>&$)/ 0 0 0
1378 0 ▶ if ($c eq '|')
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "|") 0 0 0
1379 {
1380 0 ▶ push @run, '0<', $opt->{in} unless $has_i_redir;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_i_redir 0 0 0
1381 0 $has_i_redir = 'piped';
1382 0 ▶ push @run, "2>", $opt->{err} unless $has_e_redir;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_e_redir 0 0 0
1383 0 $has_e_redir = $has_o_redir = 0;
1384 }
1385 0 push @run, $c; # $1 if $c =~ m{^(.*)$}s; # brute force untaint
1386 }
1387  
1388 0 ▶ push @run, '0<', $opt->{in} unless $has_i_redir;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_i_redir 0 0 0
1389 0 ▶ push @run, "1>", $opt->{out} unless $has_o_redir;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_o_redir 0 0 0
1390 0 ▶ push @run, "2>", $opt->{err} unless $has_e_redir;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_e_redir 0 0 0
1391  
1392 0 my $t;
1393 0 ▶ $t = IPC::Run::timer($opt->{every}-0.6) if $opt->{every};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"every"} 0 0 0
1394 0 ▶ push @run, $t if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 0 0 0
1395  
1396 0 ▶ $run[0][0] = $1 if $run[0][0] =~ m{^(.*)$}s;
Branch: 1 branch
BranchTrueFalseTotal
if $run[0][0] =~ /^(.*)$/s 0 0 0
1397 0 ▶ push @run, debug => $opt->{debug} if $opt->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"debug"} 0 0 0
1398 0 my $h = eval { IPC::Run::start @run; };
1399 0 ▶ return wantarray ? (undef, $@) : undef unless $h;
Branch: 2 branches
BranchTrueFalseTotal
unless $h 0 0 0
wantarray ? : 0 0 0
1400  
1401 # Each helper stage put itself in its own process group (setpgrp in init), so its PID is its
1402 # pgid. Capture them now: killing these groups later reaps grandchildren even after a stage exits
1403 # and its children reparent to init (they keep the group). Guarded - falls back to the live
1404 # descendant walk if IPC::Run's internals ever change.
1405 0 my @pgids;
1406 0 ▶ if ($opt->{stall_timeout} or $opt->{helper_timeout})
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"stall_timeout"} or $opt->{"helper_timeout"}) 0 0 0
Condition0%0/3
$opt->{"stall_timeout"} or $opt->{"helper_timeout"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"stall_timeout"} or $opt->{"helper_timeout"}
$opt->{"stall_timeout"} $opt->{"helper_timeout"}
Truth table0%0/3
A or B
A= $opt->{"stall_timeout"}B= $opt->{"helper_timeout"}
ABresult
1X1
011
000
1407 {
1408 0 ▶ @pgids = eval { grep { $_ and $_ > 1 } map { $_->{PID} } @{$h->{KIDS} || []} };
Condition0%0/5
$_ && $_ > 1
!ll&&!rl&&r
000
$h->{'KIDS'} || []
l!l
00
MC/DC0%0/2
$_ && $_ > 1
$_ $_ > 1
MC/DC0%0/1
$h->{'KIDS'} || []
$h->{'KIDS'}
Truth table0%0/3
A && B
A= $_B= $_ > 1
ABresult
0X0
100
111
Truth table0%0/2
A
A= $h->{'KIDS'}
Aresult
11
00
1409 }
1410  
1411 0 my $killed = 0;
1412 0 my $started = time;
1413 0 ▶ while ($h->pumpable)
Branch: 1 branch
BranchTrueFalseTotal
if ($h->pumpable) 0 0 0
1414 {
1415 # eval {} guards against 'process ended prematurely' errors.
1416 # This happens on very fast commands, despite pumpable().
1417 0 eval { $h->pump };
1418 0 ▶ if ($t && $t->is_expired)
Branch: 1 branch
BranchTrueFalseTotal
if ($t and $t->is_expired) 0 0 0
Condition0%0/3
$t and $t->is_expired
!ll&&!rl&&r
000
MC/DC0%0/2
$t and $t->is_expired
$t $t->is_expired
Truth table0%0/3
A and B
A= $tB= $t->is_expired
ABresult
0X0
100
111
1419 {
1420 0 $t->{has_fired}++;
1421 0 ▶ $opt->{prog}->($h, $opt) if $opt->{prog};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"prog"} 0 0 0
1422 # Absolute per-helper cap (opt-in) catches "slow bombs" that always make progress and so
1423 # never trip the no-progress stall watchdog. Off by default.
1424 0 ▶ if (!$opt->{stalled} and $opt->{helper_timeout} and time - $started >= $opt->{helper_timeout})
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt->{"stalled"} and $opt->{"helper_timeout"} and time - $started >= $opt->{"helper_timeout"}) 0 0 0
Condition0%0/6
not $opt->{"stalled"} and $opt->{"helper_timeout"} and time - $started >= $opt->{"helper_timeout"}
!ll&&!rl&&r
000
not $opt->{"stalled"} and $opt->{"helper_timeout"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $opt->{"stalled"} and $opt->{"helper_timeout"} and time - $started >= $opt->{"helper_timeout"}
not $opt->{"stalled"} $opt->{"helper_timeout"} time - $started >= $opt->{"helper_timeout"}
Truth table0%0/4
not(A) and B and C
A= not $opt->{"stalled"}B= $opt->{"helper_timeout"}C= time - $started >= $opt->{"helper_timeout"}
ABCresult
0XX0
10X0
1100
1111
1425 {
1426 0 $opt->{stalled} = "helper timeout: no completion within $opt->{helper_timeout}s";
1427 }
1428 0 ▶ if ($opt->{stalled})
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"stalled"}) 0 0 0
1429 {
1430 # A stalled helper (blocked on a fifo, deadlocked pipe, slow bomb, ...) would pump
1431 # forever. Kill its whole family so pumpable() clears, then stop.
1432 0 _kill_family($h, \@pgids);
1433 0 $killed = 1;
1434 0 last;
1435 }
1436 0 $t->start($opt->{every});
1437 }
1438 }
1439 # finish() throws when a child died on a signal (our kill), so guard it on the killed path only,
1440 # keeping the normal path's behaviour unchanged.
1441 0 ▶ if ($killed) { eval { $h->finish } } else { $h->finish }
Branch: 1 branch
BranchTrueFalseTotal
if ($killed) { } 0 0 0
1442 0 $opt->{finished} = 1;
1443  
1444 ## call it once more, to get the 100% printout, or somthing else...
1445 0 ▶ $opt->{prog}->($h, $opt) if $opt->{prog} && $t && $t->{has_fired};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"prog"} and $t and $t->{"has_fired"} 0 0 0
Condition0%0/6
$opt->{"prog"} and $t and $t->{"has_fired"}
!ll&&!rl&&r
000
$opt->{"prog"} and $t
!ll&&!rl&&r
000
MC/DC0%0/3
$opt->{"prog"} and $t and $t->{"has_fired"}
$opt->{"prog"} $t $t->{"has_fired"}
Truth table0%0/4
A and B and C
A= $opt->{"prog"}B= $tC= $t->{"has_fired"}
ABCresult
0XX0
10X0
1100
1111
1446  
1447 0 ▶ return wantarray ? $h->full_results : $h->result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1448 }
1449  
1450 =head2 fmt_run_shellcmd
1451  
1452 File::Unpack2::fmt_run_shellcmd( $m->{argvv} )
1453  
1454 Static function to pretty print the return value $m of method find_mime_helper();
1455 It formats a command array used with run() as a properly escaped shell command string.
1456  
1457 =cut
1458  
1459 sub _my_shell_quote
1460 1161 ▶{
Subroutine _my_shell_quote: called CC=6 SCAR=18.1
1461 1161 my @a = @_;
1462 1161 my $sub;
1463 1161 ▶ if (@a and defined $a[0])
Branch: 1 branch
BranchTrueFalseTotal
if (@a and defined $a[0]) 1161 0 1161
Condition33%1/3
@a and defined $a[0]
!ll&&!rl&&r
001161
MC/DC0%0/2
@a and defined $a[0]
@a defined $a[0]
Truth table33%1/3
A and B
A= @aB= defined $a[0]
ABresult
0X0
100
111
1464 {
1465 1161 ▶ $sub = '\\&_locate_tar' if $a[0] eq \&_locate_tar;
Branch: 1 branch
BranchTrueFalseTotal
if $a[0] eq \&File::Unpack2::_locate_tar 100 1061 1161
1466 1161 ▶ $sub = '\\&_locate_cpio_i' if $a[0] eq \&_locate_cpio_i;
Branch: 1 branch
BranchTrueFalseTotal
if $a[0] eq \&File::Unpack2::_locate_cpio_i 40 1121 1161
1467 }
1468  
1469 1161 ▶ if ($sub)
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 140 1021 1161
1470 {
1471 140 shift @a;
1472 140 return "$sub " . shell_quote(@a);
1473 }
1474 1021 return shell_quote(@a);
1475 }
1476  
1477 sub fmt_run_shellcmd
1478 521 ▶{
Subroutine fmt_run_shellcmd: called CC=5 SCAR=16.2
Pod documented
1479 521 my @a = @_;
1480 0 ▶ @a = @{$a[0]{argvv}} if ref $a[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $a[0] eq "HASH" 0 521 521
1481 521 my @r = ();
1482 521 for my $a (@a)
1483 {
1484 1161 ▶ push @r, ref($a) ? '('._my_shell_quote(@$a).')' : _my_shell_quote($a);
Branch: 1 branch
BranchTrueFalseTotal
ref $a ? : 601 560 1161
1485 }
1486 521 my $r = join ' ', @r;
1487 521 ▶ $r =~ s{^\((.*)\)$}{$1} unless $#a; # parenthesis around a single cmd are unneeded.
Branch: 1 branch
BranchTrueFalseTotal
unless $#a 261 260 521
1488 521 return $r;
1489 }
1490  
1491 ## not a method, officially.
1492 #
1493 ## Chdir in and out of a jail is done here, as IPC::Run::run({init}->())
1494 ## has bad timing for our purposes.
1495 #
1496 ## fastjar extracts happily to ../../..
1497 ## this happens in cups-1.2.1/scripting/java/cups.jar
1498 #
1499 ## FIXME:
1500 # "/tmp/xxxx/cups-1.2.4-11.5.1.el5/cups-1.2.4/scripting/java/cups.jar":
1501 # {"cmd":"/usr/bin/unzip -P no_pw -q -o '%(src)s'",
1502 # "unpacked":"/tmp/xxxx/cups-1.2.4-11.5.1.el5/cups-1.2.4/_Knw_"}
1503 # Two issues:
1504 # a) _run_mime_helper in /tmp/xxxx/cups-1.2.4-11.5.1.el5/cups-1.2.4
1505 # should be /tmp/xxxx/cups-1.2.4-11.5.1.el5/cups-1.2.4/scripting/java
1506 # b) _Knw_ should never appear in the end result ...
1507 #
1508  
1509 sub _run_mime_helper
1510 0 ▶{
Subroutine _run_mime_helper: not called CC=74 SCAR=86.2
1511 0 my ($self, $h, @argv) = @_;
1512  
1513 0 for my $i (0..$#argv)
1514 {
1515 0 ▶ $argv[$i] = $1 if $argv[$i] =~ m{^(.*)$}s; # brute force untaint
Branch: 1 branch
BranchTrueFalseTotal
if $argv[$i] =~ /^(.*)$/s 0 0 0
1516 }
1517  
1518 0 my $destdir = $argv[2];
1519 0 ▶ my $dot_dot_safeguard = $self->{dot_dot_safeguard}||0;
Condition0%0/2
$self->{'dot_dot_safeguard'} || 0
l!l
00
MC/DC0%0/1
$self->{'dot_dot_safeguard'} || 0
$self->{'dot_dot_safeguard'}
Truth table0%0/2
A
A= $self->{'dot_dot_safeguard'}
Aresult
11
00
1520 0 ▶ $dot_dot_safeguard = 2 if $dot_dot_safeguard < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $dot_dot_safeguard < 2 0 0 0
1521  
1522 0 my $jail_base = '/dev/null';
1523 0 my $jail = $jail_base;
1524 0 ▶ unless ($self->{no_op})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_op"}) 0 0 0
1525 {
1526 0 mkpath($destdir);
1527 0 $jail_base = File::Temp::tempdir($TMPDIR_TEMPL, DIR => $destdir);
1528 0 $jail = $jail_base . ("/_" x $dot_dot_safeguard);
1529 0 mkpath($jail);
1530 }
1531  
1532 0 my $args =
1533 {
1534 src => $argv[0], # abs_path() - but not symlink resolved, so that the unpacker sees 'our' name
1535 destfile => $argv[1], # filename() - a suggested name, simply based on src, in case the unpacker needs it.
1536 destdir => $jail, # abs_path() - for now...
1537 mime => $argv[3],
1538 descr => $argv[4], # mime_descr
1539 configdir => $argv[5] # abs_path()
1540 };
1541 0 $args->{lsrc} = Cwd::realpath($args->{src}); # symlinks resolved; use this with a stupid unpacker like 'upx'
1542 0 ▶ die "src must be an abs_path." unless $args->{src} =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"src"} =~ m[^/] 0 0 0
1543  
1544 0 my @cmd;
1545 0 for my $a (@{$h->{argvv}})
1546 {
1547 0 ▶ if (ref $a)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a) { } 0 0 0
1548 {
1549 0 my @c = ();
1550 0 for my $b (@$a)
1551 {
1552 0 push @c, _subst_args($b, $args);
1553 }
1554 0 push @cmd, [@c];
1555 }
1556 else
1557 {
1558 0 push @cmd, _subst_args($a, $args);
1559 }
1560 }
1561  
1562 0 ▶ if ($self->{no_op})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_op"}) 0 0 0
1563 {
1564 0 print fmt_run_shellcmd(@cmd) . "\n";
1565 0 return undef;
1566 }
1567  
1568 0 ▶ print STDERR "_run_mime_helper in $destdir: " . fmt_run_shellcmd(@cmd) . "\n" if $self->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
1569  
1570 0 ▶ my $cwd = getcwd() or carp "cannot fetch initial working directory, getcwd: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $cwd = getcwd() or carp("cannot fetch initial working directory, getcwd: $!") 0 0 0
1571 0 ▶ $cwd = $1 if $cwd =~ m{^(.*)$}s; # brute force untaint. Whereever you go, there you are.
Branch: 1 branch
BranchTrueFalseTotal
if $cwd =~ /^(.*)$/s 0 0 0
1572 0 ▶ chdir $jail or die "chdir '$jail'";
Branch: 1 branch
BranchTrueFalseTotal
chdir $jail or die "chdir '${jail}'" 0 0 0
1573 0 ▶ chmod 0, $jail_base if $self->{jail_chmod0};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"jail_chmod0"} 0 0 0
1574 # Now have fully initialzed in the parent before forking.
1575 # This is needed, as all redirect operators are executed in the parent before forking.
1576 # init => sub { ... } is no longer needed. sigh, I really wanted to the init sub for the chdir.
1577 # But hey, mkpath() and rmtree() change the cwd so often, and restore it, so why shouldn't we?
1578  
1579  
1580 0 my $run_error = undef; # we capture the first error line for the logfile.
1581 # Watchdog cadence: tick several times within the smaller of the two timeouts so both the
1582 # no-progress stall and the absolute helper_timeout are caught promptly; default to the historic 5s.
1583 0 ▶ my $stall_timeout = $self->{stall_timeout} || 0;
Condition0%0/2
$self->{'stall_timeout'} || 0
l!l
00
MC/DC0%0/1
$self->{'stall_timeout'} || 0
$self->{'stall_timeout'}
Truth table0%0/2
A
A= $self->{'stall_timeout'}
Aresult
11
00
1584 0 ▶ my $ht = $self->{helper_timeout} || 0;
Condition0%0/2
$self->{'helper_timeout'} || 0
l!l
00
MC/DC0%0/1
$self->{'helper_timeout'} || 0
$self->{'helper_timeout'}
Truth table0%0/2
A
A= $self->{'helper_timeout'}
Aresult
11
00
1585 0 my $window = $stall_timeout;
1586 0 ▶ $window = $ht if $ht and (!$window or $ht < $window);
Branch: 1 branch
BranchTrueFalseTotal
if $ht and !$window || $ht < $window 0 0 0
Condition0%0/6
$ht and !$window || $ht < $window
!ll&&!rl&&r
000
not $window or $ht < $window
l!l&&r!l&&!r
000
MC/DC0%0/3
$ht and !$window || $ht < $window
$ht not $window $ht < $window
Truth table0%0/4
A and not(B) or C
A= $htB= not $windowC= $ht < $window
ABCresult
0XX0
1000
11X1
1011
1587 0 my $every = 5;
1588 0 ▶ if ($window) { $every = int($window / 4); $every = 1 if $every < 1; $every = 5 if $every > 5; }
Branch: 3 branches
BranchTrueFalseTotal
if ($window) 0 0 0
if $every < 1 0 0 0
if $every > 5 0 0 0
1589  
1590 my %run_opts = (
1591 debug => ($self->{verbose} > 2) ? $self->{verbose} - 2 : 0,
1592 watch => $args->{src}, every => $every, stall_timeout => $stall_timeout,
1593 helper_timeout => ($self->{helper_timeout} || 0), fu_obj => $self, mime_helper => $h,
1594 0 ▶ err => sub { print "E: @_\n" if $self->{verbose}; $run_error = "@_" unless length $run_error },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
unless length $run_error 0 0 0
1595 prog => sub
1596 0 ▶{
Subroutine __ANON__: not called CC=19 SCAR=59.4
1597 # Stall watchdog: if no descendant helper advances any fd (reads input or writes output) for
1598 # stall_timeout seconds, flag it so run() can kill the family. Runs unconditionally (in every
1599 # branch) - a helper that finished reading its input but is stuck writing output would otherwise
1600 # look idle to the source-only progress display below.
1601 0 ▶ if ($_[1]{stall_timeout})
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]{"stall_timeout"}) 0 0 0
1602 {
1603 0 my $now = _family_fd_positions(POSIX::getpid());
1604 0 ▶ if (_fd_progress($_[1]{fdpos}, $now)) { $_[1]{last_progress} = time }
Branch: 2 branches
BranchTrueFalseTotal
if (_fd_progress($_[1]{'fdpos'}, $now)) { } 0 0 0
elsif (time - ($_[1]{'last_progress'} ||= time) >= $_[1]{'stall_timeout'}) { } 0 0 0
1605 ▶ elsif (time - ($_[1]{last_progress} ||= time) >= $_[1]{stall_timeout})
Condition0%0/2
$_[1]{"last_progress"} ||= time
l!l
00
MC/DC0%0/1
$_[1]{"last_progress"} ||= time
$_[1]{"last_progress"}
Truth table0%0/2
A
A= $_[1]{"last_progress"}
Aresult
11
00
1606 0 { $_[1]{stalled} = "stall timeout: mime helper made no progress for $_[1]{stall_timeout}s" }
1607 0 $_[1]{fdpos} = $now;
1608 }
1609  
1610 0 $_[1]{tick}++;
1611 0 my $name = $_[1]{watch}; $name =~ s{.*/}{};
1612 0 ▶ if ($_[1]{finished})
Branch: 2 branches
BranchTrueFalseTotal
if ($_[1]{'finished'}) { } 0 0 0
elsif (my $p = _children_fuser($_[1]{'watch'}, POSIX::getpid())) { } 0 0 0
1613 {
1614 printf "T: %s (%s, done)\n", $name, _unit_bytes(-s $_[1]{watch},1)
1615 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1616 }
1617 elsif (my $p = _children_fuser($_[1]{watch}, POSIX::getpid()))
1618 {
1619 0 _fuser_offset($p);
1620 # we may get muliple process with multiple filedescriptors.
1621 # select the one that moves fastest.
1622 0 my $largest_diff = -1;
1623 0 for my $pid (keys %$p)
1624 {
1625 0 for my $fd (keys %{$p->{$pid}{fd}})
1626 {
1627 0 ▶ my $diff = ($p->{$pid}{fd}{$fd}{pos}||0) - ($_[1]{fuser}{$pid}{fd}{$fd}{pos}||0);
Condition0%0/4
$p->{$pid}{'fd'}{$fd}{'pos'} || 0
l!l
00
$_[1]{'fuser'}{$pid}{'fd'}{$fd}{'pos'} || 0
l!l
00
MC/DC0%0/1
$p->{$pid}{'fd'}{$fd}{'pos'} || 0
$p->{$pid}{'fd'}{$fd}{'pos'}
MC/DC0%0/1
$_[1]{'fuser'}{$pid}{'fd'}{$fd}{'pos'} || 0
$_[1]{'fuser'}{$pid}{'fd'}{$fd}{'pos'}
Truth table0%0/2
A
A= $p->{$pid}{'fd'}{$fd}{'pos'}
Aresult
11
00
Truth table0%0/2
A
A= $_[1]{'fuser'}{$pid}{'fd'}{$fd}{'pos'}
Aresult
11
00
1628 0 ▶ if ($diff > $largest_diff)
Branch: 1 branch
BranchTrueFalseTotal
if ($diff > $largest_diff) 0 0 0
1629 {
1630 0 $largest_diff = $diff;
1631 0 $p->{fastest_fd} = $p->{$pid}{fd}{$fd};
1632 }
1633 }
1634 }
1635 # Stick with the one we had before, if none moves.
1636 0 ▶ $p->{fastest_fd} = $_[1]{fuser}{fastest_fd} if $largest_diff <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $largest_diff <= 0 0 0 0
1637 0 $_[1]{fuser} = $p;
1638 0 ▶ my $off = $p->{fastest_fd}{pos}||0;
Condition0%0/2
$p->{'fastest_fd'}{'pos'} || 0
l!l
00
MC/DC0%0/1
$p->{'fastest_fd'}{'pos'} || 0
$p->{'fastest_fd'}{'pos'}
Truth table0%0/2
A
A= $p->{'fastest_fd'}{'pos'}
Aresult
11
00
1639 0 ▶ my $tot = $p->{fastest_fd}{size}||(-s $_[1]{watch})||1;
Condition0%0/5
$p->{'fastest_fd'}{'size'} || -s $_[1]{'watch'} || 1
l!l
00
$p->{"fastest_fd"}{"size"} or -s $_[1]{"watch"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$p->{'fastest_fd'}{'size'} || -s $_[1]{'watch'} || 1
$p->{"fastest_fd"}{"size"} -s $_[1]{"watch"}
Truth table0%0/3
A or B
A= $p->{"fastest_fd"}{"size"}B= -s $_[1]{"watch"}
ABresult
1X1
011
000
1640 printf "T: %s (%s, %.1f%%)\n", $name, _unit_bytes($off,1), ($off*100)/$tot
1641 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1642 }
1643 else
1644 {
1645 print "T: $name tick_tick $_[1]{tick}\n"
1646 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1647 }
1648 },
1649 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'verbose'} > 2 ? : 0 0 0
Condition0%0/2
$self->{'helper_timeout'} || 0
l!l
00
MC/DC0%0/1
$self->{'helper_timeout'} || 0
$self->{'helper_timeout'}
Truth table0%0/2
A
A= $self->{'helper_timeout'}
Aresult
11
00
1650 0 my @r = $self->run(@cmd, \%run_opts);
1651  
1652  
1653 0 ▶ chmod 0700, $jail_base if $self->{jail_chmod0};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"jail_chmod0"} 0 0 0
1654 0 ▶ chdir $cwd or die "cannot chdir back to cwd: chdir($cwd): $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or die "cannot chdir back to cwd: chdir($cwd): $!" 0 0 0
1655 0 my @nonzero = grep { $_ } @r;
1656  
1657 # TODO: handle failure
1658 # - remove all,
1659 # - retry with a fallback helper , if any.
1660 printf STDERR "Non-Zero return value: $nonzero[0]: %s\n", fmt_run_shellcmd(@cmd)
1661 0 ▶ if $nonzero[0] and $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $nonzero[0] and $self->{"verbose"} 0 0 0
Condition0%0/3
$nonzero[0] and $self->{"verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
$nonzero[0] and $self->{"verbose"}
$nonzero[0] $self->{"verbose"}
Truth table0%0/3
A and B
A= $nonzero[0]B= $self->{"verbose"}
ABresult
0X0
100
111
1662  
1663 # FIXME: fallback helper not implemented
1664 # t/data/pdftxt-a.txt is really plain/text altthough it begins with "PDF-1.4..." and
1665 # thus fools the mime-type tests.
1666 # should run other helpers, and finally 'strings -' as a trivial fallback.
1667 0 ▶ if ($nonzero[0] or $run_opts{stalled})
Branch: 1 branch
BranchTrueFalseTotal
if ($nonzero[0] or $run_opts{"stalled"}) 0 0 0
Condition0%0/3
$nonzero[0] or $run_opts{"stalled"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$nonzero[0] or $run_opts{"stalled"}
$nonzero[0] $run_opts{"stalled"}
Truth table0%0/3
A or B
A= $nonzero[0]B= $run_opts{"stalled"}
ABresult
1X1
011
000
1668 {
1669 0 ▶ warn "File::Unpack2: $run_opts{stalled} (" . fmt_run_shellcmd(@cmd) . ")\n" if $run_opts{stalled};
Branch: 1 branch
BranchTrueFalseTotal
if $run_opts{"stalled"} 0 0 0
1670 0 rmtree($jail_base); # empty or has unusable contents now.
1671 ## FIXME: we should at least copy in the original file as is...
1672 my $error = $run_opts{stalled}
1673 0 ▶ ? "$run_opts{stalled}: " . fmt_run_shellcmd(@cmd)
Branch: 1 branch
BranchTrueFalseTotal
$run_opts{'stalled'} ? : 0 0 0
1674 : "nonzero retval:\n " . Dumper(\@r);
1675 0 return { error => $error, stderr => $run_error };
1676 }
1677  
1678 # loop through all _: if it only contains one item , replace it with this item,
1679 # be it a file or dir. This uses $jail_tmp, an unused pathname.
1680 0 my $jail_tmp = File::Temp::tempdir($TMPDIR_TEMPL, DIR => $destdir);
1681 0 rmdir $jail_tmp;
1682  
1683 # if only one file in $jail, move it up, and return
1684 # the filename instead of the dirname here.
1685 # (We don't search for $args->{destfile}, it is the unpackers choice to use it or not.)
1686 0 my $wanted_name;
1687 0 ▶ for (my $i = 0; $i <= $dot_dot_safeguard; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $dot_dot_safeguard) 0 0 0
1688 {
1689 0 ▶ opendir DIR, $jail_base or last;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $jail_base or last 0 0 0
1690 0 ▶ my @found = grep { $_ ne '.' and $_ ne '..' } readdir DIR;
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
1691 0 closedir DIR;
1692 0 my $found0;
1693 0 ▶ $found0 = $1 if defined($found[0]) and $found[0] =~ m{^(.*)$}s; # brute force untaint
Branch: 1 branch
BranchTrueFalseTotal
if defined $found[0] and $found[0] =~ /^(.*)$/s 0 0 0
Condition0%0/3
defined $found[0] and $found[0] =~ /^(.*)$/s
!ll&&!rl&&r
000
MC/DC0%0/2
defined $found[0] and $found[0] =~ /^(.*)$/s
defined $found[0] $found[0] =~ /^(.*)$/s
Truth table0%0/3
A and B
A= defined $found[0]B= $found[0] =~ /^(.*)$/s
ABresult
0X0
100
111
1694 0 ▶ print STDERR "dot_dot_safeguard=$dot_dot_safeguard, i=$i, found=$found0\n" if $self->{verbose} > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 2 0 0 0
1695 0 ▶ unless (@found)
Branch: 1 branch
BranchTrueFalseTotal
unless (@found) 0 0 0
1696 {
1697 0 rmdir $jail_base;
1698 0 my $name;
1699 0 ▶ $name = $1 if $args->{src} =~ m{/([^/]+)$};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"src"} =~ m[/([^/]+)$] 0 0 0
1700 0 ▶ print STDERR "oops(i=$i): nothing unpacked?? Adding $name as is.\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1701 0 return { error => "nothing unpacked" };
1702 }
1703 0 ▶ last if scalar @found != 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @found != 1 0 0 0
1704 0 ▶ $wanted_name = $found0 if $i == $dot_dot_safeguard;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $dot_dot_safeguard 0 0 0
1705 0 ▶ last unless -d $jail_base . "/" . $found0;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $jail_base . '/' . $found0 0 0 0
1706 # assert writable dirs. needed for ksh/ast-base.2012-08-01.tar.bz2/src/cmd/pax/data/ro.dat
1707 0 chmod(0755, $jail_base . "/" . $found0);
1708 0 ▶ rename $jail_base, $jail_tmp or die("4:$i rename($jail_base, $jail_tmp) failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
rename $jail_base, $jail_tmp or die "4:$i rename($jail_base, $jail_tmp) failed: $!" 0 0 0
1709 0 ▶ rename $jail_tmp . "/" . $found0, $jail_base or die("5:$i rename($jail_tmp .'/'. $found0, $jail_base) failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
rename $jail_tmp . '/' . $found0, $jail_base or die "5:$i rename($jail_tmp .'/'. $found0, $jail_base) failed: $!" 0 0 0
1710 0 ▶ rmdir $jail_tmp or last;
Branch: 1 branch
BranchTrueFalseTotal
rmdir $jail_tmp or last 0 0 0
1711 }
1712  
1713 ## this message is broken.
1714 # print STDERR "Hmmm, unpacker did not use destname: $args->{destfile}\n" if $self->{verbose} and !defined $wanted_name;
1715  
1716 # say nothing, if $args->{destfile} is equal to or a prefix of $wanted_name.
1717 print STDERR "Hmmm, unpacker saw destname: $args->{destfile}, but used destname: $wanted_name\n"
1718 0 ▶ if $self->{verbose} > 1 and defined($wanted_name) and $wanted_name !~ m{^\Q$args->{destfile}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 and defined $wanted_name and not $wanted_name =~ /^\Q$args->{"destfile"}\E/ 0 0 0
Condition0%0/6
$self->{"verbose"} > 1 and defined $wanted_name and not $wanted_name =~ /^\Q$args->{"destfile"}\E/
!ll&&!rl&&r
000
$self->{"verbose"} > 1 and defined $wanted_name
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"verbose"} > 1 and defined $wanted_name and not $wanted_name =~ /^\Q$args->{"destfile"}\E/
$self->{"verbose"} > 1 defined $wanted_name not $wanted_name =~ /^\Q$args->{"destfile"}\E/
Truth table0%0/4
A and B and not(C)
A= $self->{"verbose"} > 1B= defined $wanted_nameC= not $wanted_name =~ /^\Q$args->{"destfile"}\E/
ABCresult
0XX0
10X0
1100
1111
1719  
1720 0 ▶ $wanted_name = $args->{destfile} unless defined $wanted_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wanted_name 0 0 0
1721 0 my $wanted_path;
1722 0 ▶ $wanted_path = _unused_pathname($destdir, $wanted_name) if defined $wanted_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wanted_name 0 0 0
1723 0 my $unpacked = $jail_base;
1724 0 ▶ if (defined($wanted_name) and !-e $wanted_path)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wanted_name and not -e $wanted_path) 0 0 0
Condition0%0/3
defined $wanted_name and not -e $wanted_path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $wanted_name and not -e $wanted_path
defined $wanted_name not -e $wanted_path
Truth table0%0/3
A and not(B)
A= defined $wanted_nameB= not -e $wanted_path
ABresult
0X0
100
111
1725 {
1726 0 ▶ if (-d $jail_base)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $jail_base) { } 0 0 0
1727 {
1728 ## find out, if the unpacker created exactly one file or one directory,
1729 ## in this case we can move one level further.
1730 0 opendir DIR, $jail_base;
1731 0 ▶ my @found = grep { $_ ne '.' and $_ ne '..' } readdir DIR;
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
1732 0 closedir DIR;
1733 0 my $found0;
1734 0 ▶ $found0 = $1 if defined($found[0]) and $found[0] =~ m{^(.*)$}s; # brute force untaint
Branch: 1 branch
BranchTrueFalseTotal
if defined $found[0] and $found[0] =~ /^(.*)$/s 0 0 0
Condition0%0/3
defined $found[0] and $found[0] =~ /^(.*)$/s
!ll&&!rl&&r
000
MC/DC0%0/2
defined $found[0] and $found[0] =~ /^(.*)$/s
defined $found[0] $found[0] =~ /^(.*)$/s
Truth table0%0/3
A and B
A= defined $found[0]B= $found[0] =~ /^(.*)$/s
ABresult
0X0
100
111
1735  
1736 0 ▶ if ($#found == 0 and $found0 eq $wanted_name)
Branch: 1 branch
BranchTrueFalseTotal
if ($#found == 0 and $found0 eq $wanted_name) { } 0 0 0
Condition0%0/3
$#found == 0 and $found0 eq $wanted_name
!ll&&!rl&&r
000
MC/DC0%0/2
$#found == 0 and $found0 eq $wanted_name
$#found == 0 $found0 eq $wanted_name
Truth table0%0/3
A and B
A= $#found == 0B= $found0 eq $wanted_name
ABresult
0X0
100
111
1737 {
1738 0 ▶ rename "$jail_base/$found0", $wanted_path or die "1 rename($jail_base/$found0, $wanted_path) failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$jail_base/$found0", $wanted_path or die "1 rename($jail_base/$found0, $wanted_path) failed: $!" 0 0 0
1739 0 rmdir $jail_base;
1740 }
1741 else
1742 {
1743 0 ▶ rename $jail_base, $wanted_path or die "2 rename($jail_base, $wanted_path) failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $jail_base, $wanted_path or die "2 rename($jail_base, $wanted_path) failed: $!" 0 0 0
1744 }
1745 }
1746 else
1747 {
1748 0 ▶ rename $jail_base, $wanted_path or die "3 rename($jail_base, $wanted_path) failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $jail_base, $wanted_path or die "3 rename($jail_base, $wanted_path) failed: $!" 0 0 0
1749 }
1750 0 $unpacked = $wanted_path;
1751 }
1752  
1753 # catch some patholigical cases.
1754 0 ▶ if (-f $unpacked and !-l $unpacked)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $unpacked and not -l $unpacked) 0 0 0
Condition0%0/3
-f $unpacked and not -l $unpacked
!ll&&!rl&&r
000
MC/DC0%0/2
-f $unpacked and not -l $unpacked
-f $unpacked not -l $unpacked
Truth table0%0/3
A and not(B)
A= -f $unpackedB= not -l $unpacked
ABresult
0X0
100
111
1755 {
1756 0 ▶ if (!-s $unpacked)
Branch: 2 branches
BranchTrueFalseTotal
if (not -s $unpacked) { } 0 0 0
elsif (-s $unpacked eq (my $s = -s $args->{'src'})) { } 0 0 0
1757 {
1758 0 ▶ print STDERR "Ooops, only one empty file -> symlink back\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1759 0 unlink $unpacked;
1760 0 symlink $args->{src}, $unpacked;
1761 }
1762 elsif (-s $unpacked eq (my $s = -s $args->{src}))
1763 {
1764 0 ▶ print STDERR "Hmm, same size ($s bytes) after unpacking???\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1765 ## xz -dc -f behaves like cat, if called on an unknown file.
1766 ## Compare the files. If they are identical, stop this:
1767 0 ▶ if (File::Compare::cmp($args->{src}, $unpacked) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (File::Compare::cmp($args->{"src"}, $unpacked) == 0) 0 0 0
1768 {
1769 0 ▶ print STDERR "Oops, identical -> symlink back\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
1770 0 unlink $unpacked;
1771 0 symlink $args->{src}, $unpacked;
1772 }
1773 }
1774 }
1775  
1776 0 my $diag = undef;
1777 0 ▶ $diag->{stderr} = $run_error if defined $run_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $run_error 0 0 0
1778 0 return ($unpacked, $diag);
1779 }
1780  
1781 sub _unused_pathname
1782 0 ▶{
Subroutine _unused_pathname: not called CC=5 SCAR=34.0
1783 0 my ($destdir, $wanted_name) = @_;
1784 0 my $wanted_path = $destdir . "/" . $wanted_name;
1785  
1786 0 ▶ if (-e $wanted_path)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $wanted_path) 0 0 0
1787 {
1788 ## try to come up with a very similar name, just different suffix.
1789 ## be compatible with path name shortening in unpack()
1790 0 my $test_path = $wanted_path . '._';
1791 0 for my $i ('', 1..99999)
1792 {
1793 # All our mime detectors work on file contents, rather than on suffixes.
1794 # Thus messing with the suffix should be okay here.
1795 0 ▶ unless (-e $test_path.$i)
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $test_path . $i) 0 0 0
1796 {
1797 0 $wanted_path = $test_path.$i;
1798 0 last;
1799 }
1800 }
1801 }
1802 0 ▶ die "_unused_pathname failed: last attempt $wanted_path\n" if -e $wanted_path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $wanted_path 0 0 0
1803 0 return $wanted_path;
1804 }
1805  
1806  
1807 sub _children_fuser
1808 0 ▶{
Subroutine _children_fuser: not called CC=19 SCAR=59.4
1809 0 my ($file, $ppid) = @_;
1810 0 ▶ $ppid ||= 1;
Condition0%0/2
$ppid ||= 1
l!l
00
MC/DC0%0/1
$ppid ||= 1
$ppid
Truth table0%0/2
A
A= $ppid
Aresult
11
00
1811 0 $file = Cwd::abs_path($file);
1812  
1813 0 ▶ opendir DIR, "/proc" or die "opendir /proc failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, "/proc" or die "opendir /proc failed: $!\n" 0 0 0
1814 0 my %p = map { $_ => {} } grep { /^\d+$/ } readdir DIR;
1815 0 closedir DIR;
1816  
1817 # get all procs, and their parent pids
1818 0 for my $p (keys %p)
1819 {
1820 0 ▶ if (open IN, "<", "/proc/$p/stat")
Branch: 1 branch
BranchTrueFalseTotal
if (open IN, "<", "/proc/$p/stat") 0 0 0
1821 {
1822 # don't care if open fails. the process may have exited.
1823 0 my $text = join '', <IN>;
1824 0 close IN;
1825 0 ▶ if ($text =~ m{\((.*)\)\s+(\w)\s+(\d+)}s)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\((.*)\)\s+(\w)\s+(\d+)/s) 0 0 0
1826 {
1827 0 $p{$p}{cmd} = $1;
1828 0 $p{$p}{state} = $2;
1829 0 $p{$p}{ppid} = $3;
1830 }
1831 }
1832 }
1833  
1834 # Weed out those who are not in our family
1835 0 ▶ if ($ppid > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($ppid > 1) 0 0 0
1836 {
1837 0 for my $p (keys %p)
1838 {
1839 0 my $family = 0;
1840 0 my $pid = $p;
1841 0 ▶ while ($pid)
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
1842 {
1843 # Those that have ppid==1 may also belong to our family.
1844 # We never know.
1845 0 ▶ if ($pid == $ppid or $pid == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == $ppid or $pid == 1) 0 0 0
Condition0%0/3
$pid == $ppid or $pid == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$pid == $ppid or $pid == 1
$pid == $ppid $pid == 1
Truth table0%0/3
A or B
A= $pid == $ppidB= $pid == 1
ABresult
1X1
011
000
1846 {
1847 0 $family = 1;
1848 0 last;
1849 }
1850 0 ▶ last unless $p{$pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $p{$pid} 0 0 0
1851 0 $pid = $p{$pid}{ppid};
1852 }
1853 0 ▶ delete $p{$p} unless $family;
Branch: 1 branch
BranchTrueFalseTotal
unless $family 0 0 0
1854 }
1855 }
1856  
1857 0 my %o; # matching open files are recorded here
1858  
1859 # see what files they have open
1860 0 for my $p (keys %p)
1861 {
1862 0 ▶ if (opendir DIR, "/proc/$p/fd")
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, "/proc/$p/fd") 0 0 0
1863 {
1864 0 my @l = grep { /^\d+$/ } readdir DIR;
1865 0 closedir DIR;
1866 0 for my $l (@l)
1867 {
1868 0 my $r = readlink("/proc/$p/fd/$l");
1869 0 ▶ next unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 0 0
1870 # warn "$p, $l, $r\n";
1871 0 ▶ if ($r eq $file)
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq $file) 0 0 0
1872 {
1873 0 ▶ $o{$p}{cmd} ||= $p{$p}{cmd};
Condition0%0/3
$o{$p}{"cmd"} ||= $p{$p}{"cmd"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$o{$p}{"cmd"} ||= $p{$p}{"cmd"}
$o{$p}{"cmd"} $p{$p}{"cmd"}
Truth table0%0/3
A ||= B
A= $o{$p}{"cmd"}B= $p{$p}{"cmd"}
ABresult
1X1
011
000
1874 0 $o{$p}{fd}{$l} = { file => $file };
1875 }
1876 }
1877 }
1878 }
1879 0 return \%o;
1880 }
1881  
1882 # see if we can read the file offset of a file descriptor, and the size of its file.
1883 sub _fuser_offset
1884 0 ▶{
Subroutine _fuser_offset: not called CC=6 SCAR=37.4
1885 0 my ($p) = @_;
1886 0 for my $pid (keys %$p)
1887 {
1888 0 for my $fd (keys %{$p->{$pid}{fd}})
1889 {
1890 0 ▶ if (open IN, "/proc/$pid/fdinfo/$fd")
Branch: 1 branch
BranchTrueFalseTotal
if (open IN, "/proc/$pid/fdinfo/$fd") 0 0 0
1891 {
1892 0 ▶ while (defined (my $line = <IN>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline IN)) 0 0 0
1893 {
1894 0 chomp $line;
1895 0 ▶ $p->{$pid}{fd}{$fd}{$1} = $2 if $line =~ m{^(\w+):\s+(.*)\b};
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^(\w+):\s+(.*)\b/ 0 0 0
1896 }
1897 }
1898 0 close IN;
1899 0 $p->{$pid}{fd}{$fd}{size} = -s $p->{$pid}{fd}{$fd}{file};
1900 }
1901 }
1902 }
1903  
1904 # Strict list of PIDs descended from $ppid (children, grandchildren, ...), walking /proc ppid
1905 # chains. Unlike _children_fuser this deliberately does NOT include reparented (ppid==1) processes:
1906 # it is used to *kill* a stalled helper family, so it must never reach outside our own descendants.
1907 sub _descendant_pids
1908 1 ▶{
Subroutine _descendant_pids: called CC=13 SCAR=26.5
1909 1 my ($ppid) = @_;
1910 1 ▶ return () unless $ppid and $ppid > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $ppid and $ppid > 1 0 1 1
Condition33%1/3
$ppid and $ppid > 1
!ll&&!rl&&r
001
MC/DC0%0/2
$ppid and $ppid > 1
$ppid $ppid > 1
Truth table33%1/3
A and B
A= $ppidB= $ppid > 1
ABresult
0X0
100
111
1911  
1912 1 ▶ opendir my $dir, "/proc" or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, "/proc" or return () 1 0 1
1913 1 my @all = grep { /^\d+$/ } readdir $dir;
1914 1 closedir $dir;
1915  
1916 1 my %parent;
1917 1 for my $p (@all)
1918 {
1919 9 ▶ next unless open my $in, "<", "/proc/$p/stat";
Branch: 1 branch
BranchTrueFalseTotal
unless open my $in, "<", "/proc/$p/stat" 0 9 9
1920 9 my $text = join '', <$in>;
1921 9 close $in;
1922 9 ▶ $parent{$p} = $3 if $text =~ m{\((.*)\)\s+(\w)\s+(\d+)}s;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\((.*)\)\s+(\w)\s+(\d+)/s 9 0 9
1923 }
1924  
1925 1 my @family;
1926 1 for my $p (@all)
1927 {
1928 9 my $pid = $p;
1929 9 my %seen;
1930 9 ▶ while ($pid and $pid > 1 and !$seen{$pid}++)
Branch: 1 branch
BranchTrueFalseTotal
if ($pid and $pid > 1 and not $seen{$pid}++) 29 8 37
Condition66%4/6
$pid and $pid > 1 and not $seen{$pid}++
!ll&&!rl&&r
8029
$pid and $pid > 1
!ll&&!rl&&r
0829
MC/DC33%1/3
$pid and $pid > 1 and not $seen{$pid}++
$pid $pid > 1 not $seen{$pid}++
Truth table50%2/4
A and B and not(C)
A= $pidB= $pid > 1C= not $seen{$pid}++
ABCresult
0XX0
10X0
1100
1111
1931 {
1932 29 ▶ if ($pid == $ppid) { push @family, $p; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == $ppid) 1 28 29
1933 28 ▶ last unless defined $parent{$pid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent{$pid} 0 28 28
1934 28 $pid = $parent{$pid};
1935 }
1936 }
1937 1 return grep { $_ != $ppid } @family;
1938 }
1939  
1940 # Snapshot of byte offsets ("pid:fd" => pos) for every open fd of $ppid's descendant helpers.
1941 # Used to tell whether a helper is making any I/O progress at all: a stalled helper leaves every
1942 # offset unchanged. Pipes/fifos report pos 0 and therefore simply never count as progress.
1943 sub _family_fd_positions
1944 0 ▶{
Subroutine _family_fd_positions: not called CC=7 SCAR=40.3
1945 0 my ($ppid) = @_;
1946 0 my %pos;
1947 0 for my $pid (_descendant_pids($ppid))
1948 {
1949 0 ▶ opendir my $d, "/proc/$pid/fd" or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $d, "/proc/$pid/fd" or next 0 0 0
1950 0 my @fds = grep { /^\d+$/ } readdir $d;
1951 0 closedir $d;
1952 0 for my $fd (@fds)
1953 {
1954 0 ▶ next unless open my $in, "<", "/proc/$pid/fdinfo/$fd";
Branch: 1 branch
BranchTrueFalseTotal
unless open my $in, "<", "/proc/$pid/fdinfo/$fd" 0 0 0
1955 0 ▶ while (defined (my $line = <$in>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 0 0 0
1956 {
1957 0 ▶ if ($line =~ m{^pos:\s+(\d+)}) { $pos{"$pid:$fd"} = $1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^pos:\s+(\d+)/) 0 0 0
1958 }
1959 0 close $in;
1960 }
1961 }
1962 0 return \%pos;
1963 }
1964  
1965 # True if any fd advanced, opened, or closed between two _family_fd_positions() snapshots.
1966 sub _fd_progress
1967 0 ▶{
Subroutine _fd_progress: not called CC=7 SCAR=40.3
1968 0 my ($prev, $cur) = @_;
1969 0 ▶ return 1 unless defined $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prev 0 0 0
1970 0 ▶ for my $k (keys %$cur) { return 1 if !exists $prev->{$k} or $cur->{$k} > $prev->{$k} }
Branch: 1 branch
BranchTrueFalseTotal
if not exists $prev->{$k} or $cur->{$k} > $prev->{$k} 0 0 0
Condition0%0/3
not exists $prev->{$k} or $cur->{$k} > $prev->{$k}
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $prev->{$k} or $cur->{$k} > $prev->{$k}
not exists $prev->{$k} $cur->{$k} > $prev->{$k}
Truth table0%0/3
not(A) or B
A= not exists $prev->{$k}B= $cur->{$k} > $prev->{$k}
ABresult
1X1
011
000
1971 0 ▶ for my $k (keys %$prev) { return 1 unless exists $cur->{$k} }
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cur->{$k} 0 0 0
1972 0 return 0;
1973 }
1974  
1975 # Process group id of $pid (field 5 of /proc/PID/stat, after "(comm) state ppid"), or undef.
1976 sub _process_pgrp
1977 0 ▶{
Subroutine _process_pgrp: not called CC=3 SCAR=24.8
1978 0 my ($pid) = @_;
1979 0 ▶ open my $in, "<", "/proc/$pid/stat" or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", "/proc/$pid/stat" or return undef 0 0 0
1980 0 my $text = join '', <$in>;
1981 0 close $in;
1982 0 ▶ return $1 if $text =~ m{\)\s+\S+\s+\d+\s+(\d+)}s;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\)\s+\S+\s+\d+\s+(\d+)/s 0 0 0
1983 0 return undef;
1984 }
1985  
1986 # Forcibly terminate a stalled helper and its whole descendant family, so pump()/finish() can
1987 # unwind. Grandchildren must die too: a survivor still holding the helper's pipe would keep the
1988 # harness pumpable and hang finish() forever. Helpers are started in their own process group
1989 # (setpgrp in run()), so we signal those groups as well - that reaches grandchildren (even ones
1990 # reparented to init, which keep the group) that the ppid walk cannot see. We never signal our own
1991 # process group.
1992 sub _kill_family
1993 0 ▶{
Subroutine _kill_family: not called CC=11 SCAR=48.8
1994 0 my ($h, $pgids) = @_;
1995 0 my $me = POSIX::getpid();
1996 0 my $my_pgrp = getpgrp(0);
1997  
1998 0 for my $sig ('TERM', 'KILL')
1999 {
2000 0 my @pids = _descendant_pids($me);
2001  
2002 0 my %pgrp;
2003 # Groups captured at start (each helper stage is its own leader): reaches grandchildren even
2004 # if a stage exited and they reparented to init - they keep the group.
2005 0 ▶ for my $pg (@{$pgids || []}) { $pgrp{$pg} = 1 if $pg > 1 and $pg != $my_pgrp }
Branch: 1 branch
BranchTrueFalseTotal
if $pg > 1 and $pg != $my_pgrp 0 0 0
Condition0%0/5
$pgids || []
l!l
00
$pg > 1 and $pg != $my_pgrp
!ll&&!rl&&r
000
MC/DC0%0/1
$pgids || []
$pgids
MC/DC0%0/2
$pg > 1 and $pg != $my_pgrp
$pg > 1 $pg != $my_pgrp
Truth table0%0/2
A
A= $pgids
Aresult
11
00
Truth table0%0/3
A and B
A= $pg > 1B= $pg != $my_pgrp
ABresult
0X0
100
111
2006 # Plus the group of any still-live descendant, as a backstop.
2007 0 for my $pid (@pids)
2008 {
2009 0 my $pg = _process_pgrp($pid);
2010 0 ▶ $pgrp{$pg} = 1 if defined $pg and $pg > 1 and $pg != $my_pgrp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pg and $pg > 1 and $pg != $my_pgrp 0 0 0
Condition0%0/6
defined $pg and $pg > 1 and $pg != $my_pgrp
!ll&&!rl&&r
000
defined $pg and $pg > 1
!ll&&!rl&&r
000
MC/DC0%0/3
defined $pg and $pg > 1 and $pg != $my_pgrp
defined $pg $pg > 1 $pg != $my_pgrp
Truth table0%0/4
A and B and C
A= defined $pgB= $pg > 1C= $pg != $my_pgrp
ABCresult
0XX0
10X0
1100
1111
2011 }
2012 0 kill $sig, map { -$_ } keys %pgrp; # negative pid => whole process group
2013 0 kill $sig, @pids; # and the processes directly, as a backstop
2014 0 ▶ select undef, undef, undef, 0.3 if $sig eq 'TERM';
Branch: 1 branch
BranchTrueFalseTotal
if $sig eq "TERM" 0 0 0
2015 }
2016 0 eval { $h->kill_kill(grace => 1) };
2017 }
2018  
2019 # Recurse into a child, isolating faults: a crafted/hostile archive can make the child unpack() die
2020 # (path escape, rename/chdir/assert failure, mime death, ...). We must skip just that item and keep
2021 # going, never abort the whole run. On a die we restore cwd (a die inside a helper may have left us
2022 # in a jail dir) and the recursion counters the aborted subtree leaked, so the top-level JSON epilog
2023 # still runs and the log stays valid. Returns 1 on success, 0 if the child was skipped.
2024 sub _safe_unpack
2025 154 ▶{
Subroutine _safe_unpack: called CC=5 SCAR=16.2
2026 154 my ($self, $child, $childdestdir) = @_;
2027 154 my $lvl = $self->{recursion_level};
2028 154 my $ia = $self->{inside_archives};
2029 154 my $cwd = Cwd::getcwd();
2030 154 ▶ return 1 if eval { $self->unpack($child, $childdestdir); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->unpack($child, $childdestdir); 1 } } 64 90 154
2031  
2032 90 ▶ my $err = $@ || 'unknown error';
Condition50%1/2
$@ || 'unknown error'
l!l
900
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
2033 90 chomp $err;
2034 90 ▶ chdir $cwd if defined $cwd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cwd 90 0 90
2035 90 $self->{recursion_level} = $lvl;
2036 90 $self->{inside_archives} = $ia;
2037 90 push @{$self->{error}}, "unpack('$child') aborted: $err; ";
2038 90 $self->logf($child => {error => "aborted: $err", skipped => 'unpack error'});
2039 90 ▶ warn "File::Unpack2: unpack('$child') aborted: $err\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 90 90
2040 90 return 0;
2041 }
2042  
2043  
2044 sub _prep_configdir
2045 0 ▶{
Subroutine _prep_configdir: not called CC=7 SCAR=40.3
2046 0 my ($self) = @_;
2047 0 my $dir = "/tmp/file_unpack2_$$/";
2048 0 mkpath($dir);
2049 0 my $j = $self->{json}->allow_nonref();
2050  
2051 0 open my $SH, ">", "$dir/config.sh";
2052 0 open my $JS, ">", "$dir/config.js";
2053  
2054 0 print $JS "{\n";
2055  
2056 0 for my $group ('', 'minfree', 'exclude')
2057 {
2058 0 ▶ my $h_ref = ($group eq '') ? $self : $self->{$group};
Branch: 1 branch
BranchTrueFalseTotal
$group eq '' ? : 0 0 0
2059 0 for my $k (sort keys %$h_ref)
2060 {
2061 0 my $val = $h_ref->{$k};
2062 0 ▶ next if $k eq 'recursion_level';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "recursion_level" 0 0 0
2063 0 ▶ next if ref $val; # we only take scalars.
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 0 0 0
2064 0 ▶ my $name = ($group eq '') ? $k : "${group}_$k";
Branch: 1 branch
BranchTrueFalseTotal
$group eq '' ? : 0 0 0
2065 0 printf $SH "%s=%s\n", shell_quote(uc "fu_$name"), shell_quote($val);
2066 0 printf $JS "%s:%s,\n", $j->encode($name), $j->encode($val);
2067 }
2068 }
2069  
2070 0 print $SH "FU_VERSION=$VERSION\n";
2071 0 print $JS qq["fu_version":"$VERSION"\n}\n];
2072  
2073 0 close $SH;
2074 0 close $JS;
2075 0 return $dir;
2076 }
2077  
2078  
2079 =head2 mime_helper_dir mime_helper
2080  
2081 $u->mime_helper_dir($dir, ...)
2082 $u->mime_helper($mime_name, $suffix_regexp, \@argv, @redir, ...)
2083  
2084 Registers one or more directories where external MIME helper programs are found.
2085 Helpers plugins are shellscripts that server as specialized MIME type handlers for unpacking.
2086 A list of helpers comes builtin interfacing most well-known archivers. This list can be appended to using the mime_helper_dir() or mime_helper() methods.
2087 Multiple directories can be registered, They are searched in reverse order, i.e.
2088 last added takes precedence. Any external MIME helper takes precedence over built-in code.
2089  
2090 The suffix_regexp is used to derive the destination name from the source name.
2091 It is not used for selecting helpers.
2092  
2093 When collecting external helper scripts via C<mime_helper_dir()>, there is no C<suffix_regexp>. Instead,
2094 external helper scripts can explicitly create a toplevel directory with the desired name.
2095  
2096 Helpers are mapped to MIME types by their mime_name. The name can be constructed
2097 from the MIME type by replacing the '/' with a '=' character, and by using the
2098 word 'ANY' as a wildcard component. The '=' character is interpreted as an
2099 implicit '=ANY+' if needed.
2100  
2101 Examples:
2102  
2103 Mimetype helper names tried from top to bottom
2104 -----------------------------------------------------------------
2105 image/png image=png
2106 image=ANY
2107 image
2108 ANY=png
2109 ANY=ANY
2110 ANY
2111  
2112 application/vnd.oasis+zip application=vnd.oasis+zip
2113 application=ANY+zip
2114 application=ANYzip
2115 application=zip
2116 application=ANY
2117 ...
2118  
2119 A trailing '=ANY' is implicit, as shown by these examples.
2120 The rules for precedence are this:
2121  
2122 =over
2123  
2124 =item *
2125  
2126 Search in the latest directory is exhaused first, then the previously added directory is considered in turn,
2127 up to all directories have been traversed, or until a matching helper is found.
2128  
2129 =item *
2130  
2131 A matching name with wildcards has lower precedence than a matching name without.
2132  
2133 =item *
2134  
2135 A wildcard before the '=' sign lowers precedence more than one after it.
2136  
2137 =back
2138  
2139 The mapping takes place when C<mime_helper_dir> is called. Adding helper scripts to a directory
2140 afterwards has no effect. C<mime_helper> does not do any implicit expansions. Call it
2141 multiple times with the same helper command and different names if needed.
2142 The default argument list is "%(src)s %(destfile)s %(destdir)s %(mime)s %(descr)s %(configdir)s" --
2143 this is applied, if no args are given and no redirections are given. See also C<unpack> for more semantics and how a helper should behave.
2144  
2145 Both methods return an ARRAY-ref of HASHes describing all known (old and newly added) mime helpers.
2146  
2147 =cut
2148 16 my @def_mime_helper_fmt = qw(%(src)s %(destfile)s %(destdir)s %(mime)s %(descr)s %(configdir)s);
2149  
2150 sub _subst_args
2151 0 ▶{
Subroutine _subst_args: not called CC=1 SCAR=6.9
2152 0 my $f = Text::Sprintf::Named->new({fmt => $_[0]});
2153 0 return $f->format({args => $_[1]});
2154 }
2155  
2156 sub mime_helper
2157 522 ▶{
Subroutine mime_helper: called CC=8 SCAR=22.4
Pod documented
2158 522 my ($self, $name, $suffix_re, @args) = @_;
2159  
2160 # Called with no name it is a pure getter: return the current helper list.
2161 522 ▶ return $self->{mime_helper} unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 521 522
2162  
2163 521 ▶ @args = ($name) unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 521 521
2164 521 ▶ @args = ([@args]) unless ref $args[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args[0] 0 521 521
2165 0 ▶ push @{$args[0]}, @def_mime_helper_fmt unless $#{$args[0]} or defined $args[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $#{$args[0];} or defined $args[1] 0 521 521
Condition66%2/3
$#{$args[0];} or defined $args[1]
l!l&&r!l&&!r
501200
MC/DC0%0/2
$#{$args[0];} or defined $args[1]
$#{$args[0];} defined $args[1]
Truth table66%2/3
A or B
A= $#{$args[0];}B= defined $args[1]
ABresult
1X1
011
000
2166  
2167 # cut away the path prefix from name. And use / instead of = in the mime name.
2168 521 $name =~ s{(.*/)?(.*?)=(.*?)$}{$2=$3};
2169  
2170 521 ▶ unless ($name =~ m{[/=]})
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ m[[/=]]) 0 521 521
2171 {
2172 0 ▶ print STDERR "mime_helper '$name' needs a '=' or '/'.\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
2173 0 return $self->{mime_helper};
2174 }
2175  
2176 521 my $pat = "^\Q$name\E\$";
2177 521 $pat =~ s{\\=}{/(?:x-|ANY\\+)?};
2178 521 $pat =~ s{\\%}{ANY}g;
2179 521 $pat =~ s{^\^ANY}{};
2180 521 $pat =~ s{ANY\$$}{};
2181 521 $pat =~ s{ANY}{\\b\[\^\/\]+\\b}g;
2182 521 unshift @{$self->{mime_helper}},
2183 {
2184 name => $name, pat => $pat, suffix_re => $suffix_re,
2185 fmt_p => fmt_run_shellcmd(@args), argvv => \@args
2186 };
2187  
2188 521 delete $self->{mime_orcish}; # to be rebuilt in find_mime_helper()
2189  
2190 521 return $self->{mime_helper};
2191 }
2192  
2193 =head2 list
2194  
2195 Returns an ARRAY of preformatted patterns and MIME helpers.
2196  
2197 Example:
2198  
2199 printf @$_ for $u->list();
2200  
2201 =cut
2202  
2203 sub list
2204 1 ▶{
Subroutine list: called CC=4 SCAR=13.9
Pod documented
2205 1 my ($self) = @_;
2206  
2207 1 my $width = 10;
2208 1 for my $m (@{$self->{mime_helper}})
2209 {
2210 26 ▶ $width = length($m->{pat}) if length($m->{pat}) > $width;
Branch: 1 branch
BranchTrueFalseTotal
if length $m->{"pat"} > $width 5 21 26
2211 }
2212  
2213 1 my @r;
2214 1 for my $m (@{$self->{mime_helper}})
2215 {
2216 26 push @r, [ "%-${width}s %s\n", $m->{pat}, $m->{fmt_p} ];
2217 }
2218 1 return @r;
2219 }
2220  
2221 sub mime_helper_dir
2222 2 ▶{
Subroutine mime_helper_dir: called CC=10 SCAR=44.7
Pod documented
2223 2 my ($self, @dirs) = @_;
2224  
2225 2 for my $d (@dirs)
2226 {
2227 0 my %h;
2228 0 ▶ if (opendir DIR, $d)
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, $d) { } 0 0 0
2229 {
2230 0 %h = map { $_ => { a => "$d/$_" } } grep { -f "$d/$_" } readdir DIR;
2231 0 closedir DIR;
2232 }
2233 else
2234 {
2235 0 carp "Cannot opendir $d: $!, skipped\n";
2236 }
2237  
2238 # add =ANY suffix, if missing
2239 0 for my $h (keys %h)
2240 {
2241 0 ▶ if ($h !~ m{[/=]})
Branch: 1 branch
BranchTrueFalseTotal
unless ($h =~ m[[/=]]) 0 0 0
2242 {
2243 0 my $h2 = $h . "=ANY";
2244 0 ▶ $h{$h2} = { %{$h{$h}} } unless defined $h{$h2};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h{$h2} 0 0 0
2245 }
2246 }
2247  
2248 # calculate priorities
2249 0 for my $h (keys %h)
2250 {
2251 0 my $n = 1000000;
2252 0 my $p = 1000;
2253 0 ▶ while ($h =~ m{(ANY|=)}g)
Branch: 1 branch
BranchTrueFalseTotal
if ($h =~ /(ANY|=)/g) 0 0 0
2254 {
2255 0 ▶ if ($1 eq '=')
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '=') { } 0 0 0
2256 {
2257 0 $n = 1000;
2258 }
2259 else
2260 {
2261 0 $p += $n;
2262 }
2263 }
2264 # longer length has prio over shorter length. Hmm, this is ineffective, isnt it?
2265 0 $h{$h}{p} = $p - length($h);
2266 }
2267  
2268 # Now push them, sorted by prio.
2269 # Smaller prio_number is better. Later addition is prefered.
2270 0 for my $h (sort { $h{$b}{p} <=> $h{$a}{p} } keys %h)
2271 {
2272 # do not ruin the original name by resolving symlinks and such.
2273 0 $self->mime_helper($h, undef, [Cwd::fast_abs_path($h{$h}{a})]);
2274 }
2275 }
2276 2 return $self->{mime_helper};
2277 }
2278  
2279 =head2 find_mime_helper
2280  
2281 $u->find_mime_helper($mimetype)
2282  
2283 Returns a MIME helper suitable for unpacking the given $mimetype.
2284 If called in list context, a second return value indicates which
2285 mime helpers would be suitable, but could not be found in the system.
2286  
2287 =cut
2288  
2289 sub find_mime_helper
2290 0 ▶{
Subroutine find_mime_helper: not called CC=9 SCAR=45.0
Pod documented
2291 0 my ($self, $mimetype) = @_;
2292 0 ▶ $mimetype = $mimetype->[0] if ref $mimetype eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $mimetype eq "ARRAY" 0 0 0
2293  
2294 return $self->{mime_orcish}{$mimetype}
2295 if defined $self->{mime_orcish}{$mimetype} and
2296 0 ▶ -f $self->{mime_orcish}{$mimetype}{argvv}[0][0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"mime_orcish"}{$mimetype} and -f $self->{"mime_orcish"}{$mimetype}{"argvv"}[0][0] 0 0 0
Condition0%0/3
defined $self->{"mime_orcish"}{$mimetype} and -f $self->{"mime_orcish"}{$mimetype}{"argvv"}[0][0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"mime_orcish"}{$mimetype} and -f $self->{"mime_orcish"}{$mimetype}{"argvv"}[0][0]
defined $self->{"mime_orcish"}{$mimetype} -f $self->{"mime_orcish"}{$mimetype}{"argvv"}[0][0]
Truth table0%0/3
A and B
A= defined $self->{"mime_orcish"}{$mimetype}B= -f $self->{"mime_orcish"}{$mimetype}{"argvv"}[0][0]
ABresult
0X0
100
111
2297  
2298 0 my $r = undef;
2299 0 for my $h (@{$self->{mime_helper}})
2300 {
2301 0 ▶ if ($mimetype =~ m{$h->{pat}})
Branch: 1 branch
BranchTrueFalseTotal
if ($mimetype =~ /$h->{"pat"}/) 0 0 0
2302 {
2303 0 $self->_finalize_argvv($h);
2304 0 ▶ unless (-f $h->{argvv}[0][0])
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $h->{"argvv"}[0][0]) 0 0 0
2305 {
2306 0 push @{$r->{missing}}, $h->{argvv}[0][0];
2307 0 next;
2308 }
2309 0 $self->{mime_orcish}{$mimetype} = $h;
2310 0 ▶ return wantarray ? ($h, $r) : $h;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
2311 }
2312 }
2313 0 ▶ return wantarray ? (undef, $r) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
2314 }
2315  
2316 #
2317 # _finalize_argvv() executes a sub in 3 places:
2318 # The argvv ptr itself can be a sub:
2319 # this should return an array, where the
2320 # first element is the command (as an array-ref) and subsequent elements are
2321 # redirects. See run() for details.
2322 # One of the argvv elements is a sub:
2323 # this should return the command as an array-ref, if it is argvv[0],
2324 # or return one or more redirects.
2325 # One element of argvv[0] is a sub:
2326 # this should return one or more command names, options, arguments,
2327 #
2328 # Tricky part of the implementation is the in-place array expansion while iterating.
2329 #
2330 sub _finalize_argvv
2331 0 ▶{
Subroutine _finalize_argvv: not called CC=9 SCAR=45.0
2332 0 my ($self, $h) = @_;
2333  
2334 0 my $update_fmt_p = 0;
2335 0 ▶ if (ref $h->{argvv} eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h->{"argvv"} eq "CODE") 0 0 0
2336 {
2337 0 $h->{argvv} = [ $h->{argvv}->($self) ];
2338 0 $update_fmt_p++;
2339 }
2340  
2341 # If any part of LIST is an array, "foreach" will get very confused if you add or
2342 # remove elements within the loop body, for example with "splice". So don't do
2343 # that.
2344 # Sigh, we want do do exactly that, a sub may replace itself by any number of elements. Use booring C-style loop.
2345 0 my $last = $#{$h->{argvv}};
2346 0 ▶ for (my $idx = 0; $idx <= $last; $idx++)
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $last) 0 0 0
2347 {
2348 0 ▶ if (ref $h->{argvv}[$idx] eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h->{"argvv"}[$idx] eq "CODE") 0 0 0
2349 {
2350 0 my @r = $h->{argvv}[$idx]($self);
2351 0 splice @{$h->{argvv}}, $idx, 1, @r;
2352 0 $idx += $#r;
2353 0 $last +=$#r;
2354 0 $update_fmt_p++;
2355 }
2356 }
2357 0 $last = $#{$h->{argvv}};
2358 0 ▶ for (my $idx = 0; $idx <= $last; $idx++)
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $last) 0 0 0
2359 {
2360 0 ▶ next unless ref $h->{argvv}[$idx] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h->{"argvv"}[$idx] eq "ARRAY" 0 0 0
2361 0 my $last1 = $#{$h->{argvv}[$idx]};
2362 0 ▶ for (my $idx1 = 0; $idx1 <= $last1; $idx1++)
Branch: 1 branch
BranchTrueFalseTotal
if ($idx1 <= $last1) 0 0 0
2363 {
2364 0 ▶ if (ref $h->{argvv}[$idx][$idx1] eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h->{"argvv"}[$idx][$idx1] eq "CODE") 0 0 0
2365 {
2366 0 my @r = $h->{argvv}[$idx][$idx1]->($self);
2367 0 splice @{$h->{argvv}[$idx]}, $idx1, 1, @r;
2368 0 $idx1 += $#r;
2369 0 $last1 +=$#r;
2370 0 $update_fmt_p++;
2371 }
2372 }
2373 }
2374  
2375 0 ▶ $h->{fmt_p} = fmt_run_shellcmd($h) if $update_fmt_p;
Branch: 1 branch
BranchTrueFalseTotal
if $update_fmt_p 0 0 0
2376 }
2377  
2378 =head2 minfree
2379  
2380 $u->minfree(factor => 10, bytes => '100M', percent => '3%', warning => sub { .. })
2381  
2382 THESE TESTS ARE TO BE IMPLEMENTED.
2383  
2384 Guard the filesystem (destdir) against becoming full during C<unpack>.
2385 Before unpacking each source archive, the free space is measured and compared against three conditions:
2386  
2387 =over
2388  
2389 =item *
2390  
2391 The archive size multiplied with the given factor must fit into the filesystem.
2392  
2393 =item *
2394  
2395 The given number of bytes (in optional K, M, G, or T units) must be free.
2396  
2397 =item *
2398  
2399 The filesystem must have at least the given free percentage. The '%' character is optional.
2400  
2401 =back
2402  
2403 The warning method is called if any of the above conditions fail. Its signature is:
2404 &warning->($pathname, $full_percentage, $free_bytes, $free_inodes);
2405 It is expected to print an appropriate warning message, and delay a few seconds.
2406 It should return 0 to cause a retry. It should return nonzero to continue unpacking.
2407 The default warning method prints a message to STDERR, waits 30 seconds, and returns 0.
2408  
2409 The filesystem may still become full and unpacking may fail, if e.g. factor was chosen lower than
2410 the average compression ratio of the archives.
2411  
2412 =cut
2413  
2414 sub _bytes_unit
2415 60 ▶{
Subroutine _bytes_unit: called CC=5 SCAR=16.2
2416 60 my ($text) = @_;
2417 60 ▶ return int($1*1024) if $text =~ m{([\d\.]+)k}i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /([\d\.]+)k/i 0 60 60
2418 60 ▶ return int($1*1024*1024) if $text =~ m{([\d\.]+)m}i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /([\d\.]+)m/i 20 40 60
2419 40 ▶ return int($1*1024*1024*1024) if $text =~ m{([\d\.]+)g}i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /([\d\.]+)g/i 12 28 40
2420 28 ▶ return int($1*1024*1024*1024*1024) if $text =~ m{([\d\.]+)t}i;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /([\d\.]+)t/i 0 28 28
2421 28 return int($text);
2422 }
2423  
2424 sub _unit_bytes
2425 0 ▶{
Subroutine _unit_bytes: not called CC=7 SCAR=40.3
2426 0 my ($number, $dec_places) = @_;
2427 0 ▶ $dec_places = 2 unless defined $dec_places;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dec_places 0 0 0
2428 0 my $div = 1;
2429 0 my $unit = '';
2430 0 my $neg = '';
2431 0 ▶ if ($number < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 0) 0 0 0
2432 {
2433 0 $neg = '-'; $number = -$number;
2434 }
2435 0 ▶ if ($number > $div * 1024)
Branch: 1 branch
BranchTrueFalseTotal
if ($number > $div * 1024) 0 0 0
2436 {
2437 0 $div *= 1024; $unit = 'k';
2438 0 ▶ if ($number > $div * 1024)
Branch: 1 branch
BranchTrueFalseTotal
if ($number > $div * 1024) 0 0 0
2439 {
2440 0 $div *= 1024; $unit = 'm';
2441 0 ▶ if ($number > $div * 1024)
Branch: 1 branch
BranchTrueFalseTotal
if ($number > $div * 1024) 0 0 0
2442 {
2443 0 $div *= 1024; $unit = 'g';
2444 0 ▶ if ($number > $div * 1024)
Branch: 1 branch
BranchTrueFalseTotal
if ($number > $div * 1024) 0 0 0
2445 {
2446 0 $div *= 1024; $unit = 't';
2447 }
2448 }
2449 }
2450 }
2451 0 return sprintf "%s%.*f%s", $neg, $dec_places, ($number / $div), $unit;
2452 }
2453  
2454 # see fs.pm/check_fs_health()
2455  
2456 sub minfree
2457 20 ▶{
Subroutine minfree: called CC=5 SCAR=16.6
Pod documented
2458 20 my $self = shift;
2459 20 my %opt = @_;
2460  
2461 20 for my $i (qw(factor bytes percent))
2462 {
2463 60 ▶ $self->{minfree}{$i} = $opt{$i} if defined $opt{$i};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{$i} 0 60 60
2464 60 ▶ $self->{minfree}{$i} ||= 0;
Condition50%1/2
$self->{"minfree"}{$i} ||= 0
l!l
600
MC/DC0%0/1
$self->{"minfree"}{$i} ||= 0
$self->{"minfree"}{$i}
Truth table50%1/2
A
A= $self->{"minfree"}{$i}
Aresult
11
00
2465 }
2466 20 $self->{minfree}{bytes} = _bytes_unit($self->{minfree}{bytes});
2467 20 $self->{minfree}{percent} =~ s{%$}{};
2468 20 ▶ $self->{fs_warn} = $opt{warning} if ref $opt{warning};
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt{"warning"} 20 0 20
2469 }
2470  
2471 =head2 mime
2472  
2473 $u->mime($filename)
2474  
2475 $u->mime(file => $filename)
2476  
2477 $u->mime(buf => "#!/bin ...", file => "what-was-read")
2478  
2479 $u->mime(fd => \*STDIN, file => "what-was-opened")
2480  
2481 Determines the MIME type (and optionally additional information) of a file.
2482 The file can be specified by filename, by a provided buffer or an opened file descriptor.
2483 For the latter two cases, specifying a filename is optional, and used only for diagnostics.
2484  
2485 C<mime> uses libmagic by Christos Zoulas exposed via File::LibMagic and also uses
2486 the shared-mime-info database from freedesktop.org exposed via
2487 File::MimeInfo::Magic, if available. Either one is sufficient, but having both
2488 is better. LibMagic sometimes says 'text/x-pascal', although we have a F<.desktop>
2489 file, or says 'text/plain', but has contradicting details in its description.
2490  
2491 C<File::MimeInfo::Magic::magic> is consulted where the libmagic output is dubious. E.g. when
2492 the desciption says something interesting like 'Debian binary package (format 2.0)' but the
2493 mimetype says 'application/octet-stream'. The combination of both libraries gives us
2494 excellent reliability in the critical field of MIME type recognition.
2495  
2496 This implementation also features multi-level MIME type recognition for efficient unpacking.
2497 When e.g. unpacking a large bzipped tar archive, this saves us from creating a
2498 huge temporary tar-file which C<unpack> would extract in a second step. The multi-level recognition
2499 returns 'application/x-tar+bzip2' in this case, and allows for a MIME helper
2500 to e.g. pipe the bzip2 contents into tar (which is exactly what 'tar jxvf'
2501 does, making a very simple and efficient MIME helper).
2502  
2503 C<mime> returns a 3 or 4 element arrayref with mimetype, charset, description, diff;
2504 where diff is only present when the libfile and shared-mime-info methods disagree.
2505  
2506 In case of 'text/plain', an additional rule based on file name suffix is used to allow
2507 recognition of well known plain text pack formats.
2508 We return 'text/x-suffix-XX+plain', where XX is one of the recognized suffixes
2509 (in all lower case and without the dot). E.g. a plain mmencoded file has no
2510 header and looks like 'plain/text' to all the known magic libraries. We
2511 recognize the suffixes .mm, .b64, and .base64 for this (case insignificant).
2512 A similar rule exitst for 'application/octect-stream'. It may trigger e.g. for
2513 LZMA compressed files which fail to provide a magic number.
2514  
2515 Examples:
2516  
2517 [ 'text/x-perl', 'us-ascii', 'a /usr/bin/perl -w script text']
2518  
2519 [ 'text/x-mpegurl', 'utf-8', 'M3U playlist text',
2520 [ 'text/plain', 'application/x-mpegurl']]
2521  
2522 [ 'application/x-tar+bzip2, 'binary',
2523 "bzip2 compressed data, block size = 900k\nPOSIX tar archive (GNU)", ...]
2524  
2525 =cut
2526  
2527 sub mime
2528 94 ▶{
Subroutine mime: called CC=95 SCAR=89.6
Pod documented
2529 94 my ($self, @in) = @_;
2530  
2531 94 my %in;
2532 0 ▶ %in = %{$in[0]} if !$#in and ref $in[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not $#in and ref $in[0] eq "HASH" 0 94 94
Condition66%2/3
not $#in and ref $in[0] eq "HASH"
!ll&&!rl&&r
1930
MC/DC0%0/2
not $#in and ref $in[0] eq "HASH"
not $#in ref $in[0] eq "HASH"
Truth table66%2/3
not(A) and B
A= not $#inB= ref $in[0] eq "HASH"
ABresult
0X0
100
111
2533 94 ▶ unshift @in, 'file' if !$#in and !ref $in[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $#in or ref $in[0] 93 1 94
Condition66%2/3
$#in or ref $in[0]
l!l&&r!l&&!r
1093
MC/DC50%1/2
$#in or ref $in[0]
$#in ref $in[0]
Truth table66%2/3
A or B
A= $#inB= ref $in[0]
ABresult
1X1
011
000
2534 94 ▶ %in = @in if $#in > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $#in > 0 94 0 94
2535  
2536 94 ▶ my $flm = $self->{flm} ||= File::LibMagic->new();
Condition33%1/3
$self->{"flm"} ||= "File::LibMagic"->new
l!l&&r!l&&!r
0094
MC/DC0%0/2
$self->{"flm"} ||= "File::LibMagic"->new
$self->{"flm"} "File::LibMagic"->new
Truth table33%1/3
A ||= B
A= $self->{"flm"}B= "File::LibMagic"->new
ABresult
1X1
011
000
2537  
2538 0 ▶ unless (defined $in{buf})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $in{"buf"}) 0 0 0
2539 {
2540 0 my $fd = $in{fd};
2541 0 ▶ unless ($fd)
Branch: 1 branch
BranchTrueFalseTotal
unless ($fd) 0 0 0
2542 {
2543 open $fd, "<", $in{file} or
2544 0 ▶ return [ 'x-system/x-error', undef, "cannot open '$in{file}': $!" ];
Branch: 1 branch
BranchTrueFalseTotal
open $fd, "<", $in{"file"} or return ["x-system/x-error", undef, qq[cannot open '$in{"file"}': $!]] 0 0 0
2545 }
2546  
2547 0 ▶ my $f = $in{file}||'-';
Condition0%0/2
$in{'file'} || '-'
l!l
00
MC/DC0%0/1
$in{'file'} || '-'
$in{'file'}
Truth table0%0/2
A
A= $in{'file'}
Aresult
11
00
2548 0 $in{buf} = '';
2549 0 my $pos = tell $fd;
2550 ##bzip2 below needs a long buffer, or it returns 0.
2551 0 my $len = read $fd, $in{buf}, $UNCOMP_BUFSZ;
2552 0 ▶ return [ 'x-system/x-error', undef, "read '$f' failed: $!" ] unless defined $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
2553 0 ▶ return [ 'x-system/x-error', undef, "read '$f' failed: $len: $!" ] if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 0 0 0
2554 0 ▶ return [ 'text/x-empty', undef, 'empty' ] if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 0 0 0
2555 0 seek $fd, $pos, 0;
2556  
2557 0 ▶ close $fd unless $in{fd};
Branch: 1 branch
BranchTrueFalseTotal
unless $in{"fd"} 0 0 0
2558 }
2559  
2560  
2561 ## flm can say 'cannot open \'IP\' (No such file or directory)'
2562 ## flm can say 'CDF V2 Document, corrupt: Can\'t read SAT' (application/vnd.ms-excel)
2563 0 my $mime1 = eval { $flm->checktype_contents($in{buf}) };
2564 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
2565 0 warn $@;
2566 0 return [ 'x-system/x-error', undef, "libmimemagic exception"];
2567 }
2568 0 ▶ if ($mime1 =~ m{, corrupt: } or $mime1 =~ m{^application/octet-stream\b})
Branch: 1 branch
BranchTrueFalseTotal
if ($mime1 =~ /, corrupt: / or $mime1 =~ m[^application/octet-stream\b]) 0 0 0
Condition0%0/3
$mime1 =~ /, corrupt: / or $mime1 =~ m[^application/octet-stream\b]
l!l&&r!l&&!r
000
MC/DC0%0/2
$mime1 =~ /, corrupt: / or $mime1 =~ m[^application/octet-stream\b]
$mime1 =~ /, corrupt: / $mime1 =~ m[^application/octet-stream\b]
Truth table0%0/3
A or B
A= $mime1 =~ /, corrupt: /B= $mime1 =~ m[^application/octet-stream\b]
ABresult
1X1
011
000
2569 {
2570 # application/x-iso9660-image is reported as application/octet-stream if the buffer is short.
2571 # iso images usually start with 0x8000 bytes of all '\0'.
2572 0 ▶ print STDERR "mime: readahead buffer $UNCOMP_BUFSZ too short\n" if $self->{verbose} > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 2 0 0 0
2573 0 ▶ if (defined $in{file} and -f $in{file})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in{"file"} and -f $in{"file"}) 0 0 0
Condition0%0/3
defined $in{"file"} and -f $in{"file"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $in{"file"} and -f $in{"file"}
defined $in{"file"} -f $in{"file"}
Truth table0%0/3
A and B
A= defined $in{"file"}B= -f $in{"file"}
ABresult
0X0
100
111
2574 {
2575 0 ▶ print STDERR "mime: reopening $in{file}\n" if $self->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
2576 0 $mime1 = $flm->checktype_filename($in{file});
2577 }
2578 }
2579 0 ▶ print STDERR "flm->checktype_contents: $mime1\n" if $self->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
2580 0 ▶ $in{file} = '-' unless defined $in{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in{"file"} 0 0 0
2581  
2582 0 ▶ return [ 'x-system/x-error', undef, $mime1 ] if $mime1 =~ m{^cannot open};
Branch: 1 branch
BranchTrueFalseTotal
if $mime1 =~ /^cannot open/ 0 0 0
2583  
2584 # in SLES11 we get 'text/plain charset=utf-8' without semicolon.
2585 0 ▶ my $enc; ($mime1, $enc) = ($1,$2) if $mime1 =~ m{^(.*?);\s*(.*)$} or
Branch: 1 branch
BranchTrueFalseTotal
if $mime1 =~ /^(.*?);\s*(.*)$/ or $mime1 =~ /^(.*?)\s+(.*)$/ 0 0 0
Condition0%0/3
$mime1 =~ /^(.*?);\s*(.*)$/ or $mime1 =~ /^(.*?)\s+(.*)$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$mime1 =~ /^(.*?);\s*(.*)$/ or $mime1 =~ /^(.*?)\s+(.*)$/
$mime1 =~ /^(.*?);\s*(.*)$/ $mime1 =~ /^(.*?)\s+(.*)$/
Truth table0%0/3
A or B
A= $mime1 =~ /^(.*?);\s*(.*)$/B= $mime1 =~ /^(.*?)\s+(.*)$/
ABresult
1X1
011
000
2586 $mime1 =~ m{^(.*?)\s+(.*)$};
2587 0 ▶ $enc =~ s{^charset=}{} if defined $enc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $enc 0 0 0
2588 0 my @r = ($mime1, $enc, $flm->describe_contents($in{buf}) );
2589 0 my $mime2;
2590  
2591  
2592 0 ▶ if ($mime1 =~ m{^application/xml})
Branch: 1 branch
BranchTrueFalseTotal
if ($mime1 =~ m[^application/xml]) 0 0 0
2593 {
2594 # This is horrible from a greedy text cruncher perspective:
2595 # although xml is a plain text syntax, it is reported by flm to be
2596 # outside text/*
2597 0 $r[0] = "text/x-application-xml";
2598 }
2599  
2600 0 ▶ if ($mime1 =~ m{^text/x-(?:pascal|fortran)$})
Branch: 2 branches
BranchTrueFalseTotal
if ($mime1 =~ m[^text/x-(?:pascal|fortran)$]) { } 0 0 0
elsif ($mime1 eq 'text/plain' and $r[2] =~ /(?:PostScript|font)/i or $mime1 eq 'application/postscript') { } 0 0 0
2601 {
2602 # xterm.desktop
2603 # ['text/x-pascal; charset=utf-8','UTF-8 Unicode Pascal program text']
2604 # 'application/x-desktop'
2605 #
2606 # Times-Roman.afm
2607 # ['text/x-fortran; charset=us-ascii','ASCII font metrics']
2608 # 'application/x-font-afm'
2609 #
2610 # debian/rules
2611 # ['text/x-pascal; charset=us-ascii','a /usr/bin/make -f script text']
2612 # 'text/x-makefile'
2613 0 ▶ if ($mime2 ||= eval { open my $fd,'<',\$in{buf}; File::MimeInfo::Magic::magic($fd); })
Branch: 1 branch
BranchTrueFalseTotal
if ($mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }) 0 0 0
Condition0%0/3
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
$mime2 eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
Truth table0%0/3
A ||= B
A= $mime2B= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
ABresult
1X1
011
000
2614 {
2615 0 ▶ $r[0] = "text/$1" if $mime2 =~ m{/(\S+)};
Branch: 1 branch
BranchTrueFalseTotal
if $mime2 =~ m[/(\S+)] 0 0 0
2616 }
2617 }
2618 ▶ elsif (($mime1 eq 'text/plain' and $r[2] =~ m{(?:PostScript|font)}i)
Condition0%0/6
$mime1 eq "text/plain" and $r[2] =~ /(?:PostScript|font)/i or $mime1 eq "application/postscript"
l!l&&r!l&&!r
000
$mime1 eq "text/plain" and $r[2] =~ /(?:PostScript|font)/i
!ll&&!rl&&r
000
MC/DC0%0/3
$mime1 eq "text/plain" and $r[2] =~ /(?:PostScript|font)/i or $mime1 eq "application/postscript"
$mime1 eq "text/plain" $r[2] =~ /(?:PostScript|font)/i $mime1 eq "application/postscript"
Truth table0%0/5
A and B or C
A= $mime1 eq "text/plain"B= $r[2] =~ /(?:PostScript|font)/iC= $mime1 eq "application/postscript"
ABCresult
11X1
0X11
1011
0X00
1000
2619 or ($mime1 eq 'application/postscript'))
2620 {
2621 # 11.3 says:
2622 # IPA.pfa
2623 # ['text/plain; charset=us-ascii','PostScript Type 1 font text (OmegaSerifIPA 001.000)']
2624 # sles11 says:
2625 # IPA.pfa
2626 # ['application/postscript', undef, 'PostScript document text']
2627 #
2628 # mime2 = 'application/x-font-type1'
2629 # $mime2 = eval { File::MimeInfo::Magic::mimetype($in{file}); };
2630 0 ▶ $mime2 ||= eval { open my $fd,'<',\$in{buf}; File::MimeInfo::Magic::magic($fd); };
Condition0%0/3
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
$mime2 eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
Truth table0%0/3
A ||= B
A= $mime2B= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
ABresult
1X1
011
000
2631 0 ▶ if ($mime2 and $mime2 =~ m{^(.*)/(.*)$})
Branch: 1 branch
BranchTrueFalseTotal
if ($mime2 and $mime2 =~ m[^(.*)/(.*)$]) 0 0 0
Condition0%0/3
$mime2 and $mime2 =~ m[^(.*)/(.*)$]
!ll&&!rl&&r
000
MC/DC0%0/2
$mime2 and $mime2 =~ m[^(.*)/(.*)$]
$mime2 $mime2 =~ m[^(.*)/(.*)$]
Truth table0%0/3
A and B
A= $mime2B= $mime2 =~ m[^(.*)/(.*)$]
ABresult
0X0
100
111
2632 {
2633 0 my ($a,$b) = ($1,$2);
2634 0 ▶ $a = 'text' if $r[2] =~ m{\btext\b}i;
Branch: 1 branch
BranchTrueFalseTotal
if $r[2] =~ /\btext\b/i 0 0 0
2635 0 $r[0] = "$a/$b";
2636 }
2637 }
2638  
2639 0 ▶ if ($r[0] eq 'text/plain' or
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] eq "text/plain" or $r[0] eq "application/octet-stream") 0 0 0
Condition0%0/3
$r[0] eq "text/plain" or $r[0] eq "application/octet-stream"
l!l&&r!l&&!r
000
MC/DC0%0/2
$r[0] eq "text/plain" or $r[0] eq "application/octet-stream"
$r[0] eq "text/plain" $r[0] eq "application/octet-stream"
Truth table0%0/3
A or B
A= $r[0] eq "text/plain"B= $r[0] eq "application/octet-stream"
ABresult
1X1
011
000
2640 $r[0] eq 'application/octet-stream')
2641 {
2642 # hmm, are we sure? No, if the description contradicts:
2643 #
2644 0 ▶ $r[0] = "text/x-uuencode" if $r[2] eq 'uuencoded or xxencoded text';
Branch: 1 branch
BranchTrueFalseTotal
if $r[2] eq "uuencoded or xxencoded text" 0 0 0
2645  
2646 # bin/floor
2647 # ['text/x-pascal; charset=us-ascii','a /usr/bin/tclsh script text']
2648 # 'text/plain'
2649 0 ▶ $r[0] = "text/x-$2" if $r[2] =~ m{^a (\S*/)?([^/\s]+) .*script text$}i;
Branch: 1 branch
BranchTrueFalseTotal
if $r[2] =~ m[^a (\S*/)?([^/\s]+) .*script text$]i 0 0 0
2650 0 ▶ if ($r[2] =~ m{\bimage\b})
Branch: 1 branch
BranchTrueFalseTotal
if ($r[2] =~ /\bimage\b/) 0 0 0
2651 {
2652 # ./opengl/test.tga
2653 # ['application/octet-stream; charset=binary','Targa image data - RGB 128 x 128']
2654 # 'image/x-tga'
2655 0 ▶ $mime2 ||= eval { open my $fd,'<',\$in{buf}; File::MimeInfo::Magic::magic($fd); };
Condition0%0/3
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
$mime2 eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
Truth table0%0/3
A ||= B
A= $mime2B= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
ABresult
1X1
011
000
2656 0 ▶ $r[0] = $mime2 if $mime2 and $mime2 =~ m{^image/};
Branch: 1 branch
BranchTrueFalseTotal
if $mime2 and $mime2 =~ m[^image/] 0 0 0
Condition0%0/3
$mime2 and $mime2 =~ m[^image/]
!ll&&!rl&&r
000
MC/DC0%0/2
$mime2 and $mime2 =~ m[^image/]
$mime2 $mime2 =~ m[^image/]
Truth table0%0/3
A and B
A= $mime2B= $mime2 =~ m[^image/]
ABresult
0X0
100
111
2657 }
2658 }
2659  
2660 0 ▶ if ($r[0] eq 'application/octet-stream')
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] eq "application/octet-stream") 0 0 0
2661 {
2662 # it can't get much worse, can it?
2663 ##
2664 # dotdot.tar.lzma
2665 # {'File::MimeInfo::Magic' => 'application/x-lzma-compressed-tar'} -- no, that was suffix based!
2666 # {'File::LibMagic' => ['application/octet-stream; charset=binary','data']}
2667 0 ▶ $mime2 ||= eval { open my $fd,'<',\$in{buf}; File::MimeInfo::Magic::magic($fd); };
Condition0%0/3
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
$mime2 eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
Truth table0%0/3
A ||= B
A= $mime2B= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
ABresult
1X1
011
000
2668 #
2669 # File::LibMagic misreads monotone-0.99.1/monotone.info-1 as app/bin
2670 # File::MimeInfo::Magic::magic() returns undef for that one.
2671 # But perl itself does not agree:
2672 0 ▶ $mime2 ||= 'application/x-text-mixed' if -T $in{file};
Branch: 1 branch
BranchTrueFalseTotal
if -T $in{"file"} 0 0 0
Condition0%0/2
$mime2 ||= "application/x-text-mixed"
l!l
00
MC/DC0%0/1
$mime2 ||= "application/x-text-mixed"
$mime2
Truth table0%0/2
A
A= $mime2
Aresult
11
00
2673  
2674 0 ▶ $r[0] = $mime2 if $mime2;
Branch: 1 branch
BranchTrueFalseTotal
if $mime2 0 0 0
2675 }
2676  
2677 0 ▶ if ($r[0] eq 'application/octet-stream')
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] eq "application/octet-stream") 0 0 0
2678 {
2679 0 ▶ if ($r[2] =~ m{\bcpio\s+archive\b}i)
Branch: 1 branch
BranchTrueFalseTotal
if ($r[2] =~ /\bcpio\s+archive\b/i) 0 0 0
2680 {
2681 # Mac pax files are gzipped cpio: 'ASCII cpio archive (pre-SVR4 or odc)'
2682 0 $r[0] = 'application/x-cpio';
2683 }
2684 }
2685  
2686 0 my $uncomp_buf = '';
2687  
2688 0 ▶ if ($r[0] eq 'application/octet-stream')
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] eq "application/octet-stream") 0 0 0
2689 {
2690 ## lzma is an extremly bad format. It has no magic.
2691 #
2692 # WARNING from Compress::unLZMA
2693 # "This version only implements in-memory decompression (patches are welcomed).
2694 # There is no way to recognize a valid LZMA encoded file with the SDK.
2695 # So, in some cases, you can crash your script if you try to uncompress a
2696 # non valid LZMA encoded file."
2697 # Does this also apply to us?
2698 #
2699 # -- hmm, maybe we better leave it at calling lzcat.
2700 # Trade in "always a bit expensive" versus "sometimes crashing"...
2701 #
2702 # my $lztest = `sh -c "/usr/bin/lzcat < $in{file} | head -c 1k > /dev/null" 2>&1`;
2703 # # -> /usr/bin/lzcat: (stdin): File format not recognized
2704 # if ($lztest !~ m{(not recognized|error)}i)
2705 # {
2706 # $r[0] = 'application/x-lzma';
2707 # }
2708  
2709 0 ▶ if (10 < length $in{buf})
Branch: 1 branch
BranchTrueFalseTotal
if (10 < length $in{"buf"}) 0 0 0
2710 {
2711 16 ▶ no strict 'subs'; # Compress::Raw::Lzma::AloneDecoder, LZMA_OK, LZMA_STREAM_END
Subroutine BEGIN: called CC=1 SCAR=0.0
2712  
2713 0 my $saved_input = $in{buf};
2714 0 my ($lz, $stat) = eval { Compress::Raw::Lzma::AloneDecoder->new(-Bufsize => $UNCOMP_BUFSZ, -LimitOutput => 1); };
2715 0 ▶ if ($lz)
Branch: 1 branch
BranchTrueFalseTotal
if ($lz) 0 0 0
2716 {
2717 0 $stat = $lz->code($in{buf}, $uncomp_buf);
2718 0 ▶ if (($stat == LZMA_OK or $stat == LZMA_STREAM_END)
Branch: 1 branch
BranchTrueFalseTotal
if ($stat == "LZMA_OK" || $stat == "LZMA_STREAM_END" and length $uncomp_buf > length $saved_input) 0 0 0
Condition0%0/6
$stat == "LZMA_OK" || $stat == "LZMA_STREAM_END" and length $uncomp_buf > length $saved_input
!ll&&!rl&&r
000
$stat == "LZMA_OK" or $stat == "LZMA_STREAM_END"
l!l&&r!l&&!r
000
MC/DC0%0/3
$stat == "LZMA_OK" || $stat == "LZMA_STREAM_END" and length $uncomp_buf > length $saved_input
$stat == "LZMA_OK" $stat == "LZMA_STREAM_END" length $uncomp_buf > length $saved_input
Truth table0%0/5
A or B and C
A= $stat == "LZMA_OK"B= $stat == "LZMA_STREAM_END"C= length $uncomp_buf > length $saved_input
ABCresult
00X0
1X00
0100
1X11
0111
2719 and
2720 (length($uncomp_buf) > length($saved_input)))
2721 {
2722 0 $r[0] = "application/x-lzma";
2723 0 $r[2] = "LZMA compressed data, no magic";
2724 }
2725 # This decompressor consumes the input.
2726 0 $in{buf} = $saved_input;
2727 }
2728 }
2729 }
2730 # printf STDERR "in-buf = %d bytes\n", length($in{buf});
2731  
2732 0 ▶ if ($r[0] =~ m{^application/(?:x-)?gzip$})
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] =~ m[^application/(?:x-)?gzip$]) 0 0 0
2733 {
2734 0 my ($gz, $stat) = eval { new Compress::Raw::Zlib::Inflate( -WindowBits => WANT_GZIP() ); };
2735 0 ▶ if ($gz)
Branch: 1 branch
BranchTrueFalseTotal
if ($gz) 0 0 0
2736 {
2737 0 my $stat = $gz->inflate($in{buf}, $uncomp_buf);
2738 # printf STDERR "stat=%s, uncomp=%d bytes \n", $stat, length($uncomp_buf);
2739 }
2740 }
2741  
2742 ## bzip2 is not nice for stacked mime checking.
2743 ## It needs a huge input buffer that we do not normally provide.
2744 ## We only support it at the top of a stack, where we acquire enough additional
2745 ## input until bzip2 is happy.
2746 0 ▶ if ($r[0] =~ m{^application/(?:x-)?bzip2$} && !$in{recursion})
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] =~ m[^application/(?:x-)?bzip2$] and not $in{"recursion"}) 0 0 0
Condition0%0/3
$r[0] =~ m[^application/(?:x-)?bzip2$] and not $in{"recursion"}
!ll&&!rl&&r
000
MC/DC0%0/2
$r[0] =~ m[^application/(?:x-)?bzip2$] and not $in{"recursion"}
$r[0] =~ m[^application/(?:x-)?bzip2$] not $in{"recursion"}
Truth table0%0/3
A and not(B)
A= $r[0] =~ m[^application/(?:x-)?bzip2$]B= not $in{"recursion"}
ABresult
0X0
100
111
2747 {
2748 0 my $limitOutput = 1;
2749 0 my ($bz, $stat) = eval { new Compress::Raw::Bunzip2 0, 0, 0, 0, $limitOutput; };
2750 0 ▶ if ($bz)
Branch: 1 branch
BranchTrueFalseTotal
if ($bz) 0 0 0
2751 {
2752 ## this only works if this is a first level call.
2753 0 ▶ open my $IN, "<", $in{file} unless $in{file} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
unless $in{"file"} eq "-" 0 0 0
2754 0 seek $IN, length($in{buf}), 0;
2755 0 ▶ while (!length $uncomp_buf)
Branch: 1 branch
BranchTrueFalseTotal
unless (length $uncomp_buf) 0 0 0
2756 {
2757 0 my $stat = $bz->bzinflate($in{buf}, $uncomp_buf);
2758 # $bz->bzflush($uncomp_buf); # wishful thinking....
2759 0 ▶ last if length($in{buf}); # did not consume, strange.
Branch: 1 branch
BranchTrueFalseTotal
if length $in{"buf"} 0 0 0
2760 0 ▶ last if length $stat; # something wrong, or file ends.
Branch: 1 branch
BranchTrueFalseTotal
if length $stat 0 0 0
2761 0 ▶ last unless read $IN, $in{buf}, 10*1024, length($in{buf}); # try to get more data
Branch: 1 branch
BranchTrueFalseTotal
unless read $IN, $in{"buf"}, 10240, length $in{"buf"} 0 0 0
2762 }
2763 0 my $slurped = tell $IN; # likely to get ca. 800k yacc!
2764 0 close $IN;
2765 }
2766 }
2767  
2768 ## try to get at the second level mime type, for some well known linear compressors.
2769 0 ▶ while (length $uncomp_buf && $r[0] =~ m{^application/(x-)?([+\w]+)$} && !$in{recursion})
Branch: 1 branch
BranchTrueFalseTotal
if (length $uncomp_buf and $r[0] =~ m[^application/(x-)?([+\w]+)$] and not $in{"recursion"}) 0 0 0
Condition0%0/6
length $uncomp_buf and $r[0] =~ m[^application/(x-)?([+\w]+)$] and not $in{"recursion"}
!ll&&!rl&&r
000
length $uncomp_buf and $r[0] =~ m[^application/(x-)?([+\w]+)$]
!ll&&!rl&&r
000
MC/DC0%0/3
length $uncomp_buf and $r[0] =~ m[^application/(x-)?([+\w]+)$] and not $in{"recursion"}
length $uncomp_buf $r[0] =~ m[^application/(x-)?([+\w]+)$] not $in{"recursion"}
Truth table0%0/4
A and B and not(C)
A= length $uncomp_bufB= $r[0] =~ m[^application/(x-)?([+\w]+)$]C= not $in{"recursion"}
ABCresult
0XX0
10X0
1100
1111
2770 {
2771 0 my $compname = $2;
2772 0 my $next_uncomp_buf = '';
2773  
2774 0 my $m2 = $self->mime(buf => $uncomp_buf, file => $in{file}, uncomp => \$next_uncomp_buf, recursion => 1);
2775 # protecting against http://www.maximumcompression.com/selfgz.gz
2776 0 ▶ last if length($uncomp_buf) == length($next_uncomp_buf);
Branch: 1 branch
BranchTrueFalseTotal
if length $uncomp_buf == length $next_uncomp_buf 0 0 0
2777 0 my ($a,$xminus,$b) = ($m2->[0] =~ m{^(.*)/(x-)?(.*)$});
2778 0 ▶ if ($a eq 'application')
Branch: 1 branch
BranchTrueFalseTotal
if ($a eq 'application') { } 0 0 0
2779 {
2780 0 $r[0] = "application/x-$b+$compname"
2781 }
2782 else
2783 {
2784 0 $r[0] = "application/x-$a-$b+$compname"
2785 }
2786 0 $r[2] .= "\n" . $m2->[2];
2787 0 $uncomp_buf = $next_uncomp_buf;
2788 }
2789  
2790 0 ▶ if ($r[0] eq 'application/unknown+zip' and $r[2] =~ m{\btext\b}i)
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] eq "application/unknown+zip" and $r[2] =~ /\btext\b/i) 0 0 0
Condition0%0/3
$r[0] eq "application/unknown+zip" and $r[2] =~ /\btext\b/i
!ll&&!rl&&r
000
MC/DC0%0/2
$r[0] eq "application/unknown+zip" and $r[2] =~ /\btext\b/i
$r[0] eq "application/unknown+zip" $r[2] =~ /\btext\b/i
Truth table0%0/3
A and B
A= $r[0] eq "application/unknown+zip"B= $r[2] =~ /\btext\b/i
ABresult
0X0
100
111
2791 {
2792 # empty.odt
2793 # ['application/unknown+zip; charset=binary','Zip archive data, at least v2.0 to extract, mime type application/vnd OpenDocument Text']
2794 # application/vnd.oasis.opendocument.text
2795 0 ▶ if ($mime2 ||= eval { open my $fd,'<',\$in{buf}; File::MimeInfo::Magic::magic($fd); })
Branch: 1 branch
BranchTrueFalseTotal
if ($mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }) 0 0 0
Condition0%0/3
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$mime2 ||= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
$mime2 eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
Truth table0%0/3
A ||= B
A= $mime2B= eval { do { open my $fd, "<", \$in{"buf"}; File::MimeInfo::Magic::magic($fd) } }
ABresult
1X1
011
000
2796 {
2797 0 ▶ $mime2 .= '+zip' unless $mime2 =~ m{\+zip}i;
Branch: 1 branch
BranchTrueFalseTotal
unless $mime2 =~ /\+zip/i 0 0 0
2798 0 ▶ $r[0] = $mime2 if $mime2 =~ m{^application/};
Branch: 1 branch
BranchTrueFalseTotal
if $mime2 =~ m[^application/] 0 0 0
2799 }
2800 }
2801 0 ▶ $r[0] .= '+zip' if $r[0] =~ m{^application/vnd\.oasis\.opendocument\.text$};
Branch: 1 branch
BranchTrueFalseTotal
if $r[0] =~ m[^application/vnd\.oasis\.opendocument\.text$] 0 0 0
2802  
2803 0 ▶ if ($r[0] eq 'text/plain' and $in{file} =~ m{\.(mm|b64|base64)$}i)
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] eq "text/plain" and $in{"file"} =~ /\.(mm|b64|base64)$/i) 0 0 0
Condition0%0/3
$r[0] eq "text/plain" and $in{"file"} =~ /\.(mm|b64|base64)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$r[0] eq "text/plain" and $in{"file"} =~ /\.(mm|b64|base64)$/i
$r[0] eq "text/plain" $in{"file"} =~ /\.(mm|b64|base64)$/i
Truth table0%0/3
A and B
A= $r[0] eq "text/plain"B= $in{"file"} =~ /\.(mm|b64|base64)$/i
ABresult
0X0
100
111
2804 {
2805 0 my $suf = lc $1;
2806 0 $r[0] = "text/x-suffix-$suf+plain";
2807 }
2808  
2809 0 ▶ if ($r[0] eq 'application/octet-stream' and $in{file} =~ m{\.(lzma|zx|lz)$}i)
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] eq "application/octet-stream" and $in{"file"} =~ /\.(lzma|zx|lz)$/i) 0 0 0
Condition0%0/3
$r[0] eq "application/octet-stream" and $in{"file"} =~ /\.(lzma|zx|lz)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$r[0] eq "application/octet-stream" and $in{"file"} =~ /\.(lzma|zx|lz)$/i
$r[0] eq "application/octet-stream" $in{"file"} =~ /\.(lzma|zx|lz)$/i
Truth table0%0/3
A and B
A= $r[0] eq "application/octet-stream"B= $in{"file"} =~ /\.(lzma|zx|lz)$/i
ABresult
0X0
100
111
2810 {
2811 0 my $suf = lc $1;
2812 0 $r[0] = "application/x-suffix-$suf+octet-stream";
2813 }
2814  
2815 0 ▶ if ($r[0] =~ m{^application/x-(ms-dos-|)executable$})
Branch: 1 branch
BranchTrueFalseTotal
if ($r[0] =~ m[^application/x-(ms-dos-|)executable$]) 0 0 0
2816 {
2817 0 ▶ if (-x '/usr/bin/upx')
Branch: 1 branch
BranchTrueFalseTotal
if (-x "/usr/bin/upx") 0 0 0
2818 {
2819 # upx refuses to read symlinks. Work around this.
2820 0 my $in_file = $in{file};
2821 0 ▶ $in_file = readlink($in{file}) if -l $in{file};
Branch: 1 branch
BranchTrueFalseTotal
if -l $in{"file"} 0 0 0
2822 # Bound this probe: a hostile executable could hang upx. helper_timeout kills it cleanly
2823 # (whole process group); a timeout just skips the cosmetic '+upx' classification.
2824 0 ▶ $r[0] .= '+upx'
Branch: 1 branch
BranchTrueFalseTotal
unless run(["/usr/bin/upx", "-q", "-q", "-t", $in_file], {"every", 2, "helper_timeout", 30, "out_err", "/dev/null"}) 0 0 0
2825 unless run(['/usr/bin/upx', '-q', '-q', '-t', $in_file], {every => 2, helper_timeout => 30, out_err => '/dev/null'});
2826 }
2827 }
2828  
2829 0 ▶ ${$in{uncomp}} = $uncomp_buf if ref $in{uncomp} eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $in{"uncomp"} eq "SCALAR" 0 0 0
2830 0 ▶ $r[3] = [ $mime1, $mime2 ] if $mime1 ne $r[0] or ($mime2 and $mime2 ne $mime1);
Branch: 1 branch
BranchTrueFalseTotal
if $mime1 ne $r[0] or $mime2 and $mime2 ne $mime1 0 0 0
Condition0%0/6
$mime1 ne $r[0] or $mime2 and $mime2 ne $mime1
l!l&&r!l&&!r
000
$mime2 and $mime2 ne $mime1
!ll&&!rl&&r
000
MC/DC0%0/3
$mime1 ne $r[0] or $mime2 and $mime2 ne $mime1
$mime1 ne $r[0] $mime2 $mime2 ne $mime1
Truth table0%0/4
A or B and C
A= $mime1 ne $r[0]B= $mime2C= $mime2 ne $mime1
ABCresult
1XX1
0111
00X0
0100
2831  
2832 0 return \@r;
2833 }
2834  
2835 =head1 MIME TYPE DETECTION
2836  
2837 File::Unpack2 identifies files by content, not by name. Detection is layered:
2838 L<File::LibMagic> (the same C<libmagic> engine as F</usr/bin/file>) is the primary
2839 source of mime type, charset and a human description; L<File::MimeInfo::Magic>
2840 (the freedesktop.org shared-mime-info database) fills the gaps where libmagic is
2841 weak; and a little extra logic on top recognises compression that carries no
2842 usable magic of its own, most notably raw LZMA. The C<description> string is
2843 cross-checked against the mime type to catch mislabellings before a helper is
2844 chosen. Both magic modules are loaded lazily and only L</mime> requires them.
2845  
2846 =head1 SEE ALSO
2847  
2848 =over 2
2849  
2850 =item *
2851  
2852 L<Cavil|https://github.com/openSUSE/cavil> - the openSUSE legal review system this
2853 module is developed for.
2854  
2855 =item *
2856  
2857 L<File::LibMagic>, L<File::MimeInfo::Magic> - the mime type engines.
2858  
2859 =item *
2860  
2861 L<IPC::Run> - used to run and supervise the external mime helpers.
2862  
2863 =item *
2864  
2865 The C<docs/Architecture.md> document in the distribution, for a prose overview.
2866  
2867 =back
2868  
2869 =head1 REPOSITORY
2870  
2871 L<https://github.com/openSUSE/perl-File-Unpack2>
2872  
2873 =head1 AUTHOR
2874  
2875 Originally written by Juergen Weigert E<lt>jnw@cpan.orgE<gt>. Now maintained by
2876 Sebastian Riedel E<lt>sriedel@suse.comE<gt> and the SUSE team as a dependency of
2877 Cavil.
2878  
2879 =head1 LICENSE AND COPYRIGHT
2880  
2881 Copyright (C) 2010-2013 Juergen Weigert, (C) 2023-2026 Sebastian Riedel.
2882  
2883 This program is free software; you can redistribute it and/or modify it under the
2884 same terms as Perl itself, that is, either the GNU General Public License or the
2885 Artistic License. See L<https://dev.perl.org/licenses/> for more information.
2886  
2887 =cut
2888  
2889 16 1; # End of File::Unpack2
↑ Summary script/file_unpack2 »