o2 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. archive_write(3) BSD Library Functions Manual archive_write(3)
  2. NAME
  3. archive_write_new, archive_write_set_format_cpio,
  4. archive_write_set_format_pax, archive_write_set_format_pax_restricted,
  5. archive_write_set_format_shar, archive_write_set_format_shar_binary,
  6. archive_write_set_format_ustar, archive_write_get_bytes_per_block,
  7. archive_write_set_bytes_per_block, archive_write_set_bytes_in_last_block,
  8. archive_write_set_compression_bzip2,
  9. archive_write_set_compression_compress,
  10. archive_write_set_compression_gzip, archive_write_set_compression_none,
  11. archive_write_set_compression_program,
  12. archive_write_set_compressor_options, archive_write_set_format_options,
  13. archive_write_set_options, archive_write_open, archive_write_open_fd,
  14. archive_write_open_FILE, archive_write_open_filename,
  15. archive_write_open_memory, archive_write_header, archive_write_data,
  16. archive_write_finish_entry, archive_write_close, archive_write_finish --
  17. functions for creating archives
  18. SYNOPSIS
  19. #include <archive.h>
  20. struct archive *
  21. archive_write_new(void);
  22. int
  23. archive_write_get_bytes_per_block(struct archive *);
  24. int
  25. archive_write_set_bytes_per_block(struct archive *, int bytes_per_block);
  26. int
  27. archive_write_set_bytes_in_last_block(struct archive *, int);
  28. int
  29. archive_write_set_compression_bzip2(struct archive *);
  30. int
  31. archive_write_set_compression_compress(struct archive *);
  32. int
  33. archive_write_set_compression_gzip(struct archive *);
  34. int
  35. archive_write_set_compression_none(struct archive *);
  36. int
  37. archive_write_set_compression_program(struct archive *,
  38. const char * cmd);
  39. int
  40. archive_write_set_format_cpio(struct archive *);
  41. int
  42. archive_write_set_format_pax(struct archive *);
  43. int
  44. archive_write_set_format_pax_restricted(struct archive *);
  45. int
  46. archive_write_set_format_shar(struct archive *);
  47. int
  48. archive_write_set_format_shar_binary(struct archive *);
  49. int
  50. archive_write_set_format_ustar(struct archive *);
  51. int
  52. archive_write_set_format_options(struct archive *, const char *);
  53. int
  54. archive_write_set_compressor_options(struct archive *, const char *);
  55. int
  56. archive_write_set_options(struct archive *, const char *);
  57. int
  58. archive_write_open(struct archive *, void *client_data,
  59. archive_open_callback *, archive_write_callback *,
  60. archive_close_callback *);
  61. int
  62. archive_write_open_fd(struct archive *, int fd);
  63. int
  64. archive_write_open_FILE(struct archive *, FILE *file);
  65. int
  66. archive_write_open_filename(struct archive *, const char *filename);
  67. int
  68. archive_write_open_memory(struct archive *, void *buffer,
  69. size_t bufferSize, size_t *outUsed);
  70. int
  71. archive_write_header(struct archive *, struct archive_entry *);
  72. ssize_t
  73. archive_write_data(struct archive *, const void *, size_t);
  74. int
  75. archive_write_finish_entry(struct archive *);
  76. int
  77. archive_write_close(struct archive *);
  78. int
  79. archive_write_finish(struct archive *);
  80. DESCRIPTION
  81. These functions provide a complete API for creating streaming archive
  82. files. The general process is to first create the struct archive object,
  83. set any desired options, initialize the archive, append entries, then
  84. close the archive and release all resources. The following summary
  85. describes the functions in approximately the order they are ordinarily
  86. used:
  87. archive_write_new()
  88. Allocates and initializes a struct archive object suitable for
  89. writing a tar archive.
  90. archive_write_set_bytes_per_block()
  91. Sets the block size used for writing the archive data. Every
  92. call to the write callback function, except possibly the last
  93. one, will use this value for the length. The third parameter is
  94. a boolean that specifies whether or not the final block written
  95. will be padded to the full block size. If it is zero, the last
  96. block will not be padded. If it is non-zero, padding will be
  97. added both before and after compression. The default is to use a
  98. block size of 10240 bytes and to pad the last block. Note that a
  99. block size of zero will suppress internal blocking and cause
  100. writes to be sent directly to the write callback as they occur.
  101. archive_write_get_bytes_per_block()
  102. Retrieve the block size to be used for writing. A value of -1
  103. here indicates that the library should use default values. A
  104. value of zero indicates that internal blocking is suppressed.
  105. archive_write_set_bytes_in_last_block()
  106. Sets the block size used for writing the last block. If this
  107. value is zero, the last block will be padded to the same size as
  108. the other blocks. Otherwise, the final block will be padded to a
  109. multiple of this size. In particular, setting it to 1 will cause
  110. the final block to not be padded. For compressed output, any
  111. padding generated by this option is applied only after the com-
  112. pression. The uncompressed data is always unpadded. The default
  113. is to pad the last block to the full block size (note that
  114. archive_write_open_filename() will set this based on the file
  115. type). Unlike the other ``set'' functions, this function can be
  116. called after the archive is opened.
  117. archive_write_get_bytes_in_last_block()
  118. Retrieve the currently-set value for last block size. A value of
  119. -1 here indicates that the library should use default values.
  120. archive_write_set_format_cpio(), archive_write_set_format_pax(),
  121. archive_write_set_format_pax_restricted(),
  122. archive_write_set_format_shar(),
  123. archive_write_set_format_shar_binary(),
  124. archive_write_set_format_ustar()
  125. Sets the format that will be used for the archive. The library
  126. can write POSIX octet-oriented cpio format archives, POSIX-stan-
  127. dard ``pax interchange'' format archives, traditional ``shar''
  128. archives, enhanced ``binary'' shar archives that store a variety
  129. of file attributes and handle binary files, and POSIX-standard
  130. ``ustar'' archives. The pax interchange format is a backwards-
  131. compatible tar format that adds key/value attributes to each
  132. entry and supports arbitrary filenames, linknames, uids, sizes,
  133. etc. ``Restricted pax interchange format'' is the library
  134. default; this is the same as pax format, but suppresses the pax
  135. extended header for most normal files. In most cases, this will
  136. result in ordinary ustar archives.
  137. archive_write_set_compression_bzip2(),
  138. archive_write_set_compression_compress(),
  139. archive_write_set_compression_gzip(),
  140. archive_write_set_compression_none()
  141. The resulting archive will be compressed as specified. Note that
  142. the compressed output is always properly blocked.
  143. archive_write_set_compression_program()
  144. The archive will be fed into the specified compression program.
  145. The output of that program is blocked and written to the client
  146. write callbacks.
  147. archive_write_set_compressor_options(),
  148. archive_write_set_format_options(), archive_write_set_options()
  149. Specifies options that will be passed to the currently-enabled
  150. compressor and/or format writer. The argument is a comma-sepa-
  151. rated list of individual options. Individual options have one of
  152. the following forms:
  153. option=value
  154. The option/value pair will be provided to every module.
  155. Modules that do not accept an option with this name will
  156. ignore it.
  157. option The option will be provided to every module with a value
  158. of ``1''.
  159. !option
  160. The option will be provided to every module with a NULL
  161. value.
  162. module:option=value, module:option, module:!option
  163. As above, but the corresponding option and value will be
  164. provided only to modules whose name matches module.
  165. The return value will be ARCHIVE_OK if any module accepts the
  166. option, or ARCHIVE_WARN if no module accepted the option, or
  167. ARCHIVE_FATAL if there was a fatal error while attempting to
  168. process the option.
  169. The currently supported options are:
  170. Compressor gzip
  171. compression-level
  172. The value is interpreted as a decimal integer
  173. specifying the gzip compression level.
  174. Compressor xz
  175. compression-level
  176. The value is interpreted as a decimal integer
  177. specifying the compression level.
  178. Format mtree
  179. cksum, device, flags, gid, gname, indent, link, md5,
  180. mode, nlink, rmd160, sha1, sha256, sha384,
  181. sha512, size, time, uid, uname
  182. Enable a particular keyword in the mtree output.
  183. Prefix with an exclamation mark to disable the
  184. corresponding keyword. The default is equivalent
  185. to ``device, flags, gid, gname, link, mode,
  186. nlink, size, time, type, uid, uname''.
  187. all Enables all of the above keywords.
  188. use-set
  189. Enables generation of /set lines that specify
  190. default values for the following files and/or
  191. directories.
  192. indent XXX needs explanation XXX
  193. archive_write_open()
  194. Freeze the settings, open the archive, and prepare for writing
  195. entries. This is the most generic form of this function, which
  196. accepts pointers to three callback functions which will be
  197. invoked by the compression layer to write the constructed ar-
  198. chive.
  199. archive_write_open_fd()
  200. A convenience form of archive_write_open() that accepts a file
  201. descriptor. The archive_write_open_fd() function is safe for use
  202. with tape drives or other block-oriented devices.
  203. archive_write_open_FILE()
  204. A convenience form of archive_write_open() that accepts a FILE *
  205. pointer. Note that archive_write_open_FILE() is not safe for
  206. writing to tape drives or other devices that require correct
  207. blocking.
  208. archive_write_open_file()
  209. A deprecated synonym for archive_write_open_filename().
  210. archive_write_open_filename()
  211. A convenience form of archive_write_open() that accepts a file-
  212. name. A NULL argument indicates that the output should be writ-
  213. ten to standard output; an argument of ``-'' will open a file
  214. with that name. If you have not invoked
  215. archive_write_set_bytes_in_last_block(), then
  216. archive_write_open_filename() will adjust the last-block padding
  217. depending on the file: it will enable padding when writing to
  218. standard output or to a character or block device node, it will
  219. disable padding otherwise. You can override this by manually
  220. invoking archive_write_set_bytes_in_last_block() before calling
  221. archive_write_open(). The archive_write_open_filename() function
  222. is safe for use with tape drives or other block-oriented devices.
  223. archive_write_open_memory()
  224. A convenience form of archive_write_open() that accepts a pointer
  225. to a block of memory that will receive the archive. The final
  226. size_t * argument points to a variable that will be updated after
  227. each write to reflect how much of the buffer is currently in use.
  228. You should be careful to ensure that this variable remains allo-
  229. cated until after the archive is closed.
  230. archive_write_header()
  231. Build and write a header using the data in the provided struct
  232. archive_entry structure. See archive_entry(3) for information on
  233. creating and populating struct archive_entry objects.
  234. archive_write_data()
  235. Write data corresponding to the header just written. Returns
  236. number of bytes written or -1 on error.
  237. archive_write_finish_entry()
  238. Close out the entry just written. In particular, this writes out
  239. the final padding required by some formats. Ordinarily, clients
  240. never need to call this, as it is called automatically by
  241. archive_write_next_header() and archive_write_close() as needed.
  242. archive_write_close()
  243. Complete the archive and invoke the close callback.
  244. archive_write_finish()
  245. Invokes archive_write_close() if it was not invoked manually,
  246. then releases all resources. Note that this function was
  247. declared to return void in libarchive 1.x, which made it impossi-
  248. ble to detect errors when archive_write_close() was invoked
  249. implicitly from this function. This is corrected beginning with
  250. libarchive 2.0.
  251. More information about the struct archive object and the overall design
  252. of the library can be found in the libarchive(3) overview.
  253. IMPLEMENTATION
  254. Compression support is built-in to libarchive, which uses zlib and bzlib
  255. to handle gzip and bzip2 compression, respectively.
  256. CLIENT CALLBACKS
  257. To use this library, you will need to define and register callback func-
  258. tions that will be invoked to write data to the resulting archive. These
  259. functions are registered by calling archive_write_open():
  260. typedef int archive_open_callback(struct archive *, void
  261. *client_data)
  262. The open callback is invoked by archive_write_open(). It should return
  263. ARCHIVE_OK if the underlying file or data source is successfully opened.
  264. If the open fails, it should call archive_set_error() to register an
  265. error code and message and return ARCHIVE_FATAL.
  266. typedef ssize_t archive_write_callback(struct archive *,
  267. void *client_data, const void *buffer, size_t length)
  268. The write callback is invoked whenever the library needs to write raw
  269. bytes to the archive. For correct blocking, each call to the write call-
  270. back function should translate into a single write(2) system call. This
  271. is especially critical when writing archives to tape drives. On success,
  272. the write callback should return the number of bytes actually written.
  273. On error, the callback should invoke archive_set_error() to register an
  274. error code and message and return -1.
  275. typedef int archive_close_callback(struct archive *, void
  276. *client_data)
  277. The close callback is invoked by archive_close when the archive process-
  278. ing is complete. The callback should return ARCHIVE_OK on success. On
  279. failure, the callback should invoke archive_set_error() to register an
  280. error code and message and return ARCHIVE_FATAL.
  281. EXAMPLE
  282. The following sketch illustrates basic usage of the library. In this
  283. example, the callback functions are simply wrappers around the standard
  284. open(2), write(2), and close(2) system calls.
  285. #include <sys/stat.h>
  286. #include <archive.h>
  287. #include <archive_entry.h>
  288. #include <fcntl.h>
  289. #include <stdlib.h>
  290. #include <unistd.h>
  291. struct mydata {
  292. const char *name;
  293. int fd;
  294. };
  295. int
  296. myopen(struct archive *a, void *client_data)
  297. {
  298. struct mydata *mydata = client_data;
  299. mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644);
  300. if (mydata->fd >= 0)
  301. return (ARCHIVE_OK);
  302. else
  303. return (ARCHIVE_FATAL);
  304. }
  305. ssize_t
  306. mywrite(struct archive *a, void *client_data, const void *buff, size_t n)
  307. {
  308. struct mydata *mydata = client_data;
  309. return (write(mydata->fd, buff, n));
  310. }
  311. int
  312. myclose(struct archive *a, void *client_data)
  313. {
  314. struct mydata *mydata = client_data;
  315. if (mydata->fd > 0)
  316. close(mydata->fd);
  317. return (0);
  318. }
  319. void
  320. write_archive(const char *outname, const char **filename)
  321. {
  322. struct mydata *mydata = malloc(sizeof(struct mydata));
  323. struct archive *a;
  324. struct archive_entry *entry;
  325. struct stat st;
  326. char buff[8192];
  327. int len;
  328. int fd;
  329. a = archive_write_new();
  330. mydata->name = outname;
  331. archive_write_set_compression_gzip(a);
  332. archive_write_set_format_ustar(a);
  333. archive_write_open(a, mydata, myopen, mywrite, myclose);
  334. while (*filename) {
  335. stat(*filename, &st);
  336. entry = archive_entry_new();
  337. archive_entry_copy_stat(entry, &st);
  338. archive_entry_set_pathname(entry, *filename);
  339. archive_write_header(a, entry);
  340. fd = open(*filename, O_RDONLY);
  341. len = read(fd, buff, sizeof(buff));
  342. while ( len > 0 ) {
  343. archive_write_data(a, buff, len);
  344. len = read(fd, buff, sizeof(buff));
  345. }
  346. archive_entry_free(entry);
  347. filename++;
  348. }
  349. archive_write_finish(a);
  350. }
  351. int main(int argc, const char **argv)
  352. {
  353. const char *outname;
  354. argv++;
  355. outname = argv++;
  356. write_archive(outname, argv);
  357. return 0;
  358. }
  359. RETURN VALUES
  360. Most functions return ARCHIVE_OK (zero) on success, or one of several
  361. non-zero error codes for errors. Specific error codes include:
  362. ARCHIVE_RETRY for operations that might succeed if retried, ARCHIVE_WARN
  363. for unusual conditions that do not prevent further operations, and
  364. ARCHIVE_FATAL for serious errors that make remaining operations impossi-
  365. ble. The archive_errno() and archive_error_string() functions can be
  366. used to retrieve an appropriate error code and a textual error message.
  367. archive_write_new() returns a pointer to a newly-allocated struct archive
  368. object.
  369. archive_write_data() returns a count of the number of bytes actually
  370. written. On error, -1 is returned and the archive_errno() and
  371. archive_error_string() functions will return appropriate values. Note
  372. that if the client-provided write callback function returns a non-zero
  373. value, that error will be propagated back to the caller through whatever
  374. API function resulted in that call, which may include
  375. archive_write_header(), archive_write_data(), archive_write_close(), or
  376. archive_write_finish(). The client callback can call archive_set_error()
  377. to provide values that can then be retrieved by archive_errno() and
  378. archive_error_string().
  379. SEE ALSO
  380. tar(1), libarchive(3), tar(5)
  381. HISTORY
  382. The libarchive library first appeared in FreeBSD 5.3.
  383. AUTHORS
  384. The libarchive library was written by Tim Kientzle <[email protected]>.
  385. BUGS
  386. There are many peculiar bugs in historic tar implementations that may
  387. cause certain programs to reject archives written by this library. For
  388. example, several historic implementations calculated header checksums
  389. incorrectly and will thus reject valid archives; GNU tar does not fully
  390. support pax interchange format; some old tar implementations required
  391. specific field terminations.
  392. The default pax interchange format eliminates most of the historic tar
  393. limitations and provides a generic key/value attribute facility for ven-
  394. dor-defined extensions. One oversight in POSIX is the failure to provide
  395. a standard attribute for large device numbers. This library uses
  396. ``SCHILY.devminor'' and ``SCHILY.devmajor'' for device numbers that
  397. exceed the range supported by the backwards-compatible ustar header.
  398. These keys are compatible with Joerg Schilling's star archiver. Other
  399. implementations may not recognize these keys and will thus be unable to
  400. correctly restore device nodes with large device numbers from archives
  401. created by this library.
  402. BSD May 11, 2008 BSD