1
.\" Copyright (c) 2003-2007 Tim Kientzle
2
.\" All rights reserved.
4
.\" Redistribution and use in source and binary forms, with or without
5
.\" modification, are permitted provided that the following conditions
7
.\" 1. Redistributions of source code must retain the above copyright
8
.\" notice, this list of conditions and the following disclaimer.
9
.\" 2. Redistributions in binary form must reproduce the above copyright
10
.\" notice, this list of conditions and the following disclaimer in the
11
.\" documentation and/or other materials provided with the distribution.
13
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
.\" $FreeBSD: src/lib/libarchive/libarchive_internals.3,v 1.2 2007/12/30 04:58:22 kientzle Exp $
31
.Nm libarchive_internals
32
.Nd description of libarchive internal interfaces
36
library provides a flexible interface for reading and writing
37
streaming archive files such as tar and cpio.
38
Internally, it follows a modular layered design that should
39
make it easy to add new archive and compression formats.
40
.Sh GENERAL ARCHITECTURE
41
Externally, libarchive exposes most operations through an
42
opaque, object-style interface.
45
objects store information about a single filesystem object.
46
The rest of the library provides facilities to write
48
objects to archive files,
49
read them from archive files,
50
and write them to disk.
51
(There are plans to add a facility to read
53
objects from disk as well.)
55
The read and write APIs each have four layers: a public API
56
layer, a format layer that understands the archive file format,
57
a compression layer, and an I/O layer.
58
The I/O layer is completely exposed to clients who can replace
59
it entirely with their own functions.
61
In order to provide as much consistency as possible for clients,
62
some public functions are virtualized.
63
Eventually, it should be possible for clients to open
64
an archive or disk writer, and then use a single set of
65
code to select and write entries, regardless of the target.
67
From the outside, clients use the
71
object to read entries and bodies from an archive stream.
76
object, which holds all read-specific data.
77
The API has four layers:
78
The lowest layer is the I/O layer.
79
This layer can be overridden by clients, but most clients use
80
the packaged I/O callbacks provided, for example, by
81
.Xr archive_read_open_memory 3 ,
83
.Xr archive_read_open_fd 3 .
84
The compression layer calls the I/O layer to
85
read bytes and decompresses them for the format layer.
86
The format layer unpacks a stream of uncompressed bytes and
89
objects from the incoming data.
90
The API layer tracks overall state
91
(for example, it prevents clients from reading data before reading a header)
92
and invokes the format and compression layer operations
93
through registered function pointers.
94
In particular, the API layer drives the format-detection process:
95
When opening the archive, it reads an initial block of data
96
and offers it to each registered compression handler.
97
The one with the highest bid is initialized with the first block.
98
Similarly, the format handlers are polled to see which handler
99
is the best for each archive.
100
(Prior to 2.4.0, the format bidders were invoked for each
101
entry, but this design hindered error recovery.)
102
.Ss I/O Layer and Client Callbacks
103
The read API goes to some lengths to be nice to clients.
104
As a result, there are few restrictions on the behavior of
105
the client callbacks.
107
The client read callback is expected to provide a block
108
of data on each call.
109
A zero-length return does indicate end of file, but otherwise
110
blocks may be as small as one byte or as large as the entire file.
111
In particular, blocks may be of different sizes.
113
The client skip callback returns the number of bytes actually
114
skipped, which may be much smaller than the skip requested.
115
The only requirement is that the skip not be larger.
116
In particular, clients are allowed to return zero for any
117
skip that they don't want to handle.
118
The skip callback must never be invoked with a negative value.
120
Keep in mind that not all clients are reading from disk:
121
clients reading from networks may provide different-sized
122
blocks on every request and cannot skip at all;
123
advanced clients may use
125
to read the entire file into memory at once and return the
126
entire file to libarchive as a single block;
127
other clients may begin asynchronous I/O operations for the
128
next block on each request.
129
.Ss Decompresssion Layer
130
The decompression layer not only handles decompression,
131
it also buffers data so that the format handlers see a
132
much nicer I/O model.
133
The decompression API is a two stage peek/consume model.
134
A read_ahead request specifies a minimum read amount;
135
the decompression layer must provide a pointer to at least
137
If more data is immediately available, it should return more:
138
the format layer handles bulk data reads by asking for a minimum
139
of one byte and then copying as much data as is available.
141
A subsequent call to the
143
function advances the read pointer.
144
Note that data returned from a
146
call is guaranteed to remain in place until
151
should not cause the data to move.
153
Skip requests must always be handled exactly.
154
Decompression handlers that cannot seek forward should
155
not register a skip handler;
156
the API layer fills in a generic skip handler that reads and discards data.
158
A decompression handler has a specific lifecycle:
159
.Bl -tag -compact -width indent
160
.It Registration/Configuration
161
When the client invokes the public support function,
162
the decompression handler invokes the internal
163
.Fn __archive_read_register_compression
164
function to provide bid and initialization functions.
165
This function returns
167
on error or else a pointer to a
168
.Cm struct decompressor_t .
169
This structure contains a
171
slot that can be used for storing any customization information.
173
The bid function is invoked with a pointer and size of a block of data.
174
The decompressor can access its config data
180
The bid function is otherwise stateless.
181
In particular, it must not perform any I/O operations.
183
The value returned by the bid function indicates its suitability
184
for handling this data stream.
185
A bid of zero will ensure that this decompressor is never invoked.
186
Return zero if magic number checks fail.
187
Otherwise, your initial implementation should return the number of bits
189
For example, if you verify two full bytes and three bits of another
191
Note that the initial block may be very short;
192
be careful to only inspect the data you are given.
193
(The current decompressors require two bytes for correct bidding.)
195
The winning bidder will have its init function called.
196
This function should initialize the remaining slots of the
197
.Va struct decompressor_t
198
object pointed to by the
203
In particular, it should allocate any working data it needs
206
slot of that structure.
207
The init function is called with the block of data that
208
was used for tasting.
209
At this point, the decompressor is responsible for all I/O
210
requests to the client callbacks.
211
The decompressor is free to read more data as and when
213
.It Satisfy I/O requests
214
The format handler will invoke the
221
The finish method is called only once when the archive is closed.
222
It should release anything stored in the
229
It should not invoke the client close callback.
232
The read formats have a similar lifecycle to the decompression handlers:
233
.Bl -tag -compact -width indent
235
Allocate your private data and initialize your pointers.
237
Formats bid by invoking the
239
decompression method but not calling the
242
This allows each bidder to look ahead in the input stream.
243
Bidders should not look further ahead than necessary, as long
244
look aheads put pressure on the decompression layer to buffer
246
Most formats only require a few hundred bytes of look ahead;
247
look aheads of a few kilobytes are reasonable.
248
(The ISO9660 reader sometimes looks ahead by 48k, which
249
should be considered an upper limit.)
251
The header read is usually the most complex part of any format.
252
There are a few strategies worth mentioning:
253
For formats such as tar or cpio, reading and parsing the header is
254
straightforward since headers alternate with data.
255
For formats that store all header data at the beginning of the file,
256
the first header read request may have to read all headers into
257
memory and store that data, sorted by the location of the file
259
Subsequent header read requests will skip forward to the
260
beginning of the file data and return the corresponding header.
262
The read data interface supports sparse files; this requires that
263
each call return a block of data specifying the file offset and
265
This may require you to carefully track the location so that you
266
can return accurate file offsets for each read.
267
Remember that the decompressor will return as much data as it has.
268
Generally, you will want to request one byte,
269
examine the return value to see how much data is available, and
270
possibly trim that to the amount you can use.
271
You should invoke consume for each block just before you return it.
273
The skip data call should skip over all file data and trailing padding.
274
This is called automatically by the API layer just before each
276
It is also called in response to the client calling the public
280
On cleanup, the format should release all of its allocated memory.
284
.Sh WRITE ARCHITECTURE
285
The write API has a similar set of four layers:
286
an API layer, a format layer, a compression layer, and an I/O layer.
287
The registration here is much simpler because only
288
one format and one compression can be registered at a time.
289
.Ss I/O Layer and Client Callbacks
290
XXX To be written XXX
291
.Ss Compression Layer
292
XXX To be written XXX
294
XXX To be written XXX
296
XXX To be written XXX
297
.Sh WRITE_DISK ARCHITECTURE
298
The write_disk API is intended to look just like the write API
300
Since it does not handle multiple formats or compression, it
301
is not layered internally.
307
.Nm archive_write_disk
308
objects all contain an initial
310
object which provides common support for a set of standard services.
311
(Recall that ANSI/ISO C90 guarantees that you can cast freely between
312
a pointer to a structure and a pointer to the first element of that
316
object has a magic value that indicates which API this object
318
slots for storing error information,
319
and function pointers for virtualized API functions.
320
.Sh MISCELLANEOUS NOTES
321
Connecting existing archiving libraries into libarchive is generally
323
In particular, many existing libraries strongly assume that you
324
are reading from a file; they seek forwards and backwards as necessary
325
to locate various pieces of information.
326
In contrast, libarchive never seeks backwards in its input, which
327
sometimes requires very different approaches.
329
For example, libarchive's ISO9660 support operates very differently
330
from most ISO9660 readers.
331
The libarchive support utilizes a work-queue design that
332
keeps a list of known entries sorted by their location in the input.
333
Whenever libarchive's ISO9660 implementation is asked for the next
334
header, checks this list to find the next item on the disk.
335
Directories are parsed when they are encountered and new
336
items are added to the list.
337
This design relies heavily on the ISO9660 image being optimized so that
338
directories always occur earlier on the disk than the files they
341
Depending on the specific format, such approaches may not be possible.
342
The ZIP format specification, for example, allows archivers to store
343
key information only at the end of the file.
344
In theory, it is possible to create ZIP archives that cannot
345
be read without seeking.
346
Fortunately, such archives are very rare, and libarchive can read
347
most ZIP archives, though it cannot always extract as much information
348
as a dedicated ZIP program.
351
.Xr archive_entry 3 ,
353
.Xr archive_write 3 ,
354
.Xr archive_write_disk 3
358
library first appeared in
364
library was written by
365
.An Tim Kientzle Aq kientzle@acm.org .