read.c revision 228761
1/*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "bsdtar_platform.h" 27__FBSDID("$FreeBSD: src/usr.bin/tar/read.c,v 1.40 2008/08/21 06:41:14 kientzle Exp $"); 28 29#ifdef HAVE_SYS_TYPES_H 30#include <sys/types.h> 31#endif 32#ifdef HAVE_SYS_PARAM_H 33#include <sys/param.h> 34#endif 35#ifdef HAVE_SYS_STAT_H 36#include <sys/stat.h> 37#endif 38 39#ifdef HAVE_ERRNO_H 40#include <errno.h> 41#endif 42#ifdef HAVE_GRP_H 43#include <grp.h> 44#endif 45#ifdef HAVE_LIMITS_H 46#include <limits.h> 47#endif 48#ifdef HAVE_PWD_H 49#include <pwd.h> 50#endif 51#ifdef HAVE_STDINT_H 52#include <stdint.h> 53#endif 54#include <stdio.h> 55#ifdef HAVE_STDLIB_H 56#include <stdlib.h> 57#endif 58#ifdef HAVE_STRING_H 59#include <string.h> 60#endif 61#ifdef HAVE_TIME_H 62#include <time.h> 63#endif 64#ifdef HAVE_UNISTD_H 65#include <unistd.h> 66#endif 67 68#include "bsdtar.h" 69#include "err.h" 70 71struct progress_data { 72 struct bsdtar *bsdtar; 73 struct archive *archive; 74 struct archive_entry *entry; 75}; 76 77static void list_item_verbose(struct bsdtar *, FILE *, 78 struct archive_entry *); 79static void read_archive(struct bsdtar *bsdtar, char mode); 80 81void 82tar_mode_t(struct bsdtar *bsdtar) 83{ 84 read_archive(bsdtar, 't'); 85 if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0) 86 bsdtar->return_value = 1; 87} 88 89void 90tar_mode_x(struct bsdtar *bsdtar) 91{ 92 read_archive(bsdtar, 'x'); 93 94 if (lafe_unmatched_inclusions_warn(bsdtar->matching, "Not found in archive") != 0) 95 bsdtar->return_value = 1; 96} 97 98static void 99progress_func(void *cookie) 100{ 101 struct progress_data *progress_data = cookie; 102 struct bsdtar *bsdtar = progress_data->bsdtar; 103 struct archive *a = progress_data->archive; 104 struct archive_entry *entry = progress_data->entry; 105 uint64_t comp, uncomp; 106 107 if (!need_report()) 108 return; 109 110 if (bsdtar->verbose) 111 fprintf(stderr, "\n"); 112 if (a != NULL) { 113 comp = archive_position_compressed(a); 114 uncomp = archive_position_uncompressed(a); 115 fprintf(stderr, 116 "In: %s bytes, compression %d%%;", 117 tar_i64toa(comp), (int)((uncomp - comp) * 100 / uncomp)); 118 fprintf(stderr, " Out: %d files, %s bytes\n", 119 archive_file_count(a), tar_i64toa(uncomp)); 120 } 121 if (entry != NULL) { 122 safe_fprintf(stderr, "Current: %s", 123 archive_entry_pathname(entry)); 124 fprintf(stderr, " (%s bytes)\n", 125 tar_i64toa(archive_entry_size(entry))); 126 } 127} 128 129/* 130 * Handle 'x' and 't' modes. 131 */ 132static void 133read_archive(struct bsdtar *bsdtar, char mode) 134{ 135 struct progress_data progress_data; 136 FILE *out; 137 struct archive *a; 138 struct archive_entry *entry; 139 const struct stat *st; 140 int r; 141 142 while (*bsdtar->argv) { 143 lafe_include(&bsdtar->matching, *bsdtar->argv); 144 bsdtar->argv++; 145 } 146 147 if (bsdtar->names_from_file != NULL) 148 lafe_include_from_file(&bsdtar->matching, 149 bsdtar->names_from_file, bsdtar->option_null); 150 151 a = archive_read_new(); 152 if (bsdtar->compress_program != NULL) 153 archive_read_support_compression_program(a, bsdtar->compress_program); 154 else 155 archive_read_support_compression_all(a); 156 archive_read_support_format_all(a); 157 if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options)) 158 lafe_errc(1, 0, "%s", archive_error_string(a)); 159 if (archive_read_open_file(a, bsdtar->filename, 160 bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block : 161 DEFAULT_BYTES_PER_BLOCK)) 162 lafe_errc(1, 0, "Error opening archive: %s", 163 archive_error_string(a)); 164 165 do_chdir(bsdtar); 166 167 if (mode == 'x') { 168 /* Set an extract callback so that we can handle SIGINFO. */ 169 progress_data.bsdtar = bsdtar; 170 progress_data.archive = a; 171 archive_read_extract_set_progress_callback(a, progress_func, 172 &progress_data); 173 } 174 175 if (mode == 'x' && bsdtar->option_chroot) { 176#if HAVE_CHROOT 177 if (chroot(".") != 0) 178 lafe_errc(1, errno, "Can't chroot to \".\""); 179#else 180 lafe_errc(1, 0, 181 "chroot isn't supported on this platform"); 182#endif 183 } 184 185 for (;;) { 186 /* Support --fast-read option */ 187 if (bsdtar->option_fast_read && 188 lafe_unmatched_inclusions(bsdtar->matching) == 0) 189 break; 190 191 r = archive_read_next_header(a, &entry); 192 progress_data.entry = entry; 193 if (r == ARCHIVE_EOF) 194 break; 195 if (r < ARCHIVE_OK) 196 lafe_warnc(0, "%s", archive_error_string(a)); 197 if (r <= ARCHIVE_WARN) 198 bsdtar->return_value = 1; 199 if (r == ARCHIVE_RETRY) { 200 /* Retryable error: try again */ 201 lafe_warnc(0, "Retrying..."); 202 continue; 203 } 204 if (r == ARCHIVE_FATAL) 205 break; 206 207 if (bsdtar->uid >= 0) { 208 archive_entry_set_uid(entry, bsdtar->uid); 209 archive_entry_set_uname(entry, NULL); 210 } 211 if (bsdtar->gid >= 0) { 212 archive_entry_set_gid(entry, bsdtar->gid); 213 archive_entry_set_gname(entry, NULL); 214 } 215 if (bsdtar->uname) 216 archive_entry_set_uname(entry, bsdtar->uname); 217 if (bsdtar->gname >= 0) 218 archive_entry_set_gname(entry, bsdtar->gname); 219 220 /* 221 * Exclude entries that are too old. 222 */ 223 st = archive_entry_stat(entry); 224 if (bsdtar->newer_ctime_sec > 0) { 225 if (st->st_ctime < bsdtar->newer_ctime_sec) 226 continue; /* Too old, skip it. */ 227 if (st->st_ctime == bsdtar->newer_ctime_sec 228 && ARCHIVE_STAT_CTIME_NANOS(st) 229 <= bsdtar->newer_ctime_nsec) 230 continue; /* Too old, skip it. */ 231 } 232 if (bsdtar->newer_mtime_sec > 0) { 233 if (st->st_mtime < bsdtar->newer_mtime_sec) 234 continue; /* Too old, skip it. */ 235 if (st->st_mtime == bsdtar->newer_mtime_sec 236 && ARCHIVE_STAT_MTIME_NANOS(st) 237 <= bsdtar->newer_mtime_nsec) 238 continue; /* Too old, skip it. */ 239 } 240 241 /* 242 * Note that pattern exclusions are checked before 243 * pathname rewrites are handled. This gives more 244 * control over exclusions, since rewrites always lose 245 * information. (For example, consider a rewrite 246 * s/foo[0-9]/foo/. If we check exclusions after the 247 * rewrite, there would be no way to exclude foo1/bar 248 * while allowing foo2/bar.) 249 */ 250 if (lafe_excluded(bsdtar->matching, archive_entry_pathname(entry))) 251 continue; /* Excluded by a pattern test. */ 252 253 if (mode == 't') { 254 /* Perversely, gtar uses -O to mean "send to stderr" 255 * when used with -t. */ 256 out = bsdtar->option_stdout ? stderr : stdout; 257 258 /* 259 * TODO: Provide some reasonable way to 260 * preview rewrites. gtar always displays 261 * the unedited path in -t output, which means 262 * you cannot easily preview rewrites. 263 */ 264 if (bsdtar->verbose < 2) 265 safe_fprintf(out, "%s", 266 archive_entry_pathname(entry)); 267 else 268 list_item_verbose(bsdtar, out, entry); 269 fflush(out); 270 r = archive_read_data_skip(a); 271 if (r == ARCHIVE_WARN) { 272 fprintf(out, "\n"); 273 lafe_warnc(0, "%s", 274 archive_error_string(a)); 275 } 276 if (r == ARCHIVE_RETRY) { 277 fprintf(out, "\n"); 278 lafe_warnc(0, "%s", 279 archive_error_string(a)); 280 } 281 if (r == ARCHIVE_FATAL) { 282 fprintf(out, "\n"); 283 lafe_warnc(0, "%s", 284 archive_error_string(a)); 285 bsdtar->return_value = 1; 286 break; 287 } 288 fprintf(out, "\n"); 289 } else { 290 /* Note: some rewrite failures prevent extraction. */ 291 if (edit_pathname(bsdtar, entry)) 292 continue; /* Excluded by a rewrite failure. */ 293 294 if (bsdtar->option_interactive && 295 !yes("extract '%s'", archive_entry_pathname(entry))) 296 continue; 297 298 /* 299 * Format here is from SUSv2, including the 300 * deferred '\n'. 301 */ 302 if (bsdtar->verbose) { 303 safe_fprintf(stderr, "x %s", 304 archive_entry_pathname(entry)); 305 fflush(stderr); 306 } 307 308 // TODO siginfo_printinfo(bsdtar, 0); 309 310 if (bsdtar->option_stdout) 311 r = archive_read_data_into_fd(a, 1); 312 else 313 r = archive_read_extract(a, entry, 314 bsdtar->extract_flags); 315 if (r != ARCHIVE_OK) { 316 if (!bsdtar->verbose) 317 safe_fprintf(stderr, "%s", 318 archive_entry_pathname(entry)); 319 safe_fprintf(stderr, ": %s", 320 archive_error_string(a)); 321 if (!bsdtar->verbose) 322 fprintf(stderr, "\n"); 323 bsdtar->return_value = 1; 324 } 325 if (bsdtar->verbose) 326 fprintf(stderr, "\n"); 327 if (r == ARCHIVE_FATAL) 328 break; 329 } 330 } 331 332 333 r = archive_read_close(a); 334 if (r != ARCHIVE_OK) 335 lafe_warnc(0, "%s", archive_error_string(a)); 336 if (r <= ARCHIVE_WARN) 337 bsdtar->return_value = 1; 338 339 if (bsdtar->verbose > 2) 340 fprintf(stdout, "Archive Format: %s, Compression: %s\n", 341 archive_format_name(a), archive_compression_name(a)); 342 343 archive_read_finish(a); 344} 345 346 347/* 348 * Display information about the current file. 349 * 350 * The format here roughly duplicates the output of 'ls -l'. 351 * This is based on SUSv2, where 'tar tv' is documented as 352 * listing additional information in an "unspecified format," 353 * and 'pax -l' is documented as using the same format as 'ls -l'. 354 */ 355static void 356list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry) 357{ 358 char tmp[100]; 359 size_t w; 360 const char *p; 361 const char *fmt; 362 time_t tim; 363 static time_t now; 364 365 /* 366 * We avoid collecting the entire list in memory at once by 367 * listing things as we see them. However, that also means we can't 368 * just pre-compute the field widths. Instead, we start with guesses 369 * and just widen them as necessary. These numbers are completely 370 * arbitrary. 371 */ 372 if (!bsdtar->u_width) { 373 bsdtar->u_width = 6; 374 bsdtar->gs_width = 13; 375 } 376 if (!now) 377 time(&now); 378 fprintf(out, "%s %d ", 379 archive_entry_strmode(entry), 380 archive_entry_nlink(entry)); 381 382 /* Use uname if it's present, else uid. */ 383 p = archive_entry_uname(entry); 384 if ((p == NULL) || (*p == '\0')) { 385 sprintf(tmp, "%lu ", 386 (unsigned long)archive_entry_uid(entry)); 387 p = tmp; 388 } 389 w = strlen(p); 390 if (w > bsdtar->u_width) 391 bsdtar->u_width = w; 392 fprintf(out, "%-*s ", (int)bsdtar->u_width, p); 393 394 /* Use gname if it's present, else gid. */ 395 p = archive_entry_gname(entry); 396 if (p != NULL && p[0] != '\0') { 397 fprintf(out, "%s", p); 398 w = strlen(p); 399 } else { 400 sprintf(tmp, "%lu", 401 (unsigned long)archive_entry_gid(entry)); 402 w = strlen(tmp); 403 fprintf(out, "%s", tmp); 404 } 405 406 /* 407 * Print device number or file size, right-aligned so as to make 408 * total width of group and devnum/filesize fields be gs_width. 409 * If gs_width is too small, grow it. 410 */ 411 if (archive_entry_filetype(entry) == AE_IFCHR 412 || archive_entry_filetype(entry) == AE_IFBLK) { 413 sprintf(tmp, "%lu,%lu", 414 (unsigned long)archive_entry_rdevmajor(entry), 415 (unsigned long)archive_entry_rdevminor(entry)); 416 } else { 417 strcpy(tmp, tar_i64toa(archive_entry_size(entry))); 418 } 419 if (w + strlen(tmp) >= bsdtar->gs_width) 420 bsdtar->gs_width = w+strlen(tmp)+1; 421 fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp); 422 423 /* Format the time using 'ls -l' conventions. */ 424 tim = archive_entry_mtime(entry); 425#define HALF_YEAR (time_t)365 * 86400 / 2 426#if defined(_WIN32) && !defined(__CYGWIN__) 427#define DAY_FMT "%d" /* Windows' strftime function does not support %e format. */ 428#else 429#define DAY_FMT "%e" /* Day number without leading zeros */ 430#endif 431 if (tim < now - HALF_YEAR || tim > now + HALF_YEAR) 432 fmt = bsdtar->day_first ? DAY_FMT " %b %Y" : "%b " DAY_FMT " %Y"; 433 else 434 fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M"; 435 strftime(tmp, sizeof(tmp), fmt, localtime(&tim)); 436 fprintf(out, " %s ", tmp); 437 safe_fprintf(out, "%s", archive_entry_pathname(entry)); 438 439 /* Extra information for links. */ 440 if (archive_entry_hardlink(entry)) /* Hard link */ 441 safe_fprintf(out, " link to %s", 442 archive_entry_hardlink(entry)); 443 else if (archive_entry_symlink(entry)) /* Symbolic link */ 444 safe_fprintf(out, " -> %s", archive_entry_symlink(entry)); 445} 446