2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
45 static void read_archive(struct bsdar *bsdar, char mode);
48 ar_mode_p(struct bsdar *bsdar)
51 read_archive(bsdar, 'p');
55 ar_mode_t(struct bsdar *bsdar)
58 read_archive(bsdar, 't');
62 ar_mode_x(struct bsdar *bsdar)
65 read_archive(bsdar, 'x');
69 * Handle read modes: 'x', 't' and 'p'.
72 read_archive(struct bsdar *bsdar, char mode)
75 struct archive_entry *entry;
90 if ((a = archive_read_new()) == NULL)
91 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed");
92 archive_read_support_format_ar(a);
93 AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ));
96 r = archive_read_next_header(a, &entry);
97 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
99 bsdar_warnc(bsdar, archive_errno(a), "%s",
100 archive_error_string(a));
101 if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
103 if (r == ARCHIVE_RETRY) {
104 bsdar_warnc(bsdar, 0, "Retrying...");
108 if ((name = archive_entry_pathname(entry)) == NULL)
111 /* Skip pseudo members. */
112 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0 ||
113 strcmp(name, "/SYM64/") == 0)
116 if (bsdar->argc > 0) {
118 for(i = 0; i < bsdar->argc; i++) {
119 av = &bsdar->argv[i];
122 if ((bname = basename(*av)) == NULL)
123 bsdar_errc(bsdar, EX_SOFTWARE, errno,
125 if (strcmp(bname, name) != 0)
137 if (bsdar->options & AR_V) {
138 md = archive_entry_mode(entry);
139 uid = archive_entry_uid(entry);
140 gid = archive_entry_gid(entry);
141 size = archive_entry_size(entry);
142 mtime = archive_entry_mtime(entry);
143 (void)strmode(md, buf);
144 (void)fprintf(stdout, "%s %6d/%-6d %8ju ",
145 buf + 1, uid, gid, (uintmax_t)size);
146 tp = localtime(&mtime);
147 (void)strftime(buf, sizeof(buf),
148 "%b %e %H:%M %Y", tp);
149 (void)fprintf(stdout, "%s %s", buf, name);
151 (void)fprintf(stdout, "%s", name);
152 r = archive_read_data_skip(a);
153 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
154 r == ARCHIVE_FATAL) {
155 (void)fprintf(stdout, "\n");
156 bsdar_warnc(bsdar, archive_errno(a), "%s",
157 archive_error_string(a));
160 if (r == ARCHIVE_FATAL)
163 (void)fprintf(stdout, "\n");
165 /* mode == 'x' || mode = 'p' */
167 if (bsdar->options & AR_V) {
168 (void)fprintf(stdout, "\n<%s>\n\n",
172 r = archive_read_data_into_fd(a, 1);
175 if (stat(name, &sb) != 0) {
176 if (errno != ENOENT) {
177 bsdar_warnc(bsdar, 0,
183 /* stat success, file exist */
184 if (bsdar->options & AR_CC)
186 if (bsdar->options & AR_U &&
187 archive_entry_mtime(entry) <=
192 if (bsdar->options & AR_V)
193 (void)fprintf(stdout, "x - %s\n", name);
194 /* Disallow absolute paths. */
195 if (name[0] == '/') {
196 bsdar_warnc(bsdar, 0,
197 "Absolute path '%s'", name);
200 /* Basic path security flags. */
201 flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
202 ARCHIVE_EXTRACT_SECURE_NODOTDOT;
203 if (bsdar->options & AR_O)
204 flags |= ARCHIVE_EXTRACT_TIME;
206 r = archive_read_extract(a, entry, flags);
210 bsdar_warnc(bsdar, archive_errno(a), "%s",
211 archive_error_string(a));
214 AC(archive_read_close(a));
215 AC(archive_read_free(a));