| 1 | package Archive::Libarchive::Peek; | ||
| 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 | 0 | ▶ | use Archive::Libarchive 0.03 qw( ARCHIVE_OK ARCHIVE_WARN ARCHIVE_EOF ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Ref::Util qw( is_plain_coderef is_plain_arrayref is_plain_scalarref is_ref ); | ||
| 7 | use Carp (); | ||
| 8 | use Path::Tiny (); | ||
| 9 | use 5.022; | ||
| 10 | use experimental qw( signatures postderef refaliasing ); | ||
| 11 | |||
| 12 | # ABSTRACT: Peek into archives without extracting them | ||
| 13 | our $VERSION = '0.06'; # VERSION | ||
| 14 | |||
| 15 | |||
| 16 | sub new ($class, %options) | ||
| 17 | { | ||
| 18 | Carp::croak("Required option: one of filename or memory") | ||
| 19 | unless defined $options{filename} || defined $options{memory}; | ||
| 20 | |||
| 21 | Carp::croak("Exactly one of filename or memory is required") | ||
| 22 | if defined $options{filename} && defined $options{memory}; | ||
| 23 | |||
| 24 | if(defined $options{filename}) | ||
| 25 | { | ||
| 26 | foreach my $filename (@{ is_plain_arrayref($options{filename}) ? $options{filename} : [$options{filename}] }) | ||
| 27 | { | ||
| 28 | Carp::croak("Missing or unreadable: $filename") | ||
| 29 | unless -r $filename; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | elsif(!(is_plain_scalarref $options{memory} && defined $options{memory}->$* && !is_ref $options{memory}->$*)) | ||
| 33 | { | ||
| 34 | Carp::croak("Option memory must be a scalar reference to a plain non-reference scalar"); | ||
| 35 | } | ||
| 36 | |||
| 37 | my $self = bless { | ||
| 38 | filename => delete $options{filename}, | ||
| 39 | passphrase => delete $options{passphrase}, | ||
| 40 | memory => delete $options{memory}, | ||
| 41 | }, $class; | ||
| 42 | |||
| 43 | Carp::croak("Illegal options: @{[ sort keys %options ]}") | ||
| 44 | if %options; | ||
| 45 | |||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub filename ($self) | ||
| 51 | { | ||
| 52 | return $self->{filename}; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub _archive ($self) | ||
| 57 | { | ||
| 58 | my $r = Archive::Libarchive::ArchiveRead->new; | ||
| 59 | my $e = Archive::Libarchive::Entry->new; | ||
| 60 | |||
| 61 | $r->support_filter_all; | ||
| 62 | $r->support_format_all; | ||
| 63 | |||
| 64 | if($self->{passphrase}) | ||
| 65 | { | ||
| 66 | if(is_plain_coderef $self->{passphrase}) | ||
| 67 | { | ||
| 68 | $r->set_passphrase_callback($self->{passphrase}); | ||
| 69 | } | ||
| 70 | else | ||
| 71 | { | ||
| 72 | $r->add_passphrase($self->{passphrase}); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | my $ret; | ||
| 77 | |||
| 78 | if(defined $self->{filename}) | ||
| 79 | { | ||
| 80 | $ret = is_plain_arrayref($self->filename) ? $r->open_filenames($self->filename, 10240) : $r->open_filename($self->filename, 10240); | ||
| 81 | } | ||
| 82 | else | ||
| 83 | { | ||
| 84 | $ret = $r->open_memory($self->{memory}); | ||
| 85 | } | ||
| 86 | |||
| 87 | if($ret == ARCHIVE_WARN) | ||
| 88 | { | ||
| 89 | Carp::carp($r->error_string); | ||
| 90 | } | ||
| 91 | elsif($ret < ARCHIVE_WARN) | ||
| 92 | { | ||
| 93 | Carp::croak($r->error_string); | ||
| 94 | } | ||
| 95 | |||
| 96 | return ($r,$e); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _entry ($self, $r, $e) | ||
| 100 | { | ||
| 101 | my $ret = $r->next_header($e); | ||
| 102 | return 0 if $ret == ARCHIVE_EOF; | ||
| 103 | if($ret == ARCHIVE_WARN) | ||
| 104 | { | ||
| 105 | Carp::carp($r->error_string); | ||
| 106 | } | ||
| 107 | elsif($ret < ARCHIVE_WARN) | ||
| 108 | { | ||
| 109 | Carp::croak($r->error_string); | ||
| 110 | } | ||
| 111 | return 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub files ($self) | ||
| 115 | { | ||
| 116 | my($r, $e) = $self->_archive; | ||
| 117 | |||
| 118 | my @files; | ||
| 119 | |||
| 120 | while(1) | ||
| 121 | { | ||
| 122 | last unless $self->_entry($r,$e); | ||
| 123 | push @files, $e->pathname; | ||
| 124 | $r->read_data_skip; | ||
| 125 | } | ||
| 126 | |||
| 127 | $r->close; | ||
| 128 | |||
| 129 | return @files; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub _entry_data ($self, $r, $e, $content) | ||
| 134 | { | ||
| 135 | $$content = ''; | ||
| 136 | |||
| 137 | if($e->size > 0) | ||
| 138 | { | ||
| 139 | while(1) | ||
| 140 | { | ||
| 141 | my $buffer; | ||
| 142 | my $ret = $r->read_data(\$buffer); | ||
| 143 | last if $ret == 0; | ||
| 144 | if($ret == ARCHIVE_WARN) | ||
| 145 | { | ||
| 146 | Carp::carp($r->error_string); | ||
| 147 | } | ||
| 148 | elsif($ret < ARCHIVE_WARN) | ||
| 149 | { | ||
| 150 | Carp::croak($r->error_string); | ||
| 151 | } | ||
| 152 | $$content .= $buffer; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub file ($self, $filename) | ||
| 158 | { | ||
| 159 | my($r, $e) = $self->_archive; | ||
| 160 | |||
| 161 | while(1) | ||
| 162 | { | ||
| 163 | last unless $self->_entry($r,$e); | ||
| 164 | if($e->pathname eq $filename) | ||
| 165 | { | ||
| 166 | my $content; | ||
| 167 | $self->_entry_data($r, $e, \$content); | ||
| 168 | return $content; | ||
| 169 | } | ||
| 170 | else | ||
| 171 | { | ||
| 172 | $r->read_data_skip; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | $r->close; | ||
| 177 | |||
| 178 | return undef; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub iterate ($self, $callback) | ||
| 183 | { | ||
| 184 | my($r, $e) = $self->_archive; | ||
| 185 | |||
| 186 | while(1) | ||
| 187 | { | ||
| 188 | last unless $self->_entry($r,$e); | ||
| 189 | my $content; | ||
| 190 | $self->_entry_data($r, $e, \$content); | ||
| 191 | $callback->($e->pathname, $content, $e); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | sub as_hash ($self) | ||
| 197 | { | ||
| 198 | my %hash; | ||
| 199 | my %links; | ||
| 200 | $self->iterate(sub ($path, $content, $e) { | ||
| 201 | if(my $target = $e->hardlink) | ||
| 202 | { | ||
| 203 | if(defined $hash{$target}) | ||
| 204 | { | ||
| 205 | \$hash{$path} = \$hash{$target}; | ||
| 206 | } | ||
| 207 | else | ||
| 208 | { | ||
| 209 | Carp::croak("found hardlink but no target"); | ||
| 210 | } | ||
| 211 | return; | ||
| 212 | } | ||
| 213 | my $type = $e->filetype; | ||
| 214 | if($type eq 'reg') | ||
| 215 | { | ||
| 216 | $hash{$path} = $content; | ||
| 217 | } | ||
| 218 | elsif($type eq 'lnk') | ||
| 219 | { | ||
| 220 | my $target = Path::Tiny->new($e->symlink)->absolute(Path::Tiny->new("/")->child($e->pathname)->parent)->relative('/'); | ||
| 221 | $links{$path} = $target; | ||
| 222 | } | ||
| 223 | else | ||
| 224 | { | ||
| 225 | $hash{$path} = [$type]; | ||
| 226 | } | ||
| 227 | }); | ||
| 228 | |||
| 229 | foreach my $path (keys %links) | ||
| 230 | { | ||
| 231 | my $target = $links{$path}; | ||
| 232 | if($hash{$target}) | ||
| 233 | { | ||
| 234 | $hash{$path} = \$hash{$target}; | ||
| 235 | } | ||
| 236 | else | ||
| 237 | { | ||
| 238 | $hash{$path} = \undef; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | \%hash; | ||
| 243 | } | ||
| 244 | |||
| 245 | 1; | ||
| 246 | |||
| 247 | __END__ | ||