archive_write.3 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. .TH ARCHIVE_WRITE 3 "February 2, 2012" ""
  2. .SH NAME
  3. .ad l
  4. \fB\%archive_write\fP
  5. \- functions for creating 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 creating streaming
  15. archive files.
  16. The general process is to first create the
  17. Tn struct archive
  18. object, set any desired options, initialize the archive, append entries, then
  19. close the archive and release all resources.
  20. .SS Create archive object
  21. See
  22. \fBarchive_write_new\fP(3).
  23. .PP
  24. To write an archive, you must first obtain an initialized
  25. Tn struct archive
  26. object from
  27. \fB\%archive_write_new\fP().
  28. .SS Enable filters and formats, configure block size and padding
  29. See
  30. \fBarchive_write_filter\fP(3),
  31. \fBarchive_write_format\fP(3)
  32. and
  33. \fBarchive_write_blocksize\fP(3).
  34. .PP
  35. You can then modify this object for the desired operations with the
  36. various
  37. \fB\%archive_write_set_XXX\fP()
  38. functions.
  39. In particular, you will need to invoke appropriate
  40. \fB\%archive_write_add_XXX\fP()
  41. and
  42. \fB\%archive_write_set_XXX\fP()
  43. functions to enable the corresponding compression and format
  44. support.
  45. .SS Set options
  46. See
  47. \fBarchive_write_set_options\fP(3).
  48. .SS Open archive
  49. See
  50. \fBarchive_write_open\fP(3).
  51. .PP
  52. Once you have prepared the
  53. Tn struct archive
  54. object, you call
  55. \fB\%archive_write_open\fP()
  56. to actually open the archive and prepare it for writing.
  57. There are several variants of this function;
  58. the most basic expects you to provide pointers to several
  59. functions that can provide blocks of bytes from the archive.
  60. There are convenience forms that allow you to
  61. specify a filename, file descriptor,
  62. \fIFILE *\fP
  63. object, or a block of memory from which to write the archive data.
  64. .SS Produce archive
  65. See
  66. \fBarchive_write_header\fP(3)
  67. and
  68. \fBarchive_write_data\fP(3).
  69. .PP
  70. Individual archive entries are written in a three-step
  71. process:
  72. You first initialize a
  73. Tn struct archive_entry
  74. structure with information about the new entry.
  75. At a minimum, you should set the pathname of the
  76. entry and provide a
  77. \fIstruct\fP stat
  78. with a valid
  79. \fIst_mode\fP
  80. field, which specifies the type of object and
  81. \fIst_size\fP
  82. field, which specifies the size of the data portion of the object.
  83. .SS Release resources
  84. See
  85. \fBarchive_write_free\fP(3).
  86. .PP
  87. After all entries have been written, use the
  88. \fB\%archive_write_free\fP()
  89. function to release all resources.
  90. .SH EXAMPLES
  91. .ad l
  92. The following sketch illustrates basic usage of the library.
  93. In this example,
  94. the callback functions are simply wrappers around the standard
  95. \fBopen\fP(2),
  96. \fBwrite\fP(2),
  97. and
  98. \fBclose\fP(2)
  99. system calls.
  100. .RS 4
  101. .nf
  102. #ifdef __linux__
  103. #define _FILE_OFFSET_BITS 64
  104. #endif
  105. #include <sys/stat.h>
  106. #include <archive.h>
  107. #include <archive_entry.h>
  108. #include <fcntl.h>
  109. #include <stdlib.h>
  110. #include <unistd.h>
  111. struct mydata {
  112. const char *name;
  113. int fd;
  114. };
  115. int
  116. myopen(struct archive *a, void *client_data)
  117. {
  118. struct mydata *mydata = client_data;
  119. mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644);
  120. if (mydata->fd >= 0)
  121. return (ARCHIVE_OK);
  122. else
  123. return (ARCHIVE_FATAL);
  124. }
  125. la_ssize_t
  126. mywrite(struct archive *a, void *client_data, const void *buff, size_t n)
  127. {
  128. struct mydata *mydata = client_data;
  129. return (write(mydata->fd, buff, n));
  130. }
  131. int
  132. myclose(struct archive *a, void *client_data)
  133. {
  134. struct mydata *mydata = client_data;
  135. if (mydata->fd > 0)
  136. close(mydata->fd);
  137. return (0);
  138. }
  139. void
  140. write_archive(const char *outname, const char **filename)
  141. {
  142. struct mydata *mydata = malloc(sizeof(struct mydata));
  143. struct archive *a;
  144. struct archive_entry *entry;
  145. struct stat st;
  146. char buff[8192];
  147. int len;
  148. int fd;
  149. a = archive_write_new();
  150. mydata->name = outname;
  151. /* Set archive format and filter according to output file extension.
  152. * If it fails, set default format. Platform depended function.
  153. * See supported formats in archive_write_set_format_filter_by_ext.c */
  154. if (archive_write_set_format_filter_by_ext(a, outname) != ARCHIVE_OK) {
  155. archive_write_add_filter_gzip(a);
  156. archive_write_set_format_ustar(a);
  157. }
  158. archive_write_open(a, mydata, myopen, mywrite, myclose);
  159. while (*filename) {
  160. stat(*filename, &st);
  161. entry = archive_entry_new();
  162. archive_entry_copy_stat(entry, &st);
  163. archive_entry_set_pathname(entry, *filename);
  164. archive_write_header(a, entry);
  165. if ((fd = open(*filename, O_RDONLY)) != -1) {
  166. len = read(fd, buff, sizeof(buff));
  167. while (len > 0) {
  168. archive_write_data(a, buff, len);
  169. len = read(fd, buff, sizeof(buff));
  170. }
  171. close(fd);
  172. }
  173. archive_entry_free(entry);
  174. filename++;
  175. }
  176. archive_write_free(a);
  177. }
  178. int main(int argc, const char **argv)
  179. {
  180. const char *outname;
  181. argv++;
  182. outname = *argv++;
  183. write_archive(outname, argv);
  184. return 0;
  185. }
  186. .RE
  187. .SH SEE ALSO
  188. .ad l
  189. \fBtar\fP(1),
  190. \fBarchive_write_set_options\fP(3),
  191. \fBlibarchive\fP(3),
  192. \fBcpio\fP(5),
  193. \fBmtree\fP(5),
  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. There are many peculiar bugs in historic tar implementations that may cause
  211. certain programs to reject archives written by this library.
  212. For example, several historic implementations calculated header checksums
  213. incorrectly and will thus reject valid archives; GNU tar does not fully support
  214. pax interchange format; some old tar implementations required specific
  215. field terminations.
  216. .PP
  217. The default pax interchange format eliminates most of the historic
  218. tar limitations and provides a generic key/value attribute facility
  219. for vendor-defined extensions.
  220. One oversight in POSIX is the failure to provide a standard attribute
  221. for large device numbers.
  222. This library uses
  223. ``SCHILY.devminor''
  224. and
  225. ``SCHILY.devmajor''
  226. for device numbers that exceed the range supported by the backwards-compatible
  227. ustar header.
  228. These keys are compatible with Joerg Schilling's
  229. \fB\%star\fP
  230. archiver.
  231. Other implementations may not recognize these keys and will thus be unable
  232. to correctly restore device nodes with large device numbers from archives
  233. created by this library.