11ee8054977a8dff3e58ac9936f9f36a5665bcda
[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.6 2008/12/06 06:45:15 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_SIGNAL_H
42 #  include <signal.h>
43 #endif
44 #ifdef HAVE_STDLIB_H
45 #  include <stdlib.h>
46 #endif
47 #ifdef HAVE_STRING_H
48 #  include <string.h>
49 #endif
50 #ifdef HAVE_UNISTD_H
51 #  include <unistd.h>
52 #endif
53
54 #include "archive.h"
55 #include "archive_private.h"
56 #include "archive_read_private.h"
57
58 int
59 archive_read_support_compression_program(struct archive *a, const char *cmd)
60 {
61         return (archive_read_support_compression_program_signature(a, cmd, NULL, 0));
62 }
63
64
65 /* This capability is only available on POSIX systems. */
66 #if (!defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
67     !(defined(HAVE_FORK) || defined(HAVE_VFORK))) && (!defined(_WIN32) || defined(__CYGWIN__))
68
69 /*
70  * On non-Posix systems, allow the program to build, but choke if
71  * this function is actually invoked.
72  */
73 int
74 archive_read_support_compression_program_signature(struct archive *_a,
75     const char *cmd, void *signature, size_t signature_len)
76 {
77         (void)_a; /* UNUSED */
78         (void)cmd; /* UNUSED */
79         (void)signature; /* UNUSED */
80         (void)signature_len; /* UNUSED */
81
82         archive_set_error(_a, -1,
83             "External compression programs not supported on this platform");
84         return (ARCHIVE_FATAL);
85 }
86
87 int
88 __archive_read_program(struct archive_read_filter *self, const char *cmd)
89 {
90         (void)self; /* UNUSED */
91         (void)cmd; /* UNUSED */
92
93         archive_set_error(&self->archive->archive, -1,
94             "External compression programs not supported on this platform");
95         return (ARCHIVE_FATAL);
96 }
97
98 #else
99
100 #include "filter_fork.h"
101
102 /*
103  * The bidder object stores the command and the signature to watch for.
104  * The 'inhibit' entry here is used to ensure that unchecked filters never
105  * bid twice in the same pipeline.
106  */
107 struct program_bidder {
108         char *cmd;
109         void *signature;
110         size_t signature_len;
111         int inhibit;
112 };
113
114 static int      program_bidder_bid(struct archive_read_filter_bidder *,
115                     struct archive_read_filter *upstream);
116 static int      program_bidder_init(struct archive_read_filter *);
117 static int      program_bidder_free(struct archive_read_filter_bidder *);
118
119 /*
120  * The actual filter needs to track input and output data.
121  */
122 struct program_filter {
123         char            *description;
124         pid_t            child;
125         int              exit_status;
126         int              waitpid_return;
127         int              child_stdin, child_stdout;
128
129         char            *out_buf;
130         size_t           out_buf_len;
131 };
132
133 static ssize_t  program_filter_read(struct archive_read_filter *,
134                     const void **);
135 static int      program_filter_close(struct archive_read_filter *);
136
137 int
138 archive_read_support_compression_program_signature(struct archive *_a,
139     const char *cmd, const void *signature, size_t signature_len)
140 {
141         struct archive_read *a = (struct archive_read *)_a;
142         struct archive_read_filter_bidder *bidder;
143         struct program_bidder *state;
144
145         /*
146          * Get a bidder object from the read core.
147          */
148         bidder = __archive_read_get_bidder(a);
149         if (bidder == NULL)
150                 return (ARCHIVE_FATAL);
151
152         /*
153          * Allocate our private state.
154          */
155         state = (struct program_bidder *)calloc(sizeof (*state), 1);
156         if (state == NULL)
157                 return (ARCHIVE_FATAL);
158         state->cmd = strdup(cmd);
159         if (signature != NULL && signature_len > 0) {
160                 state->signature_len = signature_len;
161                 state->signature = malloc(signature_len);
162                 memcpy(state->signature, signature, signature_len);
163         }
164
165         /*
166          * Fill in the bidder object.
167          */
168         bidder->data = state;
169         bidder->bid = program_bidder_bid;
170         bidder->init = program_bidder_init;
171         bidder->options = NULL;
172         bidder->free = program_bidder_free;
173         return (ARCHIVE_OK);
174 }
175
176 static int
177 program_bidder_free(struct archive_read_filter_bidder *self)
178 {
179         struct program_bidder *state = (struct program_bidder *)self->data;
180         free(state->cmd);
181         free(state->signature);
182         free(self->data);
183         return (ARCHIVE_OK);
184 }
185
186 /*
187  * If we do have a signature, bid only if that matches.
188  *
189  * If there's no signature, we bid INT_MAX the first time
190  * we're called, then never bid again.
191  */
192 static int
193 program_bidder_bid(struct archive_read_filter_bidder *self,
194     struct archive_read_filter *upstream)
195 {
196         struct program_bidder *state = self->data;
197         const char *p;
198
199         /* If we have a signature, use that to match. */
200         if (state->signature_len > 0) {
201                 p = __archive_read_filter_ahead(upstream,
202                     state->signature_len, NULL);
203                 if (p == NULL)
204                         return (0);
205                 /* No match, so don't bid. */
206                 if (memcmp(p, state->signature, state->signature_len) != 0)
207                         return (0);
208                 return (state->signature_len * 8);
209         }
210
211         /* Otherwise, bid once and then never bid again. */
212         if (state->inhibit)
213                 return (0);
214         state->inhibit = 1;
215         return (INT_MAX);
216 }
217
218 /*
219  * Shut down the child, return ARCHIVE_OK if it exited normally.
220  *
221  * Note that the return value is sticky; if we're called again,
222  * we won't reap the child again, but we will return the same status
223  * (including error message if the child came to a bad end).
224  */
225 static int
226 child_stop(struct archive_read_filter *self, struct program_filter *state)
227 {
228         /* Close our side of the I/O with the child. */
229         if (state->child_stdin != -1) {
230                 close(state->child_stdin);
231                 state->child_stdin = -1;
232         }
233         if (state->child_stdout != -1) {
234                 close(state->child_stdout);
235                 state->child_stdout = -1;
236         }
237
238         if (state->child != 0) {
239                 /* Reap the child. */
240                 do {
241                         state->waitpid_return
242                             = waitpid(state->child, &state->exit_status, 0);
243                 } while (state->waitpid_return == -1 && errno == EINTR);
244                 state->child = 0;
245         }
246
247         if (state->waitpid_return < 0) {
248                 /* waitpid() failed?  This is ugly. */
249                 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
250                     "Child process exited badly");
251                 return (ARCHIVE_WARN);
252         }
253
254         if (WIFSIGNALED(state->exit_status)) {
255 #ifdef SIGPIPE
256                 /* If the child died because we stopped reading before
257                  * it was done, that's okay.  Some archive formats
258                  * have padding at the end that we routinely ignore. */
259                 /* The alternative to this would be to add a step
260                  * before close(child_stdout) above to read from the
261                  * child until the child has no more to write. */
262                 if (WTERMSIG(state->exit_status) == SIGPIPE)
263                         return (ARCHIVE_OK);
264 #endif
265                 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
266                     "Child process exited with signal %d",
267                     WTERMSIG(state->exit_status));
268                 return (ARCHIVE_WARN);
269         }
270
271         if (WIFEXITED(state->exit_status)) {
272                 if (WEXITSTATUS(state->exit_status) == 0)
273                         return (ARCHIVE_OK);
274
275                 archive_set_error(&self->archive->archive,
276                     ARCHIVE_ERRNO_MISC,
277                     "Child process exited with status %d",
278                     WEXITSTATUS(state->exit_status));
279                 return (ARCHIVE_WARN);
280         }
281
282         return (ARCHIVE_WARN);
283 }
284
285 /*
286  * Use select() to decide whether the child is ready for read or write.
287  */
288 static ssize_t
289 child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
290 {
291         struct program_filter *state = self->data;
292         ssize_t ret, requested, avail;
293         const char *p;
294
295         requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
296
297         for (;;) {
298                 do {
299                         ret = read(state->child_stdout, buf, requested);
300                 } while (ret == -1 && errno == EINTR);
301
302                 if (ret > 0)
303                         return (ret);
304                 if (ret == 0 || (ret == -1 && errno == EPIPE))
305                         /* Child has closed its output; reap the child
306                          * and return the status. */
307                         return (child_stop(self, state));
308                 if (ret == -1 && errno != EAGAIN)
309                         return (-1);
310
311                 if (state->child_stdin == -1) {
312                         /* Block until child has some I/O ready. */
313                         __archive_check_child(state->child_stdin,
314                             state->child_stdout);
315                         continue;
316                 }
317
318                 /* Get some more data from upstream. */
319                 p = __archive_read_filter_ahead(self->upstream, 1, &avail);
320                 if (p == NULL) {
321                         close(state->child_stdin);
322                         state->child_stdin = -1;
323                         fcntl(state->child_stdout, F_SETFL, 0);
324                         if (avail < 0)
325                                 return (avail);
326                         continue;
327                 }
328
329                 do {
330                         ret = write(state->child_stdin, p, avail);
331                 } while (ret == -1 && errno == EINTR);
332
333                 if (ret > 0) {
334                         /* Consume whatever we managed to write. */
335                         __archive_read_filter_consume(self->upstream, ret);
336                 } else if (ret == -1 && errno == EAGAIN) {
337                         /* Block until child has some I/O ready. */
338                         __archive_check_child(state->child_stdin,
339                             state->child_stdout);
340                 } else {
341                         /* Write failed. */
342                         close(state->child_stdin);
343                         state->child_stdin = -1;
344                         fcntl(state->child_stdout, F_SETFL, 0);
345                         /* If it was a bad error, we're done; otherwise
346                          * it was EPIPE or EOF, and we can still read
347                          * from the child. */
348                         if (ret == -1 && errno != EPIPE)
349                                 return (-1);
350                 }
351         }
352 }
353
354 int
355 __archive_read_program(struct archive_read_filter *self, const char *cmd)
356 {
357         struct program_filter   *state;
358         static const size_t out_buf_len = 65536;
359         char *out_buf;
360         char *description;
361         const char *prefix = "Program: ";
362
363         state = (struct program_filter *)calloc(1, sizeof(*state));
364         out_buf = (char *)malloc(out_buf_len);
365         description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
366         if (state == NULL || out_buf == NULL || description == NULL) {
367                 archive_set_error(&self->archive->archive, ENOMEM,
368                     "Can't allocate input data");
369                 free(state);
370                 free(out_buf);
371                 free(description);
372                 return (ARCHIVE_FATAL);
373         }
374
375         self->code = ARCHIVE_COMPRESSION_PROGRAM;
376         state->description = description;
377         strcpy(state->description, prefix);
378         strcat(state->description, cmd);
379         self->name = state->description;
380
381         state->out_buf = out_buf;
382         state->out_buf_len = out_buf_len;
383
384         if ((state->child = __archive_create_child(cmd,
385                  &state->child_stdin, &state->child_stdout)) == -1) {
386                 free(state->out_buf);
387                 free(state);
388                 archive_set_error(&self->archive->archive, EINVAL,
389                     "Can't initialise filter");
390                 return (ARCHIVE_FATAL);
391         }
392
393         self->data = state;
394         self->read = program_filter_read;
395         self->skip = NULL;
396         self->close = program_filter_close;
397
398         /* XXX Check that we can read at least one byte? */
399         return (ARCHIVE_OK);
400 }
401
402 static int
403 program_bidder_init(struct archive_read_filter *self)
404 {
405         struct program_bidder   *bidder_state;
406
407         bidder_state = (struct program_bidder *)self->bidder->data;
408         return (__archive_read_program(self, bidder_state->cmd));
409 }
410
411 static ssize_t
412 program_filter_read(struct archive_read_filter *self, const void **buff)
413 {
414         struct program_filter *state;
415         ssize_t bytes;
416         size_t total;
417         char *p;
418
419         state = (struct program_filter *)self->data;
420
421         total = 0;
422         p = state->out_buf;
423         while (state->child_stdout != -1 && total < state->out_buf_len) {
424                 bytes = child_read(self, p, state->out_buf_len - total);
425                 if (bytes < 0)
426                         /* No recovery is possible if we can no longer
427                          * read from the child. */
428                         return (ARCHIVE_FATAL);
429                 if (bytes == 0)
430                         /* We got EOF from the child. */
431                         break;
432                 total += bytes;
433                 p += bytes;
434         }
435
436         *buff = state->out_buf;
437         return (total);
438 }
439
440 static int
441 program_filter_close(struct archive_read_filter *self)
442 {
443         struct program_filter   *state;
444         int e;
445
446         state = (struct program_filter *)self->data;
447         e = child_stop(self, state);
448
449         /* Release our private data. */
450         free(state->out_buf);
451         free(state->description);
452         free(state);
453
454         return (e);
455 }
456
457 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */