Debian Codegraph @

examples/archive.tar

raw
1 archive.pl0000664000175000017500000000626714330460142012412 0ustar ollisgollisguse strict;
2 use warnings;
3 use FFI::Platypus ();
4 use FFI::Platypus::API ();
5 use FFI::CheckLib ();
6
7 # This example uses FreeBSD's libarchive to list the contents of any
8 # archive format that it suppors. We've also filled out a part of
9 # the ArchiveWrite class that could be used for writing archive formats
10 # supported by libarchive
11
12 my $ffi = My::Platypus->new;
13 $ffi->lib(FFI::CheckLib::find_lib_or_die lib => 'archive');
14
15 $ffi->custom_type(archive => {
16 native_type => 'opaque',
17 perl_to_native => sub { ${$_[0]} },
18 native_to_perl => sub {
19 # this works because archive_read_new ignores any arguments
20 # and we pass in the class name which we can get here.
21 my $class = FFI::Platypus::API::arguments_get_string(0);
22 bless \$_[0], $class;
23 },
24 });
25
26 $ffi->custom_type(archive_entry => {
27 native_type => 'opaque',
28 perl_to_native => sub { ${$_[0]} },
29 native_to_perl => sub {
30 # works likewise for archive_entry objects
31 my $class = FFI::Platypus::API::arguments_get_string(0);
32 bless \$_[0], $class,
33 },
34 });
35
36 package My::Platypus;
37
38 use parent qw( FFI::Platypus );
39
40 sub find_symbol
41 {
42 my($self, $name) = @_;
43 my $prefix = lcfirst caller(2);
44 $prefix =~ s{([A-Z])}{"_" . lc $1}eg;
45 $self->SUPER::find_symbol(join '_', $prefix, $name);
46 }
47
48 package Archive;
49
50 # base class is "abstract" having no constructor or destructor
51
52 $ffi->attach( error_string => ['archive'] => 'string' );
53
54 package ArchiveRead;
55
56 our @ISA = qw( Archive );
57
58 $ffi->attach( new => ['string'] => 'archive' );
59 $ffi->attach( [ free => 'DESTROY' ] => ['archive'] => 'void' );
60 $ffi->attach( support_filter_all => ['archive'] => 'int' );
61 $ffi->attach( support_format_all => ['archive'] => 'int' );
62 $ffi->attach( open_filename => ['archive','string','size_t'] => 'int' );
63 $ffi->attach( next_header2 => ['archive', 'archive_entry' ] => 'int' );
64 $ffi->attach( data_skip => ['archive'] => 'int' );
65 # ... define additional read methods
66
67 package ArchiveWrite;
68
69 our @ISA = qw( Archive );
70
71 $ffi->attach( new => ['string'] => 'archive' );
72 $ffi->attach( [ free => 'DESTROY' ] => ['archive'] => 'void' );
73 # ... define additional write methods
74
75 package ArchiveEntry;
76
77 $ffi->attach( new => ['string'] => 'archive_entry' );
78 $ffi->attach( [ free => 'DESTROY' ] => ['archive_entry'] => 'void' );
79 $ffi->attach( pathname => ['archive_entry'] => 'string' );
80 # ... define additional entry methods
81
82 package main;
83
84 use constant ARCHIVE_OK => 0;
85
86 # this is a Perl version of the C code here:
87 # https://github.com/libarchive/libarchive/wiki/Examples#List_contents_of_Archive_stored_in_File
88
89 my $archive_filename = shift @ARGV;
90 unless(defined $archive_filename)
91 {
92 print "usage: $0 archive.tar\n";
93 exit;
94 }
95
96 my $archive = ArchiveRead->new;
97 $archive->support_filter_all;
98 $archive->support_format_all;
99
100 my $r = $archive->open_filename($archive_filename, 1024);
101 die "error opening $archive_filename: ", $archive->error_string
102 unless $r == ARCHIVE_OK;
103
104 my $entry = ArchiveEntry->new;
105
106 while($archive->next_header2($entry) == ARCHIVE_OK)
107 {
108 print $entry->pathname, "\n";
109 $archive->data_skip;
110 }
111
112 archive_object.pl0000664000175000017500000000564214331437766013755 0ustar ollisgollisguse strict;
113 use warnings;
114 use FFI::Platypus 2.00;
115 use FFI::CheckLib qw( find_lib_or_die );
116
117 # This example uses FreeBSD's libarchive to list the contents of any
118 # archive format that it suppors. We've also filled out a part of
119 # the ArchiveWrite class that could be used for writing archive formats
120 # supported by libarchive
121
122 my $ffi = FFI::Platypus->new( api => 2 );
123 $ffi->lib(find_lib_or_die lib => 'archive');
124 $ffi->type('object(Archive)' => 'archive_t');
125 $ffi->type('object(ArchiveRead)' => 'archive_read_t');
126 $ffi->type('object(ArchiveWrite)' => 'archive_write_t');
127 $ffi->type('object(ArchiveEntry)' => 'archive_entry_t');
128
129 package Archive;
130
131 # base class is "abstract" having no constructor or destructor
132
133 $ffi->mangler(sub {
134 my($name) = @_;
135 "archive_$name";
136 });
137 $ffi->attach( error_string => ['archive_t'] => 'string' );
138
139 package ArchiveRead;
140
141 our @ISA = qw( Archive );
142
143 $ffi->mangler(sub {
144 my($name) = @_;
145 "archive_read_$name";
146 });
147
148 $ffi->attach( new => ['string'] => 'archive_read_t' );
149 $ffi->attach( [ free => 'DESTROY' ] => ['archive_t'] );
150 $ffi->attach( support_filter_all => ['archive_t'] => 'int' );
151 $ffi->attach( support_format_all => ['archive_t'] => 'int' );
152 $ffi->attach( open_filename => ['archive_t','string','size_t'] => 'int' );
153 $ffi->attach( next_header2 => ['archive_t', 'archive_entry_t' ] => 'int' );
154 $ffi->attach( data_skip => ['archive_t'] => 'int' );
155 # ... define additional read methods
156
157 package ArchiveWrite;
158
159 our @ISA = qw( Archive );
160
161 $ffi->mangler(sub {
162 my($name) = @_;
163 "archive_write_$name";
164 });
165
166 $ffi->attach( new => ['string'] => 'archive_write_t' );
167 $ffi->attach( [ free => 'DESTROY' ] => ['archive_write_t'] );
168 # ... define additional write methods
169
170 package ArchiveEntry;
171
172 $ffi->mangler(sub {
173 my($name) = @_;
174 "archive_entry_$name";
175 });
176
177 $ffi->attach( new => ['string'] => 'archive_entry_t' );
178 $ffi->attach( [ free => 'DESTROY' ] => ['archive_entry_t'] );
179 $ffi->attach( pathname => ['archive_entry_t'] => 'string' );
180 # ... define additional entry methods
181
182 package main;
183
184 use constant ARCHIVE_OK => 0;
185
186 # this is a Perl version of the C code here:
187 # https://github.com/libarchive/libarchive/wiki/Examples#List_contents_of_Archive_stored_in_File
188
189 my $archive_filename = shift @ARGV;
190 unless(defined $archive_filename)
191 {
192 print "usage: $0 archive.tar\n";
193 exit;
194 }
195
196 my $archive = ArchiveRead->new;
197 $archive->support_filter_all;
198 $archive->support_format_all;
199
200 my $r = $archive->open_filename($archive_filename, 1024);
201 die "error opening $archive_filename: ", $archive->error_string
202 unless $r == ARCHIVE_OK;
203
204 my $entry = ArchiveEntry->new;
205
206 while($archive->next_header2($entry) == ARCHIVE_OK)
207 {
208 print $entry->pathname, "\n";
209 $archive->data_skip;
210 }
211
212