1 # This file was automatically generated by SWIG (http://www.swig.org).
4 # Do not make changes to this file unless you know what you are doing--modify
5 # the SWIG interface file instead.
7 package Amanda::Archive;
9 use base qw(DynaLoader);
10 package Amanda::Archivec;
11 bootstrap Amanda::Archive;
12 package Amanda::Archive;
15 # ---------- BASE METHODS -------------
17 package Amanda::Archive;
20 my ($classname,$obj) = @_;
21 return bless $obj, $classname;
31 my ($self,$field) = @_;
32 my $member_func = "swig_${field}_get";
33 $self->$member_func();
37 my ($self,$field,$newval) = @_;
38 my $member_func = "swig_${field}_set";
39 $self->$member_func($newval);
48 # ------- FUNCTION WRAPPERS --------
50 package Amanda::Archive;
52 *amar_new = *Amanda::Archivec::amar_new;
53 *amar_close = *Amanda::Archivec::amar_close;
54 *amar_new_file = *Amanda::Archivec::amar_new_file;
55 *amar_file_close = *Amanda::Archivec::amar_file_close;
56 *amar_new_attr = *Amanda::Archivec::amar_new_attr;
57 *amar_attr_close = *Amanda::Archivec::amar_attr_close;
58 *amar_attr_add_data_buffer = *Amanda::Archivec::amar_attr_add_data_buffer;
59 *amar_attr_add_data_fd = *Amanda::Archivec::amar_attr_add_data_fd;
60 *amar_read = *Amanda::Archivec::amar_read;
62 # ------- VARIABLE STUBS --------
64 package Amanda::Archive;
73 Amanda::Archive - Perl access to the amanda archive library
79 # Write to the file descriptor or file handle $fd, and
80 # add /etc/hosts to it
81 my $archive = Amanda::Archive->new($fd, ">");
82 my $file = $archive->new_file("/etc/hosts");
83 my $attr = $file->new_attr(16);
84 open(my $fh, "<", "/etc/hosts");
85 $attr->add_data_fd($fh, 1);
89 # Read from an archive
90 my $archive = Amanda::Archive->new($fd, "<");
93 my ($user_data, $filenum, $filename) = @_;
95 return "foo"; # this becomes $file_data
98 my ($user_data, $file_data, $filenum, $truncated) = @_;
101 21 => [ 32768, # buffer into 32k chunks
103 my ($user_data, $filenum, $file_data, $attrid,
104 $attr_data, $data, $eoa, $truncated) = @_;
105 return "pants"; # becomes the new $attr_data for
106 # any subsequent fragments
108 0 => sub { # note no buffering here; attrid 0 is "default"
109 my ($user_data, $filenum, $file_data, $attrid,
110 $attr_data, $data, $eoa, $truncated) = @_;
111 return "shorts"; # becomes the new $attr_data for
112 # any subsequent fragments
114 user_data => [ "mydata" ], # sent to all callbacks
119 =head2 Amanda::Archive::Archive Objects
121 Note that C<< Amanda::Archive->new >> and C<<
122 Amanda::Archive::Archive->new >> are equivalent.
126 =item C<new($fd, $mode)>
128 Create a new archive for reading ("<") or writing (">") from or to
129 file C<$fd> (a file handle or integer file descriptor).
131 =item C<new_file($filename, $want_posn)>
133 Create a new C<Amanda::Archive::File> object with the given filename
134 (writing only). Equivalent to
136 Amanda::Archive::File->new($archive, $filename, $want_posn);
138 if C<$want_posn> is false, then this method returns a new
139 C<Amanda::Archive::File> object. If C<$want_posn> is true, then it
140 returns C<($file, $posn)> where C<$file> is the object and C<$posn> is
141 the offset into the datastream at which this file begins. This offset
142 can be stored in an index and used later to seek into the file.
146 See I<READING>, below.
150 Flush all buffers and close this archive. This does not close the file
155 =head2 Amanda::Archive::File Objects
159 =item C<new($archive, $filename, $want_posn)>
161 Create a new file in the given archive. See
162 C<Amanda::Archive::Archive::new_file>, above.
164 =item C<new_attr($attrid)>
166 Create a new C<Amanda::Archive::Attribute> object. Equivalent to
168 Amanda::Archive::Attr->new($file, $attrid);
172 Close this file, writing an EOF record.
176 =head2 Amanda::Archive::Attribute Objects
180 =item C<add_data($data, $eoa)>
182 Add C<$data> to this attribute, adding an EOA (end-of-attribute) bit
185 =item C<add_data_fd($fh, $eoa)>
187 Copy data from C<$fh> to this attribute, adding an EOA
188 (end-of-attribute) bit if C<$eoa> is true.
192 Close this attribute, adding an EOA bit if none has been written
199 The C<Amanda::Archive::Archive> method C<read()> handles reading
200 archives via a callback mechanism. It takes its arguments in hash
201 form, with the following keys:
204 my ($user_data, $filenum, $filename) = @_;
208 C<file_start> gives a sub which is called for every file in the
209 archive. It can return an arbitrary value which will become the
210 C<$file_data> for subsequent callbacks in this file, or the string
211 "IGNORE" which will cause the reader to ignore all data for this file.
212 In this case, no other callbacks will be made for the file (not even
216 my ($user_data, $file_data, $filenum, $truncated) = @_;
220 C<file_finish> gives a sub which is called when an EOF record appears.
221 C<$file_data> comes from the return value of the C<file_start>
222 callback. C<$truncated> is true if the file may be missing data
223 (e.g., when an early EOF is detected).
225 user_data => $my_object,
227 C<user_data> gives an arbitrary value which is passed to each callback
231 my ($user_data, $filenum, $file_data, $attrid,
232 $attr_data, $data, $eoa, $truncated) = @_;
235 19 => [ 10240, sub { ... } ],
237 Any numeric key is treated as an attribute ID, and specifies the
238 handling for that attribute. Attribute ID zero is treated as a
239 wildcard, and will match any attribute without an explicit handler.
240 The handler can be specified as a sub (as for attribute ID 13 in the
241 example above) or as an arrayref C<[$minsize, $sub]>. In the latter
242 case, the sub is only called when at least C<$minsize> bytes of data
243 are available for the attribute, or at the end of the attribute data.
245 The parameters to the callback include C<$file_data>, the value
246 returned from C<file_start>, and C<$attr_data>, which is the return
247 value of the last invocation of this sub for this attribute. If this
248 is the last fragment of data for this attribute, then C<$eoa> is true.
249 The meaning of C<$truncated> is similar to that in C<file_finish>.
254 my ($arch_fh, $basedir) = @_;
256 my $arch = Amanda::Archive->new(fileno($arch_fh), "<");
259 my ($user_data, $filenum, $filename) = @_;
260 return "$basedir/$filenum"; # becomes $file_data
263 my ($user_data, $filenum, $file_data, $attrid,
264 $attr_data, $data, $eoa, $truncated) = @_;
265 warn("file $filename attribute $attrid is truncated")
267 # store the open filehandle in $attr_data
269 open($attr_data, "$file_data.$attrid", ">")
272 print $attr_data $data;
285 package Amanda::Archive;
287 # Expose the Archive constructor at Amanda::Archive->new
290 Amanda::Archive::Archive->new(@_);
293 package Amanda::Archive::Archive;
296 my ($class, $fd, $mode) = @_;
297 my $arch = Amanda::Archive::amar_new($fd, $mode);
298 return bless (\$arch, $class);
304 Amanda::Archive::amar_close($$self);
315 my ($self, $filename, $want_offset) = @_;
316 return Amanda::Archive::File->new($self, $filename, $want_offset);
319 sub Amanda::Archive::Archive::read {
321 die "Archive is not open" unless ($$self);
322 # pass a hashref to the C code
324 Amanda::Archive::amar_read($$self, \%h);
327 package Amanda::Archive::File;
330 my ($class, $arch, $filename, $want_offset) = @_;
331 die "Archive is not open" unless ($$arch);
333 # note that posn is returned first by the SWIG wrapper
334 my ($file, $posn) = Amanda::Archive::amar_new_file($$arch, $filename, $want_offset);
335 return (bless([ $file, $arch ], $class), $posn);
337 my $file = Amanda::Archive::amar_new_file($$arch, $filename, $want_offset);
338 return bless([ $file, $arch ], $class);
345 Amanda::Archive::amar_file_close($self->[0]);
356 my ($self, $attrid) = @_;
357 return Amanda::Archive::Attr->new($self, $attrid);
360 package Amanda::Archive::Attr;
363 my ($class, $file, $attrid) = @_;
364 die "File is not open" unless ($file->[0]);
365 my $attr = Amanda::Archive::amar_new_attr($file->[0], $attrid);
366 return bless ([$attr, $file], $class);
372 Amanda::Archive::amar_attr_close($self->[0]);
383 my ($self, $data, $eoa) = @_;
384 die "Attr is not open" unless ($self->[0]);
385 Amanda::Archive::amar_attr_add_data_buffer($self->[0], $data, $eoa);
389 my ($self, $fd, $eoa) = @_;
390 die "Attr is not open" unless ($self->[0]);
391 return Amanda::Archive::amar_attr_add_data_fd($self->[0], $fd, $eoa);