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