Import libarchive 2.2.6.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_support_compression_program.c
1 /*-
2  * Copyright (c) 2007 Joerg Sonnenberger
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: src/lib/libarchive/archive_read_support_compression_program.c,v 1.2 2007/07/20 01:28:50 kientzle Exp $");
28
29 #ifdef HAVE_SYS_WAIT_H
30 #  include <sys/wait.h>
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #  include <errno.h>
34 #endif
35 #ifdef HAVE_FCNTL_H
36 #  include <fcntl.h>
37 #endif
38 #ifdef HAVE_LIMITS_H
39 #  include <limits.h>
40 #endif
41 #ifdef HAVE_STDLIB_H
42 #  include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #  include <string.h>
46 #endif
47 #ifdef HAVE_UNISTD_H
48 #  include <unistd.h>
49 #endif
50
51 #include "archive.h"
52 #include "archive_private.h"
53 #include "archive_read_private.h"
54
55 #include "filter_fork.h"
56
57 struct archive_decompress_program {
58         char            *description;
59         pid_t            child;
60         int              child_stdin, child_stdout;
61
62         char            *child_out_buf;
63         char            *child_out_buf_next;
64         size_t           child_out_buf_len, child_out_buf_avail;
65
66         const char      *child_in_buf;
67         size_t           child_in_buf_avail;
68 };
69
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 *,
77                     size_t);
78
79 int
80 archive_read_support_compression_program(struct archive *_a, const char *cmd)
81 {
82         struct archive_read *a = (struct archive_read *)_a;
83         struct decompressor_t *decompressor;
84
85         if (cmd == NULL || *cmd == '\0')
86                 return (ARCHIVE_WARN);
87
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);
93
94         decompressor->config = strdup(cmd);
95         return (ARCHIVE_OK);
96 }
97
98 /*
99  * If the user used us to register, they must really want us to
100  * handle it, so this module always bids INT_MAX.
101  */
102 static int
103 archive_decompressor_program_bid(const void *buff, size_t len)
104 {
105         (void)buff; /* UNUSED */
106         (void)len; /* UNUSED */
107
108         return (INT_MAX); /* Default: We'll take it. */
109 }
110
111 static ssize_t
112 child_read(struct archive_read *a, char *buf, size_t buf_len)
113 {
114         struct archive_decompress_program *state = a->decompressor->data;
115         ssize_t ret, requested;
116         const void *child_buf;
117
118         if (state->child_stdout == -1)
119                 return (-1);
120
121         if (buf_len == 0)
122                 return (-1);
123
124 restart_read:
125         requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
126
127         do {
128                 ret = read(state->child_stdout, buf, requested);
129         } while (ret == -1 && errno == EINTR);
130
131         if (ret > 0)
132                 return (ret);
133         if (ret == 0 || (ret == -1 && errno == EPIPE)) {
134                 close(state->child_stdout);
135                 state->child_stdout = -1;
136                 return (0);
137         }
138         if (ret == -1 && errno != EAGAIN)
139                 return (-1);
140
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;
146
147                 if (ret < 0) {
148                         close(state->child_stdin);
149                         state->child_stdin = -1;
150                         fcntl(state->child_stdout, F_SETFL, 0);
151                         return (-1);
152                 }
153                 if (ret == 0) {
154                         close(state->child_stdin);
155                         state->child_stdin = -1;
156                         fcntl(state->child_stdout, F_SETFL, 0);
157                         goto restart_read;
158                 }
159                 state->child_in_buf_avail = ret;
160         }
161
162         do {
163                 ret = write(state->child_stdin, state->child_in_buf,
164                     state->child_in_buf_avail);
165         } while (ret == -1 && errno == EINTR);
166
167         if (ret > 0) {
168                 state->child_in_buf += ret;
169                 state->child_in_buf_avail -= ret;
170                 goto restart_read;
171         } else if (ret == -1 && errno == EAGAIN) {
172                 __archive_check_child(state->child_stdin, state->child_stdout);
173                 goto restart_read;
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);
178                 goto restart_read;
179         } else {
180                 close(state->child_stdin);
181                 state->child_stdin = -1;
182                 fcntl(state->child_stdout, F_SETFL, 0);
183                 return (-1);
184         }
185 }
186
187 static int
188 archive_decompressor_program_init(struct archive_read *a, const void *buff, size_t n)
189 {
190         struct archive_decompress_program       *state;
191         const char *cmd = a->decompressor->config;
192         const char *prefix = "Program: ";
193
194
195         state = (struct archive_decompress_program *)malloc(sizeof(*state));
196         if (!state) {
197                 archive_set_error(&a->archive, ENOMEM,
198                     "Can't allocate input data");
199                 return (ARCHIVE_FATAL);
200         }
201
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;
207
208         state->child_out_buf_next = state->child_out_buf = malloc(65536);
209         if (!state->child_out_buf) {
210                 free(state);
211                 archive_set_error(&a->archive, ENOMEM,
212                     "Can't allocate filter buffer");
213                 return (ARCHIVE_FATAL);
214         }
215         state->child_out_buf_len = 65536;
216         state->child_out_buf_avail = 0;
217
218         state->child_in_buf = buff;
219         state->child_in_buf_avail = n;
220
221         if ((state->child = __archive_create_child(cmd,
222                  &state->child_stdin, &state->child_stdout)) == -1) {
223                 free(state->child_out_buf);
224                 free(state);
225                 archive_set_error(&a->archive, EINVAL,
226                     "Can't initialise filter");
227                 return (ARCHIVE_FATAL);
228         }
229
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;
235
236         /* XXX Check that we can read at least one byte? */
237         return (ARCHIVE_OK);
238 }
239
240 static ssize_t
241 archive_decompressor_program_read_ahead(struct archive_read *a, const void **buff,
242     size_t min)
243 {
244         struct archive_decompress_program *state;
245         ssize_t bytes_read;
246
247         state = (struct archive_decompress_program *)a->decompressor->data;
248
249         if (min > state->child_out_buf_len)
250                 min = state->child_out_buf_len;
251
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;
257                 }
258
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)
263                         return (-1);
264                 if (bytes_read == 0)
265                         break;
266                 state->child_out_buf_avail += bytes_read;
267                 a->archive.raw_position += bytes_read;
268         }
269
270         *buff = state->child_out_buf_next;
271         return (state->child_out_buf_avail);
272 }
273
274 static ssize_t
275 archive_decompressor_program_read_consume(struct archive_read *a, size_t request)
276 {
277         struct archive_decompress_program *state;
278
279         state = (struct archive_decompress_program *)a->decompressor->data;
280
281         state->child_out_buf_next += request;
282         state->child_out_buf_avail -= request;
283
284         a->archive.file_position += request;
285         return (request);
286 }
287
288 static int
289 archive_decompressor_program_finish(struct archive_read *a)
290 {
291         struct archive_decompress_program       *state;
292         int status;
293
294         state = (struct archive_decompress_program *)a->decompressor->data;
295
296         /* Release our configuration data. */
297         free(a->decompressor->config);
298         a->decompressor->config = NULL;
299
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)
306                 continue;
307
308         /* Release our private data. */
309         free(state->child_out_buf);
310         free(state->description);
311         free(state);
312         a->decompressor->data = NULL;
313
314         return (ARCHIVE_OK);
315 }