Packdrakeng.pm

filter: Statementstmt 69.5% 270 / 388 Branchbran 65.9% 124 / 188 Conditioncond 56.7% 38 / 67 MC/DCmcdc 30.0% 12 / 40 Subroutinesub 78.1% 25 / 32 Podpod 39.1% 9 / 23 totaltotal 64.7% 478 / 738 CC 137 SCAR 29.7
CC
137
Cov
67%
extract
46.7
extract_block
42.0
uncompress_handle
37.2
CRAP 19.4
« Packdrakeng/zlib.pm ↑ Summary
1 ##- Nanar <nanardon@mandriva.org>
2 ##-
3 ##- This program is free software; you can redistribute it and/or modify
4 ##- it under the terms of the GNU General Public License as published by
5 ##- the Free Software Foundation; either version 2, or (at your option)
6 ##- any later version.
7 ##-
8 ##- This program is distributed in the hope that it will be useful,
9 ##- but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ##- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ##- GNU General Public License for more details.
12 ##-
13 ##- You should have received a copy of the GNU General Public License
14 ##- along with this program; if not, write to the Free Software
15 ##- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  
17 # $Id: Packdrakeng.pm 225631 2007-08-09 11:45:44Z nanardon $
18  
19 package MDV::Packdrakeng;
20  
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use POSIX qw(O_WRONLY O_TRUNC O_CREAT O_RDONLY O_APPEND);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 our $VERSION = '1.13';
26  
27 2 my ($toc_header, $toc_footer) =
28 ('cz[0', '0]cz');
29  
30 # File::Temp qw(tempfile) hack to not require it
31 7 ▶sub tempfile {
Subroutine tempfile: called CC=6 SCAR=18.7
Pod undocumented
32 7 my ($count, $fname, $handle) = (0, undef, undef);
33 7 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen $handle, $fname, 1089 or do { do { ++$count > 10 and do { warn "Can't create temporary file ($fname)"; return undef, undef }; $fname = sprintf("%s/packdrakeng.%s.%s", $ENV{"TMPDIR"} || "/tmp", $$, join("", map({$_ = rand 51; $_ += $_ > 25 && $_ < 32 ? 91 : 65; chr $_;} 0..4))) } } 7 0 7
34 7 ▶ ++$count > 10 and do {
Branch: 1 branch
BranchTrueFalseTotal
++$count > 10 and do { warn "Can't create temporary file ($fname)"; return undef, undef } 0 7 7
35 0 warn "Can't create temporary file ($fname)";
36 0 return (undef, undef);
37 };
38 $fname = sprintf("%s/packdrakeng.%s.%s",
39 $ENV{TMPDIR} || '/tmp',
40 $$,
41 # Generating an random name
42 7 ▶ join("", map { $_=rand(51); $_ += $_ > 25 && $_ < 32 ? 91 : 65 ; chr($_) } (0 .. 4)));
Branch: 1 branch
BranchTrueFalseTotal
$_ > 25 && $_ < 32 ? : 5 30 35
Condition80%4/5
$ENV{'TMPDIR'} || '/tmp'
l!l
07
$_ > 25 and $_ < 32
!ll&&!rl&&r
15155
MC/DC0%0/1
$ENV{'TMPDIR'} || '/tmp'
$ENV{'TMPDIR'}
MC/DC100%2/2
$_ > 25 and $_ < 32
$_ > 25 $_ < 32
Truth table50%1/2
A
A= $ENV{'TMPDIR'}
Aresult
11
00
Truth table100%3/3
A and B
A= $_ > 25B= $_ < 32
ABresult
0X0
100
111
43 } while !sysopen($handle, $fname, O_WRONLY | O_APPEND | O_CREAT);
44 7 return ($handle, $fname);
45 }
46  
47 10 ▶sub method_info { "external $_[0]->{compress_method}/$_[0]->{uncompress_method} $VERSION" }
Subroutine method_info: called CC=1 SCAR=0.0
Pod undocumented
48  
49 16 ▶sub _new {
Subroutine _new: called CC=7 SCAR=26.4
50 16 my ($class, %options) = @_;
51  
52 my $pack = {
53 filename => $options{archive},
54  
55 compress_method => $options{compress},
56 uncompress_method => $options{uncompress},
57 force_extern => $options{extern} || 0, # Don't use perl-zlib
58 noargs => $options{noargs},
59  
60 # compression level, aka -X gzip or bzip option
61 level => defined($options{comp_level}) ? $options{comp_level} : 6,
62  
63 # A compressed block will contain 400k of compressed data
64 block_size => defined($options{block_size}) ? $options{block_size} : 400 * 1024,
65 bufsize => $options{bufsize} || 65536, # Arbitrary buffer size to read files
66  
67 # Internal data
68 handle => undef, # Archive handle
69  
70 # Toc information
71 files => {}, # filename => { off, size, coff, csize }
72 dir => {}, # dir => no matter what value
73 'symlink' => {}, # file => link
74  
75 coff => 0, # end of current compressed data
76  
77 # Data we need keep in memory to achieve the storage
78 current_block_files => {}, # Files in pending compressed block
79 current_block_csize => 0, # Actual size in pending compressed block
80 current_block_coff => 0, # The block block location (offset)
81 current_block_off => 0, # Actual uncompressed file offset within the pending block
82  
83 cstream_data => undef, # Wrapper data we need to keep in memory (compression)
84 ustream_data => undef, # Wrapper data we need to keep in memory (uncompression)
85  
86 # log and verbose function:
87 log => $options{quiet}
88 0 ▶ ? sub { our $error = "$_[0]\n" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 ▶ : sub { our $error = "$_[0]\n"; warn $error },
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 debug => $options{debug}
91 0 ▶ ? sub { my @w = @_; $w[0] = "Debug: $w[0]\n"; printf STDERR @w }
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 76 ▶ : sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
93 16 ▶ };
Branch: 4 branches
BranchTrueFalseTotal
defined $options{'comp_level'} ? : 0 16 16
defined $options{'block_size'} ? : 0 16 16
$options{'quiet'} ? : 0 16 16
$options{'debug'} ? : 0 16 16
Condition75%3/4
$options{'extern'} || 0
l!l
610
$options{'bufsize'} || 65536
l!l
016
MC/DC100%1/1
$options{'extern'} || 0
$options{'extern'}
MC/DC0%0/1
$options{'bufsize'} || 65536
$options{'bufsize'}
Truth table100%2/2
A
A= $options{'extern'}
Aresult
11
00
Truth table50%1/2
A
A= $options{'bufsize'}
Aresult
11
00
94  
95 16 bless($pack, $class)
96 }
97  
98 8 ▶sub new {
Subroutine new: called CC=2 SCAR=7.3
Pod documented
99 8 my ($class, %options) = @_;
100 8 my $pack = _new($class, %options);
101 8 ▶ sysopen($pack->{handle}, $pack->{filename}, O_WRONLY | O_TRUNC | O_CREAT) or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen $pack->{"handle"}, $pack->{"filename"}, 577 or do { $pack->{"log"}->(qq[Can't open $pack->{"filename"}: $!]); return undef } 8 0 8
102 0 $pack->{log}("Can't open $pack->{filename}: $!");
103 0 return undef;
104 };
105 8 $pack->choose_compression_method();
106 8 $pack->{need_build_toc} = 1;
107 $pack->{debug}(
108 8 "Creating new archive with %s.",
109 $pack->method_info(),
110 );
111 8 $pack
112 }
113  
114 8 ▶sub open {
Subroutine open: called CC=3 SCAR=12.0
Pod documented
115 8 my ($class, %options) = @_;
116 8 my $pack = _new($class, %options);
117 8 ▶ sysopen($pack->{handle}, $pack->{filename}, O_RDONLY) or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen $pack->{"handle"}, $pack->{"filename"}, 0 or do { $pack->{"log"}->(qq[Can't open $pack->{"filename"}: $!]); return undef } 8 0 8
118 0 $pack->{log}("Can't open $pack->{filename}: $!");
119 0 return undef;
120 };
121 8 ▶ $pack->read_toc() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$pack->read_toc or return undef 8 0 8
122 8 $pack->{debug}("Opening archive with %s.",
123 $pack->method_info(),
124 );
125 8 $pack
126 }
127  
128 # look $pack->{(un)compressed_method} and setup functions/commands to use
129 # Have some facility about detecting we want gzip/bzip
130 16 ▶sub choose_compression_method {
Subroutine choose_compression_method: called CC=12 SCAR=24.9
Pod undocumented
131 16 my ($pack) = @_;
132  
133 (!defined($pack->{compress_method}) && !defined($pack->{uncompress_method}))
134 16 ▶ and $pack->{compress_method} = "gzip";
Branch: 1 branch
BranchTrueFalseTotal
defined $pack->{"compress_method"} or defined $pack->{"uncompress_method"} or $pack->{"compress_method"} = "gzip" 13 3 16
Condition100%3/3
defined $pack->{"compress_method"} or defined $pack->{"uncompress_method"}
l!l&&r!l&&!r
853
MC/DC100%2/2
defined $pack->{"compress_method"} or defined $pack->{"uncompress_method"}
defined $pack->{"compress_method"} defined $pack->{"uncompress_method"}
Truth table100%3/3
A or B
A= defined $pack->{"compress_method"}B= defined $pack->{"uncompress_method"}
ABresult
1X1
011
000
135 16 ▶ my $test_method = $pack->{compress_method} || $pack->{uncompress_method};
Condition66%2/3
$pack->{'compress_method'} || $pack->{'uncompress_method'}
l!l&&r!l&&!r
1150
MC/DC0%0/2
$pack->{'compress_method'} || $pack->{'uncompress_method'}
$pack->{'compress_method'} $pack->{'uncompress_method'}
Truth table66%2/3
A || B
A= $pack->{'compress_method'}B= $pack->{'uncompress_method'}
ABresult
1X1
011
000
136  
137 16 ▶ $test_method =~ m/^bzip2|^bunzip2/ and do {
Branch: 1 branch
BranchTrueFalseTotal
$test_method =~ /^bzip2|^bunzip2/ and do { $pack->{"compress_method"} ||= "bzip2" } 2 14 16
138 2 ▶ $pack->{compress_method} ||= "bzip2";
Condition50%1/2
$pack->{"compress_method"} ||= "bzip2"
l!l
20
MC/DC0%0/1
$pack->{"compress_method"} ||= "bzip2"
$pack->{"compress_method"}
Truth table50%1/2
A
A= $pack->{"compress_method"}
Aresult
11
00
139 };
140 16 ▶ $test_method =~ m/^gzip|^gunzip/ and do {
Branch: 1 branch
BranchTrueFalseTotal
$test_method =~ /^gzip|^gunzip/ and do { $pack->{"compress_method"} ||= "gzip"; unless ($pack->{"force_extern"}) { eval { do { require Compress::Zlib; require MDV::Packdrakeng::zlib; bless $pack, "MDV::Packdrakeng::zlib" } }; } } 12 4 16
141 12 ▶ $pack->{compress_method} ||= "gzip";
Condition100%2/2
$pack->{"compress_method"} ||= "gzip"
l!l
75
MC/DC100%1/1
$pack->{"compress_method"} ||= "gzip"
$pack->{"compress_method"}
Truth table100%2/2
A
A= $pack->{"compress_method"}
Aresult
11
00
142 12 ▶ if (!$pack->{force_extern}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pack->{"force_extern"}) 8 4 12
143 8 eval {
144 8 require Compress::Zlib; #- need this to ensure that Packdrakeng::zlib will load properly
145 8 require MDV::Packdrakeng::zlib;
146  
147 8 bless($pack, 'MDV::Packdrakeng::zlib');
148 };
149 }
150 };
151 16 ▶ if (!$pack->{noargs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pack->{"noargs"}) 12 4 16
152 12 ▶ $pack->{uncompress_method} ||= "$pack->{compress_method} -d";
Condition100%2/2
$pack->{"uncompress_method"} ||= qq[$pack->{"compress_method"} -d]
l!l
75
MC/DC100%1/1
$pack->{"uncompress_method"} ||= qq[$pack->{"compress_method"} -d]
$pack->{"uncompress_method"}
Truth table100%2/2
A
A= $pack->{"uncompress_method"}
Aresult
11
00
153 12 ▶ $pack->{compress_method} = $pack->{compress_method} ? "$pack->{compress_method} -$pack->{level}" : "";
Branch: 1 branch
BranchTrueFalseTotal
$pack->{'compress_method'} ? : 12 0 12
154 }
155 }
156  
157 16 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=17.0
158 16 my ($pack) = @_;
159 16 ▶ $pack->{destroyed} and return; #- allow calling DESTROY
Branch: 1 branch
BranchTrueFalseTotal
$pack->{"destroyed"} and return 0 16 16
160 16 $pack->{destroyed} = 1;
161  
162 16 $pack->uncompress_handle(undef, undef);
163 16 ▶ $pack->build_toc() == 1 or die "Can't write toc into archive\n";
Branch: 1 branch
BranchTrueFalseTotal
$pack->build_toc == 1 or die "Can't write toc into archive\n" 16 0 16
164 16 ▶ close($pack->{handle}) if $pack->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $pack->{"handle"} 16 0 16
165 16 ▶ close($pack->{ustream_data}{handle}) if $pack->{ustream_data}{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $pack->{"ustream_data"}{"handle"} 0 16 16
166 }
167  
168 # Flush current compressed block
169 # Write
170 16 ▶sub build_toc {
Subroutine build_toc: called CC=12 SCAR=28.9
Pod undocumented
171 16 my ($pack) = @_;
172 16 ▶ $pack->{need_build_toc} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$pack->{"need_build_toc"} or return 1 8 8 16
173 8 $pack->end_block();
174 8 ▶ $pack->end_seek() or do {
Branch: 1 branch
BranchTrueFalseTotal
$pack->end_seek or do { $pack->{"log"}->("Can't seek into archive"); return 0 } 8 0 8
175 0 $pack->{log}("Can't seek into archive");
176 0 return 0;
177 };
178 8 my ($toc_length, $cf, $cd, $cl) = (0, 0, 0, 0);
179  
180 8 foreach my $entry (keys %{$pack->{'dir'}}) {
181 2 $cd++;
182 2 ▶ my $w = syswrite($pack->{handle}, $entry . "\n") or do {
Branch: 1 branch
BranchTrueFalseTotal
my $w = syswrite($pack->{"handle"}, $entry . "\n") or do { $pack->{"log"}->("Can't write toc into archive"); return 0 } 2 0 2
183 0 $pack->{log}("Can't write toc into archive");
184 0 return 0;
185 };
186 2 $toc_length += $w;
187 }
188 8 foreach my $entry (keys %{$pack->{'symlink'}}) {
189 2 $cl++;
190 2 ▶ my $w = syswrite($pack->{handle}, sprintf("%s\n%s\n", $entry, $pack->{'symlink'}{$entry})) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $w = syswrite($pack->{"handle"}, sprintf("%s\n%s\n", $entry, $pack->{"symlink"}{$entry})) or do { $pack->{"log"}->("Can't write toc into archive"); return 0 } 2 0 2
191 0 $pack->{log}("Can't write toc into archive");
192 0 return 0;
193 };
194 2 $toc_length += $w
195 }
196 8 foreach my $entry (sort keys %{$pack->{files}}) {
197 64 $cf++;
198 64 ▶ my $w = syswrite($pack->{handle}, $entry ."\n") or do {
Branch: 1 branch
BranchTrueFalseTotal
my $w = syswrite($pack->{"handle"}, $entry . "\n") or do { $pack->{"log"}->("Can't write toc into archive"); return 0 } 64 0 64
199 0 $pack->{log}("Can't write toc into archive");
200 0 return 0;
201 };
202 64 $toc_length += $w;
203 }
204 8 foreach my $file (sort keys %{$pack->{files}}) {
205 64 my $entry = $pack->{files}{$file};
206 64 ▶ syswrite($pack->{handle}, pack('NNNN', $entry->{coff}, $entry->{csize}, $entry->{off}, $entry->{size})) or do {
Branch: 1 branch
BranchTrueFalseTotal
syswrite $pack->{"handle"}, pack("NNNN", $entry->{"coff"}, $entry->{"csize"}, $entry->{"off"}, $entry->{"size"}) or do { $pack->{"log"}->("Can't write toc into archive"); return 0 } 64 0 64
207 0 $pack->{log}("Can't write toc into archive");
208 0 return 0;
209 };
210 }
211 syswrite($pack->{handle}, pack("a4NNNNa40a4",
212 $toc_header,
213 $cd, $cl, $cf,
214 $toc_length,
215 $pack->{uncompress_method},
216 8 ▶ $toc_footer)) or do {
Branch: 1 branch
BranchTrueFalseTotal
syswrite $pack->{"handle"}, pack("a4NNNNa40a4", $toc_header, $cd, $cl, $cf, $toc_length, $pack->{"uncompress_method"}, $toc_footer) or do { $pack->{"log"}->("Can't write toc into archive"); return 0 } 8 0 8
217 0 $pack->{log}("Can't write toc into archive");
218 0 return 0;
219 };
220 8 1;
221 }
222  
223 8 ▶sub read_toc {
Subroutine read_toc: called CC=8 SCAR=21.1
Pod undocumented
224 8 my ($pack) = @_;
225 8 sysseek($pack->{handle}, -64, 2) ; #or return 0;
226 8 sysread($pack->{handle}, my $buf, 64);# == 64 or return 0;
227 8 my ($header, $toc_d_count, $toc_l_count, $toc_f_count, $toc_str_size, $uncompress, $trailer) =
228 unpack("a4NNNNZ40a4", $buf);
229 8 ▶ $header eq $toc_header && $trailer eq $toc_footer or do {
Branch: 1 branch
BranchTrueFalseTotal
$header eq $toc_header and $trailer eq $toc_footer or do { $pack->{"log"}->("Error reading toc: wrong header/trailer"); return 0 } 8 0 8
Condition33%1/3
$header eq $toc_header and $trailer eq $toc_footer
!ll&&!rl&&r
008
MC/DC0%0/2
$header eq $toc_header and $trailer eq $toc_footer
$header eq $toc_header $trailer eq $toc_footer
Truth table33%1/3
A and B
A= $header eq $toc_headerB= $trailer eq $toc_footer
ABresult
0X0
100
111
230 0 $pack->{log}("Error reading toc: wrong header/trailer");
231 0 return 0;
232 };
233  
234 8 ▶ $pack->{uncompress_method} ||= $uncompress;
Condition66%2/3
$pack->{"uncompress_method"} ||= $uncompress
l!l&&r!l&&!r
170
MC/DC0%0/2
$pack->{"uncompress_method"} ||= $uncompress
$pack->{"uncompress_method"} $uncompress
Truth table66%2/3
A ||= B
A= $pack->{"uncompress_method"}B= $uncompress
ABresult
1X1
011
000
235 8 $pack->choose_compression_method();
236  
237 8 sysseek($pack->{handle}, -64 - ($toc_str_size + 16 * $toc_f_count) ,2);
238 8 sysread($pack->{handle}, my $fileslist, $toc_str_size);
239 8 my @filenames = split("\n", $fileslist);
240 8 sysread($pack->{handle}, my $sizes_offsets, 16 * $toc_f_count);
241 8 my @size_offset = unpack("N" . 4*$toc_f_count, $sizes_offsets);
242  
243 8 foreach (1 .. $toc_d_count) {
244 2 $pack->{dir}{shift(@filenames)} = 1;
245 }
246 8 foreach (1 .. $toc_l_count) {
247 2 my $n = shift(@filenames);
248 2 $pack->{'symlink'}{$n} = shift(@filenames);
249 }
250  
251 8 foreach (1 .. $toc_f_count) {
252 64 my $f = shift(@filenames);
253 64 $pack->{files}{$f}{coff} = shift(@size_offset);
254 64 $pack->{files}{$f}{csize} = shift(@size_offset);
255 64 $pack->{files}{$f}{off} = shift(@size_offset);
256 64 $pack->{files}{$f}{size} = shift(@size_offset);
257 # looking for offset for this archive
258 $pack->{files}{$f}{coff} + $pack->{files}{$f}{csize} > $pack->{coff}
259 64 ▶ and $pack->{coff} = $pack->{files}{$f}{coff} + $pack->{files}{$f}{csize};
Branch: 1 branch
BranchTrueFalseTotal
$pack->{"files"}{$f}{"coff"} + $pack->{"files"}{$f}{"csize"} > $pack->{"coff"} and $pack->{"coff"} = $pack->{"files"}{$f}{"coff"} + $pack->{"files"}{$f}{"csize"} 9 55 64
260 }
261 8 $pack->{toc_f_count} = $toc_f_count;
262 8 1;
263 }
264  
265 8 ▶sub sort_files_by_packing {
Subroutine sort_files_by_packing: called CC=4 SCAR=14.9
Pod undocumented
266 8 my ($pack, @files) = @_;
267 sort {
268 8 defined($pack->{files}{$a}) && defined($pack->{files}{$b}) ?
269 ($pack->{files}{$a}{coff} == $pack->{files}{$b}{coff} ?
270 $pack->{files}{$a}{off} <=> $pack->{files}{$b}{off} :
271 124 ▶ $pack->{files}{$a}{coff} <=> $pack->{files}{$b}{coff}) :
Branch: 2 branches
BranchTrueFalseTotal
defined $pack->{'files'}{$a} && defined $pack->{'files'}{$b} ? : 124 0 124
$pack->{'files'}{$a}{'coff'} == $pack->{'files'}{$b}{'coff'} ? : 97 27 124
Condition33%1/3
defined $pack->{"files"}{$a} and defined $pack->{"files"}{$b}
!ll&&!rl&&r
00124
MC/DC0%0/2
defined $pack->{"files"}{$a} and defined $pack->{"files"}{$b}
defined $pack->{"files"}{$a} defined $pack->{"files"}{$b}
Truth table33%1/3
A and B
A= defined $pack->{"files"}{$a}B= defined $pack->{"files"}{$b}
ABresult
0X0
100
111
272 $a cmp $b
273 } @files;
274 }
275  
276 # Goto to the end of written compressed data
277 91 ▶sub end_seek {
Subroutine end_seek: called CC=2 SCAR=6.9
Pod undocumented
278 91 my ($pack) = @_;
279 91 ▶ my $seekvalue = $pack->direct_write ? $pack->{coff} + $pack->{current_block_csize} : $pack->{coff};
Branch: 1 branch
BranchTrueFalseTotal
$pack->direct_write ? : 38 53 91
280 91 sysseek($pack->{handle}, $seekvalue, 0) == $seekvalue
281 }
282  
283 #- To terminate a compressed block, flush the pending compressed data,
284 #- fill toc data still unknown
285 19 ▶sub end_block {
Subroutine end_block: called CC=3 SCAR=11.0
Pod undocumented
286 19 my ($pack) = @_;
287 19 ▶ $pack->end_seek() or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$pack->end_seek or return 0 19 0 19
288 19 my (undef, $csize) = $pack->compress_handle(undef);
289 19 $pack->{current_block_csize} += $csize;
290 19 foreach (keys %{$pack->{current_block_files}}) {
291 64 $pack->{files}{$_} = $pack->{current_block_files}{$_};
292 64 $pack->{files}{$_}{csize} = $pack->{current_block_csize};
293 }
294 19 $pack->{coff} += $pack->{current_block_csize};
295 19 $pack->{current_block_coff} += $pack->{current_block_csize};
296 19 $pack->{current_block_csize} = 0;
297 19 $pack->{current_block_files} = {};
298 19 $pack->{current_block_off} = 0;
299 }
300  
301 #######################
302 # Compression wrapper #
303 #######################
304  
305 # true if wrapper writes directly in archive and not into temp file
306 53 ▶sub direct_write { 0; }
Subroutine direct_write: called CC=1 SCAR=0.0
Pod undocumented
307  
308 49 ▶sub compress_handle {
Subroutine compress_handle: called CC=3 SCAR=11.0
Pod undocumented
309 49 my ($pack, $sourcefh) = @_;
310 49 my ($insize, $outsize) = (0, 0); # aka uncompressed / compressed data length
311  
312 49 ▶ if (!defined($sourcefh)) { # bloc flush call
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sourcefh) { } 4 45 49
313 4 return 0, $pack->compress_data();
314 } else {
315 45 ▶ while (my $length = sysread($sourcefh, my $data, $pack->{bufsize})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = sysread($sourcefh, my $data, $pack->{"bufsize"})) 369 45 414
316 369 $outsize += $pack->compress_data($data);
317 369 $insize += $length;
318 }
319 45 return ($insize, $outsize)
320 }
321 }
322  
323 373 ▶sub compress_data {
Subroutine compress_data: called CC=10 SCAR=23.8
Pod undocumented
324 373 my ($pack, $data) = ($_[0], \$_[1]);
325 373 my ($outsize) = (0); # aka uncompressed / compressed data length
326 373 my $hout; # handle for gzip
327  
328 373 ▶ if (defined($pack->{cstream_data})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pack->{"cstream_data"}) 369 4 373
329 369 $hout = $pack->{cstream_data}{hout};
330 }
331 373 ▶ if (defined($$data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $$data) { } 369 4 373
elsif (defined $pack->{'cstream_data'}) { } 3 1 4
332 369 ▶ if (!defined($pack->{cstream_data})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pack->{"cstream_data"}) 3 366 369
333 3 my $hin;
334 3 ($hin, $pack->{cstream_data}{file_block}) = tempfile();
335 3 close($hin); # ensure the flush
336 $pack->{cstream_data}{pid} = CORE::open($hout,
337 3 ▶ "|$pack->{compress_method} > $pack->{cstream_data}{file_block}") or do {
Branch: 1 branch
BranchTrueFalseTotal
$pack->{"cstream_data"}{"pid"} = CORE::open($hout, qq[|$pack->{"compress_method"} > $pack->{"cstream_data"}{"file_block"}]) or do { $pack->{"log"}->(qq[Unable to start $pack->{"compress_method"}]); return 0 } 3 0 3
338 0 $pack->{log}("Unable to start $pack->{compress_method}");
339 0 return 0;
340 };
341 3 $pack->{cstream_data}{hout} = $hout;
342 3 binmode $hout;
343 }
344 # until we have data to push or data to read
345 # pushing data to compressor
346 369 ▶ (syswrite($hout, $$data)) == length($$data) or do {
Branch: 1 branch
BranchTrueFalseTotal
syswrite($hout, $$data) == length $$data or do { $pack->{"log"}->("Can't push all data to compressor") } 369 0 369
347 0 $pack->{log}("Can't push all data to compressor");
348 };
349 369 return 0; # We can't be sure about data really written in the pipe
350 # because of multitasking and buffer, so nothing has been
351 # written
352 } elsif (defined($pack->{cstream_data})) {
353 # If $data is not set, this mean we want a flush(), for end_block()
354 3 close($hout);
355 3 waitpid $pack->{cstream_data}{pid}, 0;
356 # copy temp bloc to archive
357 3 ▶ sysopen(my $hin, $pack->{cstream_data}{file_block}, O_RDONLY) or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $hin, $pack->{"cstream_data"}{"file_block"}, 0 or do { $pack->{"log"}->("Can't open temp block file: $!"); return 0 } 3 0 3
358 0 $pack->{log}("Can't open temp block file: $!");
359 0 return 0;
360 };
361 3 unlink($pack->{cstream_data}{file_block});
362 3 ▶ while (my $length = sysread($hin, my $tdata, $pack->{bufsize})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = sysread($hin, my $tdata, $pack->{"bufsize"})) 348 3 351
363 348 ▶ (my $l = syswrite($pack->{handle}, $tdata)) == $length or do {
Branch: 1 branch
BranchTrueFalseTotal
(my $l = syswrite($pack->{"handle"}, $tdata)) == $length or do { $pack->{"log"}->("Can't write all data in archive") } 348 0 348
364 0 $pack->{log}("Can't write all data in archive");
365 };
366 348 $outsize += $l;
367 }
368 3 close($hin);
369 3 $pack->{cstream_data} = undef;
370 # TODO current_block_csize isn't 0 ?
371 return $outsize - $pack->{current_block_csize}
372 3 }
373 }
374  
375 53 ▶sub uncompress_handle {
Subroutine uncompress_handle: called CC=22 SCAR=36.2
Pod undocumented
376 53 my ($pack, $destfh, $fileinfo) = @_;
377  
378 53 ▶ if (defined($pack->{ustream_data}) && (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pack->{"ustream_data"} and !defined($fileinfo) || ($fileinfo->{"coff"} != $pack->{"ustream_data"}{"coff"} || $fileinfo->{"off"} < $pack->{"ustream_data"}{"off"})) 3 50 53
Condition66%6/9
defined $pack->{"ustream_data"} and !defined($fileinfo) || ($fileinfo->{"coff"} != $pack->{"ustream_data"}{"coff"} || $fileinfo->{"off"} < $pack->{"ustream_data"}{"off"})
!ll&&!rl&&r
8423
not defined $fileinfo or ($fileinfo->{"coff"} != $pack->{"ustream_data"}{"coff"} or $fileinfo->{"off"} < $pack->{"ustream_data"}{"off"})
l!l&&r!l&&!r
3042
$fileinfo->{"coff"} != $pack->{"ustream_data"}{"coff"} or $fileinfo->{"off"} < $pack->{"ustream_data"}{"off"}
l!l&&r!l&&!r
0042
MC/DC50%2/4
defined $pack->{"ustream_data"} and !defined($fileinfo) || ($fileinfo->{"coff"} != $pack->{"ustream_data"}{"coff"} || $fileinfo->{"off"} < $pack->{"ustream_data"}{"off"})
defined $pack->{"ustream_data"} not defined $fileinfo $fileinfo->{"coff"} != $pack->{"ustream_data"}{"coff"} $fileinfo->{"off"} < $pack->{"ustream_data"}{"off"}
Truth table60%3/5
A and not(B) or C or D
A= defined $pack->{"ustream_data"}B= not defined $fileinfoC= $fileinfo->{"coff"} != $pack->{"ustream_data"}{"coff"}D= $fileinfo->{"off"} < $pack->{"ustream_data"}{"off"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
379 !defined($fileinfo) ||
380 ($fileinfo->{coff} != $pack->{ustream_data}{coff} || $fileinfo->{off} < $pack->{ustream_data}{off})
381 )) {
382 3 close($pack->{ustream_data}{handle});
383 3 unlink($pack->{ustream_data}{tempname}); # deleting temp file
384 3 $pack->{ustream_data} = undef;
385 }
386  
387 53 ▶ defined($fileinfo) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $fileinfo or return 0 45 8 53
388  
389 # We have to first extract the block to a temp file, burk !
390 45 ▶ if (!defined($pack->{ustream_data})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pack->{"ustream_data"}) 3 42 45
391 3 my $tempfh;
392 3 $pack->{ustream_data}{coff} = $fileinfo->{coff};
393 3 $pack->{ustream_data}{read} = 0;
394  
395 3 ($tempfh, $pack->{ustream_data}{tempname}) = tempfile();
396  
397 3 my $cread = 0;
398 3 ▶ while ($cread < $fileinfo->{csize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cread < $fileinfo->{"csize"}) 348 3 351
399 my $cl = sysread($pack->{handle}, my $data,
400 $cread + $pack->{bufsize} > $fileinfo->{csize} ?
401 $fileinfo->{csize} - $cread :
402 348 ▶ $pack->{bufsize}) or do {
Branch: 2 branches
BranchTrueFalseTotal
my $cl = sysread($pack->{"handle"}, my $data, $cread + $pack->{"bufsize"} > $fileinfo->{"csize"} ? $fileinfo->{"csize"} - $cread : $pack->{"bufsize"}) or do { $pack->{"log"}->("Unexpected end of file"); close $tempfh; unlink $pack->{"ustream_data"}{"tempname"}; $pack->{"ustream_data"} = undef; return -1 } 348 0 348
$cread + $pack->{'bufsize'} > $fileinfo->{'csize'} ? : 3 345 348
403 0 $pack->{log}("Unexpected end of file");
404 0 close($tempfh);
405 0 unlink($pack->{ustream_data}{tempname});
406 0 $pack->{ustream_data} = undef;
407 0 return -1;
408 };
409 348 $cread += $cl;
410 348 ▶ syswrite($tempfh, $data) == length($data) or do {
Branch: 1 branch
BranchTrueFalseTotal
syswrite($tempfh, $data) == length $data or do { $pack->{"log"}->("Can't write all data into temp file"); close $tempfh; unlink $pack->{"ustream_data"}{"tempname"}; $pack->{"ustream_data"} = undef; return -1 } 348 0 348
411 0 $pack->{log}("Can't write all data into temp file");
412 0 close($tempfh);
413 0 unlink($pack->{ustream_data}{tempname});
414 0 $pack->{ustream_data} = undef;
415 0 return -1;
416 };
417 }
418 3 close($tempfh);
419  
420 3 ▶ my $cmd = $pack->{uncompress_method} eq 'gzip -d' || $pack->{uncompress_method} eq 'bzip2 -d' ?
Branch: 1 branch
BranchTrueFalseTotal
$pack->{'uncompress_method'} eq 'gzip -d' || $pack->{'uncompress_method'} eq 'bzip2 -d' ? : 2 1 3
Condition100%3/3
$pack->{"uncompress_method"} eq "gzip -d" or $pack->{"uncompress_method"} eq "bzip2 -d"
l!l&&r!l&&!r
111
MC/DC100%2/2
$pack->{"uncompress_method"} eq "gzip -d" or $pack->{"uncompress_method"} eq "bzip2 -d"
$pack->{"uncompress_method"} eq "gzip -d" $pack->{"uncompress_method"} eq "bzip2 -d"
Truth table100%3/3
A or B
A= $pack->{"uncompress_method"} eq "gzip -d"B= $pack->{"uncompress_method"} eq "bzip2 -d"
ABresult
1X1
011
000
421 "$pack->{uncompress_method} -c '$pack->{ustream_data}{tempname}'" :
422 "$pack->{uncompress_method} < '$pack->{ustream_data}{tempname}'";
423 3 ▶ CORE::open($pack->{ustream_data}{handle}, "$cmd |") or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $pack->{"ustream_data"}{"handle"}, "$cmd |" or do { $pack->{"log"}->(qq[Can't start $pack->{"uncompress_method"} to uncompress data]); unlink $pack->{"ustream_data"}{"tempname"}; $pack->{"ustream_data"} = undef; return -1 } 3 0 3
424 0 $pack->{log}("Can't start $pack->{uncompress_method} to uncompress data");
425 0 unlink($pack->{ustream_data}{tempname});
426 0 $pack->{ustream_data} = undef;
427 0 return -1;
428 };
429 3 binmode($pack->{ustream_data}{handle});
430 }
431  
432 45 my $byteswritten = 0;
433 45 $pack->{ustream_data}{off} = $fileinfo->{off};
434  
435 45 ▶ while ($byteswritten < $fileinfo->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byteswritten < $fileinfo->{"size"}) 1244 45 1289
436 1244 my $data = $pack->{ustream_data}{buf};
437 1244 $pack->{ustream_data}{buf} = undef;
438 1244 my $length;
439 1244 ▶ if (!defined($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data) { } 1205 39 1244
440 1205 ▶ $length = sysread($pack->{ustream_data}{handle}, $data, $pack->{bufsize}) or do {
Branch: 1 branch
BranchTrueFalseTotal
$length = sysread($pack->{"ustream_data"}{"handle"}, $data, $pack->{"bufsize"}) or do { $pack->{"log"}->(qq[Unexpected end of stream $pack->{"ustream_data"}{"tempname"}]); unlink $pack->{"ustream_data"}{"tempname"}; close $pack->{"ustream_data"}{"handle"}; $pack->{"ustream_data"} = undef; return -1 } 1205 0 1205
441 0 $pack->{log}("Unexpected end of stream $pack->{ustream_data}{tempname}");
442 0 unlink($pack->{ustream_data}{tempname});
443 0 close($pack->{ustream_data}{handle});
444 0 $pack->{ustream_data} = undef;
445 0 return -1;
446 };
447 } else {
448 39 $length = length($data);
449 }
450  
451 1244 ▶ if ($pack->{ustream_data}{read} < $fileinfo->{off} && $pack->{ustream_data}{read} + $length > $fileinfo->{off}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack->{"ustream_data"}{"read"} < $fileinfo->{"off"} and $pack->{"ustream_data"}{"read"} + $length > $fileinfo->{"off"}) 0 1244 1244
Condition33%1/3
$pack->{"ustream_data"}{"read"} < $fileinfo->{"off"} and $pack->{"ustream_data"}{"read"} + $length > $fileinfo->{"off"}
!ll&&!rl&&r
124400
MC/DC0%0/2
$pack->{"ustream_data"}{"read"} < $fileinfo->{"off"} and $pack->{"ustream_data"}{"read"} + $length > $fileinfo->{"off"}
$pack->{"ustream_data"}{"read"} < $fileinfo->{"off"} $pack->{"ustream_data"}{"read"} + $length > $fileinfo->{"off"}
Truth table33%1/3
A and B
A= $pack->{"ustream_data"}{"read"} < $fileinfo->{"off"}B= $pack->{"ustream_data"}{"read"} + $length > $fileinfo->{"off"}
ABresult
0X0
100
111
452 0 $data = substr($data, $fileinfo->{off} - $pack->{ustream_data}{read});
453 }
454 1244 $pack->{ustream_data}{read} += $length;
455 0 ▶ if ($pack->{ustream_data}{read} <= $fileinfo->{off}) { next }
Branch: 1 branch
BranchTrueFalseTotal
if ($pack->{"ustream_data"}{"read"} <= $fileinfo->{"off"}) 0 1244 1244
456  
457 1244 my $bw;
458 1244 ▶ if ($byteswritten + length($data) > $fileinfo->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byteswritten + length($data) > $fileinfo->{'size'}) { } 39 1205 1244
459 39 $bw = $fileinfo->{size} - $byteswritten;
460 39 $pack->{ustream_data}{buf} = substr($data, $bw); # keeping track of unwritten uncompressed data
461 39 $pack->{ustream_data}{read} -= length($pack->{ustream_data}{buf});
462 } else {
463 1205 $bw = length($data);
464 }
465  
466 1244 ▶ syswrite($destfh, $data, $bw) == $bw or do {
Branch: 1 branch
BranchTrueFalseTotal
syswrite($destfh, $data, $bw) == $bw or do { $pack->{"log"}->("Can't write data into dest"); return -1 } 1244 0 1244
467 0 $pack->{log}("Can't write data into dest");
468 0 return -1;
469 };
470 1244 $byteswritten += $bw;
471 }
472  
473 $byteswritten
474  
475 45 }
476  
477 ###################
478 # Debug functions #
479 ###################
480  
481 # This function extracts in $dest the whole block containing $file, can be useful for debugging
482 0 ▶sub extract_block {
Subroutine extract_block: not called CC=6 SCAR=37.4
Pod undocumented
483 0 my ($pack, $dest, $file) = @_;
484  
485 0 ▶ sysopen(my $handle, $dest, O_WRONLY | O_TRUNC | O_CREAT) or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $handle, $dest, 577 or do { $pack->{"log"}->("Can't open ${dest}: $!"); return -1 } 0 0 0
486 0 $pack->{log}("Can't open $dest: $!");
487 0 return -1;
488 };
489  
490 0 ▶ sysseek($pack->{handle}, $pack->{files}{$file}->{coff}, 0) == $pack->{files}{$file}->{coff} or do {
Branch: 1 branch
BranchTrueFalseTotal
sysseek($pack->{"handle"}, $pack->{"files"}{$file}{"coff"}, 0) == $pack->{"files"}{$file}{"coff"} or do { $pack->{"log"}->(qq[Can't seek to offset $pack->{"files"}{$file}{"coff"}]); close $handle; return -1 } 0 0 0
491 0 $pack->{log}("Can't seek to offset $pack->{files}{$file}->{coff}");
492 0 close($handle);
493 0 return -1;
494 };
495  
496 {
497 0 my $l;
498 $l = sysread($pack->{handle}, my $buf, $pack->{files}{$file}->{csize}) == $pack->{files}{$file}{csize}
499 0 ▶ or $pack->{log}("Read only $l / $pack->{files}{$file}->{csize} bytes");
Branch: 1 branch
BranchTrueFalseTotal
$l = sysread($pack->{"handle"}, my $buf, $pack->{"files"}{$file}{"csize"}) == $pack->{"files"}{$file}{"csize"} or $pack->{"log"}->(qq[Read only $l / $pack->{"files"}{$file}{"csize"} bytes]) 0 0 0
500 0 syswrite($handle, $buf);
501 }
502  
503 0 foreach ($pack->sort_files_by_packing(keys %{$pack->{files}})) {
504 0 ▶ $pack->{files}{$_}{coff} == $pack->{files}{$file}->{coff} or next;
Branch: 1 branch
BranchTrueFalseTotal
$pack->{"files"}{$_}{"coff"} == $pack->{"files"}{$file}{"coff"} or next 0 0 0
505 }
506  
507 0 close($handle);
508  
509 }
510  
511 ##################################
512 # Really working functions #
513 # Aka function people should use #
514 ##################################
515  
516 68 ▶sub add_virtual {
Subroutine add_virtual: called CC=8 SCAR=21.1
Pod documented
517 68 my ($pack, $type, $filename, $data) = @_;
518 68 ▶ $type eq 'l' and do {
Branch: 1 branch
BranchTrueFalseTotal
$type eq "l" and do { $pack->{"symlink"}{$filename} = $data; $pack->{"need_build_toc"} = 1; return 1 } 2 66 68
519 2 $pack->{'symlink'}{$filename} = $data;
520 2 $pack->{need_build_toc} = 1;
521 2 return 1;
522 };
523 66 ▶ $type eq 'd' and do {
Branch: 1 branch
BranchTrueFalseTotal
$type eq "d" and do { ++$pack->{"dir"}{$filename}; $pack->{"need_build_toc"} = 1; return 1 } 2 64 66
524 2 $pack->{dir}{$filename}++;
525 2 $pack->{need_build_toc} = 1;
526 2 return 1;
527 };
528 64 ▶ $type eq 'f' and do {
Branch: 1 branch
BranchTrueFalseTotal
$type eq "f" and do { $pack->end_seek or do { $pack->{"log"}->(qq[Can't seek to offset $pack->{"coff"}]); next }; () = ref $data eq "GLOB" ? $pack->compress_handle($data) : (length $data, $pack->compress_data($data)); $pack->{"current_block_files"}{$filename} = {"size", $size, "off", $pack->{"current_block_off"}, "coff", $pack->{"current_block_coff"}, "csize", -1}; $pack->{"current_block_off"} += $size; $pack->{"current_block_csize"} += $csize; $pack->{"need_build_toc"} = 1; if ($pack->{"block_size"} > 0 and $pack->{"current_block_csize"} >= $pack->{"block_size"}) { $pack->end_block; } ; return 1 } 64 0 64
529 # Be sure we are at the end, allow extract + add in only one instance
530 64 ▶ $pack->end_seek() or do {
Branch: 1 branch
BranchTrueFalseTotal
$pack->end_seek or do { $pack->{"log"}->(qq[Can't seek to offset $pack->{"coff"}]); next } 64 0 64
531 0 $pack->{log}("Can't seek to offset $pack->{coff}");
532 0 next;
533 };
534  
535 64 ▶ my ($size, $csize) = (ref($data) eq 'GLOB') ?
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'GLOB' ? : 62 2 64
536 $pack->compress_handle($data) :
537 (length($data), $pack->compress_data($data));
538 $pack->{current_block_files}{$filename} = {
539 size => $size,
540 off => $pack->{current_block_off},
541 coff => $pack->{current_block_coff},
542 64 csize => -1, # Still unknown, will be fill by end_block
543 }; # Storing in toc structure availlable info
544  
545 # Updating internal info about current block
546 64 $pack->{current_block_off} += $size;
547 64 $pack->{current_block_csize} += $csize;
548 64 $pack->{need_build_toc} = 1;
549 64 ▶ if ($pack->{block_size} > 0 && $pack->{current_block_csize} >= $pack->{block_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack->{"block_size"} > 0 and $pack->{"current_block_csize"} >= $pack->{"block_size"}) 11 53 64
Condition66%2/3
$pack->{"block_size"} > 0 and $pack->{"current_block_csize"} >= $pack->{"block_size"}
!ll&&!rl&&r
05311
MC/DC50%1/2
$pack->{"block_size"} > 0 and $pack->{"current_block_csize"} >= $pack->{"block_size"}
$pack->{"block_size"} > 0 $pack->{"current_block_csize"} >= $pack->{"block_size"}
Truth table66%2/3
A and B
A= $pack->{"block_size"} > 0B= $pack->{"current_block_csize"} >= $pack->{"block_size"}
ABresult
0X0
100
111
550 11 $pack->end_block();
551 }
552 64 return 1;
553 };
554 0 0
555 }
556  
557 4 ▶sub add {
Subroutine add: called CC=8 SCAR=24.8
Pod documented
558 4 my ($pack, $prefix, @files) = @_;
559 4 ▶ $prefix ||= "";
Condition50%1/2
$prefix ||= ""
l!l
04
MC/DC0%0/1
$prefix ||= ""
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
560 4 foreach my $file (@files) {
561 60 $file =~ s://+:/:;
562 60 ▶ my $srcfile = $prefix ? "$prefix/$file" : $file;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 60 60
563 60 $pack->{debug}("Adding '%s' as '%s' into archive", $srcfile, $file);
564  
565 60 ▶ -l $srcfile and do {
Branch: 1 branch
BranchTrueFalseTotal
-l $srcfile and do { $pack->add_virtual("l", $file, readlink $srcfile); next } 0 60 60
566 0 $pack->add_virtual('l', $file, readlink($srcfile));
567 0 next;
568 };
569 60 ▶ -d $srcfile and do { # dir simple case
Branch: 1 branch
BranchTrueFalseTotal
-d $srcfile and do { $pack->add_virtual("d", $file); next } 0 60 60
570 0 $pack->add_virtual('d', $file);
571 0 next;
572 };
573 60 ▶ -f $srcfile and do {
Branch: 1 branch
BranchTrueFalseTotal
-f $srcfile and do { sysopen my $htocompress, $srcfile, 0 or do { $pack->{"log"}->("Can't add ${srcfile}: $!"); next }; $pack->add_virtual("f", $file, $htocompress); close $htocompress; next } 60 0 60
574 60 ▶ sysopen(my $htocompress, $srcfile, O_RDONLY) or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $htocompress, $srcfile, 0 or do { $pack->{"log"}->("Can't add ${srcfile}: $!"); next } 60 0 60
575 0 $pack->{log}("Can't add $srcfile: $!");
576 0 next;
577 };
578 60 $pack->add_virtual('f', $file, $htocompress);
579 60 close($htocompress);
580 60 next;
581 };
582 0 $pack->{log}("Can't pack $srcfile");
583 }
584 4 1;
585 }
586  
587 64 ▶sub extract_virtual {
Subroutine extract_virtual: called CC=3 SCAR=12.7
Pod documented
588 64 my ($pack, $destfh, $filename) = @_;
589 64 ▶ defined($pack->{files}{$filename}) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $pack->{"files"}{$filename} or return -1 64 0 64
590 64 ▶ sysseek($pack->{handle}, $pack->{files}{$filename}->{coff}, 0) == $pack->{files}{$filename}->{coff} or do {
Branch: 1 branch
BranchTrueFalseTotal
sysseek($pack->{"handle"}, $pack->{"files"}{$filename}{"coff"}, 0) == $pack->{"files"}{$filename}{"coff"} or do { $pack->{"log"}->(qq[Can't seek to offset $pack->{"files"}{$filename}{"coff"}]); return -1 } 64 0 64
591 0 $pack->{log}("Can't seek to offset $pack->{files}{$filename}->{coff}");
592 0 return -1;
593 };
594 64 $pack->uncompress_handle($destfh, $pack->{files}{$filename});
595 }
596  
597 8 ▶sub extract {
Subroutine extract: called CC=20 SCAR=38.4
Pod documented
598 8 my ($pack, $destdir, @files) = @_;
599 8 foreach my $f ($pack->sort_files_by_packing(@files)) {
600 64 ▶ my $dest = $destdir ? "$destdir/$f" : "$f";
Branch: 1 branch
BranchTrueFalseTotal
$destdir ? : 64 0 64
601 64 my ($dir) = $dest =~ m!(.*)/.*!;
602 64 ▶ $dir ||= ".";
Condition50%1/2
$dir ||= "."
l!l
640
MC/DC0%0/1
$dir ||= "."
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
603 64 ▶ if (exists($pack->{dir}{$f})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $pack->{'dir'}{$f}) { } 2 62 64
elsif (exists $pack->{'symlink'}{$f}) { } 2 60 62
elsif (exists $pack->{'files'}{$f}) { } 60 0 60
604 -d $dest || mkpath($dest)
605 2 ▶ or $pack->{log}("Unable to create dir $dest: $!");
Branch: 1 branch
BranchTrueFalseTotal
-d $dest or mkpath($dest) or $pack->{"log"}->("Unable to create dir ${dest}: $!") 2 0 2
Condition33%1/3
-d $dest or mkpath($dest)
l!l&&r!l&&!r
020
MC/DC0%0/2
-d $dest or mkpath($dest)
-d $dest mkpath($dest)
Truth table33%1/3
A or B
A= -d $destB= mkpath($dest)
ABresult
1X1
011
000
606 2 next;
607 } elsif (exists($pack->{'symlink'}{$f})) {
608 -d $dir || mkpath($dir) or
609 2 ▶ $pack->{log}("Unable to create dir $dest: $!");
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or mkpath($dir) or $pack->{"log"}->("Unable to create dir ${dest}: $!") 2 0 2
Condition33%1/3
-d $dir or mkpath($dir)
l!l&&r!l&&!r
200
MC/DC0%0/2
-d $dir or mkpath($dir)
-d $dir mkpath($dir)
Truth table33%1/3
A or B
A= -d $dirB= mkpath($dir)
ABresult
1X1
011
000
610 2 ▶ -l $dest and unlink $dest;
Branch: 1 branch
BranchTrueFalseTotal
-l $dest and unlink $dest 0 2 2
611 symlink($pack->{'symlink'}{$f}, $dest)
612 2 ▶ or $pack->{log}("Unable to extract symlink $f: $!");
Branch: 1 branch
BranchTrueFalseTotal
symlink $pack->{"symlink"}{$f}, $dest or $pack->{"log"}->("Unable to extract symlink ${f}: $!") 2 0 2
613 2 next;
614 } elsif (exists($pack->{files}{$f})) {
615 60 ▶ -d $dir || mkpath($dir) or do {
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or mkpath($dir) or do { $pack->{"log"}->("Unable to create dir $dir") } 60 0 60
Condition33%1/3
-d $dir or mkpath($dir)
l!l&&r!l&&!r
6000
MC/DC0%0/2
-d $dir or mkpath($dir)
-d $dir mkpath($dir)
Truth table33%1/3
A or B
A= -d $dirB= mkpath($dir)
ABresult
1X1
011
000
616 0 $pack->{log}("Unable to create dir $dir");
617 };
618 60 ▶ if (-l $dest) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $dest) 0 60 60
619 0 ▶ unlink($dest) or do {
Branch: 1 branch
BranchTrueFalseTotal
unlink $dest or do { $pack->{"log"}->("Can't remove link ${dest}: $!"); next } 0 0 0
620 0 $pack->{log}("Can't remove link $dest: $!");
621 0 next; # Don't overwrite a file because where the symlink point to
622 };
623 }
624 60 my $destfh;
625 60 ▶ if (defined $destdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $destdir) { } 60 0 60
626 60 ▶ sysopen($destfh, $dest, O_CREAT | O_TRUNC | O_WRONLY) or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen $destfh, $dest, 577 or do { $pack->{"log"}->("Unable to extract ${dest}: $!"); next } 60 0 60
627 0 $pack->{log}("Unable to extract $dest: $!");
628 0 next;
629 };
630 } else {
631 0 $destfh = \*STDOUT;
632 }
633 60 my $written = $pack->extract_virtual($destfh, $f);
634 60 ▶ $written == -1 and $pack->{log}("Unable to extract file $f");
Branch: 1 branch
BranchTrueFalseTotal
$written == -1 and $pack->{"log"}->("Unable to extract file $f") 0 60 60
635 60 close($destfh);
636 60 next;
637 } else {
638 0 $pack->{log}("Can't find $f in archive");
639 }
640 }
641 8 1;
642 }
643  
644 # Return \@dir, \@files, \@symlink list
645 0 ▶sub getcontent {
Subroutine getcontent: not called CC=1 SCAR=6.9
Pod documented
646 0 my ($pack) = @_;
647 return(
648 0 [ keys(%{$pack->{dir}})],
649 0 [ $pack->sort_files_by_packing(keys %{$pack->{files}}) ],
650 0 [ keys(%{$pack->{'symlink'}}) ]
651 );
652 }
653  
654 8 ▶sub infofile {
Subroutine infofile: called CC=4 SCAR=13.9
Pod documented
655 8 my ($pack, $file) = @_;
656 8 ▶ if (defined($pack->{files}{$file})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $pack->{'files'}{$file}) { } 2 6 8
elsif (defined $pack->{'symlink'}{$file}) { } 2 4 6
elsif (defined $pack->{'dir'}{$file}) { } 2 2 4
657 2 return ('f', $pack->{files}{$file}{size});
658 } elsif (defined($pack->{'symlink'}{$file})) {
659 2 return ('l', $pack->{'symlink'}{$file});
660 } elsif (defined($pack->{dir}{$file})) {
661 2 return ('d', undef);
662 } else {
663 2 return(undef, undef);
664 }
665 }
666  
667 0 ▶sub list {
Subroutine list: not called CC=5 SCAR=34.0
Pod undocumented
668 0 my ($pack, $handle) = @_;
669 0 ▶ $handle ||= *STDOUT;
Condition0%0/3
$handle ||= *STDOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$handle ||= *STDOUT
$handle *STDOUT
Truth table0%0/3
A ||= B
A= $handleB= *STDOUT
ABresult
1X1
011
000
670 0 foreach my $file (keys %{$pack->{dir}}) {
671 0 printf "d %13c %s\n", ' ', $file;
672 }
673 0 foreach my $file (keys %{$pack->{'symlink'}}) {
674 0 printf "l %13c %s -> %s\n", ' ', $file, $pack->{'symlink'}{$file};
675 }
676 0 foreach my $file ($pack->sort_files_by_packing(keys %{$pack->{files}})) {
677 0 printf "f %12d %s\n", $pack->{files}{$file}{size}, $file;
678 }
679 }
680  
681 # Print toc info
682 0 ▶sub dumptoc {
Subroutine dumptoc: not called CC=5 SCAR=34.0
Pod documented
683 0 my ($pack, $handle) = @_;
684 0 ▶ $handle ||= *STDOUT;
Condition0%0/3
$handle ||= *STDOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$handle ||= *STDOUT
$handle *STDOUT
Truth table0%0/3
A ||= B
A= $handleB= *STDOUT
ABresult
1X1
011
000
685 0 foreach my $file (keys %{$pack->{dir}}) {
686 0 printf $handle "d %13c %s\n", ' ', $file;
687 }
688 0 foreach my $file (keys %{$pack->{'symlink'}}) {
689 0 printf $handle "l %13c %s -> %s\n", ' ', $file, $pack->{'symlink'}{$file};
690 }
691 0 foreach my $file ($pack->sort_files_by_packing(keys %{$pack->{files}})) {
692 0 printf $handle "f %d %d %d %d %s\n", $pack->{files}{$file}{size}, $pack->{files}{$file}{off}, $pack->{files}{$file}{csize}, $pack->{files}{$file}{coff}, $file;
693 }
694 }
695  
696 2 1;
697  
698 __END__
« Packdrakeng/zlib.pm ↑ Summary