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