POSIX/Userland/Tar.pm

filter: Statementstmt 93.2% 97 / 104 Branchbran 72.5% 29 / 40 Conditioncond 73.3% 11 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 85.7% 157 / 183 CC 26 SCAR 25.1
CC
26
Cov
86%
_archive
23.3
_write_file
10.0
tar
2.0
CRAP 12.3
« POSIX/Bits/System.pm ↑ Summary POSIX/ReducedPrivileges/Directory.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;
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::Module ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use Filesys::POSIX::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Filesys::POSIX::Userland::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Filesys::POSIX::Userland::Tar::Header ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 7 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 my @METHODS = qw(tar);
24  
25 7 Filesys::POSIX::Module->export_methods( __PACKAGE__, @METHODS );
26  
27 =head1 NAME
28  
29 Filesys::POSIX::Userland::Tar - Generate ustar archives from L<Filesys::POSIX>
30  
31 =head1 SYNOPSIS
32  
33 use Filesys::POSIX;
34 use Filesys::POSIX::Mem;
35 use Filesys::POSIX::IO::Handle;
36 use Filesys::POSIX::Userland::Tar;
37  
38 my $fs = Filesys::POSIX->new(Filesys::POSIX::Mem->new,
39 'noatime' => 1
40 );
41  
42 $fs->mkdir('foo');
43 $fs->touch('foo/bar');
44  
45 $fs->tar(Filesys::POSIX::IO::Handle->new(\*STDOUT), '.');
46  
47 =head1 DESCRIPTION
48  
49 This module provides an implementation of the ustar standard on top of the
50 virtual filesystem layer, a mechanism intended to take advantage of the many
51 possible mapping and manipulation capabilities inherent in this mechanism.
52 Internally, it uses the L<Filesys::POSIX::Userland::Find> module to perform
53 depth- last recursion to locate inodes for packaging.
54  
55 As mentioned, archives are written in the ustar format, with pathnames of the
56 extended maximum length of 256 characters, supporting file sizes up to 4GB.
57 Currently, only user and group IDs are stored; names are not resolved and
58 stored as of the time of this writing. All inode types are supported for
59 archival.
60  
61 =head1 USAGE
62  
63 =over
64  
65 =cut
66  
67 7 our $BLOCK_SIZE = 512;
68 7 our $BUF_MAX = 20 * $BLOCK_SIZE;
69  
70 #
71 # NOTE: I'm only using $inode->open() calls to avoid having to call stat().
72 # This is not necessarily something that should be done by end user software.
73 #
74 11 ▶sub _write_file {
Subroutine _write_file: called CC=10 SCAR=23.0
75 11 my ( $fh, $inode, $handle, $size ) = @_;
76  
77 11 my $total = 0;
78 11 my $actual_file_len = 0;
79  
80 11 my $premature_eof;
81  
82 11 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$actual_file_len < $size and do { do { my $max_read = $size - $actual_file_len; $max_read = $Filesys::POSIX::Userland::Tar::BUF_MAX if $max_read > $Filesys::POSIX::Userland::Tar::BUF_MAX; my($len, $real_len, $buf); if ($premature_eof) { $len = ($real_len = $max_read); $buf = "\0" x $max_read; } else { $buf = ""; $real_len = 0; my $amt_read; do { my $incremental_buf; $amt_read = $fh->read($incremental_buf, $max_read - $real_len); $buf .= $incremental_buf; $real_len += $amt_read; if ($amt_read <= 0 and $max_read - $real_len > 0) { $premature_eof = 1; warn sprintf("WARNING: Short read while archiving file (expected total of %d bytes, but only got %d); padding with null bytes...", $size, $actual_file_len + $real_len); } } while $real_len < $max_read and $amt_read > 0; $len = $real_len; } ; if ((my $padlen = $Filesys::POSIX::Userland::Tar::BLOCK_SIZE - $len % $Filesys::POSIX::Userland::Tar::BLOCK_SIZE) != $Filesys::POSIX::Userland::Tar::BLOCK_SIZE) { $len += $padlen; $buf .= "\0" x $padlen; } ; my $written = 0; if (($written = $handle->write($buf, $len)) != $len) { Carp::confess("Short write while dumping file buffer to handle. Expected to write $len bytes, but only wrote $written."); } ; $actual_file_len += $real_len; $total += $written } } 4882 11 4893
83 4893 my $max_read = $size - $actual_file_len;
84 4893 ▶ $max_read = $BUF_MAX if $max_read > $BUF_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $max_read > $Filesys::POSIX::Userland::Tar::BUF_MAX 4882 11 4893
85  
86 4893 my ( $len, $real_len, $buf );
87 4893 ▶ if ($premature_eof) { # If we reach EOF before the expected length, pad with null bytes
Branch: 1 branch
BranchTrueFalseTotal
if ($premature_eof) { } 1 4892 4893
88 1 $len = $real_len = $max_read;
89 1 $buf = "\x0" x $max_read;
90 }
91 else {
92 4892 $buf = '';
93 4892 $real_len = 0;
94 4892 my $amt_read;
95  
96 # Attempt to read a total of $max_read bytes per buffer. ($max_read is either the
97 # maximum buffer size or the number of bytes expected remaining in the file, whichever
98 # is smaller.)
99 #
100 # Possible outcomes:
101 #
102 # 1. We received no bytes, in which case we have reached EOF unexpectedly.
103 # Produce a warning and set the flag to pad the remaining portion of the
104 # file with null bytes.
105 # 2. We received exactly $max_read bytes. This is good and means we can drop out of
106 # this sub-loop after a single iteration per read loop iteration. (Should be the
107 # most common case.)
108 # 3. We received some bytes, but not as many as we expected. Retry the read,
109 # accumulating bytes until we either have a total of $max_read bytes for
110 # this block or we reach EOF.
111 4892 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$real_len < $max_read and $amt_read > 0 and do { do { my $incremental_buf; $amt_read = $fh->read($incremental_buf, $max_read - $real_len); $buf .= $incremental_buf; $real_len += $amt_read; if ($amt_read <= 0 and $max_read - $real_len > 0) { $premature_eof = 1; warn sprintf("WARNING: Short read while archiving file (expected total of %d bytes, but only got %d); padding with null bytes...", $size, $actual_file_len + $real_len); } } } 1 4892 4893
Condition100%3/3
$real_len < $max_read and $amt_read > 0
!ll&&!rl&&r
489111
MC/DC100%2/2
$real_len < $max_read and $amt_read > 0
$real_len < $max_read $amt_read > 0
Truth table100%3/3
A and B
A= $real_len < $max_readB= $amt_read > 0
ABresult
0X0
100
111
112 4893 my $incremental_buf;
113 4893 $amt_read = $fh->read( $incremental_buf, $max_read - $real_len );
114 4893 $buf .= $incremental_buf;
115 4893 $real_len += $amt_read;
116  
117 4893 ▶ if ( $amt_read <= 0 && $max_read - $real_len > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amt_read <= 0 and $max_read - $real_len > 0) 1 4892 4893
Condition66%2/3
$amt_read <= 0 and $max_read - $real_len > 0
!ll&&!rl&&r
489201
MC/DC50%1/2
$amt_read <= 0 and $max_read - $real_len > 0
$amt_read <= 0 $max_read - $real_len > 0
Truth table66%2/3
A and B
A= $amt_read <= 0B= $max_read - $real_len > 0
ABresult
0X0
100
111
118 1 $premature_eof = 1;
119 1 warn sprintf(
120 'WARNING: Short read while archiving file (expected total of %d bytes, but only got %d); padding with null bytes...',
121 $size, $actual_file_len + $real_len,
122 );
123 }
124 } while ( $real_len < $max_read && $amt_read > 0 );
125  
126 4892 $len = $real_len;
127 }
128  
129 4893 ▶ if ( ( my $padlen = $BLOCK_SIZE - ( $len % $BLOCK_SIZE ) ) != $BLOCK_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $padlen = $Filesys::POSIX::Userland::Tar::BLOCK_SIZE - $len % $Filesys::POSIX::Userland::Tar::BLOCK_SIZE) != $Filesys::POSIX::Userland::Tar::BLOCK_SIZE) 8 4885 4893
130 8 $len += $padlen;
131 8 $buf .= "\x0" x $padlen;
132 }
133  
134 4893 my $written = 0;
135  
136 4893 ▶ if ( ( $written = $handle->write( $buf, $len ) ) != $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($written = $handle->write($buf, $len)) != $len) 0 4893 4893
137 0 Carp::confess("Short write while dumping file buffer to handle. Expected to write $len bytes, but only wrote $written.");
138 }
139  
140 4893 $actual_file_len += $real_len;
141 4893 $total += $written;
142 } while ( $actual_file_len < $size );
143  
144 11 $fh->close;
145  
146 11 return $total;
147 }
148  
149 75 ▶sub _archive {
Subroutine _archive: called CC=15 SCAR=31.5
150 75 my ( $inode, $handle, $path, $opts ) = @_;
151  
152 75 my $written = 0;
153  
154 75 my $header = Filesys::POSIX::Userland::Tar::Header->from_inode( $inode, $path );
155 75 my $blocks = '';
156  
157 75 ▶ if ( $header->{'truncated'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header->{"truncated"}) 0 75 75
158  
159 0 ▶ if ( $opts->{'gnu_extensions'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'gnu_extensions'}) { } 0 0 0
elsif ($opts->{'posix_extensions'}) { } 0 0 0
160 0 $blocks .= $header->encode_longlink;
161 }
162 elsif ( $opts->{'posix_extensions'} ) {
163 0 $blocks .= $header->encode_posix;
164 }
165 else {
166 0 die('Filename too long');
167 }
168 }
169  
170 75 $blocks .= $header->encode;
171 75 local $@;
172  
173 75 eval {
174 # Acquire the file handle before writing the header so we don't corrupt
175 # the tarball if the file is missing.
176 75 my $fh;
177  
178 75 ▶ if ( $inode->file && $header->{'size'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inode->file and $header->{"size"} > 0) 13 62 75
Condition100%3/3
$inode->file and $header->{"size"} > 0
!ll&&!rl&&r
60213
MC/DC100%2/2
$inode->file and $header->{"size"} > 0
$inode->file $header->{"size"} > 0
Truth table100%3/3
A and B
A= $inode->fileB= $header->{"size"} > 0
ABresult
0X0
100
111
179 13 $fh = $inode->open( $O_RDONLY | $O_NONBLOCK ); # Case 82969: No block on pipes
180 }
181  
182 # write header
183 73 my $header_len = length $blocks;
184 73 ▶ unless ( $handle->write( $blocks, $header_len ) == $header_len ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->write($blocks, $header_len) == $header_len) 0 73 73
185 0 Carp::confess('Short write while dumping tar header to file handle');
186 }
187 73 $written += $header_len;
188  
189 # write file
190 73 ▶ $written += _write_file( $fh, $inode, $handle, $header->{'size'} ) if ($fh);
Branch: 1 branch
BranchTrueFalseTotal
if $fh 11 62 73
191 };
192  
193 75 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 2 73 75
194 2 ▶ if ( $! == &Errno::ENOENT && $opts->{'ignore_missing'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! == &Errno::ENOENT and $opts->{'ignore_missing'}) { } 1 1 2
elsif ($! == &Errno::EACCES and $opts->{'ignore_inaccessible'}) { } 0 1 1
Condition66%2/3
$! == &Errno::ENOENT and $opts->{"ignore_missing"}
!ll&&!rl&&r
011
MC/DC50%1/2
$! == &Errno::ENOENT and $opts->{"ignore_missing"}
$! == &Errno::ENOENT $opts->{"ignore_missing"}
Truth table66%2/3
A and B
A= $! == &Errno::ENOENTB= $opts->{"ignore_missing"}
ABresult
0X0
100
111
195 $opts->{'ignore_missing'}->($path)
196 1 ▶ if ref $opts->{'ignore_missing'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{"ignore_missing"} eq "CODE" 1 0 1
197 }
198 ▶ elsif ( $! == &Errno::EACCES && $opts->{'ignore_inaccessible'} ) {
Condition33%1/3
$! == &Errno::EACCES and $opts->{"ignore_inaccessible"}
!ll&&!rl&&r
100
MC/DC0%0/2
$! == &Errno::EACCES and $opts->{"ignore_inaccessible"}
$! == &Errno::EACCES $opts->{"ignore_inaccessible"}
Truth table33%1/3
A and B
A= $! == &Errno::EACCESB= $opts->{"ignore_inaccessible"}
ABresult
0X0
100
111
199 $opts->{'ignore_inaccessible'}->($path)
200 0 ▶ if ref $opts->{'ignore_inaccessible'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{"ignore_inaccessible"} eq "CODE" 0 0 0
201 }
202 else {
203 1 die $@;
204 }
205 }
206  
207 74 return $written;
208 }
209  
210 =item C<$fs-E<gt>tar($handle, @items)>
211  
212 =item C<$fs-E<gt>tar($handle, $opts, @items)>
213  
214 Locate files and directories in each path specified in the C<@items> array,
215 writing results to the I/O handle wrapper specified by C<$handle>, an instance
216 of L<Filesys::POSIX::IO::Handle>. When an anonymous HASH argument, C<$opts>, is
217 specified, the data is passed unmodified to L<Filesys::POSIX::Userland::Find>.
218 In this way, for instance, the behavior of following symlinks can be specified.
219  
220 In addition to options supported by L<Filesys::POSIX::Userland::Find>, the
221 following options are recognized uniquely by C<$fs-E<gt>tar()>:
222  
223 =over
224  
225 =item C<gnu_extensions>
226  
227 When set, certain GNU extensions to the tar output format are enabled, namely
228 support for arbitrarily long filenames.
229  
230 =item C<ignore_missing>
231  
232 When set, ignore if a file is missing when writing it to the tarball. This can
233 happen if a file is removed between the time the find functionality finds it and
234 the time it is actually written to the output. If the value is a coderef, calls
235 that function with the name of the missing file.
236  
237 =item C<ignore_inaccessible>
238  
239 When set, ignore if a file is unreadable when writing it to the tarball. This can
240 happen if a file permissions do not allow the current UID and GID to read the file.
241 If the value is a coderef, calls that function with the name of the inaccessible
242 file.
243  
244 =back
245  
246 =cut
247  
248 11 ▶sub tar {
Subroutine tar: called CC=2 SCAR=6.9
Pod documented
249 11 my $self = shift;
250 11 my $handle = shift;
251 11 ▶ my $opts = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 10 11
252 11 my @items = @_;
253 11 my $unpadded = 0;
254  
255 $self->find(
256 76 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
257 76 my ( $path, $inode ) = @_;
258  
259 76 ▶ return if $inode->sock;
Branch: 1 branch
BranchTrueFalseTotal
if $inode->sock 1 75 76
260  
261 75 $unpadded += _archive( $inode, $handle, $path->full, $opts );
262 74 $unpadded %= $BUF_MAX;
263 },
264 11 $opts,
265 @items
266 );
267  
268 10 my $padlen = $BUF_MAX - ( $unpadded % $BUF_MAX );
269 10 $handle->write( "\x00" x $padlen, $padlen );
270  
271 10 return;
272 }
273  
274 =back
275  
276 =cut
277  
278 7 1;
279  
280 __END__
« POSIX/Bits/System.pm ↑ Summary POSIX/ReducedPrivileges/Directory.pm »