archive_write.c 21 KB

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