When reading an archive, the library automatically detects the format and the compression. The library currently has read support for:
The library automatically detects archives compressed with gzip(1), bzip2(1), or compress(1) and decompresses them transparently.
When writing an archive, you can specify the compression to be used and the format to use. The library can write
Pax interchange format is an extension of the tar archive format that eliminates essentially all of the limitations of historic tar formats in a standard fashion that is supported by POSIX-compliant pax(1) implementations on many systems as well as several newer implementations of tar(1). Note that the default write format will suppress the pax extended attributes for most entries; explicitly requesting pax format will enable those attributes for all entries.
The read and write APIs are accessed through the Fn archive_read_XXX functions and the Fn archive_write_XXX functions, respectively, and either can be used independently of the other.
The rest of this manual page provides an overview of the library operation. More detailed information can be found in the individual manual pages for each API or utility function.
Once you have prepared the struct archive object, you call Fn archive_read_open to actually open the archive and prepare it for reading. There are several variants of this function; the most basic expects you to provide pointers to several functions that can provide blocks of bytes from the archive. There are convenience forms that allow you to specify a filename, file descriptor, Ft FILE * object, or a block of memory from which to read the archive data. Note that the core library makes no assumptions about the size of the blocks read; callback functions are free to read whatever block size is most appropriate for the medium.
Each archive entry consists of a header followed by a certain amount of data. You can obtain the next header with Fn archive_read_next_header , which returns a pointer to an struct archive_entry structure with information about the current archive element. If the entry is a regular file, then the header will be followed by the file data. You can use Fn archive_read_data (which works much like the read(2) system call) to read this data from the archive. You may prefer to use the higher-level Fn archive_read_data_skip , which reads and discards the data for this entry, Fn archive_read_data_to_buffer , which reads the data into an in-memory buffer, Fn archive_read_data_to_file , which copies the data to the provided file descriptor, or Fn archive_read_extract , which recreates the specified entry on disk and copies data from the archive. In particular, note that Fn archive_read_extract uses the struct archive_entry structure that you provide it, which may differ from the entry just read from the archive. In particular, many applications will want to override the pathname, file permissions, or ownership.
Once you have finished reading data from the archive, you should call Fn archive_read_close to close the archive, then call Fn archive_read_finish to release all resources, including all memory allocated by the library.
The archive_read3 manual page provides more detailed calling information for this API.
Individual archive entries are written in a three-step process: You first initialize a struct archive_entry structure with information about the new entry. At a minimum, you should set the pathname of the entry and provide a struct stat with a valid st_mode field, which specifies the type of object and st_size field, which specifies the size of the data portion of the object. The Fn archive_write_header function actually writes the header data to the archive. You can then use Fn archive_write_data to write the actual data.
After all entries have been written, use the Fn archive_write_finish function to release all resources.
The archive_write3 manual page provides more detailed calling information for this API.
All of the functions utilize an opaque struct archive datatype that provides access to the archive contents.
The struct archive_entry structure contains a complete description of a single archive entry. It uses an opaque interface that is fully documented in archive_entry3.
Users familiar with historic formats should be aware that the newer variants have eliminated most restrictions on the length of textual fields. Clients should not assume that filenames, link names, user names, or group names are limited in length. In particular, pax interchange format can easily accommodate pathnames in arbitrary character sets that exceed PATH_MAX
Fn archive_read_new and Fn archive_write_new return pointers to an allocated and initialized struct archive object.
Fn archive_read_data and Fn archive_write_data return a count of the number of bytes actually read or written. A value of zero indicates the end of the data for this entry. A negative value indicates an error, in which case the Fn archive_errno and Fn archive_error_string functions can be used to obtain more information.
Conversely, of course, not all of the information that can be stored in an struct archive_entry is supported by all formats. For example, cpio formats do not support nanosecond timestamps; old tar formats do not support large device numbers.