archive_write_add_filter_program.c revision 316337
1/*-
2 * Copyright (c) 2007 Joerg Sonnenberger
3 * Copyright (c) 2012 Michihiro NAKAJIMA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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.
25 */
26
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 $");
29
30#ifdef HAVE_SYS_WAIT_H
31#  include <sys/wait.h>
32#endif
33#ifdef HAVE_ERRNO_H
34#  include <errno.h>
35#endif
36#ifdef HAVE_FCNTL_H
37#  include <fcntl.h>
38#endif
39#ifdef HAVE_STDLIB_H
40#  include <stdlib.h>
41#endif
42#ifdef HAVE_STRING_H
43#  include <string.h>
44#endif
45
46#include "archive.h"
47#include "archive_private.h"
48#include "archive_string.h"
49#include "archive_write_private.h"
50#include "filter_fork.h"
51
52#if ARCHIVE_VERSION_NUMBER < 4000000
53int
54archive_write_set_compression_program(struct archive *a, const char *cmd)
55{
56	__archive_write_filters_free(a);
57	return (archive_write_add_filter_program(a, cmd));
58}
59#endif
60
61struct archive_write_program_data {
62#if defined(_WIN32) && !defined(__CYGWIN__)
63	HANDLE		 child;
64#else
65	pid_t		 child;
66#endif
67	int		 child_stdin, child_stdout;
68
69	char		*child_buf;
70	size_t		 child_buf_len, child_buf_avail;
71	char		*program_name;
72};
73
74struct private_data {
75	struct archive_write_program_data *pdata;
76	struct archive_string description;
77	char		*cmd;
78};
79
80static int archive_compressor_program_open(struct archive_write_filter *);
81static int archive_compressor_program_write(struct archive_write_filter *,
82		    const void *, size_t);
83static int archive_compressor_program_close(struct archive_write_filter *);
84static int archive_compressor_program_free(struct archive_write_filter *);
85
86/*
87 * Add a filter to this write handle that passes all data through an
88 * external program.
89 */
90int
91archive_write_add_filter_program(struct archive *_a, const char *cmd)
92{
93	struct archive_write_filter *f = __archive_write_allocate_filter(_a);
94	struct private_data *data;
95	static const char prefix[] = "Program: ";
96
97	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
98	    ARCHIVE_STATE_NEW, "archive_write_add_filter_program");
99
100	f->data = calloc(1, sizeof(*data));
101	if (f->data == NULL)
102		goto memerr;
103	data = (struct private_data *)f->data;
104
105	data->cmd = strdup(cmd);
106	if (data->cmd == NULL)
107		goto memerr;
108
109	data->pdata = __archive_write_program_allocate(cmd);
110	if (data->pdata == NULL)
111		goto memerr;
112
113	/* Make up a description string. */
114	if (archive_string_ensure(&data->description,
115	    strlen(prefix) + strlen(cmd) + 1) == NULL)
116		goto memerr;
117	archive_strcpy(&data->description, prefix);
118	archive_strcat(&data->description, cmd);
119
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;
126	return (ARCHIVE_OK);
127memerr:
128	archive_compressor_program_free(f);
129	archive_set_error(_a, ENOMEM,
130	    "Can't allocate memory for filter program");
131	return (ARCHIVE_FATAL);
132}
133
134static int
135archive_compressor_program_open(struct archive_write_filter *f)
136{
137	struct private_data *data = (struct private_data *)f->data;
138
139	return __archive_write_program_open(f, data->pdata, data->cmd);
140}
141
142static int
143archive_compressor_program_write(struct archive_write_filter *f,
144    const void *buff, size_t length)
145{
146	struct private_data *data = (struct private_data *)f->data;
147
148	return __archive_write_program_write(f, data->pdata, buff, length);
149}
150
151static int
152archive_compressor_program_close(struct archive_write_filter *f)
153{
154	struct private_data *data = (struct private_data *)f->data;
155
156	return __archive_write_program_close(f, data->pdata);
157}
158
159static int
160archive_compressor_program_free(struct archive_write_filter *f)
161{
162	struct private_data *data = (struct private_data *)f->data;
163
164	if (data) {
165		free(data->cmd);
166		archive_string_free(&data->description);
167		__archive_write_program_free(data->pdata);
168		free(data);
169		f->data = NULL;
170	}
171	return (ARCHIVE_OK);
172}
173
174/*
175 * Allocate resources for executing an external program.
176 */
177struct archive_write_program_data *
178__archive_write_program_allocate(const char *program)
179{
180	struct archive_write_program_data *data;
181
182	data = calloc(1, sizeof(struct archive_write_program_data));
183	if (data == NULL)
184		return (data);
185	data->child_stdin = -1;
186	data->child_stdout = -1;
187	data->program_name = strdup(program);
188	return (data);
189}
190
191/*
192 * Release the resources.
193 */
194int
195__archive_write_program_free(struct archive_write_program_data *data)
196{
197
198	if (data) {
199#if defined(_WIN32) && !defined(__CYGWIN__)
200		if (data->child)
201			CloseHandle(data->child);
202#endif
203		free(data->program_name);
204		free(data->child_buf);
205		free(data);
206	}
207	return (ARCHIVE_OK);
208}
209
210int
211__archive_write_program_open(struct archive_write_filter *f,
212    struct archive_write_program_data *data, const char *cmd)
213{
214	pid_t child;
215	int ret;
216
217	ret = __archive_write_open_filter(f->next_filter);
218	if (ret != ARCHIVE_OK)
219		return (ret);
220
221	if (data->child_buf == NULL) {
222		data->child_buf_len = 65536;
223		data->child_buf_avail = 0;
224		data->child_buf = malloc(data->child_buf_len);
225
226		if (data->child_buf == NULL) {
227			archive_set_error(f->archive, ENOMEM,
228			    "Can't allocate compression buffer");
229			return (ARCHIVE_FATAL);
230		}
231	}
232
233	child = __archive_create_child(cmd, &data->child_stdin,
234		    &data->child_stdout);
235	if (child == -1) {
236		archive_set_error(f->archive, EINVAL,
237		    "Can't launch external program: %s", cmd);
238		return (ARCHIVE_FATAL);
239	}
240#if defined(_WIN32) && !defined(__CYGWIN__)
241	data->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
242	if (data->child == NULL) {
243		close(data->child_stdin);
244		data->child_stdin = -1;
245		close(data->child_stdout);
246		data->child_stdout = -1;
247		archive_set_error(f->archive, EINVAL,
248		    "Can't launch external program: %s", cmd);
249		return (ARCHIVE_FATAL);
250	}
251#else
252	data->child = child;
253#endif
254	return (ARCHIVE_OK);
255}
256
257static ssize_t
258child_write(struct archive_write_filter *f,
259    struct archive_write_program_data *data, const char *buf, size_t buf_len)
260{
261	ssize_t ret;
262
263	if (data->child_stdin == -1)
264		return (-1);
265
266	if (buf_len == 0)
267		return (-1);
268
269	for (;;) {
270		do {
271			ret = write(data->child_stdin, buf, buf_len);
272		} while (ret == -1 && errno == EINTR);
273
274		if (ret > 0)
275			return (ret);
276		if (ret == 0) {
277			close(data->child_stdin);
278			data->child_stdin = -1;
279			fcntl(data->child_stdout, F_SETFL, 0);
280			return (0);
281		}
282		if (ret == -1 && errno != EAGAIN)
283			return (-1);
284
285		if (data->child_stdout == -1) {
286			fcntl(data->child_stdin, F_SETFL, 0);
287			__archive_check_child(data->child_stdin,
288				data->child_stdout);
289			continue;
290		}
291
292		do {
293			ret = read(data->child_stdout,
294			    data->child_buf + data->child_buf_avail,
295			    data->child_buf_len - data->child_buf_avail);
296		} while (ret == -1 && errno == EINTR);
297
298		if (ret == 0 || (ret == -1 && errno == EPIPE)) {
299			close(data->child_stdout);
300			data->child_stdout = -1;
301			fcntl(data->child_stdin, F_SETFL, 0);
302			continue;
303		}
304		if (ret == -1 && errno == EAGAIN) {
305			__archive_check_child(data->child_stdin,
306				data->child_stdout);
307			continue;
308		}
309		if (ret == -1)
310			return (-1);
311
312		data->child_buf_avail += ret;
313
314		ret = __archive_write_filter(f->next_filter,
315		    data->child_buf, data->child_buf_avail);
316		if (ret != ARCHIVE_OK)
317			return (-1);
318		data->child_buf_avail = 0;
319	}
320}
321
322/*
323 * Write data to the filter stream.
324 */
325int
326__archive_write_program_write(struct archive_write_filter *f,
327    struct archive_write_program_data *data, const void *buff, size_t length)
328{
329	ssize_t ret;
330	const char *buf;
331
332	if (data->child == 0)
333		return (ARCHIVE_OK);
334
335	buf = buff;
336	while (length > 0) {
337		ret = child_write(f, data, buf, length);
338		if (ret == -1 || ret == 0) {
339			archive_set_error(f->archive, EIO,
340			    "Can't write to program: %s", data->program_name);
341			return (ARCHIVE_FATAL);
342		}
343		length -= ret;
344		buf += ret;
345	}
346	return (ARCHIVE_OK);
347}
348
349/*
350 * Finish the filtering...
351 */
352int
353__archive_write_program_close(struct archive_write_filter *f,
354    struct archive_write_program_data *data)
355{
356	int ret, r1, status;
357	ssize_t bytes_read;
358
359	if (data->child == 0)
360		return __archive_write_close_filter(f->next_filter);
361
362	ret = 0;
363	close(data->child_stdin);
364	data->child_stdin = -1;
365	fcntl(data->child_stdout, F_SETFL, 0);
366
367	for (;;) {
368		do {
369			bytes_read = read(data->child_stdout,
370			    data->child_buf + data->child_buf_avail,
371			    data->child_buf_len - data->child_buf_avail);
372		} while (bytes_read == -1 && errno == EINTR);
373
374		if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
375			break;
376
377		if (bytes_read == -1) {
378			archive_set_error(f->archive, errno,
379			    "Error reading from program: %s", data->program_name);
380			ret = ARCHIVE_FATAL;
381			goto cleanup;
382		}
383		data->child_buf_avail += bytes_read;
384
385		ret = __archive_write_filter(f->next_filter,
386		    data->child_buf, data->child_buf_avail);
387		if (ret != ARCHIVE_OK) {
388			ret = ARCHIVE_FATAL;
389			goto cleanup;
390		}
391		data->child_buf_avail = 0;
392	}
393
394cleanup:
395	/* Shut down the child. */
396	if (data->child_stdin != -1)
397		close(data->child_stdin);
398	if (data->child_stdout != -1)
399		close(data->child_stdout);
400	while (waitpid(data->child, &status, 0) == -1 && errno == EINTR)
401		continue;
402#if defined(_WIN32) && !defined(__CYGWIN__)
403	CloseHandle(data->child);
404#endif
405	data->child = 0;
406
407	if (status != 0) {
408		archive_set_error(f->archive, EIO,
409		    "Error closing program: %s", data->program_name);
410		ret = ARCHIVE_FATAL;
411	}
412	r1 = __archive_write_close_filter(f->next_filter);
413	return (r1 < ret ? r1 : ret);
414}
415
416