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