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$"); 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 71 struct progress_data { 72 struct bsdtar *bsdtar; 73 struct archive *archive; 74 struct archive_entry *entry; 75 }; 76 77 static void list_item_verbose(struct bsdtar *, FILE *, 78 struct archive_entry *); 79 static void read_archive(struct bsdtar *bsdtar, char mode, struct archive *); 80 static int unmatched_inclusions_warn(struct archive *matching, const char *); 81 82 83 void 84 tar_mode_t(struct bsdtar *bsdtar) 85 { 86 read_archive(bsdtar, 't', NULL); 87 if (unmatched_inclusions_warn(bsdtar->matching, 88 "Not found in archive") != 0) 89 bsdtar->return_value = 1; 90 } 91 92 void 93 tar_mode_x(struct bsdtar *bsdtar) 94 { 95 struct archive *writer; 96 97 writer = archive_write_disk_new(); 98 if (writer == NULL) 99 lafe_errc(1, ENOMEM, "Cannot allocate disk writer object"); 100 if (!bsdtar->option_numeric_owner) 101 archive_write_disk_set_standard_lookup(writer); 102 archive_write_disk_set_options(writer, bsdtar->extract_flags); 103 104 read_archive(bsdtar, 'x', writer); 105 106 if (unmatched_inclusions_warn(bsdtar->matching, 107 "Not found in archive") != 0) 108 bsdtar->return_value = 1; 109 archive_write_free(writer); 110 } 111 112 static void 113 progress_func(void *cookie) 114 { 115 struct progress_data *progress_data = cookie; 116 struct bsdtar *bsdtar = progress_data->bsdtar; 117 struct archive *a = progress_data->archive; 118 struct archive_entry *entry = progress_data->entry; 119 uint64_t comp, uncomp; 120 int compression; 121 122 if (!need_report()) 123 return; 124 125 if (bsdtar->verbose) 126 fprintf(stderr, "\n"); 127 if (a != NULL) { 128 comp = archive_position_compressed(a); 129 uncomp = archive_position_uncompressed(a); 130 if (comp > uncomp) 131 compression = 0; 132 else 133 compression = (int)((uncomp - comp) * 100 / uncomp); 134 fprintf(stderr, 135 "In: %s bytes, compression %d%%;", 136 tar_i64toa(comp), compression); 137 fprintf(stderr, " Out: %d files, %s bytes\n", 138 archive_file_count(a), tar_i64toa(uncomp)); 139 } 140 if (entry != NULL) { 141 safe_fprintf(stderr, "Current: %s", 142 archive_entry_pathname(entry)); 143 fprintf(stderr, " (%s bytes)\n", 144 tar_i64toa(archive_entry_size(entry))); 145 } 146 } 147 148 /* 149 * Handle 'x' and 't' modes. 150 */ 151 static void 152 read_archive(struct bsdtar *bsdtar, char mode, struct archive *writer) 153 { 154 struct progress_data progress_data; 155 FILE *out; 156 struct archive *a; 157 struct archive_entry *entry; 158 int r; 159 160 while (*bsdtar->argv) { 161 if (archive_match_include_pattern(bsdtar->matching, 162 *bsdtar->argv) != ARCHIVE_OK) 163 lafe_errc(1, 0, "Error inclusion pattern: %s", 164 archive_error_string(bsdtar->matching)); 165 bsdtar->argv++; 166 } 167 168 if (bsdtar->names_from_file != NULL) 169 if (archive_match_include_pattern_from_file( 170 bsdtar->matching, bsdtar->names_from_file, 171 bsdtar->option_null) != ARCHIVE_OK) 172 lafe_errc(1, 0, "Error inclusion pattern: %s", 173 archive_error_string(bsdtar->matching)); 174 175 a = archive_read_new(); 176 if (bsdtar->compress_program != NULL) 177 archive_read_support_filter_program(a, bsdtar->compress_program); 178 else 179 archive_read_support_filter_all(a); 180 archive_read_support_format_all(a); 181 if (ARCHIVE_OK != archive_read_set_options(a, bsdtar->option_options)) 182 lafe_errc(1, 0, "%s", archive_error_string(a)); 183 if (archive_read_open_file(a, bsdtar->filename, bsdtar->bytes_per_block)) 184 lafe_errc(1, 0, "Error opening archive: %s", 185 archive_error_string(a)); 186 187 do_chdir(bsdtar); 188 189 if (mode == 'x') { 190 /* Set an extract callback so that we can handle SIGINFO. */ 191 progress_data.bsdtar = bsdtar; 192 progress_data.archive = a; 193 archive_read_extract_set_progress_callback(a, progress_func, 194 &progress_data); 195 } 196 197 if (mode == 'x' && bsdtar->option_chroot) { 198 #if HAVE_CHROOT 199 if (chroot(".") != 0) 200 lafe_errc(1, errno, "Can't chroot to \".\""); 201 #else 202 lafe_errc(1, 0, 203 "chroot isn't supported on this platform"); 204 #endif 205 } 206 207 for (;;) { 208 /* Support --fast-read option */ 209 if (bsdtar->option_fast_read && 210 archive_match_path_unmatched_inclusions(bsdtar->matching) == 0) 211 break; 212 213 r = archive_read_next_header(a, &entry); 214 progress_data.entry = entry; 215 if (r == ARCHIVE_EOF) 216 break; 217 if (r < ARCHIVE_OK) 218 lafe_warnc(0, "%s", archive_error_string(a)); 219 if (r <= ARCHIVE_WARN) 220 bsdtar->return_value = 1; 221 if (r == ARCHIVE_RETRY) { 222 /* Retryable error: try again */ 223 lafe_warnc(0, "Retrying..."); 224 continue; 225 } 226 if (r == ARCHIVE_FATAL) 227 break; 228 229 if (bsdtar->uid >= 0) { 230 archive_entry_set_uid(entry, bsdtar->uid); 231 archive_entry_set_uname(entry, NULL); 232 } 233 if (bsdtar->gid >= 0) { 234 archive_entry_set_gid(entry, bsdtar->gid); 235 archive_entry_set_gname(entry, NULL); 236 } 237 if (bsdtar->uname) 238 archive_entry_set_uname(entry, bsdtar->uname); 239 if (bsdtar->gname) 240 archive_entry_set_gname(entry, bsdtar->gname); 241 242 /* 243 * Note that pattern exclusions are checked before 244 * pathname rewrites are handled. This gives more 245 * control over exclusions, since rewrites always lose 246 * information. (For example, consider a rewrite 247 * s/foo[0-9]/foo/. If we check exclusions after the 248 * rewrite, there would be no way to exclude foo1/bar 249 * while allowing foo2/bar.) 250 */ 251 if (archive_match_excluded(bsdtar->matching, entry)) 252 continue; /* Excluded by a pattern test. */ 253 254 if (mode == 't') { 255 /* Perversely, gtar uses -O to mean "send to stderr" 256 * when used with -t. */ 257 out = bsdtar->option_stdout ? stderr : stdout; 258 259 /* 260 * TODO: Provide some reasonable way to 261 * preview rewrites. gtar always displays 262 * the unedited path in -t output, which means 263 * you cannot easily preview rewrites. 264 */ 265 if (bsdtar->verbose < 2) 266 safe_fprintf(out, "%s", 267 archive_entry_pathname(entry)); 268 else 269 list_item_verbose(bsdtar, out, entry); 270 fflush(out); 271 r = archive_read_data_skip(a); 272 if (r == ARCHIVE_WARN) { 273 fprintf(out, "\n"); 274 lafe_warnc(0, "%s", 275 archive_error_string(a)); 276 } 277 if (r == ARCHIVE_RETRY) { 278 fprintf(out, "\n"); 279 lafe_warnc(0, "%s", 280 archive_error_string(a)); 281 } 282 if (r == ARCHIVE_FATAL) { 283 fprintf(out, "\n"); 284 lafe_warnc(0, "%s", 285 archive_error_string(a)); 286 bsdtar->return_value = 1; 287 break; 288 } 289 fprintf(out, "\n"); 290 } else { 291 /* Note: some rewrite failures prevent extraction. */ 292 if (edit_pathname(bsdtar, entry)) 293 continue; /* Excluded by a rewrite failure. */ 294 295 if (bsdtar->option_interactive && 296 !yes("extract '%s'", archive_entry_pathname(entry))) 297 continue; 298 299 /* 300 * Format here is from SUSv2, including the 301 * deferred '\n'. 302 */ 303 if (bsdtar->verbose) { 304 safe_fprintf(stderr, "x %s", 305 archive_entry_pathname(entry)); 306 fflush(stderr); 307 } 308 309 /* TODO siginfo_printinfo(bsdtar, 0); */ 310 311 if (bsdtar->option_stdout) 312 r = archive_read_data_into_fd(a, 1); 313 else 314 r = archive_read_extract2(a, entry, writer); 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_free(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 */ 355 static void 356 list_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 447 static int 448 unmatched_inclusions_warn(struct archive *matching, const char *msg) 449 { 450 const char *p; 451 int r; 452 453 if (matching == NULL) 454 return (0); 455 456 while ((r = archive_match_path_unmatched_inclusions_next( 457 matching, &p)) == ARCHIVE_OK) 458 lafe_warnc(0, "%s: %s", p, msg); 459 if (r == ARCHIVE_FATAL) 460 lafe_errc(1, errno, "Out of memory"); 461 462 return (archive_match_path_unmatched_inclusions(matching)); 463 } 464