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
53 int
54 archive_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 
61 struct 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 
74 struct private_data {
75 	struct archive_write_program_data *pdata;
76 	struct archive_string description;
77 	char		*cmd;
78 };
79 
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 *);
85 
86 /*
87  * Add a filter to this write handle that passes all data through an
88  * external program.
89  */
90 int
91 archive_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);
127 memerr:
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 
134 static int
135 archive_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 
142 static int
143 archive_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 
151 static int
152 archive_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 
159 static int
160 archive_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  */
177 struct 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  */
194 int
195 __archive_write_program_free(struct archive_write_program_data *data)
196 {
197 
198 	if (data) {
199 		free(data->program_name);
200 		free(data->child_buf);
201 		free(data);
202 	}
203 	return (ARCHIVE_OK);
204 }
205 
206 int
207 __archive_write_program_open(struct archive_write_filter *f,
208     struct archive_write_program_data *data, const char *cmd)
209 {
210 	int ret;
211 
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);
216 
217 		if (data->child_buf == NULL) {
218 			archive_set_error(f->archive, ENOMEM,
219 			    "Can't allocate compression buffer");
220 			return (ARCHIVE_FATAL);
221 		}
222 	}
223 
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);
230 	}
231 	return (ARCHIVE_OK);
232 }
233 
234 static ssize_t
235 child_write(struct archive_write_filter *f,
236     struct archive_write_program_data *data, const char *buf, size_t buf_len)
237 {
238 	ssize_t ret;
239 
240 	if (data->child_stdin == -1)
241 		return (-1);
242 
243 	if (buf_len == 0)
244 		return (-1);
245 
246 	for (;;) {
247 		do {
248 			ret = write(data->child_stdin, buf, buf_len);
249 		} while (ret == -1 && errno == EINTR);
250 
251 		if (ret > 0)
252 			return (ret);
253 		if (ret == 0) {
254 			close(data->child_stdin);
255 			data->child_stdin = -1;
256 			fcntl(data->child_stdout, F_SETFL, 0);
257 			return (0);
258 		}
259 		if (ret == -1 && errno != EAGAIN)
260 			return (-1);
261 
262 		if (data->child_stdout == -1) {
263 			fcntl(data->child_stdin, F_SETFL, 0);
264 			__archive_check_child(data->child_stdin,
265 				data->child_stdout);
266 			continue;
267 		}
268 
269 		do {
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);
274 
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);
279 			continue;
280 		}
281 		if (ret == -1 && errno == EAGAIN) {
282 			__archive_check_child(data->child_stdin,
283 				data->child_stdout);
284 			continue;
285 		}
286 		if (ret == -1)
287 			return (-1);
288 
289 		data->child_buf_avail += ret;
290 
291 		ret = __archive_write_filter(f->next_filter,
292 		    data->child_buf, data->child_buf_avail);
293 		if (ret != ARCHIVE_OK)
294 			return (-1);
295 		data->child_buf_avail = 0;
296 	}
297 }
298 
299 /*
300  * Write data to the filter stream.
301  */
302 int
303 __archive_write_program_write(struct archive_write_filter *f,
304     struct archive_write_program_data *data, const void *buff, size_t length)
305 {
306 	ssize_t ret;
307 	const char *buf;
308 
309 	if (data->child == 0)
310 		return (ARCHIVE_OK);
311 
312 	buf = buff;
313 	while (length > 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);
319 		}
320 		length -= ret;
321 		buf += ret;
322 	}
323 	return (ARCHIVE_OK);
324 }
325 
326 /*
327  * Finish the filtering...
328  */
329 int
330 __archive_write_program_close(struct archive_write_filter *f,
331     struct archive_write_program_data *data)
332 {
333 	int ret, status;
334 	ssize_t bytes_read;
335 
336 	if (data->child == 0)
337 		return ARCHIVE_OK;
338 
339 	ret = 0;
340 	close(data->child_stdin);
341 	data->child_stdin = -1;
342 	fcntl(data->child_stdout, F_SETFL, 0);
343 
344 	for (;;) {
345 		do {
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);
350 
351 		if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
352 			break;
353 
354 		if (bytes_read == -1) {
355 			archive_set_error(f->archive, errno,
356 			    "Error reading from program: %s", data->program_name);
357 			ret = ARCHIVE_FATAL;
358 			goto cleanup;
359 		}
360 		data->child_buf_avail += bytes_read;
361 
362 		ret = __archive_write_filter(f->next_filter,
363 		    data->child_buf, data->child_buf_avail);
364 		if (ret != ARCHIVE_OK) {
365 			ret = ARCHIVE_FATAL;
366 			goto cleanup;
367 		}
368 		data->child_buf_avail = 0;
369 	}
370 
371 cleanup:
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)
378 		continue;
379 #if defined(_WIN32) && !defined(__CYGWIN__)
380 	CloseHandle(data->child);
381 #endif
382 	data->child = 0;
383 
384 	if (status != 0) {
385 		archive_set_error(f->archive, EIO,
386 		    "Error closing program: %s", data->program_name);
387 		ret = ARCHIVE_FATAL;
388 	}
389 	return ret;
390 }
391 
392