2 * Copyright (c) 2007 Joerg Sonnenberger
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_compression_program.c,v 1.1 2007/05/29 01:00:19 kientzle Exp $");
30 #ifdef HAVE_SYS_WAIT_H
31 # include <sys/wait.h>
47 #include "archive_private.h"
48 #include "archive_write_private.h"
50 #include "filter_fork.h"
55 int child_stdin, child_stdout;
58 size_t child_buf_len, child_buf_avail;
61 static int archive_compressor_program_finish(struct archive_write *);
62 static int archive_compressor_program_init(struct archive_write *);
63 static int archive_compressor_program_write(struct archive_write *,
64 const void *, size_t);
67 * Allocate, initialize and return a archive object.
70 archive_write_set_compression_program(struct archive *_a, const char *cmd)
72 struct archive_write *a = (struct archive_write *)_a;
73 __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
74 ARCHIVE_STATE_NEW, "archive_write_set_compression_program");
75 a->compressor.init = &archive_compressor_program_init;
76 a->compressor.config = strdup(cmd);
84 archive_compressor_program_init(struct archive_write *a)
87 struct private_data *state;
88 static const char *prefix = "Program: ";
89 char *cmd = a->compressor.config;
91 if (a->client_opener != NULL) {
92 ret = (a->client_opener)(&a->archive, a->client_data);
93 if (ret != ARCHIVE_OK)
97 state = (struct private_data *)malloc(sizeof(*state));
99 archive_set_error(&a->archive, ENOMEM,
100 "Can't allocate data for compression");
101 return (ARCHIVE_FATAL);
103 memset(state, 0, sizeof(*state));
105 a->archive.compression_code = ARCHIVE_COMPRESSION_PROGRAM;
106 state->description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
107 strcpy(state->description, prefix);
108 strcat(state->description, cmd);
109 a->archive.compression_name = state->description;
111 state->child_buf_len = a->bytes_per_block;
112 state->child_buf_avail = 0;
113 state->child_buf = malloc(state->child_buf_len);
115 if (state->child_buf == NULL) {
116 archive_set_error(&a->archive, ENOMEM,
117 "Can't allocate data for compression buffer");
119 return (ARCHIVE_FATAL);
122 if ((state->child = __archive_create_child(cmd,
123 &state->child_stdin, &state->child_stdout)) == -1) {
124 archive_set_error(&a->archive, EINVAL,
125 "Can't initialise filter");
126 free(state->child_buf);
128 return (ARCHIVE_FATAL);
131 a->compressor.write = archive_compressor_program_write;
132 a->compressor.finish = archive_compressor_program_finish;
134 a->compressor.data = state;
139 child_write(struct archive_write *a, const char *buf, size_t buf_len)
141 struct private_data *state = a->compressor.data;
144 if (state->child_stdin == -1)
152 ret = write(state->child_stdin, buf, buf_len);
153 } while (ret == -1 && errno == EINTR);
158 close(state->child_stdin);
159 state->child_stdin = -1;
160 fcntl(state->child_stdout, F_SETFL, 0);
163 if (ret == -1 && errno != EAGAIN)
167 ret = read(state->child_stdout,
168 state->child_buf + state->child_buf_avail,
169 state->child_buf_len - state->child_buf_avail);
170 } while (ret == -1 && errno == EINTR);
172 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
173 close(state->child_stdout);
174 state->child_stdout = -1;
175 fcntl(state->child_stdin, F_SETFL, 0);
178 if (ret == -1 && errno == EAGAIN) {
179 __archive_check_child(state->child_stdin, state->child_stdout);
185 state->child_buf_avail += ret;
187 ret = (a->client_writer)(&a->archive, a->client_data,
188 state->child_buf, state->child_buf_avail);
192 if ((size_t)ret < state->child_buf_avail) {
193 memmove(state->child_buf, state->child_buf + ret,
194 state->child_buf_avail - ret);
196 state->child_buf_avail -= ret;
197 a->archive.raw_position += ret;
202 * Write data to the compressed stream.
205 archive_compressor_program_write(struct archive_write *a, const void *buff,
208 struct private_data *state;
212 state = (struct private_data *)a->compressor.data;
213 if (a->client_writer == NULL) {
214 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
215 "No write callback is registered? "
216 "This is probably an internal programming error.");
217 return (ARCHIVE_FATAL);
222 ret = child_write(a, buf, length);
223 if (ret == -1 || ret == 0) {
224 archive_set_error(&a->archive, EIO,
225 "Can't write to filter");
226 return (ARCHIVE_FATAL);
232 a->archive.file_position += length;
238 * Finish the compression...
241 archive_compressor_program_finish(struct archive_write *a)
244 ssize_t bytes_read, bytes_written;
245 struct private_data *state;
247 state = (struct private_data *)a->compressor.data;
249 if (a->client_writer == NULL) {
250 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
251 "No write callback is registered? "
252 "This is probably an internal programming error.");
257 /* XXX pad compressed data. */
259 close(state->child_stdin);
260 state->child_stdin = -1;
261 fcntl(state->child_stdout, F_SETFL, 0);
265 bytes_read = read(state->child_stdout,
266 state->child_buf + state->child_buf_avail,
267 state->child_buf_len - state->child_buf_avail);
268 } while (bytes_read == -1 && errno == EINTR);
270 if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
273 if (bytes_read == -1) {
274 archive_set_error(&a->archive, errno,
275 "Read from filter failed unexpectedly.");
279 state->child_buf_avail += bytes_read;
281 bytes_written = (a->client_writer)(&a->archive, a->client_data,
282 state->child_buf, state->child_buf_avail);
283 if (bytes_written <= 0) {
287 if ((size_t)bytes_written < state->child_buf_avail) {
288 memmove(state->child_buf,
289 state->child_buf + bytes_written,
290 state->child_buf_avail - bytes_written);
292 state->child_buf_avail -= bytes_written;
293 a->archive.raw_position += bytes_written;
296 /* XXX pad final compressed block. */
299 /* Shut down the child. */
300 if (state->child_stdin != -1)
301 close(state->child_stdin);
302 if (state->child_stdout != -1)
303 close(state->child_stdout);
304 while (waitpid(state->child, &status, 0) == -1 && errno == EINTR)
308 archive_set_error(&a->archive, EIO,
309 "Filter exited with failure.");
313 /* Release our configuration data. */
314 free(a->compressor.config);
315 a->compressor.config = NULL;
317 /* Release our private state data. */
318 free(state->child_buf);
319 free(state->description);