| 1 | package Archive::Libarchive::ArchiveWrite; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use 5.020; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Archive::Libarchive::Lib; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Ref::Util qw( is_plain_coderef is_blessed_ref ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use FFI::Platypus::Buffer qw( window scalar_to_buffer ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use FFI::Platypus::Memory qw( strdup free ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use Scalar::Util qw( refaddr ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use experimental qw( signatures ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use parent qw( Archive::Libarchive::Archive ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | # ABSTRACT: Libarchive write archive class | ||
| 16 | our $VERSION = '0.10'; # VERSION | ||
| 17 | |||
| 18 | my $ffi = Archive::Libarchive::Lib->ffi; | ||
| 19 | my %keep; | ||
| 20 | |||
| 21 | |||
| 22 | $ffi->mangler(sub ($name) { "archive_write_$name" }); | ||
| 23 | |||
| 24 | $ffi->attach( new => [] => 'opaque' => sub { | ||
| 25 | my($xsub, $class) = @_; | ||
| 26 | my $ptr = $xsub->(); | ||
| 27 | bless { ptr => $ptr }, $class; | ||
| 28 | }); | ||
| 29 | |||
| 30 | $ffi->attach( [ free => 'DESTROY' ] => ['archive_write'] => 'int' => sub { | ||
| 31 | my($xsub, $self) = @_; | ||
| 32 | free delete $self->{passphrase} if defined $self->{passphrase}; | ||
| 33 | return if $self->{cb} # inside a callback, we don't own the archive pointer | ||
| 34 | || ${^GLOBAL_PHASE} eq 'DESTRUCT'; # during global shutdown, the xsub might go away | ||
| 35 | my $ret = $xsub->($self); | ||
| 36 | delete $keep{refaddr $self}; | ||
| 37 | warn "destroying archive pointer did not return ARCHIVE_OK" unless $ret == 0; | ||
| 38 | }); | ||
| 39 | |||
| 40 | |||
| 41 | $ffi->attach( open => ['archive_write', 'opaque', 'archive_open_callback', 'archive_write_callback', 'archive_close_callback'] => 'int' => sub { | ||
| 42 | my($xsub, $self, %cb) = @_; | ||
| 43 | |||
| 44 | foreach my $name (qw( open write close )) | ||
| 45 | { | ||
| 46 | if(defined $cb{$name} && !is_plain_coderef $cb{$name}) | ||
| 47 | { | ||
| 48 | Carp::croak("The $name callback is not a subref"); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | my $opener = delete $cb{open}; | ||
| 53 | my $writer = delete $cb{write}; | ||
| 54 | my $closer = delete $cb{close}; | ||
| 55 | |||
| 56 | Carp::croak("Write callback is required") unless $writer; | ||
| 57 | Carp::croak("No such write callbacks: @{[ sort keys %cb ]}") if %cb; | ||
| 58 | |||
| 59 | if($opener) | ||
| 60 | { | ||
| 61 | my $orig = $opener; | ||
| 62 | $opener = FFI::Platypus->closure(sub ($w, $) { | ||
| 63 | $w = bless { ptr => $w, cb => 1 }, __PACKAGE__; | ||
| 64 | $orig->($w); | ||
| 65 | }); | ||
| 66 | push @{ $keep{refaddr $self} }, $opener; | ||
| 67 | } | ||
| 68 | |||
| 69 | if($writer) | ||
| 70 | { | ||
| 71 | my $orig = $writer; | ||
| 72 | $writer = FFI::Platypus->closure(sub ($w, $, $ptr, $size) { | ||
| 73 | $w = bless { ptr => $w, cb => 1 }, __PACKAGE__; | ||
| 74 | my $buffer; | ||
| 75 | window $buffer, $ptr, $size; | ||
| 76 | $orig->($w, \$buffer); | ||
| 77 | }); | ||
| 78 | push @{ $keep{refaddr $self} }, $writer; | ||
| 79 | } | ||
| 80 | |||
| 81 | if($closer) | ||
| 82 | { | ||
| 83 | my $orig = $closer; | ||
| 84 | $closer = FFI::Platypus->closure(sub ($w, $) { | ||
| 85 | $w = bless { ptr => $w, cb => 1 }, __PACKAGE__; | ||
| 86 | $orig->($w); | ||
| 87 | }); | ||
| 88 | push @{ $keep{refaddr $self} }, $closer; | ||
| 89 | } | ||
| 90 | |||
| 91 | $xsub->($self, undef, $opener, $writer, $closer); | ||
| 92 | }); | ||
| 93 | |||
| 94 | |||
| 95 | $ffi->attach( open_FILE => ['archive_write', 'opaque'] => 'int' => sub { | ||
| 96 | my($xsub, $self, $fp) = @_; | ||
| 97 | $fp = $$fp if is_blessed_ref $fp && $fp->isa('FFI::C::File'); | ||
| 98 | $xsub->($self, $fp); | ||
| 99 | }); | ||
| 100 | |||
| 101 | |||
| 102 | sub open_memory ($self, $image) | ||
| 103 | { | ||
| 104 | # TODO: it would be nice to pre-allocate $$ref with grow (FFI::Platypus::Buffer) | ||
| 105 | # but that gave me scary errors, so look into it later. | ||
| 106 | $self->open( | ||
| 107 | write => sub ($w, $ref) { | ||
| 108 | $$image .= $$ref; | ||
| 109 | return length $$ref; | ||
| 110 | }, | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub open_perlfile ($self, $fh) | ||
| 116 | { | ||
| 117 | $self->open( | ||
| 118 | write => sub ($w, $ref) { | ||
| 119 | return syswrite $fh, $$ref; | ||
| 120 | }, | ||
| 121 | close => sub ($w) { | ||
| 122 | close $fh; | ||
| 123 | }, | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | $ffi->attach( [ data => 'write_data' ] => ['archive_write', 'opaque', 'size_t'] => 'ssize_t' => sub { | ||
| 129 | my $xsub = shift; | ||
| 130 | my $self = shift; | ||
| 131 | $xsub->($self, scalar_to_buffer ${$_[0]}); | ||
| 132 | }); | ||
| 133 | |||
| 134 | |||
| 135 | $ffi->attach( add_filter => ['archive_write', 'archive_filter_t'] => 'int' ); | ||
| 136 | $ffi->attach( set_format => ['archive_write', 'archive_format_t'] => 'int' ); | ||
| 137 | |||
| 138 | |||
| 139 | $ffi->attach( set_passphrase_callback => ['archive_write', 'opaque', 'archive_passphrase_callback'] => 'int' => sub { | ||
| 140 | my($xsub, $self, $sub) = @_; | ||
| 141 | |||
| 142 | my $closure = FFI::Platypus->closure(sub ($w, $) { | ||
| 143 | $w = bless { ptr => $w, cb => 1 }, __PACKAGE__; | ||
| 144 | my $passphrase = $sub->($w); | ||
| 145 | $passphrase = '' unless defined $passphrase; | ||
| 146 | my $ptr = strdup $passphrase; | ||
| 147 | free delete $self->{passphrase} if defined $self->{passphrase}; | ||
| 148 | return $self->{passphrase} = $ptr; | ||
| 149 | }); | ||
| 150 | |||
| 151 | push @{ $keep{refaddr $self} }, $closure; | ||
| 152 | |||
| 153 | $xsub->($self, undef, $closure); | ||
| 154 | |||
| 155 | }); | ||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | require Archive::Libarchive::Lib::ArchiveWrite unless $Archive::Libarchive::no_gen; | ||
| 160 | |||
| 161 | 1; | ||
| 162 | |||
| 163 | __END__ | ||