archive_write.3 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. .\" Copyright (c) 2003-2011 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: head/lib/libarchive/archive_write.3 201110 2009-12-28 03:31:29Z kientzle $
  26. .\"
  27. .Dd March 23, 2011
  28. .Dt ARCHIVE_WRITE 3
  29. .Os
  30. .Sh NAME
  31. .Nm archive_write
  32. .Nd functions for creating archives
  33. .Sh SYNOPSIS
  34. .In archive.h
  35. .Sh DESCRIPTION
  36. These functions provide a complete API for creating streaming
  37. archive files.
  38. The general process is to first create the
  39. .Tn struct archive
  40. object, set any desired options, initialize the archive, append entries, then
  41. close the archive and release all resources.
  42. .\"
  43. .Ss Create archive object
  44. See
  45. .Xr archive_write_new 3 .
  46. .Pp
  47. To write an archive, you must first obtain an initialized
  48. .Tn struct archive
  49. object from
  50. .Fn archive_write_new .
  51. .\"
  52. .Ss Enable filters and formats, configure block size and padding
  53. See
  54. .Xr archive_write_filter 3 ,
  55. .Xr archive_write_format 3
  56. and
  57. .Xr archive_write_blocksize 3 .
  58. .Pp
  59. You can then modify this object for the desired operations with the
  60. various
  61. .Fn archive_write_set_XXX
  62. functions.
  63. In particular, you will need to invoke appropriate
  64. .Fn archive_write_add_XXX
  65. and
  66. .Fn archive_write_set_XXX
  67. functions to enable the corresponding compression and format
  68. support.
  69. .\"
  70. .Ss Set options
  71. See
  72. .Xr archive_read_set_options 3 .
  73. .\"
  74. .Ss Open archive
  75. See
  76. .Xr archive_write_open 3 .
  77. .Pp
  78. Once you have prepared the
  79. .Tn struct archive
  80. object, you call
  81. .Fn archive_write_open
  82. to actually open the archive and prepare it for writing.
  83. There are several variants of this function;
  84. the most basic expects you to provide pointers to several
  85. functions that can provide blocks of bytes from the archive.
  86. There are convenience forms that allow you to
  87. specify a filename, file descriptor,
  88. .Ft "FILE *"
  89. object, or a block of memory from which to write the archive data.
  90. .\"
  91. .Ss Produce archive
  92. See
  93. .Xr archive_write_header 3
  94. and
  95. .Xr archive_write_data 3 .
  96. .Pp
  97. Individual archive entries are written in a three-step
  98. process:
  99. You first initialize a
  100. .Tn struct archive_entry
  101. structure with information about the new entry.
  102. At a minimum, you should set the pathname of the
  103. entry and provide a
  104. .Va struct stat
  105. with a valid
  106. .Va st_mode
  107. field, which specifies the type of object and
  108. .Va st_size
  109. field, which specifies the size of the data portion of the object.
  110. .\"
  111. .Ss Release resources
  112. See
  113. .Xr archive_write_free 3 .
  114. .Pp
  115. After all entries have been written, use the
  116. .Fn archive_write_free
  117. function to release all resources.
  118. .\"
  119. .Sh EXAMPLE
  120. The following sketch illustrates basic usage of the library.
  121. In this example,
  122. the callback functions are simply wrappers around the standard
  123. .Xr open 2 ,
  124. .Xr write 2 ,
  125. and
  126. .Xr close 2
  127. system calls.
  128. .Bd -literal -offset indent
  129. #ifdef __linux__
  130. #define _FILE_OFFSET_BITS 64
  131. #endif
  132. #include <sys/stat.h>
  133. #include <archive.h>
  134. #include <archive_entry.h>
  135. #include <fcntl.h>
  136. #include <stdlib.h>
  137. #include <unistd.h>
  138. struct mydata {
  139. const char *name;
  140. int fd;
  141. };
  142. int
  143. myopen(struct archive *a, void *client_data)
  144. {
  145. struct mydata *mydata = client_data;
  146. mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644);
  147. if (mydata->fd >= 0)
  148. return (ARCHIVE_OK);
  149. else
  150. return (ARCHIVE_FATAL);
  151. }
  152. ssize_t
  153. mywrite(struct archive *a, void *client_data, const void *buff, size_t n)
  154. {
  155. struct mydata *mydata = client_data;
  156. return (write(mydata->fd, buff, n));
  157. }
  158. int
  159. myclose(struct archive *a, void *client_data)
  160. {
  161. struct mydata *mydata = client_data;
  162. if (mydata->fd > 0)
  163. close(mydata->fd);
  164. return (0);
  165. }
  166. void
  167. write_archive(const char *outname, const char **filename)
  168. {
  169. struct mydata *mydata = malloc(sizeof(struct mydata));
  170. struct archive *a;
  171. struct archive_entry *entry;
  172. struct stat st;
  173. char buff[8192];
  174. int len;
  175. int fd;
  176. a = archive_write_new();
  177. mydata->name = outname;
  178. archive_write_add_filter_gzip(a);
  179. archive_write_set_format_ustar(a);
  180. archive_write_open(a, mydata, myopen, mywrite, myclose);
  181. while (*filename) {
  182. stat(*filename, &st);
  183. entry = archive_entry_new();
  184. archive_entry_copy_stat(entry, &st);
  185. archive_entry_set_pathname(entry, *filename);
  186. archive_write_header(a, entry);
  187. if ((fd = open(*filename, O_RDONLY)) != -1) {
  188. len = read(fd, buff, sizeof(buff));
  189. while ( len > 0 ) {
  190. archive_write_data(a, buff, len);
  191. len = read(fd, buff, sizeof(buff));
  192. }
  193. close(fd);
  194. }
  195. archive_entry_free(entry);
  196. filename++;
  197. }
  198. archive_write_free(a);
  199. }
  200. int main(int argc, const char **argv)
  201. {
  202. const char *outname;
  203. argv++;
  204. outname = argv++;
  205. write_archive(outname, argv);
  206. return 0;
  207. }
  208. .Ed
  209. .Sh SEE ALSO
  210. .Xr tar 1 ,
  211. .Xr libarchive 3 ,
  212. .Xr archive_write_set_options 3 ,
  213. .Xr cpio 5 ,
  214. .Xr mtree 5 ,
  215. .Xr tar 5
  216. .Sh HISTORY
  217. The
  218. .Nm libarchive
  219. library first appeared in
  220. .Fx 5.3 .
  221. .Sh AUTHORS
  222. .An -nosplit
  223. The
  224. .Nm libarchive
  225. library was written by
  226. .An Tim Kientzle Aq [email protected] .
  227. .Sh BUGS
  228. There are many peculiar bugs in historic tar implementations that may cause
  229. certain programs to reject archives written by this library.
  230. For example, several historic implementations calculated header checksums
  231. incorrectly and will thus reject valid archives; GNU tar does not fully support
  232. pax interchange format; some old tar implementations required specific
  233. field terminations.
  234. .Pp
  235. The default pax interchange format eliminates most of the historic
  236. tar limitations and provides a generic key/value attribute facility
  237. for vendor-defined extensions.
  238. One oversight in POSIX is the failure to provide a standard attribute
  239. for large device numbers.
  240. This library uses
  241. .Dq SCHILY.devminor
  242. and
  243. .Dq SCHILY.devmajor
  244. for device numbers that exceed the range supported by the backwards-compatible
  245. ustar header.
  246. These keys are compatible with Joerg Schilling's
  247. .Nm star
  248. archiver.
  249. Other implementations may not recognize these keys and will thus be unable
  250. to correctly restore device nodes with large device numbers from archives
  251. created by this library.