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
;
75 struct archive_write_program_data
*pdata
;
76 struct archive_string description
;
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
*);
87 * Add a filter to this write handle that passes all data through an
91 archive_write_add_filter_program(struct archive
*_a
, const char *cmd
)
93 struct archive_write_filter
*f
= __archive_write_allocate_filter(_a
);
94 struct private_data
*data
;
95 static const char *prefix
= "Program: ";
97 archive_check_magic(_a
, ARCHIVE_WRITE_MAGIC
,
98 ARCHIVE_STATE_NEW
, "archive_write_add_filter_program");
100 f
->data
= calloc(1, sizeof(*data
));
103 data
= (struct private_data
*)f
->data
;
105 data
->cmd
= strdup(cmd
);
106 if (data
->cmd
== NULL
)
109 data
->pdata
= __archive_write_program_allocate(cmd
);
110 if (data
->pdata
== NULL
)
113 /* Make up a description string. */
114 if (archive_string_ensure(&data
->description
,
115 strlen(prefix
) + strlen(cmd
) + 1) == NULL
)
117 archive_strcpy(&data
->description
, prefix
);
118 archive_strcat(&data
->description
, cmd
);
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
;
128 archive_compressor_program_free(f
);
129 archive_set_error(_a
, ENOMEM
,
130 "Can't allocate memory for filter program");
131 return (ARCHIVE_FATAL
);
135 archive_compressor_program_open(struct archive_write_filter
*f
)
137 struct private_data
*data
= (struct private_data
*)f
->data
;
139 return __archive_write_program_open(f
, data
->pdata
, data
->cmd
);
143 archive_compressor_program_write(struct archive_write_filter
*f
,
144 const void *buff
, size_t length
)
146 struct private_data
*data
= (struct private_data
*)f
->data
;
148 return __archive_write_program_write(f
, data
->pdata
, buff
, length
);
152 archive_compressor_program_close(struct archive_write_filter
*f
)
154 struct private_data
*data
= (struct private_data
*)f
->data
;
156 return __archive_write_program_close(f
, data
->pdata
);
160 archive_compressor_program_free(struct archive_write_filter
*f
)
162 struct private_data
*data
= (struct private_data
*)f
->data
;
166 archive_string_free(&data
->description
);
167 __archive_write_program_free(data
->pdata
);
175 * Allocate resources for executing an external program.
177 struct archive_write_program_data
*
178 __archive_write_program_allocate(const char *program
)
180 struct archive_write_program_data
*data
;
182 data
= calloc(1, sizeof(struct archive_write_program_data
));
185 data
->child_stdin
= -1;
186 data
->child_stdout
= -1;
187 data
->program_name
= strdup(program
);
192 * Release the resources.
195 __archive_write_program_free(struct archive_write_program_data
*data
)
199 #if defined(_WIN32) && !defined(__CYGWIN__)
201 CloseHandle(data
->child
);
203 free(data
->child_buf
);
210 __archive_write_program_open(struct archive_write_filter
*f
,
211 struct archive_write_program_data
*data
, const char *cmd
)
216 ret
= __archive_write_open_filter(f
->next_filter
);
217 if (ret
!= ARCHIVE_OK
)
220 if (data
->child_buf
== NULL
) {
221 data
->child_buf_len
= 65536;
222 data
->child_buf_avail
= 0;
223 data
->child_buf
= malloc(data
->child_buf_len
);
225 if (data
->child_buf
== NULL
) {
226 archive_set_error(f
->archive
, ENOMEM
,
227 "Can't allocate compression buffer");
228 return (ARCHIVE_FATAL
);
232 child
= __archive_create_child(cmd
, &data
->child_stdin
,
233 &data
->child_stdout
);
235 archive_set_error(f
->archive
, EINVAL
,
236 "Can't launch external program: %s", cmd
);
237 return (ARCHIVE_FATAL
);
239 #if defined(_WIN32) && !defined(__CYGWIN__)
240 data
->child
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
, child
);
241 if (data
->child
== NULL
) {
242 close(data
->child_stdin
);
243 data
->child_stdin
= -1;
244 close(data
->child_stdout
);
245 data
->child_stdout
= -1;
246 archive_set_error(f
->archive
, EINVAL
,
247 "Can't launch external program: %s", cmd
);
248 return (ARCHIVE_FATAL
);
257 child_write(struct archive_write_filter
*f
,
258 struct archive_write_program_data
*data
, const char *buf
, size_t buf_len
)
262 if (data
->child_stdin
== -1)
270 ret
= write(data
->child_stdin
, buf
, buf_len
);
271 } while (ret
== -1 && errno
== EINTR
);
276 close(data
->child_stdin
);
277 data
->child_stdin
= -1;
278 fcntl(data
->child_stdout
, F_SETFL
, 0);
281 if (ret
== -1 && errno
!= EAGAIN
)
284 if (data
->child_stdout
== -1) {
285 fcntl(data
->child_stdin
, F_SETFL
, 0);
286 __archive_check_child(data
->child_stdin
,
292 ret
= read(data
->child_stdout
,
293 data
->child_buf
+ data
->child_buf_avail
,
294 data
->child_buf_len
- data
->child_buf_avail
);
295 } while (ret
== -1 && errno
== EINTR
);
297 if (ret
== 0 || (ret
== -1 && errno
== EPIPE
)) {
298 close(data
->child_stdout
);
299 data
->child_stdout
= -1;
300 fcntl(data
->child_stdin
, F_SETFL
, 0);
303 if (ret
== -1 && errno
== EAGAIN
) {
304 __archive_check_child(data
->child_stdin
,
311 data
->child_buf_avail
+= ret
;
313 ret
= __archive_write_filter(f
->next_filter
,
314 data
->child_buf
, data
->child_buf_avail
);
315 if (ret
!= ARCHIVE_OK
)
317 data
->child_buf_avail
= 0;
322 * Write data to the filter stream.
325 __archive_write_program_write(struct archive_write_filter
*f
,
326 struct archive_write_program_data
*data
, const void *buff
, size_t length
)
331 if (data
->child
== 0)
336 ret
= child_write(f
, data
, buf
, length
);
337 if (ret
== -1 || ret
== 0) {
338 archive_set_error(f
->archive
, EIO
,
339 "Can't write to program: %s", data
->program_name
);
340 return (ARCHIVE_FATAL
);
349 * Finish the filtering...
352 __archive_write_program_close(struct archive_write_filter
*f
,
353 struct archive_write_program_data
*data
)
358 if (data
->child
== 0)
359 return __archive_write_close_filter(f
->next_filter
);
362 close(data
->child_stdin
);
363 data
->child_stdin
= -1;
364 fcntl(data
->child_stdout
, F_SETFL
, 0);
368 bytes_read
= read(data
->child_stdout
,
369 data
->child_buf
+ data
->child_buf_avail
,
370 data
->child_buf_len
- data
->child_buf_avail
);
371 } while (bytes_read
== -1 && errno
== EINTR
);
373 if (bytes_read
== 0 || (bytes_read
== -1 && errno
== EPIPE
))
376 if (bytes_read
== -1) {
377 archive_set_error(f
->archive
, errno
,
378 "Error reading from program: %s", data
->program_name
);
382 data
->child_buf_avail
+= bytes_read
;
384 ret
= __archive_write_filter(f
->next_filter
,
385 data
->child_buf
, data
->child_buf_avail
);
386 if (ret
!= ARCHIVE_OK
) {
390 data
->child_buf_avail
= 0;
394 /* Shut down the child. */
395 if (data
->child_stdin
!= -1)
396 close(data
->child_stdin
);
397 if (data
->child_stdout
!= -1)
398 close(data
->child_stdout
);
399 while (waitpid(data
->child
, &status
, 0) == -1 && errno
== EINTR
)
401 #if defined(_WIN32) && !defined(__CYGWIN__)
402 CloseHandle(data
->child
);
407 archive_set_error(f
->archive
, EIO
,
408 "Error closing program: %s", data
->program_name
);
411 r1
= __archive_write_close_filter(f
->next_filter
);
412 return (r1
< ret
? r1
: ret
);