Add our READMEs.
[dragonfly/vkernel-mp.git] / contrib / libarchive-2.1 / libarchive / archive_write_set_compression_program.c
blob4f28a0feff27973ed474430ec2e201cc0f46f532
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$");
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
46 #include "archive.h"
47 #include "archive_private.h"
48 #include "archive_write_private.h"
50 #include "filter_fork.h"
52 struct private_data {
53 char *description;
54 pid_t child;
55 int child_stdin, child_stdout;
57 char *child_buf;
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.
69 int
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);
77 return (ARCHIVE_OK);
81 * Setup callback.
83 static int
84 archive_compressor_program_init(struct archive_write *a)
86 int ret;
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)
94 return (ret);
97 state = (struct private_data *)malloc(sizeof(*state));
98 if (state == NULL) {
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");
118 free(state);
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);
127 free(state);
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;
135 return (0);
138 static ssize_t
139 child_write(struct archive_write *a, const char *buf, size_t buf_len)
141 struct private_data *state = a->compressor.data;
142 ssize_t ret;
144 if (state->child_stdin == -1)
145 return (-1);
147 if (buf_len == 0)
148 return (-1);
150 restart_write:
151 do {
152 ret = write(state->child_stdin, buf, buf_len);
153 } while (ret == -1 && errno == EINTR);
155 if (ret > 0)
156 return (ret);
157 if (ret == 0) {
158 close(state->child_stdin);
159 state->child_stdin = -1;
160 fcntl(state->child_stdout, F_SETFL, 0);
161 return (0);
163 if (ret == -1 && errno != EAGAIN)
164 return (-1);
166 do {
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);
176 goto restart_write;
178 if (ret == -1 && errno == EAGAIN) {
179 __archive_check_child(state->child_stdin, state->child_stdout);
180 goto restart_write;
182 if (ret == -1)
183 return (-1);
185 state->child_buf_avail += ret;
187 ret = (a->client_writer)(&a->archive, a->client_data,
188 state->child_buf, state->child_buf_avail);
189 if (ret <= 0)
190 return (-1);
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;
198 goto restart_write;
202 * Write data to the compressed stream.
204 static int
205 archive_compressor_program_write(struct archive_write *a, const void *buff,
206 size_t length)
208 struct private_data *state;
209 ssize_t ret;
210 const char *buf;
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);
220 buf = buff;
221 while (length > 0) {
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);
228 length -= ret;
229 buf += ret;
232 a->archive.file_position += length;
233 return (ARCHIVE_OK);
238 * Finish the compression...
240 static int
241 archive_compressor_program_finish(struct archive_write *a)
243 int ret, status;
244 ssize_t bytes_read, bytes_written;
245 struct private_data *state;
247 state = (struct private_data *)a->compressor.data;
248 ret = 0;
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.");
253 ret = ARCHIVE_FATAL;
254 goto cleanup;
257 /* XXX pad compressed data. */
259 close(state->child_stdin);
260 state->child_stdin = -1;
261 fcntl(state->child_stdout, F_SETFL, 0);
263 for (;;) {
264 do {
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))
271 break;
273 if (bytes_read == -1) {
274 archive_set_error(&a->archive, errno,
275 "Read from filter failed unexpectedly.");
276 ret = ARCHIVE_FATAL;
277 goto cleanup;
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) {
284 ret = ARCHIVE_FATAL;
285 goto cleanup;
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. */
298 cleanup:
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)
305 continue;
307 if (status != 0) {
308 archive_set_error(&a->archive, EIO,
309 "Filter exited with failure.");
310 ret = ARCHIVE_FATAL;
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);
320 free(state);
321 return (ret);