archive_read.3.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <!-- Creator : groff version 1.22.4 -->
  2. <!-- CreationDate: Tue Jul 18 07:11:03 2023 -->
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  4. "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta name="generator" content="groff -Thtml, see www.gnu.org">
  8. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  9. <meta name="Content-Style" content="text/css">
  10. <style type="text/css">
  11. p { margin-top: 0; margin-bottom: 0; vertical-align: top }
  12. pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
  13. table { margin-top: 0; margin-bottom: 0; vertical-align: top }
  14. h1 { text-align: center }
  15. </style>
  16. <title></title>
  17. </head>
  18. <body>
  19. <hr>
  20. <p>ARCHIVE_READ(3) BSD Library Functions Manual
  21. ARCHIVE_READ(3)</p>
  22. <p style="margin-top: 1em"><b>NAME</b></p>
  23. <p style="margin-left:6%;"><b>archive_read</b> &mdash;
  24. functions for reading streaming archives</p>
  25. <p style="margin-top: 1em"><b>LIBRARY</b></p>
  26. <p style="margin-left:6%;">Streaming Archive Library
  27. (libarchive, -larchive)</p>
  28. <p style="margin-top: 1em"><b>SYNOPSIS</b></p>
  29. <p style="margin-left:6%;"><b>#include
  30. &lt;archive.h&gt;</b></p>
  31. <p style="margin-top: 1em"><b>DESCRIPTION</b></p>
  32. <p style="margin-left:6%;">These functions provide a
  33. complete API for reading streaming archives. The general
  34. process is to first create the struct archive object, set
  35. options, initialize the reader, iterate over the archive
  36. headers and associated data, then close the archive and
  37. release all resources.</p>
  38. <p style="margin-left:6%; margin-top: 1em"><b>Create
  39. archive object</b> <br>
  40. See archive_read_new(3).</p>
  41. <p style="margin-left:6%; margin-top: 1em">To read an
  42. archive, you must first obtain an initialized struct archive
  43. object from <b>archive_read_new</b>().</p>
  44. <p style="margin-left:6%; margin-top: 1em"><b>Enable
  45. filters and formats</b> <br>
  46. See archive_read_filter(3) and archive_read_format(3).</p>
  47. <p style="margin-left:6%; margin-top: 1em">You can then
  48. modify this object for the desired operations with the
  49. various <b>archive_read_set_XXX</b>() and
  50. <b>archive_read_support_XXX</b>() functions. In particular,
  51. you will need to invoke appropriate
  52. <b>archive_read_support_XXX</b>() functions to enable the
  53. corresponding compression and format support. Note that
  54. these latter functions perform two distinct operations: they
  55. cause the corresponding support code to be linked into your
  56. program, and they enable the corresponding auto-detect code.
  57. Unless you have specific constraints, you will generally
  58. want to invoke <b>archive_read_support_filter_all</b>() and
  59. <b>archive_read_support_format_all</b>() to enable
  60. auto-detect for all formats and compression types currently
  61. supported by the library.</p>
  62. <p style="margin-left:6%; margin-top: 1em"><b>Set
  63. options</b> <br>
  64. See archive_read_set_options(3).</p>
  65. <p style="margin-left:6%; margin-top: 1em"><b>Open
  66. archive</b> <br>
  67. See archive_read_open(3).</p>
  68. <p style="margin-left:6%; margin-top: 1em">Once you have
  69. prepared the struct archive object, you call
  70. <b>archive_read_open</b>() to actually open the archive and
  71. prepare it for reading. There are several variants of this
  72. function; the most basic expects you to provide pointers to
  73. several functions that can provide blocks of bytes from the
  74. archive. There are convenience forms that allow you to
  75. specify a filename, file descriptor, <i>FILE *</i> object,
  76. or a block of memory from which to read the archive data.
  77. Note that the core library makes no assumptions about the
  78. size of the blocks read; callback functions are free to read
  79. whatever block size is most appropriate for the medium.</p>
  80. <p style="margin-left:6%; margin-top: 1em"><b>Consume
  81. archive</b> <br>
  82. See archive_read_header(3), archive_read_data(3) and
  83. archive_read_extract(3).</p>
  84. <p style="margin-left:6%; margin-top: 1em">Each archive
  85. entry consists of a header followed by a certain amount of
  86. data. You can obtain the next header with
  87. <b>archive_read_next_header</b>(), which returns a pointer
  88. to an struct archive_entry structure with information about
  89. the current archive element. If the entry is a regular file,
  90. then the header will be followed by the file data. You can
  91. use <b>archive_read_data</b>() (which works much like the
  92. read(2) system call) to read this data from the archive, or
  93. <b>archive_read_data_block</b>() which provides a slightly
  94. more efficient interface. You may prefer to use the
  95. higher-level <b>archive_read_data_skip</b>(), which reads
  96. and discards the data for this entry,
  97. <b>archive_read_data_into_fd</b>(), which copies the data to
  98. the provided file descriptor, or
  99. <b>archive_read_extract</b>(), which recreates the specified
  100. entry on disk and copies data from the archive. In
  101. particular, note that <b>archive_read_extract</b>() uses the
  102. struct archive_entry structure that you provide it, which
  103. may differ from the entry just read from the archive. In
  104. particular, many applications will want to override the
  105. pathname, file permissions, or ownership.</p>
  106. <p style="margin-left:6%; margin-top: 1em"><b>Release
  107. resources</b> <br>
  108. See archive_read_free(3).</p>
  109. <p style="margin-left:6%; margin-top: 1em">Once you have
  110. finished reading data from the archive, you should call
  111. <b>archive_read_close</b>() to close the archive, then call
  112. <b>archive_read_free</b>() to release all resources,
  113. including all memory allocated by the library.</p>
  114. <p style="margin-top: 1em"><b>EXAMPLES</b></p>
  115. <p style="margin-left:6%;">The following illustrates basic
  116. usage of the library. In this example, the callback
  117. functions are simply wrappers around the standard open(2),
  118. read(2), and close(2) system calls.</p>
  119. <p style="margin-left:14%; margin-top: 1em">void <br>
  120. list_archive(const char *name) <br>
  121. { <br>
  122. struct mydata *mydata; <br>
  123. struct archive *a; <br>
  124. struct archive_entry *entry;</p>
  125. <p style="margin-left:14%; margin-top: 1em">mydata =
  126. malloc(sizeof(struct mydata)); <br>
  127. a = archive_read_new(); <br>
  128. mydata-&gt;name = name; <br>
  129. archive_read_support_filter_all(a); <br>
  130. archive_read_support_format_all(a); <br>
  131. archive_read_open(a, mydata, myopen, myread, myclose); <br>
  132. while (archive_read_next_header(a, &amp;entry) ==
  133. ARCHIVE_OK) { <br>
  134. printf(&quot;%s\n&quot;,archive_entry_pathname(entry)); <br>
  135. archive_read_data_skip(a); <br>
  136. } <br>
  137. archive_read_free(a); <br>
  138. free(mydata); <br>
  139. }</p>
  140. <p style="margin-left:14%; margin-top: 1em">la_ssize_t <br>
  141. myread(struct archive *a, void *client_data, const void
  142. **buff) <br>
  143. { <br>
  144. struct mydata *mydata = client_data;</p>
  145. <p style="margin-left:14%; margin-top: 1em">*buff =
  146. mydata-&gt;buff; <br>
  147. return (read(mydata-&gt;fd, mydata-&gt;buff, 10240)); <br>
  148. }</p>
  149. <p style="margin-left:14%; margin-top: 1em">int <br>
  150. myopen(struct archive *a, void *client_data) <br>
  151. { <br>
  152. struct mydata *mydata = client_data;</p>
  153. <p style="margin-left:14%; margin-top: 1em">mydata-&gt;fd =
  154. open(mydata-&gt;name, O_RDONLY); <br>
  155. return (mydata-&gt;fd &gt;= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
  156. <br>
  157. }</p>
  158. <p style="margin-left:14%; margin-top: 1em">int <br>
  159. myclose(struct archive *a, void *client_data) <br>
  160. { <br>
  161. struct mydata *mydata = client_data;</p>
  162. <p style="margin-left:14%; margin-top: 1em">if
  163. (mydata-&gt;fd &gt; 0) <br>
  164. close(mydata-&gt;fd); <br>
  165. return (ARCHIVE_OK); <br>
  166. }</p>
  167. <p style="margin-top: 1em"><b>SEE ALSO</b></p>
  168. <p style="margin-left:6%;">tar(1), archive_read_data(3),
  169. archive_read_extract(3), archive_read_filter(3),
  170. archive_read_format(3), archive_read_header(3),
  171. archive_read_new(3), archive_read_open(3),
  172. archive_read_set_options(3), archive_util(3), libarchive(3),
  173. tar(5)</p>
  174. <p style="margin-top: 1em"><b>HISTORY</b></p>
  175. <p style="margin-left:6%;">The <b>libarchive</b> library
  176. first appeared in FreeBSD&nbsp;5.3.</p>
  177. <p style="margin-top: 1em"><b>AUTHORS</b></p>
  178. <p style="margin-left:6%;">The <b>libarchive</b> library
  179. was written by Tim Kientzle &lt;[email protected]&gt;.</p>
  180. <p style="margin-top: 1em"><b>BUGS</b></p>
  181. <p style="margin-left:6%;">Many traditional archiver
  182. programs treat empty files as valid empty archives. For
  183. example, many implementations of tar(1) allow you to append
  184. entries to an empty file. Of course, it is impossible to
  185. determine the format of an empty file by inspecting the
  186. contents, so this library treats empty files as having a
  187. special &ldquo;empty&rdquo; format.</p>
  188. <p style="margin-left:6%; margin-top: 1em">BSD
  189. February&nbsp;2, 2012 BSD</p>
  190. <hr>
  191. </body>
  192. </html>