archive_write.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. return (ret);
  409. }
  410. /*
  411. * Open the archive using the current settings.
  412. */
  413. int
  414. archive_write_open(struct archive *_a, void *client_data,
  415. archive_open_callback *opener, archive_write_callback *writer,
  416. archive_close_callback *closer)
  417. {
  418. struct archive_write *a = (struct archive_write *)_a;
  419. struct archive_write_filter *client_filter;
  420. int ret, r1;
  421. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  422. ARCHIVE_STATE_NEW, "archive_write_open");
  423. archive_clear_error(&a->archive);
  424. a->client_writer = writer;
  425. a->client_opener = opener;
  426. a->client_closer = closer;
  427. a->client_data = client_data;
  428. client_filter = __archive_write_allocate_filter(_a);
  429. client_filter->open = archive_write_client_open;
  430. client_filter->write = archive_write_client_write;
  431. client_filter->close = archive_write_client_close;
  432. ret = __archive_write_open_filter(a->filter_first);
  433. if (ret < ARCHIVE_WARN) {
  434. r1 = __archive_write_close_filter(a->filter_first);
  435. return (r1 < ret ? r1 : ret);
  436. }
  437. a->archive.state = ARCHIVE_STATE_HEADER;
  438. if (a->format_init)
  439. ret = (a->format_init)(a);
  440. return (ret);
  441. }
  442. /*
  443. * Close out the archive.
  444. */
  445. static int
  446. _archive_write_close(struct archive *_a)
  447. {
  448. struct archive_write *a = (struct archive_write *)_a;
  449. int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
  450. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  451. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL,
  452. "archive_write_close");
  453. if (a->archive.state == ARCHIVE_STATE_NEW
  454. || a->archive.state == ARCHIVE_STATE_CLOSED)
  455. return (ARCHIVE_OK); /* Okay to close() when not open. */
  456. archive_clear_error(&a->archive);
  457. /* Finish the last entry. */
  458. if (a->archive.state == ARCHIVE_STATE_DATA)
  459. r = ((a->format_finish_entry)(a));
  460. /* Finish off the archive. */
  461. /* TODO: have format closers invoke compression close. */
  462. if (a->format_close != NULL) {
  463. r1 = (a->format_close)(a);
  464. if (r1 < r)
  465. r = r1;
  466. }
  467. /* Finish the compression and close the stream. */
  468. r1 = __archive_write_close_filter(a->filter_first);
  469. if (r1 < r)
  470. r = r1;
  471. if (a->archive.state != ARCHIVE_STATE_FATAL)
  472. a->archive.state = ARCHIVE_STATE_CLOSED;
  473. return (r);
  474. }
  475. static int
  476. _archive_write_filter_count(struct archive *_a)
  477. {
  478. struct archive_write *a = (struct archive_write *)_a;
  479. struct archive_write_filter *p = a->filter_first;
  480. int count = 0;
  481. while(p) {
  482. count++;
  483. p = p->next_filter;
  484. }
  485. return count;
  486. }
  487. void
  488. __archive_write_filters_free(struct archive *_a)
  489. {
  490. struct archive_write *a = (struct archive_write *)_a;
  491. int r = ARCHIVE_OK, r1;
  492. while (a->filter_first != NULL) {
  493. struct archive_write_filter *next
  494. = a->filter_first->next_filter;
  495. if (a->filter_first->free != NULL) {
  496. r1 = (*a->filter_first->free)(a->filter_first);
  497. if (r > r1)
  498. r = r1;
  499. }
  500. free(a->filter_first);
  501. a->filter_first = next;
  502. }
  503. a->filter_last = NULL;
  504. }
  505. /*
  506. * Destroy the archive structure.
  507. *
  508. * Be careful: user might just call write_new and then write_free.
  509. * Don't assume we actually wrote anything or performed any non-trivial
  510. * initialization.
  511. */
  512. static int
  513. _archive_write_free(struct archive *_a)
  514. {
  515. struct archive_write *a = (struct archive_write *)_a;
  516. int r = ARCHIVE_OK, r1;
  517. if (_a == NULL)
  518. return (ARCHIVE_OK);
  519. /* It is okay to call free() in state FATAL. */
  520. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  521. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free");
  522. if (a->archive.state != ARCHIVE_STATE_FATAL)
  523. r = archive_write_close(&a->archive);
  524. /* Release format resources. */
  525. if (a->format_free != NULL) {
  526. r1 = (a->format_free)(a);
  527. if (r1 < r)
  528. r = r1;
  529. }
  530. __archive_write_filters_free(_a);
  531. /* Release various dynamic buffers. */
  532. free((void *)(uintptr_t)(const void *)a->nulls);
  533. archive_string_free(&a->archive.error_string);
  534. a->archive.magic = 0;
  535. __archive_clean(&a->archive);
  536. free(a);
  537. return (r);
  538. }
  539. /*
  540. * Write the appropriate header.
  541. */
  542. static int
  543. _archive_write_header(struct archive *_a, struct archive_entry *entry)
  544. {
  545. struct archive_write *a = (struct archive_write *)_a;
  546. int ret, r2;
  547. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  548. ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
  549. archive_clear_error(&a->archive);
  550. if (a->format_write_header == NULL) {
  551. archive_set_error(&(a->archive), -1,
  552. "Format must be set before you can write to an archive.");
  553. a->archive.state = ARCHIVE_STATE_FATAL;
  554. return (ARCHIVE_FATAL);
  555. }
  556. /* In particular, "retry" and "fatal" get returned immediately. */
  557. ret = archive_write_finish_entry(&a->archive);
  558. if (ret == ARCHIVE_FATAL) {
  559. a->archive.state = ARCHIVE_STATE_FATAL;
  560. return (ARCHIVE_FATAL);
  561. }
  562. if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
  563. return (ret);
  564. if (a->skip_file_set &&
  565. archive_entry_dev_is_set(entry) &&
  566. archive_entry_ino_is_set(entry) &&
  567. archive_entry_dev(entry) == (dev_t)a->skip_file_dev &&
  568. archive_entry_ino64(entry) == a->skip_file_ino) {
  569. archive_set_error(&a->archive, 0,
  570. "Can't add archive to itself");
  571. return (ARCHIVE_FAILED);
  572. }
  573. /* Format and write header. */
  574. r2 = ((a->format_write_header)(a, entry));
  575. if (r2 == ARCHIVE_FATAL) {
  576. a->archive.state = ARCHIVE_STATE_FATAL;
  577. return (ARCHIVE_FATAL);
  578. }
  579. if (r2 < ret)
  580. ret = r2;
  581. a->archive.state = ARCHIVE_STATE_DATA;
  582. return (ret);
  583. }
  584. static int
  585. _archive_write_finish_entry(struct archive *_a)
  586. {
  587. struct archive_write *a = (struct archive_write *)_a;
  588. int ret = ARCHIVE_OK;
  589. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  590. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  591. "archive_write_finish_entry");
  592. if (a->archive.state & ARCHIVE_STATE_DATA)
  593. ret = (a->format_finish_entry)(a);
  594. a->archive.state = ARCHIVE_STATE_HEADER;
  595. return (ret);
  596. }
  597. /*
  598. * Note that the compressor is responsible for blocking.
  599. */
  600. static ssize_t
  601. _archive_write_data(struct archive *_a, const void *buff, size_t s)
  602. {
  603. struct archive_write *a = (struct archive_write *)_a;
  604. archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
  605. ARCHIVE_STATE_DATA, "archive_write_data");
  606. archive_clear_error(&a->archive);
  607. return ((a->format_write_data)(a, buff, s));
  608. }
  609. static struct archive_write_filter *
  610. filter_lookup(struct archive *_a, int n)
  611. {
  612. struct archive_write *a = (struct archive_write *)_a;
  613. struct archive_write_filter *f = a->filter_first;
  614. if (n == -1)
  615. return a->filter_last;
  616. if (n < 0)
  617. return NULL;
  618. while (n > 0 && f != NULL) {
  619. f = f->next_filter;
  620. --n;
  621. }
  622. return f;
  623. }
  624. static int
  625. _archive_filter_code(struct archive *_a, int n)
  626. {
  627. struct archive_write_filter *f = filter_lookup(_a, n);
  628. return f == NULL ? -1 : f->code;
  629. }
  630. static const char *
  631. _archive_filter_name(struct archive *_a, int n)
  632. {
  633. struct archive_write_filter *f = filter_lookup(_a, n);
  634. return f != NULL ? f->name : NULL;
  635. }
  636. static int64_t
  637. _archive_filter_bytes(struct archive *_a, int n)
  638. {
  639. struct archive_write_filter *f = filter_lookup(_a, n);
  640. return f == NULL ? -1 : f->bytes_written;
  641. }