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))
35 * On non-Posix systems, allow the program to build, but choke if
36 * this function is actually invoked.
39 archive_read_support_compression_program(struct archive *_a, const char *cmd)
41 archive_set_error(_a, -1,
42 "External compression programs not supported on this platform");
43 return (ARCHIVE_FATAL);
48 #ifdef HAVE_SYS_WAIT_H
49 # include <sys/wait.h>
71 #include "archive_private.h"
72 #include "archive_read_private.h"
74 #include "filter_fork.h"
76 struct archive_decompress_program {
79 int child_stdin, child_stdout;
82 char *child_out_buf_next;
83 size_t child_out_buf_len, child_out_buf_avail;
85 const char *child_in_buf;
86 size_t child_in_buf_avail;
89 static int archive_decompressor_program_bid(const void *, size_t);
90 static int archive_decompressor_program_finish(struct archive_read *);
91 static int archive_decompressor_program_init(struct archive_read *,
92 const void *, size_t);
93 static ssize_t archive_decompressor_program_read_ahead(struct archive_read *,
94 const void **, size_t);
95 static ssize_t archive_decompressor_program_read_consume(struct archive_read *,
99 archive_read_support_compression_program(struct archive *_a, const char *cmd)
101 struct archive_read *a = (struct archive_read *)_a;
102 struct decompressor_t *decompressor;
104 if (cmd == NULL || *cmd == '\0')
105 return (ARCHIVE_WARN);
107 decompressor = __archive_read_register_compression(a,
108 archive_decompressor_program_bid,
109 archive_decompressor_program_init);
110 if (decompressor == NULL)
111 return (ARCHIVE_WARN);
113 decompressor->config = strdup(cmd);
118 * If the user used us to register, they must really want us to
119 * handle it, so this module always bids INT_MAX.
122 archive_decompressor_program_bid(const void *buff, size_t len)
124 (void)buff; /* UNUSED */
125 (void)len; /* UNUSED */
127 return (INT_MAX); /* Default: We'll take it. */
131 child_read(struct archive_read *a, char *buf, size_t buf_len)
133 struct archive_decompress_program *state = a->decompressor->data;
134 ssize_t ret, requested;
135 const void *child_buf;
137 if (state->child_stdout == -1)
144 requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
147 ret = read(state->child_stdout, buf, requested);
148 } while (ret == -1 && errno == EINTR);
152 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
153 close(state->child_stdout);
154 state->child_stdout = -1;
157 if (ret == -1 && errno != EAGAIN)
160 if (state->child_in_buf_avail == 0) {
161 child_buf = state->child_in_buf;
162 ret = (a->client_reader)(&a->archive,
163 a->client_data,&child_buf);
164 state->child_in_buf = (const char *)child_buf;
167 close(state->child_stdin);
168 state->child_stdin = -1;
169 fcntl(state->child_stdout, F_SETFL, 0);
173 close(state->child_stdin);
174 state->child_stdin = -1;
175 fcntl(state->child_stdout, F_SETFL, 0);
178 state->child_in_buf_avail = ret;
181 if (state->child_stdin == -1) {
182 fcntl(state->child_stdout, F_SETFL, 0);
183 __archive_check_child(state->child_stdin, state->child_stdout);
188 ret = write(state->child_stdin, state->child_in_buf,
189 state->child_in_buf_avail);
190 } while (ret == -1 && errno == EINTR);
193 state->child_in_buf += ret;
194 state->child_in_buf_avail -= ret;
196 } else if (ret == -1 && errno == EAGAIN) {
197 __archive_check_child(state->child_stdin, state->child_stdout);
199 } else if (ret == 0 || (ret == -1 && errno == EPIPE)) {
200 close(state->child_stdin);
201 state->child_stdin = -1;
202 fcntl(state->child_stdout, F_SETFL, 0);
205 close(state->child_stdin);
206 state->child_stdin = -1;
207 fcntl(state->child_stdout, F_SETFL, 0);
213 archive_decompressor_program_init(struct archive_read *a, const void *buff, size_t n)
215 struct archive_decompress_program *state;
216 const char *cmd = a->decompressor->config;
217 const char *prefix = "Program: ";
220 state = (struct archive_decompress_program *)malloc(sizeof(*state));
222 archive_set_error(&a->archive, ENOMEM,
223 "Can't allocate input data");
224 return (ARCHIVE_FATAL);
227 a->archive.compression_code = ARCHIVE_COMPRESSION_PROGRAM;
228 state->description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
229 strcpy(state->description, prefix);
230 strcat(state->description, cmd);
231 a->archive.compression_name = state->description;
233 state->child_out_buf_next = state->child_out_buf = malloc(65536);
234 if (!state->child_out_buf) {
236 archive_set_error(&a->archive, ENOMEM,
237 "Can't allocate filter buffer");
238 return (ARCHIVE_FATAL);
240 state->child_out_buf_len = 65536;
241 state->child_out_buf_avail = 0;
243 state->child_in_buf = buff;
244 state->child_in_buf_avail = n;
246 if ((state->child = __archive_create_child(cmd,
247 &state->child_stdin, &state->child_stdout)) == -1) {
248 free(state->child_out_buf);
250 archive_set_error(&a->archive, EINVAL,
251 "Can't initialise filter");
252 return (ARCHIVE_FATAL);
255 a->decompressor->data = state;
256 a->decompressor->read_ahead = archive_decompressor_program_read_ahead;
257 a->decompressor->consume = archive_decompressor_program_read_consume;
258 a->decompressor->skip = NULL;
259 a->decompressor->finish = archive_decompressor_program_finish;
261 /* XXX Check that we can read at least one byte? */
266 archive_decompressor_program_read_ahead(struct archive_read *a, const void **buff,
269 struct archive_decompress_program *state;
272 state = (struct archive_decompress_program *)a->decompressor->data;
274 if (min > state->child_out_buf_len)
275 min = state->child_out_buf_len;
277 while (state->child_stdout != -1 && min > state->child_out_buf_avail) {
278 if (state->child_out_buf != state->child_out_buf_next) {
279 memmove(state->child_out_buf, state->child_out_buf_next,
280 state->child_out_buf_avail);
281 state->child_out_buf_next = state->child_out_buf;
284 bytes_read = child_read(a,
285 state->child_out_buf + state->child_out_buf_avail,
286 state->child_out_buf_len - state->child_out_buf_avail);
287 if (bytes_read == -1)
291 state->child_out_buf_avail += bytes_read;
292 a->archive.raw_position += bytes_read;
295 *buff = state->child_out_buf_next;
296 return (state->child_out_buf_avail);
300 archive_decompressor_program_read_consume(struct archive_read *a, size_t request)
302 struct archive_decompress_program *state;
304 state = (struct archive_decompress_program *)a->decompressor->data;
306 state->child_out_buf_next += request;
307 state->child_out_buf_avail -= request;
309 a->archive.file_position += request;
314 archive_decompressor_program_finish(struct archive_read *a)
316 struct archive_decompress_program *state;
319 state = (struct archive_decompress_program *)a->decompressor->data;
321 /* Release our configuration data. */
322 free(a->decompressor->config);
323 a->decompressor->config = NULL;
325 /* Shut down the child. */
326 if (state->child_stdin != -1)
327 close(state->child_stdin);
328 if (state->child_stdout != -1)
329 close(state->child_stdout);
330 while (waitpid(state->child, &status, 0) == -1 && errno == EINTR)
333 /* Release our private data. */
334 free(state->child_out_buf);
335 free(state->description);
337 a->decompressor->data = NULL;
342 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */