archive_write.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*-
  2. * Copyright (c) 2003-2010 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. /*
  27. * This file contains the "essential" portions of the write API, that
  28. * is, stuff that will essentially always be used by any client that
  29. * actually needs to write an archive. Optional pieces have been, as
  30. * far as possible, separated out into separate files to reduce
  31. * needlessly bloating statically-linked clients.
  32. */
  33. #ifdef HAVE_SYS_WAIT_H
  34. #include <sys/wait.h>
  35. #endif
  36. #ifdef HAVE_ERRNO_H
  37. #include <errno.h>
  38. #endif
  39. #ifdef HAVE_LIMITS_H
  40. #include <limits.h>
  41. #endif
  42. #include <stdio.h>
  43. #ifdef HAVE_STDLIB_H
  44. #include <stdlib.h>
  45. #endif
  46. #ifdef HAVE_STRING_H
  47. #include <string.h>
  48. #endif
  49. #include <time.h>
  50. #ifdef HAVE_UNISTD_H
  51. #include <unistd.h>
  52. #endif
  53. #include "archive.h"
  54. #include "archive_entry.h"
  55. #include "archive_private.h"
  56. #include "archive_write_private.h"
  57. static int _archive_filter_code(struct archive *, int);
  58. static const char *_archive_filter_name(struct archive *, int);
  59. static int64_t _archive_filter_bytes(struct archive *, int);
  60. static int _archive_write_filter_count(struct archive *);
  61. static int _archive_write_close(struct archive *);
  62. static int _archive_write_free(struct archive *);
  63. static int _archive_write_header(struct archive *, struct archive_entry *);
  64. static int _archive_write_finish_entry(struct archive *);
  65. static ssize_t _archive_write_data(struct archive *, const void *, size_t);
  66. struct archive_none {
  67. size_t buffer_size;
  68. size_t avail;
  69. char *buffer;
  70. char *next;
  71. };
  72. static const struct archive_vtable
  73. archive_write_vtable = {
  74. .archive_close = _archive_write_close,
  75. .archive_filter_bytes = _archive_filter_bytes,
  76. .archive_filter_code = _archive_filter_code,
  77. .archive_filter_name = _archive_filter_name,
  78. .archive_filter_count = _archive_write_filter_count,
  79. .archive_free = _archive_write_free,
  80. .archive_write_header = _archive_write_header,
  81. .archive_write_finish_entry = _archive_write_finish_entry,
  82. .archive_write_data = _archive_write_data,
  83. };
  84. /*
  85. * Allocate, initialize and return an archive object.
  86. */
  87. struct archive *
  88. archive_write_new(void)
  89. {
  90. struct archive_write *a;
  91. unsigned char *nulls;
  92. a = calloc(1, sizeof(*a));
  93. if (a == NULL)
  94. return (NULL);
  95. a->archive.magic = ARCHIVE_WRITE_MAGIC;
  96. a->archive.state = ARCHIVE_STATE_NEW;
  97. a->archive.vtable = &archive_write_vtable;
  98. /*
  99. * The value 10240 here matches the traditional tar default,
  100. * but is otherwise arbitrary.
  101. * TODO: Set the default block size from the format selected.
  102. */
  103. a->bytes_per_block = 10240;
  104. a->bytes_in_last_block = -1; /* Default */
  105. /* Initialize a block of nulls for padding purposes. */
  106. a->null_length = 1024;
  107. nulls = calloc(a->null_length, sizeof(unsigned char));
  108. if (nulls == NULL) {
  109. free(a);
  110. return (NULL);
  111. }
  112. a->nulls = nulls;
  113. return (&a->archive);
  114. }
  115. /*
  116. * Set the block size. Returns 0 if successful.
  117. */
  118. int
  119. archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
  120. {
  121. struct archive_write *a = (struct archive_write *)_a;
  122. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  123. ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
  124. if (bytes_per_block < 0) {
  125. // Do nothing if the bytes_per_block is negative
  126. return 0;
  127. }
  128. a->bytes_per_block = bytes_per_block;
  129. return (ARCHIVE_OK);
  130. }
  131. /*
  132. * Get the current block size.
  133. */
  134. int
  135. archive_write_get_bytes_per_block(struct archive *_a)
  136. {
  137. struct archive_write *a = (struct archive_write *)_a;
  138. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  139. ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
  140. if (a->bytes_per_block < 0) {
  141. // Don't return a negative value
  142. return 1;
  143. }
  144. return (a->bytes_per_block);
  145. }
  146. /*
  147. * Set the size for the last block.
  148. * Returns 0 if successful.
  149. */
  150. int
  151. archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
  152. {
  153. struct archive_write *a = (struct archive_write *)_a;
  154. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  155. ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
  156. a->bytes_in_last_block = bytes;
  157. return (ARCHIVE_OK);
  158. }
  159. /*
  160. * Return the value set above. -1 indicates it has not been set.
  161. */
  162. int
  163. archive_write_get_bytes_in_last_block(struct archive *_a)
  164. {
  165. struct archive_write *a = (struct archive_write *)_a;
  166. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  167. ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
  168. return (a->bytes_in_last_block);
  169. }
  170. /*
  171. * dev/ino of a file to be rejected. Used to prevent adding
  172. * an archive to itself recursively.
  173. */
  174. int
  175. archive_write_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
  176. {
  177. struct archive_write *a = (struct archive_write *)_a;
  178. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  179. ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
  180. a->skip_file_set = 1;
  181. a->skip_file_dev = d;
  182. a->skip_file_ino = i;
  183. return (ARCHIVE_OK);
  184. }
  185. /*
  186. * Allocate and return the next filter structure.
  187. */
  188. struct archive_write_filter *
  189. __archive_write_allocate_filter(struct archive *_a)
  190. {
  191. struct archive_write *a = (struct archive_write *)_a;
  192. struct archive_write_filter *f;
  193. f = calloc(1, sizeof(*f));
  194. if (f == NULL)
  195. return (NULL);
  196. f->archive = _a;
  197. f->state = ARCHIVE_WRITE_FILTER_STATE_NEW;
  198. if (a->filter_first == NULL)
  199. a->filter_first = f;
  200. else
  201. a->filter_last->next_filter = f;
  202. a->filter_last = f;
  203. return f;
  204. }
  205. /*
  206. * Write data to a particular filter.
  207. */
  208. int
  209. __archive_write_filter(struct archive_write_filter *f,
  210. const void *buff, size_t length)
  211. {
  212. int r;
  213. /* Never write to non-open filters */
  214. if (f->state != ARCHIVE_WRITE_FILTER_STATE_OPEN)
  215. return(ARCHIVE_FATAL);
  216. if (length == 0)
  217. return(ARCHIVE_OK);
  218. if (f->write == NULL)
  219. /* If unset, a fatal error has already occurred, so this filter
  220. * didn't open. We cannot write anything. */
  221. return(ARCHIVE_FATAL);
  222. r = (f->write)(f, buff, length);
  223. f->bytes_written += length;
  224. return (r);
  225. }
  226. /*
  227. * Recursive function for opening the filter chain
  228. * Last filter is opened first
  229. */
  230. static int
  231. __archive_write_open_filter(struct archive_write_filter *f)
  232. {
  233. int ret;
  234. ret = ARCHIVE_OK;
  235. if (f->next_filter != NULL)
  236. ret = __archive_write_open_filter(f->next_filter);
  237. if (ret != ARCHIVE_OK)
  238. return (ret);
  239. if (f->state != ARCHIVE_WRITE_FILTER_STATE_NEW)
  240. return (ARCHIVE_FATAL);
  241. if (f->open == NULL) {
  242. f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
  243. return (ARCHIVE_OK);
  244. }
  245. ret = (f->open)(f);
  246. if (ret == ARCHIVE_OK)
  247. f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
  248. else
  249. f->state = ARCHIVE_WRITE_FILTER_STATE_FATAL;
  250. return (ret);
  251. }
  252. /*
  253. * Open all filters
  254. */
  255. static int
  256. __archive_write_filters_open(struct archive_write *a)
  257. {
  258. return (__archive_write_open_filter(a->filter_first));
  259. }
  260. /*
  261. * Close all filters
  262. */
  263. static int
  264. __archive_write_filters_close(struct archive_write *a)
  265. {
  266. struct archive_write_filter *f;
  267. int ret, ret1;
  268. ret = ARCHIVE_OK;
  269. for (f = a->filter_first; f != NULL; f = f->next_filter) {
  270. /* Do not close filters that are not open */
  271. if (f->state == ARCHIVE_WRITE_FILTER_STATE_OPEN) {
  272. if (f->close != NULL) {
  273. ret1 = (f->close)(f);
  274. if (ret1 < ret)
  275. ret = ret1;
  276. if (ret1 == ARCHIVE_OK) {
  277. f->state =
  278. ARCHIVE_WRITE_FILTER_STATE_CLOSED;
  279. } else {
  280. f->state =
  281. ARCHIVE_WRITE_FILTER_STATE_FATAL;
  282. }
  283. } else
  284. f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
  285. }
  286. }
  287. return (ret);
  288. }
  289. int
  290. __archive_write_output(struct archive_write *a, const void *buff, size_t length)
  291. {
  292. return (__archive_write_filter(a->filter_first, buff, length));
  293. }
  294. static int
  295. __archive_write_filters_flush(struct archive_write *a)
  296. {
  297. struct archive_write_filter *f;
  298. int ret, ret1;
  299. ret = ARCHIVE_OK;
  300. for (f = a->filter_first; f != NULL; f = f->next_filter) {
  301. if (f->flush != NULL && f->bytes_written > 0) {
  302. ret1 = (f->flush)(f);
  303. if (ret1 < ret)
  304. ret = ret1;
  305. if (ret1 < ARCHIVE_WARN)
  306. f->state = ARCHIVE_WRITE_FILTER_STATE_FATAL;
  307. }
  308. }
  309. return (ret);
  310. }
  311. int
  312. __archive_write_nulls(struct archive_write *a, size_t length)
  313. {
  314. if (length == 0)
  315. return (ARCHIVE_OK);
  316. while (length > 0) {
  317. size_t to_write = length < a->null_length ? length : a->null_length;
  318. int r = __archive_write_output(a, a->nulls, to_write);
  319. if (r < ARCHIVE_OK)
  320. return (r);
  321. length -= to_write;
  322. }
  323. return (ARCHIVE_OK);
  324. }
  325. static int
  326. archive_write_client_open(struct archive_write_filter *f)
  327. {
  328. struct archive_write *a = (struct archive_write *)f->archive;
  329. struct archive_none *state;
  330. void *buffer;
  331. size_t buffer_size;
  332. int ret;
  333. f->bytes_per_block = archive_write_get_bytes_per_block(f->archive);
  334. f->bytes_in_last_block =
  335. archive_write_get_bytes_in_last_block(f->archive);
  336. buffer_size = f->bytes_per_block;
  337. state = calloc(1, sizeof(*state));
  338. buffer = malloc(buffer_size);
  339. if (state == NULL || buffer == NULL) {
  340. free(state);
  341. free(buffer);
  342. archive_set_error(f->archive, ENOMEM,
  343. "Can't allocate data for output buffering");
  344. return (ARCHIVE_FATAL);
  345. }
  346. state->buffer_size = buffer_size;
  347. state->buffer = buffer;
  348. state->next = state->buffer;
  349. state->avail = state->buffer_size;
  350. f->data = state;
  351. if (a->client_opener == NULL)
  352. return (ARCHIVE_OK);
  353. ret = a->client_opener(f->archive, a->client_data);
  354. if (ret != ARCHIVE_OK) {
  355. free(state->buffer);
  356. free(state);
  357. f->data = NULL;
  358. }
  359. return (ret);
  360. }
  361. static int
  362. archive_write_client_write(struct archive_write_filter *f,
  363. const void *_buff, size_t length)
  364. {
  365. struct archive_write *a = (struct archive_write *)f->archive;
  366. struct archive_none *state = (struct archive_none *)f->data;
  367. const char *buff = (const char *)_buff;
  368. ssize_t remaining, to_copy;
  369. ssize_t bytes_written;
  370. remaining = length;
  371. /*
  372. * If there is no buffer for blocking, just pass the data
  373. * straight through to the client write callback. In
  374. * particular, this supports "no write delay" operation for
  375. * special applications. Just set the block size to zero.
  376. */
  377. if (state->buffer_size == 0) {
  378. while (remaining > 0) {
  379. bytes_written = (a->client_writer)(&a->archive,
  380. a->client_data, buff, remaining);
  381. if (bytes_written <= 0)
  382. return (ARCHIVE_FATAL);
  383. remaining -= bytes_written;
  384. buff += bytes_written;
  385. }
  386. return (ARCHIVE_OK);
  387. }
  388. /* If the copy buffer isn't empty, try to fill it. */
  389. if (state->avail < state->buffer_size) {
  390. /* If buffer is not empty... */
  391. /* ... copy data into buffer ... */
  392. to_copy = ((size_t)remaining > state->avail) ?
  393. state->avail : (size_t)remaining;
  394. memcpy(state->next, buff, to_copy);
  395. state->next += to_copy;
  396. state->avail -= to_copy;
  397. buff += to_copy;
  398. remaining -= to_copy;
  399. /* ... if it's full, write it out. */
  400. if (state->avail == 0) {
  401. char *p = state->buffer;
  402. size_t to_write = state->buffer_size;
  403. while (to_write > 0) {
  404. bytes_written = (a->client_writer)(&a->archive,
  405. a->client_data, p, to_write);
  406. if (bytes_written <= 0)
  407. return (ARCHIVE_FATAL);
  408. if ((size_t)bytes_written > to_write) {
  409. archive_set_error(&(a->archive),
  410. -1, "write overrun");
  411. return (ARCHIVE_FATAL);
  412. }
  413. p += bytes_written;
  414. to_write -= bytes_written;
  415. }
  416. state->next = state->buffer;
  417. state->avail = state->buffer_size;
  418. }
  419. }
  420. while ((size_t)remaining >= state->buffer_size) {
  421. /* Write out full blocks directly to client. */
  422. bytes_written = (a->client_writer)(&a->archive,
  423. a->client_data, buff, state->buffer_size);
  424. if (bytes_written <= 0)
  425. return (ARCHIVE_FATAL);
  426. buff += bytes_written;
  427. remaining -= bytes_written;
  428. }
  429. if (remaining > 0) {
  430. /* Copy last bit into copy buffer. */
  431. memcpy(state->next, buff, remaining);
  432. state->next += remaining;
  433. state->avail -= remaining;
  434. }
  435. return (ARCHIVE_OK);
  436. }
  437. static int
  438. archive_write_client_free(struct archive_write_filter *f)
  439. {
  440. struct archive_write *a = (struct archive_write *)f->archive;
  441. if (a->client_freer)
  442. (*a->client_freer)(&a->archive, a->client_data);
  443. a->client_data = NULL;
  444. /* Clear passphrase. */
  445. if (a->passphrase != NULL) {
  446. memset(a->passphrase, 0, strlen(a->passphrase));
  447. free(a->passphrase);
  448. a->passphrase = NULL;
  449. }
  450. return (ARCHIVE_OK);
  451. }
  452. static int
  453. archive_write_client_close(struct archive_write_filter *f)
  454. {
  455. struct archive_write *a = (struct archive_write *)f->archive;
  456. struct archive_none *state = (struct archive_none *)f->data;
  457. ssize_t block_length;
  458. ssize_t target_block_length;
  459. ssize_t bytes_written;
  460. size_t to_write;
  461. char *p;
  462. int ret = ARCHIVE_OK;
  463. /* If there's pending data, pad and write the last block */
  464. if (state->next != state->buffer) {
  465. block_length = state->buffer_size - state->avail;
  466. /* Tricky calculation to determine size of last block */
  467. if (a->bytes_in_last_block <= 0)
  468. /* Default or Zero: pad to full block */
  469. target_block_length = a->bytes_per_block;
  470. else
  471. /* Round to next multiple of bytes_in_last_block. */
  472. target_block_length = a->bytes_in_last_block *
  473. ( (block_length + a->bytes_in_last_block - 1) /
  474. a->bytes_in_last_block);
  475. if (target_block_length > a->bytes_per_block)
  476. target_block_length = a->bytes_per_block;
  477. if (block_length < target_block_length) {
  478. memset(state->next, 0,
  479. target_block_length - block_length);
  480. block_length = target_block_length;
  481. }
  482. p = state->buffer;
  483. to_write = block_length;
  484. while (to_write > 0) {
  485. bytes_written = (a->client_writer)(&a->archive,
  486. a->client_data, p, to_write);
  487. if (bytes_written <= 0) {
  488. ret = ARCHIVE_FATAL;
  489. break;
  490. }
  491. if ((size_t)bytes_written > to_write) {
  492. archive_set_error(&(a->archive),
  493. -1, "write overrun");
  494. ret = ARCHIVE_FATAL;
  495. break;
  496. }
  497. p += bytes_written;
  498. to_write -= bytes_written;
  499. }
  500. }
  501. if (a->client_closer)
  502. (*a->client_closer)(&a->archive, a->client_data);
  503. free(state->buffer);
  504. free(state);
  505. /* Clear the close handler myself not to be called again. */
  506. f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
  507. return (ret);
  508. }
  509. /*
  510. * Open the archive using the current settings.
  511. */
  512. int
  513. archive_write_open2(struct archive *_a, void *client_data,
  514. archive_open_callback *opener, archive_write_callback *writer,
  515. archive_close_callback *closer, archive_free_callback *freer)
  516. {
  517. struct archive_write *a = (struct archive_write *)_a;
  518. struct archive_write_filter *client_filter;
  519. int ret, r1;
  520. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  521. ARCHIVE_STATE_NEW, "archive_write_open");
  522. archive_clear_error(&a->archive);
  523. a->client_writer = writer;
  524. a->client_opener = opener;
  525. a->client_closer = closer;
  526. a->client_freer = freer;
  527. a->client_data = client_data;
  528. client_filter = __archive_write_allocate_filter(_a);
  529. if (client_filter == NULL)
  530. return (ARCHIVE_FATAL);
  531. client_filter->open = archive_write_client_open;
  532. client_filter->write = archive_write_client_write;
  533. client_filter->close = archive_write_client_close;
  534. client_filter->free = archive_write_client_free;
  535. ret = __archive_write_filters_open(a);
  536. if (ret < ARCHIVE_WARN) {
  537. r1 = __archive_write_filters_close(a);
  538. __archive_write_filters_free(_a);
  539. return (r1 < ret ? r1 : ret);
  540. }
  541. a->archive.state = ARCHIVE_STATE_HEADER;
  542. if (a->format_init)
  543. ret = (a->format_init)(a);
  544. return (ret);
  545. }
  546. int
  547. archive_write_open(struct archive *_a, void *client_data,
  548. archive_open_callback *opener, archive_write_callback *writer,
  549. archive_close_callback *closer)
  550. {
  551. return archive_write_open2(_a, client_data, opener, writer,
  552. closer, NULL);
  553. }
  554. /*
  555. * Close out the archive.
  556. */
  557. static int
  558. _archive_write_close(struct archive *_a)
  559. {
  560. struct archive_write *a = (struct archive_write *)_a;
  561. int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
  562. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  563. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL,
  564. "archive_write_close");
  565. if (a->archive.state == ARCHIVE_STATE_NEW
  566. || a->archive.state == ARCHIVE_STATE_CLOSED)
  567. return (ARCHIVE_OK); /* Okay to close() when not open. */
  568. archive_clear_error(&a->archive);
  569. /* Finish the last entry if a finish callback is specified */
  570. if (a->archive.state == ARCHIVE_STATE_DATA
  571. && a->format_finish_entry != NULL)
  572. r = ((a->format_finish_entry)(a));
  573. /* Finish off the archive. */
  574. /* TODO: have format closers invoke compression close. */
  575. if (a->format_close != NULL) {
  576. r1 = (a->format_close)(a);
  577. if (r1 < r)
  578. r = r1;
  579. }
  580. /* Finish the compression and close the stream. */
  581. r1 = __archive_write_filters_close(a);
  582. if (r1 < r)
  583. r = r1;
  584. if (a->archive.state != ARCHIVE_STATE_FATAL)
  585. a->archive.state = ARCHIVE_STATE_CLOSED;
  586. return (r);
  587. }
  588. static int
  589. _archive_write_filter_count(struct archive *_a)
  590. {
  591. struct archive_write *a = (struct archive_write *)_a;
  592. struct archive_write_filter *p = a->filter_first;
  593. int count = 0;
  594. while(p) {
  595. count++;
  596. p = p->next_filter;
  597. }
  598. return count;
  599. }
  600. void
  601. __archive_write_filters_free(struct archive *_a)
  602. {
  603. struct archive_write *a = (struct archive_write *)_a;
  604. int r = ARCHIVE_OK, r1;
  605. while (a->filter_first != NULL) {
  606. struct archive_write_filter *next
  607. = a->filter_first->next_filter;
  608. if (a->filter_first->free != NULL) {
  609. r1 = (*a->filter_first->free)(a->filter_first);
  610. if (r > r1)
  611. r = r1;
  612. }
  613. free(a->filter_first);
  614. a->filter_first = next;
  615. }
  616. a->filter_last = NULL;
  617. }
  618. /*
  619. * Destroy the archive structure.
  620. *
  621. * Be careful: user might just call write_new and then write_free.
  622. * Don't assume we actually wrote anything or performed any non-trivial
  623. * initialization.
  624. */
  625. static int
  626. _archive_write_free(struct archive *_a)
  627. {
  628. struct archive_write *a = (struct archive_write *)_a;
  629. int r = ARCHIVE_OK, r1;
  630. if (_a == NULL)
  631. return (ARCHIVE_OK);
  632. /* It is okay to call free() in state FATAL. */
  633. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  634. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free");
  635. if (a->archive.state != ARCHIVE_STATE_FATAL)
  636. r = archive_write_close(&a->archive);
  637. /* Release format resources. */
  638. if (a->format_free != NULL) {
  639. r1 = (a->format_free)(a);
  640. if (r1 < r)
  641. r = r1;
  642. }
  643. __archive_write_filters_free(_a);
  644. /* Release various dynamic buffers. */
  645. free((void *)(uintptr_t)(const void *)a->nulls);
  646. archive_string_free(&a->archive.error_string);
  647. if (a->passphrase != NULL) {
  648. /* A passphrase should be cleaned. */
  649. memset(a->passphrase, 0, strlen(a->passphrase));
  650. free(a->passphrase);
  651. }
  652. a->archive.magic = 0;
  653. __archive_clean(&a->archive);
  654. free(a);
  655. return (r);
  656. }
  657. /*
  658. * Write the appropriate header.
  659. */
  660. static int
  661. _archive_write_header(struct archive *_a, struct archive_entry *entry)
  662. {
  663. struct archive_write *a = (struct archive_write *)_a;
  664. int ret, r2;
  665. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  666. ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
  667. archive_clear_error(&a->archive);
  668. if (a->format_write_header == NULL) {
  669. archive_set_error(&(a->archive), -1,
  670. "Format must be set before you can write to an archive.");
  671. a->archive.state = ARCHIVE_STATE_FATAL;
  672. return (ARCHIVE_FATAL);
  673. }
  674. /* In particular, "retry" and "fatal" get returned immediately. */
  675. ret = archive_write_finish_entry(&a->archive);
  676. if (ret == ARCHIVE_FATAL) {
  677. a->archive.state = ARCHIVE_STATE_FATAL;
  678. return (ARCHIVE_FATAL);
  679. }
  680. if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
  681. return (ret);
  682. if (a->skip_file_set &&
  683. archive_entry_dev_is_set(entry) &&
  684. archive_entry_ino_is_set(entry) &&
  685. archive_entry_dev(entry) == (dev_t)a->skip_file_dev &&
  686. archive_entry_ino64(entry) == a->skip_file_ino) {
  687. archive_set_error(&a->archive, 0,
  688. "Can't add archive to itself");
  689. return (ARCHIVE_FAILED);
  690. }
  691. /* Flush filters at boundary. */
  692. r2 = __archive_write_filters_flush(a);
  693. if (r2 == ARCHIVE_FAILED) {
  694. return (ARCHIVE_FAILED);
  695. }
  696. if (r2 == ARCHIVE_FATAL) {
  697. a->archive.state = ARCHIVE_STATE_FATAL;
  698. return (ARCHIVE_FATAL);
  699. }
  700. if (r2 < ret)
  701. ret = r2;
  702. /* Format and write header. */
  703. r2 = ((a->format_write_header)(a, entry));
  704. if (r2 == ARCHIVE_FAILED) {
  705. return (ARCHIVE_FAILED);
  706. }
  707. if (r2 == ARCHIVE_FATAL) {
  708. a->archive.state = ARCHIVE_STATE_FATAL;
  709. return (ARCHIVE_FATAL);
  710. }
  711. if (r2 < ret)
  712. ret = r2;
  713. a->archive.state = ARCHIVE_STATE_DATA;
  714. return (ret);
  715. }
  716. static int
  717. _archive_write_finish_entry(struct archive *_a)
  718. {
  719. struct archive_write *a = (struct archive_write *)_a;
  720. int ret = ARCHIVE_OK;
  721. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  722. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  723. "archive_write_finish_entry");
  724. if (a->archive.state & ARCHIVE_STATE_DATA
  725. && a->format_finish_entry != NULL)
  726. ret = (a->format_finish_entry)(a);
  727. a->archive.state = ARCHIVE_STATE_HEADER;
  728. return (ret);
  729. }
  730. /*
  731. * Note that the compressor is responsible for blocking.
  732. */
  733. static ssize_t
  734. _archive_write_data(struct archive *_a, const void *buff, size_t s)
  735. {
  736. struct archive_write *a = (struct archive_write *)_a;
  737. const size_t max_write = INT_MAX;
  738. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  739. ARCHIVE_STATE_DATA, "archive_write_data");
  740. /* In particular, this catches attempts to pass negative values. */
  741. if (s > max_write)
  742. s = max_write;
  743. archive_clear_error(&a->archive);
  744. return ((a->format_write_data)(a, buff, s));
  745. }
  746. static struct archive_write_filter *
  747. filter_lookup(struct archive *_a, int n)
  748. {
  749. struct archive_write *a = (struct archive_write *)_a;
  750. struct archive_write_filter *f = a->filter_first;
  751. if (n == -1)
  752. return a->filter_last;
  753. if (n < 0)
  754. return NULL;
  755. while (n > 0 && f != NULL) {
  756. f = f->next_filter;
  757. --n;
  758. }
  759. return f;
  760. }
  761. static int
  762. _archive_filter_code(struct archive *_a, int n)
  763. {
  764. struct archive_write_filter *f = filter_lookup(_a, n);
  765. return f == NULL ? -1 : f->code;
  766. }
  767. static const char *
  768. _archive_filter_name(struct archive *_a, int n)
  769. {
  770. struct archive_write_filter *f = filter_lookup(_a, n);
  771. return f != NULL ? f->name : NULL;
  772. }
  773. static int64_t
  774. _archive_filter_bytes(struct archive *_a, int n)
  775. {
  776. struct archive_write_filter *f = filter_lookup(_a, n);
  777. return f == NULL ? -1 : f->bytes_written;
  778. }