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