2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2007 Kai Wang
5 * Copyright (c) 2007 Tim Kientzle
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/queue.h>
36 #include <archive_entry.h>
47 * Handle read modes: 'x', 't' and 'p'.
50 ar_read_archive(struct bsdar *bsdar, int mode, FILE *out)
53 struct archive_entry *entry;
66 int exitcode, flags, r, i;
68 assert(mode == 'p' || mode == 't' || mode == 'x');
70 if ((a = archive_read_new()) == NULL)
71 bsdar_errc(bsdar, 0, "archive_read_new failed");
72 archive_read_support_format_ar(a);
73 AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ));
75 exitcode = EXIT_SUCCESS;
78 r = archive_read_next_header(a, &entry);
79 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
81 bsdar_warnc(bsdar, archive_errno(a), "%s",
82 archive_error_string(a));
83 if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
85 if (r == ARCHIVE_RETRY) {
86 bsdar_warnc(bsdar, 0, "Retrying...");
90 if ((name = archive_entry_pathname(entry)) == NULL)
93 /* Skip pseudo members. */
94 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0 ||
95 strcmp(name, "/SYM64/") == 0)
98 if (bsdar->argc > 0) {
100 for(i = 0; i < bsdar->argc; i++) {
101 av = &bsdar->argv[i];
104 if ((bname = basename(*av)) == NULL)
105 bsdar_errc(bsdar, errno,
107 if (strcmp(bname, name) != 0)
119 if (bsdar->options & AR_V) {
120 md = archive_entry_mode(entry);
121 uid = archive_entry_uid(entry);
122 gid = archive_entry_gid(entry);
123 size = archive_entry_size(entry);
124 mtime = archive_entry_mtime(entry);
125 (void)strmode(md, buf);
126 (void)fprintf(out, "%s %6d/%-6d %8ju ",
127 buf + 1, uid, gid, (uintmax_t)size);
128 tp = localtime(&mtime);
129 (void)strftime(buf, sizeof(buf),
130 "%b %e %H:%M %Y", tp);
131 (void)fprintf(out, "%s %s", buf, name);
133 (void)fprintf(out, "%s", name);
134 r = archive_read_data_skip(a);
135 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
136 r == ARCHIVE_FATAL) {
137 (void)fprintf(out, "\n");
138 bsdar_warnc(bsdar, archive_errno(a), "%s",
139 archive_error_string(a));
142 if (r == ARCHIVE_FATAL)
145 (void)fprintf(out, "\n");
147 /* mode == 'x' || mode = 'p' */
149 if (bsdar->options & AR_V) {
150 (void)fprintf(out, "\n<%s>\n\n",
154 r = archive_read_data_into_fd(a, 1);
157 if (stat(name, &sb) != 0) {
158 if (errno != ENOENT) {
159 bsdar_warnc(bsdar, 0,
165 /* stat success, file exist */
166 if (bsdar->options & AR_CC)
168 if (bsdar->options & AR_U &&
169 archive_entry_mtime(entry) <=
174 if (bsdar->options & AR_V)
175 (void)fprintf(out, "x - %s\n", name);
176 /* Disallow absolute paths. */
177 if (name[0] == '/') {
178 bsdar_warnc(bsdar, 0,
179 "Absolute path '%s'", name);
182 /* Basic path security flags. */
183 flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
184 ARCHIVE_EXTRACT_SECURE_NODOTDOT;
185 if (bsdar->options & AR_O)
186 flags |= ARCHIVE_EXTRACT_TIME;
188 r = archive_read_extract(a, entry, flags);
192 bsdar_warnc(bsdar, archive_errno(a), "%s",
193 archive_error_string(a));
194 exitcode = EXIT_FAILURE;
199 if (r == ARCHIVE_FATAL)
200 exitcode = EXIT_FAILURE;
202 AC(archive_read_close(a));
203 AC(archive_read_free(a));