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, data->child_stdout);
197 ret = read(data->child_stdout,
198 data->child_buf + data->child_buf_avail,
199 data->child_buf_len - data->child_buf_avail);
200 } while (ret == -1 && errno == EINTR);
202 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
203 close(data->child_stdout);
204 data->child_stdout = -1;
205 fcntl(data->child_stdin, F_SETFL, 0);
208 if (ret == -1 && errno == EAGAIN) {
209 __archive_check_child(data->child_stdin, data->child_stdout);
215 data->child_buf_avail += ret;
217 ret = __archive_write_filter(f->next_filter,
218 data->child_buf, data->child_buf_avail);
222 if ((size_t)ret < data->child_buf_avail) {
223 memmove(data->child_buf, data->child_buf + ret,
224 data->child_buf_avail - ret);
226 data->child_buf_avail -= ret;
231 * Write data to the compressed stream.
234 archive_compressor_program_write(struct archive_write_filter *f,
235 const void *buff, size_t length)
242 ret = child_write(f, buf, length);
243 if (ret == -1 || ret == 0) {
244 archive_set_error(f->archive, EIO,
245 "Can't write to filter");
246 return (ARCHIVE_FATAL);
256 * Finish the compression...
259 archive_compressor_program_close(struct archive_write_filter *f)
261 struct private_data *data = (struct private_data *)f->data;
266 close(data->child_stdin);
267 data->child_stdin = -1;
268 fcntl(data->child_stdout, F_SETFL, 0);
272 bytes_read = read(data->child_stdout,
273 data->child_buf + data->child_buf_avail,
274 data->child_buf_len - data->child_buf_avail);
275 } while (bytes_read == -1 && errno == EINTR);
277 if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
280 if (bytes_read == -1) {
281 archive_set_error(f->archive, errno,
282 "Read from filter failed unexpectedly.");
286 data->child_buf_avail += bytes_read;
288 ret = __archive_write_filter(f->next_filter,
289 data->child_buf, data->child_buf_avail);
290 if (ret != ARCHIVE_OK) {
294 data->child_buf_avail = 0;
298 /* Shut down the child. */
299 if (data->child_stdin != -1)
300 close(data->child_stdin);
301 if (data->child_stdout != -1)
302 close(data->child_stdout);
303 while (waitpid(data->child, &status, 0) == -1 && errno == EINTR)
307 archive_set_error(f->archive, EIO,
308 "Filter exited with failure.");
311 r1 = __archive_write_close_filter(f->next_filter);
312 return (r1 < ret ? r1 : ret);
316 archive_compressor_program_free(struct archive_write_filter *f)
318 struct private_data *data = (struct private_data *)f->data;
320 free(data->description);
321 free(data->child_buf);
327 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */