POSIX/Userland/Tar/Header.pm

filter: Statementstmt 68.5% 109 / 159 Branchbran 80.9% 34 / 42 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 51.8% 14 / 27 Podpod 0.0% 0 / 21 totaltotal 62.1% 164 / 264 CC 27 SCAR 15.6
CC
27
Cov
70%
split_path_components
11.1
from_inode
6.4
encode_posix
6.0
CRAP 4.8
« POSIX/Real/Inode.pm ↑ Summary POSIX/Path.pm »
1 # Copyright (c) 2014, cPanel, Inc.
2 # All rights reserved.
3 # http://cpanel.net/
4 #
5 # This is free software; you can redistribute it and/or modify it under the same
6 # terms as Perl itself. See the LICENSE file for further details.
7  
8 package Filesys::POSIX::Userland::Tar::Header;
9  
10 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use Filesys::POSIX::Bits;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Filesys::POSIX::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 our $BLOCK_SIZE = 512;
19  
20 7 my %TYPES = (
21 0 => $S_IFREG,
22 2 => $S_IFLNK,
23 3 => $S_IFCHR,
24 4 => $S_IFBLK,
25 5 => $S_IFDIR,
26 6 => $S_IFIFO
27 );
28  
29 81 ▶sub inode_linktype {
Subroutine inode_linktype: called CC=3 SCAR=11.1
Pod undocumented
30 81 my ($inode) = @_;
31  
32 81 foreach ( keys %TYPES ) {
33 323 ▶ return $_ if ( $inode->{'mode'} & $S_IFMT ) == $TYPES{$_};
Branch: 1 branch
BranchTrueFalseTotal
if ($inode->{"mode"} & $Filesys::POSIX::Userland::Tar::Header::S_IFMT) == $TYPES{$_} 81 242 323
34 }
35  
36 0 return 0;
37 }
38  
39 81 ▶sub from_inode {
Subroutine from_inode: called CC=6 SCAR=18.5
Pod undocumented
40 81 my ( $class, $inode, $path ) = @_;
41  
42 81 my $parts = Filesys::POSIX::Path->new($path);
43 81 my $cleanpath = $parts->full;
44 81 ▶ $cleanpath .= '/' if $inode->dir;
Branch: 1 branch
BranchTrueFalseTotal
if $inode->dir 58 23 81
45  
46 81 my $path_components = split_path_components( $parts, $inode );
47 81 ▶ my $size = $inode->file ? $inode->{'size'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$inode->file ? : 17 64 81
48  
49 81 my $major = 0;
50 81 my $minor = 0;
51  
52 81 ▶ if ( $inode->char || $inode->block ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inode->char or $inode->block) 0 81 81
Condition33%1/3
$inode->char or $inode->block
l!l&&r!l&&!r
0081
MC/DC0%0/2
$inode->char or $inode->block
$inode->char $inode->block
Truth table33%1/3
A or B
A= $inode->charB= $inode->block
ABresult
1X1
011
000
53 0 $major = $inode->major;
54 0 $minor = $inode->minor;
55 }
56  
57 return bless {
58 'path' => $cleanpath,
59 'prefix' => $path_components->{'prefix'},
60 'suffix' => $path_components->{'suffix'},
61 'truncated' => $path_components->{'truncated'},
62 'mode' => $inode->{'mode'},
63 'uid' => $inode->{'uid'},
64 'gid' => $inode->{'gid'},
65 'size' => $size,
66 81 ▶ 'mtime' => $inode->{'mtime'},
Branch: 1 branch
BranchTrueFalseTotal
$inode->link ? : 6 75 81
67 'linktype' => inode_linktype($inode),
68 'linkdest' => $inode->link ? $inode->readlink : '',
69 'user' => '',
70 'group' => '',
71 'major' => $major,
72 'minor' => $minor
73 }, $class;
74 }
75  
76 0 ▶sub decode {
Subroutine decode: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ( $class, $block ) = @_;
78  
79 0 my $suffix = read_str( $block, 0, 100 );
80 0 my $prefix = read_str( $block, 345, 155 );
81 0 my $checksum = read_oct( $block, 148, 8 );
82  
83 0 validate_block( $block, $checksum );
84  
85 0 return bless {
86 'suffix' => $suffix,
87 'mode' => read_oct( $block, 100, 8 ),
88 'uid' => read_oct( $block, 108, 8 ),
89 'gid' => read_oct( $block, 116, 8 ),
90 'size' => read_oct( $block, 124, 12 ),
91 'mtime' => read_oct( $block, 136, 12 ),
92 'linktype' => read_oct( $block, 156, 1 ),
93 'linkdest' => read_str( $block, 157, 100 ),
94 'user' => read_str( $block, 265, 32 ),
95 'group' => read_str( $block, 297, 32 ),
96 'major' => read_oct( $block, 329, 8 ),
97 'minor' => read_oct( $block, 337, 8 ),
98 'prefix' => $prefix
99 }, $class;
100 }
101  
102 2 ▶sub encode_longlink {
Subroutine encode_longlink: called CC=1 SCAR=0.0
Pod undocumented
103 2 my ($self) = @_;
104  
105 2 my $pathlen = length $self->{'path'};
106  
107 2 my $longlink_header = bless {
108 'prefix' => '',
109 'suffix' => '././@LongLink',
110 'mode' => 0,
111 'uid' => 0,
112 'gid' => 0,
113 'size' => $pathlen,
114 'mtime' => 0,
115 'linktype' => 'L',
116 'linkdest' => '',
117 'user' => '',
118 'group' => '',
119 'major' => 0,
120 'minor' => 0
121 },
122 ref $self;
123  
124 2 my $path_blocks = "\x00" x ( $pathlen + $BLOCK_SIZE - ( $pathlen % $BLOCK_SIZE ) );
125 2 substr( $path_blocks, 0, $pathlen ) = $self->{'path'};
126  
127 2 return $longlink_header->encode . $path_blocks;
128 }
129  
130 6 ▶sub _compute_posix_header {
Subroutine _compute_posix_header: called CC=2 SCAR=6.9
131 6 my ( $self, $key, $value ) = @_;
132 6 my $header = " $key=$value\n";
133 6 my $len = length $header;
134 6 my $hdrlen = length($len) + $len;
135 6 my $curlen = length($hdrlen);
136  
137 # The length field includes everything up to and including the newline and
138 # the length field itself. Compute the proper value if adding the length
139 # would push us to a larger number of digits.
140 6 ▶ $hdrlen = $curlen + $len if $curlen > length($len);
Branch: 1 branch
BranchTrueFalseTotal
if $curlen > length $len 1 5 6
141  
142 6 return "$hdrlen$header";
143 }
144  
145 0 ▶sub encode_posix {
Subroutine encode_posix: not called CC=2 SCAR=17.9
Pod undocumented
146 0 my ($self) = @_;
147  
148 0 my $linklen = length $self->{'linkdest'};
149 0 my $encoded = $self->_compute_posix_header( 'path', $self->{'path'} );
150 0 ▶ $encoded .= $self->_compute_posix_header( 'linkpath', $self->{'linkdest'} ) if $linklen;
Branch: 1 branch
BranchTrueFalseTotal
if $linklen 0 0 0
151  
152 0 my $encodedlen = length $encoded;
153  
154 my $posix_header = bless {
155 'prefix' => "./PaxHeaders.$$",
156 0 'suffix' => substr( $self->{'path'}, 0, 100 ),
157 'mode' => 0,
158 'uid' => 0,
159 'gid' => 0,
160 'size' => $encodedlen,
161 'mtime' => 0,
162 'linktype' => 'x',
163 'linkdest' => '',
164 'user' => '',
165 'group' => '',
166 'major' => 0,
167 'minor' => 0
168 },
169 ref $self;
170  
171 0 my $path_blocks = "\x00" x ( $encodedlen + $BLOCK_SIZE - ( $encodedlen % $BLOCK_SIZE ) );
172 0 substr( $path_blocks, 0, $encodedlen ) = $encoded;
173  
174 0 return $posix_header->encode . $path_blocks;
175 }
176  
177 80 ▶sub encode {
Subroutine encode: called CC=4 SCAR=14.0
Pod undocumented
178 80 my ($self) = @_;
179 80 my $block = "\x00" x $BLOCK_SIZE;
180  
181 80 write_str( $block, 0, 100, $self->{'suffix'} );
182 80 write_oct( $block, 100, 8, $self->{'mode'} & $S_IPERM, 7 );
183 80 write_oct( $block, 108, 8, $self->{'uid'}, 7 );
184 80 write_oct( $block, 116, 8, $self->{'gid'}, 7 );
185 80 write_oct( $block, 124, 12, $self->{'size'}, 11 );
186 80 write_oct( $block, 136, 12, $self->{'mtime'}, 11 );
187 80 write_str( $block, 148, 8, ' ' );
188  
189 80 ▶ if ( $self->{'linktype'} =~ /^[0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'linktype'} =~ /^[0-9]$/) { } 78 2 80
190 78 write_oct( $block, 156, 1, $self->{'linktype'}, 1 );
191 }
192 else {
193 2 write_str( $block, 156, 1, $self->{'linktype'} );
194 }
195  
196 80 write_str( $block, 157, 100, $self->{'linkdest'} );
197 80 write_str( $block, 257, 6, 'ustar' );
198 80 write_str( $block, 263, 2, '00' );
199 80 write_str( $block, 265, 32, $self->{'user'} );
200 80 write_str( $block, 297, 32, $self->{'group'} );
201  
202 80 ▶ if ( $self->{'major'} || $self->{'minor'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"major"} or $self->{"minor"}) 0 80 80
Condition33%1/3
$self->{"major"} or $self->{"minor"}
l!l&&r!l&&!r
0080
MC/DC0%0/2
$self->{"major"} or $self->{"minor"}
$self->{"major"} $self->{"minor"}
Truth table33%1/3
A or B
A= $self->{"major"}B= $self->{"minor"}
ABresult
1X1
011
000
203 0 write_oct( $block, 329, 8, $self->{'major'}, 7 );
204 0 write_oct( $block, 337, 8, $self->{'minor'}, 7 );
205 }
206  
207 80 write_str( $block, 345, 155, $self->{'prefix'} );
208  
209 80 my $checksum = checksum($block);
210  
211 80 write_oct( $block, 148, 8, $checksum, 7 );
212  
213 80 return $block;
214 }
215  
216 88 ▶sub split_path_components {
Subroutine split_path_components: called CC=11 SCAR=24.0
Pod undocumented
217 88 my ( $parts, $inode ) = @_;
218  
219 88 my $truncated = 0;
220  
221 88 ▶ $parts->[-1] .= '/' if $inode->dir;
Branch: 1 branch
BranchTrueFalseTotal
if $inode->dir 61 27 88
222  
223 88 my $got = 0;
224 88 my ( @prefix_items, @suffix_items );
225  
226 88 ▶ while ( @{$parts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$parts;}) 400 88 488
227 400 my $item = pop @{$parts};
228 400 my $len = length $item;
229  
230 #
231 # If the first item found is greater than 100 characters in length,
232 # truncate it so that it may fit in the standard tar path header field.
233 #
234 400 ▶ if ( $got == 0 && $len > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got == 0 and $len > 100) 3 397 400
Condition100%3/3
$got == 0 and $len > 100
!ll&&!rl&&r
312853
MC/DC100%2/2
$got == 0 and $len > 100
$got == 0 $len > 100
Truth table100%3/3
A and B
A= $got == 0B= $len > 100
ABresult
0X0
100
111
235 3 ▶ my $truncated_len = $inode->dir ? 99 : 100;
Branch: 1 branch
BranchTrueFalseTotal
$inode->dir ? : 2 1 3
236  
237 3 $item = substr( $item, 0, $truncated_len );
238 3 ▶ $item .= '/' if $inode->dir;
Branch: 1 branch
BranchTrueFalseTotal
if $inode->dir 2 1 3
239  
240 3 $len = 100;
241 3 $truncated = 1;
242 }
243  
244 400 ▶ $got++ if $got;
Branch: 1 branch
BranchTrueFalseTotal
if $got 312 88 400
245 400 $got += $len;
246  
247 400 ▶ if ( $got <= 100 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($got <= 100) { } 294 106 400
elsif ($got > 100) { } 106 0 106
248 294 push @suffix_items, $item;
249 }
250 elsif ( $got > 100 ) {
251 106 push @prefix_items, $item;
252 }
253 }
254  
255 88 my $prefix = join( '/', reverse @prefix_items );
256 88 my $suffix = join( '/', reverse @suffix_items );
257  
258 88 ▶ if ( length($prefix) > 155 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prefix > 155) 0 88 88
259 0 $prefix = substr( $prefix, 0, 155 );
260 0 $truncated = 1;
261 }
262  
263 return {
264 88 'prefix' => $prefix,
265 'suffix' => $suffix,
266 'truncated' => $truncated
267 };
268 }
269  
270 0 ▶sub read_str {
Subroutine read_str: not called CC=1 SCAR=6.9
Pod undocumented
271 0 my ( $block, $offset, $len ) = @_;
272 0 my $template = "Z$len";
273  
274 0 return unpack( $template, substr( $block, $offset, $len ) );
275 }
276  
277 642 ▶sub write_str {
Subroutine write_str: called CC=2 SCAR=6.9
Pod undocumented
278 642 my ( $block, $offset, $len, $string ) = @_;
279  
280 642 ▶ if ( length($string) == $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string == $len) { } 163 479 642
281 163 substr( $_[0], $offset, $len ) = $string;
282 }
283 else {
284 479 substr( $_[0], $offset, $len ) = pack( "Z$len", $string );
285 }
286  
287 642 return;
288 }
289  
290 0 ▶sub read_oct {
Subroutine read_oct: not called CC=1 SCAR=6.9
Pod undocumented
291 0 my ( $block, $offset, $len ) = @_;
292 0 my $template = "Z$len";
293  
294 0 return oct( unpack( $template, substr( $block, $offset, $len ) ) );
295 }
296  
297 558 ▶sub write_oct {
Subroutine write_oct: called CC=2 SCAR=6.9
Pod undocumented
298 558 my ( $block, $offset, $len, $value, $digits ) = @_;
299 558 my $string = sprintf( "%.${digits}o", $value );
300 558 my $sub_offset = length($string) - $digits;
301 558 my $substring = substr( $string, $sub_offset, $digits );
302  
303 558 ▶ if ( $len == $digits ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == $digits) { } 78 480 558
304 78 substr( $_[0], $offset, $len ) = $substring;
305 }
306 else {
307 480 substr( $_[0], $offset, $len ) = pack( "Z$len", $substring );
308 }
309  
310 558 return;
311 }
312  
313 80 ▶sub checksum {
Subroutine checksum: called CC=2 SCAR=6.9
Pod undocumented
314 80 my ($block) = @_;
315 80 my $sum = 0;
316  
317 80 foreach ( unpack 'C*', $block ) {
318 40960 $sum += $_;
319 }
320  
321 80 return $sum;
322 }
323  
324 0 ▶sub validate_block {
Subroutine validate_block: not called CC=2 SCAR=17.9
Pod undocumented
325 0 my ( $block, $checksum ) = @_;
326 0 my $copy = "$block";
327  
328 0 write_str( $block, 148, 8, ' ' x 8 );
329  
330 0 my $calculated_checksum = checksum($copy);
331  
332 0 ▶ Carp::confess('Invalid block') unless $calculated_checksum == $checksum;
Branch: 1 branch
BranchTrueFalseTotal
unless $calculated_checksum == $checksum 0 0 0
333  
334 0 return;
335 }
336  
337 0 ▶sub file {
Subroutine file: not called CC=1 SCAR=6.9
Pod undocumented
338 0 my ($self) = @_;
339  
340 0 return $TYPES{ $self->{'linktype'} } == $S_IFREG;
341 }
342  
343 0 ▶sub link {
Subroutine link: not called CC=1 SCAR=6.9
Pod undocumented
344 0 my ($self) = @_;
345  
346 0 return $self->{'linktype'} == 1;
347 }
348  
349 0 ▶sub symlink {
Subroutine symlink: not called CC=1 SCAR=6.9
Pod undocumented
350 0 my ($self) = @_;
351  
352 0 return $TYPES{ $self->{'linktype'} } == $S_IFLNK;
353 }
354  
355 0 ▶sub char {
Subroutine char: not called CC=1 SCAR=6.9
Pod undocumented
356 0 my ($self) = @_;
357  
358 0 return $TYPES{ $self->{'linktype'} } == $S_IFCHR;
359 }
360  
361 0 ▶sub block {
Subroutine block: not called CC=1 SCAR=6.9
Pod undocumented
362 0 my ($self) = @_;
363  
364 0 return $TYPES{ $self->{'linktype'} } == $S_IFBLK;
365 }
366  
367 0 ▶sub dir {
Subroutine dir: not called CC=1 SCAR=6.9
Pod undocumented
368 0 my ($self) = @_;
369  
370 0 return $TYPES{ $self->{'linktype'} } == $S_IFDIR;
371 }
372  
373 0 ▶sub fifo {
Subroutine fifo: not called CC=1 SCAR=6.9
Pod undocumented
374 0 my ($self) = @_;
375  
376 0 return $TYPES{ $self->{'linktype'} } == $S_IFIFO;
377 }
378  
379 0 ▶sub contig {
Subroutine contig: not called CC=1 SCAR=6.9
Pod undocumented
380 0 my ($self) = @_;
381  
382 0 return $self->{'linktype'} == 7;
383 }
384  
385 7 1;
« POSIX/Real/Inode.pm ↑ Summary POSIX/Path.pm »