Blame libarchive/archive_read_extract2.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
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: src/lib/libarchive/archive_read_extract.c,v 1.61 2008/05/26 17:00:22 kientzle Exp $");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_SYS_TYPES_H
Packit Service 1d0348
#include <sys/types.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_STRING_H
Packit Service 1d0348
#include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_entry.h"
Packit Service 1d0348
#include "archive_private.h"
Packit Service 1d0348
#include "archive_read_private.h"
Packit Service 1d0348
Packit Service 1d0348
static int	copy_data(struct archive *ar, struct archive *aw);
Packit Service 1d0348
static int	archive_read_extract_cleanup(struct archive_read *);
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
/* Retrieve an extract object without initialising the associated
Packit Service 1d0348
 * archive_write_disk object.
Packit Service 1d0348
 */
Packit Service 1d0348
struct archive_read_extract *
Packit Service 1d0348
__archive_read_get_extract(struct archive_read *a)
Packit Service 1d0348
{
Packit Service 1d0348
	if (a->extract == NULL) {
Packit Service 1d0348
		a->extract = (struct archive_read_extract *)calloc(1, sizeof(*a->extract));
Packit Service 1d0348
		if (a->extract == NULL) {
Packit Service 1d0348
			archive_set_error(&a->archive, ENOMEM, "Can't extract");
Packit Service 1d0348
			return (NULL);
Packit Service 1d0348
		}
Packit Service 1d0348
		a->cleanup_archive_extract = archive_read_extract_cleanup;
Packit Service 1d0348
	}
Packit Service 1d0348
	return (a->extract);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Cleanup function for archive_extract.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
archive_read_extract_cleanup(struct archive_read *a)
Packit Service 1d0348
{
Packit Service 1d0348
	int ret = ARCHIVE_OK;
Packit Service 1d0348
Packit Service 1d0348
	if (a->extract->ad != NULL) {
Packit Service 1d0348
		ret = archive_write_free(a->extract->ad);
Packit Service 1d0348
	}
Packit Service 1d0348
	free(a->extract);
Packit Service 1d0348
	a->extract = NULL;
Packit Service 1d0348
	return (ret);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_extract2(struct archive *_a, struct archive_entry *entry,
Packit Service 1d0348
    struct archive *ad)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_read *a = (struct archive_read *)_a;
Packit Service 1d0348
	int r, r2;
Packit Service 1d0348
Packit Service 1d0348
	/* Set up for this particular entry. */
Packit Service 1d0348
	if (a->skip_file_set)
Packit Service 1d0348
		archive_write_disk_set_skip_file(ad,
Packit Service 1d0348
		    a->skip_file_dev, a->skip_file_ino);
Packit Service 1d0348
	r = archive_write_header(ad, entry);
Packit Service 1d0348
	if (r < ARCHIVE_WARN)
Packit Service 1d0348
		r = ARCHIVE_WARN;
Packit Service 1d0348
	if (r != ARCHIVE_OK)
Packit Service 1d0348
		/* If _write_header failed, copy the error. */
Packit Service 1d0348
 		archive_copy_error(&a->archive, ad);
Packit Service 1d0348
	else if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) > 0)
Packit Service 1d0348
		/* Otherwise, pour data into the entry. */
Packit Service 1d0348
		r = copy_data(_a, ad);
Packit Service 1d0348
	r2 = archive_write_finish_entry(ad);
Packit Service 1d0348
	if (r2 < ARCHIVE_WARN)
Packit Service 1d0348
		r2 = ARCHIVE_WARN;
Packit Service 1d0348
	/* Use the first message. */
Packit Service 1d0348
	if (r2 != ARCHIVE_OK && r == ARCHIVE_OK)
Packit Service 1d0348
		archive_copy_error(&a->archive, ad);
Packit Service 1d0348
	/* Use the worst error return. */
Packit Service 1d0348
	if (r2 < r)
Packit Service 1d0348
		r = r2;
Packit Service 1d0348
	return (r);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
void
Packit Service 1d0348
archive_read_extract_set_progress_callback(struct archive *_a,
Packit Service 1d0348
    void (*progress_func)(void *), void *user_data)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_read *a = (struct archive_read *)_a;
Packit Service 1d0348
	struct archive_read_extract *extract = __archive_read_get_extract(a);
Packit Service 1d0348
	if (extract != NULL) {
Packit Service 1d0348
		extract->extract_progress = progress_func;
Packit Service 1d0348
		extract->extract_progress_user_data = user_data;
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
copy_data(struct archive *ar, struct archive *aw)
Packit Service 1d0348
{
Packit Service 1d0348
	int64_t offset;
Packit Service 1d0348
	const void *buff;
Packit Service 1d0348
	struct archive_read_extract *extract;
Packit Service 1d0348
	size_t size;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	extract = __archive_read_get_extract((struct archive_read *)ar);
Packit Service 1d0348
	if (extract == NULL)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	for (;;) {
Packit Service 1d0348
		r = archive_read_data_block(ar, &buff, &size, &offset);
Packit Service 1d0348
		if (r == ARCHIVE_EOF)
Packit Service 1d0348
			return (ARCHIVE_OK);
Packit Service 1d0348
		if (r != ARCHIVE_OK)
Packit Service 1d0348
			return (r);
Packit Service 1d0348
		r = (int)archive_write_data_block(aw, buff, size, offset);
Packit Service 1d0348
		if (r < ARCHIVE_WARN)
Packit Service 1d0348
			r = ARCHIVE_WARN;
Packit Service 1d0348
		if (r < ARCHIVE_OK) {
Packit Service 1d0348
			archive_set_error(ar, archive_errno(aw),
Packit Service 1d0348
			    "%s", archive_error_string(aw));
Packit Service 1d0348
			return (r);
Packit Service 1d0348
		}
Packit Service 1d0348
		if (extract->extract_progress)
Packit Service 1d0348
			(extract->extract_progress)
Packit Service 1d0348
			    (extract->extract_progress_user_data);
Packit Service 1d0348
	}
Packit Service 1d0348
}