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;
74 struct archive_write_program_data *pdata;
75 struct archive_string description;
79 static int archive_compressor_program_open(struct archive_write_filter *);
80 static int archive_compressor_program_write(struct archive_write_filter *,
81 const void *, size_t);
82 static int archive_compressor_program_close(struct archive_write_filter *);
83 static int archive_compressor_program_free(struct archive_write_filter *);
86 * Add a filter to this write handle that passes all data through an
90 archive_write_add_filter_program(struct archive *_a, const char *cmd)
92 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
93 struct private_data *data;
94 static const char *prefix = "Program: ";
96 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
97 ARCHIVE_STATE_NEW, "archive_write_add_filter_program");
99 f->data = calloc(1, sizeof(*data));
102 data = (struct private_data *)f->data;
104 data->cmd = strdup(cmd);
105 if (data->cmd == NULL)
108 data->pdata = __archive_write_program_allocate();
109 if (data->pdata == NULL)
112 /* Make up a description string. */
113 if (archive_string_ensure(&data->description,
114 strlen(prefix) + strlen(cmd) + 1) == NULL)
116 archive_strcpy(&data->description, prefix);
117 archive_strcat(&data->description, cmd);
119 f->name = data->description.s;
120 f->code = ARCHIVE_FILTER_PROGRAM;
121 f->open = archive_compressor_program_open;
122 f->write = archive_compressor_program_write;
123 f->close = archive_compressor_program_close;
124 f->free = archive_compressor_program_free;
127 archive_compressor_program_free(f);
128 archive_set_error(_a, ENOMEM,
129 "Can't allocate memory for filter program");
130 return (ARCHIVE_FATAL);
134 archive_compressor_program_open(struct archive_write_filter *f)
136 struct private_data *data = (struct private_data *)f->data;
138 return __archive_write_program_open(f, data->pdata, data->cmd);
142 archive_compressor_program_write(struct archive_write_filter *f,
143 const void *buff, size_t length)
145 struct private_data *data = (struct private_data *)f->data;
147 return __archive_write_program_write(f, data->pdata, buff, length);
151 archive_compressor_program_close(struct archive_write_filter *f)
153 struct private_data *data = (struct private_data *)f->data;
155 return __archive_write_program_close(f, data->pdata);
159 archive_compressor_program_free(struct archive_write_filter *f)
161 struct private_data *data = (struct private_data *)f->data;
165 archive_string_free(&data->description);
166 __archive_write_program_free(data->pdata);
174 * Allocate resources for executing an external program.
176 struct archive_write_program_data *
177 __archive_write_program_allocate(void)
179 struct archive_write_program_data *data;
181 data = calloc(1, sizeof(struct archive_write_program_data));
184 data->child_stdin = -1;
185 data->child_stdout = -1;
190 * Release the resources.
193 __archive_write_program_free(struct archive_write_program_data *data)
197 #if defined(_WIN32) && !defined(__CYGWIN__)
199 CloseHandle(data->child);
201 free(data->child_buf);
208 __archive_write_program_open(struct archive_write_filter *f,
209 struct archive_write_program_data *data, const char *cmd)
214 ret = __archive_write_open_filter(f->next_filter);
215 if (ret != ARCHIVE_OK)
218 if (data->child_buf == NULL) {
219 data->child_buf_len = 65536;
220 data->child_buf_avail = 0;
221 data->child_buf = malloc(data->child_buf_len);
223 if (data->child_buf == NULL) {
224 archive_set_error(f->archive, ENOMEM,
225 "Can't allocate compression buffer");
226 return (ARCHIVE_FATAL);
230 child = __archive_create_child(cmd, &data->child_stdin,
231 &data->child_stdout);
233 archive_set_error(f->archive, EINVAL,
234 "Can't initialise filter");
235 return (ARCHIVE_FATAL);
237 #if defined(_WIN32) && !defined(__CYGWIN__)
238 data->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
239 if (data->child == NULL) {
240 close(data->child_stdin);
241 data->child_stdin = -1;
242 close(data->child_stdout);
243 data->child_stdout = -1;
244 archive_set_error(f->archive, EINVAL,
245 "Can't initialise filter");
246 return (ARCHIVE_FATAL);
255 child_write(struct archive_write_filter *f,
256 struct archive_write_program_data *data, const char *buf, size_t buf_len)
260 if (data->child_stdin == -1)
268 ret = write(data->child_stdin, buf, buf_len);
269 } while (ret == -1 && errno == EINTR);
274 close(data->child_stdin);
275 data->child_stdin = -1;
276 fcntl(data->child_stdout, F_SETFL, 0);
279 if (ret == -1 && errno != EAGAIN)
282 if (data->child_stdout == -1) {
283 fcntl(data->child_stdin, F_SETFL, 0);
284 __archive_check_child(data->child_stdin,
290 ret = read(data->child_stdout,
291 data->child_buf + data->child_buf_avail,
292 data->child_buf_len - data->child_buf_avail);
293 } while (ret == -1 && errno == EINTR);
295 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
296 close(data->child_stdout);
297 data->child_stdout = -1;
298 fcntl(data->child_stdin, F_SETFL, 0);
301 if (ret == -1 && errno == EAGAIN) {
302 __archive_check_child(data->child_stdin,
309 data->child_buf_avail += ret;
311 ret = __archive_write_filter(f->next_filter,
312 data->child_buf, data->child_buf_avail);
313 if (ret != ARCHIVE_OK)
315 data->child_buf_avail = 0;
320 * Write data to the filter stream.
323 __archive_write_program_write(struct archive_write_filter *f,
324 struct archive_write_program_data *data, const void *buff, size_t length)
329 if (data->child == 0)
334 ret = child_write(f, data, buf, length);
335 if (ret == -1 || ret == 0) {
336 archive_set_error(f->archive, EIO,
337 "Can't write to filter");
338 return (ARCHIVE_FATAL);
347 * Finish the filtering...
350 __archive_write_program_close(struct archive_write_filter *f,
351 struct archive_write_program_data *data)
356 if (data->child == 0)
357 return __archive_write_close_filter(f->next_filter);
360 close(data->child_stdin);
361 data->child_stdin = -1;
362 fcntl(data->child_stdout, F_SETFL, 0);
366 bytes_read = read(data->child_stdout,
367 data->child_buf + data->child_buf_avail,
368 data->child_buf_len - data->child_buf_avail);
369 } while (bytes_read == -1 && errno == EINTR);
371 if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
374 if (bytes_read == -1) {
375 archive_set_error(f->archive, errno,
376 "Read from filter failed unexpectedly.");
380 data->child_buf_avail += bytes_read;
382 ret = __archive_write_filter(f->next_filter,
383 data->child_buf, data->child_buf_avail);
384 if (ret != ARCHIVE_OK) {
388 data->child_buf_avail = 0;
392 /* Shut down the child. */
393 if (data->child_stdin != -1)
394 close(data->child_stdin);
395 if (data->child_stdout != -1)
396 close(data->child_stdout);
397 while (waitpid(data->child, &status, 0) == -1 && errno == EINTR)
399 #if defined(_WIN32) && !defined(__CYGWIN__)
400 CloseHandle(data->child);
405 archive_set_error(f->archive, EIO,
406 "Filter exited with failure.");
409 r1 = __archive_write_close_filter(f->next_filter);
410 return (r1 < ret ? r1 : ret);