| 1 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use 5.020; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use experimental qw( postderef signatures ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | package Archive::Libarchive::Compress 0.01 { | ||
| 6 | |||
| 7 | 1 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Path::Tiny (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use File::chdir; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Ref::Util qw( is_ref is_plain_scalarref is_plain_coderef ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use Archive::Libarchive 0.04 qw( ARCHIVE_OK ARCHIVE_WARN ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use FFI::C::Stat; | ||
| 13 | |||
| 14 | # ABSTRACT: Recursively archive a directory (using libarchive) | ||
| 15 | |||
| 16 | |||
| 17 | sub new ($class, %options) { | ||
| 18 | Carp::croak("Required option: one of filename or memory") | ||
| 19 | unless defined($options{filename} // $options{memory}); | ||
| 20 | Carp::croak("Exactly one of filename or memory is required") | ||
| 21 | if defined($options{filename}) && defined($options{memory}); | ||
| 22 | |||
| 23 | if(defined $options{memory} && !(is_plain_scalarref $options{memory} && defined $options{memory}->$* && !is_ref $options{memory}->$*)) { | ||
| 24 | Carp::croak("Option memory must be a scalar reference to a plain non-reference scalar"); | ||
| 25 | } | ||
| 26 | |||
| 27 | Carp::croak("Entry is not a code reference") | ||
| 28 | if defined $options{entry} && !is_plain_coderef $options{entry}; | ||
| 29 | |||
| 30 | my $self = bless { | ||
| 31 | filename => delete $options{filename}, | ||
| 32 | entry => delete($options{entry}) // sub ($e) { return 1 }, | ||
| 33 | memory => delete $options{memory}, | ||
| 34 | prep => delete($options{prep}) // sub ($ar) { $ar->set_format_pax_restricted }, | ||
| 35 | }, $class; | ||
| 36 | |||
| 37 | Carp::croak("Illegal options: @{[ sort keys %options ]}") | ||
| 38 | if %options; | ||
| 39 | |||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub filename ($self) { return $self->{filename} } | ||
| 45 | sub from ($self) { return $self->{from} } | ||
| 46 | |||
| 47 | sub _archive ($self) { | ||
| 48 | my $w = Archive::Libarchive::ArchiveWrite->new; | ||
| 49 | my $e = Archive::Libarchive::Entry->new; | ||
| 50 | |||
| 51 | $self->{prep}->($w); | ||
| 52 | |||
| 53 | my $ret; | ||
| 54 | |||
| 55 | if($self->filename) { | ||
| 56 | $ret = $w->open_filename($self->filename); | ||
| 57 | } else { | ||
| 58 | $ret = $w->open_memory($self->{memory}); | ||
| 59 | } | ||
| 60 | |||
| 61 | if($ret == ARCHIVE_WARN) { | ||
| 62 | Carp::carp($w->error_string); | ||
| 63 | } elsif($ret < ARCHIVE_WARN) { | ||
| 64 | Carp::croak($w->error_string); | ||
| 65 | } | ||
| 66 | |||
| 67 | return($w,$e); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _iterate ($self, $w, $e, $path) { | ||
| 71 | |||
| 72 | if(-d $path) { | ||
| 73 | foreach my $child (sort { $a->basename cmp $b->basename } $path->children) { | ||
| 74 | $self->_iterate($w, $e, $child); | ||
| 75 | } | ||
| 76 | } elsif(-f $path) { | ||
| 77 | $e->clear; | ||
| 78 | $e->set_pathname("$path"); | ||
| 79 | $e->set_filetype('reg'); | ||
| 80 | my $stat = FFI::C::Stat->new("$path"); | ||
| 81 | $e->copy_stat($stat); | ||
| 82 | |||
| 83 | if($self->{entry}->($e)) { | ||
| 84 | my $ret = $w->write_header($e); | ||
| 85 | |||
| 86 | if($ret == ARCHIVE_WARN) { | ||
| 87 | Carp::carp($w->error_string); | ||
| 88 | } elsif($ret < ARCHIVE_WARN) { | ||
| 89 | Carp::croak($w->error_string); | ||
| 90 | } | ||
| 91 | |||
| 92 | $ret = $w->write_data(\$path->slurp_raw); | ||
| 93 | |||
| 94 | if($ret == ARCHIVE_WARN) { | ||
| 95 | Carp::carp($w->error_string); | ||
| 96 | } elsif($ret < ARCHIVE_WARN) { | ||
| 97 | Carp::croak($w->error_string); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub compress ($self, %options) { | ||
| 105 | Carp::croak("Already compressed") if defined $self->from; | ||
| 106 | |||
| 107 | my $from = Path::Tiny->new($options{from} // $CWD); | ||
| 108 | |||
| 109 | my($w, $e) = $self->_archive; | ||
| 110 | |||
| 111 | local $CWD = $from; | ||
| 112 | |||
| 113 | $self->_iterate($w, $e, Path::Tiny->new(".")); | ||
| 114 | |||
| 115 | $w->close; | ||
| 116 | |||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | |||
| 122 | __END__ | ||