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 
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write.c 201099 2009-12-28 03:03:00Z kientzle $");
28 
29 /*
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.
35  */
36 
37 #ifdef HAVE_SYS_WAIT_H
38 #include <sys/wait.h>
39 #endif
40 #ifdef HAVE_ERRNO_H
41 #include <errno.h>
42 #endif
43 #ifdef HAVE_LIMITS_H
44 #include <limits.h>
45 #endif
46 #include <stdio.h>
47 #ifdef HAVE_STDLIB_H
48 #include <stdlib.h>
49 #endif
50 #ifdef HAVE_STRING_H
51 #include <string.h>
52 #endif
53 #include <time.h>
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 
58 #include "archive.h"
59 #include "archive_entry.h"
60 #include "archive_private.h"
61 #include "archive_write_private.h"
62 
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);
72 
73 struct archive_none {
74 	size_t buffer_size;
75 	size_t avail;
76 	char *buffer;
77 	char *next;
78 };
79 
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,
91 };
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 
102 	a = (struct archive_write *)calloc(1, sizeof(*a));
103 	if (a == NULL)
104 		return (NULL);
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 
116 	/* Initialize a block of nulls for padding purposes. */
117 	a->null_length = 1024;
118 	nulls = (unsigned char *)calloc(1, a->null_length);
119 	if (nulls == NULL) {
120 		free(a);
121 		return (NULL);
122 	}
123 	a->nulls = nulls;
124 	return (&a->archive);
125 }
126 
127 /*
128  * Set the block size.  Returns 0 if successful.
129  */
130 int
131 archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
132 {
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;
137 	return (ARCHIVE_OK);
138 }
139 
140 /*
141  * Get the current block size.  -1 if it has never been set.
142  */
143 int
144 archive_write_get_bytes_per_block(struct archive *_a)
145 {
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);
150 }
151 
152 /*
153  * Set the size for the last block.
154  * Returns 0 if successful.
155  */
156 int
157 archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
158 {
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;
163 	return (ARCHIVE_OK);
164 }
165 
166 /*
167  * Return the value set above.  -1 indicates it has not been set.
168  */
169 int
170 archive_write_get_bytes_in_last_block(struct archive *_a)
171 {
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);
176 }
177 
178 /*
179  * dev/ino of a file to be rejected.  Used to prevent adding
180  * an archive to itself recursively.
181  */
182 int
183 archive_write_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
184 {
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;
191 	return (ARCHIVE_OK);
192 }
193 
194 /*
195  * Allocate and return the next filter structure.
196  */
197 struct archive_write_filter *
198 __archive_write_allocate_filter(struct archive *_a)
199 {
200 	struct archive_write *a = (struct archive_write *)_a;
201 	struct archive_write_filter *f;
202 
203 	f = calloc(1, sizeof(*f));
204 	f->archive = _a;
205 	f->state = ARCHIVE_WRITE_FILTER_STATE_NEW;
206 	if (a->filter_first == NULL)
207 		a->filter_first = f;
208 	else
209 		a->filter_last->next_filter = f;
210 	a->filter_last = f;
211 	return f;
212 }
213 
214 /*
215  * Write data to a particular filter.
216  */
217 int
218 __archive_write_filter(struct archive_write_filter *f,
219     const void *buff, size_t length)
220 {
221 	int r;
222 	/* Never write to non-open filters */
223 	if (f->state != ARCHIVE_WRITE_FILTER_STATE_OPEN)
224 		return(ARCHIVE_FATAL);
225 	if (length == 0)
226 		return(ARCHIVE_OK);
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;
233 	return (r);
234 }
235 
236 /*
237  * Recursive function for opening the filter chain
238  * Last filter is opened first
239  */
240 static int
241 __archive_write_open_filter(struct archive_write_filter *f)
242 {
243 	int ret;
244 
245 	ret = ARCHIVE_OK;
246 	if (f->next_filter != NULL)
247 		ret = __archive_write_open_filter(f->next_filter);
248 	if (ret != ARCHIVE_OK)
249 		return (ret);
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;
254 		return (ARCHIVE_OK);
255 	}
256 	ret = (f->open)(f);
257 	if (ret == ARCHIVE_OK)
258 		f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
259 	else
260 		f->state = ARCHIVE_WRITE_FILTER_STATE_FATAL;
261 	return (ret);
262 }
263 
264 /*
265  * Open all filters
266  */
267 static int
268 __archive_write_filters_open(struct archive_write *a)
269 {
270 	return (__archive_write_open_filter(a->filter_first));
271 }
272 
273 /*
274  * Close all filtes
275  */
276 static int
277 __archive_write_filters_close(struct archive_write *a)
278 {
279 	struct archive_write_filter *f;
280 	int ret, ret1;
281 	ret = ARCHIVE_OK;
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);
287 				if (ret1 < ret)
288 					ret = ret1;
289 				if (ret1 == ARCHIVE_OK) {
290 					f->state =
291 					    ARCHIVE_WRITE_FILTER_STATE_CLOSED;
292 				} else {
293 					f->state =
294 					    ARCHIVE_WRITE_FILTER_STATE_FATAL;
295 				}
296 			} else
297 				f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
298 		}
299 	}
300 	return (ret);
301 }
302 
303 int
304 __archive_write_output(struct archive_write *a, const void *buff, size_t length)
305 {
306 	return (__archive_write_filter(a->filter_first, buff, length));
307 }
308 
309 int
310 __archive_write_nulls(struct archive_write *a, size_t length)
311 {
312 	if (length == 0)
313 		return (ARCHIVE_OK);
314 
315 	while (length > 0) {
316 		size_t to_write = length < a->null_length ? length : a->null_length;
317 		int r = __archive_write_output(a, a->nulls, to_write);
318 		if (r < ARCHIVE_OK)
319 			return (r);
320 		length -= to_write;
321 	}
322 	return (ARCHIVE_OK);
323 }
324 
325 static int
326 archive_write_client_open(struct archive_write_filter *f)
327 {
328 	struct archive_write *a = (struct archive_write *)f->archive;
329 	struct archive_none *state;
330 	void *buffer;
331 	size_t buffer_size;
332 	int ret;
333 
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;
338 
339 	state = (struct archive_none *)calloc(1, sizeof(*state));
340 	buffer = (char *)malloc(buffer_size);
341 	if (state == NULL || buffer == NULL) {
342 		free(state);
343 		free(buffer);
344 		archive_set_error(f->archive, ENOMEM,
345 		    "Can't allocate data for output buffering");
346 		return (ARCHIVE_FATAL);
347 	}
348 
349 	state->buffer_size = buffer_size;
350 	state->buffer = buffer;
351 	state->next = state->buffer;
352 	state->avail = state->buffer_size;
353 	f->data = state;
354 
355 	if (a->client_opener == NULL)
356 		return (ARCHIVE_OK);
357 	ret = a->client_opener(f->archive, a->client_data);
358 	if (ret != ARCHIVE_OK) {
359 		free(state->buffer);
360 		free(state);
361 		f->data = NULL;
362 	}
363 	return (ret);
364 }
365 
366 static int
367 archive_write_client_write(struct archive_write_filter *f,
368     const void *_buff, size_t length)
369 {
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;
375 
376 	remaining = length;
377 
378 	/*
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.
383 	 */
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;
392 		}
393 		return (ARCHIVE_OK);
394 	}
395 
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;
405 		buff += 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);
420 				}
421 				p += bytes_written;
422 				to_write -= bytes_written;
423 			}
424 			state->next = state->buffer;
425 			state->avail = state->buffer_size;
426 		}
427 	}
428 
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;
437 	}
438 
439 	if (remaining > 0) {
440 		/* Copy last bit into copy buffer. */
441 		memcpy(state->next, buff, remaining);
442 		state->next += remaining;
443 		state->avail -= remaining;
444 	}
445 	return (ARCHIVE_OK);
446 }
447 
448 static int
449 archive_write_client_free(struct archive_write_filter *f)
450 {
451 	struct archive_write *a = (struct archive_write *)f->archive;
452 
453 	if (a->client_freer)
454 		(*a->client_freer)(&a->archive, a->client_data);
455 	a->client_data = NULL;
456 
457 	/* Clear passphrase. */
458 	if (a->passphrase != NULL) {
459 		memset(a->passphrase, 0, strlen(a->passphrase));
460 		free(a->passphrase);
461 		a->passphrase = NULL;
462 	}
463 
464 	return (ARCHIVE_OK);
465 }
466 
467 static int
468 archive_write_client_close(struct archive_write_filter *f)
469 {
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;
475 	size_t to_write;
476 	char *p;
477 	int ret = ARCHIVE_OK;
478 
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;
482 
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;
487 		else
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;
498 		}
499 		p = state->buffer;
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) {
505 				ret = ARCHIVE_FATAL;
506 				break;
507 			}
508 			if ((size_t)bytes_written > to_write) {
509 				archive_set_error(&(a->archive),
510 						  -1, "write overrun");
511 				ret = ARCHIVE_FATAL;
512 				break;
513 			}
514 			p += bytes_written;
515 			to_write -= bytes_written;
516 		}
517 	}
518 	if (a->client_closer)
519 		(*a->client_closer)(&a->archive, a->client_data);
520 	free(state->buffer);
521 	free(state);
522 
523 	/* Clear the close handler myself not to be called again. */
524 	f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
525 	return (ret);
526 }
527 
528 /*
529  * Open the archive using the current settings.
530  */
531 int
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)
535 {
536 	struct archive_write *a = (struct archive_write *)_a;
537 	struct archive_write_filter *client_filter;
538 	int ret, r1;
539 
540 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
541 	    ARCHIVE_STATE_NEW, "archive_write_open");
542 	archive_clear_error(&a->archive);
543 
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;
549 
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;
555 
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);
561 	}
562 
563 	a->archive.state = ARCHIVE_STATE_HEADER;
564 	if (a->format_init)
565 		ret = (a->format_init)(a);
566 	return (ret);
567 }
568 
569 int
570 archive_write_open(struct archive *_a, void *client_data,
571     archive_open_callback *opener, archive_write_callback *writer,
572     archive_close_callback *closer)
573 {
574 	return archive_write_open2(_a, client_data, opener, writer,
575 	    closer, NULL);
576 }
577 
578 /*
579  * Close out the archive.
580  */
581 static int
582 _archive_write_close(struct archive *_a)
583 {
584 	struct archive_write *a = (struct archive_write *)_a;
585 	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
586 
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. */
593 
594 	archive_clear_error(&a->archive);
595 
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));
600 
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);
605 		if (r1 < r)
606 			r = r1;
607 	}
608 
609 	/* Finish the compression and close the stream. */
610 	r1 = __archive_write_filters_close(a);
611 	if (r1 < r)
612 		r = r1;
613 
614 	if (a->archive.state != ARCHIVE_STATE_FATAL)
615 		a->archive.state = ARCHIVE_STATE_CLOSED;
616 	return (r);
617 }
618 
619 static int
620 _archive_write_filter_count(struct archive *_a)
621 {
622 	struct archive_write *a = (struct archive_write *)_a;
623 	struct archive_write_filter *p = a->filter_first;
624 	int count = 0;
625 	while(p) {
626 		count++;
627 		p = p->next_filter;
628 	}
629 	return count;
630 }
631 
632 void
633 __archive_write_filters_free(struct archive *_a)
634 {
635 	struct archive_write *a = (struct archive_write *)_a;
636 	int r = ARCHIVE_OK, r1;
637 
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);
643 			if (r > r1)
644 				r = r1;
645 		}
646 		free(a->filter_first);
647 		a->filter_first = next;
648 	}
649 	a->filter_last = NULL;
650 }
651 
652 /*
653  * Destroy the archive structure.
654  *
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
657  * initialization.
658  */
659 static int
660 _archive_write_free(struct archive *_a)
661 {
662 	struct archive_write *a = (struct archive_write *)_a;
663 	int r = ARCHIVE_OK, r1;
664 
665 	if (_a == NULL)
666 		return (ARCHIVE_OK);
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);
672 
673 	/* Release format resources. */
674 	if (a->format_free != NULL) {
675 		r1 = (a->format_free)(a);
676 		if (r1 < r)
677 			r = r1;
678 	}
679 
680 	__archive_write_filters_free(_a);
681 
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));
688 		free(a->passphrase);
689 	}
690 	a->archive.magic = 0;
691 	__archive_clean(&a->archive);
692 	free(a);
693 	return (r);
694 }
695 
696 /*
697  * Write the appropriate header.
698  */
699 static int
700 _archive_write_header(struct archive *_a, struct archive_entry *entry)
701 {
702 	struct archive_write *a = (struct archive_write *)_a;
703 	int ret, r2;
704 
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);
708 
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);
714 	}
715 
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);
721 	}
722 	if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
723 		return (ret);
724 
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);
733 	}
734 
735 	/* Format and write header. */
736 	r2 = ((a->format_write_header)(a, entry));
737 	if (r2 == ARCHIVE_FAILED) {
738 		return (ARCHIVE_FAILED);
739 	}
740 	if (r2 == ARCHIVE_FATAL) {
741 		a->archive.state = ARCHIVE_STATE_FATAL;
742 		return (ARCHIVE_FATAL);
743 	}
744 	if (r2 < ret)
745 		ret = r2;
746 
747 	a->archive.state = ARCHIVE_STATE_DATA;
748 	return (ret);
749 }
750 
751 static int
752 _archive_write_finish_entry(struct archive *_a)
753 {
754 	struct archive_write *a = (struct archive_write *)_a;
755 	int ret = ARCHIVE_OK;
756 
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;
764 	return (ret);
765 }
766 
767 /*
768  * Note that the compressor is responsible for blocking.
769  */
770 static ssize_t
771 _archive_write_data(struct archive *_a, const void *buff, size_t s)
772 {
773 	struct archive_write *a = (struct archive_write *)_a;
774 	const size_t max_write = INT_MAX;
775 
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. */
779 	if (s > max_write)
780 		s = max_write;
781 	archive_clear_error(&a->archive);
782 	return ((a->format_write_data)(a, buff, s));
783 }
784 
785 static struct archive_write_filter *
786 filter_lookup(struct archive *_a, int n)
787 {
788 	struct archive_write *a = (struct archive_write *)_a;
789 	struct archive_write_filter *f = a->filter_first;
790 	if (n == -1)
791 		return a->filter_last;
792 	if (n < 0)
793 		return NULL;
794 	while (n > 0 && f != NULL) {
795 		f = f->next_filter;
796 		--n;
797 	}
798 	return f;
799 }
800 
801 static int
802 _archive_filter_code(struct archive *_a, int n)
803 {
804 	struct archive_write_filter *f = filter_lookup(_a, n);
805 	return f == NULL ? -1 : f->code;
806 }
807 
808 static const char *
809 _archive_filter_name(struct archive *_a, int n)
810 {
811 	struct archive_write_filter *f = filter_lookup(_a, n);
812 	return f != NULL ? f->name : NULL;
813 }
814 
815 static int64_t
816 _archive_filter_bytes(struct archive *_a, int n)
817 {
818 	struct archive_write_filter *f = filter_lookup(_a, n);
819 	return f == NULL ? -1 : f->bytes_written;
820 }
821