Import libarchive and bsdtar version 2.0.25
[dragonfly/port-amd64.git] / contrib / libarchive-2.0 / libarchive / archive_read_extract.c
bloba06671e4793c88cc0bb75e90ce60eac3d2738dff
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_extract.c,v 1.54 2007/03/11 10:29:52 kientzle Exp $");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
42 #include "archive.h"
43 #include "archive_private.h"
44 #include "archive_read_private.h"
45 #include "archive_write_disk_private.h"
47 struct extract {
48 struct archive *ad; /* archive_write_disk object */
50 /* Progress function invoked during extract. */
51 void (*extract_progress)(void *);
52 void *extract_progress_user_data;
55 static int archive_read_extract_cleanup(struct archive_read *);
56 static int copy_data(struct archive *ar, struct archive *aw);
57 static struct extract *get_extract(struct archive_read *);
59 static struct extract *
60 get_extract(struct archive_read *a)
62 /* If we haven't initialized, do it now. */
63 /* This also sets up a lot of global state. */
64 if (a->extract == NULL) {
65 a->extract = (struct extract *)malloc(sizeof(*a->extract));
66 if (a->extract == NULL) {
67 archive_set_error(&a->archive, ENOMEM, "Can't extract");
68 return (NULL);
70 a->extract->ad = archive_write_disk_new();
71 if (a->extract->ad == NULL) {
72 archive_set_error(&a->archive, ENOMEM, "Can't extract");
73 return (NULL);
75 archive_write_disk_set_standard_lookup(a->extract->ad);
76 a->cleanup_archive_extract = archive_read_extract_cleanup;
78 return (a->extract);
81 int
82 archive_read_extract(struct archive *_a, struct archive_entry *entry, int flags)
84 struct archive_read *a = (struct archive_read *)_a;
85 struct extract *extract;
86 int r, r2;
88 extract = get_extract(a);
89 if (extract == NULL)
90 return (ARCHIVE_FATAL);
92 /* Set up for this particular entry. */
93 extract = a->extract;
94 archive_write_disk_set_options(a->extract->ad, flags);
95 archive_write_disk_set_skip_file(a->extract->ad,
96 a->skip_file_dev, a->skip_file_ino);
97 r = archive_write_header(a->extract->ad, entry);
98 if (r == ARCHIVE_OK)
99 /* If there's an FD, pour data into it. */
100 r = copy_data(_a, a->extract->ad);
101 if (r != ARCHIVE_OK)
102 archive_set_error(&a->archive,
103 archive_errno(extract->ad),
104 "%s", archive_error_string(extract->ad));
105 r2 = archive_write_finish_entry(a->extract->ad);
106 /* Use the first message. */
107 if (r2 != ARCHIVE_OK && r == ARCHIVE_OK)
108 archive_set_error(&a->archive,
109 archive_errno(extract->ad),
110 "%s", archive_error_string(extract->ad));
111 /* Use the worst error return. */
112 if (r2 < r)
113 r = r2;
114 return (r);
117 void
118 archive_read_extract_set_progress_callback(struct archive *_a,
119 void (*progress_func)(void *), void *user_data)
121 struct archive_read *a = (struct archive_read *)_a;
122 struct extract *extract = get_extract(a);
123 if (extract != NULL) {
124 extract->extract_progress = progress_func;
125 extract->extract_progress_user_data = user_data;
129 static int
130 copy_data(struct archive *ar, struct archive *aw)
132 int r;
133 const void *buff;
134 size_t size;
135 off_t offset;
137 for (;;) {
138 r = archive_read_data_block(ar, &buff, &size, &offset);
139 if (r == ARCHIVE_EOF)
140 return (ARCHIVE_OK);
141 if (r != ARCHIVE_OK)
142 return (r);
143 r = archive_write_data_block(aw, buff, size, offset);
144 if (r != ARCHIVE_OK)
145 return (r);
150 * Cleanup function for archive_extract.
152 static int
153 archive_read_extract_cleanup(struct archive_read *a)
155 int ret = ARCHIVE_OK;
157 #if ARCHIVE_API_VERSION > 1
158 ret =
159 #endif
160 archive_write_finish(a->extract->ad);
161 free(a->extract);
162 a->extract = NULL;
163 return (ret);