archive_read.3 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. .TH ARCHIVE_READ 3 "February 2, 2012" ""
  2. .SH NAME
  3. .ad l
  4. \fB\%archive_read\fP
  5. \- functions for reading streaming archives
  6. .SH LIBRARY
  7. .ad l
  8. Streaming Archive Library (libarchive, -larchive)
  9. .SH SYNOPSIS
  10. .ad l
  11. \fB#include <archive.h>\fP
  12. .SH DESCRIPTION
  13. .ad l
  14. These functions provide a complete API for reading streaming archives.
  15. The general process is to first create the
  16. Tn struct archive
  17. object, set options, initialize the reader, iterate over the archive
  18. headers and associated data, then close the archive and release all
  19. resources.
  20. .SS Create archive object
  21. See
  22. \fBarchive_read_new\fP(3).
  23. .PP
  24. To read an archive, you must first obtain an initialized
  25. Tn struct archive
  26. object from
  27. \fB\%archive_read_new\fP().
  28. .SS Enable filters and formats
  29. See
  30. \fBarchive_read_filter\fP(3)
  31. and
  32. \fBarchive_read_format\fP(3).
  33. .PP
  34. You can then modify this object for the desired operations with the
  35. various
  36. \fB\%archive_read_set_XXX\fP()
  37. and
  38. \fB\%archive_read_support_XXX\fP()
  39. functions.
  40. In particular, you will need to invoke appropriate
  41. \fB\%archive_read_support_XXX\fP()
  42. functions to enable the corresponding compression and format
  43. support.
  44. Note that these latter functions perform two distinct operations:
  45. they cause the corresponding support code to be linked into your
  46. program, and they enable the corresponding auto-detect code.
  47. Unless you have specific constraints, you will generally want
  48. to invoke
  49. \fB\%archive_read_support_filter_all\fP()
  50. and
  51. \fB\%archive_read_support_format_all\fP()
  52. to enable auto-detect for all formats and compression types
  53. currently supported by the library.
  54. .SS Set options
  55. See
  56. \fBarchive_read_set_options\fP(3).
  57. .SS Open archive
  58. See
  59. \fBarchive_read_open\fP(3).
  60. .PP
  61. Once you have prepared the
  62. Tn struct archive
  63. object, you call
  64. \fB\%archive_read_open\fP()
  65. to actually open the archive and prepare it for reading.
  66. There are several variants of this function;
  67. the most basic expects you to provide pointers to several
  68. functions that can provide blocks of bytes from the archive.
  69. There are convenience forms that allow you to
  70. specify a filename, file descriptor,
  71. \fIFILE *\fP
  72. object, or a block of memory from which to read the archive data.
  73. Note that the core library makes no assumptions about the
  74. size of the blocks read;
  75. callback functions are free to read whatever block size is
  76. most appropriate for the medium.
  77. .SS Consume archive
  78. See
  79. \fBarchive_read_header\fP(3),
  80. \fBarchive_read_data\fP(3)
  81. and
  82. \fBarchive_read_extract\fP(3).
  83. .PP
  84. Each archive entry consists of a header followed by a certain
  85. amount of data.
  86. You can obtain the next header with
  87. \fB\%archive_read_next_header\fP(),
  88. which returns a pointer to an
  89. Tn struct archive_entry
  90. structure with information about the current archive element.
  91. If the entry is a regular file, then the header will be followed
  92. by the file data.
  93. You can use
  94. \fB\%archive_read_data\fP()
  95. (which works much like the
  96. \fBread\fP(2)
  97. system call)
  98. to read this data from the archive, or
  99. \fB\%archive_read_data_block\fP()
  100. which provides a slightly more efficient interface.
  101. You may prefer to use the higher-level
  102. \fB\%archive_read_data_skip\fP(),
  103. which reads and discards the data for this entry,
  104. \fB\%archive_read_data_into_fd\fP(),
  105. which copies the data to the provided file descriptor, or
  106. \fB\%archive_read_extract\fP(),
  107. which recreates the specified entry on disk and copies data
  108. from the archive.
  109. In particular, note that
  110. \fB\%archive_read_extract\fP()
  111. uses the
  112. Tn struct archive_entry
  113. structure that you provide it, which may differ from the
  114. entry just read from the archive.
  115. In particular, many applications will want to override the
  116. pathname, file permissions, or ownership.
  117. .SS Release resources
  118. See
  119. \fBarchive_read_free\fP(3).
  120. .PP
  121. Once you have finished reading data from the archive, you
  122. should call
  123. \fB\%archive_read_close\fP()
  124. to close the archive, then call
  125. \fB\%archive_read_free\fP()
  126. to release all resources, including all memory allocated by the library.
  127. .SH EXAMPLES
  128. .ad l
  129. The following illustrates basic usage of the library.
  130. In this example,
  131. the callback functions are simply wrappers around the standard
  132. \fBopen\fP(2),
  133. \fBread\fP(2),
  134. and
  135. \fBclose\fP(2)
  136. system calls.
  137. .RS 4
  138. .nf
  139. void
  140. list_archive(const char *name)
  141. {
  142. struct mydata *mydata;
  143. struct archive *a;
  144. struct archive_entry *entry;
  145. mydata = malloc(sizeof(struct mydata));
  146. a = archive_read_new();
  147. mydata->name = name;
  148. archive_read_support_filter_all(a);
  149. archive_read_support_format_all(a);
  150. archive_read_open(a, mydata, myopen, myread, myclose);
  151. while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
  152. printf("%s\en",archive_entry_pathname(entry));
  153. archive_read_data_skip(a);
  154. }
  155. archive_read_free(a);
  156. free(mydata);
  157. }
  158. la_ssize_t
  159. myread(struct archive *a, void *client_data, const void **buff)
  160. {
  161. struct mydata *mydata = client_data;
  162. *buff = mydata->buff;
  163. return (read(mydata->fd, mydata->buff, 10240));
  164. }
  165. int
  166. myopen(struct archive *a, void *client_data)
  167. {
  168. struct mydata *mydata = client_data;
  169. mydata->fd = open(mydata->name, O_RDONLY);
  170. return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
  171. }
  172. int
  173. myclose(struct archive *a, void *client_data)
  174. {
  175. struct mydata *mydata = client_data;
  176. if (mydata->fd > 0)
  177. close(mydata->fd);
  178. return (ARCHIVE_OK);
  179. }
  180. .RE
  181. .SH SEE ALSO
  182. .ad l
  183. \fBtar\fP(1),
  184. \fBarchive_read_data\fP(3),
  185. \fBarchive_read_extract\fP(3),
  186. \fBarchive_read_filter\fP(3),
  187. \fBarchive_read_format\fP(3),
  188. \fBarchive_read_header\fP(3),
  189. \fBarchive_read_new\fP(3),
  190. \fBarchive_read_open\fP(3),
  191. \fBarchive_read_set_options\fP(3),
  192. \fBarchive_util\fP(3),
  193. \fBlibarchive\fP(3),
  194. \fBtar\fP(5)
  195. .SH HISTORY
  196. .ad l
  197. The
  198. \fB\%libarchive\fP
  199. library first appeared in
  200. FreeBSD 5.3.
  201. .SH AUTHORS
  202. .ad l
  203. -nosplit
  204. The
  205. \fB\%libarchive\fP
  206. library was written by
  207. Tim Kientzle \%<[email protected].>
  208. .SH BUGS
  209. .ad l
  210. Many traditional archiver programs treat
  211. empty files as valid empty archives.
  212. For example, many implementations of
  213. \fBtar\fP(1)
  214. allow you to append entries to an empty file.
  215. Of course, it is impossible to determine the format of an empty file
  216. by inspecting the contents, so this library treats empty files as
  217. having a special
  218. ``empty''
  219. format.