Tar/File.pm

filter: Statementstmt 91.3% 105 / 115 Branchbran 53.2% 33 / 62 Conditioncond 54.1% 13 / 24 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 96.5% 28 / 29 Podpod 100% 3 / 3 totaltotal 75.7% 184 / 243 CC 41 SCAR 26.0
CC
41
Cov
75%
_build_type
73.0
add_block
8.5
_stat
6.2
CRAP 13.5
↑ Summary Tar.pm »
1 package Mojo::Tar::File;
2 2 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use constant DEBUG => !!$ENV{MOJO_TAR_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶our $GID = $( =~ /(\d+)/ && int($1) || 0;
Condition40%2/5
$( =~ /(\d+)/u && int $1 || 0
l!l
20
$( =~ /(\d+)/u and int $1
!ll&&!rl&&r
002
MC/DC0%0/2
$( =~ /(\d+)/u && int $1 || 0
$( =~ /(\d+)/u int $1
Truth table33%1/3
A and B
A= $( =~ /(\d+)/uB= int $1
ABresult
111
0X0
100
12 2 our ($PACK_FORMAT, @EXPORT);
13  
14 2 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
15 2 $PACK_FORMAT = q(
16 a100 # pos=0 name=name desc=file name (chars)
17 a8 # pos=100 name=mode desc=file mode (octal)
18 a8 # pos=108 name=uid desc=uid (octal)
19 a8 # pos=116 name=gid desc=gid (octal)
20 a12 # pos=124 name=size desc=size (octal)
21 a12 # pos=136 name=mtime desc=mtime (octal)
22 a8 # pos=148 name=checksum desc=checksum (octal)
23 a1 # pos=156 name=type desc=type
24 a100 # pos=157 name=symlink desc=file symlink destination (chars)
25 A6 # pos=257 name=ustar desc=ustar
26 a2 # pos=263 name=ustar_ver desc=ustar version (00)
27 a32 # pos=265 name=owner desc=owner user name (chars)
28 a32 # pos=297 name=group desc=owner group name (chars)
29 a8 # pos=329 name=dev_major desc=device major number
30 a8 # pos=337 name=dev_minor desc=device minor number
31 a155 # pos=345 name=prefix desc=file name prefix
32 a12 # pos=500 name=padding desc=padding (\0)
33 );
34  
35 # Generate constants:
36 # TAR_USTAR_NAME_LEN TAR_USTAR_NAME_POS
37 # TAR_USTAR_MODE_LEN TAR_USTAR_MODE_POS
38 # TAR_USTAR_UID_LEN TAR_USTAR_UID_POS
39 # TAR_USTAR_GID_LEN TAR_USTAR_GID_POS
40 # TAR_USTAR_SIZE_LEN TAR_USTAR_SIZE_POS
41 # TAR_USTAR_MTIME_LEN TAR_USTAR_MTIME_POS
42 # TAR_USTAR_CHECKSUM_LEN TAR_USTAR_CHECKSUM_POS
43 # TAR_USTAR_TYPE_LEN TAR_USTAR_TYPE_POS
44 # TAR_USTAR_SYMLINK_LEN TAR_USTAR_SYMLINK_POS
45 # TAR_USTAR_USTAR_LEN TAR_USTAR_USTAR_POS
46 # TAR_USTAR_USTAR_VER_LEN TAR_USTAR_USTAR_VER_POS
47 # TAR_USTAR_OWNER_LEN TAR_USTAR_OWNER_POS
48 # TAR_USTAR_GROUP_LEN TAR_USTAR_GROUP_POS
49 # TAR_USTAR_DEV_MAJOR_LEN TAR_USTAR_DEV_MAJOR_POS
50 # TAR_USTAR_DEV_MINOR_LEN TAR_USTAR_DEV_MINOR_POS
51 # TAR_USTAR_PREFIX_LEN TAR_USTAR_PREFIX_POS
52 # TAR_USTAR_PADDING_LEN TAR_USTAR_PADDING_POS
53 2 for my $line (split /\n/, $PACK_FORMAT) {
54 38 ▶ my ($len, $pos, $name) = $line =~ /(\d+)\W+pos=(\d+)\W+name=(\w+)/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /(\d+)\W+pos=(\d+)\W+name=(\w+)/u or next 34 4 38
55  
56 34 my $const = uc "TAR_USTAR_${name}_LEN";
57 34 constant->import($const => $len);
58 34 push @EXPORT, $const;
59  
60 34 $const = uc "TAR_USTAR_${name}_POS";
61 34 constant->import($const => $pos);
62 34 push @EXPORT, $const;
63 }
64 }
65  
66 5 ▶has asset => sub ($self) {Mojo::File::tempfile};
Subroutine __ANON__: called CC=1 SCAR=0.0
67 has checksum =>
68 0 ▶ sub ($self) { substr $self->to_header, TAR_USTAR_CHECKSUM_POS, TAR_USTAR_CHECKSUM_LEN };
Subroutine __ANON__: not called CC=1 SCAR=1.2
69 2 has dev_major => '';
70 2 has dev_minor => '';
71 5 ▶has gid => sub ($self) { $self->_stat('gid') || $GID };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self->_stat("gid") or $Mojo::Tar::File::GID 5 0 5
72 5 ▶has group => sub ($self) { getgrgid($self->gid) || '' };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
getgrgid $self->gid or "" 5 0 5
73 1 ▶has is_complete => sub ($self) { $self->_stat('size') == $self->size ? 1 : 0 };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self->_stat('size') == $self->size ? : 0 1 1
74 5 ▶has mode => sub ($self) { ($self->_stat('mode') || 0) & 0777 };
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$self->_stat('mode') || 0
l!l
50
MC/DC0%0/1
$self->_stat('mode') || 0
$self->_stat('mode')
Truth table50%1/2
A
A= $self->_stat('mode')
Aresult
11
00
75 5 ▶has mtime => sub ($self) { $self->_stat('mtime') || time };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self->_stat("mtime") or time 5 0 5
76 5 ▶has owner => sub ($self) { getpwuid($self->uid) || '' };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
getpwuid $self->uid or "" 5 0 5
77 1 ▶has path => sub ($self) { $self->asset->to_string || '' };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self->asset->to_string or "" 1 0 1
78 5 ▶has size => sub ($self) { $self->_stat('size') || 0 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self->_stat("size") or 0 4 1 5
79 2 has symlink => '';
80 5 ▶has type => sub ($self) { $self->_build_type };
Subroutine __ANON__: called CC=1 SCAR=0.0
81 5 ▶has uid => sub ($self) { $self->_stat('uid') || $( };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self->_stat("uid") or $( 5 0 5
82  
83 6 ▶sub add_block ($self, $block) {
Subroutine add_block: called CC=8 SCAR=21.4
Pod documented
84 6 ▶ return $self unless $self->type eq 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type eq 0 0 6 6
85  
86 6 ▶ $self->{bytes_added} //= 0;
Condition100%2/2
$self->{"bytes_added"} //= 0
l!l
24
MC/DC100%1/1
$self->{"bytes_added"} //= 0
$self->{"bytes_added"}
Truth table100%2/2
A
A= $self->{"bytes_added"}
Aresult
11
00
87 6 my $chunk = substr $block, 0, $self->size - $self->{bytes_added};
88 6 $self->{bytes_added} += length $chunk;
89 6 ▶ croak 'File size is out of range' if $self->{bytes_added} > $self->size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bytes_added"} > $self->size 0 6 6
90  
91 6 ▶ my $handle = $self->{add_block_handle} //= $self->asset->open('>');
Condition66%2/3
$self->{"add_block_handle"} //= $self->asset->open(">")
l!l&&r!l&&!r
240
MC/DC0%0/2
$self->{"add_block_handle"} //= $self->asset->open(">")
$self->{"add_block_handle"} $self->asset->open(">")
Truth table66%2/3
A //= B
A= $self->{"add_block_handle"}B= $self->asset->open(">")
ABresult
1X1
011
000
92 6 ▶ ($handle->syswrite($chunk) // -1) == length $chunk or croak "Can't write to asset: $!";
Branch: 1 branch
BranchTrueFalseTotal
($handle->syswrite($chunk) // -1) == length $chunk or croak("Can't write to asset: $!") 6 0 6
Condition50%1/2
$handle->syswrite($chunk) // -1
l!l
60
MC/DC0%0/1
$handle->syswrite($chunk) // -1
$handle->syswrite($chunk)
Truth table50%1/2
A
A= $handle->syswrite($chunk)
Aresult
11
00
93 6 ▶ $self->is_complete(1)->_cleanup if $self->{bytes_added} == $self->size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bytes_added"} == $self->size 4 2 6
94  
95 warn sprintf "[tar:add_block] chunk=%s/%s size=%s/%s is_complete=%s path=%s\n", length($chunk),
96 6 length($block), $self->{bytes_added}, $self->size, $self->is_complete, $self->path
97 if DEBUG;
98  
99 6 return $self;
100 }
101  
102 15 ▶sub from_header ($self, $header) {
Subroutine from_header: called CC=4 SCAR=13.9
Pod documented
103 15 my @fields = unpack $PACK_FORMAT, $header;
104 15 my $checksum = $self->_checksum($header);
105  
106 15 my ($prefix, $path) = map { _trim_nul($fields[$_]) } 15, 0;
107 15 ▶ $path = Mojo::File->new($prefix, $path)->to_string if length $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if length $prefix 2 13 15
108  
109 15 $self->path($path);
110 15 $self->mode(_from_oct($fields[1]));
111 15 $self->uid(_from_oct($fields[2]));
112 15 $self->gid(_from_oct($fields[3]));
113 15 $self->size(_from_oct($fields[4]));
114 15 $self->mtime(_from_oct($fields[5]));
115 15 ▶ $self->checksum($checksum eq $fields[6] =~ s/\0\s$//r ? $checksum : '');
Branch: 1 branch
BranchTrueFalseTotal
$checksum eq $fields[6] =~ s/\0\s$//ru ? : 14 1 15
116 15 ▶ $self->type($fields[7] eq "\0" ? '0' : $fields[7]);
Branch: 1 branch
BranchTrueFalseTotal
$fields[7] eq "\000" ? : 0 15 15
117 15 $self->symlink(_trim_nul($fields[8]));
118 15 $self->owner(_trim_nul($fields[11]));
119 15 $self->group(_trim_nul($fields[12]));
120 15 $self->dev_major($fields[13]);
121 15 $self->dev_minor($fields[14]);
122  
123 warn sprintf
124 "[tar:from_header] path=%s mode=%s uid=%s gid=%s size=%s mtime=%s checksum=%s type=%s symlink=%s owner=%s group=%s\n",
125 15 map { $self->$_ } qw(path mode uid gid size mtime checksum type symlink owner group)
126 if DEBUG;
127  
128 15 return $self;
129 }
130  
131 7 ▶sub to_header ($self) {
Subroutine to_header: called CC=4 SCAR=14.8
Pod documented
132 7 my ($name, $prefix) = (Mojo::File->new($self->path), '');
133 7 ▶ ($name, $prefix) = ($name->basename, $name->dirname->to_string) if length($name) > 100;
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 100 1 6 7
134 0 ▶ croak qq(path "@{[$self->path]}" is too long) if length($name) > 100 or length($prefix) > 155;
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 100 or length $prefix > 155 0 7 7
Condition33%1/3
length $name > 100 or length $prefix > 155
l!l&&r!l&&!r
007
MC/DC0%0/2
length $name > 100 or length $prefix > 155
length $name > 100 length $prefix > 155
Truth table33%1/3
A or B
A= length $name > 100B= length $prefix > 155
ABresult
1X1
011
000
135  
136 7 my $header = pack $PACK_FORMAT, $name, # 0
137 sprintf('%06o ', $self->mode), # 1
138 sprintf('%06o ', $self->uid), # 2
139 sprintf('%06o ', $self->gid), # 3
140 sprintf('%011o ', $self->size), # 4
141 sprintf('%011o ', $self->mtime), # 5
142 '', # 6 - checksum
143 $self->type, # 7
144 $self->symlink, # 8
145 "ustar\0", # 9 - ustar
146 '00', # 10 - ustar version
147 $self->owner, # 11
148 $self->group, # 12
149 sprintf('%07s', $self->dev_major), # 13
150 sprintf('%07s', $self->dev_minor), # 14
151 $prefix, # 15
152 ''; # 16 - padding
153  
154 # Inject checksum
155 7 substr $header, TAR_USTAR_CHECKSUM_POS, TAR_USTAR_CHECKSUM_LEN, $self->_checksum($header) . "\0 ";
156  
157 7 return $header;
158 }
159  
160 5 ▶sub _build_type ($self) {
Subroutine _build_type: called CC=10 SCAR=42.9
161 5 ▶ return '0' unless my $asset = $self->{asset};
Branch: 1 branch
BranchTrueFalseTotal
unless my $asset = $self->{"asset"} 0 5 5
162 5 ▶ return '0' if -f $asset; # plain file
Branch: 1 branch
BranchTrueFalseTotal
if -f $asset 5 0 5
163 0 ▶ return '1' if -l _; # symlink
Branch: 1 branch
BranchTrueFalseTotal
if -l _ 0 0 0
164 0 ▶ return '3' if -c _; # char dev
Branch: 1 branch
BranchTrueFalseTotal
if -c _ 0 0 0
165 0 ▶ return '4' if -b _; # block dev
Branch: 1 branch
BranchTrueFalseTotal
if -b _ 0 0 0
166 0 ▶ return '5' if -d _; # directory
Branch: 1 branch
BranchTrueFalseTotal
if -d _ 0 0 0
167 0 ▶ return '6' if -p _; # pipe
Branch: 1 branch
BranchTrueFalseTotal
if -p _ 0 0 0
168 0 ▶ return '8' if -s _; # socket
Branch: 1 branch
BranchTrueFalseTotal
if -s _ 0 0 0
169 0 ▶ return '2' if $asset->stat->nlink > 1; # hard link
Branch: 1 branch
BranchTrueFalseTotal
if $asset->stat->nlink > 1 0 0 0
170 0 return '9'; # unknown
171 }
172  
173 22 ▶sub _checksum ($self, $header) {
Subroutine _checksum: called CC=1 SCAR=0.0
174 22 return sprintf '%06o', int unpack '%16C*', join ' ',
175 substr($header, 0, TAR_USTAR_CHECKSUM_POS), substr($header, TAR_USTAR_TYPE_POS);
176 }
177  
178 24 ▶sub _cleanup ($self) {
Subroutine _cleanup: called CC=2 SCAR=6.9
179 24 my $handle = delete $self->{add_block_handle};
180 24 ▶ $handle->close if $handle;
Branch: 1 branch
BranchTrueFalseTotal
if $handle 4 20 24
181 }
182  
183 75 ▶sub _from_oct ($str) {
Subroutine _from_oct: called CC=2 SCAR=6.9
184 75 $str =~ s/^0+//;
185 75 $str =~ s/[\s\0]+$//;
186 75 ▶ return length($str) ? oct $str : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $str ? : 66 9 75
187 }
188  
189 26 ▶sub _stat ($self, $field) {
Subroutine _stat: called CC=5 SCAR=18.2
190 26 ▶ return undef unless my $stat = $self->{stat} //= $self->{asset} && $self->{asset}->stat || 0;
Branch: 1 branch
BranchTrueFalseTotal
unless my $stat = ($self->{"stat"} //= $self->{"asset"} && $self->{"asset"}->stat || 0) 0 26 26
Condition57%4/7
$self->{"stat"} //= $self->{"asset"} && $self->{"asset"}->stat || 0
l!l
206
$self->{'asset'} && $self->{'asset'}->stat || 0
l!l
60
$self->{"asset"} and $self->{"asset"}->stat
!ll&&!rl&&r
006
MC/DC100%1/1
$self->{"stat"} //= $self->{"asset"} && $self->{"asset"}->stat || 0
$self->{"stat"}
Truth table100%2/2
A
A= $self->{"stat"}
Aresult
11
00
191 26 return $stat->$field;
192 }
193  
194 75 ▶sub _trim_nul ($str) {
Subroutine _trim_nul: called CC=2 SCAR=6.9
195 75 my $idx = index $str, "\0";
196 75 ▶ return $idx == -1 ? $str : substr $str, 0, $idx;
Branch: 1 branch
BranchTrueFalseTotal
$idx == -1 ? : 2 73 75
197 }
198  
199 20 ▶sub DESTROY ($self) { $self->_cleanup }
Subroutine DESTROY: called CC=1 SCAR=0.0
200  
201 2 1;
202  
203 =encoding utf8
204  
205 =head1 NAME
206  
207 Mojo::Tar::File - A Mojo::Tar file
208  
209 =head1 SYNOPSIS
210  
211 my $file = Mojo::Tar->new(path => 'some/file.txt');
212  
213 # This can be dangerous! Make sure path() does not contain ".."
214 # or other dangerous path parts.
215 $file->asset->move_to($file->path);
216  
217 =head1 DESCRIPTION
218  
219 L<Mojo::Asset::File> represents a tar file.
220  
221 =head1 ATTRIBUTES
222  
223 =head2 asset
224  
225 $file = $file->asset(Mojo::File->new);
226 $asset = $file->asset;
227  
228 Returns a L<Mojo::File> object. Defaults to L<Mojo::File/tempfile>.
229  
230 This attribute is currently EXPERIMENTAL, but unlikely to change.
231  
232 =head2 checksum
233  
234 $str = $file->checksum;
235  
236 Holds the checksum read by L</from_header> or contains empty string if
237 the checksum does not match. This attribute can also be built from all the
238 attributes if L</from_header> was not called.
239  
240 =head2 dev_major
241  
242 This attribute is not supported yet. Pull request welcome!
243  
244 =head2 dev_minor
245  
246 This attribute is not supported yet. Pull request welcome!
247  
248 =head2 gid
249  
250 $file = $file->gid(1001);
251 $int = $file->gid;
252  
253 The numeric representation of L</group>.
254  
255 =head2 group
256  
257 $file = $file->group('users')
258 $str = $file->group;
259  
260 The string representation of L</gid>.
261  
262 =head2 is_complete
263  
264 $bool = $file->is_complete;
265  
266 Returns true if L</add_block> has added enough blocks to match L</size>.
267  
268 =head2 mode
269  
270 $file = $file->mode(0644); # 0644 == 420
271 $int = $file->mode;
272  
273 The file mode. Note that this is 10-base, meaning C<$int> will be something
274 like "420" and not "644".
275  
276 =head2 mtime
277  
278 $file = $file->mtime(time);
279 $epoch = $file->mtime;
280  
281 Epoch timestamp for this file.
282  
283 =head2 owner
284  
285 $file = $file->owner('jhthorsen')
286 $str = $file->owner;
287  
288 The string representation of L</uid>.
289  
290 =head2 path
291  
292 $file = $file->path('some/file/or/directory');
293 $str = $file->path;
294  
295 The path from the tar file. This is constructed with both the filename and
296 prefix (if any) in the ustar tar format.
297  
298 =head2 size
299  
300 $file = $file->size(42);
301 $int = $file->size;
302  
303 The size of the file in bytes.
304  
305 =head2 symlink
306  
307 $file = $file->symlink('path/for/symlink');
308 $str = $file->symlink;
309  
310 This attribute is not fully supported yet. Pull request welcome!
311  
312 =head2 type
313  
314 $file = $file->type(5);
315 $str = $file->type;
316  
317 The tar file type.
318  
319 This attribute is currently EXPERIMENTAL and might change from raw
320 representation to something more readable.
321  
322 =head2 uid
323  
324 $file = $file->uid(1001);
325 $int = $file->uid;
326  
327 The numeric representation of L</owner>.
328  
329 =head1 METHODS
330  
331 =head2 add_block
332  
333 $file = $file->add_block($bytes);
334  
335 Used to add a block from of bytes from the tar file to the L</asset>.
336  
337 =head2 from_header
338  
339 $file = $file->from_header($bytes);
340  
341 Will parse the header chunk from the tar file and set the L</ATTRIBUTES>.
342  
343 =head2 to_header
344  
345 $bytes = $file->to_header;
346  
347 Will construct a header chunk from the L</ATTRIBUTES>.
348  
349 =head1 SEE ALSO
350  
351 L<Mojo::Tar>.
352  
353 =cut
↑ Summary Tar.pm »