archive_read_newarchive_read_support_compression_allarchive_read_support_compression_bzip2archive_read_support_compression_compressarchive_read_support_compression_gziparchive_read_support_compression_nonearchive_read_support_compression_programarchive_read_support_format_allarchive_read_support_format_cpioarchive_read_support_format_emptyarchive_read_support_format_iso9660archive_read_support_format_tararchive_read_support_format_ziparchive_read_openarchive_read_open2archive_read_open_fdarchive_read_open_FILEarchive_read_open_filenamearchive_read_open_memoryarchive_read_next_headerarchive_read_dataarchive_read_data_blockarchive_read_data_skiparchive_read_data_into_bufferarchive_read_data_into_fdarchive_read_extractarchive_read_extract2archive_read_extract_set_progress_callbackarchive_read_closearchive_read_finish
- functions for reading streaming archives
SYNOPSIS
#include <archive.h> struct archive *
archive_read_new (void); int
archive_read_support_compression_all (struct archive *); int
archive_read_support_compression_bzip2 (struct archive *); int
archive_read_support_compression_compress (struct archive *); int
archive_read_support_compression_gzip (struct archive *); int
archive_read_support_compression_none (struct archive *); int
Fo archive_read_support_compression_program
Fa struct archive *
Fa const char *cmd
Fc Ft int
archive_read_support_format_all (struct archive *); int
archive_read_support_format_cpio (struct archive *); int
archive_read_support_format_empty (struct archive *); int
archive_read_support_format_iso9660 (struct archive *); int
archive_read_support_format_tar (struct archive *); int
archive_read_support_format_zip (struct archive *); int
Fo archive_read_open
Fa struct archive *
Fa void *client_data
Fa archive_open_callback *
Fa archive_read_callback *
Fa archive_close_callback *
Fc Ft int
Fo archive_read_open2
Fa struct archive *
Fa void *client_data
Fa archive_open_callback *
Fa archive_read_callback *
Fa archive_skip_callback *
Fa archive_close_callback *
Fc Ft int
archive_read_open_FILE (struct archive * FILE *file); int
archive_read_open_fd (struct archive * int fd size_t block_size); int
Fo archive_read_open_filename
Fa struct archive *
Fa const char *filename
Fa size_t block_size
Fc Ft int
archive_read_open_memory (struct archive * void *buff size_t size); int
archive_read_next_header (struct archive * struct archive_entry **); ssize_t
archive_read_data (struct archive * void *buff size_t len); int
Fo archive_read_data_block
Fa struct archive *
Fa const void **buff
Fa size_t *len
Fa off_t *offset
Fc Ft int
archive_read_data_skip (struct archive *); int
archive_read_data_into_buffer (struct archive * void * ssize_t len); int
archive_read_data_into_fd (struct archive * int fd); int
Fo archive_read_extract
Fa struct archive *
Fa struct archive_entry *
Fa int flags
Fc Ft int
Fo archive_read_extract2
Fa struct archive *src
Fa struct archive_entry *
Fa struct archive *dest
Fc Ft void
Fo archive_read_extract_set_progress_callback
Fa struct archive *
Fa void (*func)(void *)
Fa void *user_data
Fc Ft int
archive_read_close (struct archive *); int
archive_read_finish (struct archive *);
DESCRIPTION
These functions provide a complete API for reading streaming archives.
The general process is to first create the
struct archive
object, set options, initialize the reader, iterate over the archive
headers and associated data, then close the archive and release all
resources.
The following summary describes the functions in approximately the
order they would be used:
Fn archive_read_new
Allocates and initializes a
struct archive
object suitable for reading from an archive.
Enables auto-detection code and decompression support for the
specified compression.
Note that
``none''
is always enabled by default.
For convenience,
archive_read_support_compression_all ();
enables all available decompression code.
Fn archive_read_support_compression_program
Data is fed through the specified external program before being dearchived.
Note that this disables automatic detection of the compression format,
so it makes no sense to specify this in conjunction with any other
decompression option.
Enables support---including auto-detection code---for the
specified archive format.
For example,
archive_read_support_format_tar ();
enables support for a variety of standard tar formats, old-style tar,
ustar, pax interchange format, and many common variants.
For convenience,
archive_read_support_format_all ();
enables support for all available formats.
Only empty archives are supported by default.
Fn archive_read_open
The same as
archive_read_open2 (,);
except that the skip callback is assumed to be
NULL
Fn archive_read_open2
Freeze the settings, open the archive, and prepare for reading entries.
This is the most generic version of this call, which accepts
four callback functions.
Most clients will want to use
archive_read_open_filename (,);
archive_read_open_FILE (,);
archive_read_open_fd (,);
or
archive_read_open_memory ();
instead.
The library invokes the client-provided functions to obtain
raw bytes from the archive.
Fn archive_read_open_FILE
Like
archive_read_open (,);
except that it accepts a
FILE *
pointer.
This function should not be used with tape drives or other devices
that require strict I/O blocking.
Fn archive_read_open_fd
Like
archive_read_open (,);
except that it accepts a file descriptor and block size rather than
a set of function pointers.
Note that the file descriptor will not be automatically closed at
end-of-archive.
This function is safe for use with tape drives or other blocked devices.
Fn archive_read_open_file
This is a deprecated synonym for
archive_read_open_filename (.);
Fn archive_read_open_filename
Like
archive_read_open (,);
except that it accepts a simple filename and a block size.
A NULL filename represents standard input.
This function is safe for use with tape drives or other blocked devices.
Fn archive_read_open_memory
Like
archive_read_open (,);
except that it accepts a pointer and size of a block of
memory containing the archive data.
Fn archive_read_next_header
Read the header for the next entry and return a pointer to
a
struct archive_entry
Fn archive_read_data
Read data associated with the header just read.
Internally, this is a convenience function that calls
archive_read_data_block ();
and fills any gaps with nulls so that callers see a single
continuous stream of data.
Fn archive_read_data_block
Return the next available block of data for this entry.
Unlike
archive_read_data (,);
the
archive_read_data_block ();
function avoids copying data and allows you to correctly handle
sparse files, as supported by some archive formats.
The library guarantees that offsets will increase and that blocks
will not overlap.
Note that the blocks returned from this function can be much larger
than the block size read from disk, due to compression
and internal buffer optimizations.
Fn archive_read_data_skip
A convenience function that repeatedly calls
archive_read_data_block ();
to skip all of the data for this archive entry.
Fn archive_read_data_into_buffer
This function is deprecated and will be removed.
Use
archive_read_data ();
instead.
Fn archive_read_data_into_fd
A convenience function that repeatedly calls
archive_read_data_block ();
to copy the entire entry to the provided file descriptor.
A convenience function that wraps the corresponding
archive_write_disk3
interfaces.
The first call to
archive_read_extract ();
creates a restore object using
archive_write_disk_new3
and
archive_write_disk_set_standard_lookup3,
then transparently invokes
archive_write_disk_set_options3,
archive_write_header3,
archive_write_data3,
and
archive_write_finish_entry3
to create the entry on disk and copy data into it.
The
flags
argument is passed unmodified to
archive_write_disk_set_options3.
Fn archive_read_extract2
This is another version of
archive_read_extract ();
that allows you to provide your own restore object.
In particular, this allows you to override the standard lookup functions
using
archive_write_disk_set_group_lookup3,
and
archive_write_disk_set_user_lookup3.
Note that
archive_read_extract2 ();
does not accept a
flags
argument; you should use
archive_write_disk_set_options ();
to set the restore options yourself.
Fn archive_read_extract_set_progress_callback
Sets a pointer to a user-defined callback that can be used
for updating progress displays during extraction.
The progress function will be invoked during the extraction of large
regular files.
The progress function will be invoked with the pointer provided to this call.
Generally, the data pointed to should include a reference to the archive
object and the archive_entry object so that various statistics
can be retrieved for the progress display.
Fn archive_read_close
Complete the archive and invoke the close callback.
Fn archive_read_finish
Invokes
archive_read_close ();
if it was not invoked manually, then release all resources.
Note: In libarchive 1.x, this function was declared to return
void ,
which made it impossible to detect certain errors when
archive_read_close ();
was invoked implicitly from this function.
The declaration is corrected beginning with libarchive 2.0.
Note that the library determines most of the relevant information about
the archive by inspection.
In particular, it automatically detects
gzip(1)
or
bzip2(1)
compression and transparently performs the appropriate decompression.
It also automatically detects the archive format.
A complete description of the
struct archive
and
struct archive_entry
objects can be found in the overview manual page for
libarchive(3).
CLIENT CALLBACKS
The callback functions must match the following prototypes:
Ft typedef ssize_t
Fo archive_read_callback
Fa struct archive *
Fa void *client_data
Fa const void **buffer
Fc
typedef int
Fo archive_skip_callback
Fa struct archive *
Fa void *client_data
Fa size_t request
Fc
The open callback is invoked by
archive_open (.);
It should return
ARCHIVE_OK
if the underlying file or data source is successfully
opened.
If the open fails, it should call
archive_set_error ();
to register an error code and message and return
ARCHIVE_FATAL
The read callback is invoked whenever the library
requires raw bytes from the archive.
The read callback should read data into a buffer,
set the
const void **buffer
argument to point to the available data, and
return a count of the number of bytes available.
The library will invoke the read callback again
only after it has consumed this data.
The library imposes no constraints on the size
of the data blocks returned.
On end-of-file, the read callback should
return zero.
On error, the read callback should invoke
archive_set_error ();
to register an error code and message and
return -1.
The skip callback is invoked when the
library wants to ignore a block of data.
The return value is the number of bytes actually
skipped, which may differ from the request.
If the callback cannot skip data, it should return
zero.
If the skip callback is not provided (the
function pointer is
NULL ),
the library will invoke the read function
instead and simply discard the result.
A skip callback can provide significant
performance gains when reading uncompressed
archives from slow disk drives or other media
that can skip quickly.
The close callback is invoked by archive_close when
the archive processing is complete.
The callback should return
ARCHIVE_OK
on success.
On failure, the callback should invoke
archive_set_error ();
to register an error code and message and
return
ARCHIVE_FATAL.
EXAMPLE
The following illustrates basic usage of the library.
In this example,
the callback functions are simply wrappers around the standard
open(2),
read(2),
and
close(2)
system calls.
Most functions return zero on success, non-zero on error.
The possible return codes include:
ARCHIVE_OK
(the operation succeeded),
ARCHIVE_WARN
(the operation succeeded but a non-critical error was encountered),
ARCHIVE_EOF
(end-of-archive was encountered),
ARCHIVE_RETRY
(the operation failed but can be retried),
and
ARCHIVE_FATAL
(there was a fatal error; the archive should be closed immediately).
Detailed error codes and textual descriptions are available from the
archive_errno ();
and
archive_error_string ();
functions.
archive_read_new ();
returns a pointer to a freshly allocated
struct archive
object.
It returns
NULL
on error.
archive_read_data ();
returns a count of bytes actually read or zero at the end of the entry.
On error, a value of
ARCHIVE_FATALARCHIVE_WARN
or
ARCHIVE_RETRY
is returned and an error code and textual description can be retrieved from the
archive_errno ();
and
archive_error_string ();
functions.
The library expects the client callbacks to behave similarly.
If there is an error, you can use
archive_set_error ();
to set an appropriate error code and description,
then return one of the non-zero values above.
(Note that the value eventually returned to the client may
not be the same; many errors that are not critical at the level
of basic I/O can prevent the archive from being properly read,
thus most I/O errors eventually cause
ARCHIVE_FATAL
to be returned.)
An -nosplit
The
libarchive
library was written by
An Tim Kientzle Aq kientzle@acm.org .
BUGS
Many traditional archiver programs treat
empty files as valid empty archives.
For example, many implementations of
tar(1)
allow you to append entries to an empty file.
Of course, it is impossible to determine the format of an empty file
by inspecting the contents, so this library treats empty files as
having a special
``empty''
format.