Rename libarchive-2/ to libarchive/. No need for it to be versioned.
[dragonfly.git] / contrib / libarchive / 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.4 2008/06/15 10:45:57 kientzle Exp $");
28
29 /* This capability is only available on POSIX systems. */
30 #if !defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
31     !(defined(HAVE_FORK) || defined(HAVE_VFORK))
32
33 /*
34  * On non-Posix systems, allow the program to build, but choke if
35  * this function is actually invoked.
36  */
37 int
38 archive_read_support_compression_program(struct archive *_a, const char *cmd)
39 {
40         archive_set_error(_a, -1,
41             "External compression programs not supported on this platform");
42         return (ARCHIVE_FATAL);
43 }
44
45 #else
46
47 #ifdef HAVE_SYS_WAIT_H
48 #  include <sys/wait.h>
49 #endif
50 #ifdef HAVE_ERRNO_H
51 #  include <errno.h>
52 #endif
53 #ifdef HAVE_FCNTL_H
54 #  include <fcntl.h>
55 #endif
56 #ifdef HAVE_LIMITS_H
57 #  include <limits.h>
58 #endif
59 #ifdef HAVE_STDLIB_H
60 #  include <stdlib.h>
61 #endif
62 #ifdef HAVE_STRING_H
63 #  include <string.h>
64 #endif
65 #ifdef HAVE_UNISTD_H
66 #  include <unistd.h>
67 #endif
68
69 #include "archive.h"
70 #include "archive_private.h"
71 #include "archive_read_private.h"
72
73 #include "filter_fork.h"
74
75 struct archive_decompress_program {
76         char            *description;
77         pid_t            child;
78         int              child_stdin, child_stdout;
79
80         char            *child_out_buf;
81         char            *child_out_buf_next;
82         size_t           child_out_buf_len, child_out_buf_avail;
83
84         const char      *child_in_buf;
85         size_t           child_in_buf_avail;
86 };
87
88 static int      archive_decompressor_program_bid(const void *, size_t);
89 static int      archive_decompressor_program_finish(struct archive_read *);
90 static int      archive_decompressor_program_init(struct archive_read *,
91                     const void *, size_t);
92 static ssize_t  archive_decompressor_program_read_ahead(struct archive_read *,
93                     const void **, size_t);
94 static ssize_t  archive_decompressor_program_read_consume(struct archive_read *,
95                     size_t);
96
97 int
98 archive_read_support_compression_program(struct archive *_a, const char *cmd)
99 {
100         struct archive_read *a = (struct archive_read *)_a;
101         struct decompressor_t *decompressor;
102
103         if (cmd == NULL || *cmd == '\0')
104                 return (ARCHIVE_WARN);
105
106         decompressor = __archive_read_register_compression(a,
107                     archive_decompressor_program_bid,
108                     archive_decompressor_program_init);
109         if (decompressor == NULL)
110                 return (ARCHIVE_WARN);
111
112         decompressor->config = strdup(cmd);
113         return (ARCHIVE_OK);
114 }
115
116 /*
117  * If the user used us to register, they must really want us to
118  * handle it, so this module always bids INT_MAX.
119  */
120 static int
121 archive_decompressor_program_bid(const void *buff, size_t len)
122 {
123         (void)buff; /* UNUSED */
124         (void)len; /* UNUSED */
125
126         return (INT_MAX); /* Default: We'll take it. */
127 }
128
129 static ssize_t
130 child_read(struct archive_read *a, char *buf, size_t buf_len)
131 {
132         struct archive_decompress_program *state = a->decompressor->data;
133         ssize_t ret, requested;
134         const void *child_buf;
135
136         if (state->child_stdout == -1)
137                 return (-1);
138
139         if (buf_len == 0)
140                 return (-1);
141
142 restart_read:
143         requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
144
145         do {
146                 ret = read(state->child_stdout, buf, requested);
147         } while (ret == -1 && errno == EINTR);
148
149         if (ret > 0)
150                 return (ret);
151         if (ret == 0 || (ret == -1 && errno == EPIPE)) {
152                 close(state->child_stdout);
153                 state->child_stdout = -1;
154                 return (0);
155         }
156         if (ret == -1 && errno != EAGAIN)
157                 return (-1);
158
159         if (state->child_in_buf_avail == 0) {
160                 child_buf = state->child_in_buf;
161                 ret = (a->client_reader)(&a->archive,
162                     a->client_data,&child_buf);
163                 state->child_in_buf = (const char *)child_buf;
164
165                 if (ret < 0) {
166                         close(state->child_stdin);
167                         state->child_stdin = -1;
168                         fcntl(state->child_stdout, F_SETFL, 0);
169                         return (-1);
170                 }
171                 if (ret == 0) {
172                         close(state->child_stdin);
173                         state->child_stdin = -1;
174                         fcntl(state->child_stdout, F_SETFL, 0);
175                         goto restart_read;
176                 }
177                 state->child_in_buf_avail = ret;
178         }
179
180         if (state->child_stdin == -1) {
181                 fcntl(state->child_stdout, F_SETFL, 0);
182                 __archive_check_child(state->child_stdin, state->child_stdout);
183                 goto restart_read;
184         }
185
186         do {
187                 ret = write(state->child_stdin, state->child_in_buf,
188                     state->child_in_buf_avail);
189         } while (ret == -1 && errno == EINTR);
190
191         if (ret > 0) {
192                 state->child_in_buf += ret;
193                 state->child_in_buf_avail -= ret;
194                 goto restart_read;
195         } else if (ret == -1 && errno == EAGAIN) {
196                 __archive_check_child(state->child_stdin, state->child_stdout);
197                 goto restart_read;
198         } else if (ret == 0 || (ret == -1 && errno == EPIPE)) {
199                 close(state->child_stdin);
200                 state->child_stdin = -1;
201                 fcntl(state->child_stdout, F_SETFL, 0);
202                 goto restart_read;
203         } else {
204                 close(state->child_stdin);
205                 state->child_stdin = -1;
206                 fcntl(state->child_stdout, F_SETFL, 0);
207                 return (-1);
208         }
209 }
210
211 static int
212 archive_decompressor_program_init(struct archive_read *a, const void *buff, size_t n)
213 {
214         struct archive_decompress_program       *state;
215         const char *cmd = a->decompressor->config;
216         const char *prefix = "Program: ";
217
218
219         state = (struct archive_decompress_program *)malloc(sizeof(*state));
220         if (!state) {
221                 archive_set_error(&a->archive, ENOMEM,
222                     "Can't allocate input data");
223                 return (ARCHIVE_FATAL);
224         }
225
226         a->archive.compression_code = ARCHIVE_COMPRESSION_PROGRAM;
227         state->description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
228         strcpy(state->description, prefix);
229         strcat(state->description, cmd);
230         a->archive.compression_name = state->description;
231
232         state->child_out_buf_next = state->child_out_buf = malloc(65536);
233         if (!state->child_out_buf) {
234                 free(state);
235                 archive_set_error(&a->archive, ENOMEM,
236                     "Can't allocate filter buffer");
237                 return (ARCHIVE_FATAL);
238         }
239         state->child_out_buf_len = 65536;
240         state->child_out_buf_avail = 0;
241
242         state->child_in_buf = buff;
243         state->child_in_buf_avail = n;
244
245         if ((state->child = __archive_create_child(cmd,
246                  &state->child_stdin, &state->child_stdout)) == -1) {
247                 free(state->child_out_buf);
248                 free(state);
249                 archive_set_error(&a->archive, EINVAL,
250                     "Can't initialise filter");
251                 return (ARCHIVE_FATAL);
252         }
253
254         a->decompressor->data = state;
255         a->decompressor->read_ahead = archive_decompressor_program_read_ahead;
256         a->decompressor->consume = archive_decompressor_program_read_consume;
257         a->decompressor->skip = NULL;
258         a->decompressor->finish = archive_decompressor_program_finish;
259
260         /* XXX Check that we can read at least one byte? */
261         return (ARCHIVE_OK);
262 }
263
264 static ssize_t
265 archive_decompressor_program_read_ahead(struct archive_read *a, const void **buff,
266     size_t min)
267 {
268         struct archive_decompress_program *state;
269         ssize_t bytes_read;
270
271         state = (struct archive_decompress_program *)a->decompressor->data;
272
273         if (min > state->child_out_buf_len)
274                 min = state->child_out_buf_len;
275
276         while (state->child_stdout != -1 && min > state->child_out_buf_avail) {
277                 if (state->child_out_buf != state->child_out_buf_next) {
278                         memmove(state->child_out_buf, state->child_out_buf_next,
279                             state->child_out_buf_avail);
280                         state->child_out_buf_next = state->child_out_buf;
281                 }
282
283                 bytes_read = child_read(a,
284                     state->child_out_buf + state->child_out_buf_avail,
285                     state->child_out_buf_len - state->child_out_buf_avail);
286                 if (bytes_read == -1)
287                         return (-1);
288                 if (bytes_read == 0)
289                         break;
290                 state->child_out_buf_avail += bytes_read;
291                 a->archive.raw_position += bytes_read;
292         }
293
294         *buff = state->child_out_buf_next;
295         return (state->child_out_buf_avail);
296 }
297
298 static ssize_t
299 archive_decompressor_program_read_consume(struct archive_read *a, size_t request)
300 {
301         struct archive_decompress_program *state;
302
303         state = (struct archive_decompress_program *)a->decompressor->data;
304
305         state->child_out_buf_next += request;
306         state->child_out_buf_avail -= request;
307
308         a->archive.file_position += request;
309         return (request);
310 }
311
312 static int
313 archive_decompressor_program_finish(struct archive_read *a)
314 {
315         struct archive_decompress_program       *state;
316         int status;
317
318         state = (struct archive_decompress_program *)a->decompressor->data;
319
320         /* Release our configuration data. */
321         free(a->decompressor->config);
322         a->decompressor->config = NULL;
323
324         /* Shut down the child. */
325         if (state->child_stdin != -1)
326                 close(state->child_stdin);
327         if (state->child_stdout != -1)
328                 close(state->child_stdout);
329         while (waitpid(state->child, &status, 0) == -1 && errno == EINTR)
330                 continue;
331
332         /* Release our private data. */
333         free(state->child_out_buf);
334         free(state->description);
335         free(state);
336         a->decompressor->data = NULL;
337
338         return (ARCHIVE_OK);
339 }
340
341 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */