archive_write_add_filter_program.c revision 358088
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
216	if (data->child_buf == NULL) {
217		data->child_buf_len = 65536;
218		data->child_buf_avail = 0;
219		data->child_buf = malloc(data->child_buf_len);
220
221		if (data->child_buf == NULL) {
222			archive_set_error(f->archive, ENOMEM,
223			    "Can't allocate compression buffer");
224			return (ARCHIVE_FATAL);
225		}
226	}
227
228	child = __archive_create_child(cmd, &data->child_stdin,
229		    &data->child_stdout);
230	if (child == -1) {
231		archive_set_error(f->archive, EINVAL,
232		    "Can't launch external program: %s", cmd);
233		return (ARCHIVE_FATAL);
234	}
235#if defined(_WIN32) && !defined(__CYGWIN__)
236	data->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
237	if (data->child == NULL) {
238		close(data->child_stdin);
239		data->child_stdin = -1;
240		close(data->child_stdout);
241		data->child_stdout = -1;
242		archive_set_error(f->archive, EINVAL,
243		    "Can't launch external program: %s", cmd);
244		return (ARCHIVE_FATAL);
245	}
246#else
247	data->child = child;
248#endif
249	return (ARCHIVE_OK);
250}
251
252static ssize_t
253child_write(struct archive_write_filter *f,
254    struct archive_write_program_data *data, const char *buf, size_t buf_len)
255{
256	ssize_t ret;
257
258	if (data->child_stdin == -1)
259		return (-1);
260
261	if (buf_len == 0)
262		return (-1);
263
264	for (;;) {
265		do {
266			ret = write(data->child_stdin, buf, buf_len);
267		} while (ret == -1 && errno == EINTR);
268
269		if (ret > 0)
270			return (ret);
271		if (ret == 0) {
272			close(data->child_stdin);
273			data->child_stdin = -1;
274			fcntl(data->child_stdout, F_SETFL, 0);
275			return (0);
276		}
277		if (ret == -1 && errno != EAGAIN)
278			return (-1);
279
280		if (data->child_stdout == -1) {
281			fcntl(data->child_stdin, F_SETFL, 0);
282			__archive_check_child(data->child_stdin,
283				data->child_stdout);
284			continue;
285		}
286
287		do {
288			ret = read(data->child_stdout,
289			    data->child_buf + data->child_buf_avail,
290			    data->child_buf_len - data->child_buf_avail);
291		} while (ret == -1 && errno == EINTR);
292
293		if (ret == 0 || (ret == -1 && errno == EPIPE)) {
294			close(data->child_stdout);
295			data->child_stdout = -1;
296			fcntl(data->child_stdin, F_SETFL, 0);
297			continue;
298		}
299		if (ret == -1 && errno == EAGAIN) {
300			__archive_check_child(data->child_stdin,
301				data->child_stdout);
302			continue;
303		}
304		if (ret == -1)
305			return (-1);
306
307		data->child_buf_avail += ret;
308
309		ret = __archive_write_filter(f->next_filter,
310		    data->child_buf, data->child_buf_avail);
311		if (ret != ARCHIVE_OK)
312			return (-1);
313		data->child_buf_avail = 0;
314	}
315}
316
317/*
318 * Write data to the filter stream.
319 */
320int
321__archive_write_program_write(struct archive_write_filter *f,
322    struct archive_write_program_data *data, const void *buff, size_t length)
323{
324	ssize_t ret;
325	const char *buf;
326
327	if (data->child == 0)
328		return (ARCHIVE_OK);
329
330	buf = buff;
331	while (length > 0) {
332		ret = child_write(f, data, buf, length);
333		if (ret == -1 || ret == 0) {
334			archive_set_error(f->archive, EIO,
335			    "Can't write to program: %s", data->program_name);
336			return (ARCHIVE_FATAL);
337		}
338		length -= ret;
339		buf += ret;
340	}
341	return (ARCHIVE_OK);
342}
343
344/*
345 * Finish the filtering...
346 */
347int
348__archive_write_program_close(struct archive_write_filter *f,
349    struct archive_write_program_data *data)
350{
351	int ret, status;
352	ssize_t bytes_read;
353
354	if (data->child == 0)
355		return ARCHIVE_OK;
356
357	ret = 0;
358	close(data->child_stdin);
359	data->child_stdin = -1;
360	fcntl(data->child_stdout, F_SETFL, 0);
361
362	for (;;) {
363		do {
364			bytes_read = read(data->child_stdout,
365			    data->child_buf + data->child_buf_avail,
366			    data->child_buf_len - data->child_buf_avail);
367		} while (bytes_read == -1 && errno == EINTR);
368
369		if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
370			break;
371
372		if (bytes_read == -1) {
373			archive_set_error(f->archive, errno,
374			    "Error reading from program: %s", data->program_name);
375			ret = ARCHIVE_FATAL;
376			goto cleanup;
377		}
378		data->child_buf_avail += bytes_read;
379
380		ret = __archive_write_filter(f->next_filter,
381		    data->child_buf, data->child_buf_avail);
382		if (ret != ARCHIVE_OK) {
383			ret = ARCHIVE_FATAL;
384			goto cleanup;
385		}
386		data->child_buf_avail = 0;
387	}
388
389cleanup:
390	/* Shut down the child. */
391	if (data->child_stdin != -1)
392		close(data->child_stdin);
393	if (data->child_stdout != -1)
394		close(data->child_stdout);
395	while (waitpid(data->child, &status, 0) == -1 && errno == EINTR)
396		continue;
397#if defined(_WIN32) && !defined(__CYGWIN__)
398	CloseHandle(data->child);
399#endif
400	data->child = 0;
401
402	if (status != 0) {
403		archive_set_error(f->archive, EIO,
404		    "Error closing program: %s", data->program_name);
405		ret = ARCHIVE_FATAL;
406	}
407	return ret;
408}
409
410