archive_write.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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: head/lib/libarchive/archive_write.c 201099 2009-12-28 03:03:00Z kientzle $");
  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, int64_t d, 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. if (a->filter_first == NULL)
  197. a->filter_first = f;
  198. else
  199. a->filter_last->next_filter = f;
  200. a->filter_last = f;
  201. return f;
  202. }
  203. /*
  204. * Write data to a particular filter.
  205. */
  206. int
  207. __archive_write_filter(struct archive_write_filter *f,
  208. const void *buff, size_t length)
  209. {
  210. int r;
  211. if (length == 0)
  212. return(ARCHIVE_OK);
  213. if (f->write == NULL)
  214. /* If unset, a fatal error has already occurred, so this filter
  215. * didn't open. We cannot write anything. */
  216. return(ARCHIVE_FATAL);
  217. r = (f->write)(f, buff, length);
  218. f->bytes_written += length;
  219. return (r);
  220. }
  221. /*
  222. * Open a filter.
  223. */
  224. int
  225. __archive_write_open_filter(struct archive_write_filter *f)
  226. {
  227. if (f->open == NULL)
  228. return (ARCHIVE_OK);
  229. return (f->open)(f);
  230. }
  231. /*
  232. * Close a filter.
  233. */
  234. int
  235. __archive_write_close_filter(struct archive_write_filter *f)
  236. {
  237. if (f->close != NULL)
  238. return (f->close)(f);
  239. if (f->next_filter != NULL)
  240. return (__archive_write_close_filter(f->next_filter));
  241. return (ARCHIVE_OK);
  242. }
  243. int
  244. __archive_write_output(struct archive_write *a, const void *buff, size_t length)
  245. {
  246. return (__archive_write_filter(a->filter_first, buff, length));
  247. }
  248. int
  249. __archive_write_nulls(struct archive_write *a, size_t length)
  250. {
  251. if (length == 0)
  252. return (ARCHIVE_OK);
  253. while (length > 0) {
  254. size_t to_write = length < a->null_length ? length : a->null_length;
  255. int r = __archive_write_output(a, a->nulls, to_write);
  256. if (r < ARCHIVE_OK)
  257. return (r);
  258. length -= to_write;
  259. }
  260. return (ARCHIVE_OK);
  261. }
  262. static int
  263. archive_write_client_open(struct archive_write_filter *f)
  264. {
  265. struct archive_write *a = (struct archive_write *)f->archive;
  266. struct archive_none *state;
  267. void *buffer;
  268. size_t buffer_size;
  269. f->bytes_per_block = archive_write_get_bytes_per_block(f->archive);
  270. f->bytes_in_last_block =
  271. archive_write_get_bytes_in_last_block(f->archive);
  272. buffer_size = f->bytes_per_block;
  273. state = (struct archive_none *)calloc(1, sizeof(*state));
  274. buffer = (char *)malloc(buffer_size);
  275. if (state == NULL || buffer == NULL) {
  276. free(state);
  277. free(buffer);
  278. archive_set_error(f->archive, ENOMEM,
  279. "Can't allocate data for output buffering");
  280. return (ARCHIVE_FATAL);
  281. }
  282. state->buffer_size = buffer_size;
  283. state->buffer = buffer;
  284. state->next = state->buffer;
  285. state->avail = state->buffer_size;
  286. f->data = state;
  287. if (a->client_opener == NULL)
  288. return (ARCHIVE_OK);
  289. return (a->client_opener(f->archive, a->client_data));
  290. }
  291. static int
  292. archive_write_client_write(struct archive_write_filter *f,
  293. const void *_buff, size_t length)
  294. {
  295. struct archive_write *a = (struct archive_write *)f->archive;
  296. struct archive_none *state = (struct archive_none *)f->data;
  297. const char *buff = (const char *)_buff;
  298. ssize_t remaining, to_copy;
  299. ssize_t bytes_written;
  300. remaining = length;
  301. /*
  302. * If there is no buffer for blocking, just pass the data
  303. * straight through to the client write callback. In
  304. * particular, this supports "no write delay" operation for
  305. * special applications. Just set the block size to zero.
  306. */
  307. if (state->buffer_size == 0) {
  308. while (remaining > 0) {
  309. bytes_written = (a->client_writer)(&a->archive,
  310. a->client_data, buff, remaining);
  311. if (bytes_written <= 0)
  312. return (ARCHIVE_FATAL);
  313. remaining -= bytes_written;
  314. buff += bytes_written;
  315. }
  316. return (ARCHIVE_OK);
  317. }
  318. /* If the copy buffer isn't empty, try to fill it. */
  319. if (state->avail < state->buffer_size) {
  320. /* If buffer is not empty... */
  321. /* ... copy data into buffer ... */
  322. to_copy = ((size_t)remaining > state->avail) ?
  323. state->avail : (size_t)remaining;
  324. memcpy(state->next, buff, to_copy);
  325. state->next += to_copy;
  326. state->avail -= to_copy;
  327. buff += to_copy;
  328. remaining -= to_copy;
  329. /* ... if it's full, write it out. */
  330. if (state->avail == 0) {
  331. char *p = state->buffer;
  332. size_t to_write = state->buffer_size;
  333. while (to_write > 0) {
  334. bytes_written = (a->client_writer)(&a->archive,
  335. a->client_data, p, to_write);
  336. if (bytes_written <= 0)
  337. return (ARCHIVE_FATAL);
  338. if ((size_t)bytes_written > to_write) {
  339. archive_set_error(&(a->archive),
  340. -1, "write overrun");
  341. return (ARCHIVE_FATAL);
  342. }
  343. p += bytes_written;
  344. to_write -= bytes_written;
  345. }
  346. state->next = state->buffer;
  347. state->avail = state->buffer_size;
  348. }
  349. }
  350. while ((size_t)remaining >= state->buffer_size) {
  351. /* Write out full blocks directly to client. */
  352. bytes_written = (a->client_writer)(&a->archive,
  353. a->client_data, buff, state->buffer_size);
  354. if (bytes_written <= 0)
  355. return (ARCHIVE_FATAL);
  356. buff += bytes_written;
  357. remaining -= bytes_written;
  358. }
  359. if (remaining > 0) {
  360. /* Copy last bit into copy buffer. */
  361. memcpy(state->next, buff, remaining);
  362. state->next += remaining;
  363. state->avail -= remaining;
  364. }
  365. return (ARCHIVE_OK);
  366. }
  367. static int
  368. archive_write_client_close(struct archive_write_filter *f)
  369. {
  370. struct archive_write *a = (struct archive_write *)f->archive;
  371. struct archive_none *state = (struct archive_none *)f->data;
  372. ssize_t block_length;
  373. ssize_t target_block_length;
  374. ssize_t bytes_written;
  375. int ret = ARCHIVE_OK;
  376. /* If there's pending data, pad and write the last block */
  377. if (state->next != state->buffer) {
  378. block_length = state->buffer_size - state->avail;
  379. /* Tricky calculation to determine size of last block */
  380. if (a->bytes_in_last_block <= 0)
  381. /* Default or Zero: pad to full block */
  382. target_block_length = a->bytes_per_block;
  383. else
  384. /* Round to next multiple of bytes_in_last_block. */
  385. target_block_length = a->bytes_in_last_block *
  386. ( (block_length + a->bytes_in_last_block - 1) /
  387. a->bytes_in_last_block);
  388. if (target_block_length > a->bytes_per_block)
  389. target_block_length = a->bytes_per_block;
  390. if (block_length < target_block_length) {
  391. memset(state->next, 0,
  392. target_block_length - block_length);
  393. block_length = target_block_length;
  394. }
  395. bytes_written = (a->client_writer)(&a->archive,
  396. a->client_data, state->buffer, block_length);
  397. ret = bytes_written <= 0 ? ARCHIVE_FATAL : ARCHIVE_OK;
  398. }
  399. if (a->client_closer)
  400. (*a->client_closer)(&a->archive, a->client_data);
  401. free(state->buffer);
  402. free(state);
  403. /* Clear the close handler myself not to be called again. */
  404. f->close = NULL;
  405. a->client_data = NULL;
  406. /* Clear passphrase. */
  407. if (a->passphrase != NULL) {
  408. memset(a->passphrase, 0, strlen(a->passphrase));
  409. free(a->passphrase);
  410. a->passphrase = NULL;
  411. }
  412. return (ret);
  413. }
  414. /*
  415. * Open the archive using the current settings.
  416. */
  417. int
  418. archive_write_open(struct archive *_a, void *client_data,
  419. archive_open_callback *opener, archive_write_callback *writer,
  420. archive_close_callback *closer)
  421. {
  422. struct archive_write *a = (struct archive_write *)_a;
  423. struct archive_write_filter *client_filter;
  424. int ret, r1;
  425. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  426. ARCHIVE_STATE_NEW, "archive_write_open");
  427. archive_clear_error(&a->archive);
  428. a->client_writer = writer;
  429. a->client_opener = opener;
  430. a->client_closer = closer;
  431. a->client_data = client_data;
  432. client_filter = __archive_write_allocate_filter(_a);
  433. client_filter->open = archive_write_client_open;
  434. client_filter->write = archive_write_client_write;
  435. client_filter->close = archive_write_client_close;
  436. ret = __archive_write_open_filter(a->filter_first);
  437. if (ret < ARCHIVE_WARN) {
  438. r1 = __archive_write_close_filter(a->filter_first);
  439. return (r1 < ret ? r1 : ret);
  440. }
  441. a->archive.state = ARCHIVE_STATE_HEADER;
  442. if (a->format_init)
  443. ret = (a->format_init)(a);
  444. return (ret);
  445. }
  446. /*
  447. * Close out the archive.
  448. */
  449. static int
  450. _archive_write_close(struct archive *_a)
  451. {
  452. struct archive_write *a = (struct archive_write *)_a;
  453. int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
  454. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  455. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL,
  456. "archive_write_close");
  457. if (a->archive.state == ARCHIVE_STATE_NEW
  458. || a->archive.state == ARCHIVE_STATE_CLOSED)
  459. return (ARCHIVE_OK); /* Okay to close() when not open. */
  460. archive_clear_error(&a->archive);
  461. /* Finish the last entry if a finish callback is specified */
  462. if (a->archive.state == ARCHIVE_STATE_DATA
  463. && a->format_finish_entry != NULL)
  464. r = ((a->format_finish_entry)(a));
  465. /* Finish off the archive. */
  466. /* TODO: have format closers invoke compression close. */
  467. if (a->format_close != NULL) {
  468. r1 = (a->format_close)(a);
  469. if (r1 < r)
  470. r = r1;
  471. }
  472. /* Finish the compression and close the stream. */
  473. r1 = __archive_write_close_filter(a->filter_first);
  474. if (r1 < r)
  475. r = r1;
  476. if (a->archive.state != ARCHIVE_STATE_FATAL)
  477. a->archive.state = ARCHIVE_STATE_CLOSED;
  478. return (r);
  479. }
  480. static int
  481. _archive_write_filter_count(struct archive *_a)
  482. {
  483. struct archive_write *a = (struct archive_write *)_a;
  484. struct archive_write_filter *p = a->filter_first;
  485. int count = 0;
  486. while(p) {
  487. count++;
  488. p = p->next_filter;
  489. }
  490. return count;
  491. }
  492. void
  493. __archive_write_filters_free(struct archive *_a)
  494. {
  495. struct archive_write *a = (struct archive_write *)_a;
  496. int r = ARCHIVE_OK, r1;
  497. while (a->filter_first != NULL) {
  498. struct archive_write_filter *next
  499. = a->filter_first->next_filter;
  500. if (a->filter_first->free != NULL) {
  501. r1 = (*a->filter_first->free)(a->filter_first);
  502. if (r > r1)
  503. r = r1;
  504. }
  505. free(a->filter_first);
  506. a->filter_first = next;
  507. }
  508. a->filter_last = NULL;
  509. }
  510. /*
  511. * Destroy the archive structure.
  512. *
  513. * Be careful: user might just call write_new and then write_free.
  514. * Don't assume we actually wrote anything or performed any non-trivial
  515. * initialization.
  516. */
  517. static int
  518. _archive_write_free(struct archive *_a)
  519. {
  520. struct archive_write *a = (struct archive_write *)_a;
  521. int r = ARCHIVE_OK, r1;
  522. if (_a == NULL)
  523. return (ARCHIVE_OK);
  524. /* It is okay to call free() in state FATAL. */
  525. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  526. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free");
  527. if (a->archive.state != ARCHIVE_STATE_FATAL)
  528. r = archive_write_close(&a->archive);
  529. /* Release format resources. */
  530. if (a->format_free != NULL) {
  531. r1 = (a->format_free)(a);
  532. if (r1 < r)
  533. r = r1;
  534. }
  535. __archive_write_filters_free(_a);
  536. /* Release various dynamic buffers. */
  537. free((void *)(uintptr_t)(const void *)a->nulls);
  538. archive_string_free(&a->archive.error_string);
  539. if (a->passphrase != NULL) {
  540. /* A passphrase should be cleaned. */
  541. memset(a->passphrase, 0, strlen(a->passphrase));
  542. free(a->passphrase);
  543. }
  544. a->archive.magic = 0;
  545. __archive_clean(&a->archive);
  546. free(a);
  547. return (r);
  548. }
  549. /*
  550. * Write the appropriate header.
  551. */
  552. static int
  553. _archive_write_header(struct archive *_a, struct archive_entry *entry)
  554. {
  555. struct archive_write *a = (struct archive_write *)_a;
  556. int ret, r2;
  557. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  558. ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
  559. archive_clear_error(&a->archive);
  560. if (a->format_write_header == NULL) {
  561. archive_set_error(&(a->archive), -1,
  562. "Format must be set before you can write to an archive.");
  563. a->archive.state = ARCHIVE_STATE_FATAL;
  564. return (ARCHIVE_FATAL);
  565. }
  566. /* In particular, "retry" and "fatal" get returned immediately. */
  567. ret = archive_write_finish_entry(&a->archive);
  568. if (ret == ARCHIVE_FATAL) {
  569. a->archive.state = ARCHIVE_STATE_FATAL;
  570. return (ARCHIVE_FATAL);
  571. }
  572. if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
  573. return (ret);
  574. if (a->skip_file_set &&
  575. archive_entry_dev_is_set(entry) &&
  576. archive_entry_ino_is_set(entry) &&
  577. archive_entry_dev(entry) == (dev_t)a->skip_file_dev &&
  578. archive_entry_ino64(entry) == a->skip_file_ino) {
  579. archive_set_error(&a->archive, 0,
  580. "Can't add archive to itself");
  581. return (ARCHIVE_FAILED);
  582. }
  583. /* Format and write header. */
  584. r2 = ((a->format_write_header)(a, entry));
  585. if (r2 == ARCHIVE_FAILED) {
  586. return (ARCHIVE_FAILED);
  587. }
  588. if (r2 == ARCHIVE_FATAL) {
  589. a->archive.state = ARCHIVE_STATE_FATAL;
  590. return (ARCHIVE_FATAL);
  591. }
  592. if (r2 < ret)
  593. ret = r2;
  594. a->archive.state = ARCHIVE_STATE_DATA;
  595. return (ret);
  596. }
  597. static int
  598. _archive_write_finish_entry(struct archive *_a)
  599. {
  600. struct archive_write *a = (struct archive_write *)_a;
  601. int ret = ARCHIVE_OK;
  602. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  603. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  604. "archive_write_finish_entry");
  605. if (a->archive.state & ARCHIVE_STATE_DATA
  606. && a->format_finish_entry != NULL)
  607. ret = (a->format_finish_entry)(a);
  608. a->archive.state = ARCHIVE_STATE_HEADER;
  609. return (ret);
  610. }
  611. /*
  612. * Note that the compressor is responsible for blocking.
  613. */
  614. static ssize_t
  615. _archive_write_data(struct archive *_a, const void *buff, size_t s)
  616. {
  617. struct archive_write *a = (struct archive_write *)_a;
  618. const size_t max_write = INT_MAX;
  619. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  620. ARCHIVE_STATE_DATA, "archive_write_data");
  621. /* In particular, this catches attempts to pass negative values. */
  622. if (s > max_write)
  623. s = max_write;
  624. archive_clear_error(&a->archive);
  625. return ((a->format_write_data)(a, buff, s));
  626. }
  627. static struct archive_write_filter *
  628. filter_lookup(struct archive *_a, int n)
  629. {
  630. struct archive_write *a = (struct archive_write *)_a;
  631. struct archive_write_filter *f = a->filter_first;
  632. if (n == -1)
  633. return a->filter_last;
  634. if (n < 0)
  635. return NULL;
  636. while (n > 0 && f != NULL) {
  637. f = f->next_filter;
  638. --n;
  639. }
  640. return f;
  641. }
  642. static int
  643. _archive_filter_code(struct archive *_a, int n)
  644. {
  645. struct archive_write_filter *f = filter_lookup(_a, n);
  646. return f == NULL ? -1 : f->code;
  647. }
  648. static const char *
  649. _archive_filter_name(struct archive *_a, int n)
  650. {
  651. struct archive_write_filter *f = filter_lookup(_a, n);
  652. return f != NULL ? f->name : NULL;
  653. }
  654. static int64_t
  655. _archive_filter_bytes(struct archive *_a, int n)
  656. {
  657. struct archive_write_filter *f = filter_lookup(_a, n);
  658. return f == NULL ? -1 : f->bytes_written;
  659. }