Blame libarchive/archive_read_support_filter_program.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2007 Joerg Sonnenberger
Packit Service 1d0348
 * Copyright (c) 2012 Michihiro NAKAJIMA
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
__FBSDID("$FreeBSD$");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_SYS_WAIT_H
Packit Service 1d0348
#  include <sys/wait.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#  include <errno.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_FCNTL_H
Packit Service 1d0348
#  include <fcntl.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_LIMITS_H
Packit Service 1d0348
#  include <limits.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_SIGNAL_H
Packit Service 1d0348
#  include <signal.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#  include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STRING_H
Packit Service 1d0348
#  include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_UNISTD_H
Packit Service 1d0348
#  include <unistd.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_private.h"
Packit Service 1d0348
#include "archive_string.h"
Packit Service 1d0348
#include "archive_read_private.h"
Packit Service 1d0348
#include "filter_fork.h"
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
#if ARCHIVE_VERSION_NUMBER < 4000000
Packit Service 1d0348
/* Deprecated; remove in libarchive 4.0 */
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_compression_program(struct archive *a, const char *cmd)
Packit Service 1d0348
{
Packit Service 1d0348
	return archive_read_support_filter_program(a, cmd);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_compression_program_signature(struct archive *a,
Packit Service 1d0348
    const char *cmd, const void *signature, size_t signature_len)
Packit Service 1d0348
{
Packit Service 1d0348
	return archive_read_support_filter_program_signature(a,
Packit Service 1d0348
	    cmd, signature, signature_len);
Packit Service 1d0348
}
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_filter_program(struct archive *a, const char *cmd)
Packit Service 1d0348
{
Packit Service 1d0348
	return (archive_read_support_filter_program_signature(a, cmd, NULL, 0));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * The bidder object stores the command and the signature to watch for.
Packit Service 1d0348
 * The 'inhibit' entry here is used to ensure that unchecked filters never
Packit Service 1d0348
 * bid twice in the same pipeline.
Packit Service 1d0348
 */
Packit Service 1d0348
struct program_bidder {
Packit Service 1d0348
	char *description;
Packit Service 1d0348
	char *cmd;
Packit Service 1d0348
	void *signature;
Packit Service 1d0348
	size_t signature_len;
Packit Service 1d0348
	int inhibit;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
static int	program_bidder_bid(struct archive_read_filter_bidder *,
Packit Service 1d0348
		    struct archive_read_filter *upstream);
Packit Service 1d0348
static int	program_bidder_init(struct archive_read_filter *);
Packit Service 1d0348
static int	program_bidder_free(struct archive_read_filter_bidder *);
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * The actual filter needs to track input and output data.
Packit Service 1d0348
 */
Packit Service 1d0348
struct program_filter {
Packit Service 1d0348
	struct archive_string description;
Packit Service 1d0348
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit Service 1d0348
	HANDLE		 child;
Packit Service 1d0348
#else
Packit Service 1d0348
	pid_t		 child;
Packit Service 1d0348
#endif
Packit Service 1d0348
	int		 exit_status;
Packit Service 1d0348
	int		 waitpid_return;
Packit Service 1d0348
	int		 child_stdin, child_stdout;
Packit Service 1d0348
Packit Service 1d0348
	char		*out_buf;
Packit Service 1d0348
	size_t		 out_buf_len;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
static ssize_t	program_filter_read(struct archive_read_filter *,
Packit Service 1d0348
		    const void **);
Packit Service 1d0348
static int	program_filter_close(struct archive_read_filter *);
Packit Service 1d0348
static void	free_state(struct program_bidder *);
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
set_bidder_signature(struct archive_read_filter_bidder *bidder,
Packit Service 1d0348
    struct program_bidder *state, const void *signature, size_t signature_len)
Packit Service 1d0348
{
Packit Service 1d0348
Packit Service 1d0348
	if (signature != NULL && signature_len > 0) {
Packit Service 1d0348
		state->signature_len = signature_len;
Packit Service 1d0348
		state->signature = malloc(signature_len);
Packit Service 1d0348
		memcpy(state->signature, signature, signature_len);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Fill in the bidder object.
Packit Service 1d0348
	 */
Packit Service 1d0348
	bidder->data = state;
Packit Service 1d0348
	bidder->bid = program_bidder_bid;
Packit Service 1d0348
	bidder->init = program_bidder_init;
Packit Service 1d0348
	bidder->options = NULL;
Packit Service 1d0348
	bidder->free = program_bidder_free;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_filter_program_signature(struct archive *_a,
Packit Service 1d0348
    const char *cmd, const void *signature, size_t signature_len)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_read *a = (struct archive_read *)_a;
Packit Service 1d0348
	struct archive_read_filter_bidder *bidder;
Packit Service 1d0348
	struct program_bidder *state;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Get a bidder object from the read core.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Allocate our private state.
Packit Service 1d0348
	 */
Packit Service 1d0348
	state = (struct program_bidder *)calloc(1, sizeof (*state));
Packit Service 1d0348
	if (state == NULL)
Packit Service 1d0348
		goto memerr;
Packit Service 1d0348
	state->cmd = strdup(cmd);
Packit Service 1d0348
	if (state->cmd == NULL)
Packit Service 1d0348
		goto memerr;
Packit Service 1d0348
Packit Service 1d0348
	return set_bidder_signature(bidder, state, signature, signature_len);
Packit Service 1d0348
memerr:
Packit Service 1d0348
	free_state(state);
Packit Service 1d0348
	archive_set_error(_a, ENOMEM, "Can't allocate memory");
Packit Service 1d0348
	return (ARCHIVE_FATAL);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
program_bidder_free(struct archive_read_filter_bidder *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct program_bidder *state = (struct program_bidder *)self->data;
Packit Service 1d0348
Packit Service 1d0348
	free_state(state);
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
free_state(struct program_bidder *state)
Packit Service 1d0348
{
Packit Service 1d0348
Packit Service 1d0348
	if (state) {
Packit Service 1d0348
		free(state->cmd);
Packit Service 1d0348
		free(state->signature);
Packit Service 1d0348
		free(state);
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * If we do have a signature, bid only if that matches.
Packit Service 1d0348
 *
Packit Service 1d0348
 * If there's no signature, we bid INT_MAX the first time
Packit Service 1d0348
 * we're called, then never bid again.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
program_bidder_bid(struct archive_read_filter_bidder *self,
Packit Service 1d0348
    struct archive_read_filter *upstream)
Packit Service 1d0348
{
Packit Service 1d0348
	struct program_bidder *state = self->data;
Packit Service 1d0348
	const char *p;
Packit Service 1d0348
Packit Service 1d0348
	/* If we have a signature, use that to match. */
Packit Service 1d0348
	if (state->signature_len > 0) {
Packit Service 1d0348
		p = __archive_read_filter_ahead(upstream,
Packit Service 1d0348
		    state->signature_len, NULL);
Packit Service 1d0348
		if (p == NULL)
Packit Service 1d0348
			return (0);
Packit Service 1d0348
		/* No match, so don't bid. */
Packit Service 1d0348
		if (memcmp(p, state->signature, state->signature_len) != 0)
Packit Service 1d0348
			return (0);
Packit Service 1d0348
		return ((int)state->signature_len * 8);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Otherwise, bid once and then never bid again. */
Packit Service 1d0348
	if (state->inhibit)
Packit Service 1d0348
		return (0);
Packit Service 1d0348
	state->inhibit = 1;
Packit Service 1d0348
	return (INT_MAX);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Shut down the child, return ARCHIVE_OK if it exited normally.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Note that the return value is sticky; if we're called again,
Packit Service 1d0348
 * we won't reap the child again, but we will return the same status
Packit Service 1d0348
 * (including error message if the child came to a bad end).
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
child_stop(struct archive_read_filter *self, struct program_filter *state)
Packit Service 1d0348
{
Packit Service 1d0348
	/* Close our side of the I/O with the child. */
Packit Service 1d0348
	if (state->child_stdin != -1) {
Packit Service 1d0348
		close(state->child_stdin);
Packit Service 1d0348
		state->child_stdin = -1;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (state->child_stdout != -1) {
Packit Service 1d0348
		close(state->child_stdout);
Packit Service 1d0348
		state->child_stdout = -1;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (state->child != 0) {
Packit Service 1d0348
		/* Reap the child. */
Packit Service 1d0348
		do {
Packit Service 1d0348
			state->waitpid_return
Packit Service 1d0348
			    = waitpid(state->child, &state->exit_status, 0);
Packit Service 1d0348
		} while (state->waitpid_return == -1 && errno == EINTR);
Packit Service 1d0348
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit Service 1d0348
		CloseHandle(state->child);
Packit Service 1d0348
#endif
Packit Service 1d0348
		state->child = 0;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (state->waitpid_return < 0) {
Packit Service 1d0348
		/* waitpid() failed?  This is ugly. */
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Child process exited badly");
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
#if !defined(_WIN32) || defined(__CYGWIN__)
Packit Service 1d0348
	if (WIFSIGNALED(state->exit_status)) {
Packit Service 1d0348
#ifdef SIGPIPE
Packit Service 1d0348
		/* If the child died because we stopped reading before
Packit Service 1d0348
		 * it was done, that's okay.  Some archive formats
Packit Service 1d0348
		 * have padding at the end that we routinely ignore. */
Packit Service 1d0348
		/* The alternative to this would be to add a step
Packit Service 1d0348
		 * before close(child_stdout) above to read from the
Packit Service 1d0348
		 * child until the child has no more to write. */
Packit Service 1d0348
		if (WTERMSIG(state->exit_status) == SIGPIPE)
Packit Service 1d0348
			return (ARCHIVE_OK);
Packit Service 1d0348
#endif
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Child process exited with signal %d",
Packit Service 1d0348
		    WTERMSIG(state->exit_status));
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
#endif /* !_WIN32 || __CYGWIN__ */
Packit Service 1d0348
Packit Service 1d0348
	if (WIFEXITED(state->exit_status)) {
Packit Service 1d0348
		if (WEXITSTATUS(state->exit_status) == 0)
Packit Service 1d0348
			return (ARCHIVE_OK);
Packit Service 1d0348
Packit Service 1d0348
		archive_set_error(&self->archive->archive,
Packit Service 1d0348
		    ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Child process exited with status %d",
Packit Service 1d0348
		    WEXITSTATUS(state->exit_status));
Packit Service 1d0348
		return (ARCHIVE_WARN);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	return (ARCHIVE_WARN);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Use select() to decide whether the child is ready for read or write.
Packit Service 1d0348
 */
Packit Service 1d0348
static ssize_t
Packit Service 1d0348
child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
Packit Service 1d0348
{
Packit Service 1d0348
	struct program_filter *state = self->data;
Packit Service 1d0348
	ssize_t ret, requested, avail;
Packit Service 1d0348
	const char *p;
Packit Service 1d0348
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit Service 1d0348
	HANDLE handle = (HANDLE)_get_osfhandle(state->child_stdout);
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
	requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
Packit Service 1d0348
Packit Service 1d0348
	for (;;) {
Packit Service 1d0348
		do {
Packit Service 1d0348
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit Service 1d0348
			/* Avoid infinity wait.
Packit Service 1d0348
			 * Note: If there is no data in the pipe, ReadFile()
Packit Service 1d0348
			 * called in read() never returns and so we won't
Packit Service 1d0348
			 * write remaining encoded data to the pipe.
Packit Service 1d0348
			 * Note: This way may cause performance problem.
Packit Service 1d0348
			 * we are looking forward to great code to resolve
Packit Service 1d0348
			 * this.  */
Packit Service 1d0348
			DWORD pipe_avail = -1;
Packit Service 1d0348
			int cnt = 2;
Packit Service 1d0348
Packit Service 1d0348
			while (PeekNamedPipe(handle, NULL, 0, NULL,
Packit Service 1d0348
			    &pipe_avail, NULL) != 0 && pipe_avail == 0 &&
Packit Service 1d0348
			    cnt--)
Packit Service 1d0348
				Sleep(5);
Packit Service 1d0348
			if (pipe_avail == 0) {
Packit Service 1d0348
				ret = -1;
Packit Service 1d0348
				errno = EAGAIN;
Packit Service 1d0348
				break;
Packit Service 1d0348
			}
Packit Service 1d0348
#endif
Packit Service 1d0348
			ret = read(state->child_stdout, buf, requested);
Packit Service 1d0348
		} while (ret == -1 && errno == EINTR);
Packit Service 1d0348
Packit Service 1d0348
		if (ret > 0)
Packit Service 1d0348
			return (ret);
Packit Service 1d0348
		if (ret == 0 || (ret == -1 && errno == EPIPE))
Packit Service 1d0348
			/* Child has closed its output; reap the child
Packit Service 1d0348
			 * and return the status. */
Packit Service 1d0348
			return (child_stop(self, state));
Packit Service 1d0348
		if (ret == -1 && errno != EAGAIN)
Packit Service 1d0348
			return (-1);
Packit Service 1d0348
Packit Service 1d0348
		if (state->child_stdin == -1) {
Packit Service 1d0348
			/* Block until child has some I/O ready. */
Packit Service 1d0348
			__archive_check_child(state->child_stdin,
Packit Service 1d0348
			    state->child_stdout);
Packit Service 1d0348
			continue;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		/* Get some more data from upstream. */
Packit Service 1d0348
		p = __archive_read_filter_ahead(self->upstream, 1, &avail);
Packit Service 1d0348
		if (p == NULL) {
Packit Service 1d0348
			close(state->child_stdin);
Packit Service 1d0348
			state->child_stdin = -1;
Packit Service 1d0348
			fcntl(state->child_stdout, F_SETFL, 0);
Packit Service 1d0348
			if (avail < 0)
Packit Service 1d0348
				return (avail);
Packit Service 1d0348
			continue;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		do {
Packit Service 1d0348
			ret = write(state->child_stdin, p, avail);
Packit Service 1d0348
		} while (ret == -1 && errno == EINTR);
Packit Service 1d0348
Packit Service 1d0348
		if (ret > 0) {
Packit Service 1d0348
			/* Consume whatever we managed to write. */
Packit Service 1d0348
			__archive_read_filter_consume(self->upstream, ret);
Packit Service 1d0348
		} else if (ret == -1 && errno == EAGAIN) {
Packit Service 1d0348
			/* Block until child has some I/O ready. */
Packit Service 1d0348
			__archive_check_child(state->child_stdin,
Packit Service 1d0348
			    state->child_stdout);
Packit Service 1d0348
		} else {
Packit Service 1d0348
			/* Write failed. */
Packit Service 1d0348
			close(state->child_stdin);
Packit Service 1d0348
			state->child_stdin = -1;
Packit Service 1d0348
			fcntl(state->child_stdout, F_SETFL, 0);
Packit Service 1d0348
			/* If it was a bad error, we're done; otherwise
Packit Service 1d0348
			 * it was EPIPE or EOF, and we can still read
Packit Service 1d0348
			 * from the child. */
Packit Service 1d0348
			if (ret == -1 && errno != EPIPE)
Packit Service 1d0348
				return (-1);
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
__archive_read_program(struct archive_read_filter *self, const char *cmd)
Packit Service 1d0348
{
Packit Service 1d0348
	struct program_filter	*state;
Packit Service 1d0348
	static const size_t out_buf_len = 65536;
Packit Service 1d0348
	char *out_buf;
Packit Service 1d0348
	const char *prefix = "Program: ";
Packit Service 1d0348
	pid_t child;
Packit Service 1d0348
	size_t l;
Packit Service 1d0348
Packit Service 1d0348
	l = strlen(prefix) + strlen(cmd) + 1;
Packit Service 1d0348
	state = (struct program_filter *)calloc(1, sizeof(*state));
Packit Service 1d0348
	out_buf = (char *)malloc(out_buf_len);
Packit Service 1d0348
	if (state == NULL || out_buf == NULL ||
Packit Service 1d0348
	    archive_string_ensure(&state->description, l) == NULL) {
Packit Service 1d0348
		archive_set_error(&self->archive->archive, ENOMEM,
Packit Service 1d0348
		    "Can't allocate input data");
Packit Service 1d0348
		if (state != NULL) {
Packit Service 1d0348
			archive_string_free(&state->description);
Packit Service 1d0348
			free(state);
Packit Service 1d0348
		}
Packit Service 1d0348
		free(out_buf);
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	archive_strcpy(&state->description, prefix);
Packit Service 1d0348
	archive_strcat(&state->description, cmd);
Packit Service 1d0348
Packit Service 1d0348
	self->code = ARCHIVE_FILTER_PROGRAM;
Packit Service 1d0348
	self->name = state->description.s;
Packit Service 1d0348
Packit Service 1d0348
	state->out_buf = out_buf;
Packit Service 1d0348
	state->out_buf_len = out_buf_len;
Packit Service 1d0348
Packit Service 1d0348
	child = __archive_create_child(cmd, &state->child_stdin,
Packit Service 1d0348
	    &state->child_stdout);
Packit Service 1d0348
	if (child == -1) {
Packit Service 1d0348
		free(state->out_buf);
Packit Service 1d0348
		archive_string_free(&state->description);
Packit Service 1d0348
		free(state);
Packit Service 1d0348
		archive_set_error(&self->archive->archive, EINVAL,
Packit Service 1d0348
		    "Can't initialize filter; unable to run program \"%s\"",
Packit Service 1d0348
		    cmd);
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit Service 1d0348
	state->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
Packit Service 1d0348
	if (state->child == NULL) {
Packit Service 1d0348
		child_stop(self, state);
Packit Service 1d0348
		free(state->out_buf);
Packit Service 1d0348
		archive_string_free(&state->description);
Packit Service 1d0348
		free(state);
Packit Service 1d0348
		archive_set_error(&self->archive->archive, EINVAL,
Packit Service 1d0348
		    "Can't initialize filter; unable to run program \"%s\"",
Packit Service 1d0348
		    cmd);
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
#else
Packit Service 1d0348
	state->child = child;
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
	self->data = state;
Packit Service 1d0348
	self->read = program_filter_read;
Packit Service 1d0348
	self->skip = NULL;
Packit Service 1d0348
	self->close = program_filter_close;
Packit Service 1d0348
Packit Service 1d0348
	/* XXX Check that we can read at least one byte? */
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
program_bidder_init(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct program_bidder   *bidder_state;
Packit Service 1d0348
Packit Service 1d0348
	bidder_state = (struct program_bidder *)self->bidder->data;
Packit Service 1d0348
	return (__archive_read_program(self, bidder_state->cmd));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static ssize_t
Packit Service 1d0348
program_filter_read(struct archive_read_filter *self, const void **buff)
Packit Service 1d0348
{
Packit Service 1d0348
	struct program_filter *state;
Packit Service 1d0348
	ssize_t bytes;
Packit Service 1d0348
	size_t total;
Packit Service 1d0348
	char *p;
Packit Service 1d0348
Packit Service 1d0348
	state = (struct program_filter *)self->data;
Packit Service 1d0348
Packit Service 1d0348
	total = 0;
Packit Service 1d0348
	p = state->out_buf;
Packit Service 1d0348
	while (state->child_stdout != -1 && total < state->out_buf_len) {
Packit Service 1d0348
		bytes = child_read(self, p, state->out_buf_len - total);
Packit Service 1d0348
		if (bytes < 0)
Packit Service 1d0348
			/* No recovery is possible if we can no longer
Packit Service 1d0348
			 * read from the child. */
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		if (bytes == 0)
Packit Service 1d0348
			/* We got EOF from the child. */
Packit Service 1d0348
			break;
Packit Service 1d0348
		total += bytes;
Packit Service 1d0348
		p += bytes;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	*buff = state->out_buf;
Packit Service 1d0348
	return (total);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
program_filter_close(struct archive_read_filter *self)
Packit Service 1d0348
{
Packit Service 1d0348
	struct program_filter	*state;
Packit Service 1d0348
	int e;
Packit Service 1d0348
Packit Service 1d0348
	state = (struct program_filter *)self->data;
Packit Service 1d0348
	e = child_stop(self, state);
Packit Service 1d0348
Packit Service 1d0348
	/* Release our private data. */
Packit Service 1d0348
	free(state->out_buf);
Packit Service 1d0348
	archive_string_free(&state->description);
Packit Service 1d0348
	free(state);
Packit Service 1d0348
Packit Service 1d0348
	return (e);
Packit Service 1d0348
}