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 #include <sys/queue.h>
34 #include <archive_entry.h>
45 * Handle read modes: 'x', 't' and 'p'.
48 ar_read_archive(struct bsdar *bsdar, int mode, FILE *out)
51 struct archive_entry *entry;
64 int exitcode, flags, r, i;
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));
73 exitcode = EXIT_SUCCESS;
76 r = archive_read_next_header(a, &entry);
77 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
79 bsdar_warnc(bsdar, archive_errno(a), "%s",
80 archive_error_string(a));
81 if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
83 if (r == ARCHIVE_RETRY) {
84 bsdar_warnc(bsdar, 0, "Retrying...");
88 if ((name = archive_entry_pathname(entry)) == NULL)
91 /* Skip pseudo members. */
92 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0 ||
93 strcmp(name, "/SYM64/") == 0)
96 if (bsdar->argc > 0) {
98 for(i = 0; i < bsdar->argc; i++) {
102 if ((bname = basename(*av)) == NULL)
103 bsdar_errc(bsdar, errno,
105 if (strcmp(bname, name) != 0)
117 if (bsdar->options & AR_V) {
118 md = archive_entry_mode(entry);
119 uid = archive_entry_uid(entry);
120 gid = archive_entry_gid(entry);
121 size = archive_entry_size(entry);
122 mtime = archive_entry_mtime(entry);
123 (void)strmode(md, buf);
124 (void)fprintf(out, "%s %6d/%-6d %8ju ",
125 buf + 1, uid, gid, (uintmax_t)size);
126 tp = localtime(&mtime);
127 (void)strftime(buf, sizeof(buf),
128 "%b %e %H:%M %Y", tp);
129 (void)fprintf(out, "%s %s", buf, name);
131 (void)fprintf(out, "%s", name);
132 r = archive_read_data_skip(a);
133 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
134 r == ARCHIVE_FATAL) {
135 (void)fprintf(out, "\n");
136 bsdar_warnc(bsdar, archive_errno(a), "%s",
137 archive_error_string(a));
140 if (r == ARCHIVE_FATAL)
143 (void)fprintf(out, "\n");
145 /* mode == 'x' || mode = 'p' */
147 if (bsdar->options & AR_V) {
148 (void)fprintf(out, "\n<%s>\n\n",
152 r = archive_read_data_into_fd(a, 1);
155 if (stat(name, &sb) != 0) {
156 if (errno != ENOENT) {
157 bsdar_warnc(bsdar, 0,
163 /* stat success, file exist */
164 if (bsdar->options & AR_CC)
166 if (bsdar->options & AR_U &&
167 archive_entry_mtime(entry) <=
172 if (bsdar->options & AR_V)
173 (void)fprintf(out, "x - %s\n", name);
174 /* Disallow absolute paths. */
175 if (name[0] == '/') {
176 bsdar_warnc(bsdar, 0,
177 "Absolute path '%s'", name);
180 /* Basic path security flags. */
181 flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
182 ARCHIVE_EXTRACT_SECURE_NODOTDOT;
183 if (bsdar->options & AR_O)
184 flags |= ARCHIVE_EXTRACT_TIME;
186 r = archive_read_extract(a, entry, flags);
190 bsdar_warnc(bsdar, archive_errno(a), "%s",
191 archive_error_string(a));
192 exitcode = EXIT_FAILURE;
197 if (r == ARCHIVE_FATAL)
198 exitcode = EXIT_FAILURE;
200 AC(archive_read_close(a));
201 AC(archive_read_free(a));