2007-05-29 01:00:21 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2007 Joerg Sonnenberger
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "archive_platform.h"
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
# include <errno.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
# include <limits.h>
|
|
|
|
#endif
|
2009-04-12 05:47:23 +00:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
# include <signal.h>
|
|
|
|
#endif
|
2007-05-29 01:00:21 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_private.h"
|
|
|
|
#include "archive_read_private.h"
|
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
int
|
|
|
|
archive_read_support_compression_program(struct archive *a, const char *cmd)
|
|
|
|
{
|
|
|
|
return (archive_read_support_compression_program_signature(a, cmd, NULL, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This capability is only available on POSIX systems. */
|
|
|
|
#if (!defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
|
2009-04-12 05:33:34 +00:00
|
|
|
!(defined(HAVE_FORK) || defined(HAVE_VFORK))) && (!defined(_WIN32) || defined(__CYGWIN__))
|
2009-03-07 02:47:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* On non-Posix systems, allow the program to build, but choke if
|
|
|
|
* this function is actually invoked.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
archive_read_support_compression_program_signature(struct archive *_a,
|
|
|
|
const char *cmd, void *signature, size_t signature_len)
|
|
|
|
{
|
|
|
|
(void)_a; /* UNUSED */
|
|
|
|
(void)cmd; /* UNUSED */
|
|
|
|
(void)signature; /* UNUSED */
|
|
|
|
(void)signature_len; /* UNUSED */
|
|
|
|
|
|
|
|
archive_set_error(_a, -1,
|
|
|
|
"External compression programs not supported on this platform");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__archive_read_program(struct archive_read_filter *self, const char *cmd)
|
|
|
|
{
|
|
|
|
(void)self; /* UNUSED */
|
|
|
|
(void)cmd; /* UNUSED */
|
|
|
|
|
|
|
|
archive_set_error(&self->archive->archive, -1,
|
|
|
|
"External compression programs not supported on this platform");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
#include "filter_fork.h"
|
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
/*
|
|
|
|
* The bidder object stores the command and the signature to watch for.
|
|
|
|
* The 'inhibit' entry here is used to ensure that unchecked filters never
|
|
|
|
* bid twice in the same pipeline.
|
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_bidder {
|
2008-12-06 06:45:15 +00:00
|
|
|
char *cmd;
|
2009-03-07 02:47:04 +00:00
|
|
|
void *signature;
|
|
|
|
size_t signature_len;
|
|
|
|
int inhibit;
|
2008-12-06 06:45:15 +00:00
|
|
|
};
|
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
static int program_bidder_bid(struct archive_read_filter_bidder *,
|
|
|
|
struct archive_read_filter *upstream);
|
|
|
|
static int program_bidder_init(struct archive_read_filter *);
|
|
|
|
static int program_bidder_free(struct archive_read_filter_bidder *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The actual filter needs to track input and output data.
|
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_filter {
|
2007-05-29 01:00:21 +00:00
|
|
|
char *description;
|
|
|
|
pid_t child;
|
2009-04-12 05:47:23 +00:00
|
|
|
int exit_status;
|
|
|
|
int waitpid_return;
|
2007-05-29 01:00:21 +00:00
|
|
|
int child_stdin, child_stdout;
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
char *out_buf;
|
|
|
|
size_t out_buf_len;
|
2007-05-29 01:00:21 +00:00
|
|
|
};
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
static ssize_t program_filter_read(struct archive_read_filter *,
|
|
|
|
const void **);
|
|
|
|
static int program_filter_close(struct archive_read_filter *);
|
2008-12-06 06:45:15 +00:00
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
int
|
2009-03-07 02:47:04 +00:00
|
|
|
archive_read_support_compression_program_signature(struct archive *_a,
|
|
|
|
const char *cmd, const void *signature, size_t signature_len)
|
2007-05-29 01:00:21 +00:00
|
|
|
{
|
|
|
|
struct archive_read *a = (struct archive_read *)_a;
|
2009-03-07 02:47:04 +00:00
|
|
|
struct archive_read_filter_bidder *bidder;
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_bidder *state;
|
2007-05-29 01:00:21 +00:00
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
/*
|
|
|
|
* Get a bidder object from the read core.
|
|
|
|
*/
|
|
|
|
bidder = __archive_read_get_bidder(a);
|
2009-03-06 04:22:34 +00:00
|
|
|
if (bidder == NULL)
|
|
|
|
return (ARCHIVE_FATAL);
|
2007-05-29 01:00:21 +00:00
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
/*
|
|
|
|
* Allocate our private state.
|
|
|
|
*/
|
2009-03-06 04:22:34 +00:00
|
|
|
state = (struct program_bidder *)calloc(sizeof (*state), 1);
|
2008-12-06 06:45:15 +00:00
|
|
|
if (state == NULL)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
state->cmd = strdup(cmd);
|
2009-03-07 02:47:04 +00:00
|
|
|
if (signature != NULL && signature_len > 0) {
|
|
|
|
state->signature_len = signature_len;
|
|
|
|
state->signature = malloc(signature_len);
|
|
|
|
memcpy(state->signature, signature, signature_len);
|
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
/*
|
|
|
|
* Fill in the bidder object.
|
|
|
|
*/
|
2009-03-05 02:19:42 +00:00
|
|
|
bidder->data = state;
|
|
|
|
bidder->bid = program_bidder_bid;
|
|
|
|
bidder->init = program_bidder_init;
|
2009-03-06 05:58:56 +00:00
|
|
|
bidder->options = NULL;
|
2009-03-05 02:19:42 +00:00
|
|
|
bidder->free = program_bidder_free;
|
2008-12-06 06:45:15 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-03-05 02:19:42 +00:00
|
|
|
program_bidder_free(struct archive_read_filter_bidder *self)
|
2008-12-06 06:45:15 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_bidder *state = (struct program_bidder *)self->data;
|
|
|
|
free(state->cmd);
|
2009-03-07 02:47:04 +00:00
|
|
|
free(state->signature);
|
2008-12-06 06:45:15 +00:00
|
|
|
free(self->data);
|
2007-05-29 01:00:21 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-03-07 02:47:04 +00:00
|
|
|
* If we do have a signature, bid only if that matches.
|
|
|
|
*
|
|
|
|
* If there's no signature, we bid INT_MAX the first time
|
|
|
|
* we're called, then never bid again.
|
2007-05-29 01:00:21 +00:00
|
|
|
*/
|
|
|
|
static int
|
2009-03-05 02:19:42 +00:00
|
|
|
program_bidder_bid(struct archive_read_filter_bidder *self,
|
|
|
|
struct archive_read_filter *upstream)
|
2007-05-29 01:00:21 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_bidder *state = self->data;
|
2009-03-07 02:47:04 +00:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
/* If we have a signature, use that to match. */
|
|
|
|
if (state->signature_len > 0) {
|
|
|
|
p = __archive_read_filter_ahead(upstream,
|
|
|
|
state->signature_len, NULL);
|
|
|
|
if (p == NULL)
|
|
|
|
return (0);
|
|
|
|
/* No match, so don't bid. */
|
|
|
|
if (memcmp(p, state->signature, state->signature_len) != 0)
|
|
|
|
return (0);
|
|
|
|
return (state->signature_len * 8);
|
|
|
|
}
|
2008-12-06 06:45:15 +00:00
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
/* Otherwise, bid once and then never bid again. */
|
|
|
|
if (state->inhibit)
|
|
|
|
return (0);
|
|
|
|
state->inhibit = 1;
|
|
|
|
return (INT_MAX);
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2009-04-12 05:47:23 +00:00
|
|
|
/*
|
|
|
|
* Shut down the child, return ARCHIVE_OK if it exited normally.
|
|
|
|
*
|
|
|
|
* Note that the return value is sticky; if we're called again,
|
|
|
|
* we won't reap the child again, but we will return the same status
|
|
|
|
* (including error message if the child came to a bad end).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
child_stop(struct archive_read_filter *self, struct program_filter *state)
|
|
|
|
{
|
|
|
|
/* Close our side of the I/O with the child. */
|
|
|
|
if (state->child_stdin != -1) {
|
|
|
|
close(state->child_stdin);
|
|
|
|
state->child_stdin = -1;
|
|
|
|
}
|
|
|
|
if (state->child_stdout != -1) {
|
|
|
|
close(state->child_stdout);
|
|
|
|
state->child_stdout = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->child != 0) {
|
|
|
|
/* Reap the child. */
|
|
|
|
do {
|
|
|
|
state->waitpid_return
|
|
|
|
= waitpid(state->child, &state->exit_status, 0);
|
|
|
|
} while (state->waitpid_return == -1 && errno == EINTR);
|
|
|
|
state->child = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->waitpid_return < 0) {
|
|
|
|
/* waitpid() failed? This is ugly. */
|
|
|
|
archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Child process exited badly");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFSIGNALED(state->exit_status)) {
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
/* If the child died because we stopped reading before
|
|
|
|
* it was done, that's okay. Some archive formats
|
|
|
|
* have padding at the end that we routinely ignore. */
|
|
|
|
/* The alternative to this would be to add a step
|
|
|
|
* before close(child_stdout) above to read from the
|
|
|
|
* child until the child has no more to write. */
|
|
|
|
if (WTERMSIG(state->exit_status) == SIGPIPE)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
#endif
|
|
|
|
archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Child process exited with signal %d",
|
|
|
|
WTERMSIG(state->exit_status));
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(state->exit_status)) {
|
|
|
|
if (WEXITSTATUS(state->exit_status) == 0)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
|
|
|
|
archive_set_error(&self->archive->archive,
|
|
|
|
ARCHIVE_ERRNO_MISC,
|
|
|
|
"Child process exited with status %d",
|
|
|
|
WEXITSTATUS(state->exit_status));
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
/*
|
|
|
|
* Use select() to decide whether the child is ready for read or write.
|
|
|
|
*/
|
2007-05-29 01:00:21 +00:00
|
|
|
static ssize_t
|
2009-03-05 02:19:42 +00:00
|
|
|
child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
|
2007-05-29 01:00:21 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_filter *state = self->data;
|
2009-03-07 02:47:04 +00:00
|
|
|
ssize_t ret, requested, avail;
|
|
|
|
const char *p;
|
2007-05-29 01:00:21 +00:00
|
|
|
|
|
|
|
requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
|
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
for (;;) {
|
|
|
|
do {
|
|
|
|
ret = read(state->child_stdout, buf, requested);
|
|
|
|
} while (ret == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
return (ret);
|
2009-04-12 05:47:23 +00:00
|
|
|
if (ret == 0 || (ret == -1 && errno == EPIPE))
|
|
|
|
/* Child has closed its output; reap the child
|
|
|
|
* and return the status. */
|
|
|
|
return (child_stop(self, state));
|
2009-03-07 02:47:04 +00:00
|
|
|
if (ret == -1 && errno != EAGAIN)
|
|
|
|
return (-1);
|
2007-05-29 01:00:21 +00:00
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
if (state->child_stdin == -1) {
|
|
|
|
/* Block until child has some I/O ready. */
|
|
|
|
__archive_check_child(state->child_stdin,
|
|
|
|
state->child_stdout);
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-29 01:00:21 +00:00
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
/* Get some more data from upstream. */
|
|
|
|
p = __archive_read_filter_ahead(self->upstream, 1, &avail);
|
|
|
|
if (p == NULL) {
|
2007-05-29 01:00:21 +00:00
|
|
|
close(state->child_stdin);
|
|
|
|
state->child_stdin = -1;
|
|
|
|
fcntl(state->child_stdout, F_SETFL, 0);
|
2009-03-07 02:47:04 +00:00
|
|
|
if (avail < 0)
|
|
|
|
return (avail);
|
|
|
|
continue;
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
2009-03-07 02:47:04 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
ret = write(state->child_stdin, p, avail);
|
|
|
|
} while (ret == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
/* Consume whatever we managed to write. */
|
|
|
|
__archive_read_filter_consume(self->upstream, ret);
|
|
|
|
} else if (ret == -1 && errno == EAGAIN) {
|
|
|
|
/* Block until child has some I/O ready. */
|
|
|
|
__archive_check_child(state->child_stdin,
|
|
|
|
state->child_stdout);
|
|
|
|
} else {
|
|
|
|
/* Write failed. */
|
2007-05-29 01:00:21 +00:00
|
|
|
close(state->child_stdin);
|
|
|
|
state->child_stdin = -1;
|
|
|
|
fcntl(state->child_stdout, F_SETFL, 0);
|
2009-03-07 02:47:04 +00:00
|
|
|
/* If it was a bad error, we're done; otherwise
|
|
|
|
* it was EPIPE or EOF, and we can still read
|
|
|
|
* from the child. */
|
|
|
|
if (ret == -1 && errno != EPIPE)
|
|
|
|
return (-1);
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
int
|
|
|
|
__archive_read_program(struct archive_read_filter *self, const char *cmd)
|
2007-05-29 01:00:21 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_filter *state;
|
2008-12-06 06:45:15 +00:00
|
|
|
static const size_t out_buf_len = 65536;
|
|
|
|
char *out_buf;
|
|
|
|
char *description;
|
2007-05-29 01:00:21 +00:00
|
|
|
const char *prefix = "Program: ";
|
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
state = (struct program_filter *)calloc(1, sizeof(*state));
|
2008-12-06 06:45:15 +00:00
|
|
|
out_buf = (char *)malloc(out_buf_len);
|
2009-03-07 02:47:04 +00:00
|
|
|
description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
|
2009-03-05 02:19:42 +00:00
|
|
|
if (state == NULL || out_buf == NULL || description == NULL) {
|
|
|
|
archive_set_error(&self->archive->archive, ENOMEM,
|
2007-05-29 01:00:21 +00:00
|
|
|
"Can't allocate input data");
|
2008-12-06 06:45:15 +00:00
|
|
|
free(state);
|
|
|
|
free(out_buf);
|
|
|
|
free(description);
|
2009-03-05 02:19:42 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
self->code = ARCHIVE_COMPRESSION_PROGRAM;
|
2008-12-06 06:45:15 +00:00
|
|
|
state->description = description;
|
2007-05-29 01:00:21 +00:00
|
|
|
strcpy(state->description, prefix);
|
2009-03-07 02:47:04 +00:00
|
|
|
strcat(state->description, cmd);
|
2009-03-05 02:19:42 +00:00
|
|
|
self->name = state->description;
|
2007-05-29 01:00:21 +00:00
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
state->out_buf = out_buf;
|
|
|
|
state->out_buf_len = out_buf_len;
|
2007-05-29 01:00:21 +00:00
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
if ((state->child = __archive_create_child(cmd,
|
2007-05-29 01:00:21 +00:00
|
|
|
&state->child_stdin, &state->child_stdout)) == -1) {
|
2008-12-06 06:45:15 +00:00
|
|
|
free(state->out_buf);
|
2007-05-29 01:00:21 +00:00
|
|
|
free(state);
|
2009-03-05 02:19:42 +00:00
|
|
|
archive_set_error(&self->archive->archive, EINVAL,
|
2007-05-29 01:00:21 +00:00
|
|
|
"Can't initialise filter");
|
2009-03-05 02:19:42 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
self->data = state;
|
2009-03-05 02:19:42 +00:00
|
|
|
self->read = program_filter_read;
|
2008-12-06 06:45:15 +00:00
|
|
|
self->skip = NULL;
|
2009-03-05 02:19:42 +00:00
|
|
|
self->close = program_filter_close;
|
2007-05-29 01:00:21 +00:00
|
|
|
|
|
|
|
/* XXX Check that we can read at least one byte? */
|
2009-03-05 02:19:42 +00:00
|
|
|
return (ARCHIVE_OK);
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 02:47:04 +00:00
|
|
|
static int
|
|
|
|
program_bidder_init(struct archive_read_filter *self)
|
|
|
|
{
|
|
|
|
struct program_bidder *bidder_state;
|
|
|
|
|
|
|
|
bidder_state = (struct program_bidder *)self->bidder->data;
|
|
|
|
return (__archive_read_program(self, bidder_state->cmd));
|
|
|
|
}
|
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
static ssize_t
|
2009-03-05 02:19:42 +00:00
|
|
|
program_filter_read(struct archive_read_filter *self, const void **buff)
|
2007-05-29 01:00:21 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_filter *state;
|
2009-03-07 02:47:04 +00:00
|
|
|
ssize_t bytes;
|
|
|
|
size_t total;
|
2008-12-06 06:45:15 +00:00
|
|
|
char *p;
|
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
state = (struct program_filter *)self->data;
|
2008-12-06 06:45:15 +00:00
|
|
|
|
|
|
|
total = 0;
|
|
|
|
p = state->out_buf;
|
2009-03-07 02:47:04 +00:00
|
|
|
while (state->child_stdout != -1 && total < state->out_buf_len) {
|
2008-12-06 06:45:15 +00:00
|
|
|
bytes = child_read(self, p, state->out_buf_len - total);
|
|
|
|
if (bytes < 0)
|
2009-04-12 05:47:23 +00:00
|
|
|
/* No recovery is possible if we can no longer
|
|
|
|
* read from the child. */
|
|
|
|
return (ARCHIVE_FATAL);
|
2008-12-06 06:45:15 +00:00
|
|
|
if (bytes == 0)
|
2009-04-12 05:47:23 +00:00
|
|
|
/* We got EOF from the child. */
|
2007-05-29 01:00:21 +00:00
|
|
|
break;
|
2008-12-06 06:45:15 +00:00
|
|
|
total += bytes;
|
2009-03-07 02:47:04 +00:00
|
|
|
p += bytes;
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 06:45:15 +00:00
|
|
|
*buff = state->out_buf;
|
|
|
|
return (total);
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-03-05 02:19:42 +00:00
|
|
|
program_filter_close(struct archive_read_filter *self)
|
2007-05-29 01:00:21 +00:00
|
|
|
{
|
2009-03-05 02:19:42 +00:00
|
|
|
struct program_filter *state;
|
2009-04-12 05:47:23 +00:00
|
|
|
int e;
|
2007-05-29 01:00:21 +00:00
|
|
|
|
2009-03-05 02:19:42 +00:00
|
|
|
state = (struct program_filter *)self->data;
|
2009-04-12 05:47:23 +00:00
|
|
|
e = child_stop(self, state);
|
2007-05-29 01:00:21 +00:00
|
|
|
|
|
|
|
/* Release our private data. */
|
2008-12-06 06:45:15 +00:00
|
|
|
free(state->out_buf);
|
2007-05-29 01:00:21 +00:00
|
|
|
free(state->description);
|
|
|
|
free(state);
|
|
|
|
|
2009-04-12 05:47:23 +00:00
|
|
|
return (e);
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
2008-05-26 17:00:24 +00:00
|
|
|
|
|
|
|
#endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */
|