ManPageArchiveRead3.wiki 6.3 KB

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