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