|
NAMElibarchive_internals —
description of libarchive internal interfaces
OVERVIEWThelibarchive library provides a flexible interface for
reading and writing streaming archive files such as tar and cpio. Internally,
it follows a modular layered design that should make it easy to add new
archive and compression formats.
GENERAL ARCHITECTUREExternally, libarchive exposes most operations through an opaque, object-style interface. The archive_entry(3) objects store information about a single filesystem object. The rest of the library provides facilities to write archive_entry(3) objects to archive files, read them from archive files, and write them to disk. (There are plans to add a facility to read archive_entry(3) objects from disk as well.)The read and write APIs each have four layers: a public API layer, a format layer that understands the archive file format, a compression layer, and an I/O layer. The I/O layer is completely exposed to clients who can replace it entirely with their own functions. In order to provide as much consistency as possible for clients, some public functions are virtualized. Eventually, it should be possible for clients to open an archive or disk writer, and then use a single set of code to select and write entries, regardless of the target. READ ARCHITECTUREFrom the outside, clients use the archive_read(3) API to manipulate anarchive object to read entries
and bodies from an archive stream. Internally, the
archive object is cast to an
archive_read object, which holds all read-specific
data. The API has four layers: The lowest layer is the I/O layer. This layer
can be overridden by clients, but most clients use the packaged I/O callbacks
provided, for example, by
archive_read_open_memory(3),
and
archive_read_open_fd(3).
The compression layer calls the I/O layer to read bytes and decompresses them
for the format layer. The format layer unpacks a stream of uncompressed bytes
and creates archive_entry objects from the incoming
data. The API layer tracks overall state (for example, it prevents clients
from reading data before reading a header) and invokes the format and
compression layer operations through registered function pointers. In
particular, the API layer drives the format-detection process: When opening
the archive, it reads an initial block of data and offers it to each
registered compression handler. The one with the highest bid is initialized
with the first block. Similarly, the format handlers are polled to see which
handler is the best for each archive. (Prior to 2.4.0, the format bidders were
invoked for each entry, but this design hindered error recovery.)
I/O Layer and Client CallbacksThe read API goes to some lengths to be nice to clients. As a result, there are few restrictions on the behavior of the client callbacks.The client read callback is expected to provide a block of data on each call. A zero-length return does indicate end of file, but otherwise blocks may be as small as one byte or as large as the entire file. In particular, blocks may be of different sizes. The client skip callback returns the number of bytes actually skipped, which may be much smaller than the skip requested. The only requirement is that the skip not be larger. In particular, clients are allowed to return zero for any skip that they don't want to handle. The skip callback must never be invoked with a negative value. Keep in mind that not all clients are reading from disk: clients reading from networks may provide different-sized blocks on every request and cannot skip at all; advanced clients may use mmap(2) to read the entire file into memory at once and return the entire file to libarchive as a single block; other clients may begin asynchronous I/O operations for the next block on each request. Decompresssion LayerThe decompression layer not only handles decompression, it also buffers data so that the format handlers see a much nicer I/O model. The decompression API is a two stage peek/consume model. A read_ahead request specifies a minimum read amount; the decompression layer must provide a pointer to at least that much data. If more data is immediately available, it should return more: the format layer handles bulk data reads by asking for a minimum of one byte and then copying as much data as is available.A subsequent call to the Skip requests must always be handled exactly. Decompression handlers that cannot seek forward should not register a skip handler; the API layer fills in a generic skip handler that reads and discards data. A decompression handler has a specific lifecycle:
Format LayerThe read formats have a similar lifecycle to the decompression handlers:
API LayerXXX to do XXXWRITE ARCHITECTUREThe write API has a similar set of four layers: an API layer, a format layer, a compression layer, and an I/O layer. The registration here is much simpler because only one format and one compression can be registered at a time.I/O Layer and Client CallbacksXXX To be written XXXCompression LayerXXX To be written XXXFormat LayerXXX To be written XXXAPI LayerXXX To be written XXXWRITE_DISK ARCHITECTUREThe write_disk API is intended to look just like the write API to clients. Since it does not handle multiple formats or compression, it is not layered internally.GENERAL SERVICESThearchive_read , archive_write ,
and archive_write_disk objects all contain an initial
archive object which provides common support for a set
of standard services. (Recall that ANSI/ISO C90 guarantees that you can cast
freely between a pointer to a structure and a pointer to the first element of
that structure.) The archive object has a magic value
that indicates which API this object is associated with, slots for storing
error information, and function pointers for virtualized API functions.
MISCELLANEOUS NOTESConnecting existing archiving libraries into libarchive is generally quite difficult. In particular, many existing libraries strongly assume that you are reading from a file; they seek forwards and backwards as necessary to locate various pieces of information. In contrast, libarchive never seeks backwards in its input, which sometimes requires very different approaches.For example, libarchive's ISO9660 support operates very differently from most ISO9660 readers. The libarchive support utilizes a work-queue design that keeps a list of known entries sorted by their location in the input. Whenever libarchive's ISO9660 implementation is asked for the next header, checks this list to find the next item on the disk. Directories are parsed when they are encountered and new items are added to the list. This design relies heavily on the ISO9660 image being optimized so that directories always occur earlier on the disk than the files they describe. Depending on the specific format, such approaches may not be possible. The ZIP format specification, for example, allows archivers to store key information only at the end of the file. In theory, it is possible to create ZIP archives that cannot be read without seeking. Fortunately, such archives are very rare, and libarchive can read most ZIP archives, though it cannot always extract as much information as a dedicated ZIP program. SEE ALSOarchive_entry(3), archive_read(3), archive_write(3), archive_write_disk(3), libarchive(3)HISTORYThelibarchive library first appeared in
FreeBSD 5.3.
AUTHORSThelibarchive library was written by
Tim Kientzle ⟨kientzle@acm.org⟩.
Visit the GSP FreeBSD Man Page Interface. |