archive_write.3.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <!-- Creator : groff version 1.22.4 -->
  2. <!-- CreationDate: Tue Jul 18 07:11:05 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_WRITE(3) BSD Library Functions Manual
  21. ARCHIVE_WRITE(3)</p>
  22. <p style="margin-top: 1em"><b>NAME</b></p>
  23. <p style="margin-left:6%;"><b>archive_write</b> &mdash;
  24. functions for creating 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 creating streaming archive files. The
  34. general process is to first create the struct archive
  35. object, set any desired options, initialize the archive,
  36. append entries, then close the archive and release all
  37. resources.</p>
  38. <p style="margin-left:6%; margin-top: 1em"><b>Create
  39. archive object</b> <br>
  40. See archive_write_new(3).</p>
  41. <p style="margin-left:6%; margin-top: 1em">To write an
  42. archive, you must first obtain an initialized struct archive
  43. object from <b>archive_write_new</b>().</p>
  44. <p style="margin-left:6%; margin-top: 1em"><b>Enable
  45. filters and formats, configure block size and padding</b>
  46. <br>
  47. See archive_write_filter(3), archive_write_format(3) and
  48. archive_write_blocksize(3).</p>
  49. <p style="margin-left:6%; margin-top: 1em">You can then
  50. modify this object for the desired operations with the
  51. various <b>archive_write_set_XXX</b>() functions. In
  52. particular, you will need to invoke appropriate
  53. <b>archive_write_add_XXX</b>() and
  54. <b>archive_write_set_XXX</b>() functions to enable the
  55. corresponding compression and format support.</p>
  56. <p style="margin-left:6%; margin-top: 1em"><b>Set
  57. options</b> <br>
  58. See archive_write_set_options(3).</p>
  59. <p style="margin-left:6%; margin-top: 1em"><b>Open
  60. archive</b> <br>
  61. See archive_write_open(3).</p>
  62. <p style="margin-left:6%; margin-top: 1em">Once you have
  63. prepared the struct archive object, you call
  64. <b>archive_write_open</b>() to actually open the archive and
  65. prepare it for writing. There are several variants of this
  66. function; the most basic expects you to provide pointers to
  67. several functions that can provide blocks of bytes from the
  68. archive. There are convenience forms that allow you to
  69. specify a filename, file descriptor, <i>FILE *</i> object,
  70. or a block of memory from which to write the archive
  71. data.</p>
  72. <p style="margin-left:6%; margin-top: 1em"><b>Produce
  73. archive</b> <br>
  74. See archive_write_header(3) and archive_write_data(3).</p>
  75. <p style="margin-left:6%; margin-top: 1em">Individual
  76. archive entries are written in a three-step process: You
  77. first initialize a struct archive_entry structure with
  78. information about the new entry. At a minimum, you should
  79. set the pathname of the entry and provide a <i>struct
  80. stat</i> with a valid <i>st_mode</i> field, which specifies
  81. the type of object and <i>st_size</i> field, which specifies
  82. the size of the data portion of the object.</p>
  83. <p style="margin-left:6%; margin-top: 1em"><b>Release
  84. resources</b> <br>
  85. See archive_write_free(3).</p>
  86. <p style="margin-left:6%; margin-top: 1em">After all
  87. entries have been written, use the
  88. <b>archive_write_free</b>() function to release all
  89. resources.</p>
  90. <p style="margin-top: 1em"><b>EXAMPLES</b></p>
  91. <p style="margin-left:6%;">The following sketch illustrates
  92. basic usage of the library. In this example, the callback
  93. functions are simply wrappers around the standard open(2),
  94. write(2), and close(2) system calls.</p>
  95. <p style="margin-left:14%; margin-top: 1em">#ifdef
  96. __linux__</p>
  97. <table width="100%" border="0" rules="none" frame="void"
  98. cellspacing="0" cellpadding="0">
  99. <tr valign="top" align="left">
  100. <td width="14%"></td>
  101. <td width="10%">
  102. <p>#define</p></td>
  103. <td width="11%">
  104. <p>_FILE_OFFSET_BITS 64</p></td>
  105. <td width="65%">
  106. </td></tr>
  107. </table>
  108. <p style="margin-left:14%;">#endif <br>
  109. #include &lt;sys/stat.h&gt; <br>
  110. #include &lt;archive.h&gt; <br>
  111. #include &lt;archive_entry.h&gt; <br>
  112. #include &lt;fcntl.h&gt; <br>
  113. #include &lt;stdlib.h&gt; <br>
  114. #include &lt;unistd.h&gt;</p>
  115. <p style="margin-left:14%; margin-top: 1em">struct mydata {
  116. <br>
  117. const char *name; <br>
  118. int fd; <br>
  119. };</p>
  120. <p style="margin-left:14%; margin-top: 1em">int <br>
  121. myopen(struct archive *a, void *client_data) <br>
  122. { <br>
  123. struct mydata *mydata = client_data;</p>
  124. <p style="margin-left:14%; margin-top: 1em">mydata-&gt;fd =
  125. open(mydata-&gt;name, O_WRONLY | O_CREAT, 0644); <br>
  126. if (mydata-&gt;fd &gt;= 0) <br>
  127. return (ARCHIVE_OK); <br>
  128. else <br>
  129. return (ARCHIVE_FATAL); <br>
  130. }</p>
  131. <p style="margin-left:14%; margin-top: 1em">la_ssize_t <br>
  132. mywrite(struct archive *a, void *client_data, const void
  133. *buff, size_t n) <br>
  134. { <br>
  135. struct mydata *mydata = client_data;</p>
  136. <p style="margin-left:14%; margin-top: 1em">return
  137. (write(mydata-&gt;fd, buff, n)); <br>
  138. }</p>
  139. <p style="margin-left:14%; margin-top: 1em">int <br>
  140. myclose(struct archive *a, void *client_data) <br>
  141. { <br>
  142. struct mydata *mydata = client_data;</p>
  143. <p style="margin-left:14%; margin-top: 1em">if
  144. (mydata-&gt;fd &gt; 0) <br>
  145. close(mydata-&gt;fd); <br>
  146. return (0); <br>
  147. }</p>
  148. <p style="margin-left:14%; margin-top: 1em">void <br>
  149. write_archive(const char *outname, const char **filename)
  150. <br>
  151. { <br>
  152. struct mydata *mydata = malloc(sizeof(struct mydata)); <br>
  153. struct archive *a; <br>
  154. struct archive_entry *entry; <br>
  155. struct stat st; <br>
  156. char buff[8192]; <br>
  157. int len; <br>
  158. int fd;</p>
  159. <p style="margin-left:14%; margin-top: 1em">a =
  160. archive_write_new(); <br>
  161. mydata-&gt;name = outname; <br>
  162. /* Set archive format and filter according to output file
  163. extension. <br>
  164. * If it fails, set default format. Platform depended
  165. function. <br>
  166. * See supported formats in
  167. archive_write_set_format_filter_by_ext.c */ <br>
  168. if (archive_write_set_format_filter_by_ext(a, outname) !=
  169. ARCHIVE_OK) { <br>
  170. archive_write_add_filter_gzip(a); <br>
  171. archive_write_set_format_ustar(a); <br>
  172. } <br>
  173. archive_write_open(a, mydata, myopen, mywrite, myclose);
  174. <br>
  175. while (*filename) { <br>
  176. stat(*filename, &amp;st); <br>
  177. entry = archive_entry_new(); <br>
  178. archive_entry_copy_stat(entry, &amp;st); <br>
  179. archive_entry_set_pathname(entry, *filename); <br>
  180. archive_write_header(a, entry); <br>
  181. if ((fd = open(*filename, O_RDONLY)) != -1) { <br>
  182. len = read(fd, buff, sizeof(buff)); <br>
  183. while (len &gt; 0) { <br>
  184. archive_write_data(a, buff, len); <br>
  185. len = read(fd, buff, sizeof(buff)); <br>
  186. } <br>
  187. close(fd); <br>
  188. } <br>
  189. archive_entry_free(entry); <br>
  190. filename++; <br>
  191. } <br>
  192. archive_write_free(a); <br>
  193. }</p>
  194. <p style="margin-left:14%; margin-top: 1em">int main(int
  195. argc, const char **argv) <br>
  196. { <br>
  197. const char *outname; <br>
  198. argv++; <br>
  199. outname = *argv++; <br>
  200. write_archive(outname, argv); <br>
  201. return 0; <br>
  202. }</p>
  203. <p style="margin-top: 1em"><b>SEE ALSO</b></p>
  204. <p style="margin-left:6%;">tar(1),
  205. archive_write_set_options(3), libarchive(3), cpio(5),
  206. mtree(5), tar(5)</p>
  207. <p style="margin-top: 1em"><b>HISTORY</b></p>
  208. <p style="margin-left:6%;">The <b>libarchive</b> library
  209. first appeared in FreeBSD&nbsp;5.3.</p>
  210. <p style="margin-top: 1em"><b>AUTHORS</b></p>
  211. <p style="margin-left:6%;">The <b>libarchive</b> library
  212. was written by Tim Kientzle &lt;[email protected]&gt;.</p>
  213. <p style="margin-top: 1em"><b>BUGS</b></p>
  214. <p style="margin-left:6%;">There are many peculiar bugs in
  215. historic tar implementations that may cause certain programs
  216. to reject archives written by this library. For example,
  217. several historic implementations calculated header checksums
  218. incorrectly and will thus reject valid archives; GNU tar
  219. does not fully support pax interchange format; some old tar
  220. implementations required specific field terminations.</p>
  221. <p style="margin-left:6%; margin-top: 1em">The default pax
  222. interchange format eliminates most of the historic tar
  223. limitations and provides a generic key/value attribute
  224. facility for vendor-defined extensions. One oversight in
  225. POSIX is the failure to provide a standard attribute for
  226. large device numbers. This library uses
  227. &ldquo;SCHILY.devminor&rdquo; and
  228. &ldquo;SCHILY.devmajor&rdquo; for device numbers that exceed
  229. the range supported by the backwards-compatible ustar
  230. header. These keys are compatible with Joerg
  231. Schilling&rsquo;s <b>star</b> archiver. Other
  232. implementations may not recognize these keys and will thus
  233. be unable to correctly restore device nodes with large
  234. device numbers from archives created by this library.</p>
  235. <p style="margin-left:6%; margin-top: 1em">BSD
  236. February&nbsp;2, 2012 BSD</p>
  237. <hr>
  238. </body>
  239. </html>