blib/lib/Archive/Tar/Wrapper/IPC/Cmd.pm

filter: Statementstmt 78.9% 199 / 252 Branchbran 56.8% 91 / 160 Conditioncond 61.1% 11 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 87.8% 29 / 33 Podpod 0.0% 0 / 19 totaltotal 67.8% 334 / 492 CC 90 SCAR 29.0
CC
90
Cov
70%
ramdisk_mount
72.0
add
38.8
write
17.0
CRAP 18.1
↑ Summary
1 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Archive::Tar::Wrapper::IPC::Cmd;
4 # ABSTRACT: Archive-Tar-Wrapper minus IPC::Run, IO::Pty
5 6 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use IPC::Cmd qw(run);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 our $VERSION = "0.22";
17  
18 ###########################################
19 13 ▶sub new {
Subroutine new: called CC=8 SCAR=25.1
Pod undocumented
20 ###########################################
21 13 my($class, %options) = @_;
22  
23 13 my $self = {
24 tar => undef,
25 tmpdir => undef,
26 tar_read_options => '',
27 tar_write_options => '',
28 tar_gnu_read_options => [],
29 dirs => 0,
30 max_cmd_line_args => 512,
31 ramdisk => undef,
32 %options,
33 };
34  
35 13 bless $self, $class;
36  
37 13 ▶ $self->{tar} = bin_find("tar") unless defined $self->{tar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tar"} 13 0 13
38 13 ▶ $self->{tar} = bin_find("gtar") unless defined $self->{tar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tar"} 0 13 13
39  
40 13 ▶ if( ! defined $self->{tar} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"tar"}) 0 13 13
41 0 LOGDIE "tar not found in PATH, please specify location";
42 }
43  
44 13 ▶ if(defined $self->{ramdisk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'ramdisk'}) { } 0 13 13
45 0 my $rc = $self->ramdisk_mount( %{ $self->{ramdisk} } );
46 0 ▶ if(!$rc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc) 0 0 0
47 0 LOGDIE "Mounting ramdisk failed";
48 }
49 0 $self->{tmpdir} = $self->{ramdisk}->{tmpdir};
50 } else {
51 $self->{tmpdir} = tempdir($self->{tmpdir} ?
52 13 ▶ (DIR => $self->{tmpdir}) : ());
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tmpdir'} ? : 2 11 13
53 }
54  
55 13 $self->{tardir} = File::Spec->catfile($self->{tmpdir}, "tar");
56 13 ▶ mkpath [$self->{tardir}], 0, 0755 or
Branch: 1 branch
BranchTrueFalseTotal
mkpath([$self->{"tardir"}], 0, 493) or LOGDIE(qq[Cannot mkpath $self->{"tardir"} ($!)]) 13 0 13
57 LOGDIE "Cannot mkpath $self->{tardir} ($!)";
58  
59 13 $self->{objdir} = tempdir();
60  
61 13 return $self;
62 }
63  
64 ###########################################
65 0 ▶sub tardir {
Subroutine tardir: not called CC=1 SCAR=6.9
Pod undocumented
66 ###########################################
67 0 my($self) = @_;
68  
69 0 return $self->{tardir};
70 }
71  
72 ###########################################
73 12 ▶sub read {
Subroutine read: called CC=8 SCAR=20.9
Pod undocumented
74 ###########################################
75 12 my($self, $tarfile, @files) = @_;
76  
77 12 my $cwd = getcwd();
78  
79 12 ▶ unless(File::Spec::Functions::file_name_is_absolute($tarfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Spec::Functions::file_name_is_absolute($tarfile)) 9 3 12
80 9 $tarfile = File::Spec::Functions::rel2abs($tarfile, $cwd);
81 }
82  
83 chdir $self->{tardir} or
84 12 ▶ LOGDIE "Cannot chdir to $self->{tardir}";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"tardir"} or LOGDIE(qq[Cannot chdir to $self->{"tardir"}]) 12 0 12
85  
86 12 my $compr_opt = "";
87 12 ▶ $compr_opt = "z" if $self->is_compressed($tarfile);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_compressed($tarfile) 3 9 12
88  
89 my $cmd = [$self->{tar}, "${compr_opt}x$self->{tar_read_options}",
90 12 @{$self->{tar_gnu_read_options}},
91 "-f", $tarfile, @files];
92  
93 12 DEBUG "Running @$cmd";
94  
95 12 my( $success, $error_message, $full_buf, $out, $err ) =
96 run( command => $cmd, verbose => 0 );
97 12 ▶ if(!$success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 1 11 12
98 1 ERROR "@$cmd failed: $err";
99 1 ▶ chdir $cwd or LOGDIE "Cannot chdir to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Cannot chdir to $cwd") 1 0 1
100 1 return undef;
101 }
102  
103 11 ▶ WARN $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 11 0 11
104  
105 11 ▶ chdir $cwd or LOGDIE "Cannot chdir to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Cannot chdir to $cwd") 11 0 11
106  
107 11 return 1;
108 }
109  
110 ###########################################
111 12 ▶sub is_compressed {
Subroutine is_compressed: called CC=6 SCAR=19.0
Pod undocumented
112 ###########################################
113 12 my($self, $tarfile) = @_;
114  
115 12 ▶ return 1 if $tarfile =~ /\.t?gz$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $tarfile =~ /\.t?gz$/i 3 9 12
116  
117 # Sloppy check for gzip files
118 9 ▶ open FILE, "<$tarfile" or die "Cannot open $tarfile";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$tarfile" or die "Cannot open $tarfile" 9 0 9
119 9 binmode FILE;
120 9 ▶ my $read = sysread(FILE, my $two, 2, 0) or die "Cannot sysread";
Branch: 1 branch
BranchTrueFalseTotal
my $read = sysread(FILE, my $two, 2, 0) or die "Cannot sysread" 9 0 9
121 9 close FILE;
122 9 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if ord substr($two, 0, 1) eq 31 and ord substr($two, 1, 1) eq 139 0 9 9
Condition33%1/3
ord substr($two, 0, 1) eq 31 and ord substr($two, 1, 1) eq 139
!ll&&!rl&&r
900
MC/DC0%0/2
ord substr($two, 0, 1) eq 31 and ord substr($two, 1, 1) eq 139
ord substr($two, 0, 1) eq 31 ord substr($two, 1, 1) eq 139
Truth table33%1/3
A and B
A= ord substr($two, 0, 1) eq 31B= ord substr($two, 1, 1) eq 139
ABresult
0X0
100
111
123 ord(substr($two, 0, 1)) eq 0x1F and
124 ord(substr($two, 1, 1)) eq 0x8B;
125  
126 9 return 0;
127 }
128  
129 ###########################################
130 13 ▶sub locate {
Subroutine locate: called CC=2 SCAR=6.9
Pod undocumented
131 ###########################################
132 13 my($self, $rel_path) = @_;
133  
134 13 my $real_path = File::Spec->catfile($self->{tardir}, $rel_path);
135  
136 13 ▶ if(-e $real_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $real_path) 12 1 13
137 12 DEBUG "$real_path exists";
138 12 return $real_path;
139 }
140 1 DEBUG "$real_path doesn't exist";
141  
142 1 WARN "$rel_path not found in tarball";
143 1 return undef;
144 }
145  
146 ###########################################
147 10 ▶sub add {
Subroutine add: called CC=27 SCAR=36.6
Pod undocumented
148 ###########################################
149 10 my($self, $rel_path, $path_or_stringref, $opts) = @_;
150
151 10 ▶ if($opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts) 2 8 10
152 2 ▶ if(!ref($opts) or ref($opts) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $opts or ref $opts ne "HASH") 0 2 2
Condition33%1/3
not ref $opts or ref $opts ne "HASH"
l!l&&r!l&&!r
002
MC/DC0%0/2
not ref $opts or ref $opts ne "HASH"
not ref $opts ref $opts ne "HASH"
Truth table33%1/3
not(A) or B
A= not ref $optsB= ref $opts ne "HASH"
ABresult
1X1
011
000
153 0 LOGDIE "Option parameter given to add() not a hashref.";
154 }
155 }
156  
157 10 ▶ my $perm = $opts->{perm} if defined $opts->{perm};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"perm"} 1 9 10
158 10 ▶ my $uid = $opts->{uid} if defined $opts->{uid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"uid"} 0 10 10
159 10 ▶ my $gid = $opts->{gid} if defined $opts->{gid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"gid"} 0 10 10
160 10 ▶ my $binmode = $opts->{binmode} if defined $opts->{binmode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"binmode"} 1 9 10
161  
162 10 my $target = File::Spec->catfile($self->{tardir}, $rel_path);
163 10 my $target_dir = dirname($target);
164  
165 10 ▶ if( ! -d $target_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $target_dir) 5 5 10
166 5 ▶ if( ref($path_or_stringref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path_or_stringref) { } 1 4 5
167 1 $self->add( dirname( $rel_path ), dirname( $target_dir ) );
168 } else {
169 4 $self->add( dirname( $rel_path ), dirname( $path_or_stringref ) );
170 }
171 }
172  
173 10 ▶ if(ref($path_or_stringref)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $path_or_stringref) { } 2 8 10
elsif (-d $path_or_stringref) { } 5 3 8
174 2 ▶ open FILE, ">$target" or LOGDIE "Can't open $target ($!)";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$target" or LOGDIE("Can't open $target ($!)") 2 0 2
175 2 ▶ if(defined $binmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $binmode) 1 1 2
176 1 binmode FILE, $binmode;
177 }
178 2 print FILE $$path_or_stringref;
179 2 close FILE;
180 } elsif( -d $path_or_stringref ) {
181 # perms will be fixed further down
182 5 ▶ mkpath($target, 0, 0755) unless -d $target;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $target 5 0 5
183 } else {
184 3 ▶ copy $path_or_stringref, $target or
Branch: 1 branch
BranchTrueFalseTotal
copy($path_or_stringref, $target) or LOGDIE("Can't copy $path_or_stringref to $target ($!)") 3 0 3
185 LOGDIE "Can't copy $path_or_stringref to $target ($!)";
186 }
187  
188 10 ▶ if(defined $uid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uid) 0 10 10
189 0 ▶ chown $uid, -1, $target or
Branch: 1 branch
BranchTrueFalseTotal
chown $uid, -1, $target or LOGDIE("Can't chown $target uid to $uid ($!)") 0 0 0
190 LOGDIE "Can't chown $target uid to $uid ($!)";
191 }
192  
193 10 ▶ if(defined $gid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gid) 0 10 10
194 0 ▶ chown -1, $gid, $target or
Branch: 1 branch
BranchTrueFalseTotal
chown -1, $gid, $target or LOGDIE("Can't chown $target gid to $gid ($!)") 0 0 0
195 LOGDIE "Can't chown $target gid to $gid ($!)";
196 }
197  
198 10 ▶ if(defined $perm) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $perm) 1 9 10
199 1 ▶ chmod $perm, $target or
Branch: 1 branch
BranchTrueFalseTotal
chmod $perm, $target or LOGDIE("Can't chmod $target to $perm ($!)") 1 0 1
200 LOGDIE "Can't chmod $target to $perm ($!)";
201 }
202  
203 10 ▶ if(!defined $uid and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uid or defined $gid or defined $perm or ref $path_or_stringref) 7 3 10
Condition66%6/9
defined $uid or defined $gid or defined $perm or ref $path_or_stringref
l!l&&r!l&&!r
127
defined $uid or defined $gid or defined $perm
l!l&&r!l&&!r
019
defined $uid or defined $gid
l!l&&r!l&&!r
0010
MC/DC50%2/4
defined $uid or defined $gid or defined $perm or ref $path_or_stringref
defined $uid defined $gid defined $perm ref $path_or_stringref
Truth table60%3/5
A or B or C or D
A= defined $uidB= defined $gidC= defined $permD= ref $path_or_stringref
ABCDresult
1XXX1
01XX1
001X1
00011
00000
204 !defined $gid and
205 !defined $perm and
206 !ref($path_or_stringref)) {
207 7 ▶ perm_cp($path_or_stringref, $target) or
Branch: 1 branch
BranchTrueFalseTotal
perm_cp($path_or_stringref, $target) or LOGDIE("Can't perm_cp $path_or_stringref to $target ($!)") 7 0 7
208 LOGDIE "Can't perm_cp $path_or_stringref to $target ($!)";
209 }
210  
211 10 1;
212 }
213  
214 ######################################
215 7 ▶sub perm_cp {
Subroutine perm_cp: called CC=1 SCAR=0.0
Pod undocumented
216 ######################################
217 # Lifted from Ben Okopnik's
218 # http://www.linuxgazette.com/issue87/misc/tips/cpmod.pl.txt
219  
220 7 my $perms = perm_get($_[0]);
221 7 perm_set($_[1], $perms);
222 }
223  
224 ######################################
225 7 ▶sub perm_get {
Subroutine perm_get: called CC=2 SCAR=7.1
Pod undocumented
226 ######################################
227 7 my($filename) = @_;
228  
229 7 ▶ my @stats = (stat $filename)[2,4,5] or
Branch: 1 branch
BranchTrueFalseTotal
my(@stats) = (stat $filename)[2, 4, 5] or LOGDIE("Cannot stat $filename ($!)") 7 0 7
230 LOGDIE "Cannot stat $filename ($!)";
231  
232 7 return \@stats;
233 }
234  
235 ######################################
236 7 ▶sub perm_set {
Subroutine perm_set: called CC=3 SCAR=11.7
Pod undocumented
237 ######################################
238 7 my($filename, $perms) = @_;
239  
240 7 ▶ chown($perms->[1], $perms->[2], $filename) or
Branch: 1 branch
BranchTrueFalseTotal
chown $perms->[1], $perms->[2], $filename or LOGDIE("Cannot chown $filename ($!)") 7 0 7
241 LOGDIE "Cannot chown $filename ($!)";
242 7 ▶ chmod($perms->[0] & 07777, $filename) or
Branch: 1 branch
BranchTrueFalseTotal
chmod $perms->[0] & 4095, $filename or LOGDIE("Cannot chmod $filename ($!)") 7 0 7
243 LOGDIE "Cannot chmod $filename ($!)";
244 }
245  
246 ###########################################
247 0 ▶sub remove {
Subroutine remove: not called CC=2 SCAR=17.9
Pod undocumented
248 ###########################################
249 0 my($self, $rel_path) = @_;
250  
251 0 my $target = File::Spec->catfile($self->{tardir}, $rel_path);
252  
253 0 ▶ rmtree($target) or LOGDIE "Can't rmtree $target ($!)";
Branch: 1 branch
BranchTrueFalseTotal
rmtree($target) or LOGDIE("Can't rmtree $target ($!)") 0 0 0
254 }
255  
256 ###########################################
257 4 ▶sub list_all {
Subroutine list_all: called CC=2 SCAR=6.9
Pod undocumented
258 ###########################################
259 4 my($self) = @_;
260  
261 4 my @entries = ();
262  
263 4 $self->list_reset();
264  
265 4 ▶ while(my $entry = $self->list_next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->list_next) 12 4 16
266 12 push @entries, $entry;
267 }
268  
269 4 return \@entries;
270 }
271  
272 ###########################################
273 6 ▶sub list_reset {
Subroutine list_reset: called CC=4 SCAR=14.1
Pod undocumented
274 ###########################################
275 6 my($self) = @_;
276  
277 6 my $list_file = File::Spec->catfile($self->{objdir}, "list");
278 6 ▶ open FILE, ">$list_file" or LOGDIE "Can't open $list_file";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$list_file" or LOGDIE("Can't open $list_file") 6 0 6
279  
280 6 my $cwd = getcwd();
281 6 ▶ chdir $self->{tardir} or LOGDIE "Can't chdir to $self->{tardir} ($!)";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"tardir"} or LOGDIE(qq[Can't chdir to $self->{"tardir"} ($!)]) 6 0 6
282  
283 31 ▶ find(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
284 31 my $entry = $File::Find::name;
285 31 $entry =~ s#^\./##;
286 31 ▶ my $type = (-d $_ ? "d" :
Branch: 2 branches
BranchTrueFalseTotal
-d $_ ? : 14 17 31
-l $_ ? : 0 17 17
287 -l $_ ? "l" :
288 "f"
289 );
290 31 print FILE "$type $entry\n";
291 6 }, ".");
292  
293 6 ▶ chdir $cwd or LOGDIE "Can't chdir to $cwd ($!)";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Can't chdir to $cwd ($!)") 6 0 6
294  
295 6 close FILE;
296  
297 6 $self->offset(0);
298 }
299  
300 ###########################################
301 25 ▶sub list_next {
Subroutine list_next: called CC=5 SCAR=16.1
Pod undocumented
302 ###########################################
303 25 my($self) = @_;
304  
305 25 my $offset = $self->offset();
306  
307 25 my $list_file = File::Spec->catfile($self->{objdir}, "list");
308 25 ▶ open FILE, "<$list_file" or LOGDIE "Can't open $list_file";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$list_file" or LOGDIE("Can't open $list_file") 25 0 25
309 25 seek FILE, $offset, 0;
310  
311 25 { my $line = <FILE>;
312  
313 37 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 6 31 37
314  
315 31 chomp $line;
316 31 my($type, $entry) = split / /, $line, 2;
317 31 ▶ redo if $type eq "d" and ! $self->{dirs};
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "d" and not $self->{"dirs"} 12 19 31
Condition100%3/3
$type eq "d" and not $self->{"dirs"}
!ll&&!rl&&r
17212
MC/DC100%2/2
$type eq "d" and not $self->{"dirs"}
$type eq "d" not $self->{"dirs"}
Truth table100%3/3
A and not(B)
A= $type eq "d"B= not $self->{"dirs"}
ABresult
0X0
100
111
318 19 $self->offset(tell FILE);
319 19 return [$entry, File::Spec->catfile($self->{tardir}, $entry),
320 $type];
321 }
322 }
323  
324 ###########################################
325 50 ▶sub offset {
Subroutine offset: called CC=4 SCAR=14.1
Pod undocumented
326 ###########################################
327 50 my($self, $new_offset) = @_;
328  
329 50 my $offset_file = File::Spec->catfile($self->{objdir}, "offset");
330  
331 50 ▶ if(defined $new_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_offset) 25 25 50
332 25 ▶ open FILE, ">$offset_file" or LOGDIE "Can't open $offset_file";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$offset_file" or LOGDIE("Can't open $offset_file") 25 0 25
333 25 print FILE "$new_offset\n";
334 25 close FILE;
335 }
336  
337 50 ▶ open FILE, "<$offset_file" or LOGDIE "Can't open $offset_file (Did you call list_next() without a previous list_reset()?)";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$offset_file" or LOGDIE("Can't open $offset_file (Did you call list_next() without a previous list_reset()?)") 50 0 50
338 50 my $offset = <FILE>;
339 50 chomp $offset;
340 50 return $offset;
341 0 close FILE;
342 }
343  
344 ###########################################
345 4 ▶sub write {
Subroutine write: called CC=12 SCAR=28.3
Pod undocumented
346 ###########################################
347 4 my($self, $tarfile, $compress) = @_;
348  
349 4 my $cwd = getcwd();
350 4 ▶ chdir $self->{tardir} or LOGDIE "Can't chdir to $self->{tardir} ($!)";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"tardir"} or LOGDIE(qq[Can't chdir to $self->{"tardir"} ($!)]) 4 0 4
351  
352 4 ▶ unless(File::Spec::Functions::file_name_is_absolute($tarfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Spec::Functions::file_name_is_absolute($tarfile)) 0 4 4
353 0 $tarfile = File::Spec::Functions::rel2abs($tarfile, $cwd);
354 }
355  
356 4 my $compr_opt = "";
357 4 ▶ $compr_opt = "z" if $compress;
Branch: 1 branch
BranchTrueFalseTotal
if $compress 1 3 4
358  
359 4 ▶ opendir DIR, "." or LOGDIE "Cannot open $self->{tardir}";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, "." or LOGDIE(qq[Cannot open $self->{"tardir"}]) 4 0 4
360 4 my @top_entries = grep { $_ !~ /^\.\.?$/ } readdir DIR;
361 4 closedir DIR;
362  
363 4 my $cmd;
364  
365 4 ▶ if(@top_entries > $self->{max_cmd_line_args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@top_entries > $self->{'max_cmd_line_args'}) { } 0 4 4
366 0 my $filelist_file = $self->{tmpdir}."/file-list";
367 0 ▶ open FLIST, ">$filelist_file" or
Branch: 1 branch
BranchTrueFalseTotal
open FLIST, ">$filelist_file" or LOGDIE("Cannot open $filelist_file ($!)") 0 0 0
368 LOGDIE "Cannot open $filelist_file ($!)";
369 0 for(@top_entries) {
370 0 print FLIST "$_\n";
371 }
372 0 close FLIST;
373 0 $cmd = [$self->{tar}, "${compr_opt}cf$self->{tar_write_options}",
374 $tarfile, "-T", $filelist_file];
375 } else {
376 4 $cmd = [$self->{tar}, "${compr_opt}cf$self->{tar_write_options}",
377 $tarfile, @top_entries];
378 }
379  
380  
381 4 DEBUG "Running @$cmd";
382 4 my( $success, $error_message, $full_buf, $out, $err ) =
383 run( command => $cmd, verbose => 0 );
384 4 ▶ if(!$success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 1 3 4
385 1 ERROR "@$cmd failed: $err";
386 1 ▶ chdir $cwd or LOGDIE "Cannot chdir to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Cannot chdir to $cwd") 1 0 1
387 1 return undef;
388 }
389  
390 3 ▶ WARN $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 0 3
391  
392 3 ▶ chdir $cwd or LOGDIE "Cannot chdir to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Cannot chdir to $cwd") 3 0 3
393  
394 3 return 1;
395 }
396  
397 ###########################################
398 13 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=14.9
399 ###########################################
400 13 my($self) = @_;
401  
402 13 ▶ $self->ramdisk_unmount() if defined $self->{ramdisk};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ramdisk"} 0 13 13
403  
404 13 ▶ rmtree($self->{objdir}) if defined $self->{objdir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"objdir"} 13 0 13
405 13 ▶ rmtree($self->{tmpdir}) if defined $self->{tmpdir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tmpdir"} 13 0 13
406 }
407  
408 ######################################
409 13 ▶sub bin_find {
Subroutine bin_find: called CC=3 SCAR=11.0
Pod undocumented
410 ######################################
411 13 my($exe) = @_;
412  
413 13 my @paths = split /:/, $ENV{PATH};
414  
415 13 push @paths,
416 "/usr/bin",
417 "/bin",
418 "/usr/sbin",
419 "/opt/bin",
420 "/ops/csw/bin",
421 ;
422  
423 13 for my $path ( @paths ) {
424 104 my $full = File::Spec->catfile($path, $exe);
425 104 ▶ return $full if -x $full;
Branch: 1 branch
BranchTrueFalseTotal
if -x $full 13 91 104
426 }
427  
428 0 return undef;
429 }
430  
431 ###########################################
432 1 ▶sub is_gnu {
Subroutine is_gnu: called CC=2 SCAR=7.0
Pod undocumented
433 ###########################################
434 1 my($self) = @_;
435  
436 1 ▶ open PIPE, "$self->{tar} --version |" or
Branch: 1 branch
BranchTrueFalseTotal
open PIPE, qq[$self->{"tar"} --version |] or return 0 1 0 1
437 return 0;
438  
439 1 my $output = join "\n", <PIPE>;
440 1 close PIPE;
441  
442 1 return $output =~ /GNU/;
443 }
444  
445 ###########################################
446 0 ▶sub ramdisk_mount {
Subroutine ramdisk_mount: not called CC=8 SCAR=42.8
Pod undocumented
447 ###########################################
448 0 my($self, %options) = @_;
449  
450 # mkdir -p /mnt/myramdisk
451 # mount -t tmpfs -o size=20m tmpfs /mnt/myramdisk
452  
453 0 ▶ $self->{mount} = bin_find("mount") unless $self->{mount};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mount"} 0 0 0
454 0 ▶ $self->{umount} = bin_find("umount") unless $self->{umount};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"umount"} 0 0 0
455  
456 0 for (qw(mount umount)) {
457 0 ▶ if(!defined $self->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$_}) 0 0 0
458 0 LOGWARN "No $_ command found in PATH";
459 0 return undef;
460 }
461 }
462  
463 0 $self->{ramdisk} = { %options };
464
465 $self->{ramdisk}->{size} = "100m" unless
466 0 ▶ defined $self->{ramdisk}->{size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ramdisk"}{"size"} 0 0 0
467
468 0 ▶ if(! defined $self->{ramdisk}->{tmpdir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ramdisk"}{"tmpdir"}) 0 0 0
469 0 $self->{ramdisk}->{tmpdir} = tempdir( CLEANUP => 1 );
470 }
471
472 my @cmd = ($self->{mount},
473 "-t", "tmpfs", "-o", "size=$self->{ramdisk}->{size}",
474 0 "tmpfs", $self->{ramdisk}->{tmpdir});
475  
476 0 INFO "Mounting ramdisk: @cmd";
477 0 my $rc = system( @cmd );
478
479 0 ▶ if($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
480 0 LOGWARN "Mount command '@cmd' failed: $?";
481 0 LOGWARN "Note that this only works on Linux and as root";
482 0 return;
483 }
484
485 0 $self->{ramdisk}->{mounted} = 1;
486
487 0 return 1;
488 }
489  
490 ###########################################
491 0 ▶sub ramdisk_unmount {
Subroutine ramdisk_unmount: not called CC=3 SCAR=24.8
Pod undocumented
492 ###########################################
493 0 my($self) = @_;
494  
495 0 ▶ return if !exists $self->{ramdisk}->{mounted};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"ramdisk"}{"mounted"} 0 0 0
496  
497 0 my @cmd = ($self->{umount}, $self->{ramdisk}->{tmpdir});
498  
499 0 INFO "Unmounting ramdisk: @cmd";
500  
501 0 my $rc = system( @cmd );
502
503 0 ▶ if($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
504 0 LOGWARN "Unmount command '@cmd' failed: $?";
505 0 return;
506 }
507  
508 0 delete $self->{ramdisk};
509 0 return 1;
510 }
511  
512 6 1;
513  
514 __END__
↑ Summary