2
* Copyright (c) 2003-2007 Tim Kientzle
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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"
27
__FBSDID("$FreeBSD: src/lib/libarchive/archive_read_extract.c,v 1.61 2008/05/26 17:00:22 kientzle Exp $");
29
#ifdef HAVE_SYS_TYPES_H
30
#include <sys/types.h>
43
#include "archive_private.h"
44
#include "archive_read_private.h"
45
#include "archive_write_disk_private.h"
48
struct archive *ad; /* archive_write_disk object */
50
/* Progress function invoked during extract. */
51
void (*extract_progress)(void *);
52
void *extract_progress_user_data;
55
static int archive_read_extract_cleanup(struct archive_read *);
56
static int copy_data(struct archive *ar, struct archive *aw);
57
static struct extract *get_extract(struct archive_read *);
59
static struct extract *
60
get_extract(struct archive_read *a)
62
/* If we haven't initialized, do it now. */
63
/* This also sets up a lot of global state. */
64
if (a->extract == NULL) {
65
a->extract = (struct extract *)malloc(sizeof(*a->extract));
66
if (a->extract == NULL) {
67
archive_set_error(&a->archive, ENOMEM, "Can't extract");
70
memset(a->extract, 0, sizeof(*a->extract));
71
a->extract->ad = archive_write_disk_new();
72
if (a->extract->ad == NULL) {
73
archive_set_error(&a->archive, ENOMEM, "Can't extract");
76
archive_write_disk_set_standard_lookup(a->extract->ad);
77
a->cleanup_archive_extract = archive_read_extract_cleanup;
83
archive_read_extract(struct archive *_a, struct archive_entry *entry, int flags)
85
struct extract *extract;
87
extract = get_extract((struct archive_read *)_a);
89
return (ARCHIVE_FATAL);
90
archive_write_disk_set_options(extract->ad, flags);
91
return (archive_read_extract2(_a, entry, extract->ad));
95
archive_read_extract2(struct archive *_a, struct archive_entry *entry,
98
struct archive_read *a = (struct archive_read *)_a;
101
/* Set up for this particular entry. */
102
archive_write_disk_set_skip_file(ad,
103
a->skip_file_dev, a->skip_file_ino);
104
r = archive_write_header(ad, entry);
105
if (r < ARCHIVE_WARN)
108
/* If _write_header failed, copy the error. */
109
archive_copy_error(&a->archive, ad);
111
/* Otherwise, pour data into the entry. */
112
r = copy_data(_a, ad);
113
r2 = archive_write_finish_entry(ad);
114
if (r2 < ARCHIVE_WARN)
116
/* Use the first message. */
117
if (r2 != ARCHIVE_OK && r == ARCHIVE_OK)
118
archive_copy_error(&a->archive, ad);
119
/* Use the worst error return. */
126
archive_read_extract_set_progress_callback(struct archive *_a,
127
void (*progress_func)(void *), void *user_data)
129
struct archive_read *a = (struct archive_read *)_a;
130
struct extract *extract = get_extract(a);
131
if (extract != NULL) {
132
extract->extract_progress = progress_func;
133
extract->extract_progress_user_data = user_data;
138
copy_data(struct archive *ar, struct archive *aw)
142
struct extract *extract;
146
extract = get_extract((struct archive_read *)ar);
148
r = archive_read_data_block(ar, &buff, &size, &offset);
149
if (r == ARCHIVE_EOF)
153
r = archive_write_data_block(aw, buff, size, offset);
154
if (r < ARCHIVE_WARN)
156
if (r != ARCHIVE_OK) {
157
archive_set_error(ar, archive_errno(aw),
158
"%s", archive_error_string(aw));
161
if (extract->extract_progress)
162
(extract->extract_progress)
163
(extract->extract_progress_user_data);
168
* Cleanup function for archive_extract.
171
archive_read_extract_cleanup(struct archive_read *a)
173
int ret = ARCHIVE_OK;
175
#if ARCHIVE_API_VERSION > 1
178
archive_write_finish(a->extract->ad);