| 1 | package App::digestarchive; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use v5.8.0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Archive::Tar; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Compress::Zlib; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use IO::Uncompress::Bunzip2 qw($Bunzip2Error); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Digest; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Class::Accessor "antlers"; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 6 | has digest_type => (is => "rw", type => "Str"); | |||||||||||||||||||||||||||||||
| 14 | 6 | has archiver => (is => "rw", type => "Str"); | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 6 | our $VERSION = '0.044'; | |||||||||||||||||||||||||||||||
| 17 | 6 | our $DIGEST_TYPE = "MD5"; | |||||||||||||||||||||||||||||||
| 18 | 6 | our $NONE_DIGEST_MESSAGE = "** can not get digest **"; | |||||||||||||||||||||||||||||||
| 19 | 6 | our @ADD_ENTRY_METHODS = qw(digest link_or_real_name); | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 5 | my($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 24 | bless { | ||||||||||||||||||||||||||||||||
| 25 | 5 | ▶ | digest_type => (exists $args{digest_type} && defined $args{digest_type}) ? $args{digest_type} : $DIGEST_TYPE, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"digest_type"} and defined $args{"digest_type"}
MC/DC0%0/2
exists $args{"digest_type"} and defined $args{"digest_type"}
exists $args{"digest_type"}
defined $args{"digest_type"}
Truth table33%1/3
A and B
A= exists $args{"digest_type"}B= defined $args{"digest_type"}
| |||||||||||||||||||||||||||||||||
| 26 | # only Archive::Tar | ||||||||||||||||||||||||||||||||
| 27 | archiver => Archive::Tar->new | ||||||||||||||||||||||||||||||||
| 28 | }, $class; | ||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 13 | ▶ | sub read { | ||||||||||||||||||||||||||||||
|
Subroutine
read: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 13 | my($self, $file_or_fh_or_buffer) = @_; | |||||||||||||||||||||||||||||||
| 35 | 13 | my $buffer = $self->slurp($file_or_fh_or_buffer); | |||||||||||||||||||||||||||||||
| 36 | 13 | my $fh; | |||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 13 | my $magic = $self->get_magic($buffer); | |||||||||||||||||||||||||||||||
| 39 | 13 | ▶ | if ($magic =~ Archive::Tar::GZIP_MAGIC_NUM) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | my $dest = Compress::Zlib::memGunzip($buffer) or die "Cannot uncompress: $gzerrno\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 3 | $fh = $self->scalar2fh($dest); | |||||||||||||||||||||||||||||||
| 42 | } elsif ($magic =~ Archive::Tar::BZIP_MAGIC_NUM) { | ||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | $fh = IO::Uncompress::Bunzip2->new(\$buffer) or die "Cannot open bunzip2: $Bunzip2Error\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||||||
| 45 | 7 | $fh = $self->scalar2fh($buffer); | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | 13 | $self->archiver->read($fh); | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 3 | ▶ | sub all { | ||||||||||||||||||||||||||||||
|
Subroutine
all: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 3 | my($self, $filter_cb) = @_; | |||||||||||||||||||||||||||||||
| 53 | 3 | my @all; | |||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | while (my $f = $self->next) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 15 | ▶ | if (defined $filter_cb && ref($filter_cb) && "CODE") { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
defined $filter_cb and ref $filter_cb and "CODE"
defined $filter_cb and ref $filter_cb
MC/DC50%1/2
defined $filter_cb and ref $filter_cb and "CODE"
defined $filter_cb
ref $filter_cb
Truth table66%2/3
A and B
A= defined $filter_cbB= ref $filter_cb
| |||||||||||||||||||||||||||||||||
| 57 | 5 | ▶ | if ($filter_cb->($f)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 3 | push @all, $f; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||||||
| 61 | 10 | push @all, $f; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | 3 | return \@all; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 19 | ▶ | sub next { | ||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||
| 70 | 19 | my $f = shift @{$self->archiver->_data}; | |||||||||||||||||||||||||||||||
| 71 | 19 | ▶ | return if !defined $f; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||
| 74 | 6 | ▶ | no strict "refs"; ## no critic | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 75 | 6 | ▶ | no warnings "redefine"; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 76 | 16 | my $pkg = ref $f; | |||||||||||||||||||||||||||||||
| 77 | 16 | foreach my $method (@ADD_ENTRY_METHODS) { | |||||||||||||||||||||||||||||||
| 78 | 32 | ▶ | *{"$pkg\::$method"} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 79 | 38 | my $self = shift; | |||||||||||||||||||||||||||||||
| 80 | 38 | ▶ | if (scalar(@_) > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 32 | $self->{$method} = $_[0]; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | 38 | return $self->{$method}; | |||||||||||||||||||||||||||||||
| 84 | 32 | }; | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | # set digest | ||||||||||||||||||||||||||||||||
| 89 | 16 | ▶ | $f->digest(($f->type == Archive::Tar::FILE or $f->type == Archive::Tar::HARDLINK) ? $self->digest($f->data) : $NONE_DIGEST_MESSAGE); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$f->type == 0 or $f->type == 1
MC/DC50%1/2
$f->type == 0 or $f->type == 1
$f->type == 0
$f->type == 1
Truth table66%2/3
A or B
A= $f->type == 0B= $f->type == 1
| |||||||||||||||||||||||||||||||||
| 90 | # set link_or_real_name | ||||||||||||||||||||||||||||||||
| 91 | 16 | ▶ | $f->link_or_real_name(($f->type == Archive::Tar::SYMLINK) ? sprintf "%s -> %s", $f->name, $f->linkname : $f->name); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 16 | return $f; | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 9 | ▶ | sub digest { | ||||||||||||||||||||||||||||||
|
Subroutine
digest: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 9 | my($self, $data) = @_; | |||||||||||||||||||||||||||||||
| 100 | 9 | ▶ | return if !defined $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | 9 | return Digest->new($self->digest_type)->add($data)->hexdigest; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 10 | ▶ | sub scalar2fh { | ||||||||||||||||||||||||||||||
|
Subroutine
scalar2fh: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 10 | my($self, $buffer) = @_; | |||||||||||||||||||||||||||||||
| 107 | 10 | ▶ | open my $fh, "<:scalar", \$buffer or die "change readable filehandle convert failed\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | 10 | return $fh; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 13 | ▶ | sub slurp { | ||||||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 13 | my($self, $file_or_fh_or_buffer) = @_; | |||||||||||||||||||||||||||||||
| 114 | 13 | my $fh; | |||||||||||||||||||||||||||||||
| 115 | my $buffer; | ||||||||||||||||||||||||||||||||
| 116 | 13 | ▶ | if (ref($file_or_fh_or_buffer) eq "GLOB") { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 117 | 3 | $fh = $file_or_fh_or_buffer; | |||||||||||||||||||||||||||||||
| 118 | } elsif (-f $file_or_fh_or_buffer) { | ||||||||||||||||||||||||||||||||
| 119 | 7 | ▶ | open $fh, "<", $file_or_fh_or_buffer or die "can not open file:$file_or_fh_or_buffer. $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 13 | ▶ | if (defined $fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 123 | 10 | $buffer = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||
| 124 | 10 | close $fh; | |||||||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||||||
| 126 | 3 | $buffer = $file_or_fh_or_buffer; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | 13 | return $buffer; | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 13 | ▶ | sub get_magic { | ||||||||||||||||||||||||||||||
|
Subroutine
get_magic: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 13 | my($self, $data) = @_; | |||||||||||||||||||||||||||||||
| 135 | 13 | ▶ | if (!defined $data) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 0 | die "invalid data\n"; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | 13 | return substr $data, 0, 4; | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 6 | 1; | |||||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||||||