archive_write.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 a 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_LIMITS_H
  38. #include <limits.h>
  39. #endif
  40. #include <stdio.h>
  41. #ifdef HAVE_STDLIB_H
  42. #include <stdlib.h>
  43. #endif
  44. #ifdef HAVE_STRING_H
  45. #include <string.h>
  46. #endif
  47. #include <time.h>
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #include "archive.h"
  52. #include "archive_entry.h"
  53. #include "archive_private.h"
  54. #include "archive_write_private.h"
  55. static struct archive_vtable *archive_write_vtable(void);
  56. static int _archive_write_close(struct archive *);
  57. static int _archive_write_finish(struct archive *);
  58. static int _archive_write_header(struct archive *, struct archive_entry *);
  59. static int _archive_write_finish_entry(struct archive *);
  60. static ssize_t _archive_write_data(struct archive *, const void *, size_t);
  61. static struct archive_vtable *
  62. archive_write_vtable(void)
  63. {
  64. static struct archive_vtable av;
  65. static int inited = 0;
  66. if (!inited) {
  67. av.archive_close = _archive_write_close;
  68. av.archive_finish = _archive_write_finish;
  69. av.archive_write_header = _archive_write_header;
  70. av.archive_write_finish_entry = _archive_write_finish_entry;
  71. av.archive_write_data = _archive_write_data;
  72. }
  73. return (&av);
  74. }
  75. /*
  76. * Allocate, initialize and return an archive object.
  77. */
  78. struct archive *
  79. archive_write_new(void)
  80. {
  81. struct archive_write *a;
  82. unsigned char *nulls;
  83. a = (struct archive_write *)malloc(sizeof(*a));
  84. if (a == NULL)
  85. return (NULL);
  86. memset(a, 0, sizeof(*a));
  87. a->archive.magic = ARCHIVE_WRITE_MAGIC;
  88. a->archive.state = ARCHIVE_STATE_NEW;
  89. a->archive.vtable = archive_write_vtable();
  90. /*
  91. * The value 10240 here matches the traditional tar default,
  92. * but is otherwise arbitrary.
  93. * TODO: Set the default block size from the format selected.
  94. */
  95. a->bytes_per_block = 10240;
  96. a->bytes_in_last_block = -1; /* Default */
  97. /* Initialize a block of nulls for padding purposes. */
  98. a->null_length = 1024;
  99. nulls = (unsigned char *)malloc(a->null_length);
  100. if (nulls == NULL) {
  101. free(a);
  102. return (NULL);
  103. }
  104. memset(nulls, 0, a->null_length);
  105. a->nulls = nulls;
  106. /*
  107. * Set default compression, but don't set a default format.
  108. * Were we to set a default format here, we would force every
  109. * client to link in support for that format, even if they didn't
  110. * ever use it.
  111. */
  112. archive_write_set_compression_none(&a->archive);
  113. return (&a->archive);
  114. }
  115. /*
  116. * Set write options for the format. Returns 0 if successful.
  117. */
  118. int
  119. archive_write_set_format_options(struct archive *_a, const char *s)
  120. {
  121. struct archive_write *a = (struct archive_write *)_a;
  122. char key[64], val[64];
  123. int len, r, ret = ARCHIVE_OK;
  124. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  125. ARCHIVE_STATE_NEW, "archive_write_set_format_options");
  126. archive_clear_error(&a->archive);
  127. if (s == NULL || *s == '\0')
  128. return (ARCHIVE_OK);
  129. if (a->format_options == NULL)
  130. /* This format does not support option. */
  131. return (ARCHIVE_OK);
  132. while ((len = __archive_parse_options(s, a->format_name,
  133. sizeof(key), key, sizeof(val), val)) > 0) {
  134. if (val[0] == '\0')
  135. r = a->format_options(a, key, NULL);
  136. else
  137. r = a->format_options(a, key, val);
  138. if (r == ARCHIVE_FATAL)
  139. return (r);
  140. if (r < ARCHIVE_OK) { /* This key was not handled. */
  141. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  142. "Unsupported option ``%s''", key);
  143. ret = ARCHIVE_WARN;
  144. }
  145. s += len;
  146. }
  147. if (len < 0) {
  148. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  149. "Malformed options string.");
  150. return (ARCHIVE_WARN);
  151. }
  152. return (ret);
  153. }
  154. /*
  155. * Set write options for the compressor. Returns 0 if successful.
  156. */
  157. int
  158. archive_write_set_compressor_options(struct archive *_a, const char *s)
  159. {
  160. struct archive_write *a = (struct archive_write *)_a;
  161. char key[64], val[64];
  162. int len, r;
  163. int ret = ARCHIVE_OK;
  164. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  165. ARCHIVE_STATE_NEW, "archive_write_set_compressor_options");
  166. archive_clear_error(&a->archive);
  167. if (s == NULL || *s == '\0')
  168. return (ARCHIVE_OK);
  169. if (a->compressor.options == NULL) {
  170. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  171. "Unsupported option ``%s''", s);
  172. /* This compressor does not support option. */
  173. return (ARCHIVE_WARN);
  174. }
  175. while ((len = __archive_parse_options(s, a->archive.compression_name,
  176. sizeof(key), key, sizeof(val), val)) > 0) {
  177. if (val[0] == '\0')
  178. r = a->compressor.options(a, key, NULL);
  179. else
  180. r = a->compressor.options(a, key, val);
  181. if (r == ARCHIVE_FATAL)
  182. return (r);
  183. if (r < ARCHIVE_OK) {
  184. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  185. "Unsupported option ``%s''", key);
  186. ret = ARCHIVE_WARN;
  187. }
  188. s += len;
  189. }
  190. if (len < 0) {
  191. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  192. "Illegal format options.");
  193. return (ARCHIVE_WARN);
  194. }
  195. return (ret);
  196. }
  197. /*
  198. * Set write options for the format and the compressor. Returns 0 if successful.
  199. */
  200. int
  201. archive_write_set_options(struct archive *_a, const char *s)
  202. {
  203. int r1, r2;
  204. r1 = archive_write_set_format_options(_a, s);
  205. if (r1 < ARCHIVE_WARN)
  206. return (r1);
  207. r2 = archive_write_set_compressor_options(_a, s);
  208. if (r2 < ARCHIVE_WARN)
  209. return (r2);
  210. if (r1 == ARCHIVE_WARN && r2 == ARCHIVE_WARN)
  211. return (ARCHIVE_WARN);
  212. return (ARCHIVE_OK);
  213. }
  214. /*
  215. * Set the block size. Returns 0 if successful.
  216. */
  217. int
  218. archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
  219. {
  220. struct archive_write *a = (struct archive_write *)_a;
  221. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  222. ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
  223. a->bytes_per_block = bytes_per_block;
  224. return (ARCHIVE_OK);
  225. }
  226. /*
  227. * Get the current block size. -1 if it has never been set.
  228. */
  229. int
  230. archive_write_get_bytes_per_block(struct archive *_a)
  231. {
  232. struct archive_write *a = (struct archive_write *)_a;
  233. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  234. ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
  235. return (a->bytes_per_block);
  236. }
  237. /*
  238. * Set the size for the last block.
  239. * Returns 0 if successful.
  240. */
  241. int
  242. archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
  243. {
  244. struct archive_write *a = (struct archive_write *)_a;
  245. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  246. ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
  247. a->bytes_in_last_block = bytes;
  248. return (ARCHIVE_OK);
  249. }
  250. /*
  251. * Return the value set above. -1 indicates it has not been set.
  252. */
  253. int
  254. archive_write_get_bytes_in_last_block(struct archive *_a)
  255. {
  256. struct archive_write *a = (struct archive_write *)_a;
  257. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  258. ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
  259. return (a->bytes_in_last_block);
  260. }
  261. /*
  262. * dev/ino of a file to be rejected. Used to prevent adding
  263. * an archive to itself recursively.
  264. */
  265. int
  266. archive_write_set_skip_file(struct archive *_a, dev_t d, ino_t i)
  267. {
  268. struct archive_write *a = (struct archive_write *)_a;
  269. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  270. ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
  271. a->skip_file_dev = d;
  272. a->skip_file_ino = i;
  273. return (ARCHIVE_OK);
  274. }
  275. /*
  276. * Open the archive using the current settings.
  277. */
  278. int
  279. archive_write_open(struct archive *_a, void *client_data,
  280. archive_open_callback *opener, archive_write_callback *writer,
  281. archive_close_callback *closer)
  282. {
  283. struct archive_write *a = (struct archive_write *)_a;
  284. int ret;
  285. ret = ARCHIVE_OK;
  286. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  287. ARCHIVE_STATE_NEW, "archive_write_open");
  288. archive_clear_error(&a->archive);
  289. a->archive.state = ARCHIVE_STATE_HEADER;
  290. a->client_data = client_data;
  291. a->client_writer = writer;
  292. a->client_opener = opener;
  293. a->client_closer = closer;
  294. ret = (a->compressor.init)(a);
  295. if (a->format_init && ret == ARCHIVE_OK)
  296. ret = (a->format_init)(a);
  297. return (ret);
  298. }
  299. /*
  300. * Close out the archive.
  301. *
  302. * Be careful: user might just call write_new and then write_finish.
  303. * Don't assume we actually wrote anything or performed any non-trivial
  304. * initialization.
  305. */
  306. static int
  307. _archive_write_close(struct archive *_a)
  308. {
  309. struct archive_write *a = (struct archive_write *)_a;
  310. int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
  311. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  312. ARCHIVE_STATE_ANY, "archive_write_close");
  313. /* Finish the last entry. */
  314. if (a->archive.state & ARCHIVE_STATE_DATA)
  315. r = ((a->format_finish_entry)(a));
  316. /* Finish off the archive. */
  317. if (a->format_finish != NULL) {
  318. r1 = (a->format_finish)(a);
  319. if (r1 < r)
  320. r = r1;
  321. }
  322. /* Release format resources. */
  323. if (a->format_destroy != NULL) {
  324. r1 = (a->format_destroy)(a);
  325. if (r1 < r)
  326. r = r1;
  327. }
  328. /* Finish the compression and close the stream. */
  329. if (a->compressor.finish != NULL) {
  330. r1 = (a->compressor.finish)(a);
  331. if (r1 < r)
  332. r = r1;
  333. }
  334. /* Close out the client stream. */
  335. if (a->client_closer != NULL) {
  336. r1 = (a->client_closer)(&a->archive, a->client_data);
  337. if (r1 < r)
  338. r = r1;
  339. }
  340. a->archive.state = ARCHIVE_STATE_CLOSED;
  341. return (r);
  342. }
  343. /*
  344. * Destroy the archive structure.
  345. */
  346. static int
  347. _archive_write_finish(struct archive *_a)
  348. {
  349. struct archive_write *a = (struct archive_write *)_a;
  350. int r = ARCHIVE_OK;
  351. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  352. ARCHIVE_STATE_ANY, "archive_write_finish");
  353. if (a->archive.state != ARCHIVE_STATE_CLOSED)
  354. r = archive_write_close(&a->archive);
  355. /* Release various dynamic buffers. */
  356. free((void *)(uintptr_t)(const void *)a->nulls);
  357. archive_string_free(&a->archive.error_string);
  358. a->archive.magic = 0;
  359. free(a);
  360. return (r);
  361. }
  362. /*
  363. * Write the appropriate header.
  364. */
  365. static int
  366. _archive_write_header(struct archive *_a, struct archive_entry *entry)
  367. {
  368. struct archive_write *a = (struct archive_write *)_a;
  369. int ret, r2;
  370. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  371. ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
  372. archive_clear_error(&a->archive);
  373. /* In particular, "retry" and "fatal" get returned immediately. */
  374. ret = archive_write_finish_entry(&a->archive);
  375. if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
  376. return (ret);
  377. if (a->skip_file_dev != 0 &&
  378. archive_entry_dev(entry) == a->skip_file_dev &&
  379. a->skip_file_ino != 0 &&
  380. archive_entry_ino64(entry) == a->skip_file_ino) {
  381. archive_set_error(&a->archive, 0,
  382. "Can't add archive to itself");
  383. return (ARCHIVE_FAILED);
  384. }
  385. /* Format and write header. */
  386. r2 = ((a->format_write_header)(a, entry));
  387. if (r2 < ret)
  388. ret = r2;
  389. a->archive.state = ARCHIVE_STATE_DATA;
  390. return (ret);
  391. }
  392. static int
  393. _archive_write_finish_entry(struct archive *_a)
  394. {
  395. struct archive_write *a = (struct archive_write *)_a;
  396. int ret = ARCHIVE_OK;
  397. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  398. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  399. "archive_write_finish_entry");
  400. if (a->archive.state & ARCHIVE_STATE_DATA)
  401. ret = (a->format_finish_entry)(a);
  402. a->archive.state = ARCHIVE_STATE_HEADER;
  403. return (ret);
  404. }
  405. /*
  406. * Note that the compressor is responsible for blocking.
  407. */
  408. static ssize_t
  409. _archive_write_data(struct archive *_a, const void *buff, size_t s)
  410. {
  411. struct archive_write *a = (struct archive_write *)_a;
  412. __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  413. ARCHIVE_STATE_DATA, "archive_write_data");
  414. archive_clear_error(&a->archive);
  415. return ((a->format_write_data)(a, buff, s));
  416. }