Debian Codegraph @

examples/archive_object.pl

raw
1 use strict;
2 use warnings;
3 use FFI::Platypus 2.00;
4 use FFI::CheckLib qw( find_lib_or_die );
5
6 # This example uses FreeBSD's libarchive to list the contents of any
7 # archive format that it suppors. We've also filled out a part of
8 # the ArchiveWrite class that could be used for writing archive formats
9 # supported by libarchive
10
11 my $ffi = FFI::Platypus->new(
12 api => 2,
13 lib => find_lib_or_die(lib => 'archive'),
14 );
15 $ffi->type('object(Archive)' => 'archive_t');
16 $ffi->type('object(ArchiveRead)' => 'archive_read_t');
17 $ffi->type('object(ArchiveWrite)' => 'archive_write_t');
18 $ffi->type('object(ArchiveEntry)' => 'archive_entry_t');
19
20 package Archive {
21 # base class is "abstract" having no constructor or destructor
22
23 $ffi->mangler(sub {
24 my($name) = @_;
25 "archive_$name";
26 });
27 $ffi->attach( error_string => ['archive_t'] => 'string' );
28 }
29
30 package ArchiveRead {
31 our @ISA = qw( Archive );
32
33 $ffi->mangler(sub {
34 my($name) = @_;
35 "archive_read_$name";
36 });
37
38 $ffi->attach( new => ['string'] => 'archive_read_t' );
39 $ffi->attach( [ free => 'DESTROY' ] => ['archive_t'] );
40 $ffi->attach( support_filter_all => ['archive_t'] => 'int' );
41 $ffi->attach( support_format_all => ['archive_t'] => 'int' );
42 $ffi->attach( open_filename => ['archive_t','string','size_t'] => 'int' );
43 $ffi->attach( next_header2 => ['archive_t', 'archive_entry_t' ] => 'int' );
44 $ffi->attach( data_skip => ['archive_t'] => 'int' );
45 # ... define additional read methods
46 }
47
48 package ArchiveWrite {
49
50 our @ISA = qw( Archive );
51
52 $ffi->mangler(sub {
53 my($name) = @_;
54 "archive_write_$name";
55 });
56
57 $ffi->attach( new => ['string'] => 'archive_write_t' );
58 $ffi->attach( [ free => 'DESTROY' ] => ['archive_write_t'] );
59 # ... define additional write methods
60 }
61
62 package ArchiveEntry {
63
64 $ffi->mangler(sub {
65 my($name) = @_;
66 "archive_entry_$name";
67 });
68
69 $ffi->attach( new => ['string'] => 'archive_entry_t' );
70 $ffi->attach( [ free => 'DESTROY' ] => ['archive_entry_t'] );
71 $ffi->attach( pathname => ['archive_entry_t'] => 'string' );
72 # ... define additional entry methods
73 }
74
75 use constant ARCHIVE_OK => 0;
76
77 # this is a Perl version of the C code here:
78 # https://github.com/libarchive/libarchive/wiki/Examples#List_contents_of_Archive_stored_in_File
79
80 my $archive_filename = shift @ARGV;
81 unless(defined $archive_filename)
82 {
83 print "usage: $0 archive.tar\n";
84 exit;
85 }
86
87 my $archive = ArchiveRead->new;
88 $archive->support_filter_all;
89 $archive->support_format_all;
90
91 my $r = $archive->open_filename($archive_filename, 1024);
92 die "error opening $archive_filename: ", $archive->error_string
93 unless $r == ARCHIVE_OK;
94
95 my $entry = ArchiveEntry->new;
96
97 while($archive->next_header2($entry) == ARCHIVE_OK)
98 {
99 print $entry->pathname, "\n";
100 $archive->data_skip;
101 }