archive_write.c 20 KB

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