archive_write.c 20 KB

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