| 1 | package Archive::Libarchive::ArchiveRead; | ||
| 2 | |||
| 3 | 13 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 13 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 13 | ▶ | use 5.020; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 13 | ▶ | use Archive::Libarchive::Lib; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 13 | ▶ | use FFI::Platypus::Buffer qw( scalar_to_buffer scalar_to_pointer grow set_used_length window ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 13 | ▶ | use FFI::Platypus::Memory qw( strdup free ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 13 | ▶ | use Ref::Util qw( is_plain_scalarref is_plain_coderef is_blessed_ref is_plain_arrayref ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 13 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 13 | ▶ | use Scalar::Util qw( refaddr ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 13 | ▶ | use experimental qw( signatures ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 13 | ▶ | use parent qw( Archive::Libarchive::Archive ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | use constant; | ||
| 15 | |||
| 16 | # ABSTRACT: Libarchive read archive class | ||
| 17 | our $VERSION = '0.10'; # VERSION | ||
| 18 | |||
| 19 | my $ffi = Archive::Libarchive::Lib->ffi; | ||
| 20 | constant->import(_opaque_size => $ffi->sizeof('opaque')); | ||
| 21 | my %keep; | ||
| 22 | |||
| 23 | |||
| 24 | $ffi->mangler(sub ($name) { "archive_read_$name" }); | ||
| 25 | |||
| 26 | $ffi->attach( new => [] => 'opaque' => sub { | ||
| 27 | my($xsub, $class) = @_; | ||
| 28 | my $ptr = $xsub->(); | ||
| 29 | bless { ptr => $ptr }, $class; | ||
| 30 | }); | ||
| 31 | |||
| 32 | $ffi->attach( [ free => 'DESTROY' ] => ['archive_read'] => 'int' => sub { | ||
| 33 | my($xsub, $self) = @_; | ||
| 34 | free delete $self->{passphrase} if defined $self->{passphrase}; | ||
| 35 | return if $self->{cb} # inside a callback, we don't own the archive pointer | ||
| 36 | || ${^GLOBAL_PHASE} eq 'DESTRUCT'; # during global shutdown the xsub might go away | ||
| 37 | my $ret = $xsub->($self); | ||
| 38 | delete $keep{refaddr $self}; | ||
| 39 | warn "destroying archive pointer did not return ARCHIVE_OK" unless $ret == 0; | ||
| 40 | }); | ||
| 41 | |||
| 42 | |||
| 43 | my %set_callback = map { | ||
| 44 | ($_ => $ffi->function( "set_${_}_callback" => ['archive_read',"archive_${_}_callback"] => 'int' )->sub_ref) | ||
| 45 | } qw( open close read seek skip ); | ||
| 46 | |||
| 47 | $ffi->attach( [ set_callback_data => '_set_callback_data' ] => ['archive_read','opaque'] => 'int' ); | ||
| 48 | |||
| 49 | $ffi->attach( [ open1 => 'open' ] => [ 'archive_read'] => 'int' => sub { | ||
| 50 | my($xsub, $self, %callbacks) = @_; | ||
| 51 | |||
| 52 | Carp::croak("The read callback is required") unless $callbacks{read}; | ||
| 53 | |||
| 54 | foreach my $name (keys %set_callback) | ||
| 55 | { | ||
| 56 | my $set = $set_callback{$name}; | ||
| 57 | my $sub = delete $callbacks{$name}; | ||
| 58 | |||
| 59 | unless(defined $sub) | ||
| 60 | { | ||
| 61 | $set->($self, undef); | ||
| 62 | next; | ||
| 63 | } | ||
| 64 | |||
| 65 | Carp::croak("Callback for $name is not a code reference") unless is_plain_coderef $sub; | ||
| 66 | |||
| 67 | my $closure; | ||
| 68 | |||
| 69 | if($name eq 'read') | ||
| 70 | { | ||
| 71 | $closure = FFI::Platypus->closure(sub ($r, $, $ptrptr) { | ||
| 72 | $r = bless { ptr => $r, cb => 1 }, __PACKAGE__; | ||
| 73 | $self->{read_buffer} = undef; | ||
| 74 | my $size = $sub->($r, \$self->{read_buffer}); | ||
| 75 | my $ptr = defined $self->{read_buffer} ? scalar_to_pointer($self->{read_buffer}) : undef; | ||
| 76 | _memcpy($ptrptr, [$ptr], _opaque_size()); | ||
| 77 | return $size; | ||
| 78 | }); | ||
| 79 | } | ||
| 80 | else | ||
| 81 | { | ||
| 82 | $closure = FFI::Platypus->closure(sub ($r, $, @therest) { | ||
| 83 | $r = bless { ptr => $r, cb => 1 }, __PACKAGE__; | ||
| 84 | $sub->($r, @therest); | ||
| 85 | }); | ||
| 86 | } | ||
| 87 | |||
| 88 | push @{ $keep{refaddr $self} }, $closure; | ||
| 89 | |||
| 90 | $set->($self, $closure); | ||
| 91 | } | ||
| 92 | |||
| 93 | Carp::croak("No such read callbacks: @{[ sort keys %callbacks ]}") if %callbacks; | ||
| 94 | |||
| 95 | _set_callback_data($self, undef); | ||
| 96 | |||
| 97 | $xsub->($self); | ||
| 98 | }); | ||
| 99 | |||
| 100 | |||
| 101 | $ffi->attach( open_memory => ['archive_read','opaque','size_t'] => 'int' => sub { | ||
| 102 | my($xsub, $self, $ref) = @_; | ||
| 103 | Carp::croak("buffer must be a scalar reference") | ||
| 104 | unless defined $ref && is_plain_scalarref $ref; | ||
| 105 | push @{ $keep{refaddr $self} }, \($$ref); | ||
| 106 | my($ptr, $size) = scalar_to_buffer $$ref; | ||
| 107 | $xsub->($self, $ptr, $size); | ||
| 108 | }); | ||
| 109 | |||
| 110 | |||
| 111 | $ffi->attach( open_FILE => ['archive_read', 'opaque'] => 'int' => sub { | ||
| 112 | my($xsub, $self, $fp) = @_; | ||
| 113 | $fp = $$fp if is_blessed_ref $fp && $fp->isa('FFI::C::File'); | ||
| 114 | $xsub->($self, $fp); | ||
| 115 | }); | ||
| 116 | |||
| 117 | |||
| 118 | sub open_perlfile ($self, $fh) | ||
| 119 | { | ||
| 120 | $self->open( | ||
| 121 | read => sub ($r, $ref) { | ||
| 122 | return sysread $fh, $$ref, 512; | ||
| 123 | }, | ||
| 124 | close => sub ($r) { | ||
| 125 | close $fh; | ||
| 126 | }, | ||
| 127 | ); | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | $ffi->attach( open_filenames => ['archive_read', 'string[]', 'size_t'] => 'int' => sub { | ||
| 132 | my($xsub, $self, $filenames, $size) = @_; | ||
| 133 | Carp::croak("Filenames must be provided as an array reference") | ||
| 134 | unless defined $filenames && is_plain_arrayref $filenames; | ||
| 135 | $xsub->($self, defined $filenames->[-1] ? [@$filenames, undef] : $filenames, $size); | ||
| 136 | }); | ||
| 137 | |||
| 138 | |||
| 139 | $ffi->attach( [ next_header2 => 'next_header' ] => ['archive_read','archive_entry'] => 'int' => sub { | ||
| 140 | my($xsub, $self, $entry) = @_; | ||
| 141 | $xsub->($self, $entry); | ||
| 142 | }); | ||
| 143 | |||
| 144 | |||
| 145 | $ffi->attach( [data => 'read_data'] => ['archive_read', 'opaque', 'size_t'] => 'ssize_t' => sub { | ||
| 146 | my($xsub, $self, $ref, $size) = @_; | ||
| 147 | $size ||= 512; | ||
| 148 | |||
| 149 | grow $$ref, $size, { clear => 0 }; | ||
| 150 | my $rsize = $xsub->($self, (scalar_to_buffer $$ref)); | ||
| 151 | set_used_length $$ref, $rsize; | ||
| 152 | |||
| 153 | return $rsize; | ||
| 154 | }); | ||
| 155 | |||
| 156 | |||
| 157 | $ffi->attach( [data_block => 'read_data_block' ] => ['archive_read', 'opaque*', 'size_t*', 'sint64*'] => 'int' => sub { | ||
| 158 | my $xsub = shift; | ||
| 159 | my($ptr, $size); | ||
| 160 | my $ret = $xsub->($_[0], \$ptr, \$size, $_[2]); | ||
| 161 | window ${$_[1]}, $ptr, $size if defined $ptr; | ||
| 162 | return $ret; | ||
| 163 | }); | ||
| 164 | |||
| 165 | |||
| 166 | $ffi->attach( append_filter => ['archive_read', 'archive_filter_t'] => 'int' ); | ||
| 167 | $ffi->attach( set_format => ['archive_read', 'archive_format_t'] => 'int' ); | ||
| 168 | |||
| 169 | |||
| 170 | $ffi->attach( set_passphrase_callback => ['archive_read', 'opaque', 'archive_passphrase_callback'] => 'int' => sub { | ||
| 171 | my($xsub, $self, $sub) = @_; | ||
| 172 | |||
| 173 | my $closure = FFI::Platypus->closure(sub ($r, $) { | ||
| 174 | $r = bless { ptr => $r, cb => 1 }, __PACKAGE__; | ||
| 175 | my $passphrase = $sub->($r); | ||
| 176 | $passphrase = '' unless defined $passphrase; | ||
| 177 | my $ptr = strdup $passphrase; | ||
| 178 | free delete $self->{passphrase} if defined $self->{passphrase}; | ||
| 179 | return $self->{passphrase} = $ptr; | ||
| 180 | }); | ||
| 181 | |||
| 182 | push @{ $keep{refaddr $self} }, $closure; | ||
| 183 | |||
| 184 | $xsub->($self, undef, $closure); | ||
| 185 | |||
| 186 | }); | ||
| 187 | |||
| 188 | require Archive::Libarchive::Lib::ArchiveRead unless $Archive::Libarchive::no_gen; | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ | ||