1 /*- 2 * Copyright (c) 2007 Kai Wang 3 * Copyright (c) 2007 Tim Kientzle 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/queue.h> 32 #include <sys/stat.h> 33 #include <archive.h> 34 #include <archive_entry.h> 35 #include <errno.h> 36 #include <libgen.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <sysexits.h> 40 41 #include "ar.h" 42 43 static void read_archive(struct bsdar *bsdar, char mode); 44 45 void 46 ar_mode_p(struct bsdar *bsdar) 47 { 48 49 read_archive(bsdar, 'p'); 50 } 51 52 void 53 ar_mode_t(struct bsdar *bsdar) 54 { 55 56 read_archive(bsdar, 't'); 57 } 58 59 void 60 ar_mode_x(struct bsdar *bsdar) 61 { 62 63 read_archive(bsdar, 'x'); 64 } 65 66 /* 67 * Handle read modes: 'x', 't' and 'p'. 68 */ 69 static void 70 read_archive(struct bsdar *bsdar, char mode) 71 { 72 struct archive *a; 73 struct archive_entry *entry; 74 struct stat sb; 75 struct tm *tp; 76 const char *bname; 77 const char *name; 78 mode_t md; 79 size_t size; 80 time_t mtime; 81 uid_t uid; 82 gid_t gid; 83 char **av; 84 char buf[25]; 85 char find; 86 int flags, r, i; 87 88 if ((a = archive_read_new()) == NULL) 89 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed"); 90 archive_read_support_format_ar(a); 91 AC(archive_read_open_filename(a, bsdar->filename, DEF_BLKSZ)); 92 93 for (;;) { 94 r = archive_read_next_header(a, &entry); 95 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY || 96 r == ARCHIVE_FATAL) 97 bsdar_warnc(bsdar, 0, "%s", archive_error_string(a)); 98 if (r == ARCHIVE_EOF || r == ARCHIVE_FATAL) 99 break; 100 if (r == ARCHIVE_RETRY) { 101 bsdar_warnc(bsdar, 0, "Retrying..."); 102 continue; 103 } 104 105 name = archive_entry_pathname(entry); 106 107 /* Skip pseudo members. */ 108 if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0) 109 continue; 110 111 if (bsdar->argc > 0) { 112 find = 0; 113 for(i = 0; i < bsdar->argc; i++) { 114 av = &bsdar->argv[i]; 115 if (*av == NULL) 116 continue; 117 if ((bname = basename(*av)) == NULL) 118 bsdar_errc(bsdar, EX_SOFTWARE, errno, 119 "basename failed"); 120 if (strcmp(bname, name) != 0) 121 continue; 122 123 *av = NULL; 124 find = 1; 125 break; 126 } 127 if (!find) 128 continue; 129 } 130 131 if (mode == 't') { 132 if (bsdar->options & AR_V) { 133 md = archive_entry_mode(entry); 134 uid = archive_entry_uid(entry); 135 gid = archive_entry_gid(entry); 136 size = archive_entry_size(entry); 137 mtime = archive_entry_mtime(entry); 138 (void)strmode(md, buf); 139 (void)fprintf(stdout, "%s %6d/%-6d %8ju ", 140 buf + 1, uid, gid, (uintmax_t)size); 141 tp = localtime(&mtime); 142 (void)strftime(buf, sizeof(buf), 143 "%b %e %H:%M %Y", tp); 144 (void)fprintf(stdout, "%s %s", buf, name); 145 } else 146 (void)fprintf(stdout, "%s", name); 147 r = archive_read_data_skip(a); 148 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY || 149 r == ARCHIVE_FATAL) { 150 (void)fprintf(stdout, "\n"); 151 bsdar_warnc(bsdar, 0, "%s", 152 archive_error_string(a)); 153 } 154 155 if (r == ARCHIVE_FATAL) 156 break; 157 158 (void)fprintf(stdout, "\n"); 159 } else { 160 /* mode == 'x' || mode = 'p' */ 161 if (mode == 'p') { 162 if (bsdar->options & AR_V) { 163 (void)fprintf(stdout, "\n<%s>\n\n", 164 name); 165 fflush(stdout); 166 } 167 r = archive_read_data_into_fd(a, 1); 168 } else { 169 /* mode == 'x' */ 170 if (stat(name, &sb) != 0) { 171 if (errno != ENOENT) { 172 bsdar_warnc(bsdar, 0, 173 "stat %s failed", 174 bsdar->filename); 175 continue; 176 } 177 } else { 178 /* stat success, file exist */ 179 if (bsdar->options & AR_CC) 180 continue; 181 if (bsdar->options & AR_U && 182 archive_entry_mtime(entry) <= 183 sb.st_mtime) 184 continue; 185 } 186 187 if (bsdar->options & AR_V) 188 (void)fprintf(stdout, "x - %s\n", name); 189 flags = 0; 190 if (bsdar->options & AR_O) 191 flags |= ARCHIVE_EXTRACT_TIME; 192 193 r = archive_read_extract(a, entry, flags); 194 } 195 196 if (r) 197 bsdar_warnc(bsdar, 0, "%s", 198 archive_error_string(a)); 199 } 200 } 201 AC(archive_read_close(a)); 202 AC(archive_read_free(a)); 203 } 204