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: src/lib/libarchive/archive_read_support_compression_program.c,v 1.2 2007/07/20 01:28:50 kientzle Exp $");
29 #ifdef HAVE_SYS_WAIT_H
30 # include <sys/wait.h>
52 #include "archive_private.h"
53 #include "archive_read_private.h"
55 #include "filter_fork.h"
57 struct archive_decompress_program {
60 int child_stdin, child_stdout;
63 char *child_out_buf_next;
64 size_t child_out_buf_len, child_out_buf_avail;
66 const char *child_in_buf;
67 size_t child_in_buf_avail;
70 static int archive_decompressor_program_bid(const void *, size_t);
71 static int archive_decompressor_program_finish(struct archive_read *);
72 static int archive_decompressor_program_init(struct archive_read *,
73 const void *, size_t);
74 static ssize_t archive_decompressor_program_read_ahead(struct archive_read *,
75 const void **, size_t);
76 static ssize_t archive_decompressor_program_read_consume(struct archive_read *,
80 archive_read_support_compression_program(struct archive *_a, const char *cmd)
82 struct archive_read *a = (struct archive_read *)_a;
83 struct decompressor_t *decompressor;
85 if (cmd == NULL || *cmd == '\0')
86 return (ARCHIVE_WARN);
88 decompressor = __archive_read_register_compression(a,
89 archive_decompressor_program_bid,
90 archive_decompressor_program_init);
91 if (decompressor == NULL)
92 return (ARCHIVE_WARN);
94 decompressor->config = strdup(cmd);
99 * If the user used us to register, they must really want us to
100 * handle it, so this module always bids INT_MAX.
103 archive_decompressor_program_bid(const void *buff, size_t len)
105 (void)buff; /* UNUSED */
106 (void)len; /* UNUSED */
108 return (INT_MAX); /* Default: We'll take it. */
112 child_read(struct archive_read *a, char *buf, size_t buf_len)
114 struct archive_decompress_program *state = a->decompressor->data;
115 ssize_t ret, requested;
116 const void *child_buf;
118 if (state->child_stdout == -1)
125 requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
128 ret = read(state->child_stdout, buf, requested);
129 } while (ret == -1 && errno == EINTR);
133 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
134 close(state->child_stdout);
135 state->child_stdout = -1;
138 if (ret == -1 && errno != EAGAIN)
141 if (state->child_in_buf_avail == 0) {
142 child_buf = state->child_in_buf;
143 ret = (a->client_reader)(&a->archive,
144 a->client_data,&child_buf);
145 state->child_in_buf = (const char *)child_buf;
148 close(state->child_stdin);
149 state->child_stdin = -1;
150 fcntl(state->child_stdout, F_SETFL, 0);
154 close(state->child_stdin);
155 state->child_stdin = -1;
156 fcntl(state->child_stdout, F_SETFL, 0);
159 state->child_in_buf_avail = ret;
163 ret = write(state->child_stdin, state->child_in_buf,
164 state->child_in_buf_avail);
165 } while (ret == -1 && errno == EINTR);
168 state->child_in_buf += ret;
169 state->child_in_buf_avail -= ret;
171 } else if (ret == -1 && errno == EAGAIN) {
172 __archive_check_child(state->child_stdin, state->child_stdout);
174 } else if (ret == 0 || (ret == -1 && errno == EPIPE)) {
175 close(state->child_stdin);
176 state->child_stdout = -1;
177 fcntl(state->child_stdout, F_SETFL, 0);
180 close(state->child_stdin);
181 state->child_stdin = -1;
182 fcntl(state->child_stdout, F_SETFL, 0);
188 archive_decompressor_program_init(struct archive_read *a, const void *buff, size_t n)
190 struct archive_decompress_program *state;
191 const char *cmd = a->decompressor->config;
192 const char *prefix = "Program: ";
195 state = (struct archive_decompress_program *)malloc(sizeof(*state));
197 archive_set_error(&a->archive, ENOMEM,
198 "Can't allocate input data");
199 return (ARCHIVE_FATAL);
202 a->archive.compression_code = ARCHIVE_COMPRESSION_PROGRAM;
203 state->description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
204 strcpy(state->description, prefix);
205 strcat(state->description, cmd);
206 a->archive.compression_name = state->description;
208 state->child_out_buf_next = state->child_out_buf = malloc(65536);
209 if (!state->child_out_buf) {
211 archive_set_error(&a->archive, ENOMEM,
212 "Can't allocate filter buffer");
213 return (ARCHIVE_FATAL);
215 state->child_out_buf_len = 65536;
216 state->child_out_buf_avail = 0;
218 state->child_in_buf = buff;
219 state->child_in_buf_avail = n;
221 if ((state->child = __archive_create_child(cmd,
222 &state->child_stdin, &state->child_stdout)) == -1) {
223 free(state->child_out_buf);
225 archive_set_error(&a->archive, EINVAL,
226 "Can't initialise filter");
227 return (ARCHIVE_FATAL);
230 a->decompressor->data = state;
231 a->decompressor->read_ahead = archive_decompressor_program_read_ahead;
232 a->decompressor->consume = archive_decompressor_program_read_consume;
233 a->decompressor->skip = NULL;
234 a->decompressor->finish = archive_decompressor_program_finish;
236 /* XXX Check that we can read at least one byte? */
241 archive_decompressor_program_read_ahead(struct archive_read *a, const void **buff,
244 struct archive_decompress_program *state;
247 state = (struct archive_decompress_program *)a->decompressor->data;
249 if (min > state->child_out_buf_len)
250 min = state->child_out_buf_len;
252 while (state->child_stdout != -1 && min > state->child_out_buf_avail) {
253 if (state->child_out_buf != state->child_out_buf_next) {
254 memmove(state->child_out_buf, state->child_out_buf_next,
255 state->child_out_buf_avail);
256 state->child_out_buf_next = state->child_out_buf;
259 bytes_read = child_read(a,
260 state->child_out_buf + state->child_out_buf_avail,
261 state->child_out_buf_len - state->child_out_buf_avail);
262 if (bytes_read == -1)
266 state->child_out_buf_avail += bytes_read;
267 a->archive.raw_position += bytes_read;
270 *buff = state->child_out_buf_next;
271 return (state->child_out_buf_avail);
275 archive_decompressor_program_read_consume(struct archive_read *a, size_t request)
277 struct archive_decompress_program *state;
279 state = (struct archive_decompress_program *)a->decompressor->data;
281 state->child_out_buf_next += request;
282 state->child_out_buf_avail -= request;
284 a->archive.file_position += request;
289 archive_decompressor_program_finish(struct archive_read *a)
291 struct archive_decompress_program *state;
294 state = (struct archive_decompress_program *)a->decompressor->data;
296 /* Release our configuration data. */
297 free(a->decompressor->config);
298 a->decompressor->config = NULL;
300 /* Shut down the child. */
301 if (state->child_stdin != -1)
302 close(state->child_stdin);
303 if (state->child_stdout != -1)
304 close(state->child_stdout);
305 while (waitpid(state->child, &status, 0) == -1 && errno == EINTR)
308 /* Release our private data. */
309 free(state->child_out_buf);
310 free(state->description);
312 a->decompressor->data = NULL;