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 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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, 0, "%s", archive_error_string(a));
100 if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
102 if (r == ARCHIVE_RETRY) {
103 bsdar_warnc(bsdar, 0, "Retrying...");
107 if ((name = archive_entry_pathname(entry)) == NULL)
110 /* Skip pseudo members. */
111 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0)
114 if (bsdar->argc > 0) {
116 for(i = 0; i < bsdar->argc; i++) {
117 av = &bsdar->argv[i];
120 if ((bname = basename(*av)) == NULL)
121 bsdar_errc(bsdar, EX_SOFTWARE, errno,
123 if (strcmp(bname, name) != 0)
135 if (bsdar->options & AR_V) {
136 md = archive_entry_mode(entry);
137 uid = archive_entry_uid(entry);
138 gid = archive_entry_gid(entry);
139 size = archive_entry_size(entry);
140 mtime = archive_entry_mtime(entry);
141 (void)strmode(md, buf);
142 (void)fprintf(stdout, "%s %6d/%-6d %8ju ",
143 buf + 1, uid, gid, (uintmax_t)size);
144 tp = localtime(&mtime);
145 (void)strftime(buf, sizeof(buf),
146 "%b %e %H:%M %Y", tp);
147 (void)fprintf(stdout, "%s %s", buf, name);
149 (void)fprintf(stdout, "%s", name);
150 r = archive_read_data_skip(a);
151 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
152 r == ARCHIVE_FATAL) {
153 (void)fprintf(stdout, "\n");
154 bsdar_warnc(bsdar, 0, "%s",
155 archive_error_string(a));
158 if (r == ARCHIVE_FATAL)
161 (void)fprintf(stdout, "\n");
163 /* mode == 'x' || mode = 'p' */
165 if (bsdar->options & AR_V) {
166 (void)fprintf(stdout, "\n<%s>\n\n",
170 r = archive_read_data_into_fd(a, 1);
173 if (stat(name, &sb) != 0) {
174 if (errno != ENOENT) {
175 bsdar_warnc(bsdar, 0,
181 /* stat success, file exist */
182 if (bsdar->options & AR_CC)
184 if (bsdar->options & AR_U &&
185 archive_entry_mtime(entry) <=
190 if (bsdar->options & AR_V)
191 (void)fprintf(stdout, "x - %s\n", name);
192 /* Disallow absolute paths. */
193 if (name[0] == '/') {
194 bsdar_warnc(bsdar, 0,
195 "Absolute path '%s'", name);
198 /* Basic path security flags. */
199 flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
200 ARCHIVE_EXTRACT_SECURE_NODOTDOT;
201 if (bsdar->options & AR_O)
202 flags |= ARCHIVE_EXTRACT_TIME;
204 r = archive_read_extract(a, entry, flags);
208 bsdar_warnc(bsdar, 0, "%s",
209 archive_error_string(a));
212 AC(archive_read_close(a));
213 AC(archive_read_free(a));