archive_read.3 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. .\" Copyright (c) 2003-2007 Tim Kientzle
  2. .\" All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  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.
  12. .\"
  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
  23. .\" SUCH DAMAGE.
  24. .\"
  25. .\" $FreeBSD$
  26. .\"
  27. .Dd February 2, 2012
  28. .Dt ARCHIVE_READ 3
  29. .Os
  30. .Sh NAME
  31. .Nm archive_read
  32. .Nd functions for reading streaming archives
  33. .Sh LIBRARY
  34. Streaming Archive Library (libarchive, -larchive)
  35. .Sh SYNOPSIS
  36. .In archive.h
  37. .Sh DESCRIPTION
  38. These functions provide a complete API for reading streaming archives.
  39. The general process is to first create the
  40. .Tn struct archive
  41. object, set options, initialize the reader, iterate over the archive
  42. headers and associated data, then close the archive and release all
  43. resources.
  44. .\"
  45. .Ss Create archive object
  46. See
  47. .Xr archive_read_new 3 .
  48. .Pp
  49. To read an archive, you must first obtain an initialized
  50. .Tn struct archive
  51. object from
  52. .Fn archive_read_new .
  53. .\"
  54. .Ss Enable filters and formats
  55. See
  56. .Xr archive_read_filter 3
  57. and
  58. .Xr archive_read_format 3 .
  59. .Pp
  60. You can then modify this object for the desired operations with the
  61. various
  62. .Fn archive_read_set_XXX
  63. and
  64. .Fn archive_read_support_XXX
  65. functions.
  66. In particular, you will need to invoke appropriate
  67. .Fn archive_read_support_XXX
  68. functions to enable the corresponding compression and format
  69. support.
  70. Note that these latter functions perform two distinct operations:
  71. they cause the corresponding support code to be linked into your
  72. program, and they enable the corresponding auto-detect code.
  73. Unless you have specific constraints, you will generally want
  74. to invoke
  75. .Fn archive_read_support_filter_all
  76. and
  77. .Fn archive_read_support_format_all
  78. to enable auto-detect for all formats and compression types
  79. currently supported by the library.
  80. .\"
  81. .Ss Set options
  82. See
  83. .Xr archive_read_set_options 3 .
  84. .\"
  85. .Ss Open archive
  86. See
  87. .Xr archive_read_open 3 .
  88. .Pp
  89. Once you have prepared the
  90. .Tn struct archive
  91. object, you call
  92. .Fn archive_read_open
  93. to actually open the archive and prepare it for reading.
  94. There are several variants of this function;
  95. the most basic expects you to provide pointers to several
  96. functions that can provide blocks of bytes from the archive.
  97. There are convenience forms that allow you to
  98. specify a filename, file descriptor,
  99. .Ft "FILE *"
  100. object, or a block of memory from which to read the archive data.
  101. Note that the core library makes no assumptions about the
  102. size of the blocks read;
  103. callback functions are free to read whatever block size is
  104. most appropriate for the medium.
  105. .\"
  106. .Ss Consume archive
  107. See
  108. .Xr archive_read_header 3 ,
  109. .Xr archive_read_data 3
  110. and
  111. .Xr archive_read_extract 3 .
  112. .Pp
  113. Each archive entry consists of a header followed by a certain
  114. amount of data.
  115. You can obtain the next header with
  116. .Fn archive_read_next_header ,
  117. which returns a pointer to an
  118. .Tn struct archive_entry
  119. structure with information about the current archive element.
  120. If the entry is a regular file, then the header will be followed
  121. by the file data.
  122. You can use
  123. .Fn archive_read_data
  124. (which works much like the
  125. .Xr read 2
  126. system call)
  127. to read this data from the archive, or
  128. .Fn archive_read_data_block
  129. which provides a slightly more efficient interface.
  130. You may prefer to use the higher-level
  131. .Fn archive_read_data_skip ,
  132. which reads and discards the data for this entry,
  133. .Fn archive_read_data_into_fd ,
  134. which copies the data to the provided file descriptor, or
  135. .Fn archive_read_extract ,
  136. which recreates the specified entry on disk and copies data
  137. from the archive.
  138. In particular, note that
  139. .Fn archive_read_extract
  140. uses the
  141. .Tn struct archive_entry
  142. structure that you provide it, which may differ from the
  143. entry just read from the archive.
  144. In particular, many applications will want to override the
  145. pathname, file permissions, or ownership.
  146. .\"
  147. .Ss Release resources
  148. See
  149. .Xr archive_read_free 3 .
  150. .Pp
  151. Once you have finished reading data from the archive, you
  152. should call
  153. .Fn archive_read_close
  154. to close the archive, then call
  155. .Fn archive_read_free
  156. to release all resources, including all memory allocated by the library.
  157. .\"
  158. .Sh EXAMPLES
  159. The following illustrates basic usage of the library.
  160. In this example,
  161. the callback functions are simply wrappers around the standard
  162. .Xr open 2 ,
  163. .Xr read 2 ,
  164. and
  165. .Xr close 2
  166. system calls.
  167. .Bd -literal -offset indent
  168. void
  169. list_archive(const char *name)
  170. {
  171. struct mydata *mydata;
  172. struct archive *a;
  173. struct archive_entry *entry;
  174. mydata = malloc(sizeof(struct mydata));
  175. a = archive_read_new();
  176. mydata->name = name;
  177. archive_read_support_filter_all(a);
  178. archive_read_support_format_all(a);
  179. archive_read_open(a, mydata, myopen, myread, myclose);
  180. while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
  181. printf("%s\en",archive_entry_pathname(entry));
  182. archive_read_data_skip(a);
  183. }
  184. archive_read_free(a);
  185. free(mydata);
  186. }
  187. la_ssize_t
  188. myread(struct archive *a, void *client_data, const void **buff)
  189. {
  190. struct mydata *mydata = client_data;
  191. *buff = mydata->buff;
  192. return (read(mydata->fd, mydata->buff, 10240));
  193. }
  194. int
  195. myopen(struct archive *a, void *client_data)
  196. {
  197. struct mydata *mydata = client_data;
  198. mydata->fd = open(mydata->name, O_RDONLY);
  199. return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
  200. }
  201. int
  202. myclose(struct archive *a, void *client_data)
  203. {
  204. struct mydata *mydata = client_data;
  205. if (mydata->fd > 0)
  206. close(mydata->fd);
  207. return (ARCHIVE_OK);
  208. }
  209. .Ed
  210. .\" .Sh ERRORS
  211. .Sh SEE ALSO
  212. .Xr tar 1 ,
  213. .Xr archive_read_data 3 ,
  214. .Xr archive_read_extract 3 ,
  215. .Xr archive_read_filter 3 ,
  216. .Xr archive_read_format 3 ,
  217. .Xr archive_read_header 3 ,
  218. .Xr archive_read_new 3 ,
  219. .Xr archive_read_open 3 ,
  220. .Xr archive_read_set_options 3 ,
  221. .Xr archive_util 3 ,
  222. .Xr libarchive 3 ,
  223. .Xr tar 5
  224. .Sh HISTORY
  225. The
  226. .Nm libarchive
  227. library first appeared in
  228. .Fx 5.3 .
  229. .Sh AUTHORS
  230. .An -nosplit
  231. The
  232. .Nm libarchive
  233. library was written by
  234. .An Tim Kientzle Aq kientzle@acm.org .
  235. .Sh BUGS
  236. Many traditional archiver programs treat
  237. empty files as valid empty archives.
  238. For example, many implementations of
  239. .Xr tar 1
  240. allow you to append entries to an empty file.
  241. Of course, it is impossible to determine the format of an empty file
  242. by inspecting the contents, so this library treats empty files as
  243. having a special
  244. .Dq empty
  245. format.