Synchronous libarchive to 2.2.4 from FreeBSD, including fixes related to
[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.1 2007/05/29 01:00:19 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
117         if (state->child_stdout == -1)
118                 return (-1);
119
120         if (buf_len == 0)
121                 return (-1);
122
123 restart_read:
124         requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
125
126         do {
127                 ret = read(state->child_stdout, buf, requested);
128         } while (ret == -1 && errno == EINTR);
129
130         if (ret > 0)
131                 return (ret);
132         if (ret == 0 || (ret == -1 && errno == EPIPE)) {
133                 close(state->child_stdout);
134                 state->child_stdout = -1;
135                 return (0);
136         }
137         if (ret == -1 && errno != EAGAIN)
138                 return (-1);
139
140         if (state->child_in_buf_avail == 0) {
141                 ret = (a->client_reader)(&a->archive,
142                     a->client_data, (const void **)&state->child_in_buf);
143
144                 if (ret < 0) {
145                         close(state->child_stdin);
146                         state->child_stdin = -1;
147                         fcntl(state->child_stdout, F_SETFL, 0);
148                         return (-1);
149                 }
150                 if (ret == 0) {
151                         close(state->child_stdin);
152                         state->child_stdin = -1;
153                         fcntl(state->child_stdout, F_SETFL, 0);
154                         goto restart_read;
155                 }
156                 state->child_in_buf_avail = ret;
157         }
158
159         do {
160                 ret = write(state->child_stdin, state->child_in_buf,
161                     state->child_in_buf_avail);
162         } while (ret == -1 && errno == EINTR);
163
164         if (ret > 0) {
165                 state->child_in_buf += ret;
166                 state->child_in_buf_avail -= ret;
167                 goto restart_read;
168         } else if (ret == -1 && errno == EAGAIN) {
169                 __archive_check_child(state->child_stdin, state->child_stdout);
170                 goto restart_read;
171         } else if (ret == 0 || (ret == -1 && errno == EPIPE)) {
172                 close(state->child_stdin);
173                 state->child_stdout = -1;
174                 fcntl(state->child_stdout, F_SETFL, 0);
175                 goto restart_read;
176         } else {
177                 close(state->child_stdin);
178                 state->child_stdin = -1;
179                 fcntl(state->child_stdout, F_SETFL, 0);
180                 return (-1);
181         }
182 }
183
184 static int
185 archive_decompressor_program_init(struct archive_read *a, const void *buff, size_t n)
186 {
187         struct archive_decompress_program       *state;
188         const char *cmd = a->decompressor->config;
189         const char *prefix = "Program: ";
190
191
192         state = (struct archive_decompress_program *)malloc(sizeof(*state));
193         if (!state) {
194                 archive_set_error(&a->archive, ENOMEM,
195                     "Can't allocate input data");
196                 return (ARCHIVE_FATAL);
197         }
198
199         a->archive.compression_code = ARCHIVE_COMPRESSION_PROGRAM;
200         state->description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
201         strcpy(state->description, prefix);
202         strcat(state->description, cmd);
203         a->archive.compression_name = state->description;
204
205         state->child_out_buf_next = state->child_out_buf = malloc(65536);
206         if (!state->child_out_buf) {
207                 free(state);
208                 archive_set_error(&a->archive, ENOMEM,
209                     "Can't allocate filter buffer");
210                 return (ARCHIVE_FATAL);
211         }
212         state->child_out_buf_len = 65536;
213         state->child_out_buf_avail = 0;
214
215         state->child_in_buf = buff;
216         state->child_in_buf_avail = n;
217
218         if ((state->child = __archive_create_child(cmd,
219                  &state->child_stdin, &state->child_stdout)) == -1) {
220                 free(state->child_out_buf);
221                 free(state);
222                 archive_set_error(&a->archive, EINVAL,
223                     "Can't initialise filter");
224                 return (ARCHIVE_FATAL);
225         }
226
227         a->decompressor->data = state;
228         a->decompressor->read_ahead = archive_decompressor_program_read_ahead;
229         a->decompressor->consume = archive_decompressor_program_read_consume;
230         a->decompressor->skip = NULL;
231         a->decompressor->finish = archive_decompressor_program_finish;
232
233         /* XXX Check that we can read at least one byte? */
234         return (ARCHIVE_OK);
235 }
236
237 static ssize_t
238 archive_decompressor_program_read_ahead(struct archive_read *a, const void **buff,
239     size_t min)
240 {
241         struct archive_decompress_program *state;
242         ssize_t bytes_read;
243
244         state = (struct archive_decompress_program *)a->decompressor->data;
245
246         if (min > state->child_out_buf_len)
247                 min = state->child_out_buf_len;
248
249         while (state->child_stdout != -1 && min > state->child_out_buf_avail) {
250                 if (state->child_out_buf != state->child_out_buf_next) {
251                         memmove(state->child_out_buf, state->child_out_buf_next,
252                             state->child_out_buf_avail);
253                         state->child_out_buf_next = state->child_out_buf;
254                 }
255
256                 bytes_read = child_read(a,
257                     state->child_out_buf + state->child_out_buf_avail,
258                     state->child_out_buf_len - state->child_out_buf_avail);
259                 if (bytes_read == -1)
260                         return (-1);
261                 if (bytes_read == 0)
262                         break;
263                 state->child_out_buf_avail += bytes_read;
264                 a->archive.raw_position += bytes_read;
265         }
266
267         *buff = state->child_out_buf_next;
268         return (state->child_out_buf_avail);
269 }
270
271 static ssize_t
272 archive_decompressor_program_read_consume(struct archive_read *a, size_t request)
273 {
274         struct archive_decompress_program *state;
275
276         state = (struct archive_decompress_program *)a->decompressor->data;
277
278         state->child_out_buf_next += request;
279         state->child_out_buf_avail -= request;
280
281         a->archive.file_position += request;
282         return (request);
283 }
284
285 static int
286 archive_decompressor_program_finish(struct archive_read *a)
287 {
288         struct archive_decompress_program       *state;
289         int status;
290
291         state = (struct archive_decompress_program *)a->decompressor->data;
292
293         /* Release our configuration data. */
294         free(a->decompressor->config);
295         a->decompressor->config = NULL;
296
297         /* Shut down the child. */
298         if (state->child_stdin != -1)
299                 close(state->child_stdin);
300         if (state->child_stdout != -1)
301                 close(state->child_stdout);
302         while (waitpid(state->child, &status, 0) == -1 && errno == EINTR)
303                 continue;
304
305         /* Release our private data. */
306         free(state->child_out_buf);
307         free(state->description);
308         free(state);
309         a->decompressor->data = NULL;
310
311         return (ARCHIVE_OK);
312 }