Blame doc/text/archive_read.3.txt

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