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$");
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 struct archive_vtable *archive_write_vtable(void);
65 static int _archive_filter_code(struct archive *, int);
66 static const char *_archive_filter_name(struct archive *, int);
67 static int64_t _archive_filter_bytes(struct archive *, int);
68 static int _archive_write_filter_count(struct archive *);
69 static int _archive_write_close(struct archive *);
70 static int _archive_write_free(struct archive *);
71 static int _archive_write_header(struct archive *, struct archive_entry *);
72 static int _archive_write_finish_entry(struct archive *);
73 static ssize_t _archive_write_data(struct archive *, const void *, size_t);
82 static struct archive_vtable *
83 archive_write_vtable(void)
85 static struct archive_vtable av;
86 static int inited = 0;
89 av.archive_close = _archive_write_close;
90 av.archive_filter_bytes = _archive_filter_bytes;
91 av.archive_filter_code = _archive_filter_code;
92 av.archive_filter_name = _archive_filter_name;
93 av.archive_filter_count = _archive_write_filter_count;
94 av.archive_free = _archive_write_free;
95 av.archive_write_header = _archive_write_header;
96 av.archive_write_finish_entry = _archive_write_finish_entry;
97 av.archive_write_data = _archive_write_data;
104 * Allocate, initialize and return an archive object.
107 archive_write_new(void)
109 struct archive_write *a;
110 unsigned char *nulls;
112 a = (struct archive_write *)calloc(1, sizeof(*a));
115 a->archive.magic = ARCHIVE_WRITE_MAGIC;
116 a->archive.state = ARCHIVE_STATE_NEW;
117 a->archive.vtable = archive_write_vtable();
119 * The value 10240 here matches the traditional tar default,
120 * but is otherwise arbitrary.
121 * TODO: Set the default block size from the format selected.
123 a->bytes_per_block = 10240;
124 a->bytes_in_last_block = -1; /* Default */
126 /* Initialize a block of nulls for padding purposes. */
127 a->null_length = 1024;
128 nulls = (unsigned char *)calloc(1, a->null_length);
134 return (&a->archive);
138 * Set the block size. Returns 0 if successful.
141 archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
143 struct archive_write *a = (struct archive_write *)_a;
144 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
145 ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
146 a->bytes_per_block = bytes_per_block;
151 * Get the current block size. -1 if it has never been set.
154 archive_write_get_bytes_per_block(struct archive *_a)
156 struct archive_write *a = (struct archive_write *)_a;
157 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
158 ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
159 return (a->bytes_per_block);
163 * Set the size for the last block.
164 * Returns 0 if successful.
167 archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
169 struct archive_write *a = (struct archive_write *)_a;
170 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
171 ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
172 a->bytes_in_last_block = bytes;
177 * Return the value set above. -1 indicates it has not been set.
180 archive_write_get_bytes_in_last_block(struct archive *_a)
182 struct archive_write *a = (struct archive_write *)_a;
183 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
184 ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
185 return (a->bytes_in_last_block);
189 * dev/ino of a file to be rejected. Used to prevent adding
190 * an archive to itself recursively.
193 archive_write_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
195 struct archive_write *a = (struct archive_write *)_a;
196 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
197 ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
198 a->skip_file_set = 1;
199 a->skip_file_dev = d;
200 a->skip_file_ino = i;
205 * Allocate and return the next filter structure.
207 struct archive_write_filter *
208 __archive_write_allocate_filter(struct archive *_a)
210 struct archive_write *a = (struct archive_write *)_a;
211 struct archive_write_filter *f;
213 f = calloc(1, sizeof(*f));
215 f->state = ARCHIVE_WRITE_FILTER_STATE_NEW;
216 if (a->filter_first == NULL)
219 a->filter_last->next_filter = f;
225 * Write data to a particular filter.
228 __archive_write_filter(struct archive_write_filter *f,
229 const void *buff, size_t length)
232 /* Never write to non-open filters */
233 if (f->state != ARCHIVE_WRITE_FILTER_STATE_OPEN)
234 return(ARCHIVE_FATAL);
237 if (f->write == NULL)
238 /* If unset, a fatal error has already occurred, so this filter
239 * didn't open. We cannot write anything. */
240 return(ARCHIVE_FATAL);
241 r = (f->write)(f, buff, length);
242 f->bytes_written += length;
247 * Recursive function for opening the filter chain
248 * Last filter is opened first
251 __archive_write_open_filter(struct archive_write_filter *f)
256 if (f->next_filter != NULL)
257 ret = __archive_write_open_filter(f->next_filter);
258 if (ret != ARCHIVE_OK)
260 if (f->state != ARCHIVE_WRITE_FILTER_STATE_NEW)
261 return (ARCHIVE_FATAL);
262 if (f->open == NULL) {
263 f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
267 if (ret == ARCHIVE_OK)
268 f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
270 f->state = ARCHIVE_WRITE_FILTER_STATE_FATAL;
278 __archive_write_filters_open(struct archive_write *a)
280 return (__archive_write_open_filter(a->filter_first));
287 __archive_write_filters_close(struct archive_write *a)
289 struct archive_write_filter *f;
292 for (f = a->filter_first; f != NULL; f = f->next_filter) {
293 /* Do not close filters that are not open */
294 if (f->state == ARCHIVE_WRITE_FILTER_STATE_OPEN) {
295 if (f->close != NULL) {
296 ret1 = (f->close)(f);
299 if (ret1 == ARCHIVE_OK) {
301 ARCHIVE_WRITE_FILTER_STATE_CLOSED;
304 ARCHIVE_WRITE_FILTER_STATE_FATAL;
307 f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
314 __archive_write_output(struct archive_write *a, const void *buff, size_t length)
316 return (__archive_write_filter(a->filter_first, buff, length));
320 __archive_write_nulls(struct archive_write *a, size_t length)
326 size_t to_write = length < a->null_length ? length : a->null_length;
327 int r = __archive_write_output(a, a->nulls, to_write);
336 archive_write_client_open(struct archive_write_filter *f)
338 struct archive_write *a = (struct archive_write *)f->archive;
339 struct archive_none *state;
344 f->bytes_per_block = archive_write_get_bytes_per_block(f->archive);
345 f->bytes_in_last_block =
346 archive_write_get_bytes_in_last_block(f->archive);
347 buffer_size = f->bytes_per_block;
349 state = (struct archive_none *)calloc(1, sizeof(*state));
350 buffer = (char *)malloc(buffer_size);
351 if (state == NULL || buffer == NULL) {
354 archive_set_error(f->archive, ENOMEM,
355 "Can't allocate data for output buffering");
356 return (ARCHIVE_FATAL);
359 state->buffer_size = buffer_size;
360 state->buffer = buffer;
361 state->next = state->buffer;
362 state->avail = state->buffer_size;
365 if (a->client_opener == NULL)
367 ret = a->client_opener(f->archive, a->client_data);
368 if (ret != ARCHIVE_OK) {
377 archive_write_client_write(struct archive_write_filter *f,
378 const void *_buff, size_t length)
380 struct archive_write *a = (struct archive_write *)f->archive;
381 struct archive_none *state = (struct archive_none *)f->data;
382 const char *buff = (const char *)_buff;
383 ssize_t remaining, to_copy;
384 ssize_t bytes_written;
389 * If there is no buffer for blocking, just pass the data
390 * straight through to the client write callback. In
391 * particular, this supports "no write delay" operation for
392 * special applications. Just set the block size to zero.
394 if (state->buffer_size == 0) {
395 while (remaining > 0) {
396 bytes_written = (a->client_writer)(&a->archive,
397 a->client_data, buff, remaining);
398 if (bytes_written <= 0)
399 return (ARCHIVE_FATAL);
400 remaining -= bytes_written;
401 buff += bytes_written;
406 /* If the copy buffer isn't empty, try to fill it. */
407 if (state->avail < state->buffer_size) {
408 /* If buffer is not empty... */
409 /* ... copy data into buffer ... */
410 to_copy = ((size_t)remaining > state->avail) ?
411 state->avail : (size_t)remaining;
412 memcpy(state->next, buff, to_copy);
413 state->next += to_copy;
414 state->avail -= to_copy;
416 remaining -= to_copy;
417 /* ... if it's full, write it out. */
418 if (state->avail == 0) {
419 char *p = state->buffer;
420 size_t to_write = state->buffer_size;
421 while (to_write > 0) {
422 bytes_written = (a->client_writer)(&a->archive,
423 a->client_data, p, to_write);
424 if (bytes_written <= 0)
425 return (ARCHIVE_FATAL);
426 if ((size_t)bytes_written > to_write) {
427 archive_set_error(&(a->archive),
428 -1, "write overrun");
429 return (ARCHIVE_FATAL);
432 to_write -= bytes_written;
434 state->next = state->buffer;
435 state->avail = state->buffer_size;
439 while ((size_t)remaining >= state->buffer_size) {
440 /* Write out full blocks directly to client. */
441 bytes_written = (a->client_writer)(&a->archive,
442 a->client_data, buff, state->buffer_size);
443 if (bytes_written <= 0)
444 return (ARCHIVE_FATAL);
445 buff += bytes_written;
446 remaining -= bytes_written;
450 /* Copy last bit into copy buffer. */
451 memcpy(state->next, buff, remaining);
452 state->next += remaining;
453 state->avail -= remaining;
459 archive_write_client_free(struct archive_write_filter *f)
461 struct archive_write *a = (struct archive_write *)f->archive;
464 (*a->client_freer)(&a->archive, a->client_data);
465 a->client_data = NULL;
467 /* Clear passphrase. */
468 if (a->passphrase != NULL) {
469 memset(a->passphrase, 0, strlen(a->passphrase));
471 a->passphrase = NULL;
478 archive_write_client_close(struct archive_write_filter *f)
480 struct archive_write *a = (struct archive_write *)f->archive;
481 struct archive_none *state = (struct archive_none *)f->data;
482 ssize_t block_length;
483 ssize_t target_block_length;
484 ssize_t bytes_written;
485 int ret = ARCHIVE_OK;
487 /* If there's pending data, pad and write the last block */
488 if (state->next != state->buffer) {
489 block_length = state->buffer_size - state->avail;
491 /* Tricky calculation to determine size of last block */
492 if (a->bytes_in_last_block <= 0)
493 /* Default or Zero: pad to full block */
494 target_block_length = a->bytes_per_block;
496 /* Round to next multiple of bytes_in_last_block. */
497 target_block_length = a->bytes_in_last_block *
498 ( (block_length + a->bytes_in_last_block - 1) /
499 a->bytes_in_last_block);
500 if (target_block_length > a->bytes_per_block)
501 target_block_length = a->bytes_per_block;
502 if (block_length < target_block_length) {
503 memset(state->next, 0,
504 target_block_length - block_length);
505 block_length = target_block_length;
507 bytes_written = (a->client_writer)(&a->archive,
508 a->client_data, state->buffer, block_length);
509 ret = bytes_written <= 0 ? ARCHIVE_FATAL : ARCHIVE_OK;
511 if (a->client_closer)
512 (*a->client_closer)(&a->archive, a->client_data);
516 /* Clear the close handler myself not to be called again. */
517 f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
522 * Open the archive using the current settings.
525 archive_write_open2(struct archive *_a, void *client_data,
526 archive_open_callback *opener, archive_write_callback *writer,
527 archive_close_callback *closer, archive_free_callback *freer)
529 struct archive_write *a = (struct archive_write *)_a;
530 struct archive_write_filter *client_filter;
533 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
534 ARCHIVE_STATE_NEW, "archive_write_open");
535 archive_clear_error(&a->archive);
537 a->client_writer = writer;
538 a->client_opener = opener;
539 a->client_closer = closer;
540 a->client_freer = freer;
541 a->client_data = client_data;
543 client_filter = __archive_write_allocate_filter(_a);
544 client_filter->open = archive_write_client_open;
545 client_filter->write = archive_write_client_write;
546 client_filter->close = archive_write_client_close;
547 client_filter->free = archive_write_client_free;
549 ret = __archive_write_filters_open(a);
550 if (ret < ARCHIVE_WARN) {
551 r1 = __archive_write_filters_close(a);
552 __archive_write_filters_free(_a);
553 return (r1 < ret ? r1 : ret);
556 a->archive.state = ARCHIVE_STATE_HEADER;
558 ret = (a->format_init)(a);
563 archive_write_open(struct archive *_a, void *client_data,
564 archive_open_callback *opener, archive_write_callback *writer,
565 archive_close_callback *closer)
567 return archive_write_open2(_a, client_data, opener, writer,
572 * Close out the archive.
575 _archive_write_close(struct archive *_a)
577 struct archive_write *a = (struct archive_write *)_a;
578 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
580 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
581 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL,
582 "archive_write_close");
583 if (a->archive.state == ARCHIVE_STATE_NEW
584 || a->archive.state == ARCHIVE_STATE_CLOSED)
585 return (ARCHIVE_OK); /* Okay to close() when not open. */
587 archive_clear_error(&a->archive);
589 /* Finish the last entry if a finish callback is specified */
590 if (a->archive.state == ARCHIVE_STATE_DATA
591 && a->format_finish_entry != NULL)
592 r = ((a->format_finish_entry)(a));
594 /* Finish off the archive. */
595 /* TODO: have format closers invoke compression close. */
596 if (a->format_close != NULL) {
597 r1 = (a->format_close)(a);
602 /* Finish the compression and close the stream. */
603 r1 = __archive_write_filters_close(a);
607 if (a->archive.state != ARCHIVE_STATE_FATAL)
608 a->archive.state = ARCHIVE_STATE_CLOSED;
613 _archive_write_filter_count(struct archive *_a)
615 struct archive_write *a = (struct archive_write *)_a;
616 struct archive_write_filter *p = a->filter_first;
626 __archive_write_filters_free(struct archive *_a)
628 struct archive_write *a = (struct archive_write *)_a;
629 int r = ARCHIVE_OK, r1;
631 while (a->filter_first != NULL) {
632 struct archive_write_filter *next
633 = a->filter_first->next_filter;
634 if (a->filter_first->free != NULL) {
635 r1 = (*a->filter_first->free)(a->filter_first);
639 free(a->filter_first);
640 a->filter_first = next;
642 a->filter_last = NULL;
646 * Destroy the archive structure.
648 * Be careful: user might just call write_new and then write_free.
649 * Don't assume we actually wrote anything or performed any non-trivial
653 _archive_write_free(struct archive *_a)
655 struct archive_write *a = (struct archive_write *)_a;
656 int r = ARCHIVE_OK, r1;
660 /* It is okay to call free() in state FATAL. */
661 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
662 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free");
663 if (a->archive.state != ARCHIVE_STATE_FATAL)
664 r = archive_write_close(&a->archive);
666 /* Release format resources. */
667 if (a->format_free != NULL) {
668 r1 = (a->format_free)(a);
673 __archive_write_filters_free(_a);
675 /* Release various dynamic buffers. */
676 free((void *)(uintptr_t)(const void *)a->nulls);
677 archive_string_free(&a->archive.error_string);
678 if (a->passphrase != NULL) {
679 /* A passphrase should be cleaned. */
680 memset(a->passphrase, 0, strlen(a->passphrase));
683 a->archive.magic = 0;
684 __archive_clean(&a->archive);
690 * Write the appropriate header.
693 _archive_write_header(struct archive *_a, struct archive_entry *entry)
695 struct archive_write *a = (struct archive_write *)_a;
698 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
699 ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
700 archive_clear_error(&a->archive);
702 if (a->format_write_header == NULL) {
703 archive_set_error(&(a->archive), -1,
704 "Format must be set before you can write to an archive.");
705 a->archive.state = ARCHIVE_STATE_FATAL;
706 return (ARCHIVE_FATAL);
709 /* In particular, "retry" and "fatal" get returned immediately. */
710 ret = archive_write_finish_entry(&a->archive);
711 if (ret == ARCHIVE_FATAL) {
712 a->archive.state = ARCHIVE_STATE_FATAL;
713 return (ARCHIVE_FATAL);
715 if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
718 if (a->skip_file_set &&
719 archive_entry_dev_is_set(entry) &&
720 archive_entry_ino_is_set(entry) &&
721 archive_entry_dev(entry) == (dev_t)a->skip_file_dev &&
722 archive_entry_ino64(entry) == a->skip_file_ino) {
723 archive_set_error(&a->archive, 0,
724 "Can't add archive to itself");
725 return (ARCHIVE_FAILED);
728 /* Format and write header. */
729 r2 = ((a->format_write_header)(a, entry));
730 if (r2 == ARCHIVE_FAILED) {
731 return (ARCHIVE_FAILED);
733 if (r2 == ARCHIVE_FATAL) {
734 a->archive.state = ARCHIVE_STATE_FATAL;
735 return (ARCHIVE_FATAL);
740 a->archive.state = ARCHIVE_STATE_DATA;
745 _archive_write_finish_entry(struct archive *_a)
747 struct archive_write *a = (struct archive_write *)_a;
748 int ret = ARCHIVE_OK;
750 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
751 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
752 "archive_write_finish_entry");
753 if (a->archive.state & ARCHIVE_STATE_DATA
754 && a->format_finish_entry != NULL)
755 ret = (a->format_finish_entry)(a);
756 a->archive.state = ARCHIVE_STATE_HEADER;
761 * Note that the compressor is responsible for blocking.
764 _archive_write_data(struct archive *_a, const void *buff, size_t s)
766 struct archive_write *a = (struct archive_write *)_a;
767 const size_t max_write = INT_MAX;
769 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
770 ARCHIVE_STATE_DATA, "archive_write_data");
771 /* In particular, this catches attempts to pass negative values. */
774 archive_clear_error(&a->archive);
775 return ((a->format_write_data)(a, buff, s));
778 static struct archive_write_filter *
779 filter_lookup(struct archive *_a, int n)
781 struct archive_write *a = (struct archive_write *)_a;
782 struct archive_write_filter *f = a->filter_first;
784 return a->filter_last;
787 while (n > 0 && f != NULL) {
795 _archive_filter_code(struct archive *_a, int n)
797 struct archive_write_filter *f = filter_lookup(_a, n);
798 return f == NULL ? -1 : f->code;
802 _archive_filter_name(struct archive *_a, int n)
804 struct archive_write_filter *f = filter_lookup(_a, n);
805 return f != NULL ? f->name : NULL;
809 _archive_filter_bytes(struct archive *_a, int n)
811 struct archive_write_filter *f = filter_lookup(_a, n);
812 return f == NULL ? -1 : f->bytes_written;