Blame doc/text/libarchive_internals.3.txt

Packit 08bd4c
LIBARCHIVE_INTERNALS(3)  BSD Library Functions Manual  LIBARCHIVE_INTERNALS(3)
Packit 08bd4c
Packit 08bd4c
NAME
Packit 08bd4c
     libarchive_internals — description of libarchive internal interfaces
Packit 08bd4c
Packit 08bd4c
OVERVIEW
Packit 08bd4c
     The libarchive library provides a flexible interface for reading and
Packit 08bd4c
     writing streaming archive files such as tar and cpio.  Internally, it
Packit 08bd4c
     follows a modular layered design that should make it easy to add new ar‐
Packit 08bd4c
     chive and compression formats.
Packit 08bd4c
Packit 08bd4c
GENERAL ARCHITECTURE
Packit 08bd4c
     Externally, libarchive exposes most operations through an opaque, object-
Packit 08bd4c
     style interface.  The archive_entry(3) objects store information about a
Packit 08bd4c
     single filesystem object.	The rest of the library provides facilities to
Packit 08bd4c
     write archive_entry(3) objects to archive files, read them from archive
Packit 08bd4c
     files, and write them to disk.  (There are plans to add a facility to
Packit 08bd4c
     read archive_entry(3) objects from disk as well.)
Packit 08bd4c
Packit 08bd4c
     The read and write APIs each have four layers: a public API layer, a for‐
Packit 08bd4c
     mat layer that understands the archive file format, a compression layer,
Packit 08bd4c
     and an I/O layer.	The I/O layer is completely exposed to clients who can
Packit 08bd4c
     replace it entirely with their own functions.
Packit 08bd4c
Packit 08bd4c
     In order to provide as much consistency as possible for clients, some
Packit 08bd4c
     public functions are virtualized.	Eventually, it should be possible for
Packit 08bd4c
     clients to open an archive or disk writer, and then use a single set of
Packit 08bd4c
     code to select and write entries, regardless of the target.
Packit 08bd4c
Packit 08bd4c
READ ARCHITECTURE
Packit 08bd4c
     From the outside, clients use the archive_read(3) API to manipulate an
Packit 08bd4c
     archive object to read entries and bodies from an archive stream.	Inter‐
Packit 08bd4c
     nally, the archive object is cast to an archive_read object, which holds
Packit 08bd4c
     all read-specific data.  The API has four layers: The lowest layer is the
Packit 08bd4c
     I/O layer.  This layer can be overridden by clients, but most clients use
Packit 08bd4c
     the packaged I/O callbacks provided, for example, by
Packit 08bd4c
     archive_read_open_memory(3), and archive_read_open_fd(3).	The compres‐
Packit 08bd4c
     sion layer calls the I/O layer to read bytes and decompresses them for
Packit 08bd4c
     the format layer.	The format layer unpacks a stream of uncompressed
Packit 08bd4c
     bytes and creates archive_entry objects from the incoming data.  The API
Packit 08bd4c
     layer tracks overall state (for example, it prevents clients from reading
Packit 08bd4c
     data before reading a header) and invokes the format and compression
Packit 08bd4c
     layer operations through registered function pointers.  In particular,
Packit 08bd4c
     the API layer drives the format-detection process: When opening the ar‐
Packit 08bd4c
     chive, it reads an initial block of data and offers it to each registered
Packit 08bd4c
     compression handler.  The one with the highest bid is initialized with
Packit 08bd4c
     the first block.  Similarly, the format handlers are polled to see which
Packit 08bd4c
     handler is the best for each archive.  (Prior to 2.4.0, the format bid‐
Packit 08bd4c
     ders were invoked for each entry, but this design hindered error recov‐
Packit 08bd4c
     ery.)
Packit 08bd4c
Packit 08bd4c
   I/O Layer and Client Callbacks
Packit 08bd4c
     The read API goes to some lengths to be nice to clients.  As a result,
Packit 08bd4c
     there are few restrictions on the behavior of the client callbacks.
Packit 08bd4c
Packit 08bd4c
     The client read callback is expected to provide a block of data on each
Packit 08bd4c
     call.  A zero-length return does indicate end of file, but otherwise
Packit 08bd4c
     blocks may be as small as one byte or as large as the entire file.  In
Packit 08bd4c
     particular, blocks may be of different sizes.
Packit 08bd4c
Packit 08bd4c
     The client skip callback returns the number of bytes actually skipped,
Packit 08bd4c
     which may be much smaller than the skip requested.  The only requirement
Packit 08bd4c
     is that the skip not be larger.  In particular, clients are allowed to
Packit 08bd4c
     return zero for any skip that they don't want to handle.  The skip call‐
Packit 08bd4c
     back must never be invoked with a negative value.
Packit 08bd4c
Packit 08bd4c
     Keep in mind that not all clients are reading from disk: clients reading
Packit 08bd4c
     from networks may provide different-sized blocks on every request and
Packit 08bd4c
     cannot skip at all; advanced clients may use mmap(2) to read the entire
Packit 08bd4c
     file into memory at once and return the entire file to libarchive as a
Packit 08bd4c
     single block; other clients may begin asynchronous I/O operations for the
Packit 08bd4c
     next block on each request.
Packit 08bd4c
Packit 08bd4c
   Decompresssion Layer
Packit 08bd4c
     The decompression layer not only handles decompression, it also buffers
Packit 08bd4c
     data so that the format handlers see a much nicer I/O model.  The decom‐
Packit 08bd4c
     pression API is a two stage peek/consume model.  A read_ahead request
Packit 08bd4c
     specifies a minimum read amount; the decompression layer must provide a
Packit 08bd4c
     pointer to at least that much data.  If more data is immediately avail‐
Packit 08bd4c
     able, it should return more: the format layer handles bulk data reads by
Packit 08bd4c
     asking for a minimum of one byte and then copying as much data as is
Packit 08bd4c
     available.
Packit 08bd4c
Packit 08bd4c
     A subsequent call to the consume() function advances the read pointer.
Packit 08bd4c
     Note that data returned from a read_ahead() call is guaranteed to remain
Packit 08bd4c
     in place until the next call to read_ahead().  Intervening calls to
Packit 08bd4c
     consume() should not cause the data to move.
Packit 08bd4c
Packit 08bd4c
     Skip requests must always be handled exactly.  Decompression handlers
Packit 08bd4c
     that cannot seek forward should not register a skip handler; the API
Packit 08bd4c
     layer fills in a generic skip handler that reads and discards data.
Packit 08bd4c
Packit 08bd4c
     A decompression handler has a specific lifecycle:
Packit 08bd4c
     Registration/Configuration
Packit 08bd4c
	     When the client invokes the public support function, the decom‐
Packit 08bd4c
	     pression handler invokes the internal
Packit 08bd4c
	     __archive_read_register_compression() function to provide bid and
Packit 08bd4c
	     initialization functions.	This function returns NULL on error or
Packit 08bd4c
	     else a pointer to a struct decompressor_t.  This structure con‐
Packit 08bd4c
	     tains a void * config slot that can be used for storing any cus‐
Packit 08bd4c
	     tomization information.
Packit 08bd4c
     Bid     The bid function is invoked with a pointer and size of a block of
Packit 08bd4c
	     data.  The decompressor can access its config data through the
Packit 08bd4c
	     decompressor element of the archive_read object.  The bid func‐
Packit 08bd4c
	     tion is otherwise stateless.  In particular, it must not perform
Packit 08bd4c
	     any I/O operations.
Packit 08bd4c
Packit 08bd4c
	     The value returned by the bid function indicates its suitability
Packit 08bd4c
	     for handling this data stream.  A bid of zero will ensure that
Packit 08bd4c
	     this decompressor is never invoked.  Return zero if magic number
Packit 08bd4c
	     checks fail.  Otherwise, your initial implementation should
Packit 08bd4c
	     return the number of bits actually checked.  For example, if you
Packit 08bd4c
	     verify two full bytes and three bits of another byte, bid 19.
Packit 08bd4c
	     Note that the initial block may be very short; be careful to only
Packit 08bd4c
	     inspect the data you are given.  (The current decompressors
Packit 08bd4c
	     require two bytes for correct bidding.)
Packit 08bd4c
     Initialize
Packit 08bd4c
	     The winning bidder will have its init function called.  This
Packit 08bd4c
	     function should initialize the remaining slots of the struct
Packit 08bd4c
	     decompressor_t object pointed to by the decompressor element of
Packit 08bd4c
	     the archive_read object.  In particular, it should allocate any
Packit 08bd4c
	     working data it needs in the data slot of that structure.	The
Packit 08bd4c
	     init function is called with the block of data that was used for
Packit 08bd4c
	     tasting.  At this point, the decompressor is responsible for all
Packit 08bd4c
	     I/O requests to the client callbacks.  The decompressor is free
Packit 08bd4c
	     to read more data as and when necessary.
Packit 08bd4c
     Satisfy I/O requests
Packit 08bd4c
	     The format handler will invoke the read_ahead, consume, and skip
Packit 08bd4c
	     functions as needed.
Packit 08bd4c
     Finish  The finish method is called only once when the archive is closed.
Packit 08bd4c
	     It should release anything stored in the data and config slots of
Packit 08bd4c
	     the decompressor object.  It should not invoke the client close
Packit 08bd4c
	     callback.
Packit 08bd4c
Packit 08bd4c
   Format Layer
Packit 08bd4c
     The read formats have a similar lifecycle to the decompression handlers:
Packit 08bd4c
     Registration
Packit 08bd4c
	     Allocate your private data and initialize your pointers.
Packit 08bd4c
     Bid     Formats bid by invoking the read_ahead() decompression method but
Packit 08bd4c
	     not calling the consume() method.	This allows each bidder to
Packit 08bd4c
	     look ahead in the input stream.  Bidders should not look further
Packit 08bd4c
	     ahead than necessary, as long look aheads put pressure on the
Packit 08bd4c
	     decompression layer to buffer lots of data.  Most formats only
Packit 08bd4c
	     require a few hundred bytes of look ahead; look aheads of a few
Packit 08bd4c
	     kilobytes are reasonable.	(The ISO9660 reader sometimes looks
Packit 08bd4c
	     ahead by 48k, which should be considered an upper limit.)
Packit 08bd4c
     Read header
Packit 08bd4c
	     The header read is usually the most complex part of any format.
Packit 08bd4c
	     There are a few strategies worth mentioning: For formats such as
Packit 08bd4c
	     tar or cpio, reading and parsing the header is straightforward
Packit 08bd4c
	     since headers alternate with data.  For formats that store all
Packit 08bd4c
	     header data at the beginning of the file, the first header read
Packit 08bd4c
	     request may have to read all headers into memory and store that
Packit 08bd4c
	     data, sorted by the location of the file data.  Subsequent header
Packit 08bd4c
	     read requests will skip forward to the beginning of the file data
Packit 08bd4c
	     and return the corresponding header.
Packit 08bd4c
     Read Data
Packit 08bd4c
	     The read data interface supports sparse files; this requires that
Packit 08bd4c
	     each call return a block of data specifying the file offset and
Packit 08bd4c
	     size.  This may require you to carefully track the location so
Packit 08bd4c
	     that you can return accurate file offsets for each read.  Remem‐
Packit 08bd4c
	     ber that the decompressor will return as much data as it has.
Packit 08bd4c
	     Generally, you will want to request one byte, examine the return
Packit 08bd4c
	     value to see how much data is available, and possibly trim that
Packit 08bd4c
	     to the amount you can use.  You should invoke consume for each
Packit 08bd4c
	     block just before you return it.
Packit 08bd4c
     Skip All Data
Packit 08bd4c
	     The skip data call should skip over all file data and trailing
Packit 08bd4c
	     padding.  This is called automatically by the API layer just
Packit 08bd4c
	     before each header read.  It is also called in response to the
Packit 08bd4c
	     client calling the public data_skip() function.
Packit 08bd4c
     Cleanup
Packit 08bd4c
	     On cleanup, the format should release all of its allocated mem‐
Packit 08bd4c
	     ory.
Packit 08bd4c
Packit 08bd4c
   API Layer
Packit 08bd4c
     XXX to do XXX
Packit 08bd4c
Packit 08bd4c
WRITE ARCHITECTURE
Packit 08bd4c
     The write API has a similar set of four layers: an API layer, a format
Packit 08bd4c
     layer, a compression layer, and an I/O layer.  The registration here is
Packit 08bd4c
     much simpler because only one format and one compression can be regis‐
Packit 08bd4c
     tered at a time.
Packit 08bd4c
Packit 08bd4c
   I/O Layer and Client Callbacks
Packit 08bd4c
     XXX To be written XXX
Packit 08bd4c
Packit 08bd4c
   Compression Layer
Packit 08bd4c
     XXX To be written XXX
Packit 08bd4c
Packit 08bd4c
   Format Layer
Packit 08bd4c
     XXX To be written XXX
Packit 08bd4c
Packit 08bd4c
   API Layer
Packit 08bd4c
     XXX To be written XXX
Packit 08bd4c
Packit 08bd4c
WRITE_DISK ARCHITECTURE
Packit 08bd4c
     The write_disk API is intended to look just like the write API to
Packit 08bd4c
     clients.  Since it does not handle multiple formats or compression, it is
Packit 08bd4c
     not layered internally.
Packit 08bd4c
Packit 08bd4c
GENERAL SERVICES
Packit 08bd4c
     The archive_read, archive_write, and archive_write_disk objects all con‐
Packit 08bd4c
     tain an initial archive object which provides common support for a set of
Packit 08bd4c
     standard services.  (Recall that ANSI/ISO C90 guarantees that you can
Packit 08bd4c
     cast freely between a pointer to a structure and a pointer to the first
Packit 08bd4c
     element of that structure.)  The archive object has a magic value that
Packit 08bd4c
     indicates which API this object is associated with, slots for storing
Packit 08bd4c
     error information, and function pointers for virtualized API functions.
Packit 08bd4c
Packit 08bd4c
MISCELLANEOUS NOTES
Packit 08bd4c
     Connecting existing archiving libraries into libarchive is generally
Packit 08bd4c
     quite difficult.  In particular, many existing libraries strongly assume
Packit 08bd4c
     that you are reading from a file; they seek forwards and backwards as
Packit 08bd4c
     necessary to locate various pieces of information.  In contrast,
Packit 08bd4c
     libarchive never seeks backwards in its input, which sometimes requires
Packit 08bd4c
     very different approaches.
Packit 08bd4c
Packit 08bd4c
     For example, libarchive's ISO9660 support operates very differently from
Packit 08bd4c
     most ISO9660 readers.  The libarchive support utilizes a work-queue
Packit 08bd4c
     design that keeps a list of known entries sorted by their location in the
Packit 08bd4c
     input.  Whenever libarchive's ISO9660 implementation is asked for the
Packit 08bd4c
     next header, checks this list to find the next item on the disk.  Direc‐
Packit 08bd4c
     tories are parsed when they are encountered and new items are added to
Packit 08bd4c
     the list.	This design relies heavily on the ISO9660 image being opti‐
Packit 08bd4c
     mized so that directories always occur earlier on the disk than the files
Packit 08bd4c
     they describe.
Packit 08bd4c
Packit 08bd4c
     Depending on the specific format, such approaches may not be possible.
Packit 08bd4c
     The ZIP format specification, for example, allows archivers to store key
Packit 08bd4c
     information only at the end of the file.  In theory, it is possible to
Packit 08bd4c
     create ZIP archives that cannot be read without seeking.  Fortunately,
Packit 08bd4c
     such archives are very rare, and libarchive can read most ZIP archives,
Packit 08bd4c
     though it cannot always extract as much information as a dedicated ZIP
Packit 08bd4c
     program.
Packit 08bd4c
Packit 08bd4c
SEE ALSO
Packit 08bd4c
     archive_entry(3), archive_read(3), archive_write(3),
Packit 08bd4c
     archive_write_disk(3) libarchive(3),
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
BSD			       January 26, 2011 			   BSD