1<!-- Creator     : groff version 1.19.2 -->
2<!-- CreationDate: Sun Mar 14 19:50:23 2010 -->
3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
4"http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta name="generator" content="groff -Thtml, see www.gnu.org">
8<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
9<meta name="Content-Style" content="text/css">
10<style type="text/css">
11       p     { margin-top: 0; margin-bottom: 0; }
12       pre   { margin-top: 0; margin-bottom: 0; }
13       table { margin-top: 0; margin-bottom: 0; }
14</style>
15<title></title>
16</head>
17<body>
18
19<hr>
20
21
22<p valign="top">archive_write(3) FreeBSD Library Functions
23Manual archive_write(3)</p>
24
25<p style="margin-top: 1em" valign="top"><b>NAME</b></p>
26
27<p style="margin-left:8%;"><b>archive_write_new</b>,
28<b>archive_write_set_format_cpio</b>,
29<b>archive_write_set_format_pax</b>,
30<b>archive_write_set_format_pax_restricted</b>,
31<b>archive_write_set_format_shar</b>,
32<b>archive_write_set_format_shar_binary</b>,
33<b>archive_write_set_format_ustar</b>,
34<b>archive_write_get_bytes_per_block</b>,
35<b>archive_write_set_bytes_per_block</b>,
36<b>archive_write_set_bytes_in_last_block</b>,
37<b>archive_write_set_compression_bzip2</b>,
38<b>archive_write_set_compression_compress</b>,
39<b>archive_write_set_compression_gzip</b>,
40<b>archive_write_set_compression_none</b>,
41<b>archive_write_set_compression_program</b>,
42<b>archive_write_set_compressor_options</b>,
43<b>archive_write_set_format_options</b>,
44<b>archive_write_set_options</b>, <b>archive_write_open</b>,
45<b>archive_write_open_fd</b>,
46<b>archive_write_open_FILE</b>,
47<b>archive_write_open_filename</b>,
48<b>archive_write_open_memory</b>,
49<b>archive_write_header</b>, <b>archive_write_data</b>,
50<b>archive_write_finish_entry</b>,
51<b>archive_write_close</b>, <b>archive_write_finish</b>
52&mdash; functions for creating archives</p>
53
54
55<p style="margin-top: 1em" valign="top"><b>SYNOPSIS</b></p>
56
57<p style="margin-left:8%;"><b>#include
58&lt;archive.h&gt;</b></p>
59
60<p style="margin-left:8%; margin-top: 1em"><i>struct
61archive *</i></p>
62
63
64<p style="margin-left:14%;"><b>archive_write_new</b>(<i>void</i>);</p>
65
66<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
67
68
69<p style="margin-left:14%;"><b>archive_write_get_bytes_per_block</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
70
71<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
72
73
74<p style="margin-left:14%;"><b>archive_write_set_bytes_per_block</b>(<i>struct&nbsp;archive&nbsp;*</i>,
75<i>int&nbsp;bytes_per_block</i>);</p>
76
77<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
78
79
80<p style="margin-left:14%;"><b>archive_write_set_bytes_in_last_block</b>(<i>struct&nbsp;archive&nbsp;*</i>,
81<i>int</i>);</p>
82
83<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
84
85
86<p style="margin-left:14%;"><b>archive_write_set_compression_bzip2</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
87
88<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
89
90
91<p style="margin-left:14%;"><b>archive_write_set_compression_compress</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
92
93<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
94
95
96<p style="margin-left:14%;"><b>archive_write_set_compression_gzip</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
97
98<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
99
100
101<p style="margin-left:14%;"><b>archive_write_set_compression_none</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
102
103<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
104
105
106<p style="margin-left:14%;"><b>archive_write_set_compression_program</b>(<i>struct&nbsp;archive&nbsp;*</i>,
107<i>const&nbsp;char&nbsp;*&nbsp;cmd</i>);</p>
108
109<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
110
111
112<p style="margin-left:14%;"><b>archive_write_set_format_cpio</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
113
114<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
115
116
117<p style="margin-left:14%;"><b>archive_write_set_format_pax</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
118
119<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
120
121
122<p style="margin-left:14%;"><b>archive_write_set_format_pax_restricted</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
123
124<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
125
126
127<p style="margin-left:14%;"><b>archive_write_set_format_shar</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
128
129<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
130
131
132<p style="margin-left:14%;"><b>archive_write_set_format_shar_binary</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
133
134<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
135
136
137<p style="margin-left:14%;"><b>archive_write_set_format_ustar</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
138
139<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
140
141
142<p style="margin-left:14%;"><b>archive_write_set_format_options</b>(<i>struct&nbsp;archive&nbsp;*</i>,
143<i>const&nbsp;char&nbsp;*</i>);</p>
144
145<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
146
147
148<p style="margin-left:14%;"><b>archive_write_set_compressor_options</b>(<i>struct&nbsp;archive&nbsp;*</i>,
149<i>const&nbsp;char&nbsp;*</i>);</p>
150
151<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
152
153
154<p style="margin-left:14%;"><b>archive_write_set_options</b>(<i>struct&nbsp;archive&nbsp;*</i>,
155<i>const&nbsp;char&nbsp;*</i>);</p>
156
157<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
158
159
160<p valign="top"><b>archive_write_open</b>(<i>struct&nbsp;archive&nbsp;*</i>,
161<i>void&nbsp;*client_data</i>,
162<i>archive_open_callback&nbsp;*</i>,
163<i>archive_write_callback&nbsp;*</i>,
164<i>archive_close_callback&nbsp;*</i>);</p>
165
166<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
167
168
169<p style="margin-left:14%;"><b>archive_write_open_fd</b>(<i>struct&nbsp;archive&nbsp;*</i>,
170<i>int&nbsp;fd</i>);</p>
171
172<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
173
174
175<p style="margin-left:14%;"><b>archive_write_open_FILE</b>(<i>struct&nbsp;archive&nbsp;*</i>,
176<i>FILE&nbsp;*file</i>);</p>
177
178<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
179
180
181<p style="margin-left:14%;"><b>archive_write_open_filename</b>(<i>struct&nbsp;archive&nbsp;*</i>,
182<i>const&nbsp;char&nbsp;*filename</i>);</p>
183
184<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
185
186
187<p valign="top"><b>archive_write_open_memory</b>(<i>struct&nbsp;archive&nbsp;*</i>,
188<i>void&nbsp;*buffer</i>, <i>size_t&nbsp;bufferSize</i>,
189<i>size_t&nbsp;*outUsed</i>);</p>
190
191<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
192
193
194<p style="margin-left:14%;"><b>archive_write_header</b>(<i>struct&nbsp;archive&nbsp;*</i>,
195<i>struct&nbsp;archive_entry&nbsp;*</i>);</p>
196
197
198<p style="margin-left:8%; margin-top: 1em"><i>ssize_t</i></p>
199
200
201<p style="margin-left:14%;"><b>archive_write_data</b>(<i>struct&nbsp;archive&nbsp;*</i>,
202<i>const&nbsp;void&nbsp;*</i>, <i>size_t</i>);</p>
203
204<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
205
206
207<p style="margin-left:14%;"><b>archive_write_finish_entry</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
208
209<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
210
211
212<p style="margin-left:14%;"><b>archive_write_close</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
213
214<p style="margin-left:8%; margin-top: 1em"><i>int</i></p>
215
216
217<p style="margin-left:14%;"><b>archive_write_finish</b>(<i>struct&nbsp;archive&nbsp;*</i>);</p>
218
219
220<p style="margin-top: 1em" valign="top"><b>DESCRIPTION</b></p>
221
222<p style="margin-left:8%;">These functions provide a
223complete API for creating streaming archive files. The
224general process is to first create the struct archive
225object, set any desired options, initialize the archive,
226append entries, then close the archive and release all
227resources. The following summary describes the functions in
228approximately the order they are ordinarily used:</p>
229
230
231<p style="margin-top: 1em" valign="top"><b>archive_write_new</b>()</p>
232
233<p style="margin-left:20%;">Allocates and initializes a
234struct archive object suitable for writing a tar
235archive.</p>
236
237
238<p style="margin-top: 1em" valign="top"><b>archive_write_set_bytes_per_block</b>()</p>
239
240<p style="margin-left:20%;">Sets the block size used for
241writing the archive data. Every call to the write callback
242function, except possibly the last one, will use this value
243for the length. The third parameter is a boolean that
244specifies whether or not the final block written will be
245padded to the full block size. If it is zero, the last block
246will not be padded. If it is non-zero, padding will be added
247both before and after compression. The default is to use a
248block size of 10240 bytes and to pad the last block. Note
249that a block size of zero will suppress internal blocking
250and cause writes to be sent directly to the write callback
251as they occur.</p>
252
253
254<p style="margin-top: 1em" valign="top"><b>archive_write_get_bytes_per_block</b>()</p>
255
256<p style="margin-left:20%;">Retrieve the block size to be
257used for writing. A value of -1 here indicates that the
258library should use default values. A value of zero indicates
259that internal blocking is suppressed.</p>
260
261
262<p style="margin-top: 1em" valign="top"><b>archive_write_set_bytes_in_last_block</b>()</p>
263
264<p style="margin-left:20%;">Sets the block size used for
265writing the last block. If this value is zero, the last
266block will be padded to the same size as the other blocks.
267Otherwise, the final block will be padded to a multiple of
268this size. In particular, setting it to 1 will cause the
269final block to not be padded. For compressed output, any
270padding generated by this option is applied only after the
271compression. The uncompressed data is always unpadded. The
272default is to pad the last block to the full block size
273(note that <b>archive_write_open_filename</b>() will set
274this based on the file type). Unlike the other
275&lsquo;&lsquo;set&rsquo;&rsquo; functions, this function can
276be called after the archive is opened.</p>
277
278
279<p style="margin-top: 1em" valign="top"><b>archive_write_get_bytes_in_last_block</b>()</p>
280
281<p style="margin-left:20%;">Retrieve the currently-set
282value for last block size. A value of -1 here indicates that
283the library should use default values.</p>
284
285
286<p style="margin-top: 1em" valign="top"><b>archive_write_set_format_cpio</b>(),
287<b>archive_write_set_format_pax</b>(),
288<b>archive_write_set_format_pax_restricted</b>(),
289<b>archive_write_set_format_shar</b>(),
290<b>archive_write_set_format_shar_binary</b>(),
291<b>archive_write_set_format_ustar</b>()</p>
292
293<p style="margin-left:20%;">Sets the format that will be
294used for the archive. The library can write POSIX
295octet-oriented cpio format archives, POSIX-standard
296&lsquo;&lsquo;pax interchange&rsquo;&rsquo; format archives,
297traditional &lsquo;&lsquo;shar&rsquo;&rsquo; archives,
298enhanced &lsquo;&lsquo;binary&rsquo;&rsquo; shar archives
299that store a variety of file attributes and handle binary
300files, and POSIX-standard &lsquo;&lsquo;ustar&rsquo;&rsquo;
301archives. The pax interchange format is a
302backwards-compatible tar format that adds key/value
303attributes to each entry and supports arbitrary filenames,
304linknames, uids, sizes, etc. &lsquo;&lsquo;Restricted pax
305interchange format&rsquo;&rsquo; is the library default;
306this is the same as pax format, but suppresses the pax
307extended header for most normal files. In most cases, this
308will result in ordinary ustar archives.</p>
309
310
311<p style="margin-top: 1em" valign="top"><b>archive_write_set_compression_bzip2</b>(),
312<b>archive_write_set_compression_compress</b>(),
313<b>archive_write_set_compression_gzip</b>(),
314<b>archive_write_set_compression_none</b>()</p>
315
316<p style="margin-left:20%;">The resulting archive will be
317compressed as specified. Note that the compressed output is
318always properly blocked.</p>
319
320
321<p style="margin-top: 1em" valign="top"><b>archive_write_set_compression_program</b>()</p>
322
323<p style="margin-left:20%;">The archive will be fed into
324the specified compression program. The output of that
325program is blocked and written to the client write
326callbacks.</p>
327
328
329<p style="margin-top: 1em" valign="top"><b>archive_write_set_compressor_options</b>(),
330<b>archive_write_set_format_options</b>(),
331<b>archive_write_set_options</b>()</p>
332
333<p style="margin-left:20%;">Specifies options that will be
334passed to the currently-enabled compressor and/or format
335writer. The argument is a comma-separated list of individual
336options. Individual options have one of the following
337forms:</p>
338
339<p valign="top"><i>option=value</i></p>
340
341<p style="margin-left:32%;">The option/value pair will be
342provided to every module. Modules that do not accept an
343option with this name will ignore it.</p>
344
345<p valign="top"><i>option</i></p>
346
347<p style="margin-left:32%; margin-top: 1em">The option will
348be provided to every module with a value of
349&lsquo;&lsquo;1&rsquo;&rsquo;.</p>
350
351<p valign="top"><i>!option</i></p>
352
353<p style="margin-left:32%;">The option will be provided to
354every module with a NULL value.</p>
355
356<p valign="top"><i>module:option=value</i>,
357<i>module:option</i>, <i>module:!option</i></p>
358
359<p style="margin-left:32%;">As above, but the corresponding
360option and value will be provided only to modules whose name
361matches <i>module</i>.</p>
362
363<p style="margin-left:20%;">The return value will be
364<b>ARCHIVE_OK</b> if any module accepts the option, or
365<b>ARCHIVE_WARN</b> if no module accepted the option, or
366<b>ARCHIVE_FATAL</b> if there was a fatal error while
367attempting to process the option.</p>
368
369<p style="margin-left:20%; margin-top: 1em">The currently
370supported options are:</p>
371
372<p valign="top">Compressor gzip <b><br>
373compression-level</b></p>
374
375<p style="margin-left:45%;">The value is interpreted as a
376decimal integer specifying the gzip compression level.</p>
377
378<p valign="top">Compressor xz <b><br>
379compression-level</b></p>
380
381<p style="margin-left:45%;">The value is interpreted as a
382decimal integer specifying the compression level.</p>
383
384<p valign="top">Format mtree <b><br>
385cksum</b>, <b>device</b>, <b>flags</b>, <b>gid</b>,
386<b>gname</b>, <b>indent</b>, <b>link</b>, <b>md5</b>,
387<b>mode</b>, <b>nlink</b>, <b>rmd160</b>, <b>sha1</b>,
388<b>sha256</b>, <b>sha384</b>, <b>sha512</b>, <b>size</b>,
389<b>time</b>, <b>uid</b>, <b>uname</b></p>
390
391<p style="margin-left:45%;">Enable a particular keyword in
392the mtree output. Prefix with an exclamation mark to disable
393the corresponding keyword. The default is equivalent to
394&lsquo;&lsquo;device, flags, gid, gname, link, mode, nlink,
395size, time, type, uid, uname&rsquo;&rsquo;.</p>
396
397<p valign="top"><b>all</b></p>
398
399<p style="margin-left:45%; margin-top: 1em">Enables all of
400the above keywords.</p>
401
402<p valign="top"><b>use-set</b></p>
403
404<p style="margin-left:45%;">Enables generation of
405<b>/set</b> lines that specify default values for the
406following files and/or directories.</p>
407
408<p valign="top"><b>indent</b></p>
409
410<p style="margin-left:45%; margin-top: 1em">XXX needs
411explanation XXX</p>
412
413
414<p style="margin-top: 1em" valign="top"><b>archive_write_open</b>()</p>
415
416<p style="margin-left:20%;">Freeze the settings, open the
417archive, and prepare for writing entries. This is the most
418generic form of this function, which accepts pointers to
419three callback functions which will be invoked by the
420compression layer to write the constructed archive.</p>
421
422
423<p style="margin-top: 1em" valign="top"><b>archive_write_open_fd</b>()</p>
424
425<p style="margin-left:20%;">A convenience form of
426<b>archive_write_open</b>() that accepts a file descriptor.
427The <b>archive_write_open_fd</b>() function is safe for use
428with tape drives or other block-oriented devices.</p>
429
430
431<p style="margin-top: 1em" valign="top"><b>archive_write_open_FILE</b>()</p>
432
433<p style="margin-left:20%;">A convenience form of
434<b>archive_write_open</b>() that accepts a <i>FILE *</i>
435pointer. Note that <b>archive_write_open_FILE</b>() is not
436safe for writing to tape drives or other devices that
437require correct blocking.</p>
438
439
440<p style="margin-top: 1em" valign="top"><b>archive_write_open_file</b>()</p>
441
442<p style="margin-left:20%;">A deprecated synonym for
443<b>archive_write_open_filename</b>().</p>
444
445
446<p style="margin-top: 1em" valign="top"><b>archive_write_open_filename</b>()</p>
447
448<p style="margin-left:20%;">A convenience form of
449<b>archive_write_open</b>() that accepts a filename. A NULL
450argument indicates that the output should be written to
451standard output; an argument of
452&lsquo;&lsquo;-&rsquo;&rsquo; will open a file with that
453name. If you have not invoked
454<b>archive_write_set_bytes_in_last_block</b>(), then
455<b>archive_write_open_filename</b>() will adjust the
456last-block padding depending on the file: it will enable
457padding when writing to standard output or to a character or
458block device node, it will disable padding otherwise. You
459can override this by manually invoking
460<b>archive_write_set_bytes_in_last_block</b>() before
461calling <b>archive_write_open</b>(). The
462<b>archive_write_open_filename</b>() function is safe for
463use with tape drives or other block-oriented devices.</p>
464
465
466<p style="margin-top: 1em" valign="top"><b>archive_write_open_memory</b>()</p>
467
468<p style="margin-left:20%;">A convenience form of
469<b>archive_write_open</b>() that accepts a pointer to a
470block of memory that will receive the archive. The final
471<i>size_t *</i> argument points to a variable that will be
472updated after each write to reflect how much of the buffer
473is currently in use. You should be careful to ensure that
474this variable remains allocated until after the archive is
475closed.</p>
476
477
478<p style="margin-top: 1em" valign="top"><b>archive_write_header</b>()</p>
479
480<p style="margin-left:20%;">Build and write a header using
481the data in the provided struct archive_entry structure. See
482archive_entry(3) for information on creating and populating
483struct archive_entry objects.</p>
484
485
486<p style="margin-top: 1em" valign="top"><b>archive_write_data</b>()</p>
487
488<p style="margin-left:20%;">Write data corresponding to the
489header just written. Returns number of bytes written or -1
490on error.</p>
491
492
493<p style="margin-top: 1em" valign="top"><b>archive_write_finish_entry</b>()</p>
494
495<p style="margin-left:20%;">Close out the entry just
496written. In particular, this writes out the final padding
497required by some formats. Ordinarily, clients never need to
498call this, as it is called automatically by
499<b>archive_write_next_header</b>() and
500<b>archive_write_close</b>() as needed.</p>
501
502
503<p style="margin-top: 1em" valign="top"><b>archive_write_close</b>()</p>
504
505<p style="margin-left:20%;">Complete the archive and invoke
506the close callback.</p>
507
508
509<p style="margin-top: 1em" valign="top"><b>archive_write_finish</b>()</p>
510
511<p style="margin-left:20%;">Invokes
512<b>archive_write_close</b>() if it was not invoked manually,
513then releases all resources. Note that this function was
514declared to return <i>void</i> in libarchive 1.x, which made
515it impossible to detect errors when
516<b>archive_write_close</b>() was invoked implicitly from
517this function. This is corrected beginning with libarchive
5182.0.</p>
519
520<p style="margin-left:8%;">More information about the
521<i>struct archive</i> object and the overall design of the
522library can be found in the libarchive(3) overview.</p>
523
524
525<p style="margin-top: 1em" valign="top"><b>IMPLEMENTATION</b></p>
526
527<p style="margin-left:8%;">Compression support is built-in
528to libarchive, which uses zlib and bzlib to handle gzip and
529bzip2 compression, respectively.</p>
530
531<p style="margin-top: 1em" valign="top"><b>CLIENT
532CALLBACKS</b></p>
533
534<p style="margin-left:8%;">To use this library, you will
535need to define and register callback functions that will be
536invoked to write data to the resulting archive. These
537functions are registered by calling
538<b>archive_write_open</b>():</p>
539
540<p style="margin-left:17%; margin-top: 1em"><i>typedef
541int</i> <b>archive_open_callback</b>(<i>struct archive
542*</i>, <i>void *client_data</i>)</p>
543
544<p style="margin-left:8%; margin-top: 1em">The open
545callback is invoked by <b>archive_write_open</b>(). It
546should return <b>ARCHIVE_OK</b> if the underlying file or
547data source is successfully opened. If the open fails, it
548should call <b>archive_set_error</b>() to register an error
549code and message and return <b>ARCHIVE_FATAL</b>.</p>
550
551<p style="margin-left:17%; margin-top: 1em"><i>typedef
552ssize_t</i></p>
553
554
555<p valign="top"><b>archive_write_callback</b>(<i>struct&nbsp;archive&nbsp;*</i>,
556<i>void&nbsp;*client_data</i>,
557<i>const&nbsp;void&nbsp;*buffer</i>,
558<i>size_t&nbsp;length</i>)</p>
559
560<p style="margin-left:8%; margin-top: 1em">The write
561callback is invoked whenever the library needs to write raw
562bytes to the archive. For correct blocking, each call to the
563write callback function should translate into a single
564write(2) system call. This is especially critical when
565writing archives to tape drives. On success, the write
566callback should return the number of bytes actually written.
567On error, the callback should invoke
568<b>archive_set_error</b>() to register an error code and
569message and return -1.</p>
570
571<p style="margin-left:17%; margin-top: 1em"><i>typedef
572int</i> <b>archive_close_callback</b>(<i>struct archive
573*</i>, <i>void *client_data</i>)</p>
574
575<p style="margin-left:8%; margin-top: 1em">The close
576callback is invoked by archive_close when the archive
577processing is complete. The callback should return
578<b>ARCHIVE_OK</b> on success. On failure, the callback
579should invoke <b>archive_set_error</b>() to register an
580error code and message and return <b>ARCHIVE_FATAL.</b></p>
581
582<p style="margin-top: 1em" valign="top"><b>EXAMPLE</b></p>
583
584<p style="margin-left:8%;">The following sketch illustrates
585basic usage of the library. In this example, the callback
586functions are simply wrappers around the standard open(2),
587write(2), and close(2) system calls.</p>
588
589<p style="margin-left:17%; margin-top: 1em">#ifdef
590__linux__</p>
591
592<table width="100%" border=0 rules="none" frame="void"
593       cellspacing="0" cellpadding="0">
594<tr valign="top" align="left">
595<td width="17%"></td>
596<td width="12%">
597
598
599<p valign="top">#define</p></td>
600<td width="13%">
601
602
603<p valign="top">_FILE_OFFSET_BITS 64</p></td>
604<td width="58%">
605</td>
606</table>
607
608<p style="margin-left:17%;">#endif <br>
609#include &lt;sys/stat.h&gt; <br>
610#include &lt;archive.h&gt; <br>
611#include &lt;archive_entry.h&gt; <br>
612#include &lt;fcntl.h&gt; <br>
613#include &lt;stdlib.h&gt; <br>
614#include &lt;unistd.h&gt;</p>
615
616<p style="margin-left:17%; margin-top: 1em">struct mydata
617{</p>
618
619<table width="100%" border=0 rules="none" frame="void"
620       cellspacing="0" cellpadding="0">
621<tr valign="top" align="left">
622<td width="29%"></td>
623<td width="71%">
624
625
626<p valign="top">const char *name;</p></td>
627<tr valign="top" align="left">
628<td width="29%"></td>
629<td width="71%">
630
631
632<p valign="top">int fd;</p></td>
633</table>
634
635<p style="margin-left:17%;">};</p>
636
637<p style="margin-left:17%; margin-top: 1em">int <br>
638myopen(struct archive *a, void *client_data) <br>
639{ <br>
640struct mydata *mydata = client_data;</p>
641
642<p style="margin-left:17%; margin-top: 1em">mydata-&gt;fd =
643open(mydata-&gt;name, O_WRONLY | O_CREAT, 0644); <br>
644if (mydata-&gt;fd &gt;= 0) <br>
645return (ARCHIVE_OK); <br>
646else <br>
647return (ARCHIVE_FATAL); <br>
648}</p>
649
650<p style="margin-left:17%; margin-top: 1em">ssize_t <br>
651mywrite(struct archive *a, void *client_data, const void
652*buff, size_t n) <br>
653{ <br>
654struct mydata *mydata = client_data;</p>
655
656<p style="margin-left:17%; margin-top: 1em">return
657(write(mydata-&gt;fd, buff, n)); <br>
658}</p>
659
660<p style="margin-left:17%; margin-top: 1em">int <br>
661myclose(struct archive *a, void *client_data) <br>
662{ <br>
663struct mydata *mydata = client_data;</p>
664
665<p style="margin-left:17%; margin-top: 1em">if
666(mydata-&gt;fd &gt; 0) <br>
667close(mydata-&gt;fd); <br>
668return (0); <br>
669}</p>
670
671<p style="margin-left:17%; margin-top: 1em">void <br>
672write_archive(const char *outname, const char **filename)
673<br>
674{ <br>
675struct mydata *mydata = malloc(sizeof(struct mydata)); <br>
676struct archive *a; <br>
677struct archive_entry *entry; <br>
678struct stat st; <br>
679char buff[8192]; <br>
680int len; <br>
681int fd;</p>
682
683<p style="margin-left:17%; margin-top: 1em">a =
684archive_write_new(); <br>
685mydata-&gt;name = outname; <br>
686archive_write_set_compression_gzip(a); <br>
687archive_write_set_format_ustar(a); <br>
688archive_write_open(a, mydata, myopen, mywrite, myclose);
689<br>
690while (*filename) { <br>
691stat(*filename, &amp;st); <br>
692entry = archive_entry_new(); <br>
693archive_entry_copy_stat(entry, &amp;st); <br>
694archive_entry_set_pathname(entry, *filename); <br>
695archive_write_header(a, entry); <br>
696fd = open(*filename, O_RDONLY); <br>
697len = read(fd, buff, sizeof(buff)); <br>
698while ( len &gt; 0 ) {</p>
699
700<table width="100%" border=0 rules="none" frame="void"
701       cellspacing="0" cellpadding="0">
702<tr valign="top" align="left">
703<td width="29%"></td>
704<td width="71%">
705
706
707<p valign="top">archive_write_data(a, buff, len);</p></td>
708<tr valign="top" align="left">
709<td width="29%"></td>
710<td width="71%">
711
712
713<p valign="top">len = read(fd, buff, sizeof(buff));</p></td>
714</table>
715
716<p style="margin-left:17%;">} <br>
717archive_entry_free(entry); <br>
718filename++; <br>
719} <br>
720archive_write_finish(a); <br>
721}</p>
722
723<p style="margin-left:17%; margin-top: 1em">int main(int
724argc, const char **argv) <br>
725{</p>
726
727<table width="100%" border=0 rules="none" frame="void"
728       cellspacing="0" cellpadding="0">
729<tr valign="top" align="left">
730<td width="29%"></td>
731<td width="71%">
732
733
734<p valign="top">const char *outname;</p></td>
735<tr valign="top" align="left">
736<td width="29%"></td>
737<td width="71%">
738
739
740<p valign="top">argv++;</p></td>
741<tr valign="top" align="left">
742<td width="29%"></td>
743<td width="71%">
744
745
746<p valign="top">outname = argv++;</p></td>
747<tr valign="top" align="left">
748<td width="29%"></td>
749<td width="71%">
750
751
752<p valign="top">write_archive(outname, argv);</p></td>
753<tr valign="top" align="left">
754<td width="29%"></td>
755<td width="71%">
756
757
758<p valign="top">return 0;</p></td>
759</table>
760
761<p style="margin-left:17%;">}</p>
762
763<p style="margin-top: 1em" valign="top"><b>RETURN
764VALUES</b></p>
765
766<p style="margin-left:8%;">Most functions return
767<b>ARCHIVE_OK</b> (zero) on success, or one of several
768non-zero error codes for errors. Specific error codes
769include: <b>ARCHIVE_RETRY</b> for operations that might
770succeed if retried, <b>ARCHIVE_WARN</b> for unusual
771conditions that do not prevent further operations, and
772<b>ARCHIVE_FATAL</b> for serious errors that make remaining
773operations impossible. The <b>archive_errno</b>() and
774<b>archive_error_string</b>() functions can be used to
775retrieve an appropriate error code and a textual error
776message.</p>
777
778
779<p style="margin-left:8%; margin-top: 1em"><b>archive_write_new</b>()
780returns a pointer to a newly-allocated struct archive
781object.</p>
782
783
784<p style="margin-left:8%; margin-top: 1em"><b>archive_write_data</b>()
785returns a count of the number of bytes actually written. On
786error, -1 is returned and the <b>archive_errno</b>() and
787<b>archive_error_string</b>() functions will return
788appropriate values. Note that if the client-provided write
789callback function returns a non-zero value, that error will
790be propagated back to the caller through whatever API
791function resulted in that call, which may include
792<b>archive_write_header</b>(), <b>archive_write_data</b>(),
793<b>archive_write_close</b>(), or
794<b>archive_write_finish</b>(). The client callback can call
795<b>archive_set_error</b>() to provide values that can then
796be retrieved by <b>archive_errno</b>() and
797<b>archive_error_string</b>().</p>
798
799<p style="margin-top: 1em" valign="top"><b>SEE ALSO</b></p>
800
801<p style="margin-left:8%;">tar(1), libarchive(3),
802tar(5)</p>
803
804<p style="margin-top: 1em" valign="top"><b>HISTORY</b></p>
805
806<p style="margin-left:8%;">The <b>libarchive</b> library
807first appeared in FreeBSD&nbsp;5.3.</p>
808
809<p style="margin-top: 1em" valign="top"><b>AUTHORS</b></p>
810
811<p style="margin-left:8%;">The <b>libarchive</b> library
812was written by Tim Kientzle
813&lang;kientzle@acm.org&rang;.</p>
814
815<p style="margin-top: 1em" valign="top"><b>BUGS</b></p>
816
817<p style="margin-left:8%;">There are many peculiar bugs in
818historic tar implementations that may cause certain programs
819to reject archives written by this library. For example,
820several historic implementations calculated header checksums
821incorrectly and will thus reject valid archives; GNU tar
822does not fully support pax interchange format; some old tar
823implementations required specific field terminations.</p>
824
825<p style="margin-left:8%; margin-top: 1em">The default pax
826interchange format eliminates most of the historic tar
827limitations and provides a generic key/value attribute
828facility for vendor-defined extensions. One oversight in
829POSIX is the failure to provide a standard attribute for
830large device numbers. This library uses
831&lsquo;&lsquo;SCHILY.devminor&rsquo;&rsquo; and
832&lsquo;&lsquo;SCHILY.devmajor&rsquo;&rsquo; for device
833numbers that exceed the range supported by the
834backwards-compatible ustar header. These keys are compatible
835with Joerg Schilling&rsquo;s <b>star</b> archiver. Other
836implementations may not recognize these keys and will thus
837be unable to correctly restore device nodes with large
838device numbers from archives created by this library.</p>
839
840
841<p style="margin-left:8%; margin-top: 1em">FreeBSD&nbsp;9.0
842May&nbsp;11, 2008 FreeBSD&nbsp;9.0</p>
843<hr>
844</body>
845</html>
846