2 * Copyright (c) 2003-2010 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write.c 201099 2009-12-28 03:03:00Z kientzle $");
30 * This file contains the "essential" portions of the write API, that
31 * is, stuff that will essentially always be used by any client that
32 * actually needs to write an archive. Optional pieces have been, as
33 * far as possible, separated out into separate files to reduce
34 * needlessly bloating statically-linked clients.
37 #ifdef HAVE_SYS_WAIT_H
59 #include "archive_entry.h"
60 #include "archive_private.h"
61 #include "archive_write_private.h"
63 static int _archive_filter_code(struct archive *, int);
64 static const char *_archive_filter_name(struct archive *, int);
65 static int64_t _archive_filter_bytes(struct archive *, int);
66 static int _archive_write_filter_count(struct archive *);
67 static int _archive_write_close(struct archive *);
68 static int _archive_write_free(struct archive *);
69 static int _archive_write_header(struct archive *, struct archive_entry *);
70 static int _archive_write_finish_entry(struct archive *);
71 static ssize_t _archive_write_data(struct archive *, const void *, size_t);
80 static const struct archive_vtable
81 archive_write_vtable = {
82 .archive_close = _archive_write_close,
83 .archive_filter_bytes = _archive_filter_bytes,
84 .archive_filter_code = _archive_filter_code,
85 .archive_filter_name = _archive_filter_name,
86 .archive_filter_count = _archive_write_filter_count,
87 .archive_free = _archive_write_free,
88 .archive_write_header = _archive_write_header,
89 .archive_write_finish_entry = _archive_write_finish_entry,
90 .archive_write_data = _archive_write_data,
94 * Allocate, initialize and return an archive object.
97 archive_write_new(void)
99 struct archive_write *a;
100 unsigned char *nulls;
102 a = (struct archive_write *)calloc(1, sizeof(*a));
105 a->archive.magic = ARCHIVE_WRITE_MAGIC;
106 a->archive.state = ARCHIVE_STATE_NEW;
107 a->archive.vtable = &archive_write_vtable;
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.
113 a->bytes_per_block = 10240;
114 a->bytes_in_last_block = -1; /* Default */
116 /* Initialize a block of nulls for padding purposes. */
117 a->null_length = 1024;
118 nulls = (unsigned char *)calloc(1, a->null_length);
124 return (&a->archive);
128 * Set the block size. Returns 0 if successful.
131 archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
133 struct archive_write *a = (struct archive_write *)_a;
134 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
135 ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
136 a->bytes_per_block = bytes_per_block;
141 * Get the current block size. -1 if it has never been set.
144 archive_write_get_bytes_per_block(struct archive *_a)
146 struct archive_write *a = (struct archive_write *)_a;
147 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
148 ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
149 return (a->bytes_per_block);
153 * Set the size for the last block.
154 * Returns 0 if successful.
157 archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
159 struct archive_write *a = (struct archive_write *)_a;
160 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
161 ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
162 a->bytes_in_last_block = bytes;
167 * Return the value set above. -1 indicates it has not been set.
170 archive_write_get_bytes_in_last_block(struct archive *_a)
172 struct archive_write *a = (struct archive_write *)_a;
173 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
174 ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
175 return (a->bytes_in_last_block);
179 * dev/ino of a file to be rejected. Used to prevent adding
180 * an archive to itself recursively.
183 archive_write_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
185 struct archive_write *a = (struct archive_write *)_a;
186 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
187 ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
188 a->skip_file_set = 1;
189 a->skip_file_dev = d;
190 a->skip_file_ino = i;
195 * Allocate and return the next filter structure.
197 struct archive_write_filter *
198 __archive_write_allocate_filter(struct archive *_a)
200 struct archive_write *a = (struct archive_write *)_a;
201 struct archive_write_filter *f;
203 f = calloc(1, sizeof(*f));
205 f->state = ARCHIVE_WRITE_FILTER_STATE_NEW;
206 if (a->filter_first == NULL)
209 a->filter_last->next_filter = f;
215 * Write data to a particular filter.
218 __archive_write_filter(struct archive_write_filter *f,
219 const void *buff, size_t length)
222 /* Never write to non-open filters */
223 if (f->state != ARCHIVE_WRITE_FILTER_STATE_OPEN)
224 return(ARCHIVE_FATAL);
227 if (f->write == NULL)
228 /* If unset, a fatal error has already occurred, so this filter
229 * didn't open. We cannot write anything. */
230 return(ARCHIVE_FATAL);
231 r = (f->write)(f, buff, length);
232 f->bytes_written += length;
237 * Recursive function for opening the filter chain
238 * Last filter is opened first
241 __archive_write_open_filter(struct archive_write_filter *f)
246 if (f->next_filter != NULL)
247 ret = __archive_write_open_filter(f->next_filter);
248 if (ret != ARCHIVE_OK)
250 if (f->state != ARCHIVE_WRITE_FILTER_STATE_NEW)
251 return (ARCHIVE_FATAL);
252 if (f->open == NULL) {
253 f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
257 if (ret == ARCHIVE_OK)
258 f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
260 f->state = ARCHIVE_WRITE_FILTER_STATE_FATAL;
268 __archive_write_filters_open(struct archive_write *a)
270 return (__archive_write_open_filter(a->filter_first));
277 __archive_write_filters_close(struct archive_write *a)
279 struct archive_write_filter *f;
282 for (f = a->filter_first; f != NULL; f = f->next_filter) {
283 /* Do not close filters that are not open */
284 if (f->state == ARCHIVE_WRITE_FILTER_STATE_OPEN) {
285 if (f->close != NULL) {
286 ret1 = (f->close)(f);
289 if (ret1 == ARCHIVE_OK) {
291 ARCHIVE_WRITE_FILTER_STATE_CLOSED;
294 ARCHIVE_WRITE_FILTER_STATE_FATAL;
297 f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
304 __archive_write_output(struct archive_write *a, const void *buff, size_t length)
306 return (__archive_write_filter(a->filter_first, buff, length));
310 __archive_write_nulls(struct archive_write *a, size_t length)
316 size_t to_write = length < a->null_length ? length : a->null_length;
317 int r = __archive_write_output(a, a->nulls, to_write);
326 archive_write_client_open(struct archive_write_filter *f)
328 struct archive_write *a = (struct archive_write *)f->archive;
329 struct archive_none *state;
334 f->bytes_per_block = archive_write_get_bytes_per_block(f->archive);
335 f->bytes_in_last_block =
336 archive_write_get_bytes_in_last_block(f->archive);
337 buffer_size = f->bytes_per_block;
339 state = (struct archive_none *)calloc(1, sizeof(*state));
340 buffer = (char *)malloc(buffer_size);
341 if (state == NULL || buffer == NULL) {
344 archive_set_error(f->archive, ENOMEM,
345 "Can't allocate data for output buffering");
346 return (ARCHIVE_FATAL);
349 state->buffer_size = buffer_size;
350 state->buffer = buffer;
351 state->next = state->buffer;
352 state->avail = state->buffer_size;
355 if (a->client_opener == NULL)
357 ret = a->client_opener(f->archive, a->client_data);
358 if (ret != ARCHIVE_OK) {
367 archive_write_client_write(struct archive_write_filter *f,
368 const void *_buff, size_t length)
370 struct archive_write *a = (struct archive_write *)f->archive;
371 struct archive_none *state = (struct archive_none *)f->data;
372 const char *buff = (const char *)_buff;
373 ssize_t remaining, to_copy;
374 ssize_t bytes_written;
379 * If there is no buffer for blocking, just pass the data
380 * straight through to the client write callback. In
381 * particular, this supports "no write delay" operation for
382 * special applications. Just set the block size to zero.
384 if (state->buffer_size == 0) {
385 while (remaining > 0) {
386 bytes_written = (a->client_writer)(&a->archive,
387 a->client_data, buff, remaining);
388 if (bytes_written <= 0)
389 return (ARCHIVE_FATAL);
390 remaining -= bytes_written;
391 buff += bytes_written;
396 /* If the copy buffer isn't empty, try to fill it. */
397 if (state->avail < state->buffer_size) {
398 /* If buffer is not empty... */
399 /* ... copy data into buffer ... */
400 to_copy = ((size_t)remaining > state->avail) ?
401 state->avail : (size_t)remaining;
402 memcpy(state->next, buff, to_copy);
403 state->next += to_copy;
404 state->avail -= to_copy;
406 remaining -= to_copy;
407 /* ... if it's full, write it out. */
408 if (state->avail == 0) {
409 char *p = state->buffer;
410 size_t to_write = state->buffer_size;
411 while (to_write > 0) {
412 bytes_written = (a->client_writer)(&a->archive,
413 a->client_data, p, to_write);
414 if (bytes_written <= 0)
415 return (ARCHIVE_FATAL);
416 if ((size_t)bytes_written > to_write) {
417 archive_set_error(&(a->archive),
418 -1, "write overrun");
419 return (ARCHIVE_FATAL);
422 to_write -= bytes_written;
424 state->next = state->buffer;
425 state->avail = state->buffer_size;
429 while ((size_t)remaining >= state->buffer_size) {
430 /* Write out full blocks directly to client. */
431 bytes_written = (a->client_writer)(&a->archive,
432 a->client_data, buff, state->buffer_size);
433 if (bytes_written <= 0)
434 return (ARCHIVE_FATAL);
435 buff += bytes_written;
436 remaining -= bytes_written;
440 /* Copy last bit into copy buffer. */
441 memcpy(state->next, buff, remaining);
442 state->next += remaining;
443 state->avail -= remaining;
449 archive_write_client_free(struct archive_write_filter *f)
451 struct archive_write *a = (struct archive_write *)f->archive;
454 (*a->client_freer)(&a->archive, a->client_data);
455 a->client_data = NULL;
457 /* Clear passphrase. */
458 if (a->passphrase != NULL) {
459 memset(a->passphrase, 0, strlen(a->passphrase));
461 a->passphrase = NULL;
468 archive_write_client_close(struct archive_write_filter *f)
470 struct archive_write *a = (struct archive_write *)f->archive;
471 struct archive_none *state = (struct archive_none *)f->data;
472 ssize_t block_length;
473 ssize_t target_block_length;
474 ssize_t bytes_written;
477 int ret = ARCHIVE_OK;
479 /* If there's pending data, pad and write the last block */
480 if (state->next != state->buffer) {
481 block_length = state->buffer_size - state->avail;
483 /* Tricky calculation to determine size of last block */
484 if (a->bytes_in_last_block <= 0)
485 /* Default or Zero: pad to full block */
486 target_block_length = a->bytes_per_block;
488 /* Round to next multiple of bytes_in_last_block. */
489 target_block_length = a->bytes_in_last_block *
490 ( (block_length + a->bytes_in_last_block - 1) /
491 a->bytes_in_last_block);
492 if (target_block_length > a->bytes_per_block)
493 target_block_length = a->bytes_per_block;
494 if (block_length < target_block_length) {
495 memset(state->next, 0,
496 target_block_length - block_length);
497 block_length = target_block_length;
500 to_write = block_length;
501 while (to_write > 0) {
502 bytes_written = (a->client_writer)(&a->archive,
503 a->client_data, p, to_write);
504 if (bytes_written <= 0) {
508 if ((size_t)bytes_written > to_write) {
509 archive_set_error(&(a->archive),
510 -1, "write overrun");
515 to_write -= bytes_written;
518 if (a->client_closer)
519 (*a->client_closer)(&a->archive, a->client_data);
523 /* Clear the close handler myself not to be called again. */
524 f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
529 * Open the archive using the current settings.
532 archive_write_open2(struct archive *_a, void *client_data,
533 archive_open_callback *opener, archive_write_callback *writer,
534 archive_close_callback *closer, archive_free_callback *freer)
536 struct archive_write *a = (struct archive_write *)_a;
537 struct archive_write_filter *client_filter;
540 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
541 ARCHIVE_STATE_NEW, "archive_write_open");
542 archive_clear_error(&a->archive);
544 a->client_writer = writer;
545 a->client_opener = opener;
546 a->client_closer = closer;
547 a->client_freer = freer;
548 a->client_data = client_data;
550 client_filter = __archive_write_allocate_filter(_a);
551 client_filter->open = archive_write_client_open;
552 client_filter->write = archive_write_client_write;
553 client_filter->close = archive_write_client_close;
554 client_filter->free = archive_write_client_free;
556 ret = __archive_write_filters_open(a);
557 if (ret < ARCHIVE_WARN) {
558 r1 = __archive_write_filters_close(a);
559 __archive_write_filters_free(_a);
560 return (r1 < ret ? r1 : ret);
563 a->archive.state = ARCHIVE_STATE_HEADER;
565 ret = (a->format_init)(a);
570 archive_write_open(struct archive *_a, void *client_data,
571 archive_open_callback *opener, archive_write_callback *writer,
572 archive_close_callback *closer)
574 return archive_write_open2(_a, client_data, opener, writer,
579 * Close out the archive.
582 _archive_write_close(struct archive *_a)
584 struct archive_write *a = (struct archive_write *)_a;
585 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
587 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
588 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL,
589 "archive_write_close");
590 if (a->archive.state == ARCHIVE_STATE_NEW
591 || a->archive.state == ARCHIVE_STATE_CLOSED)
592 return (ARCHIVE_OK); /* Okay to close() when not open. */
594 archive_clear_error(&a->archive);
596 /* Finish the last entry if a finish callback is specified */
597 if (a->archive.state == ARCHIVE_STATE_DATA
598 && a->format_finish_entry != NULL)
599 r = ((a->format_finish_entry)(a));
601 /* Finish off the archive. */
602 /* TODO: have format closers invoke compression close. */
603 if (a->format_close != NULL) {
604 r1 = (a->format_close)(a);
609 /* Finish the compression and close the stream. */
610 r1 = __archive_write_filters_close(a);
614 if (a->archive.state != ARCHIVE_STATE_FATAL)
615 a->archive.state = ARCHIVE_STATE_CLOSED;
620 _archive_write_filter_count(struct archive *_a)
622 struct archive_write *a = (struct archive_write *)_a;
623 struct archive_write_filter *p = a->filter_first;
633 __archive_write_filters_free(struct archive *_a)
635 struct archive_write *a = (struct archive_write *)_a;
636 int r = ARCHIVE_OK, r1;
638 while (a->filter_first != NULL) {
639 struct archive_write_filter *next
640 = a->filter_first->next_filter;
641 if (a->filter_first->free != NULL) {
642 r1 = (*a->filter_first->free)(a->filter_first);
646 free(a->filter_first);
647 a->filter_first = next;
649 a->filter_last = NULL;
653 * Destroy the archive structure.
655 * Be careful: user might just call write_new and then write_free.
656 * Don't assume we actually wrote anything or performed any non-trivial
660 _archive_write_free(struct archive *_a)
662 struct archive_write *a = (struct archive_write *)_a;
663 int r = ARCHIVE_OK, r1;
667 /* It is okay to call free() in state FATAL. */
668 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
669 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free");
670 if (a->archive.state != ARCHIVE_STATE_FATAL)
671 r = archive_write_close(&a->archive);
673 /* Release format resources. */
674 if (a->format_free != NULL) {
675 r1 = (a->format_free)(a);
680 __archive_write_filters_free(_a);
682 /* Release various dynamic buffers. */
683 free((void *)(uintptr_t)(const void *)a->nulls);
684 archive_string_free(&a->archive.error_string);
685 if (a->passphrase != NULL) {
686 /* A passphrase should be cleaned. */
687 memset(a->passphrase, 0, strlen(a->passphrase));
690 a->archive.magic = 0;
691 __archive_clean(&a->archive);
697 * Write the appropriate header.
700 _archive_write_header(struct archive *_a, struct archive_entry *entry)
702 struct archive_write *a = (struct archive_write *)_a;
705 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
706 ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
707 archive_clear_error(&a->archive);
709 if (a->format_write_header == NULL) {
710 archive_set_error(&(a->archive), -1,
711 "Format must be set before you can write to an archive.");
712 a->archive.state = ARCHIVE_STATE_FATAL;
713 return (ARCHIVE_FATAL);
716 /* In particular, "retry" and "fatal" get returned immediately. */
717 ret = archive_write_finish_entry(&a->archive);
718 if (ret == ARCHIVE_FATAL) {
719 a->archive.state = ARCHIVE_STATE_FATAL;
720 return (ARCHIVE_FATAL);
722 if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
725 if (a->skip_file_set &&
726 archive_entry_dev_is_set(entry) &&
727 archive_entry_ino_is_set(entry) &&
728 archive_entry_dev(entry) == (dev_t)a->skip_file_dev &&
729 archive_entry_ino64(entry) == a->skip_file_ino) {
730 archive_set_error(&a->archive, 0,
731 "Can't add archive to itself");
732 return (ARCHIVE_FAILED);
735 /* Format and write header. */
736 r2 = ((a->format_write_header)(a, entry));
737 if (r2 == ARCHIVE_FAILED) {
738 return (ARCHIVE_FAILED);
740 if (r2 == ARCHIVE_FATAL) {
741 a->archive.state = ARCHIVE_STATE_FATAL;
742 return (ARCHIVE_FATAL);
747 a->archive.state = ARCHIVE_STATE_DATA;
752 _archive_write_finish_entry(struct archive *_a)
754 struct archive_write *a = (struct archive_write *)_a;
755 int ret = ARCHIVE_OK;
757 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
758 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
759 "archive_write_finish_entry");
760 if (a->archive.state & ARCHIVE_STATE_DATA
761 && a->format_finish_entry != NULL)
762 ret = (a->format_finish_entry)(a);
763 a->archive.state = ARCHIVE_STATE_HEADER;
768 * Note that the compressor is responsible for blocking.
771 _archive_write_data(struct archive *_a, const void *buff, size_t s)
773 struct archive_write *a = (struct archive_write *)_a;
774 const size_t max_write = INT_MAX;
776 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
777 ARCHIVE_STATE_DATA, "archive_write_data");
778 /* In particular, this catches attempts to pass negative values. */
781 archive_clear_error(&a->archive);
782 return ((a->format_write_data)(a, buff, s));
785 static struct archive_write_filter *
786 filter_lookup(struct archive *_a, int n)
788 struct archive_write *a = (struct archive_write *)_a;
789 struct archive_write_filter *f = a->filter_first;
791 return a->filter_last;
794 while (n > 0 && f != NULL) {
802 _archive_filter_code(struct archive *_a, int n)
804 struct archive_write_filter *f = filter_lookup(_a, n);
805 return f == NULL ? -1 : f->code;
809 _archive_filter_name(struct archive *_a, int n)
811 struct archive_write_filter *f = filter_lookup(_a, n);
812 return f != NULL ? f->name : NULL;
816 _archive_filter_bytes(struct archive *_a, int n)
818 struct archive_write_filter *f = filter_lookup(_a, n);
819 return f == NULL ? -1 : f->bytes_written;