| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Footprintless::Extract; | ||||||||||||||||||||||||||||
| 5 | 3 | $Footprintless::Extract::VERSION = '1.29'; | |||||||||||||||||||||||||||
| 6 | # ABSTRACT: Extracts data from archives | ||||||||||||||||||||||||||||
| 7 | # PODNAME: Footprintless::Extract | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Log::Any; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | my $logger = Log::Any->get_logger(); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 8 | return bless( {}, shift )->_init(@_); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | sub extract { | ||||||||||||||||||||||||||
|
Subroutine
extract: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 8 | my ( $self, %options ) = @_; | |||||||||||||||||||||||||||
| 23 | 8 | ▶ | my $to = $options{to} || getcwd(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$options{'to'} || getcwd()
MC/DC0%0/2
$options{'to'} || getcwd()
$options{'to'}
getcwd()
Truth table33%1/3
A || B
A= $options{'to'}B= getcwd()
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | my $current_dir = getcwd(); | |||||||||||||||||||||||||||
| 26 | 8 | eval { | |||||||||||||||||||||||||||
| 27 | 8 | ▶ | croak("$to is not a directory") if ( -e $to && !-d $to ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $to and not -d $to
MC/DC0%0/2
-e $to and not -d $to
-e $to
not -d $to
Truth table66%2/3
A and not(B)
A= -e $toB= not -d $to
| |||||||||||||||||||||||||||||
| 28 | 8 | make_path($to); | |||||||||||||||||||||||||||
| 29 | 8 | chdir($to); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 8 | ▶ | if ( $self->{type} eq 'zip' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 6 | _unzip( $self->{archive}, $to ); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 8 | ▶ | if ( $self->{type} eq 'tar' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 2 | _untar( $self->{archive}, $to ); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||||||||
| 38 | 8 | my $error = $@; | |||||||||||||||||||||||||||
| 39 | 8 | chdir($current_dir); | |||||||||||||||||||||||||||
| 40 | 8 | ▶ | die($error) if ($error); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 8 | return 1; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 8 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 46 | 8 | my ( $self, %options ) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 8 | ▶ | croak('archive required') unless ( $options{archive} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 8 | $self->{archive} = $options{archive}; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 8 | ▶ | my $dot_extension = $options{type} ? ".$options{type}" : $self->{archive}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 8 | ▶ | if ( $dot_extension =~ /\.zip|\.war|\.jar|\.ear|\.twbx$/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 6 | $self->{type} = 'zip'; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | elsif ( $dot_extension =~ /\.tar|\.tar\.gz|\.tgz$/ ) { | ||||||||||||||||||||||||||||
| 56 | 2 | $self->{type} = 'tar'; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 0 | croak("unknown archive type"); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 8 | return $self; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | ▶ | sub _untar { | ||||||||||||||||||||||||||
|
Subroutine
_untar: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 2 | my ( $archive, $to ) = @_; | |||||||||||||||||||||||||||
| 67 | 2 | $logger->tracef( 'untar [%s] to [%s]', $archive, $to ); | |||||||||||||||||||||||||||
| 68 | 2 | require Archive::Tar; | |||||||||||||||||||||||||||
| 69 | 2 | Archive::Tar->new($archive)->extract(); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 6 | ▶ | sub _unzip { | ||||||||||||||||||||||||||
|
Subroutine
_unzip: called CC=12 SCAR=25.2
| |||||||||||||||||||||||||||||
| 73 | 6 | my ( $archive, $to ) = @_; | |||||||||||||||||||||||||||
| 74 | 6 | $logger->tracef( 'unzip [%s] to [%s]', $archive, $to ); | |||||||||||||||||||||||||||
| 75 | 6 | require IO::Uncompress::Unzip; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | my $unzip = IO::Uncompress::Unzip->new($archive) | ||||||||||||||||||||||||||
|
Condition50%1/2
'IO::Uncompress::Unzip'->new($archive) || croak("unable to open ${archive}: $IO::Uncompress::Unzip::UnzipError")
MC/DC0%0/1
'IO::Uncompress::Unzip'->new($archive) || croak("unable to open ${archive}: $IO::Uncompress::Unzip::UnzipError")
'IO::Uncompress::Unzip'->new($archive)
Truth table50%1/2
A
A= 'IO::Uncompress::Unzip'->new($archive)
| |||||||||||||||||||||||||||||
| 78 | || croak("unable to open $archive: $IO::Uncompress::Unzip::UnzipError"); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 6 | my $status; | |||||||||||||||||||||||||||
| 81 | 6 | eval { | |||||||||||||||||||||||||||
| 82 | 6 | ▶ | for ( $status = 1; $status > 0; $status = $unzip->nextStream() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 54 | my $header = $unzip->getHeaderInfo(); | |||||||||||||||||||||||||||
| 84 | 54 | my ( undef, $path, $name ) = File::Spec->splitpath( $header->{Name} ); | |||||||||||||||||||||||||||
| 85 | 54 | my $dest_dir = File::Spec->catdir( $to, $path ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 54 | ▶ | unless ( -d $dest_dir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 28 | ▶ | make_path($dest_dir) || croak("unable to create dir $dest_dir: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 54 | ▶ | unless ($name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 28 | ▶ | last if ( $status < 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 28 | next; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 26 | my $dest_file = File::Spec->catfile( $dest_dir, $name ); | |||||||||||||||||||||||||||
| 97 | 26 | my $buffer; | |||||||||||||||||||||||||||
| 98 | 26 | ▶ | my $file = IO::File->new( $dest_file, "w" ) | ||||||||||||||||||||||||||
|
Condition50%1/2
'IO::File'->new($dest_file, 'w') || croak("unable to create file ${dest_file}: $!")
MC/DC0%0/1
'IO::File'->new($dest_file, 'w') || croak("unable to create file ${dest_file}: $!")
'IO::File'->new($dest_file, 'w')
Truth table50%1/2
A
A= 'IO::File'->new($dest_file, 'w')
| |||||||||||||||||||||||||||||
| 99 | || croak("unable to create file $dest_file: $!"); | ||||||||||||||||||||||||||||
| 100 | 26 | ▶ | while ( ( $status = $unzip->read($buffer) ) > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 26 | $file->write($buffer); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 26 | $file->close(); | |||||||||||||||||||||||||||
| 104 | 26 | my $stored_time = $header->{Time}; | |||||||||||||||||||||||||||
| 105 | 26 | ▶ | utime( $stored_time, $stored_time, $dest_file ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | || croak("couldn't set utime on $dest_file: $!"); | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 6 | ▶ | croak("error processing $archive: $!") if ( $status < 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||
| 110 | 6 | my $error = $@; | |||||||||||||||||||||||||||
| 111 | 6 | $unzip->close(); | |||||||||||||||||||||||||||
| 112 | 6 | ▶ | die($error) if ($error); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 6 | return; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 3 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||