HardenedBSD src tree
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

783 lines
21KB

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