2 * Copyright (c) 2007 Joerg Sonnenberger
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_program.c 201104 2009-12-28 03:14:30Z kientzle $");
29 #ifdef HAVE_SYS_WAIT_H
30 # include <sys/wait.h>
46 #include "archive_private.h"
47 #include "archive_write_private.h"
49 #if ARCHIVE_VERSION_NUMBER < 4000000
51 archive_write_set_compression_program(struct archive *a, const char *cmd)
53 __archive_write_filters_free(a);
54 return (archive_write_add_filter_program(a, cmd));
58 /* This capability is only available on POSIX systems. */
59 #if (!defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
60 !(defined(HAVE_FORK) || defined(HAVE_VFORK))) && (!defined(_WIN32) || defined(__CYGWIN__))
63 * On non-Posix systems, allow the program to build, but choke if
64 * this function is actually invoked.
67 archive_write_add_filter_program(struct archive *_a, const char *cmd)
69 archive_set_error(_a, -1,
70 "External compression programs not supported on this platform");
71 return (ARCHIVE_FATAL);
76 #include "filter_fork.h"
82 int child_stdin, child_stdout;
85 size_t child_buf_len, child_buf_avail;
88 static int archive_compressor_program_open(struct archive_write_filter *);
89 static int archive_compressor_program_write(struct archive_write_filter *,
90 const void *, size_t);
91 static int archive_compressor_program_close(struct archive_write_filter *);
92 static int archive_compressor_program_free(struct archive_write_filter *);
95 * Add a filter to this write handle that passes all data through an
99 archive_write_add_filter_program(struct archive *_a, const char *cmd)
101 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
102 struct archive_write *a = (struct archive_write *)_a;
103 struct private_data *data;
104 static const char *prefix = "Program: ";
105 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
106 ARCHIVE_STATE_NEW, "archive_write_add_filter_program");
107 data = calloc(1, sizeof(*data));
109 archive_set_error(&a->archive, ENOMEM, "Out of memory");
110 return (ARCHIVE_FATAL);
112 data->cmd = strdup(cmd);
113 data->description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
114 strcpy(data->description, prefix);
115 strcat(data->description, cmd);
117 f->name = data->description;
119 f->open = &archive_compressor_program_open;
120 f->code = ARCHIVE_COMPRESSION_PROGRAM;
128 archive_compressor_program_open(struct archive_write_filter *f)
130 struct private_data *data = (struct private_data *)f->data;
133 ret = __archive_write_open_filter(f->next_filter);
134 if (ret != ARCHIVE_OK)
137 if (data->child_buf == NULL) {
138 data->child_buf_len = 65536;
139 data->child_buf_avail = 0;
140 data->child_buf = malloc(data->child_buf_len);
142 if (data->child_buf == NULL) {
143 archive_set_error(f->archive, ENOMEM,
144 "Can't allocate compression buffer");
145 return (ARCHIVE_FATAL);
149 if ((data->child = __archive_create_child(data->cmd,
150 &data->child_stdin, &data->child_stdout)) == -1) {
151 archive_set_error(f->archive, EINVAL,
152 "Can't initialise filter");
153 return (ARCHIVE_FATAL);
156 f->write = archive_compressor_program_write;
157 f->close = archive_compressor_program_close;
158 f->free = archive_compressor_program_free;
163 child_write(struct archive_write_filter *f, const char *buf, size_t buf_len)
165 struct private_data *data = f->data;
168 if (data->child_stdin == -1)
176 ret = write(data->child_stdin, buf, buf_len);
177 } while (ret == -1 && errno == EINTR);
182 close(data->child_stdin);
183 data->child_stdin = -1;
184 fcntl(data->child_stdout, F_SETFL, 0);
187 if (ret == -1 && errno != EAGAIN)
190 if (data->child_stdout == -1) {
191 fcntl(data->child_stdin, F_SETFL, 0);
192 __archive_check_child(data->child_stdin,
198 ret = read(data->child_stdout,
199 data->child_buf + data->child_buf_avail,
200 data->child_buf_len - data->child_buf_avail);
201 } while (ret == -1 && errno == EINTR);
203 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
204 close(data->child_stdout);
205 data->child_stdout = -1;
206 fcntl(data->child_stdin, F_SETFL, 0);
209 if (ret == -1 && errno == EAGAIN) {
210 __archive_check_child(data->child_stdin,
217 data->child_buf_avail += ret;
219 ret = __archive_write_filter(f->next_filter,
220 data->child_buf, data->child_buf_avail);
224 if ((size_t)ret < data->child_buf_avail) {
225 memmove(data->child_buf, data->child_buf + ret,
226 data->child_buf_avail - ret);
228 data->child_buf_avail -= ret;
233 * Write data to the compressed stream.
236 archive_compressor_program_write(struct archive_write_filter *f,
237 const void *buff, size_t length)
244 ret = child_write(f, buf, length);
245 if (ret == -1 || ret == 0) {
246 archive_set_error(f->archive, EIO,
247 "Can't write to filter");
248 return (ARCHIVE_FATAL);
258 * Finish the compression...
261 archive_compressor_program_close(struct archive_write_filter *f)
263 struct private_data *data = (struct private_data *)f->data;
268 close(data->child_stdin);
269 data->child_stdin = -1;
270 fcntl(data->child_stdout, F_SETFL, 0);
274 bytes_read = read(data->child_stdout,
275 data->child_buf + data->child_buf_avail,
276 data->child_buf_len - data->child_buf_avail);
277 } while (bytes_read == -1 && errno == EINTR);
279 if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
282 if (bytes_read == -1) {
283 archive_set_error(f->archive, errno,
284 "Read from filter failed unexpectedly.");
288 data->child_buf_avail += bytes_read;
290 ret = __archive_write_filter(f->next_filter,
291 data->child_buf, data->child_buf_avail);
292 if (ret != ARCHIVE_OK) {
296 data->child_buf_avail = 0;
300 /* Shut down the child. */
301 if (data->child_stdin != -1)
302 close(data->child_stdin);
303 if (data->child_stdout != -1)
304 close(data->child_stdout);
305 while (waitpid(data->child, &status, 0) == -1 && errno == EINTR)
309 archive_set_error(f->archive, EIO,
310 "Filter exited with failure.");
313 r1 = __archive_write_close_filter(f->next_filter);
314 return (r1 < ret ? r1 : ret);
318 archive_compressor_program_free(struct archive_write_filter *f)
320 struct private_data *data = (struct private_data *)f->data;
322 free(data->description);
323 free(data->child_buf);
329 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */