archive_write.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_write.c,v 1.27 2008/03/14 23:09:02 kientzle Exp $");
  27. /*
  28. * This file contains the "essential" portions of the write API, that
  29. * is, stuff that will essentially always be used by any client that
  30. * actually needs to write an archive. Optional pieces have been, as
  31. * far as possible, separated out into separate files to reduce
  32. * needlessly bloating statically-linked clients.
  33. */
  34. #ifdef HAVE_SYS_WAIT_H
  35. #include <sys/wait.h>
  36. #endif
  37. #ifdef HAVE_ERRNO_H
  38. #include <errno.h>
  39. #endif
  40. #ifdef HAVE_LIMITS_H
  41. #include <limits.h>
  42. #endif
  43. #include <stdio.h>
  44. #ifdef HAVE_STDLIB_H
  45. #include <stdlib.h>
  46. #endif
  47. #ifdef HAVE_STRING_H
  48. #include <string.h>
  49. #endif
  50. #include <time.h>
  51. #ifdef HAVE_UNISTD_H
  52. #include <unistd.h>
  53. #endif
  54. #include "archive.h"
  55. #include "archive_entry.h"
  56. #include "archive_private.h"
  57. #include "archive_write_private.h"
  58. static struct archive_vtable *archive_write_vtable(void);
  59. static int _archive_write_close(struct archive *);
  60. static int _archive_write_finish(struct archive *);
  61. static int _archive_write_header(struct archive *, struct archive_entry *);
  62. static int _archive_write_finish_entry(struct archive *);
  63. static ssize_t _archive_write_data(struct archive *, const void *, size_t);
  64. static struct archive_vtable *
  65. archive_write_vtable(void)
  66. {
  67. static struct archive_vtable av;
  68. static int inited = 0;
  69. if (!inited) {
  70. av.archive_close = _archive_write_close;
  71. av.archive_finish = _archive_write_finish;
  72. av.archive_write_header = _archive_write_header;
  73. av.archive_write_finish_entry = _archive_write_finish_entry;
  74. av.archive_write_data = _archive_write_data;
  75. inited = 1;
  76. }
  77. return (&av);
  78. }
  79. /*
  80. * Allocate, initialize and return an archive object.
  81. */
  82. struct archive *
  83. archive_write_new(void)
  84. {
  85. struct archive_write *a;
  86. unsigned char *nulls;
  87. a = (struct archive_write *)malloc(sizeof(*a));
  88. if (a == NULL)
  89. return (NULL);
  90. memset(a, 0, sizeof(*a));
  91. a->archive.magic = ARCHIVE_WRITE_MAGIC;
  92. a->archive.state = ARCHIVE_STATE_NEW;
  93. a->archive.vtable = archive_write_vtable();
  94. /*
  95. * The value 10240 here matches the traditional tar default,
  96. * but is otherwise arbitrary.
  97. * TODO: Set the default block size from the format selected.
  98. */
  99. a->bytes_per_block = 10240;
  100. a->bytes_in_last_block = -1; /* Default */
  101. /* Initialize a block of nulls for padding purposes. */
  102. a->null_length = 1024;
  103. nulls = (unsigned char *)malloc(a->null_length);
  104. if (nulls == NULL) {
  105. free(a);
  106. return (NULL);
  107. }
  108. memset(nulls, 0, a->null_length);
  109. a->nulls = nulls;
  110. /*
  111. * Set default compression, but don't set a default format.
  112. * Were we to set a default format here, we would force every
  113. * client to link in support for that format, even if they didn't
  114. * ever use it.
  115. */
  116. archive_write_set_compression_none(&a->archive);
  117. return (&a->archive);
  118. }
  119. /*
  120. * Set write options for the format. Returns 0 if successful.
  121. */
  122. int
  123. archive_write_set_format_options(struct archive *_a, const char *s)
  124. {
  125. struct archive_write *a = (struct archive_write *)_a;
  126. char key[64], val[64];
  127. int len, r, ret = ARCHIVE_OK;
  128. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  129. ARCHIVE_STATE_NEW, "archive_write_set_format_options");
  130. archive_clear_error(&a->archive);
  131. if (s == NULL || *s == '\0')
  132. return (ARCHIVE_OK);
  133. if (a->format_options == NULL)
  134. /* This format does not support option. */
  135. return (ARCHIVE_OK);
  136. while ((len = __archive_parse_options(s, a->format_name,
  137. sizeof(key), key, sizeof(val), val)) > 0) {
  138. if (val[0] == '\0')
  139. r = a->format_options(a, key, NULL);
  140. else
  141. r = a->format_options(a, key, val);
  142. if (r == ARCHIVE_FATAL)
  143. return (r);
  144. if (r < ARCHIVE_OK) { /* This key was not handled. */
  145. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  146. "Unsupported option ``%s''", key);
  147. ret = ARCHIVE_WARN;
  148. }
  149. s += len;
  150. }
  151. if (len < 0) {
  152. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  153. "Malformed options string.");
  154. return (ARCHIVE_WARN);
  155. }
  156. return (ret);
  157. }
  158. /*
  159. * Set write options for the compressor. Returns 0 if successful.
  160. */
  161. int
  162. archive_write_set_compressor_options(struct archive *_a, const char *s)
  163. {
  164. struct archive_write *a = (struct archive_write *)_a;
  165. char key[64], val[64];
  166. int len, r;
  167. int ret = ARCHIVE_OK;
  168. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  169. ARCHIVE_STATE_NEW, "archive_write_set_compressor_options");
  170. archive_clear_error(&a->archive);
  171. if (s == NULL || *s == '\0')
  172. return (ARCHIVE_OK);
  173. if (a->compressor.options == NULL) {
  174. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  175. "Unsupported option ``%s''", s);
  176. /* This compressor does not support option. */
  177. return (ARCHIVE_WARN);
  178. }
  179. while ((len = __archive_parse_options(s, a->archive.compression_name,
  180. sizeof(key), key, sizeof(val), val)) > 0) {
  181. if (val[0] == '\0')
  182. r = a->compressor.options(a, key, NULL);
  183. else
  184. r = a->compressor.options(a, key, val);
  185. if (r == ARCHIVE_FATAL)
  186. return (r);
  187. if (r < ARCHIVE_OK) {
  188. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  189. "Unsupported option ``%s''", key);
  190. ret = ARCHIVE_WARN;
  191. }
  192. s += len;
  193. }
  194. if (len < 0) {
  195. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  196. "Illegal format options.");
  197. return (ARCHIVE_WARN);
  198. }
  199. return (ret);
  200. }
  201. /*
  202. * Set write options for the format and the compressor. Returns 0 if successful.
  203. */
  204. int
  205. archive_write_set_options(struct archive *_a, const char *s)
  206. {
  207. int r1, r2;
  208. r1 = archive_write_set_format_options(_a, s);
  209. if (r1 < ARCHIVE_WARN)
  210. return (r1);
  211. r2 = archive_write_set_compressor_options(_a, s);
  212. if (r2 < ARCHIVE_WARN)
  213. return (r2);
  214. if (r1 == ARCHIVE_WARN && r2 == ARCHIVE_WARN)
  215. return (ARCHIVE_WARN);
  216. return (ARCHIVE_OK);
  217. }
  218. /*
  219. * Set the block size. Returns 0 if successful.
  220. */
  221. int
  222. archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
  223. {
  224. struct archive_write *a = (struct archive_write *)_a;
  225. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  226. ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
  227. a->bytes_per_block = bytes_per_block;
  228. return (ARCHIVE_OK);
  229. }
  230. /*
  231. * Get the current block size. -1 if it has never been set.
  232. */
  233. int
  234. archive_write_get_bytes_per_block(struct archive *_a)
  235. {
  236. struct archive_write *a = (struct archive_write *)_a;
  237. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  238. ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
  239. return (a->bytes_per_block);
  240. }
  241. /*
  242. * Set the size for the last block.
  243. * Returns 0 if successful.
  244. */
  245. int
  246. archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
  247. {
  248. struct archive_write *a = (struct archive_write *)_a;
  249. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  250. ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
  251. a->bytes_in_last_block = bytes;
  252. return (ARCHIVE_OK);
  253. }
  254. /*
  255. * Return the value set above. -1 indicates it has not been set.
  256. */
  257. int
  258. archive_write_get_bytes_in_last_block(struct archive *_a)
  259. {
  260. struct archive_write *a = (struct archive_write *)_a;
  261. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  262. ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
  263. return (a->bytes_in_last_block);
  264. }
  265. /*
  266. * dev/ino of a file to be rejected. Used to prevent adding
  267. * an archive to itself recursively.
  268. */
  269. int
  270. archive_write_set_skip_file(struct archive *_a, dev_t d, ino_t i)
  271. {
  272. struct archive_write *a = (struct archive_write *)_a;
  273. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  274. ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
  275. a->skip_file_dev = d;
  276. a->skip_file_ino = i;
  277. return (ARCHIVE_OK);
  278. }
  279. /*
  280. * Open the archive using the current settings.
  281. */
  282. int
  283. archive_write_open(struct archive *_a, void *client_data,
  284. archive_open_callback *opener, archive_write_callback *writer,
  285. archive_close_callback *closer)
  286. {
  287. struct archive_write *a = (struct archive_write *)_a;
  288. int ret;
  289. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  290. ARCHIVE_STATE_NEW, "archive_write_open");
  291. archive_clear_error(&a->archive);
  292. a->archive.state = ARCHIVE_STATE_HEADER;
  293. a->client_data = client_data;
  294. a->client_writer = writer;
  295. a->client_opener = opener;
  296. a->client_closer = closer;
  297. ret = (a->compressor.init)(a);
  298. if (a->format_init && ret == ARCHIVE_OK)
  299. ret = (a->format_init)(a);
  300. return (ret);
  301. }
  302. /*
  303. * Close out the archive.
  304. *
  305. * Be careful: user might just call write_new and then write_finish.
  306. * Don't assume we actually wrote anything or performed any non-trivial
  307. * initialization.
  308. */
  309. static int
  310. _archive_write_close(struct archive *_a)
  311. {
  312. struct archive_write *a = (struct archive_write *)_a;
  313. int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
  314. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  315. ARCHIVE_STATE_ANY, "archive_write_close");
  316. /* Finish the last entry. */
  317. if (a->archive.state & ARCHIVE_STATE_DATA)
  318. r = ((a->format_finish_entry)(a));
  319. /* Finish off the archive. */
  320. if (a->format_finish != NULL) {
  321. r1 = (a->format_finish)(a);
  322. if (r1 < r)
  323. r = r1;
  324. }
  325. /* Release format resources. */
  326. if (a->format_destroy != NULL) {
  327. r1 = (a->format_destroy)(a);
  328. if (r1 < r)
  329. r = r1;
  330. }
  331. /* Finish the compression and close the stream. */
  332. if (a->compressor.finish != NULL) {
  333. r1 = (a->compressor.finish)(a);
  334. if (r1 < r)
  335. r = r1;
  336. }
  337. /* Close out the client stream. */
  338. if (a->client_closer != NULL) {
  339. r1 = (a->client_closer)(&a->archive, a->client_data);
  340. if (r1 < r)
  341. r = r1;
  342. }
  343. a->archive.state = ARCHIVE_STATE_CLOSED;
  344. return (r);
  345. }
  346. /*
  347. * Destroy the archive structure.
  348. */
  349. static int
  350. _archive_write_finish(struct archive *_a)
  351. {
  352. struct archive_write *a = (struct archive_write *)_a;
  353. int r = ARCHIVE_OK;
  354. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  355. ARCHIVE_STATE_ANY, "archive_write_finish");
  356. if (a->archive.state != ARCHIVE_STATE_CLOSED)
  357. r = archive_write_close(&a->archive);
  358. /* Release various dynamic buffers. */
  359. free((void *)(uintptr_t)(const void *)a->nulls);
  360. archive_string_free(&a->archive.error_string);
  361. a->archive.magic = 0;
  362. free(a);
  363. return (r);
  364. }
  365. /*
  366. * Write the appropriate header.
  367. */
  368. static int
  369. _archive_write_header(struct archive *_a, struct archive_entry *entry)
  370. {
  371. struct archive_write *a = (struct archive_write *)_a;
  372. int ret, r2;
  373. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  374. ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
  375. archive_clear_error(&a->archive);
  376. /* In particular, "retry" and "fatal" get returned immediately. */
  377. ret = archive_write_finish_entry(&a->archive);
  378. if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
  379. return (ret);
  380. if (a->skip_file_dev != 0 &&
  381. archive_entry_dev(entry) == a->skip_file_dev &&
  382. a->skip_file_ino != 0 &&
  383. archive_entry_ino(entry) == a->skip_file_ino) {
  384. archive_set_error(&a->archive, 0,
  385. "Can't add archive to itself");
  386. return (ARCHIVE_FAILED);
  387. }
  388. /* Format and write header. */
  389. r2 = ((a->format_write_header)(a, entry));
  390. if (r2 < ret)
  391. ret = r2;
  392. a->archive.state = ARCHIVE_STATE_DATA;
  393. return (ret);
  394. }
  395. static int
  396. _archive_write_finish_entry(struct archive *_a)
  397. {
  398. struct archive_write *a = (struct archive_write *)_a;
  399. int ret = ARCHIVE_OK;
  400. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  401. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  402. "archive_write_finish_entry");
  403. if (a->archive.state & ARCHIVE_STATE_DATA)
  404. ret = (a->format_finish_entry)(a);
  405. a->archive.state = ARCHIVE_STATE_HEADER;
  406. return (ret);
  407. }
  408. /*
  409. * Note that the compressor is responsible for blocking.
  410. */
  411. static ssize_t
  412. _archive_write_data(struct archive *_a, const void *buff, size_t s)
  413. {
  414. struct archive_write *a = (struct archive_write *)_a;
  415. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  416. ARCHIVE_STATE_DATA, "archive_write_data");
  417. archive_clear_error(&a->archive);
  418. return ((a->format_write_data)(a, buff, s));
  419. }
  420. int
  421. archive_write_skip(struct archive *_a, off_t s)
  422. {
  423. struct archive_write *a = (struct archive_write *)_a;
  424. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  425. ARCHIVE_STATE_DATA, "archive_write_skip");
  426. archive_clear_error(&a->archive);
  427. if (a->format_skip_data == NULL) {
  428. archive_set_error(&a->archive, ENOSYS,
  429. "No format skip handler registered");
  430. return (ARCHIVE_FATAL);
  431. }
  432. /* Adjust raw position. */
  433. a->archive.raw_position += s;
  434. /* Adjust uncompressed position. Used by Tarsnap. */
  435. _a->file_position += s;
  436. return ((a->format_skip_data)(a, s));
  437. }