diff(1): Commit our patches to contrib/ and get rid of -I-.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_write_set_compression_program.c
blob69c5d4286332b768fa83e2db508a7ac7dde1570f
1 /*-
2 * Copyright (c) 2007 Joerg Sonnenberger
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.3 2008/06/15 10:45:57 kientzle Exp $");
30 /* This capability is only available on POSIX systems. */
31 #if !defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
32 !(defined(HAVE_FORK) || defined(HAVE_VFORK))
33 #include "archive.h"
36 * On non-Posix systems, allow the program to build, but choke if
37 * this function is actually invoked.
39 int
40 archive_write_set_compression_program(struct archive *_a, const char *cmd)
42 archive_set_error(_a, -1,
43 "External compression programs not supported on this platform");
44 return (ARCHIVE_FATAL);
47 #else
49 #ifdef HAVE_SYS_WAIT_H
50 # include <sys/wait.h>
51 #endif
52 #ifdef HAVE_ERRNO_H
53 # include <errno.h>
54 #endif
55 #ifdef HAVE_FCNTL_H
56 # include <fcntl.h>
57 #endif
58 #ifdef HAVE_STDLIB_H
59 # include <stdlib.h>
60 #endif
61 #ifdef HAVE_STRING_H
62 # include <string.h>
63 #endif
65 #include "archive.h"
66 #include "archive_private.h"
67 #include "archive_write_private.h"
69 #include "filter_fork.h"
71 struct private_data {
72 char *description;
73 pid_t child;
74 int child_stdin, child_stdout;
76 char *child_buf;
77 size_t child_buf_len, child_buf_avail;
80 static int archive_compressor_program_finish(struct archive_write *);
81 static int archive_compressor_program_init(struct archive_write *);
82 static int archive_compressor_program_write(struct archive_write *,
83 const void *, size_t);
86 * Allocate, initialize and return a archive object.
88 int
89 archive_write_set_compression_program(struct archive *_a, const char *cmd)
91 struct archive_write *a = (struct archive_write *)_a;
92 __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
93 ARCHIVE_STATE_NEW, "archive_write_set_compression_program");
94 a->compressor.init = &archive_compressor_program_init;
95 a->compressor.config = strdup(cmd);
96 return (ARCHIVE_OK);
100 * Setup callback.
102 static int
103 archive_compressor_program_init(struct archive_write *a)
105 int ret;
106 struct private_data *state;
107 static const char *prefix = "Program: ";
108 char *cmd = a->compressor.config;
110 if (a->client_opener != NULL) {
111 ret = (a->client_opener)(&a->archive, a->client_data);
112 if (ret != ARCHIVE_OK)
113 return (ret);
116 state = (struct private_data *)malloc(sizeof(*state));
117 if (state == NULL) {
118 archive_set_error(&a->archive, ENOMEM,
119 "Can't allocate data for compression");
120 return (ARCHIVE_FATAL);
122 memset(state, 0, sizeof(*state));
124 a->archive.compression_code = ARCHIVE_COMPRESSION_PROGRAM;
125 state->description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
126 strcpy(state->description, prefix);
127 strcat(state->description, cmd);
128 a->archive.compression_name = state->description;
130 state->child_buf_len = a->bytes_per_block;
131 state->child_buf_avail = 0;
132 state->child_buf = malloc(state->child_buf_len);
134 if (state->child_buf == NULL) {
135 archive_set_error(&a->archive, ENOMEM,
136 "Can't allocate data for compression buffer");
137 free(state);
138 return (ARCHIVE_FATAL);
141 if ((state->child = __archive_create_child(cmd,
142 &state->child_stdin, &state->child_stdout)) == -1) {
143 archive_set_error(&a->archive, EINVAL,
144 "Can't initialise filter");
145 free(state->child_buf);
146 free(state);
147 return (ARCHIVE_FATAL);
150 a->compressor.write = archive_compressor_program_write;
151 a->compressor.finish = archive_compressor_program_finish;
153 a->compressor.data = state;
154 return (0);
157 static ssize_t
158 child_write(struct archive_write *a, const char *buf, size_t buf_len)
160 struct private_data *state = a->compressor.data;
161 ssize_t ret;
163 if (state->child_stdin == -1)
164 return (-1);
166 if (buf_len == 0)
167 return (-1);
169 restart_write:
170 do {
171 ret = write(state->child_stdin, buf, buf_len);
172 } while (ret == -1 && errno == EINTR);
174 if (ret > 0)
175 return (ret);
176 if (ret == 0) {
177 close(state->child_stdin);
178 state->child_stdin = -1;
179 fcntl(state->child_stdout, F_SETFL, 0);
180 return (0);
182 if (ret == -1 && errno != EAGAIN)
183 return (-1);
185 if (state->child_stdout == -1) {
186 fcntl(state->child_stdin, F_SETFL, 0);
187 __archive_check_child(state->child_stdin, state->child_stdout);
188 goto restart_write;
191 do {
192 ret = read(state->child_stdout,
193 state->child_buf + state->child_buf_avail,
194 state->child_buf_len - state->child_buf_avail);
195 } while (ret == -1 && errno == EINTR);
197 if (ret == 0 || (ret == -1 && errno == EPIPE)) {
198 close(state->child_stdout);
199 state->child_stdout = -1;
200 fcntl(state->child_stdin, F_SETFL, 0);
201 goto restart_write;
203 if (ret == -1 && errno == EAGAIN) {
204 __archive_check_child(state->child_stdin, state->child_stdout);
205 goto restart_write;
207 if (ret == -1)
208 return (-1);
210 state->child_buf_avail += ret;
212 ret = (a->client_writer)(&a->archive, a->client_data,
213 state->child_buf, state->child_buf_avail);
214 if (ret <= 0)
215 return (-1);
217 if ((size_t)ret < state->child_buf_avail) {
218 memmove(state->child_buf, state->child_buf + ret,
219 state->child_buf_avail - ret);
221 state->child_buf_avail -= ret;
222 a->archive.raw_position += ret;
223 goto restart_write;
227 * Write data to the compressed stream.
229 static int
230 archive_compressor_program_write(struct archive_write *a, const void *buff,
231 size_t length)
233 struct private_data *state;
234 ssize_t ret;
235 const char *buf;
237 state = (struct private_data *)a->compressor.data;
238 if (a->client_writer == NULL) {
239 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
240 "No write callback is registered? "
241 "This is probably an internal programming error.");
242 return (ARCHIVE_FATAL);
245 buf = buff;
246 while (length > 0) {
247 ret = child_write(a, buf, length);
248 if (ret == -1 || ret == 0) {
249 archive_set_error(&a->archive, EIO,
250 "Can't write to filter");
251 return (ARCHIVE_FATAL);
253 length -= ret;
254 buf += ret;
257 a->archive.file_position += length;
258 return (ARCHIVE_OK);
263 * Finish the compression...
265 static int
266 archive_compressor_program_finish(struct archive_write *a)
268 int ret, status;
269 ssize_t bytes_read, bytes_written;
270 struct private_data *state;
272 state = (struct private_data *)a->compressor.data;
273 ret = 0;
274 if (a->client_writer == NULL) {
275 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
276 "No write callback is registered? "
277 "This is probably an internal programming error.");
278 ret = ARCHIVE_FATAL;
279 goto cleanup;
282 /* XXX pad compressed data. */
284 close(state->child_stdin);
285 state->child_stdin = -1;
286 fcntl(state->child_stdout, F_SETFL, 0);
288 for (;;) {
289 do {
290 bytes_read = read(state->child_stdout,
291 state->child_buf + state->child_buf_avail,
292 state->child_buf_len - state->child_buf_avail);
293 } while (bytes_read == -1 && errno == EINTR);
295 if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
296 break;
298 if (bytes_read == -1) {
299 archive_set_error(&a->archive, errno,
300 "Read from filter failed unexpectedly.");
301 ret = ARCHIVE_FATAL;
302 goto cleanup;
304 state->child_buf_avail += bytes_read;
306 bytes_written = (a->client_writer)(&a->archive, a->client_data,
307 state->child_buf, state->child_buf_avail);
308 if (bytes_written <= 0) {
309 ret = ARCHIVE_FATAL;
310 goto cleanup;
312 if ((size_t)bytes_written < state->child_buf_avail) {
313 memmove(state->child_buf,
314 state->child_buf + bytes_written,
315 state->child_buf_avail - bytes_written);
317 state->child_buf_avail -= bytes_written;
318 a->archive.raw_position += bytes_written;
321 /* XXX pad final compressed block. */
323 cleanup:
324 /* Shut down the child. */
325 if (state->child_stdin != -1)
326 close(state->child_stdin);
327 if (state->child_stdout != -1)
328 close(state->child_stdout);
329 while (waitpid(state->child, &status, 0) == -1 && errno == EINTR)
330 continue;
332 if (status != 0) {
333 archive_set_error(&a->archive, EIO,
334 "Filter exited with failure.");
335 ret = ARCHIVE_FATAL;
338 /* Release our configuration data. */
339 free(a->compressor.config);
340 a->compressor.config = NULL;
342 /* Release our private state data. */
343 free(state->child_buf);
344 free(state->description);
345 free(state);
346 return (ret);
349 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */