1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25 
26 #include "archive_platform.h"
27 
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 
38 #include "archive.h"
39 #include "archive_entry.h"
40 #include "archive_private.h"
41 #include "archive_read_private.h"
42 
43 static int          copy_data(struct archive *ar, struct archive *aw);
44 static int          archive_read_extract_cleanup(struct archive_read *);
45 
46 
47 /* Retrieve an extract object without initialising the associated
48  * archive_write_disk object.
49  */
50 struct archive_read_extract *
__archive_read_get_extract(struct archive_read * a)51 __archive_read_get_extract(struct archive_read *a)
52 {
53           if (a->extract == NULL) {
54                     a->extract = calloc(1, sizeof(*a->extract));
55                     if (a->extract == NULL) {
56                               archive_set_error(&a->archive, ENOMEM, "Can't extract");
57                               return (NULL);
58                     }
59                     a->cleanup_archive_extract = archive_read_extract_cleanup;
60           }
61           return (a->extract);
62 }
63 
64 /*
65  * Cleanup function for archive_extract.
66  */
67 static int
archive_read_extract_cleanup(struct archive_read * a)68 archive_read_extract_cleanup(struct archive_read *a)
69 {
70           int ret = ARCHIVE_OK;
71 
72           if (a->extract->ad != NULL) {
73                     ret = archive_write_free(a->extract->ad);
74           }
75           free(a->extract);
76           a->extract = NULL;
77           return (ret);
78 }
79 
80 int
archive_read_extract2(struct archive * _a,struct archive_entry * entry,struct archive * ad)81 archive_read_extract2(struct archive *_a, struct archive_entry *entry,
82     struct archive *ad)
83 {
84           struct archive_read *a = (struct archive_read *)_a;
85           int r, r2;
86 
87           /* Set up for this particular entry. */
88           if (a->skip_file_set)
89                     archive_write_disk_set_skip_file(ad,
90                         a->skip_file_dev, a->skip_file_ino);
91           r = archive_write_header(ad, entry);
92           if (r < ARCHIVE_WARN)
93                     r = ARCHIVE_WARN;
94           if (r != ARCHIVE_OK)
95                     /* If _write_header failed, copy the error. */
96                     archive_copy_error(&a->archive, ad);
97           else if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) > 0)
98                     /* Otherwise, pour data into the entry. */
99                     r = copy_data(_a, ad);
100           r2 = archive_write_finish_entry(ad);
101           if (r2 < ARCHIVE_WARN)
102                     r2 = ARCHIVE_WARN;
103           /* Use the first message. */
104           if (r2 != ARCHIVE_OK && r == ARCHIVE_OK)
105                     archive_copy_error(&a->archive, ad);
106           /* Use the worst error return. */
107           if (r2 < r)
108                     r = r2;
109           return (r);
110 }
111 
112 void
archive_read_extract_set_progress_callback(struct archive * _a,void (* progress_func)(void *),void * user_data)113 archive_read_extract_set_progress_callback(struct archive *_a,
114     void (*progress_func)(void *), void *user_data)
115 {
116           struct archive_read *a = (struct archive_read *)_a;
117           struct archive_read_extract *extract = __archive_read_get_extract(a);
118           if (extract != NULL) {
119                     extract->extract_progress = progress_func;
120                     extract->extract_progress_user_data = user_data;
121           }
122 }
123 
124 static int
copy_data(struct archive * ar,struct archive * aw)125 copy_data(struct archive *ar, struct archive *aw)
126 {
127           int64_t offset;
128           const void *buff;
129           struct archive_read_extract *extract;
130           size_t size;
131           int r;
132 
133           extract = __archive_read_get_extract((struct archive_read *)ar);
134           if (extract == NULL)
135                     return (ARCHIVE_FATAL);
136           for (;;) {
137                     r = archive_read_data_block(ar, &buff, &size, &offset);
138                     if (r == ARCHIVE_EOF)
139                               return (ARCHIVE_OK);
140                     if (r != ARCHIVE_OK)
141                               return (r);
142                     r = (int)archive_write_data_block(aw, buff, size, offset);
143                     if (r < ARCHIVE_WARN)
144                               r = ARCHIVE_WARN;
145                     if (r < ARCHIVE_OK) {
146                               archive_set_error(ar, archive_errno(aw),
147                                   "%s", archive_error_string(aw));
148                               return (r);
149                     }
150                     if (extract->extract_progress)
151                               (extract->extract_progress)
152                                   (extract->extract_progress_user_data);
153           }
154 }
155