2 * Copyright (c) 2007 Kai Wang
3 * Copyright (c) 2007 Tim Kientzle
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/queue.h>
32 #include <archive_entry.h>
41 ELFTC_VCSID("$Id: read.c 3180 2015-04-09 15:13:57Z emaste $");
44 * Handle read modes: 'x', 't' and 'p'.
47 ar_read_archive(struct bsdar *bsdar, int mode)
51 struct archive_entry *entry;
66 assert(mode == 'p' || mode == 't' || mode == 'x');
68 if ((a = archive_read_new()) == NULL)
69 bsdar_errc(bsdar, 0, "archive_read_new failed");
70 archive_read_support_format_ar(a);
71 AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ));
76 r = archive_read_next_header(a, &entry);
77 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
79 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a));
80 if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
82 if (r == ARCHIVE_RETRY) {
83 bsdar_warnc(bsdar, 0, "Retrying...");
87 if (archive_format(a) == ARCHIVE_FORMAT_AR_BSD)
88 bsdar->options |= AR_BSD;
90 bsdar->options &= ~AR_BSD;
92 if ((name = archive_entry_pathname(entry)) == NULL)
95 /* Skip pseudo members. */
96 if (bsdar_is_pseudomember(bsdar, name))
99 if (bsdar->argc > 0) {
101 for(i = 0; i < bsdar->argc; i++) {
102 av = &bsdar->argv[i];
105 if ((bname = basename(*av)) == NULL)
106 bsdar_errc(bsdar, errno,
108 if (strcmp(bname, name) != 0)
120 if (bsdar->options & AR_V) {
121 md = archive_entry_mode(entry);
122 uid = archive_entry_uid(entry);
123 gid = archive_entry_gid(entry);
124 size = archive_entry_size(entry);
125 mtime = archive_entry_mtime(entry);
126 (void)fprintf(out, "%s %6d/%-6d %8ju ",
127 bsdar_strmode(md) + 1, uid, gid,
129 tp = localtime(&mtime);
130 (void)strftime(buf, sizeof(buf),
131 "%b %e %H:%M %Y", tp);
132 (void)fprintf(out, "%s %s", buf, name);
134 (void)fprintf(out, "%s", name);
135 r = archive_read_data_skip(a);
136 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
137 r == ARCHIVE_FATAL) {
138 (void)fprintf(out, "\n");
139 bsdar_warnc(bsdar, 0, "%s",
140 archive_error_string(a));
143 if (r == ARCHIVE_FATAL)
146 (void)fprintf(out, "\n");
148 /* mode == 'x' || mode = 'p' */
150 if (bsdar->options & AR_V) {
151 (void)fprintf(out, "\n<%s>\n\n",
155 r = archive_read_data_into_fd(a, fileno(out));
158 if (stat(name, &sb) != 0) {
159 if (errno != ENOENT) {
160 bsdar_warnc(bsdar, 0,
166 /* stat success, file exist */
167 if (bsdar->options & AR_CC)
169 if (bsdar->options & AR_U &&
170 archive_entry_mtime(entry) <=
175 if (bsdar->options & AR_V)
176 (void)fprintf(out, "x - %s\n", name);
177 /* Disallow absolute paths. */
178 if (name[0] == '/') {
179 bsdar_warnc(bsdar, 0,
180 "Absolute path '%s'", name);
183 /* Basic path security flags. */
184 flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
185 ARCHIVE_EXTRACT_SECURE_NODOTDOT;
186 if (bsdar->options & AR_O)
187 flags |= ARCHIVE_EXTRACT_TIME;
189 r = archive_read_extract(a, entry, flags);
193 bsdar_warnc(bsdar, 0, "%s",
194 archive_error_string(a));
197 AC(archive_read_close(a));
198 ACV(archive_read_free(a));