libarchive_internals.3 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. .TH LIBARCHIVE_INTERNALS 3 "January 26, 2011" ""
  2. .SH NAME
  3. .ad l
  4. \fB\%libarchive_internals\fP
  5. \- description of libarchive internal interfaces
  6. .SH OVERVIEW
  7. .ad l
  8. The
  9. \fB\%libarchive\fP
  10. library provides a flexible interface for reading and writing
  11. streaming archive files such as tar and cpio.
  12. Internally, it follows a modular layered design that should
  13. make it easy to add new archive and compression formats.
  14. .SH GENERAL ARCHITECTURE
  15. .ad l
  16. Externally, libarchive exposes most operations through an
  17. opaque, object-style interface.
  18. The
  19. \fBarchive_entry\fP(3)
  20. objects store information about a single filesystem object.
  21. The rest of the library provides facilities to write
  22. \fBarchive_entry\fP(3)
  23. objects to archive files,
  24. read them from archive files,
  25. and write them to disk.
  26. (There are plans to add a facility to read
  27. \fBarchive_entry\fP(3)
  28. objects from disk as well.)
  29. .PP
  30. The read and write APIs each have four layers: a public API
  31. layer, a format layer that understands the archive file format,
  32. a compression layer, and an I/O layer.
  33. The I/O layer is completely exposed to clients who can replace
  34. it entirely with their own functions.
  35. .PP
  36. In order to provide as much consistency as possible for clients,
  37. some public functions are virtualized.
  38. Eventually, it should be possible for clients to open
  39. an archive or disk writer, and then use a single set of
  40. code to select and write entries, regardless of the target.
  41. .SH READ ARCHITECTURE
  42. .ad l
  43. From the outside, clients use the
  44. \fBarchive_read\fP(3)
  45. API to manipulate an
  46. \fB\%archive\fP
  47. object to read entries and bodies from an archive stream.
  48. Internally, the
  49. \fB\%archive\fP
  50. object is cast to an
  51. \fB\%archive_read\fP
  52. object, which holds all read-specific data.
  53. The API has four layers:
  54. The lowest layer is the I/O layer.
  55. This layer can be overridden by clients, but most clients use
  56. the packaged I/O callbacks provided, for example, by
  57. \fBarchive_read_open_memory\fP(3),
  58. and
  59. \fBarchive_read_open_fd\fP(3).
  60. The compression layer calls the I/O layer to
  61. read bytes and decompresses them for the format layer.
  62. The format layer unpacks a stream of uncompressed bytes and
  63. creates
  64. \fB\%archive_entry\fP
  65. objects from the incoming data.
  66. The API layer tracks overall state
  67. (for example, it prevents clients from reading data before reading a header)
  68. and invokes the format and compression layer operations
  69. through registered function pointers.
  70. In particular, the API layer drives the format-detection process:
  71. When opening the archive, it reads an initial block of data
  72. and offers it to each registered compression handler.
  73. The one with the highest bid is initialized with the first block.
  74. Similarly, the format handlers are polled to see which handler
  75. is the best for each archive.
  76. (Prior to 2.4.0, the format bidders were invoked for each
  77. entry, but this design hindered error recovery.)
  78. .SS I/O Layer and Client Callbacks
  79. The read API goes to some lengths to be nice to clients.
  80. As a result, there are few restrictions on the behavior of
  81. the client callbacks.
  82. .PP
  83. The client read callback is expected to provide a block
  84. of data on each call.
  85. A zero-length return does indicate end of file, but otherwise
  86. blocks may be as small as one byte or as large as the entire file.
  87. In particular, blocks may be of different sizes.
  88. .PP
  89. The client skip callback returns the number of bytes actually
  90. skipped, which may be much smaller than the skip requested.
  91. The only requirement is that the skip not be larger.
  92. In particular, clients are allowed to return zero for any
  93. skip that they don't want to handle.
  94. The skip callback must never be invoked with a negative value.
  95. .PP
  96. Keep in mind that not all clients are reading from disk:
  97. clients reading from networks may provide different-sized
  98. blocks on every request and cannot skip at all;
  99. advanced clients may use
  100. \fBmmap\fP(2)
  101. to read the entire file into memory at once and return the
  102. entire file to libarchive as a single block;
  103. other clients may begin asynchronous I/O operations for the
  104. next block on each request.
  105. .SS Decompresssion Layer
  106. The decompression layer not only handles decompression,
  107. it also buffers data so that the format handlers see a
  108. much nicer I/O model.
  109. The decompression API is a two stage peek/consume model.
  110. A read_ahead request specifies a minimum read amount;
  111. the decompression layer must provide a pointer to at least
  112. that much data.
  113. If more data is immediately available, it should return more:
  114. the format layer handles bulk data reads by asking for a minimum
  115. of one byte and then copying as much data as is available.
  116. .PP
  117. A subsequent call to the
  118. \fB\%consume\fP()
  119. function advances the read pointer.
  120. Note that data returned from a
  121. \fB\%read_ahead\fP()
  122. call is guaranteed to remain in place until
  123. the next call to
  124. \fB\%read_ahead\fP().
  125. Intervening calls to
  126. \fB\%consume\fP()
  127. should not cause the data to move.
  128. .PP
  129. Skip requests must always be handled exactly.
  130. Decompression handlers that cannot seek forward should
  131. not register a skip handler;
  132. the API layer fills in a generic skip handler that reads and discards data.
  133. .PP
  134. A decompression handler has a specific lifecycle:
  135. .RS 5
  136. .TP
  137. Registration/Configuration
  138. When the client invokes the public support function,
  139. the decompression handler invokes the internal
  140. \fB\%__archive_read_register_compression\fP()
  141. function to provide bid and initialization functions.
  142. This function returns
  143. \fBNULL\fP
  144. on error or else a pointer to a
  145. \fBstruct\fP decompressor_t.
  146. This structure contains a
  147. \fIvoid\fP * config
  148. slot that can be used for storing any customization information.
  149. .TP
  150. Bid
  151. The bid function is invoked with a pointer and size of a block of data.
  152. The decompressor can access its config data
  153. through the
  154. \fIdecompressor\fP
  155. element of the
  156. \fBarchive_read\fP
  157. object.
  158. The bid function is otherwise stateless.
  159. In particular, it must not perform any I/O operations.
  160. .PP
  161. The value returned by the bid function indicates its suitability
  162. for handling this data stream.
  163. A bid of zero will ensure that this decompressor is never invoked.
  164. Return zero if magic number checks fail.
  165. Otherwise, your initial implementation should return the number of bits
  166. actually checked.
  167. For example, if you verify two full bytes and three bits of another
  168. byte, bid 19.
  169. Note that the initial block may be very short;
  170. be careful to only inspect the data you are given.
  171. (The current decompressors require two bytes for correct bidding.)
  172. .TP
  173. Initialize
  174. The winning bidder will have its init function called.
  175. This function should initialize the remaining slots of the
  176. \fIstruct\fP decompressor_t
  177. object pointed to by the
  178. \fIdecompressor\fP
  179. element of the
  180. \fIarchive_read\fP
  181. object.
  182. In particular, it should allocate any working data it needs
  183. in the
  184. \fIdata\fP
  185. slot of that structure.
  186. The init function is called with the block of data that
  187. was used for tasting.
  188. At this point, the decompressor is responsible for all I/O
  189. requests to the client callbacks.
  190. The decompressor is free to read more data as and when
  191. necessary.
  192. .TP
  193. Satisfy I/O requests
  194. The format handler will invoke the
  195. \fIread_ahead\fP,
  196. \fIconsume\fP,
  197. and
  198. \fIskip\fP
  199. functions as needed.
  200. .TP
  201. Finish
  202. The finish method is called only once when the archive is closed.
  203. It should release anything stored in the
  204. \fIdata\fP
  205. and
  206. \fIconfig\fP
  207. slots of the
  208. \fIdecompressor\fP
  209. object.
  210. It should not invoke the client close callback.
  211. .RE
  212. .SS Format Layer
  213. The read formats have a similar lifecycle to the decompression handlers:
  214. .RS 5
  215. .TP
  216. Registration
  217. Allocate your private data and initialize your pointers.
  218. .TP
  219. Bid
  220. Formats bid by invoking the
  221. \fB\%read_ahead\fP()
  222. decompression method but not calling the
  223. \fB\%consume\fP()
  224. method.
  225. This allows each bidder to look ahead in the input stream.
  226. Bidders should not look further ahead than necessary, as long
  227. look aheads put pressure on the decompression layer to buffer
  228. lots of data.
  229. Most formats only require a few hundred bytes of look ahead;
  230. look aheads of a few kilobytes are reasonable.
  231. (The ISO9660 reader sometimes looks ahead by 48k, which
  232. should be considered an upper limit.)
  233. .TP
  234. Read header
  235. The header read is usually the most complex part of any format.
  236. There are a few strategies worth mentioning:
  237. For formats such as tar or cpio, reading and parsing the header is
  238. straightforward since headers alternate with data.
  239. For formats that store all header data at the beginning of the file,
  240. the first header read request may have to read all headers into
  241. memory and store that data, sorted by the location of the file
  242. data.
  243. Subsequent header read requests will skip forward to the
  244. beginning of the file data and return the corresponding header.
  245. .TP
  246. Read Data
  247. The read data interface supports sparse files; this requires that
  248. each call return a block of data specifying the file offset and
  249. size.
  250. This may require you to carefully track the location so that you
  251. can return accurate file offsets for each read.
  252. Remember that the decompressor will return as much data as it has.
  253. Generally, you will want to request one byte,
  254. examine the return value to see how much data is available, and
  255. possibly trim that to the amount you can use.
  256. You should invoke consume for each block just before you return it.
  257. .TP
  258. Skip All Data
  259. The skip data call should skip over all file data and trailing padding.
  260. This is called automatically by the API layer just before each
  261. header read.
  262. It is also called in response to the client calling the public
  263. \fB\%data_skip\fP()
  264. function.
  265. .TP
  266. Cleanup
  267. On cleanup, the format should release all of its allocated memory.
  268. .RE
  269. .SS API Layer
  270. XXX to do XXX
  271. .SH WRITE ARCHITECTURE
  272. .ad l
  273. The write API has a similar set of four layers:
  274. an API layer, a format layer, a compression layer, and an I/O layer.
  275. The registration here is much simpler because only
  276. one format and one compression can be registered at a time.
  277. .SS I/O Layer and Client Callbacks
  278. XXX To be written XXX
  279. .SS Compression Layer
  280. XXX To be written XXX
  281. .SS Format Layer
  282. XXX To be written XXX
  283. .SS API Layer
  284. XXX To be written XXX
  285. .SH WRITE_DISK ARCHITECTURE
  286. .ad l
  287. The write_disk API is intended to look just like the write API
  288. to clients.
  289. Since it does not handle multiple formats or compression, it
  290. is not layered internally.
  291. .SH GENERAL SERVICES
  292. .ad l
  293. The
  294. \fB\%archive_read\fP,
  295. \fB\%archive_write\fP,
  296. and
  297. \fB\%archive_write_disk\fP
  298. objects all contain an initial
  299. \fB\%archive\fP
  300. object which provides common support for a set of standard services.
  301. (Recall that ANSI/ISO C90 guarantees that you can cast freely between
  302. a pointer to a structure and a pointer to the first element of that
  303. structure.)
  304. The
  305. \fB\%archive\fP
  306. object has a magic value that indicates which API this object
  307. is associated with,
  308. slots for storing error information,
  309. and function pointers for virtualized API functions.
  310. .SH MISCELLANEOUS NOTES
  311. .ad l
  312. Connecting existing archiving libraries into libarchive is generally
  313. quite difficult.
  314. In particular, many existing libraries strongly assume that you
  315. are reading from a file; they seek forwards and backwards as necessary
  316. to locate various pieces of information.
  317. In contrast, libarchive never seeks backwards in its input, which
  318. sometimes requires very different approaches.
  319. .PP
  320. For example, libarchive's ISO9660 support operates very differently
  321. from most ISO9660 readers.
  322. The libarchive support utilizes a work-queue design that
  323. keeps a list of known entries sorted by their location in the input.
  324. Whenever libarchive's ISO9660 implementation is asked for the next
  325. header, checks this list to find the next item on the disk.
  326. Directories are parsed when they are encountered and new
  327. items are added to the list.
  328. This design relies heavily on the ISO9660 image being optimized so that
  329. directories always occur earlier on the disk than the files they
  330. describe.
  331. .PP
  332. Depending on the specific format, such approaches may not be possible.
  333. The ZIP format specification, for example, allows archivers to store
  334. key information only at the end of the file.
  335. In theory, it is possible to create ZIP archives that cannot
  336. be read without seeking.
  337. Fortunately, such archives are very rare, and libarchive can read
  338. most ZIP archives, though it cannot always extract as much information
  339. as a dedicated ZIP program.
  340. .SH SEE ALSO
  341. .ad l
  342. \fBarchive_entry\fP(3),
  343. \fBarchive_read\fP(3),
  344. \fBarchive_write\fP(3),
  345. \fBarchive_write_disk\fP(3),
  346. \fBlibarchive\fP(3)
  347. .SH HISTORY
  348. .ad l
  349. The
  350. \fB\%libarchive\fP
  351. library first appeared in
  352. FreeBSD 5.3.
  353. .SH AUTHORS
  354. .ad l
  355. -nosplit
  356. The
  357. \fB\%libarchive\fP
  358. library was written by
  359. Tim Kientzle \%<[email protected].>