Blame doc/text/archive_read.3.txt

Packit 08bd4c
ARCHIVE_READ(3) 	 BSD Library Functions Manual	       ARCHIVE_READ(3)
Packit 08bd4c
Packit 08bd4c
NAME
Packit 08bd4c
     archive_read — functions for reading streaming archives
Packit 08bd4c
Packit 08bd4c
LIBRARY
Packit 08bd4c
     Streaming Archive Library (libarchive, -larchive)
Packit 08bd4c
Packit 08bd4c
SYNOPSIS
Packit 08bd4c
     #include <archive.h>
Packit 08bd4c
Packit 08bd4c
DESCRIPTION
Packit 08bd4c
     These functions provide a complete API for reading streaming archives.
Packit 08bd4c
     The general process is to first create the struct archive object, set
Packit 08bd4c
     options, initialize the reader, iterate over the archive headers and
Packit 08bd4c
     associated data, then close the archive and release all resources.
Packit 08bd4c
Packit 08bd4c
   Create archive object
Packit 08bd4c
     See archive_read_new(3).
Packit 08bd4c
Packit 08bd4c
     To read an archive, you must first obtain an initialized struct archive
Packit 08bd4c
     object from archive_read_new().
Packit 08bd4c
Packit 08bd4c
   Enable filters and formats
Packit 08bd4c
     See archive_read_filter(3) and archive_read_format(3).
Packit 08bd4c
Packit 08bd4c
     You can then modify this object for the desired operations with the vari‐
Packit 08bd4c
     ous archive_read_set_XXX() and archive_read_support_XXX() functions.  In
Packit 08bd4c
     particular, you will need to invoke appropriate
Packit 08bd4c
     archive_read_support_XXX() functions to enable the corresponding compres‐
Packit 08bd4c
     sion and format support.  Note that these latter functions perform two
Packit 08bd4c
     distinct operations: they cause the corresponding support code to be
Packit 08bd4c
     linked into your program, and they enable the corresponding auto-detect
Packit 08bd4c
     code.  Unless you have specific constraints, you will generally want to
Packit 08bd4c
     invoke archive_read_support_filter_all() and
Packit 08bd4c
     archive_read_support_format_all() to enable auto-detect for all formats
Packit 08bd4c
     and compression types currently supported by the library.
Packit 08bd4c
Packit 08bd4c
   Set options
Packit 08bd4c
     See archive_read_set_options(3).
Packit 08bd4c
Packit 08bd4c
   Open archive
Packit 08bd4c
     See archive_read_open(3).
Packit 08bd4c
Packit 08bd4c
     Once you have prepared the struct archive object, you call
Packit 08bd4c
     archive_read_open() to actually open the archive and prepare it for read‐
Packit 08bd4c
     ing.  There are several variants of this function; the most basic expects
Packit 08bd4c
     you to provide pointers to several functions that can provide blocks of
Packit 08bd4c
     bytes from the archive.  There are convenience forms that allow you to
Packit 08bd4c
     specify a filename, file descriptor, FILE * object, or a block of memory
Packit 08bd4c
     from which to read the archive data.  Note that the core library makes no
Packit 08bd4c
     assumptions about the size of the blocks read; callback functions are
Packit 08bd4c
     free to read whatever block size is most appropriate for the medium.
Packit 08bd4c
Packit 08bd4c
   Consume archive
Packit 08bd4c
     See archive_read_header(3), archive_read_data(3) and
Packit 08bd4c
     archive_read_extract(3).
Packit 08bd4c
Packit 08bd4c
     Each archive entry consists of a header followed by a certain amount of
Packit 08bd4c
     data.  You can obtain the next header with archive_read_next_header(),
Packit 08bd4c
     which returns a pointer to an struct archive_entry structure with infor‐
Packit 08bd4c
     mation about the current archive element.	If the entry is a regular
Packit 08bd4c
     file, then the header will be followed by the file data.  You can use
Packit 08bd4c
     archive_read_data() (which works much like the read(2) system call) to
Packit 08bd4c
     read this data from the archive, or archive_read_data_block() which pro‐
Packit 08bd4c
     vides a slightly more efficient interface.  You may prefer to use the
Packit 08bd4c
     higher-level archive_read_data_skip(), which reads and discards the data
Packit 08bd4c
     for this entry, archive_read_data_into_fd(), which copies the data to the
Packit 08bd4c
     provided file descriptor, or archive_read_extract(), which recreates the
Packit 08bd4c
     specified entry on disk and copies data from the archive.	In particular,
Packit 08bd4c
     note that archive_read_extract() uses the struct archive_entry structure
Packit 08bd4c
     that you provide it, which may differ from the entry just read from the
Packit 08bd4c
     archive.  In particular, many applications will want to override the
Packit 08bd4c
     pathname, file permissions, or ownership.
Packit 08bd4c
Packit 08bd4c
   Release resources
Packit 08bd4c
     See archive_read_free(3).
Packit 08bd4c
Packit 08bd4c
     Once you have finished reading data from the archive, you should call
Packit 08bd4c
     archive_read_close() to close the archive, then call archive_read_free()
Packit 08bd4c
     to release all resources, including all memory allocated by the library.
Packit 08bd4c
Packit 08bd4c
EXAMPLE
Packit 08bd4c
     The following illustrates basic usage of the library.  In this example,
Packit 08bd4c
     the callback functions are simply wrappers around the standard open(2),
Packit 08bd4c
     read(2), and close(2) system calls.
Packit 08bd4c
Packit 08bd4c
	   void
Packit 08bd4c
	   list_archive(const char *name)
Packit 08bd4c
	   {
Packit 08bd4c
	     struct mydata *mydata;
Packit 08bd4c
	     struct archive *a;
Packit 08bd4c
	     struct archive_entry *entry;
Packit 08bd4c
Packit 08bd4c
	     mydata = malloc(sizeof(struct mydata));
Packit 08bd4c
	     a = archive_read_new();
Packit 08bd4c
	     mydata->name = name;
Packit 08bd4c
	     archive_read_support_filter_all(a);
Packit 08bd4c
	     archive_read_support_format_all(a);
Packit 08bd4c
	     archive_read_open(a, mydata, myopen, myread, myclose);
Packit 08bd4c
	     while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
Packit 08bd4c
	       printf("%s\n",archive_entry_pathname(entry));
Packit 08bd4c
	       archive_read_data_skip(a);
Packit 08bd4c
	     }
Packit 08bd4c
	     archive_read_free(a);
Packit 08bd4c
	     free(mydata);
Packit 08bd4c
	   }
Packit 08bd4c
Packit 08bd4c
	   la_ssize_t
Packit 08bd4c
	   myread(struct archive *a, void *client_data, const void **buff)
Packit 08bd4c
	   {
Packit 08bd4c
	     struct mydata *mydata = client_data;
Packit 08bd4c
Packit 08bd4c
	     *buff = mydata->buff;
Packit 08bd4c
	     return (read(mydata->fd, mydata->buff, 10240));
Packit 08bd4c
	   }
Packit 08bd4c
Packit 08bd4c
	   int
Packit 08bd4c
	   myopen(struct archive *a, void *client_data)
Packit 08bd4c
	   {
Packit 08bd4c
	     struct mydata *mydata = client_data;
Packit 08bd4c
Packit 08bd4c
	     mydata->fd = open(mydata->name, O_RDONLY);
Packit 08bd4c
	     return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
Packit 08bd4c
	   }
Packit 08bd4c
Packit 08bd4c
	   int
Packit 08bd4c
	   myclose(struct archive *a, void *client_data)
Packit 08bd4c
	   {
Packit 08bd4c
	     struct mydata *mydata = client_data;
Packit 08bd4c
Packit 08bd4c
	     if (mydata->fd > 0)
Packit 08bd4c
	       close(mydata->fd);
Packit 08bd4c
	     return (ARCHIVE_OK);
Packit 08bd4c
	   }
Packit 08bd4c
Packit 08bd4c
SEE ALSO
Packit 08bd4c
     tar(1), libarchive(3), archive_read_new(3), archive_read_data(3),
Packit 08bd4c
     archive_read_extract(3), archive_read_filter(3), archive_read_format(3),
Packit 08bd4c
     archive_read_header(3), archive_read_open(3),
Packit 08bd4c
     archive_read_set_options(3), archive_util(3), tar(5)
Packit 08bd4c
Packit 08bd4c
HISTORY
Packit 08bd4c
     The libarchive library first appeared in FreeBSD 5.3.
Packit 08bd4c
Packit 08bd4c
AUTHORS
Packit 08bd4c
     The libarchive library was written by Tim Kientzle <kientzle@acm.org>.
Packit 08bd4c
Packit 08bd4c
BUGS
Packit 08bd4c
     Many traditional archiver programs treat empty files as valid empty ar‐
Packit 08bd4c
     chives.  For example, many implementations of tar(1) allow you to append
Packit 08bd4c
     entries to an empty file.	Of course, it is impossible to determine the
Packit 08bd4c
     format of an empty file by inspecting the contents, so this library
Packit 08bd4c
     treats empty files as having a special “empty” format.
Packit 08bd4c
Packit 08bd4c
BSD			       February 2, 2012 			   BSD