2 * Copyright (c) 2007 Joerg Sonnenberger
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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$");
30 /* This capability is only available on POSIX systems. */
31 #if !defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
32 !(defined(HAVE_FORK) || defined(HAVE_VFORK))
37 * On non-Posix systems, allow the program to build, but choke if
38 * this function is actually invoked.
41 archive_read_support_compression_program(struct archive *_a, const char *cmd)
43 archive_set_error(_a, -1,
44 "External compression programs not supported on this platform");
45 return (ARCHIVE_FATAL);
50 #ifdef HAVE_SYS_WAIT_H
51 # include <sys/wait.h>
73 #include "archive_private.h"
74 #include "archive_read_private.h"
76 #include "filter_fork.h"
78 struct archive_decompress_program {
81 int child_stdin, child_stdout;
84 char *child_out_buf_next;
85 size_t child_out_buf_len, child_out_buf_avail;
87 const char *child_in_buf;
88 size_t child_in_buf_avail;
91 static int archive_decompressor_program_bid(const void *, size_t);
92 static int archive_decompressor_program_finish(struct archive_read *);
93 static int archive_decompressor_program_init(struct archive_read *,
94 const void *, size_t);
95 static ssize_t archive_decompressor_program_read_ahead(struct archive_read *,
96 const void **, size_t);
97 static ssize_t archive_decompressor_program_read_consume(struct archive_read *,
101 archive_read_support_compression_program(struct archive *_a, const char *cmd)
103 struct archive_read *a = (struct archive_read *)_a;
104 struct decompressor_t *decompressor;
106 if (cmd == NULL || *cmd == '\0')
107 return (ARCHIVE_WARN);
109 decompressor = __archive_read_register_compression(a,
110 archive_decompressor_program_bid,
111 archive_decompressor_program_init);
112 if (decompressor == NULL)
113 return (ARCHIVE_WARN);
115 decompressor->config = strdup(cmd);
120 * If the user used us to register, they must really want us to
121 * handle it, so this module always bids INT_MAX.
124 archive_decompressor_program_bid(const void *buff, size_t len)
126 (void)buff; /* UNUSED */
127 (void)len; /* UNUSED */
129 return (INT_MAX); /* Default: We'll take it. */
133 child_read(struct archive_read *a, char *buf, size_t buf_len)
135 struct archive_decompress_program *state = a->decompressor->data;
136 ssize_t ret, requested;
137 const void *child_buf;
139 if (state->child_stdout == -1)
146 requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
149 ret = read(state->child_stdout, buf, requested);
150 } while (ret == -1 && errno == EINTR);
154 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
155 close(state->child_stdout);
156 state->child_stdout = -1;
159 if (ret == -1 && errno != EAGAIN)
162 if (state->child_in_buf_avail == 0) {
163 child_buf = state->child_in_buf;
164 ret = (a->client_reader)(&a->archive,
165 a->client_data,&child_buf);
166 state->child_in_buf = (const char *)child_buf;
169 close(state->child_stdin);
170 state->child_stdin = -1;
171 fcntl(state->child_stdout, F_SETFL, 0);
175 close(state->child_stdin);
176 state->child_stdin = -1;
177 fcntl(state->child_stdout, F_SETFL, 0);
180 state->child_in_buf_avail = ret;
183 if (state->child_stdin == -1) {
184 fcntl(state->child_stdout, F_SETFL, 0);
185 __archive_check_child(state->child_stdin, state->child_stdout);
190 ret = write(state->child_stdin, state->child_in_buf,
191 state->child_in_buf_avail);
192 } while (ret == -1 && errno == EINTR);
195 state->child_in_buf += ret;
196 state->child_in_buf_avail -= ret;
198 } else if (ret == -1 && errno == EAGAIN) {
199 __archive_check_child(state->child_stdin, state->child_stdout);
201 } else if (ret == 0 || (ret == -1 && errno == EPIPE)) {
202 close(state->child_stdin);
203 state->child_stdin = -1;
204 fcntl(state->child_stdout, F_SETFL, 0);
207 close(state->child_stdin);
208 state->child_stdin = -1;
209 fcntl(state->child_stdout, F_SETFL, 0);
215 archive_decompressor_program_init(struct archive_read *a, const void *buff, size_t n)
217 struct archive_decompress_program *state;
218 const char *cmd = a->decompressor->config;
219 const char *prefix = "Program: ";
222 state = (struct archive_decompress_program *)malloc(sizeof(*state));
224 archive_set_error(&a->archive, ENOMEM,
225 "Can't allocate input data");
226 return (ARCHIVE_FATAL);
229 a->archive.compression_code = ARCHIVE_COMPRESSION_PROGRAM;
230 state->description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
231 strcpy(state->description, prefix);
232 strcat(state->description, cmd);
233 a->archive.compression_name = state->description;
235 state->child_out_buf_next = state->child_out_buf = malloc(65536);
236 if (!state->child_out_buf) {
238 archive_set_error(&a->archive, ENOMEM,
239 "Can't allocate filter buffer");
240 return (ARCHIVE_FATAL);
242 state->child_out_buf_len = 65536;
243 state->child_out_buf_avail = 0;
245 state->child_in_buf = buff;
246 state->child_in_buf_avail = n;
248 if ((state->child = __archive_create_child(cmd,
249 &state->child_stdin, &state->child_stdout)) == -1) {
250 free(state->child_out_buf);
252 archive_set_error(&a->archive, EINVAL,
253 "Can't initialise filter");
254 return (ARCHIVE_FATAL);
257 a->decompressor->data = state;
258 a->decompressor->read_ahead = archive_decompressor_program_read_ahead;
259 a->decompressor->consume = archive_decompressor_program_read_consume;
260 a->decompressor->skip = NULL;
261 a->decompressor->finish = archive_decompressor_program_finish;
263 /* XXX Check that we can read at least one byte? */
268 archive_decompressor_program_read_ahead(struct archive_read *a, const void **buff,
271 struct archive_decompress_program *state;
274 state = (struct archive_decompress_program *)a->decompressor->data;
276 if (min > state->child_out_buf_len)
277 min = state->child_out_buf_len;
279 while (state->child_stdout != -1 && min > state->child_out_buf_avail) {
280 if (state->child_out_buf != state->child_out_buf_next) {
281 memmove(state->child_out_buf, state->child_out_buf_next,
282 state->child_out_buf_avail);
283 state->child_out_buf_next = state->child_out_buf;
286 bytes_read = child_read(a,
287 state->child_out_buf + state->child_out_buf_avail,
288 state->child_out_buf_len - state->child_out_buf_avail);
289 if (bytes_read == -1)
293 state->child_out_buf_avail += bytes_read;
294 a->archive.raw_position += bytes_read;
297 *buff = state->child_out_buf_next;
298 return (state->child_out_buf_avail);
302 archive_decompressor_program_read_consume(struct archive_read *a, size_t request)
304 struct archive_decompress_program *state;
306 state = (struct archive_decompress_program *)a->decompressor->data;
308 state->child_out_buf_next += request;
309 state->child_out_buf_avail -= request;
311 a->archive.file_position += request;
316 archive_decompressor_program_finish(struct archive_read *a)
318 struct archive_decompress_program *state;
321 state = (struct archive_decompress_program *)a->decompressor->data;
323 /* Release our configuration data. */
324 free(a->decompressor->config);
325 a->decompressor->config = NULL;
327 /* Shut down the child. */
328 if (state->child_stdin != -1)
329 close(state->child_stdin);
330 if (state->child_stdout != -1)
331 close(state->child_stdout);
332 while (waitpid(state->child, &status, 0) == -1 && errno == EINTR)
335 /* Release our private data. */
336 free(state->child_out_buf);
337 free(state->description);
339 a->decompressor->data = NULL;
344 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */