2 * Copyright (c) Zmanda, Inc. All Rights Reserved.
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11 * License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 * Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18 * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
23 /* A note regarding error handling in this module. Amar returns errors via the
24 * Glib GError mechanism. Most functions return a boolean, where TRUE
25 * indicates success, and FALSE indicates an error which is indicated in the
28 * Fatal programming errors are handled with assertions and error exits; any
29 * fatal format or system errors are handled via GError. Some format errors
30 * (e.g., missing EOAs at the end of a file) are handled without any
33 * The domain for amar errors is that returned from amar_error_quark, and error
34 * codes are system error codes (e.g., EINVAL, ENOSPC). */
36 GQuark amar_error_quark(void);
38 /* opaque types for archives, files, and attributes */
40 typedef struct amar_s amar_t;
41 typedef struct amar_file_s amar_file_t;
42 typedef struct amar_attr_s amar_attr_t;
44 /* Application attribute IDs should start at AMAR_ATTR_APP_START */
47 /* internal-use only attributes */
48 AMAR_ATTR_FILENAME = 0,
51 /* anything above this value can be used by the application */
52 AMAR_ATTR_APP_START = 16,
53 AMAR_ATTR_GENERIC_DATA = AMAR_ATTR_APP_START,
56 /* Create an object to read/write an amanda archive on the file descriptor fd.
57 * @param fd: file descriptor of the file, it must already be opened
58 * @mode: O_RDONLY for reading, O_WRONLY for writing
59 * @returns: NULL on error
61 amar_t *amar_new(int fd, mode_t mode, GError **error);
63 /* Finish writing to this fd. All buffers are flushed, but the file descriptor
64 * is not closed -- the user must close it. */
65 gboolean amar_close(amar_t *archive, GError **error);
67 /* create a new 'file' object on the archive. The filename is treated as a
68 * binary blob, but if filename_len is zero, then its length will be calculated
69 * with strlen(). A zero-length filename_buf is not allowed.
71 * Note that a header record will only be written if header_offset is non-NULL,
72 * as this represents a location to which a reader could seek.
74 * @param archive: the archive containing this file
75 * @param filename_buf: filename to include in the file
76 * @param filename_len: length of the filename_buf, or 0 to calculate
77 * @param header_offset (output): offset of the header record preceding
78 * this file; pass NULL to ignore.
79 * @returns: NULL on error, otherwise a file object
81 amar_file_t *amar_new_file(
88 /* Flush all buffer the 'file' object and write a record with ID=2 */
89 gboolean amar_file_close(
93 /* create a new 'attribute' object with attrid attached to the file
95 * @returns: NULL on error, otherwise an attribute object
97 amar_attr_t *amar_new_attr(
102 /* flush all buffers and mark the end of the attribute */
103 gboolean amar_attr_close(
104 amar_attr_t *attribute,
107 /* Add 'size' byte of data from 'data' to the attribute. If this is the
108 * last data in this attribute, set eoa to TRUE. This will save space by
109 * writing and end-of-attribute indication in this record, instead of adding
110 * an empty EOA record.
112 gboolean amar_attr_add_data_buffer(
113 amar_attr_t *attribute,
119 /* This function reads from the file descriptor 'fd' until EOF and adds
120 * the resulting data to the attribute. The end of the attribute is
121 * flagged appropriately if EOA is true.
123 * @param attribute: the attribute for the data
124 * @param fd: the file descriptor from which to read
125 * @param eoa: add an EOA bit to the end?
126 * @returns: number of bytes read from fd, or -1 on error
128 off_t amar_attr_add_data_fd(
129 amar_attr_t *attribute,
134 /* When reading files, the handling of each attribute can be configured
135 * separately. Some attributes may always be short enough to fit in memory,
136 * and in this case the archive interface will take care of assembling any
137 * fragments for you. Some attributes should be ignored, while others
138 * will call a function for each fragment.
140 * There are a a number of xx_data options available here, that deserve some
142 * - user_data is global to the entire read operation (it is a parameter to
144 * - file_data is specific to the current file; it is set by the start_file
145 * callback and freed by the finish_file callback.
146 * - attrid_data is specific this the current attribute ID, across all files;
147 * it comes from the amar_attr_handling_t struct.
148 * - attr_data is specific to the current instance of the particular
149 * attribute. It points to a NULL pointer on the first call to the fragment
150 * callback, and can be set at that time. It should be freed when the EOA
153 * @param user_data: the pointer passed to amar_read
154 * @param filenum: the file number for this record
155 * @param file_data: the file_data pointer returned from the start_file callback
156 * @param attrid: the attribute id for this record
157 * @param attrid_data: the data from the handling array
158 * @param attr_data (in/out): data for this attribute; this will be the same
159 * pointer for every callback for a particular instance of an attribute.
160 * Any resources should be freed when eoa is true.
161 * @param data: the data for this fragment
162 * @param size: the size of data
163 * @param eoa: TRUE iff this is the last fragment for this attribute
164 * @param truncated: TRUE if this attribute is likely to be incomplete (e.g.,
165 * in an error situation)
166 * @returns: FALSE if the amar_read call should be aborted
168 typedef gboolean (*amar_fragment_callback_t)(
173 gpointer attrid_data,
180 /* amar_read takes an array of this struct, terminated by an entry
181 * with attrid 0. This final entry is used as the "catchall" for attributes
182 * not matching any other array entries. */
183 typedef struct amar_attr_handling_s {
186 /* if nonzero, this is the minimum size fragment that will be passed to the
187 * callback. Use SIZE_MAX for no limit, although this may result in
188 * excessive memory use while parsing a malicious or corrupt archive. */
191 /* if non-NULL, this function will be called for each fragment
192 * with this attribute ID */
193 amar_fragment_callback_t callback;
195 /* this value is passed as the attr_data parameter to the callback */
196 gpointer attrid_data;
197 } amar_attr_handling_t;
199 /* This function is called for each new file, and can decide whether to ignore
200 * the file, or set up file-specific data.
202 * @param user_data: the pointer passed to amar_read
203 * @param filenum: the file number for this record
204 * @param filename_buf: the filename of this file
205 * @param filename_len: the length of the filename
206 * @param ignore (output): if set to TRUE, ignore all attributes for this file.
207 * @param file_data (output): space to store file-specific data
208 * @returns: FALSE if the amar_read call should be aborted
210 typedef gboolean (*amar_file_start_callback_t)(
213 gpointer filename_buf,
216 gpointer *file_data);
218 /* This function is called for each new file, and can decide whether to ignore
219 * the file, or set up file-specific data.
221 * @param user_data: the pointer passed to amar_read
222 * @param filenum: the file number for this record
223 * @param file_data (output): space to store file-specific data
224 * @param truncated: TRUE if this file is likely to be incomplete (e.g.,
225 * in an error situation, or at an early EOF)
226 * @returns: FALSE if the amar_read call should be aborted
228 typedef gboolean (*amar_file_finish_callback_t)(
234 /* This function actually performs the read operation, calling all of the
235 * above callbacks. If any of the callbacks return FALSE, this function
236 * returns FALSE but does not set its error parameter.
238 * @param user_data: passed to all callbacks
239 * @param handling_array: array giving handling information
240 * @param file_start_cb: callback for file starts
241 * @param file_finish_cb: callback for file finishs
242 * @param error (output): the error result
243 * @returns: FALSE on error or an early exit, otherwise TRUE
248 amar_attr_handling_t *handling_array,
249 amar_file_start_callback_t file_start_cb,
250 amar_file_finish_callback_t file_finish_cb,