2 * Copyright (c) 2007 Joerg Sonnenberger
3 * Copyright (c) 2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_program.c 201104 2009-12-28 03:14:30Z kientzle $");
30 #ifdef HAVE_SYS_WAIT_H
31 # include <sys/wait.h>
47 #include "archive_private.h"
48 #include "archive_string.h"
49 #include "archive_write_private.h"
50 #include "filter_fork.h"
52 #if ARCHIVE_VERSION_NUMBER < 4000000
54 archive_write_set_compression_program(struct archive *a, const char *cmd)
56 __archive_write_filters_free(a);
57 return (archive_write_add_filter_program(a, cmd));
61 struct archive_write_program_data {
62 #if defined(_WIN32) && !defined(__CYGWIN__)
67 int child_stdin, child_stdout;
70 size_t child_buf_len, child_buf_avail;
75 struct archive_write_program_data *pdata;
76 struct archive_string description;
80 static int archive_compressor_program_open(struct archive_write_filter *);
81 static int archive_compressor_program_write(struct archive_write_filter *,
82 const void *, size_t);
83 static int archive_compressor_program_close(struct archive_write_filter *);
84 static int archive_compressor_program_free(struct archive_write_filter *);
87 * Add a filter to this write handle that passes all data through an
91 archive_write_add_filter_program(struct archive *_a, const char *cmd)
93 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
94 struct private_data *data;
95 static const char prefix[] = "Program: ";
97 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
98 ARCHIVE_STATE_NEW, "archive_write_add_filter_program");
100 f->data = calloc(1, sizeof(*data));
103 data = (struct private_data *)f->data;
105 data->cmd = strdup(cmd);
106 if (data->cmd == NULL)
109 data->pdata = __archive_write_program_allocate(cmd);
110 if (data->pdata == NULL)
113 /* Make up a description string. */
114 if (archive_string_ensure(&data->description,
115 strlen(prefix) + strlen(cmd) + 1) == NULL)
117 archive_strcpy(&data->description, prefix);
118 archive_strcat(&data->description, cmd);
120 f->name = data->description.s;
121 f->code = ARCHIVE_FILTER_PROGRAM;
122 f->open = archive_compressor_program_open;
123 f->write = archive_compressor_program_write;
124 f->close = archive_compressor_program_close;
125 f->free = archive_compressor_program_free;
128 archive_compressor_program_free(f);
129 archive_set_error(_a, ENOMEM,
130 "Can't allocate memory for filter program");
131 return (ARCHIVE_FATAL);
135 archive_compressor_program_open(struct archive_write_filter *f)
137 struct private_data *data = (struct private_data *)f->data;
139 return __archive_write_program_open(f, data->pdata, data->cmd);
143 archive_compressor_program_write(struct archive_write_filter *f,
144 const void *buff, size_t length)
146 struct private_data *data = (struct private_data *)f->data;
148 return __archive_write_program_write(f, data->pdata, buff, length);
152 archive_compressor_program_close(struct archive_write_filter *f)
154 struct private_data *data = (struct private_data *)f->data;
156 return __archive_write_program_close(f, data->pdata);
160 archive_compressor_program_free(struct archive_write_filter *f)
162 struct private_data *data = (struct private_data *)f->data;
166 archive_string_free(&data->description);
167 __archive_write_program_free(data->pdata);
175 * Allocate resources for executing an external program.
177 struct archive_write_program_data *
178 __archive_write_program_allocate(const char *program)
180 struct archive_write_program_data *data;
182 data = calloc(1, sizeof(struct archive_write_program_data));
185 data->child_stdin = -1;
186 data->child_stdout = -1;
187 data->program_name = strdup(program);
192 * Release the resources.
195 __archive_write_program_free(struct archive_write_program_data *data)
199 free(data->program_name);
200 free(data->child_buf);
207 __archive_write_program_open(struct archive_write_filter *f,
208 struct archive_write_program_data *data, const char *cmd)
212 if (data->child_buf == NULL) {
213 data->child_buf_len = 65536;
214 data->child_buf_avail = 0;
215 data->child_buf = malloc(data->child_buf_len);
217 if (data->child_buf == NULL) {
218 archive_set_error(f->archive, ENOMEM,
219 "Can't allocate compression buffer");
220 return (ARCHIVE_FATAL);
224 ret = __archive_create_child(cmd, &data->child_stdin,
225 &data->child_stdout, &data->child);
226 if (ret != ARCHIVE_OK) {
227 archive_set_error(f->archive, EINVAL,
228 "Can't launch external program: %s", cmd);
229 return (ARCHIVE_FATAL);
235 child_write(struct archive_write_filter *f,
236 struct archive_write_program_data *data, const char *buf, size_t buf_len)
240 if (data->child_stdin == -1)
248 ret = write(data->child_stdin, buf, buf_len);
249 } while (ret == -1 && errno == EINTR);
254 close(data->child_stdin);
255 data->child_stdin = -1;
256 fcntl(data->child_stdout, F_SETFL, 0);
259 if (ret == -1 && errno != EAGAIN)
262 if (data->child_stdout == -1) {
263 fcntl(data->child_stdin, F_SETFL, 0);
264 __archive_check_child(data->child_stdin,
270 ret = read(data->child_stdout,
271 data->child_buf + data->child_buf_avail,
272 data->child_buf_len - data->child_buf_avail);
273 } while (ret == -1 && errno == EINTR);
275 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
276 close(data->child_stdout);
277 data->child_stdout = -1;
278 fcntl(data->child_stdin, F_SETFL, 0);
281 if (ret == -1 && errno == EAGAIN) {
282 __archive_check_child(data->child_stdin,
289 data->child_buf_avail += ret;
291 ret = __archive_write_filter(f->next_filter,
292 data->child_buf, data->child_buf_avail);
293 if (ret != ARCHIVE_OK)
295 data->child_buf_avail = 0;
300 * Write data to the filter stream.
303 __archive_write_program_write(struct archive_write_filter *f,
304 struct archive_write_program_data *data, const void *buff, size_t length)
309 if (data->child == 0)
314 ret = child_write(f, data, buf, length);
315 if (ret == -1 || ret == 0) {
316 archive_set_error(f->archive, EIO,
317 "Can't write to program: %s", data->program_name);
318 return (ARCHIVE_FATAL);
327 * Finish the filtering...
330 __archive_write_program_close(struct archive_write_filter *f,
331 struct archive_write_program_data *data)
336 if (data->child == 0)
340 close(data->child_stdin);
341 data->child_stdin = -1;
342 fcntl(data->child_stdout, F_SETFL, 0);
346 bytes_read = read(data->child_stdout,
347 data->child_buf + data->child_buf_avail,
348 data->child_buf_len - data->child_buf_avail);
349 } while (bytes_read == -1 && errno == EINTR);
351 if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
354 if (bytes_read == -1) {
355 archive_set_error(f->archive, errno,
356 "Error reading from program: %s", data->program_name);
360 data->child_buf_avail += bytes_read;
362 ret = __archive_write_filter(f->next_filter,
363 data->child_buf, data->child_buf_avail);
364 if (ret != ARCHIVE_OK) {
368 data->child_buf_avail = 0;
372 /* Shut down the child. */
373 if (data->child_stdin != -1)
374 close(data->child_stdin);
375 if (data->child_stdout != -1)
376 close(data->child_stdout);
377 while (waitpid(data->child, &status, 0) == -1 && errno == EINTR)
379 #if defined(_WIN32) && !defined(__CYGWIN__)
380 CloseHandle(data->child);
385 archive_set_error(f->archive, EIO,
386 "Error closing program: %s", data->program_name);