lib/File/Unpack2.pm

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