13fe401a5SEd Maste /*-
23fe401a5SEd Maste * Copyright (c) 2007 Kai Wang
33fe401a5SEd Maste * Copyright (c) 2007 Tim Kientzle
43fe401a5SEd Maste * All rights reserved.
53fe401a5SEd Maste *
63fe401a5SEd Maste * Redistribution and use in source and binary forms, with or without
73fe401a5SEd Maste * modification, are permitted provided that the following conditions
83fe401a5SEd Maste * are met:
93fe401a5SEd Maste * 1. Redistributions of source code must retain the above copyright
103fe401a5SEd Maste * notice, this list of conditions and the following disclaimer
113fe401a5SEd Maste * in this position and unchanged.
123fe401a5SEd Maste * 2. Redistributions in binary form must reproduce the above copyright
133fe401a5SEd Maste * notice, this list of conditions and the following disclaimer in the
143fe401a5SEd Maste * documentation and/or other materials provided with the distribution.
153fe401a5SEd Maste *
163fe401a5SEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
173fe401a5SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183fe401a5SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193fe401a5SEd Maste * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
203fe401a5SEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
213fe401a5SEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223fe401a5SEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
233fe401a5SEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
243fe401a5SEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
253fe401a5SEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263fe401a5SEd Maste */
273fe401a5SEd Maste
283fe401a5SEd Maste #include <sys/queue.h>
293fe401a5SEd Maste #include <sys/stat.h>
303fe401a5SEd Maste
313fe401a5SEd Maste #include <archive.h>
323fe401a5SEd Maste #include <archive_entry.h>
333fe401a5SEd Maste #include <assert.h>
343fe401a5SEd Maste #include <errno.h>
353fe401a5SEd Maste #include <libgen.h>
363fe401a5SEd Maste #include <stdio.h>
37*ae500c1fSEd Maste #include <stdlib.h>
383fe401a5SEd Maste #include <string.h>
393fe401a5SEd Maste
403fe401a5SEd Maste #include "ar.h"
413fe401a5SEd Maste
42*ae500c1fSEd Maste ELFTC_VCSID("$Id: read.c 3629 2018-09-30 19:26:28Z jkoshy $");
433fe401a5SEd Maste
443fe401a5SEd Maste /*
453fe401a5SEd Maste * Handle read modes: 'x', 't' and 'p'.
46*ae500c1fSEd Maste *
47*ae500c1fSEd Maste * Returns EXIT_SUCCESS if all operations completed successfully or returns
48*ae500c1fSEd Maste * EXIT_FAILURE otherwise.
493fe401a5SEd Maste */
50*ae500c1fSEd Maste int
ar_read_archive(struct bsdar * bsdar,int mode)513fe401a5SEd Maste ar_read_archive(struct bsdar *bsdar, int mode)
523fe401a5SEd Maste {
533fe401a5SEd Maste FILE *out;
543fe401a5SEd Maste struct archive *a;
553fe401a5SEd Maste struct archive_entry *entry;
563fe401a5SEd Maste struct stat sb;
573fe401a5SEd Maste struct tm *tp;
583fe401a5SEd Maste const char *bname;
593fe401a5SEd Maste const char *name;
603fe401a5SEd Maste mode_t md;
613fe401a5SEd Maste size_t size;
623fe401a5SEd Maste time_t mtime;
633fe401a5SEd Maste uid_t uid;
643fe401a5SEd Maste gid_t gid;
653fe401a5SEd Maste char **av;
663fe401a5SEd Maste char buf[25];
67*ae500c1fSEd Maste int found;
68*ae500c1fSEd Maste int exitcode, i, flags, r;
693fe401a5SEd Maste
703fe401a5SEd Maste assert(mode == 'p' || mode == 't' || mode == 'x');
713fe401a5SEd Maste
723fe401a5SEd Maste if ((a = archive_read_new()) == NULL)
733fe401a5SEd Maste bsdar_errc(bsdar, 0, "archive_read_new failed");
743fe401a5SEd Maste archive_read_support_format_ar(a);
753fe401a5SEd Maste AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ));
763fe401a5SEd Maste
77*ae500c1fSEd Maste exitcode = EXIT_SUCCESS;
783fe401a5SEd Maste out = bsdar->output;
793fe401a5SEd Maste
803fe401a5SEd Maste for (;;) {
813fe401a5SEd Maste r = archive_read_next_header(a, &entry);
823fe401a5SEd Maste if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
833fe401a5SEd Maste r == ARCHIVE_FATAL)
843fe401a5SEd Maste bsdar_warnc(bsdar, 0, "%s", archive_error_string(a));
853fe401a5SEd Maste if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL)
863fe401a5SEd Maste break;
873fe401a5SEd Maste if (r == ARCHIVE_RETRY) {
883fe401a5SEd Maste bsdar_warnc(bsdar, 0, "Retrying...");
893fe401a5SEd Maste continue;
903fe401a5SEd Maste }
913fe401a5SEd Maste
923fe401a5SEd Maste if (archive_format(a) == ARCHIVE_FORMAT_AR_BSD)
933fe401a5SEd Maste bsdar->options |= AR_BSD;
943fe401a5SEd Maste else
953fe401a5SEd Maste bsdar->options &= ~AR_BSD;
963fe401a5SEd Maste
973fe401a5SEd Maste if ((name = archive_entry_pathname(entry)) == NULL)
983fe401a5SEd Maste break;
993fe401a5SEd Maste
1003fe401a5SEd Maste /* Skip pseudo members. */
1013fe401a5SEd Maste if (bsdar_is_pseudomember(bsdar, name))
1023fe401a5SEd Maste continue;
1033fe401a5SEd Maste
104*ae500c1fSEd Maste /* The ar(5) format only supports 'leaf' file names. */
105*ae500c1fSEd Maste if (strchr(name, '/')) {
106*ae500c1fSEd Maste bsdar_warnc(bsdar, 0, "ignoring entry: %s",
107*ae500c1fSEd Maste name);
108*ae500c1fSEd Maste continue;
109*ae500c1fSEd Maste }
110*ae500c1fSEd Maste
111*ae500c1fSEd Maste /*
112*ae500c1fSEd Maste * If we had been given a list of file names to process, check
113*ae500c1fSEd Maste * that the current entry is present in this list.
114*ae500c1fSEd Maste */
1153fe401a5SEd Maste if (bsdar->argc > 0) {
116*ae500c1fSEd Maste found = 0;
1173fe401a5SEd Maste for(i = 0; i < bsdar->argc; i++) {
1183fe401a5SEd Maste av = &bsdar->argv[i];
1193fe401a5SEd Maste if (*av == NULL)
1203fe401a5SEd Maste continue;
121*ae500c1fSEd Maste /*
122*ae500c1fSEd Maste * Per POSIX, only the basename of a file
123*ae500c1fSEd Maste * argument should be compared.
124*ae500c1fSEd Maste */
1253fe401a5SEd Maste if ((bname = basename(*av)) == NULL)
1263fe401a5SEd Maste bsdar_errc(bsdar, errno,
1273fe401a5SEd Maste "basename failed");
1283fe401a5SEd Maste if (strcmp(bname, name) != 0)
1293fe401a5SEd Maste continue;
1303fe401a5SEd Maste
1313fe401a5SEd Maste *av = NULL;
132*ae500c1fSEd Maste found = 1;
1333fe401a5SEd Maste break;
1343fe401a5SEd Maste }
135*ae500c1fSEd Maste if (!found)
1363fe401a5SEd Maste continue;
1373fe401a5SEd Maste }
1383fe401a5SEd Maste
1393fe401a5SEd Maste if (mode == 't') {
1403fe401a5SEd Maste if (bsdar->options & AR_V) {
1413fe401a5SEd Maste md = archive_entry_mode(entry);
1423fe401a5SEd Maste uid = archive_entry_uid(entry);
1433fe401a5SEd Maste gid = archive_entry_gid(entry);
1443fe401a5SEd Maste size = archive_entry_size(entry);
1453fe401a5SEd Maste mtime = archive_entry_mtime(entry);
1463fe401a5SEd Maste (void)fprintf(out, "%s %6d/%-6d %8ju ",
1473fe401a5SEd Maste bsdar_strmode(md) + 1, uid, gid,
1483fe401a5SEd Maste (uintmax_t)size);
1493fe401a5SEd Maste tp = localtime(&mtime);
1503fe401a5SEd Maste (void)strftime(buf, sizeof(buf),
1513fe401a5SEd Maste "%b %e %H:%M %Y", tp);
1523fe401a5SEd Maste (void)fprintf(out, "%s %s", buf, name);
1533fe401a5SEd Maste } else
1543fe401a5SEd Maste (void)fprintf(out, "%s", name);
1553fe401a5SEd Maste r = archive_read_data_skip(a);
1563fe401a5SEd Maste if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY ||
1573fe401a5SEd Maste r == ARCHIVE_FATAL) {
1583fe401a5SEd Maste (void)fprintf(out, "\n");
1593fe401a5SEd Maste bsdar_warnc(bsdar, 0, "%s",
1603fe401a5SEd Maste archive_error_string(a));
1613fe401a5SEd Maste }
1623fe401a5SEd Maste
1633fe401a5SEd Maste if (r == ARCHIVE_FATAL)
1643fe401a5SEd Maste break;
1653fe401a5SEd Maste
1663fe401a5SEd Maste (void)fprintf(out, "\n");
1673fe401a5SEd Maste } else {
1683fe401a5SEd Maste /* mode == 'x' || mode = 'p' */
1693fe401a5SEd Maste if (mode == 'p') {
1703fe401a5SEd Maste if (bsdar->options & AR_V) {
1713fe401a5SEd Maste (void)fprintf(out, "\n<%s>\n\n",
1723fe401a5SEd Maste name);
1733fe401a5SEd Maste fflush(out);
1743fe401a5SEd Maste }
1753fe401a5SEd Maste r = archive_read_data_into_fd(a, fileno(out));
1763fe401a5SEd Maste } else {
1773fe401a5SEd Maste /* mode == 'x' */
1783fe401a5SEd Maste if (stat(name, &sb) != 0) {
1793fe401a5SEd Maste if (errno != ENOENT) {
180*ae500c1fSEd Maste bsdar_warnc(bsdar, errno,
1813fe401a5SEd Maste "stat %s failed",
1823fe401a5SEd Maste bsdar->filename);
1833fe401a5SEd Maste continue;
1843fe401a5SEd Maste }
1853fe401a5SEd Maste } else {
1863fe401a5SEd Maste /* stat success, file exist */
1873fe401a5SEd Maste if (bsdar->options & AR_CC)
1883fe401a5SEd Maste continue;
1893fe401a5SEd Maste if (bsdar->options & AR_U &&
1903fe401a5SEd Maste archive_entry_mtime(entry) <=
1913fe401a5SEd Maste sb.st_mtime)
1923fe401a5SEd Maste continue;
1933fe401a5SEd Maste }
1943fe401a5SEd Maste
1953fe401a5SEd Maste if (bsdar->options & AR_V)
1963fe401a5SEd Maste (void)fprintf(out, "x - %s\n", name);
1973fe401a5SEd Maste /* Basic path security flags. */
1983fe401a5SEd Maste flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS |
1993fe401a5SEd Maste ARCHIVE_EXTRACT_SECURE_NODOTDOT;
2003fe401a5SEd Maste if (bsdar->options & AR_O)
2013fe401a5SEd Maste flags |= ARCHIVE_EXTRACT_TIME;
2023fe401a5SEd Maste
2033fe401a5SEd Maste r = archive_read_extract(a, entry, flags);
2043fe401a5SEd Maste }
2053fe401a5SEd Maste
206*ae500c1fSEd Maste if (r) {
2073fe401a5SEd Maste bsdar_warnc(bsdar, 0, "%s",
2083fe401a5SEd Maste archive_error_string(a));
209*ae500c1fSEd Maste exitcode = EXIT_FAILURE;
2103fe401a5SEd Maste }
2113fe401a5SEd Maste }
212*ae500c1fSEd Maste }
213*ae500c1fSEd Maste
214*ae500c1fSEd Maste if (r == ARCHIVE_FATAL)
215*ae500c1fSEd Maste exitcode = EXIT_FAILURE;
216*ae500c1fSEd Maste
2173fe401a5SEd Maste AC(archive_read_close(a));
2183fe401a5SEd Maste ACV(archive_read_free(a));
219*ae500c1fSEd Maste
220*ae500c1fSEd Maste return (exitcode);
2213fe401a5SEd Maste }
222