ManPageArchiveWrite3.wiki 6.3 KB

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