xref: /freebsd/contrib/libarchive/libarchive/archive_write.c (revision 401026e4825a05abba6f945cf1b74b3328876fa2)
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 
28 /*
29  * This file contains the "essential" portions of the write API, that
30  * is, stuff that will essentially always be used by any client that
31  * actually needs to write an archive.  Optional pieces have been, as
32  * far as possible, separated out into separate files to reduce
33  * needlessly bloating statically-linked clients.
34  */
35 
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h>
44 #endif
45 #include <stdio.h>
46 #ifdef HAVE_STDLIB_H
47 #include <stdlib.h>
48 #endif
49 #ifdef HAVE_STRING_H
50 #include <string.h>
51 #endif
52 #include <time.h>
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56 
57 #include "archive.h"
58 #include "archive_entry.h"
59 #include "archive_private.h"
60 #include "archive_write_private.h"
61 
62 static int	_archive_filter_code(struct archive *, int);
63 static const char *_archive_filter_name(struct archive *, int);
64 static int64_t	_archive_filter_bytes(struct archive *, int);
65 static int  _archive_write_filter_count(struct archive *);
66 static int	_archive_write_close(struct archive *);
67 static int	_archive_write_free(struct archive *);
68 static int	_archive_write_header(struct archive *, struct archive_entry *);
69 static int	_archive_write_finish_entry(struct archive *);
70 static ssize_t	_archive_write_data(struct archive *, const void *, size_t);
71 
72 struct archive_none {
73 	size_t buffer_size;
74 	size_t avail;
75 	char *buffer;
76 	char *next;
77 };
78 
79 static const struct archive_vtable
80 archive_write_vtable = {
81 	.archive_close = _archive_write_close,
82 	.archive_filter_bytes = _archive_filter_bytes,
83 	.archive_filter_code = _archive_filter_code,
84 	.archive_filter_name = _archive_filter_name,
85 	.archive_filter_count = _archive_write_filter_count,
86 	.archive_free = _archive_write_free,
87 	.archive_write_header = _archive_write_header,
88 	.archive_write_finish_entry = _archive_write_finish_entry,
89 	.archive_write_data = _archive_write_data,
90 };
91 
92 /*
93  * Allocate, initialize and return an archive object.
94  */
95 struct archive *
archive_write_new(void)96 archive_write_new(void)
97 {
98 	struct archive_write *a;
99 	unsigned char *nulls;
100 
101 	a = calloc(1, sizeof(*a));
102 	if (a == NULL)
103 		return (NULL);
104 	a->archive.magic = ARCHIVE_WRITE_MAGIC;
105 	a->archive.state = ARCHIVE_STATE_NEW;
106 	a->archive.vtable = &archive_write_vtable;
107 	/*
108 	 * The value 10240 here matches the traditional tar default,
109 	 * but is otherwise arbitrary.
110 	 * TODO: Set the default block size from the format selected.
111 	 */
112 	a->bytes_per_block = 10240;
113 	a->bytes_in_last_block = -1;	/* Default */
114 
115 	/* Initialize a block of nulls for padding purposes. */
116 	a->null_length = 1024;
117 	nulls = calloc(a->null_length, sizeof(unsigned char));
118 	if (nulls == NULL) {
119 		free(a);
120 		return (NULL);
121 	}
122 	a->nulls = nulls;
123 	return (&a->archive);
124 }
125 
126 /*
127  * Set the block size.  Returns 0 if successful.
128  */
129 int
archive_write_set_bytes_per_block(struct archive * _a,int bytes_per_block)130 archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
131 {
132 	struct archive_write *a = (struct archive_write *)_a;
133 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
134 	    ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
135 
136 	if (bytes_per_block < 0) {
137 		// Do nothing if the bytes_per_block is negative
138 		return 0;
139 	}
140 	a->bytes_per_block = bytes_per_block;
141 	return (ARCHIVE_OK);
142 }
143 
144 /*
145  * Get the current block size.
146  */
147 int
archive_write_get_bytes_per_block(struct archive * _a)148 archive_write_get_bytes_per_block(struct archive *_a)
149 {
150 	struct archive_write *a = (struct archive_write *)_a;
151 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
152 	    ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
153 	if (a->bytes_per_block < 0) {
154 		// Don't return a negative value
155 		return 1;
156 	}
157 	return (a->bytes_per_block);
158 }
159 
160 /*
161  * Set the size for the last block.
162  * Returns 0 if successful.
163  */
164 int
archive_write_set_bytes_in_last_block(struct archive * _a,int bytes)165 archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
166 {
167 	struct archive_write *a = (struct archive_write *)_a;
168 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
169 	    ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
170 	a->bytes_in_last_block = bytes;
171 	return (ARCHIVE_OK);
172 }
173 
174 /*
175  * Return the value set above.  -1 indicates it has not been set.
176  */
177 int
archive_write_get_bytes_in_last_block(struct archive * _a)178 archive_write_get_bytes_in_last_block(struct archive *_a)
179 {
180 	struct archive_write *a = (struct archive_write *)_a;
181 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
182 	    ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
183 	return (a->bytes_in_last_block);
184 }
185 
186 /*
187  * dev/ino of a file to be rejected.  Used to prevent adding
188  * an archive to itself recursively.
189  */
190 int
archive_write_set_skip_file(struct archive * _a,la_int64_t d,la_int64_t i)191 archive_write_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
192 {
193 	struct archive_write *a = (struct archive_write *)_a;
194 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
195 	    ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
196 	a->skip_file_set = 1;
197 	a->skip_file_dev = d;
198 	a->skip_file_ino = i;
199 	return (ARCHIVE_OK);
200 }
201 
202 /*
203  * Allocate and return the next filter structure.
204  */
205 struct archive_write_filter *
__archive_write_allocate_filter(struct archive * _a)206 __archive_write_allocate_filter(struct archive *_a)
207 {
208 	struct archive_write *a = (struct archive_write *)_a;
209 	struct archive_write_filter *f;
210 
211 	f = calloc(1, sizeof(*f));
212 
213 	if (f == NULL)
214 		return (NULL);
215 
216 	f->archive = _a;
217 	f->state = ARCHIVE_WRITE_FILTER_STATE_NEW;
218 	if (a->filter_first == NULL)
219 		a->filter_first = f;
220 	else
221 		a->filter_last->next_filter = f;
222 	a->filter_last = f;
223 	return f;
224 }
225 
226 /*
227  * Write data to a particular filter.
228  */
229 int
__archive_write_filter(struct archive_write_filter * f,const void * buff,size_t length)230 __archive_write_filter(struct archive_write_filter *f,
231     const void *buff, size_t length)
232 {
233 	int r;
234 	/* Never write to non-open filters */
235 	if (f->state != ARCHIVE_WRITE_FILTER_STATE_OPEN)
236 		return(ARCHIVE_FATAL);
237 	if (length == 0)
238 		return(ARCHIVE_OK);
239 	if (f->write == NULL)
240 		/* If unset, a fatal error has already occurred, so this filter
241 		 * didn't open. We cannot write anything. */
242 		return(ARCHIVE_FATAL);
243 	r = (f->write)(f, buff, length);
244 	f->bytes_written += length;
245 	return (r);
246 }
247 
248 /*
249  * Recursive function for opening the filter chain
250  * Last filter is opened first
251  */
252 static int
__archive_write_open_filter(struct archive_write_filter * f)253 __archive_write_open_filter(struct archive_write_filter *f)
254 {
255 	int ret;
256 
257 	ret = ARCHIVE_OK;
258 	if (f->next_filter != NULL)
259 		ret = __archive_write_open_filter(f->next_filter);
260 	if (ret != ARCHIVE_OK)
261 		return (ret);
262 	if (f->state != ARCHIVE_WRITE_FILTER_STATE_NEW)
263 		return (ARCHIVE_FATAL);
264 	if (f->open == NULL) {
265 		f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
266 		return (ARCHIVE_OK);
267 	}
268 	ret = (f->open)(f);
269 	if (ret == ARCHIVE_OK)
270 		f->state = ARCHIVE_WRITE_FILTER_STATE_OPEN;
271 	else
272 		f->state = ARCHIVE_WRITE_FILTER_STATE_FATAL;
273 	return (ret);
274 }
275 
276 /*
277  * Open all filters
278  */
279 static int
__archive_write_filters_open(struct archive_write * a)280 __archive_write_filters_open(struct archive_write *a)
281 {
282 	return (__archive_write_open_filter(a->filter_first));
283 }
284 
285 /*
286  * Close all filters
287  */
288 static int
__archive_write_filters_close(struct archive_write * a)289 __archive_write_filters_close(struct archive_write *a)
290 {
291 	struct archive_write_filter *f;
292 	int ret, ret1;
293 	ret = ARCHIVE_OK;
294 	for (f = a->filter_first; f != NULL; f = f->next_filter) {
295 		/* Do not close filters that are not open */
296 		if (f->state == ARCHIVE_WRITE_FILTER_STATE_OPEN) {
297 			if (f->close != NULL) {
298 				ret1 = (f->close)(f);
299 				if (ret1 < ret)
300 					ret = ret1;
301 				if (ret1 == ARCHIVE_OK) {
302 					f->state =
303 					    ARCHIVE_WRITE_FILTER_STATE_CLOSED;
304 				} else {
305 					f->state =
306 					    ARCHIVE_WRITE_FILTER_STATE_FATAL;
307 				}
308 			} else
309 				f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
310 		}
311 	}
312 	return (ret);
313 }
314 
315 int
__archive_write_output(struct archive_write * a,const void * buff,size_t length)316 __archive_write_output(struct archive_write *a, const void *buff, size_t length)
317 {
318 	return (__archive_write_filter(a->filter_first, buff, length));
319 }
320 
321 static int
__archive_write_filters_flush(struct archive_write * a)322 __archive_write_filters_flush(struct archive_write *a)
323 {
324 	struct archive_write_filter *f;
325 	int ret, ret1;
326 
327 	ret = ARCHIVE_OK;
328 	for (f = a->filter_first; f != NULL; f = f->next_filter) {
329 		if (f->flush != NULL && f->bytes_written > 0) {
330 			ret1 = (f->flush)(f);
331 			if (ret1 < ret)
332 				ret = ret1;
333 			if (ret1 < ARCHIVE_WARN)
334 				f->state = ARCHIVE_WRITE_FILTER_STATE_FATAL;
335 		}
336 	}
337 	return (ret);
338 }
339 
340 int
__archive_write_nulls(struct archive_write * a,size_t length)341 __archive_write_nulls(struct archive_write *a, size_t length)
342 {
343 	if (length == 0)
344 		return (ARCHIVE_OK);
345 
346 	while (length > 0) {
347 		size_t to_write = length < a->null_length ? length : a->null_length;
348 		int r = __archive_write_output(a, a->nulls, to_write);
349 		if (r < ARCHIVE_OK)
350 			return (r);
351 		length -= to_write;
352 	}
353 	return (ARCHIVE_OK);
354 }
355 
356 static int
archive_write_client_open(struct archive_write_filter * f)357 archive_write_client_open(struct archive_write_filter *f)
358 {
359 	struct archive_write *a = (struct archive_write *)f->archive;
360 	struct archive_none *state;
361 	void *buffer;
362 	size_t buffer_size;
363 
364 	f->bytes_per_block = archive_write_get_bytes_per_block(f->archive);
365 	f->bytes_in_last_block =
366 	    archive_write_get_bytes_in_last_block(f->archive);
367 	buffer_size = f->bytes_per_block;
368 
369 	state = calloc(1, sizeof(*state));
370 	buffer = malloc(buffer_size);
371 	if (state == NULL || buffer == NULL) {
372 		free(state);
373 		free(buffer);
374 		archive_set_error(f->archive, ENOMEM,
375 		    "Can't allocate data for output buffering");
376 		return (ARCHIVE_FATAL);
377 	}
378 
379 	state->buffer_size = buffer_size;
380 	state->buffer = buffer;
381 	state->next = state->buffer;
382 	state->avail = state->buffer_size;
383 	f->data = state;
384 
385 	if (a->client_opener == NULL)
386 		return (ARCHIVE_OK);
387 	return (a->client_opener(f->archive, a->client_data));
388 }
389 
390 static int
archive_write_client_write(struct archive_write_filter * f,const void * _buff,size_t length)391 archive_write_client_write(struct archive_write_filter *f,
392     const void *_buff, size_t length)
393 {
394 	struct archive_write *a = (struct archive_write *)f->archive;
395         struct archive_none *state = (struct archive_none *)f->data;
396 	const char *buff = (const char *)_buff;
397 	ssize_t remaining, to_copy;
398 	ssize_t bytes_written;
399 
400 	remaining = length;
401 
402 	/*
403 	 * If there is no buffer for blocking, just pass the data
404 	 * straight through to the client write callback.  In
405 	 * particular, this supports "no write delay" operation for
406 	 * special applications.  Just set the block size to zero.
407 	 */
408 	if (state->buffer_size == 0) {
409 		while (remaining > 0) {
410 			bytes_written = (a->client_writer)(&a->archive,
411 			    a->client_data, buff, remaining);
412 			if (bytes_written <= 0)
413 				return (ARCHIVE_FATAL);
414 			remaining -= bytes_written;
415 			buff += bytes_written;
416 		}
417 		return (ARCHIVE_OK);
418 	}
419 
420 	/* If the copy buffer isn't empty, try to fill it. */
421 	if (state->avail < state->buffer_size) {
422 		/* If buffer is not empty... */
423 		/* ... copy data into buffer ... */
424 		to_copy = ((size_t)remaining > state->avail) ?
425 			state->avail : (size_t)remaining;
426 		memcpy(state->next, buff, to_copy);
427 		state->next += to_copy;
428 		state->avail -= to_copy;
429 		buff += to_copy;
430 		remaining -= to_copy;
431 		/* ... if it's full, write it out. */
432 		if (state->avail == 0) {
433 			char *p = state->buffer;
434 			size_t to_write = state->buffer_size;
435 			while (to_write > 0) {
436 				bytes_written = (a->client_writer)(&a->archive,
437 				    a->client_data, p, to_write);
438 				if (bytes_written <= 0)
439 					return (ARCHIVE_FATAL);
440 				if ((size_t)bytes_written > to_write) {
441 					archive_set_error(&(a->archive),
442 					    -1, "write overrun");
443 					return (ARCHIVE_FATAL);
444 				}
445 				p += bytes_written;
446 				to_write -= bytes_written;
447 			}
448 			state->next = state->buffer;
449 			state->avail = state->buffer_size;
450 		}
451 	}
452 
453 	while ((size_t)remaining >= state->buffer_size) {
454 		/* Write out full blocks directly to client. */
455 		bytes_written = (a->client_writer)(&a->archive,
456 		    a->client_data, buff, state->buffer_size);
457 		if (bytes_written <= 0)
458 			return (ARCHIVE_FATAL);
459 		buff += bytes_written;
460 		remaining -= bytes_written;
461 	}
462 
463 	if (remaining > 0) {
464 		/* Copy last bit into copy buffer. */
465 		memcpy(state->next, buff, remaining);
466 		state->next += remaining;
467 		state->avail -= remaining;
468 	}
469 	return (ARCHIVE_OK);
470 }
471 
472 static int
archive_write_client_free(struct archive_write_filter * f)473 archive_write_client_free(struct archive_write_filter *f)
474 {
475 	struct archive_write *a = (struct archive_write *)f->archive;
476 	struct archive_none *state = (struct archive_none *)f->data;
477 
478 	if (a->client_freer)
479 		(*a->client_freer)(&a->archive, a->client_data);
480 	a->client_data = NULL;
481 
482 	/* Clear passphrase. */
483 	if (a->passphrase != NULL) {
484 		memset(a->passphrase, 0, strlen(a->passphrase));
485 		free(a->passphrase);
486 		a->passphrase = NULL;
487 	}
488 
489 	/* Free state. */
490 	if (state != NULL) {
491 		free(state->buffer);
492 		free(state);
493 		f->data = NULL;
494 	}
495 
496 	return (ARCHIVE_OK);
497 }
498 
499 static int
archive_write_client_close(struct archive_write_filter * f)500 archive_write_client_close(struct archive_write_filter *f)
501 {
502 	struct archive_write *a = (struct archive_write *)f->archive;
503 	struct archive_none *state = (struct archive_none *)f->data;
504 	ssize_t block_length;
505 	ssize_t target_block_length;
506 	ssize_t bytes_written;
507 	size_t to_write;
508 	char *p;
509 	int ret = ARCHIVE_OK;
510 
511 	/* If there's pending data, pad and write the last block */
512 	if (state->next != state->buffer) {
513 		block_length = state->buffer_size - state->avail;
514 
515 		/* Tricky calculation to determine size of last block */
516 		if (a->bytes_in_last_block <= 0)
517 			/* Default or Zero: pad to full block */
518 			target_block_length = a->bytes_per_block;
519 		else
520 			/* Round to next multiple of bytes_in_last_block. */
521 			target_block_length = a->bytes_in_last_block *
522 			    ( (block_length + a->bytes_in_last_block - 1) /
523 			        a->bytes_in_last_block);
524 		if (target_block_length > a->bytes_per_block)
525 			target_block_length = a->bytes_per_block;
526 		if (block_length < target_block_length) {
527 			memset(state->next, 0,
528 			    target_block_length - block_length);
529 			block_length = target_block_length;
530 		}
531 		p = state->buffer;
532 		to_write = block_length;
533 		while (to_write > 0) {
534 			bytes_written = (a->client_writer)(&a->archive,
535 			    a->client_data, p, to_write);
536 			if (bytes_written <= 0) {
537 				ret = ARCHIVE_FATAL;
538 				break;
539 			}
540 			if ((size_t)bytes_written > to_write) {
541 				archive_set_error(&(a->archive),
542 						  -1, "write overrun");
543 				ret = ARCHIVE_FATAL;
544 				break;
545 			}
546 			p += bytes_written;
547 			to_write -= bytes_written;
548 		}
549 	}
550 	if (a->client_closer)
551 		(*a->client_closer)(&a->archive, a->client_data);
552 
553 	/* Clear the close handler myself not to be called again. */
554 	f->state = ARCHIVE_WRITE_FILTER_STATE_CLOSED;
555 	return (ret);
556 }
557 
558 /*
559  * Open the archive using the current settings.
560  */
561 int
archive_write_open2(struct archive * _a,void * client_data,archive_open_callback * opener,archive_write_callback * writer,archive_close_callback * closer,archive_free_callback * freer)562 archive_write_open2(struct archive *_a, void *client_data,
563     archive_open_callback *opener, archive_write_callback *writer,
564     archive_close_callback *closer, archive_free_callback *freer)
565 {
566 	struct archive_write *a = (struct archive_write *)_a;
567 	struct archive_write_filter *client_filter;
568 	int ret, r1;
569 
570 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
571 	    ARCHIVE_STATE_NEW, "archive_write_open");
572 	archive_clear_error(&a->archive);
573 
574 	a->client_writer = writer;
575 	a->client_opener = opener;
576 	a->client_closer = closer;
577 	a->client_freer = freer;
578 	a->client_data = client_data;
579 
580 	client_filter = __archive_write_allocate_filter(_a);
581 
582 	if (client_filter == NULL)
583 		return (ARCHIVE_FATAL);
584 
585 	client_filter->open = archive_write_client_open;
586 	client_filter->write = archive_write_client_write;
587 	client_filter->close = archive_write_client_close;
588 	client_filter->free = archive_write_client_free;
589 
590 	ret = __archive_write_filters_open(a);
591 	if (ret < ARCHIVE_WARN) {
592 		r1 = __archive_write_filters_close(a);
593 		__archive_write_filters_free(_a);
594 		return (r1 < ret ? r1 : ret);
595 	}
596 
597 	a->archive.state = ARCHIVE_STATE_HEADER;
598 	if (a->format_init)
599 		ret = (a->format_init)(a);
600 	return (ret);
601 }
602 
603 int
archive_write_open(struct archive * _a,void * client_data,archive_open_callback * opener,archive_write_callback * writer,archive_close_callback * closer)604 archive_write_open(struct archive *_a, void *client_data,
605     archive_open_callback *opener, archive_write_callback *writer,
606     archive_close_callback *closer)
607 {
608 	return archive_write_open2(_a, client_data, opener, writer,
609 	    closer, NULL);
610 }
611 
612 /*
613  * Close out the archive.
614  */
615 static int
_archive_write_close(struct archive * _a)616 _archive_write_close(struct archive *_a)
617 {
618 	struct archive_write *a = (struct archive_write *)_a;
619 	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
620 
621 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
622 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL,
623 	    "archive_write_close");
624 	if (a->archive.state == ARCHIVE_STATE_NEW
625 	    || a->archive.state == ARCHIVE_STATE_CLOSED)
626 		return (ARCHIVE_OK); /* Okay to close() when not open. */
627 
628 	archive_clear_error(&a->archive);
629 
630 	/* Finish the last entry if a finish callback is specified */
631 	if (a->archive.state == ARCHIVE_STATE_DATA
632 	    && a->format_finish_entry != NULL)
633 		r = ((a->format_finish_entry)(a));
634 
635 	/* Finish off the archive. */
636 	/* TODO: have format closers invoke compression close. */
637 	if (a->format_close != NULL) {
638 		r1 = (a->format_close)(a);
639 		if (r1 < r)
640 			r = r1;
641 	}
642 
643 	/* Finish the compression and close the stream. */
644 	r1 = __archive_write_filters_close(a);
645 	if (r1 < r)
646 		r = r1;
647 
648 	if (a->archive.state != ARCHIVE_STATE_FATAL)
649 		a->archive.state = ARCHIVE_STATE_CLOSED;
650 	return (r);
651 }
652 
653 static int
_archive_write_filter_count(struct archive * _a)654 _archive_write_filter_count(struct archive *_a)
655 {
656 	struct archive_write *a = (struct archive_write *)_a;
657 	struct archive_write_filter *p = a->filter_first;
658 	int count = 0;
659 	while(p) {
660 		count++;
661 		p = p->next_filter;
662 	}
663 	return count;
664 }
665 
666 void
__archive_write_filters_free(struct archive * _a)667 __archive_write_filters_free(struct archive *_a)
668 {
669 	struct archive_write *a = (struct archive_write *)_a;
670 	int r = ARCHIVE_OK, r1;
671 
672 	while (a->filter_first != NULL) {
673 		struct archive_write_filter *next
674 		    = a->filter_first->next_filter;
675 		if (a->filter_first->free != NULL) {
676 			r1 = (*a->filter_first->free)(a->filter_first);
677 			if (r > r1)
678 				r = r1;
679 		}
680 		free(a->filter_first);
681 		a->filter_first = next;
682 	}
683 	a->filter_last = NULL;
684 }
685 
686 /*
687  * Destroy the archive structure.
688  *
689  * Be careful: user might just call write_new and then write_free.
690  * Don't assume we actually wrote anything or performed any non-trivial
691  * initialization.
692  */
693 static int
_archive_write_free(struct archive * _a)694 _archive_write_free(struct archive *_a)
695 {
696 	struct archive_write *a = (struct archive_write *)_a;
697 	int r = ARCHIVE_OK, r1;
698 
699 	if (_a == NULL)
700 		return (ARCHIVE_OK);
701 	/* It is okay to call free() in state FATAL. */
702 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
703 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free");
704 	if (a->archive.state != ARCHIVE_STATE_FATAL)
705 		r = archive_write_close(&a->archive);
706 
707 	/* Release format resources. */
708 	if (a->format_free != NULL) {
709 		r1 = (a->format_free)(a);
710 		if (r1 < r)
711 			r = r1;
712 	}
713 
714 	__archive_write_filters_free(_a);
715 
716 	/* Release various dynamic buffers. */
717 	free((void *)(uintptr_t)(const void *)a->nulls);
718 	archive_string_free(&a->archive.error_string);
719 	if (a->passphrase != NULL) {
720 		/* A passphrase should be cleaned. */
721 		memset(a->passphrase, 0, strlen(a->passphrase));
722 		free(a->passphrase);
723 	}
724 	a->archive.magic = 0;
725 	__archive_clean(&a->archive);
726 	free(a);
727 	return (r);
728 }
729 
730 /*
731  * Write the appropriate header.
732  */
733 static int
_archive_write_header(struct archive * _a,struct archive_entry * entry)734 _archive_write_header(struct archive *_a, struct archive_entry *entry)
735 {
736 	struct archive_write *a = (struct archive_write *)_a;
737 	int ret, r2;
738 
739 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
740 	    ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
741 	archive_clear_error(&a->archive);
742 
743 	if (a->format_write_header == NULL) {
744 		archive_set_error(&(a->archive), -1,
745 		    "Format must be set before you can write to an archive.");
746 		a->archive.state = ARCHIVE_STATE_FATAL;
747 		return (ARCHIVE_FATAL);
748 	}
749 
750 	/* In particular, "retry" and "fatal" get returned immediately. */
751 	ret = archive_write_finish_entry(&a->archive);
752 	if (ret == ARCHIVE_FATAL) {
753 		a->archive.state = ARCHIVE_STATE_FATAL;
754 		return (ARCHIVE_FATAL);
755 	}
756 	if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
757 		return (ret);
758 
759 	if (a->skip_file_set &&
760 	    archive_entry_dev_is_set(entry) &&
761 	    archive_entry_ino_is_set(entry) &&
762 	    archive_entry_dev(entry) == (dev_t)a->skip_file_dev &&
763 	    archive_entry_ino64(entry) == a->skip_file_ino) {
764 		archive_set_error(&a->archive, 0,
765 		    "Can't add archive to itself");
766 		return (ARCHIVE_FAILED);
767 	}
768 
769 	/* Flush filters at boundary. */
770 	r2 = __archive_write_filters_flush(a);
771 	if (r2 == ARCHIVE_FAILED) {
772 		return (ARCHIVE_FAILED);
773 	}
774 	if (r2 == ARCHIVE_FATAL) {
775 		a->archive.state = ARCHIVE_STATE_FATAL;
776 		return (ARCHIVE_FATAL);
777 	}
778 	if (r2 < ret)
779 		ret = r2;
780 
781 	/* Format and write header. */
782 	r2 = ((a->format_write_header)(a, entry));
783 	if (r2 == ARCHIVE_FAILED) {
784 		return (ARCHIVE_FAILED);
785 	}
786 	if (r2 == ARCHIVE_FATAL) {
787 		a->archive.state = ARCHIVE_STATE_FATAL;
788 		return (ARCHIVE_FATAL);
789 	}
790 	if (r2 < ret)
791 		ret = r2;
792 
793 	a->archive.state = ARCHIVE_STATE_DATA;
794 	return (ret);
795 }
796 
797 static int
_archive_write_finish_entry(struct archive * _a)798 _archive_write_finish_entry(struct archive *_a)
799 {
800 	struct archive_write *a = (struct archive_write *)_a;
801 	int ret = ARCHIVE_OK;
802 
803 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
804 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
805 	    "archive_write_finish_entry");
806 	if (a->archive.state & ARCHIVE_STATE_DATA
807 	    && a->format_finish_entry != NULL)
808 		ret = (a->format_finish_entry)(a);
809 	if (ret == ARCHIVE_FATAL)
810 		a->archive.state = ARCHIVE_STATE_FATAL;
811 	else
812 		a->archive.state = ARCHIVE_STATE_HEADER;
813 	return (ret);
814 }
815 
816 /*
817  * Note that the compressor is responsible for blocking.
818  */
819 static ssize_t
_archive_write_data(struct archive * _a,const void * buff,size_t s)820 _archive_write_data(struct archive *_a, const void *buff, size_t s)
821 {
822 	struct archive_write *a = (struct archive_write *)_a;
823 	const size_t max_write = INT_MAX;
824 	int ret;
825 
826 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
827 	    ARCHIVE_STATE_DATA, "archive_write_data");
828 	/* In particular, this catches attempts to pass negative values. */
829 	if (s > max_write)
830 		s = max_write;
831 	archive_clear_error(&a->archive);
832 	ret = (a->format_write_data)(a, buff, s);
833 	if (ret == ARCHIVE_FATAL)
834 		a->archive.state = ARCHIVE_STATE_FATAL;
835 	return (ret);
836 }
837 
838 static struct archive_write_filter *
filter_lookup(struct archive * _a,int n)839 filter_lookup(struct archive *_a, int n)
840 {
841 	struct archive_write *a = (struct archive_write *)_a;
842 	struct archive_write_filter *f = a->filter_first;
843 	if (n == -1)
844 		return a->filter_last;
845 	if (n < 0)
846 		return NULL;
847 	while (n > 0 && f != NULL) {
848 		f = f->next_filter;
849 		--n;
850 	}
851 	return f;
852 }
853 
854 static int
_archive_filter_code(struct archive * _a,int n)855 _archive_filter_code(struct archive *_a, int n)
856 {
857 	struct archive_write_filter *f = filter_lookup(_a, n);
858 	return f == NULL ? -1 : f->code;
859 }
860 
861 static const char *
_archive_filter_name(struct archive * _a,int n)862 _archive_filter_name(struct archive *_a, int n)
863 {
864 	struct archive_write_filter *f = filter_lookup(_a, n);
865 	return f != NULL ? f->name : NULL;
866 }
867 
868 static int64_t
_archive_filter_bytes(struct archive * _a,int n)869 _archive_filter_bytes(struct archive *_a, int n)
870 {
871 	struct archive_write_filter *f = filter_lookup(_a, n);
872 	return f == NULL ? -1 : f->bytes_written;
873 }
874