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