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