2 * Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
26 Amanda::Archive - Perl access to the amanda archive library
32 # Write to the file descriptor or file handle $fd, and
33 # add /etc/hosts to it
34 my $archive = Amanda::Archive->new($fd, ">");
35 my $file = $archive->new_file("/etc/hosts");
36 my $attr = $file->new_attr(16);
37 open(my $fh, "<", "/etc/hosts");
38 $attr->add_data_fd($fh, 1);
42 # Read from an archive
43 my $archive = Amanda::Archive->new($fd, "<");
46 my ($user_data, $filenum, $filename) = @_;
48 return "foo"; # this becomes $file_data
51 my ($user_data, $file_data, $filenum, $truncated) = @_;
54 21 => [ 32768, # buffer into 32k chunks
56 my ($user_data, $filenum, $file_data, $attrid,
57 $attr_data, $data, $eoa, $truncated) = @_;
58 return "pants"; # becomes the new $attr_data for
59 # any subsequent fragments
61 0 => sub { # note no buffering here; attrid 0 is "default"
62 my ($user_data, $filenum, $file_data, $attrid,
63 $attr_data, $data, $eoa, $truncated) = @_;
64 return "shorts"; # becomes the new $attr_data for
65 # any subsequent fragments
67 user_data => [ "mydata" ], # sent to all callbacks
72 =head2 Amanda::Archive::Archive Objects
74 Note that C<< Amanda::Archive->new >> and C<<
75 Amanda::Archive::Archive->new >> are equivalent.
79 =item C<new($fd, $mode)>
81 Create a new archive for reading ("<") or writing (">") from or to
82 file C<$fd> (a file handle or integer file descriptor).
84 =item C<new_file($filename, $want_posn)>
86 Create a new C<Amanda::Archive::File> object with the given filename
87 (writing only). Equivalent to
89 Amanda::Archive::File->new($archive, $filename, $want_posn);
91 if C<$want_posn> is false, then this method returns a new
92 C<Amanda::Archive::File> object. If C<$want_posn> is true, then it
93 returns C<($file, $posn)> where C<$file> is the object and C<$posn> is
94 the offset into the datastream at which this file begins. This offset
95 can be stored in an index and used later to seek into the file.
99 See I<READING>, below.
103 Flush all buffers and close this archive. This does not close the file
108 =head2 Amanda::Archive::File Objects
112 =item C<new($archive, $filename, $want_posn)>
114 Create a new file in the given archive. See
115 C<Amanda::Archive::Archive::new_file>, above.
117 =item C<new_attr($attrid)>
119 Create a new C<Amanda::Archive::Attribute> object. Equivalent to
121 Amanda::Archive::Attr->new($file, $attrid);
125 Close this file, writing an EOF record.
129 =head2 Amanda::Archive::Attribute Objects
133 =item C<add_data($data, $eoa)>
135 Add C<$data> to this attribute, adding an EOA (end-of-attribute) bit
138 =item C<add_data_fd($fh, $eoa)>
140 Copy data from C<$fh> to this attribute, adding an EOA
141 (end-of-attribute) bit if C<$eoa> is true.
145 Close this attribute, adding an EOA bit if none has been written
152 The C<Amanda::Archive::Archive> method C<read()> handles reading
153 archives via a callback mechanism. It takes its arguments in hash
154 form, with the following keys:
157 my ($user_data, $filenum, $filename) = @_;
161 C<file_start> gives a sub which is called for every file in the
162 archive. It can return an arbitrary value which will become the
163 C<$file_data> for subsequent callbacks in this file, or the string
164 "IGNORE" which will cause the reader to ignore all data for this file.
165 In this case, no other callbacks will be made for the file (not even
169 my ($user_data, $file_data, $filenum, $truncated) = @_;
173 C<file_finish> gives a sub which is called when an EOF record appears.
174 C<$file_data> comes from the return value of the C<file_start>
175 callback. C<$truncated> is true if the file may be missing data
176 (e.g., when an early EOF is detected).
178 user_data => $my_object,
180 C<user_data> gives an arbitrary value which is passed to each callback
184 my ($user_data, $filenum, $file_data, $attrid,
185 $attr_data, $data, $eoa, $truncated) = @_;
188 19 => [ 10240, sub { ... } ],
190 Any numeric key is treated as an attribute ID, and specifies the
191 handling for that attribute. Attribute ID zero is treated as a
192 wildcard, and will match any attribute without an explicit handler.
193 The handler can be specified as a sub (as for attribute ID 13 in the
194 example above) or as an arrayref C<[$minsize, $sub]>. In the latter
195 case, the sub is only called when at least C<$minsize> bytes of data
196 are available for the attribute, or at the end of the attribute data.
198 The parameters to the callback include C<$file_data>, the value
199 returned from C<file_start>, and C<$attr_data>, which is the return
200 value of the last invocation of this sub for this attribute. If this
201 is the last fragment of data for this attribute, then C<$eoa> is true.
202 The meaning of C<$truncated> is similar to that in C<file_finish>.
207 my ($arch_fh, $basedir) = @_;
209 my $arch = Amanda::Archive->new(fileno($arch_fh), "<");
212 my ($user_data, $filenum, $filename) = @_;
213 return "$basedir/$filenum"; # becomes $file_data
216 my ($user_data, $filenum, $file_data, $attrid,
217 $attr_data, $data, $eoa, $truncated) = @_;
218 warn("file $filename attribute $attrid is truncated")
220 # store the open filehandle in $attr_data
222 open($attr_data, "$file_data.$attrid", ">")
225 print $attr_data $data;