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