| 1 | package Mojo::Tar; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Mojo::Base 'Mojo::EventEmitter', -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 Mojo::Collection; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::Tar::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 | 2 | ▶ | use constant TAR_BLOCK_SIZE => 512; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use constant TAR_BLOCK_PAD => "\0" x TAR_BLOCK_SIZE; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | has is_complete => 0; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | sub create ($self) { | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 1 | my ($current, $handle, $header, $idx, $read) = (undef, undef, '', -1, 0); | |||||||||||||||||||||||||||
| 19 | 14 | ▶ | my $cb = sub (@) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 20 | 14 | ▶ | unless ($current //= $self->files->[++$idx]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$current //= $self->files->[++$idx]
MC/DC100%2/2
$current //= $self->files->[++$idx]
$current
$self->files->[++$idx]
Truth table100%3/3
A //= B
A= $currentB= $self->files->[++$idx]
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | return '' if $self->is_complete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 1 | warn "[tar:create] EOF\n" if DEBUG; | |||||||||||||||||||||||||||
| 23 | 1 | ▶ | return $self->is_complete(1) && TAR_BLOCK_PAD . TAR_BLOCK_PAD; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->is_complete(1) && "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
MC/DC0%0/1
$self->is_complete(1) && "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
$self->is_complete(1)
Truth table50%1/2
A
A= $self->is_complete(1)
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 12 | ▶ | unless ($header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 4 | warn "[tar:create] Adding @{[$current->asset]}\n" if DEBUG; | |||||||||||||||||||||||||||
| 27 | 4 | $self->emit(adding => $current); | |||||||||||||||||||||||||||
| 28 | 4 | return $header = $current->to_header; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 8 | ▶ | $handle //= $current->asset->open('<'); | ||||||||||||||||||||||||||
|
Condition66%2/3
$handle //= $current->asset->open("<")
MC/DC0%0/2
$handle //= $current->asset->open("<")
$handle
$current->asset->open("<")
Truth table66%2/3
A //= B
A= $handleB= $current->asset->open("<")
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $r = $handle->sysread(my $buffer, 131072) // croak "Unable to read @{[$current->asset]}: $!"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$handle->sysread(my $buffer, 131072) // croak("Unable to read @{[$current->asset];}: $!")
MC/DC0%0/1
$handle->sysread(my $buffer, 131072) // croak("Unable to read @{[$current->asset];}: $!")
$handle->sysread(my $buffer, 131072)
Truth table50%1/2
A
A= $handle->sysread(my $buffer, 131072)
| |||||||||||||||||||||||||||||
| 33 | 8 | $read += $r; | |||||||||||||||||||||||||||
| 34 | 8 | ▶ | return $buffer if length $buffer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | warn "[tar:create] Added @{[$current->asset]} ($read)\n" if DEBUG; | |||||||||||||||||||||||||||
| 37 | 4 | $self->emit(added => $current); | |||||||||||||||||||||||||||
| 38 | 4 | ($current, $handle, $header) = (undef, undef, ''); | |||||||||||||||||||||||||||
| 39 | 4 | return __SUB__->(); | |||||||||||||||||||||||||||
| 40 | 1 | }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | return $self->is_complete(0) && $cb; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->is_complete(0) && $cb
MC/DC0%0/2
$self->is_complete(0) && $cb
$self->is_complete(0)
$cb
Truth table33%1/3
A && B
A= $self->is_complete(0)B= $cb
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 21 | ▶ | sub extract ($self, $bytes) { | ||||||||||||||||||||||||||
|
Subroutine
extract: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 21 | $self->{buf} .= $bytes; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 21 | while (1) { | |||||||||||||||||||||||||||
| 49 | 38 | ▶ | last if length($self->{buf}) < 512; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 19 | my $block = substr $self->{buf}, 0, 512, ''; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 19 | ▶ | if (my $file = $self->{current}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 6 | $file->add_block($block); | |||||||||||||||||||||||||||
| 54 | 6 | ▶ | $self->emit(extracted => delete $self->{current}) if $file->is_complete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | elsif ($block eq TAR_BLOCK_PAD) { | ||||||||||||||||||||||||||||
| 57 | 2 | warn "[tar:extract] Got tar pad block\n" if DEBUG; | |||||||||||||||||||||||||||
| 58 | 2 | $self->{buf} = ''; | |||||||||||||||||||||||||||
| 59 | 2 | return $self->is_complete(1); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | 11 | my $file = Mojo::Tar::File->new->from_header($block); | |||||||||||||||||||||||||||
| 63 | 11 | push @{$self->files}, $file; | |||||||||||||||||||||||||||
| 64 | 11 | $self->is_complete(0)->emit(extracting => $file); | |||||||||||||||||||||||||||
| 65 | 11 | ▶ | $file->size ? ($self->{current} = $file) : $self->emit(extracted => $file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 19 | return $self; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 24 | ▶ | sub files ($self, $files = undef) { | ||||||||||||||||||||||||||
|
Subroutine
files: called CC=7 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 24 | ▶ | return $self->{files} //= Mojo::Collection->new unless $files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"files"} //= "Mojo::Collection"->new
MC/DC0%0/2
$self->{"files"} //= "Mojo::Collection"->new
$self->{"files"}
"Mojo::Collection"->new
Truth table66%2/3
A //= B
A= $self->{"files"}B= "Mojo::Collection"->new
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | my $c = $self->{files} = Mojo::Collection->new; | |||||||||||||||||||||||||||
| 76 | 1 | for my $file (@$files) { | |||||||||||||||||||||||||||
| 77 | 4 | ▶ | if (blessed($file) && $file->isa('Mojo::Tar::File')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($file) and $file->isa("Mojo::Tar::File")
MC/DC0%0/2
builtin::blessed($file) and $file->isa("Mojo::Tar::File")
builtin::blessed($file)
$file->isa("Mojo::Tar::File")
Truth table33%1/3
A and B
A= builtin::blessed($file)B= $file->isa("Mojo::Tar::File")
| |||||||||||||||||||||||||||||
| 78 | 0 | push @$c, $file; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||
| 81 | 4 | push @$c, Mojo::Tar::File->new->asset(Mojo::File->new("$file"))->path("$file"); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | return $self; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | sub looks_like_tar ($self, $bytes) { | ||||||||||||||||||||||||||
|
Subroutine
looks_like_tar: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 4 | state $padding = "\0" x TAR_USTAR_PADDING_LEN; | |||||||||||||||||||||||||||
| 90 | return | ||||||||||||||||||||||||||||
| 91 | 4 | ▶ | length($bytes) < TAR_BLOCK_SIZE ? 0 | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 92 | : substr($bytes, TAR_USTAR_PADDING_POS, TAR_USTAR_PADDING_LEN) ne $padding ? 0 | ||||||||||||||||||||||||||||
| 93 | : Mojo::Tar::File->new->from_header($bytes)->checksum ? 1 | ||||||||||||||||||||||||||||
| 94 | : 0; | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | sub new ($class, @attrs) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 3 | my $self = $class->SUPER::new(@attrs); | |||||||||||||||||||||||||||
| 99 | 3 | ▶ | $self->files($self->{files}) if $self->{files}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 3 | return $self; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =encoding utf8 | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 NAME | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Mojo::Tar - Stream your (ustar) tar files | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head2 Create | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | use Mojo::Tar | ||||||||||||||||||||||||||||
| 116 | my $tar = Mojo::Tar->new; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | $tar->on(adding => sub ($self, $file) { | ||||||||||||||||||||||||||||
| 119 | warn sprintf qq(Adding "%s" %sb to archive\n), $file->path, $file->size; | ||||||||||||||||||||||||||||
| 120 | }); | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | my $cb = $tar->files(['a.baz', 'b.foo'])->create; | ||||||||||||||||||||||||||||
| 123 | open my $fh, '>', '/path/to/my-archive.tar'; | ||||||||||||||||||||||||||||
| 124 | while (length(my $chunk = $cb->())) { | ||||||||||||||||||||||||||||
| 125 | print {$fh} $chunk; | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 Extract | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | use Mojo::Tar | ||||||||||||||||||||||||||||
| 131 | my $tar = Mojo::Tar->new; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | $tar->on(extracted => sub ($self, $file) { | ||||||||||||||||||||||||||||
| 134 | warn sprintf qq(Extracted "%s" %sb\n), $file->path, $file->size; | ||||||||||||||||||||||||||||
| 135 | }); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | open my $fh, '<', '/path/to/my-archive.tar'; | ||||||||||||||||||||||||||||
| 138 | while (1) { | ||||||||||||||||||||||||||||
| 139 | sysread $fh, my ($chunk), 512 or die $!; | ||||||||||||||||||||||||||||
| 140 | $tar->extract($chunk); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | L<Mojo::Tar> can create and extract L<ustar|http://www.gnu.org/software/tar/manual/tar.html> | ||||||||||||||||||||||||||||
| 146 | tar files as a stream. This can be useful if for example your webserver is | ||||||||||||||||||||||||||||
| 147 | receiving a big tar file and you don't want to exhaust the memory while | ||||||||||||||||||||||||||||
| 148 | reading it. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | The L<pax|http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html> | ||||||||||||||||||||||||||||
| 151 | tar format is not planned, but a pull request is more than welcome! | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Note that this module is currently EXPERIMENTAL, but the API will only change | ||||||||||||||||||||||||||||
| 154 | if major design issues is discovered. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 added | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | $tar->on(added => sub ($tar, $file) { ... }); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Emitted after the callback from L</create> has returned all the content of the C<$file>. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 adding | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | $tar->on(adding => sub ($tar, $file) { ... }); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Emitted right before the callback from L</create> returns the tar header for the | ||||||||||||||||||||||||||||
| 169 | C<$file>. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head2 extracted | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | $tar->on(extracted => sub ($tar, $file) { ... }); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Emitted when L</extract> has read the complete content of the file. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head2 extracting | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | $tar->on(extracting => sub ($tar, $file) { ... }); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Emitted when L</extract> has read the tar header for a L<Mojo::Tar::File>. This | ||||||||||||||||||||||||||||
| 182 | event can be used to set the L<Mojo::Tar::File/asset> to something else than a | ||||||||||||||||||||||||||||
| 183 | temp file. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 files | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | $tar = $tar->files(Mojo::Collection->new('a.file', ...)]); | ||||||||||||||||||||||||||||
| 190 | $tar = $tar->files([Mojo::File->new]); | ||||||||||||||||||||||||||||
| 191 | $tar = $tar->files([Mojo::Tar::File->new, ...]); | ||||||||||||||||||||||||||||
| 192 | $collection = $tar->files; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | This attribute holds a L<Mojo::Collection> of L<Mojo::Tar::File> objects which | ||||||||||||||||||||||||||||
| 195 | is used by either L</create> or L</extract>. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Setting this attribute will make sure each item is a L<Mojo::Tar::File> object, | ||||||||||||||||||||||||||||
| 198 | even if the original list contained a L<Mojo::File> or a plain string. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head2 is_complete | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | $bool = $tar->is_complete; | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | True when the callback from L</create> has returned the whole tar-file or when | ||||||||||||||||||||||||||||
| 205 | L</extract> thinks the whole tar file has been read. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Note that because of this, L</create> and L</extract> should not be called on | ||||||||||||||||||||||||||||
| 208 | the same object. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head1 METHODS | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head2 create | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | $cb = $tar->create; | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | This method will take L</files> and return a callback that will return a chunk | ||||||||||||||||||||||||||||
| 217 | of the tar file each time it is called, and an empty string when all files has | ||||||||||||||||||||||||||||
| 218 | been processed. Example: | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | while (length(my $chunk = $cb->())) { | ||||||||||||||||||||||||||||
| 221 | warn sprintf qq(Got %sb of tar data\n), length $chunk; | ||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | The L</adding> and L</added> events will be emitted for each file. In addition | ||||||||||||||||||||||||||||
| 225 | L</is_complete> will be set when all the L</files> has been processed. | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head2 extract | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | $tar = $tar->extract($bytes); | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Used to parse C<$bytes> and turn the information into L<Mojo::Tar::File> | ||||||||||||||||||||||||||||
| 232 | objects which are emitted as L</extracting> and L</extracted> events. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head2 looks_like_tar | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | $bool = $tar->looks_like_tar($bytes); | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Returns true if L<Mojo::Tar> thinks C<$bytes> looks like the beginning of a | ||||||||||||||||||||||||||||
| 239 | tar stream. Currently this checks if C<$bytes> is at least 512 bytes long and | ||||||||||||||||||||||||||||
| 240 | the checksum value in the tar header is correct. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head2 new | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | $tar = Mojo::Tar->new(\%attrs); | ||||||||||||||||||||||||||||
| 245 | $tar = Mojo::Tar->new(%attrs); | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Used to create a new L<Mojo::Tar> object. L</files> will be normalized. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Jan Henning Thorsen | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Copyright (C) Jan Henning Thorsen | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 258 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | L<Archive::Tar> | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =cut | ||||||||||||||||||||||||||||