1 /*-
2 * Copyright (c) 2003-2007 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$");
28
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
41
42 #include "archive.h"
43 #include "archive_entry.h"
44 #include "archive_private.h"
45 #include "archive_read_private.h"
46 #include "archive_write_disk_private.h"
47
48 struct extract {
49 struct archive *ad; /* archive_write_disk object */
50
51 /* Progress function invoked during extract. */
52 void (*extract_progress)(void *);
53 void *extract_progress_user_data;
54 };
55
56 static int archive_read_extract_cleanup(struct archive_read *);
57 static int copy_data(struct archive *ar, struct archive *aw);
58 static struct extract *get_extract(struct archive_read *);
59
60 static struct extract *
get_extract(struct archive_read * a)61 get_extract(struct archive_read *a)
62 {
63 /* If we haven't initialized, do it now. */
64 /* This also sets up a lot of global state. */
65 if (a->extract == NULL) {
66 a->extract = (struct extract *)malloc(sizeof(*a->extract));
67 if (a->extract == NULL) {
68 archive_set_error(&a->archive, ENOMEM, "Can't extract");
69 return (NULL);
70 }
71 memset(a->extract, 0, sizeof(*a->extract));
72 a->extract->ad = archive_write_disk_new();
73 if (a->extract->ad == NULL) {
74 archive_set_error(&a->archive, ENOMEM, "Can't extract");
75 return (NULL);
76 }
77 archive_write_disk_set_standard_lookup(a->extract->ad);
78 a->cleanup_archive_extract = archive_read_extract_cleanup;
79 }
80 return (a->extract);
81 }
82
83 int
archive_read_extract(struct archive * _a,struct archive_entry * entry,int flags)84 archive_read_extract(struct archive *_a, struct archive_entry *entry, int flags)
85 {
86 struct extract *extract;
87
88 extract = get_extract((struct archive_read *)_a);
89 if (extract == NULL)
90 return (ARCHIVE_FATAL);
91 archive_write_disk_set_options(extract->ad, flags);
92 return (archive_read_extract2(_a, entry, extract->ad));
93 }
94
95 int
archive_read_extract2(struct archive * _a,struct archive_entry * entry,struct archive * ad)96 archive_read_extract2(struct archive *_a, struct archive_entry *entry,
97 struct archive *ad)
98 {
99 struct archive_read *a = (struct archive_read *)_a;
100 int r, r2;
101
102 /* Set up for this particular entry. */
103 if (a->skip_file_set)
104 archive_write_disk_set_skip_file(ad,
105 a->skip_file_dev, a->skip_file_ino);
106 r = archive_write_header(ad, entry);
107 if (r < ARCHIVE_WARN)
108 r = ARCHIVE_WARN;
109 if (r != ARCHIVE_OK)
110 /* If _write_header failed, copy the error. */
111 archive_copy_error(&a->archive, ad);
112 else if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) > 0)
113 /* Otherwise, pour data into the entry. */
114 r = copy_data(_a, ad);
115 r2 = archive_write_finish_entry(ad);
116 if (r2 < ARCHIVE_WARN)
117 r2 = ARCHIVE_WARN;
118 /* Use the first message. */
119 if (r2 != ARCHIVE_OK && r == ARCHIVE_OK)
120 archive_copy_error(&a->archive, ad);
121 /* Use the worst error return. */
122 if (r2 < r)
123 r = r2;
124 return (r);
125 }
126
127 void
archive_read_extract_set_progress_callback(struct archive * _a,void (* progress_func)(void *),void * user_data)128 archive_read_extract_set_progress_callback(struct archive *_a,
129 void (*progress_func)(void *), void *user_data)
130 {
131 struct archive_read *a = (struct archive_read *)_a;
132 struct extract *extract = get_extract(a);
133 if (extract != NULL) {
134 extract->extract_progress = progress_func;
135 extract->extract_progress_user_data = user_data;
136 }
137 }
138
139 static int
copy_data(struct archive * ar,struct archive * aw)140 copy_data(struct archive *ar, struct archive *aw)
141 {
142 int64_t offset;
143 const void *buff;
144 struct extract *extract;
145 size_t size;
146 int r;
147
148 extract = get_extract((struct archive_read *)ar);
149 if (extract == NULL)
150 return (ARCHIVE_FATAL);
151 for (;;) {
152 r = archive_read_data_block(ar, &buff, &size, &offset);
153 if (r == ARCHIVE_EOF)
154 return (ARCHIVE_OK);
155 if (r != ARCHIVE_OK)
156 return (r);
157 r = (int)archive_write_data_block(aw, buff, size, offset);
158 if (r < ARCHIVE_WARN)
159 r = ARCHIVE_WARN;
160 if (r != ARCHIVE_OK) {
161 archive_set_error(ar, archive_errno(aw),
162 "%s", archive_error_string(aw));
163 return (r);
164 }
165 if (extract->extract_progress)
166 (extract->extract_progress)
167 (extract->extract_progress_user_data);
168 }
169 }
170
171 /*
172 * Cleanup function for archive_extract.
173 */
174 static int
archive_read_extract_cleanup(struct archive_read * a)175 archive_read_extract_cleanup(struct archive_read *a)
176 {
177 int ret = ARCHIVE_OK;
178
179 ret = archive_write_free(a->extract->ad);
180 free(a->extract);
181 a->extract = NULL;
182 return (ret);
183 }
184