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 *
archive_write_vtable(void)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 *
archive_write_new(void)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
archive_write_set_bytes_per_block(struct archive * _a,int bytes_per_block)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
archive_write_get_bytes_per_block(struct archive * _a)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
archive_write_set_bytes_in_last_block(struct archive * _a,int bytes)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
archive_write_get_bytes_in_last_block(struct archive * _a)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
archive_write_set_skip_file(struct archive * _a,la_int64_t d,la_int64_t i)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 *
__archive_write_allocate_filter(struct archive * _a)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 if (a->filter_first == NULL)
216 a->filter_first = f;
217 else
218 a->filter_last->next_filter = f;
219 a->filter_last = f;
220 return f;
221 }
222
223 /*
224 * Write data to a particular filter.
225 */
226 int
__archive_write_filter(struct archive_write_filter * f,const void * buff,size_t length)227 __archive_write_filter(struct archive_write_filter *f,
228 const void *buff, size_t length)
229 {
230 int r;
231 if (length == 0)
232 return(ARCHIVE_OK);
233 if (f->write == NULL)
234 /* If unset, a fatal error has already occurred, so this filter
235 * didn't open. We cannot write anything. */
236 return(ARCHIVE_FATAL);
237 r = (f->write)(f, buff, length);
238 f->bytes_written += length;
239 return (r);
240 }
241
242 /*
243 * Open a filter.
244 */
245 int
__archive_write_open_filter(struct archive_write_filter * f)246 __archive_write_open_filter(struct archive_write_filter *f)
247 {
248 if (f->open == NULL)
249 return (ARCHIVE_OK);
250 return (f->open)(f);
251 }
252
253 /*
254 * Close a filter.
255 */
256 int
__archive_write_close_filter(struct archive_write_filter * f)257 __archive_write_close_filter(struct archive_write_filter *f)
258 {
259 if (f->close != NULL)
260 return (f->close)(f);
261 if (f->next_filter != NULL)
262 return (__archive_write_close_filter(f->next_filter));
263 return (ARCHIVE_OK);
264 }
265
266 int
__archive_write_output(struct archive_write * a,const void * buff,size_t length)267 __archive_write_output(struct archive_write *a, const void *buff, size_t length)
268 {
269 return (__archive_write_filter(a->filter_first, buff, length));
270 }
271
272 int
__archive_write_nulls(struct archive_write * a,size_t length)273 __archive_write_nulls(struct archive_write *a, size_t length)
274 {
275 if (length == 0)
276 return (ARCHIVE_OK);
277
278 while (length > 0) {
279 size_t to_write = length < a->null_length ? length : a->null_length;
280 int r = __archive_write_output(a, a->nulls, to_write);
281 if (r < ARCHIVE_OK)
282 return (r);
283 length -= to_write;
284 }
285 return (ARCHIVE_OK);
286 }
287
288 static int
archive_write_client_open(struct archive_write_filter * f)289 archive_write_client_open(struct archive_write_filter *f)
290 {
291 struct archive_write *a = (struct archive_write *)f->archive;
292 struct archive_none *state;
293 void *buffer;
294 size_t buffer_size;
295
296 f->bytes_per_block = archive_write_get_bytes_per_block(f->archive);
297 f->bytes_in_last_block =
298 archive_write_get_bytes_in_last_block(f->archive);
299 buffer_size = f->bytes_per_block;
300
301 state = (struct archive_none *)calloc(1, sizeof(*state));
302 buffer = (char *)malloc(buffer_size);
303 if (state == NULL || buffer == NULL) {
304 free(state);
305 free(buffer);
306 archive_set_error(f->archive, ENOMEM,
307 "Can't allocate data for output buffering");
308 return (ARCHIVE_FATAL);
309 }
310
311 state->buffer_size = buffer_size;
312 state->buffer = buffer;
313 state->next = state->buffer;
314 state->avail = state->buffer_size;
315 f->data = state;
316
317 if (a->client_opener == NULL)
318 return (ARCHIVE_OK);
319 return (a->client_opener(f->archive, a->client_data));
320 }
321
322 static int
archive_write_client_write(struct archive_write_filter * f,const void * _buff,size_t length)323 archive_write_client_write(struct archive_write_filter *f,
324 const void *_buff, size_t length)
325 {
326 struct archive_write *a = (struct archive_write *)f->archive;
327 struct archive_none *state = (struct archive_none *)f->data;
328 const char *buff = (const char *)_buff;
329 ssize_t remaining, to_copy;
330 ssize_t bytes_written;
331
332 remaining = length;
333
334 /*
335 * If there is no buffer for blocking, just pass the data
336 * straight through to the client write callback. In
337 * particular, this supports "no write delay" operation for
338 * special applications. Just set the block size to zero.
339 */
340 if (state->buffer_size == 0) {
341 while (remaining > 0) {
342 bytes_written = (a->client_writer)(&a->archive,
343 a->client_data, buff, remaining);
344 if (bytes_written <= 0)
345 return (ARCHIVE_FATAL);
346 remaining -= bytes_written;
347 buff += bytes_written;
348 }
349 return (ARCHIVE_OK);
350 }
351
352 /* If the copy buffer isn't empty, try to fill it. */
353 if (state->avail < state->buffer_size) {
354 /* If buffer is not empty... */
355 /* ... copy data into buffer ... */
356 to_copy = ((size_t)remaining > state->avail) ?
357 state->avail : (size_t)remaining;
358 memcpy(state->next, buff, to_copy);
359 state->next += to_copy;
360 state->avail -= to_copy;
361 buff += to_copy;
362 remaining -= to_copy;
363 /* ... if it's full, write it out. */
364 if (state->avail == 0) {
365 char *p = state->buffer;
366 size_t to_write = state->buffer_size;
367 while (to_write > 0) {
368 bytes_written = (a->client_writer)(&a->archive,
369 a->client_data, p, to_write);
370 if (bytes_written <= 0)
371 return (ARCHIVE_FATAL);
372 if ((size_t)bytes_written > to_write) {
373 archive_set_error(&(a->archive),
374 -1, "write overrun");
375 return (ARCHIVE_FATAL);
376 }
377 p += bytes_written;
378 to_write -= bytes_written;
379 }
380 state->next = state->buffer;
381 state->avail = state->buffer_size;
382 }
383 }
384
385 while ((size_t)remaining >= state->buffer_size) {
386 /* Write out full blocks directly to client. */
387 bytes_written = (a->client_writer)(&a->archive,
388 a->client_data, buff, state->buffer_size);
389 if (bytes_written <= 0)
390 return (ARCHIVE_FATAL);
391 buff += bytes_written;
392 remaining -= bytes_written;
393 }
394
395 if (remaining > 0) {
396 /* Copy last bit into copy buffer. */
397 memcpy(state->next, buff, remaining);
398 state->next += remaining;
399 state->avail -= remaining;
400 }
401 return (ARCHIVE_OK);
402 }
403
404 static int
archive_write_client_close(struct archive_write_filter * f)405 archive_write_client_close(struct archive_write_filter *f)
406 {
407 struct archive_write *a = (struct archive_write *)f->archive;
408 struct archive_none *state = (struct archive_none *)f->data;
409 ssize_t block_length;
410 ssize_t target_block_length;
411 ssize_t bytes_written;
412 int ret = ARCHIVE_OK;
413
414 /* If there's pending data, pad and write the last block */
415 if (state->next != state->buffer) {
416 block_length = state->buffer_size - state->avail;
417
418 /* Tricky calculation to determine size of last block */
419 if (a->bytes_in_last_block <= 0)
420 /* Default or Zero: pad to full block */
421 target_block_length = a->bytes_per_block;
422 else
423 /* Round to next multiple of bytes_in_last_block. */
424 target_block_length = a->bytes_in_last_block *
425 ( (block_length + a->bytes_in_last_block - 1) /
426 a->bytes_in_last_block);
427 if (target_block_length > a->bytes_per_block)
428 target_block_length = a->bytes_per_block;
429 if (block_length < target_block_length) {
430 memset(state->next, 0,
431 target_block_length - block_length);
432 block_length = target_block_length;
433 }
434 bytes_written = (a->client_writer)(&a->archive,
435 a->client_data, state->buffer, block_length);
436 ret = bytes_written <= 0 ? ARCHIVE_FATAL : ARCHIVE_OK;
437 }
438 if (a->client_closer)
439 (*a->client_closer)(&a->archive, a->client_data);
440 free(state->buffer);
441 free(state);
442 /* Clear the close handler myself not to be called again. */
443 f->close = NULL;
444 a->client_data = NULL;
445 /* Clear passphrase. */
446 if (a->passphrase != NULL) {
447 memset(a->passphrase, 0, strlen(a->passphrase));
448 free(a->passphrase);
449 a->passphrase = NULL;
450 }
451 return (ret);
452 }
453
454 /*
455 * Open the archive using the current settings.
456 */
457 int
archive_write_open(struct archive * _a,void * client_data,archive_open_callback * opener,archive_write_callback * writer,archive_close_callback * closer)458 archive_write_open(struct archive *_a, void *client_data,
459 archive_open_callback *opener, archive_write_callback *writer,
460 archive_close_callback *closer)
461 {
462 struct archive_write *a = (struct archive_write *)_a;
463 struct archive_write_filter *client_filter;
464 int ret, r1;
465
466 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
467 ARCHIVE_STATE_NEW, "archive_write_open");
468 archive_clear_error(&a->archive);
469
470 a->client_writer = writer;
471 a->client_opener = opener;
472 a->client_closer = closer;
473 a->client_data = client_data;
474
475 client_filter = __archive_write_allocate_filter(_a);
476 client_filter->open = archive_write_client_open;
477 client_filter->write = archive_write_client_write;
478 client_filter->close = archive_write_client_close;
479
480 ret = __archive_write_open_filter(a->filter_first);
481 if (ret < ARCHIVE_WARN) {
482 r1 = __archive_write_close_filter(a->filter_first);
483 return (r1 < ret ? r1 : ret);
484 }
485
486 a->archive.state = ARCHIVE_STATE_HEADER;
487 if (a->format_init)
488 ret = (a->format_init)(a);
489 return (ret);
490 }
491
492 /*
493 * Close out the archive.
494 */
495 static int
_archive_write_close(struct archive * _a)496 _archive_write_close(struct archive *_a)
497 {
498 struct archive_write *a = (struct archive_write *)_a;
499 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
500
501 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
502 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL,
503 "archive_write_close");
504 if (a->archive.state == ARCHIVE_STATE_NEW
505 || a->archive.state == ARCHIVE_STATE_CLOSED)
506 return (ARCHIVE_OK); /* Okay to close() when not open. */
507
508 archive_clear_error(&a->archive);
509
510 /* Finish the last entry if a finish callback is specified */
511 if (a->archive.state == ARCHIVE_STATE_DATA
512 && a->format_finish_entry != NULL)
513 r = ((a->format_finish_entry)(a));
514
515 /* Finish off the archive. */
516 /* TODO: have format closers invoke compression close. */
517 if (a->format_close != NULL) {
518 r1 = (a->format_close)(a);
519 if (r1 < r)
520 r = r1;
521 }
522
523 /* Finish the compression and close the stream. */
524 r1 = __archive_write_close_filter(a->filter_first);
525 if (r1 < r)
526 r = r1;
527
528 if (a->archive.state != ARCHIVE_STATE_FATAL)
529 a->archive.state = ARCHIVE_STATE_CLOSED;
530 return (r);
531 }
532
533 static int
_archive_write_filter_count(struct archive * _a)534 _archive_write_filter_count(struct archive *_a)
535 {
536 struct archive_write *a = (struct archive_write *)_a;
537 struct archive_write_filter *p = a->filter_first;
538 int count = 0;
539 while(p) {
540 count++;
541 p = p->next_filter;
542 }
543 return count;
544 }
545
546 void
__archive_write_filters_free(struct archive * _a)547 __archive_write_filters_free(struct archive *_a)
548 {
549 struct archive_write *a = (struct archive_write *)_a;
550 int r = ARCHIVE_OK, r1;
551
552 while (a->filter_first != NULL) {
553 struct archive_write_filter *next
554 = a->filter_first->next_filter;
555 if (a->filter_first->free != NULL) {
556 r1 = (*a->filter_first->free)(a->filter_first);
557 if (r > r1)
558 r = r1;
559 }
560 free(a->filter_first);
561 a->filter_first = next;
562 }
563 a->filter_last = NULL;
564 }
565
566 /*
567 * Destroy the archive structure.
568 *
569 * Be careful: user might just call write_new and then write_free.
570 * Don't assume we actually wrote anything or performed any non-trivial
571 * initialization.
572 */
573 static int
_archive_write_free(struct archive * _a)574 _archive_write_free(struct archive *_a)
575 {
576 struct archive_write *a = (struct archive_write *)_a;
577 int r = ARCHIVE_OK, r1;
578
579 if (_a == NULL)
580 return (ARCHIVE_OK);
581 /* It is okay to call free() in state FATAL. */
582 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
583 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free");
584 if (a->archive.state != ARCHIVE_STATE_FATAL)
585 r = archive_write_close(&a->archive);
586
587 /* Release format resources. */
588 if (a->format_free != NULL) {
589 r1 = (a->format_free)(a);
590 if (r1 < r)
591 r = r1;
592 }
593
594 __archive_write_filters_free(_a);
595
596 /* Release various dynamic buffers. */
597 free((void *)(uintptr_t)(const void *)a->nulls);
598 archive_string_free(&a->archive.error_string);
599 if (a->passphrase != NULL) {
600 /* A passphrase should be cleaned. */
601 memset(a->passphrase, 0, strlen(a->passphrase));
602 free(a->passphrase);
603 }
604 a->archive.magic = 0;
605 __archive_clean(&a->archive);
606 free(a);
607 return (r);
608 }
609
610 /*
611 * Write the appropriate header.
612 */
613 static int
_archive_write_header(struct archive * _a,struct archive_entry * entry)614 _archive_write_header(struct archive *_a, struct archive_entry *entry)
615 {
616 struct archive_write *a = (struct archive_write *)_a;
617 int ret, r2;
618
619 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
620 ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
621 archive_clear_error(&a->archive);
622
623 if (a->format_write_header == NULL) {
624 archive_set_error(&(a->archive), -1,
625 "Format must be set before you can write to an archive.");
626 a->archive.state = ARCHIVE_STATE_FATAL;
627 return (ARCHIVE_FATAL);
628 }
629
630 /* In particular, "retry" and "fatal" get returned immediately. */
631 ret = archive_write_finish_entry(&a->archive);
632 if (ret == ARCHIVE_FATAL) {
633 a->archive.state = ARCHIVE_STATE_FATAL;
634 return (ARCHIVE_FATAL);
635 }
636 if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
637 return (ret);
638
639 if (a->skip_file_set &&
640 archive_entry_dev_is_set(entry) &&
641 archive_entry_ino_is_set(entry) &&
642 archive_entry_dev(entry) == (dev_t)a->skip_file_dev &&
643 archive_entry_ino64(entry) == a->skip_file_ino) {
644 archive_set_error(&a->archive, 0,
645 "Can't add archive to itself");
646 return (ARCHIVE_FAILED);
647 }
648
649 /* Format and write header. */
650 r2 = ((a->format_write_header)(a, entry));
651 if (r2 == ARCHIVE_FAILED) {
652 return (ARCHIVE_FAILED);
653 }
654 if (r2 == ARCHIVE_FATAL) {
655 a->archive.state = ARCHIVE_STATE_FATAL;
656 return (ARCHIVE_FATAL);
657 }
658 if (r2 < ret)
659 ret = r2;
660
661 a->archive.state = ARCHIVE_STATE_DATA;
662 return (ret);
663 }
664
665 static int
_archive_write_finish_entry(struct archive * _a)666 _archive_write_finish_entry(struct archive *_a)
667 {
668 struct archive_write *a = (struct archive_write *)_a;
669 int ret = ARCHIVE_OK;
670
671 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
672 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
673 "archive_write_finish_entry");
674 if (a->archive.state & ARCHIVE_STATE_DATA
675 && a->format_finish_entry != NULL)
676 ret = (a->format_finish_entry)(a);
677 a->archive.state = ARCHIVE_STATE_HEADER;
678 return (ret);
679 }
680
681 /*
682 * Note that the compressor is responsible for blocking.
683 */
684 static ssize_t
_archive_write_data(struct archive * _a,const void * buff,size_t s)685 _archive_write_data(struct archive *_a, const void *buff, size_t s)
686 {
687 struct archive_write *a = (struct archive_write *)_a;
688 const size_t max_write = INT_MAX;
689
690 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
691 ARCHIVE_STATE_DATA, "archive_write_data");
692 /* In particular, this catches attempts to pass negative values. */
693 if (s > max_write)
694 s = max_write;
695 archive_clear_error(&a->archive);
696 return ((a->format_write_data)(a, buff, s));
697 }
698
699 static struct archive_write_filter *
filter_lookup(struct archive * _a,int n)700 filter_lookup(struct archive *_a, int n)
701 {
702 struct archive_write *a = (struct archive_write *)_a;
703 struct archive_write_filter *f = a->filter_first;
704 if (n == -1)
705 return a->filter_last;
706 if (n < 0)
707 return NULL;
708 while (n > 0 && f != NULL) {
709 f = f->next_filter;
710 --n;
711 }
712 return f;
713 }
714
715 static int
_archive_filter_code(struct archive * _a,int n)716 _archive_filter_code(struct archive *_a, int n)
717 {
718 struct archive_write_filter *f = filter_lookup(_a, n);
719 return f == NULL ? -1 : f->code;
720 }
721
722 static const char *
_archive_filter_name(struct archive * _a,int n)723 _archive_filter_name(struct archive *_a, int n)
724 {
725 struct archive_write_filter *f = filter_lookup(_a, n);
726 return f != NULL ? f->name : NULL;
727 }
728
729 static int64_t
_archive_filter_bytes(struct archive * _a,int n)730 _archive_filter_bytes(struct archive *_a, int n)
731 {
732 struct archive_write_filter *f = filter_lookup(_a, n);
733 return f == NULL ? -1 : f->bytes_written;
734 }
735