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 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 28 #include "cpio_platform.h" 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/types.h> 32 #include <archive.h> 33 #include <archive_entry.h> 34 35 #ifdef HAVE_SYS_MKDEV_H 36 #include <sys/mkdev.h> 37 #endif 38 #ifdef HAVE_SYS_STAT_H 39 #include <sys/stat.h> 40 #endif 41 #ifdef HAVE_SYS_TIME_H 42 #include <sys/time.h> 43 #endif 44 #ifdef HAVE_ERRNO_H 45 #include <errno.h> 46 #endif 47 #ifdef HAVE_FCNTL_H 48 #include <fcntl.h> 49 #endif 50 #ifdef HAVE_GRP_H 51 #include <grp.h> 52 #endif 53 #ifdef HAVE_LOCALE_H 54 #include <locale.h> 55 #endif 56 #ifdef HAVE_PWD_H 57 #include <pwd.h> 58 #endif 59 #ifdef HAVE_SIGNAL_H 60 #include <signal.h> 61 #endif 62 #ifdef HAVE_STDARG_H 63 #include <stdarg.h> 64 #endif 65 #ifdef HAVE_STDINT_H 66 #include <stdint.h> 67 #endif 68 #include <stdio.h> 69 #ifdef HAVE_STDLIB_H 70 #include <stdlib.h> 71 #endif 72 #ifdef HAVE_STRING_H 73 #include <string.h> 74 #endif 75 #ifdef HAVE_UNISTD_H 76 #include <unistd.h> 77 #endif 78 #ifdef HAVE_TIME_H 79 #include <time.h> 80 #endif 81 82 #include "cpio.h" 83 #include "err.h" 84 #include "line_reader.h" 85 #include "passphrase.h" 86 87 /* Fixed size of uname/gname caches. */ 88 #define name_cache_size 101 89 90 #ifndef O_BINARY 91 #define O_BINARY 0 92 #endif 93 94 struct name_cache { 95 int probes; 96 int hits; 97 size_t size; 98 struct { 99 id_t id; 100 char *name; 101 } cache[name_cache_size]; 102 }; 103 104 static int extract_data(struct archive *, struct archive *); 105 const char * cpio_i64toa(int64_t); 106 static const char *cpio_rename(const char *name); 107 static int entry_to_archive(struct cpio *, struct archive_entry *); 108 static int file_to_archive(struct cpio *, const char *); 109 static void free_cache(struct name_cache *cache); 110 static void list_item_verbose(struct cpio *, struct archive_entry *); 111 static void long_help(void); 112 static const char *lookup_gname(struct cpio *, gid_t gid); 113 static int lookup_gname_helper(struct cpio *, 114 const char **name, id_t gid); 115 static const char *lookup_uname(struct cpio *, uid_t uid); 116 static int lookup_uname_helper(struct cpio *, 117 const char **name, id_t uid); 118 static void mode_in(struct cpio *); 119 static void mode_list(struct cpio *); 120 static void mode_out(struct cpio *); 121 static void mode_pass(struct cpio *, const char *); 122 static const char *remove_leading_slash(const char *); 123 static int restore_time(struct cpio *, struct archive_entry *, 124 const char *, int fd); 125 static void usage(void); 126 static void version(void); 127 static const char * passphrase_callback(struct archive *, void *); 128 static void passphrase_free(char *); 129 130 int 131 main(int argc, char *argv[]) 132 { 133 static char buff[16384]; 134 struct cpio _cpio; /* Allocated on stack. */ 135 struct cpio *cpio; 136 const char *errmsg; 137 int uid, gid; 138 int opt; 139 140 cpio = &_cpio; 141 memset(cpio, 0, sizeof(*cpio)); 142 cpio->buff = buff; 143 cpio->buff_size = sizeof(buff); 144 145 #if defined(HAVE_SIGACTION) && defined(SIGPIPE) 146 { /* Ignore SIGPIPE signals. */ 147 struct sigaction sa; 148 sigemptyset(&sa.sa_mask); 149 sa.sa_flags = 0; 150 sa.sa_handler = SIG_IGN; 151 sigaction(SIGPIPE, &sa, NULL); 152 } 153 #endif 154 155 /* Set lafe_progname before calling lafe_warnc. */ 156 lafe_setprogname(*argv, "bsdcpio"); 157 158 #if HAVE_SETLOCALE 159 if (setlocale(LC_ALL, "") == NULL) 160 lafe_warnc(0, "Failed to set default locale"); 161 #endif 162 163 cpio->uid_override = -1; 164 cpio->gid_override = -1; 165 cpio->argv = argv; 166 cpio->argc = argc; 167 cpio->mode = '\0'; 168 cpio->verbose = 0; 169 cpio->compress = '\0'; 170 cpio->extract_flags = ARCHIVE_EXTRACT_NO_AUTODIR; 171 cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 172 cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS; 173 cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT; 174 cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS; 175 cpio->extract_flags |= ARCHIVE_EXTRACT_PERM; 176 cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 177 cpio->extract_flags |= ARCHIVE_EXTRACT_ACL; 178 #if !defined(_WIN32) && !defined(__CYGWIN__) 179 if (geteuid() == 0) 180 cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; 181 #endif 182 cpio->bytes_per_block = 512; 183 cpio->filename = NULL; 184 185 cpio->matching = archive_match_new(); 186 if (cpio->matching == NULL) 187 lafe_errc(1, 0, "Out of memory"); 188 189 while ((opt = cpio_getopt(cpio)) != -1) { 190 switch (opt) { 191 case '0': /* GNU convention: --null, -0 */ 192 cpio->option_null = 1; 193 break; 194 case 'A': /* NetBSD/OpenBSD */ 195 cpio->option_append = 1; 196 break; 197 case 'a': /* POSIX 1997 */ 198 cpio->option_atime_restore = 1; 199 break; 200 case 'B': /* POSIX 1997 */ 201 cpio->bytes_per_block = 5120; 202 break; 203 case OPTION_B64ENCODE: 204 cpio->add_filter = opt; 205 break; 206 case 'C': /* NetBSD/OpenBSD */ 207 cpio->bytes_per_block = atoi(cpio->argument); 208 if (cpio->bytes_per_block <= 0) 209 lafe_errc(1, 0, "Invalid blocksize %s", cpio->argument); 210 break; 211 case 'c': /* POSIX 1997 */ 212 cpio->format = "odc"; 213 break; 214 case 'd': /* POSIX 1997 */ 215 cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR; 216 break; 217 case 'E': /* NetBSD/OpenBSD */ 218 if (archive_match_include_pattern_from_file( 219 cpio->matching, cpio->argument, 220 cpio->option_null) != ARCHIVE_OK) 221 lafe_errc(1, 0, "Error : %s", 222 archive_error_string(cpio->matching)); 223 break; 224 case 'F': /* NetBSD/OpenBSD/GNU cpio */ 225 cpio->filename = cpio->argument; 226 break; 227 case 'f': /* POSIX 1997 */ 228 if (archive_match_exclude_pattern(cpio->matching, 229 cpio->argument) != ARCHIVE_OK) 230 lafe_errc(1, 0, "Error : %s", 231 archive_error_string(cpio->matching)); 232 break; 233 case OPTION_GRZIP: 234 cpio->compress = opt; 235 break; 236 case 'H': /* GNU cpio (also --format) */ 237 cpio->format = cpio->argument; 238 break; 239 case 'h': 240 long_help(); 241 break; 242 case 'I': /* NetBSD/OpenBSD */ 243 cpio->filename = cpio->argument; 244 break; 245 case 'i': /* POSIX 1997 */ 246 if (cpio->mode != '\0') 247 lafe_errc(1, 0, 248 "Cannot use both -i and -%c", cpio->mode); 249 cpio->mode = opt; 250 break; 251 case 'J': /* GNU tar, others */ 252 cpio->compress = opt; 253 break; 254 case 'j': /* GNU tar, others */ 255 cpio->compress = opt; 256 break; 257 case OPTION_INSECURE: 258 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS; 259 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; 260 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS; 261 break; 262 case 'L': /* GNU cpio */ 263 cpio->option_follow_links = 1; 264 break; 265 case 'l': /* POSIX 1997 */ 266 cpio->option_link = 1; 267 break; 268 case OPTION_LRZIP: 269 case OPTION_LZ4: 270 case OPTION_LZMA: /* GNU tar, others */ 271 case OPTION_LZOP: /* GNU tar, others */ 272 cpio->compress = opt; 273 break; 274 case 'm': /* POSIX 1997 */ 275 cpio->extract_flags |= ARCHIVE_EXTRACT_TIME; 276 break; 277 case 'n': /* GNU cpio */ 278 cpio->option_numeric_uid_gid = 1; 279 break; 280 case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */ 281 cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; 282 break; 283 case 'O': /* GNU cpio */ 284 cpio->filename = cpio->argument; 285 break; 286 case 'o': /* POSIX 1997 */ 287 if (cpio->mode != '\0') 288 lafe_errc(1, 0, 289 "Cannot use both -o and -%c", cpio->mode); 290 cpio->mode = opt; 291 break; 292 case 'p': /* POSIX 1997 */ 293 if (cpio->mode != '\0') 294 lafe_errc(1, 0, 295 "Cannot use both -p and -%c", cpio->mode); 296 cpio->mode = opt; 297 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; 298 break; 299 case OPTION_PASSPHRASE: 300 cpio->passphrase = cpio->argument; 301 break; 302 case OPTION_PRESERVE_OWNER: 303 cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; 304 break; 305 case OPTION_QUIET: /* GNU cpio */ 306 cpio->quiet = 1; 307 break; 308 case 'R': /* GNU cpio, also --owner */ 309 /* TODO: owner_parse should return uname/gname 310 * also; use that to set [ug]name_override. */ 311 errmsg = owner_parse(cpio->argument, &uid, &gid); 312 if (errmsg) { 313 lafe_warnc(-1, "%s", errmsg); 314 usage(); 315 } 316 if (uid != -1) { 317 cpio->uid_override = uid; 318 cpio->uname_override = NULL; 319 } 320 if (gid != -1) { 321 cpio->gid_override = gid; 322 cpio->gname_override = NULL; 323 } 324 break; 325 case 'r': /* POSIX 1997 */ 326 cpio->option_rename = 1; 327 break; 328 case 't': /* POSIX 1997 */ 329 cpio->option_list = 1; 330 break; 331 case 'u': /* POSIX 1997 */ 332 cpio->extract_flags 333 &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 334 break; 335 case OPTION_UUENCODE: 336 cpio->add_filter = opt; 337 break; 338 case 'v': /* POSIX 1997 */ 339 cpio->verbose++; 340 break; 341 case 'V': /* GNU cpio */ 342 cpio->dot++; 343 break; 344 case OPTION_VERSION: /* GNU convention */ 345 version(); 346 break; 347 #if 0 348 /* 349 * cpio_getopt() handles -W specially, so it's not 350 * available here. 351 */ 352 case 'W': /* Obscure, but useful GNU convention. */ 353 break; 354 #endif 355 case 'y': /* tar convention */ 356 cpio->compress = opt; 357 break; 358 case 'Z': /* tar convention */ 359 cpio->compress = opt; 360 break; 361 case 'z': /* tar convention */ 362 cpio->compress = opt; 363 break; 364 default: 365 usage(); 366 } 367 } 368 369 /* 370 * Sanity-check args, error out on nonsensical combinations. 371 */ 372 /* -t implies -i if no mode was specified. */ 373 if (cpio->option_list && cpio->mode == '\0') 374 cpio->mode = 'i'; 375 /* -t requires -i */ 376 if (cpio->option_list && cpio->mode != 'i') 377 lafe_errc(1, 0, "Option -t requires -i"); 378 /* -n requires -it */ 379 if (cpio->option_numeric_uid_gid && !cpio->option_list) 380 lafe_errc(1, 0, "Option -n requires -it"); 381 /* Can only specify format when writing */ 382 if (cpio->format != NULL && cpio->mode != 'o') 383 lafe_errc(1, 0, "Option --format requires -o"); 384 /* -l requires -p */ 385 if (cpio->option_link && cpio->mode != 'p') 386 lafe_errc(1, 0, "Option -l requires -p"); 387 /* -v overrides -V */ 388 if (cpio->dot && cpio->verbose) 389 cpio->dot = 0; 390 /* TODO: Flag other nonsensical combinations. */ 391 392 switch (cpio->mode) { 393 case 'o': 394 /* TODO: Implement old binary format in libarchive, 395 use that here. */ 396 if (cpio->format == NULL) 397 cpio->format = "odc"; /* Default format */ 398 399 mode_out(cpio); 400 break; 401 case 'i': 402 while (*cpio->argv != NULL) { 403 if (archive_match_include_pattern(cpio->matching, 404 *cpio->argv) != ARCHIVE_OK) 405 lafe_errc(1, 0, "Error : %s", 406 archive_error_string(cpio->matching)); 407 --cpio->argc; 408 ++cpio->argv; 409 } 410 if (cpio->option_list) 411 mode_list(cpio); 412 else 413 mode_in(cpio); 414 break; 415 case 'p': 416 if (*cpio->argv == NULL || **cpio->argv == '\0') 417 lafe_errc(1, 0, 418 "-p mode requires a target directory"); 419 mode_pass(cpio, *cpio->argv); 420 break; 421 default: 422 lafe_errc(1, 0, 423 "Must specify at least one of -i, -o, or -p"); 424 } 425 426 archive_match_free(cpio->matching); 427 free_cache(cpio->gname_cache); 428 free_cache(cpio->uname_cache); 429 free(cpio->destdir); 430 passphrase_free(cpio->ppbuff); 431 return (cpio->return_value); 432 } 433 434 static void 435 usage(void) 436 { 437 const char *p; 438 439 p = lafe_getprogname(); 440 441 fprintf(stderr, "Brief Usage:\n"); 442 fprintf(stderr, " List: %s -it < archive\n", p); 443 fprintf(stderr, " Extract: %s -i < archive\n", p); 444 fprintf(stderr, " Create: %s -o < filenames > archive\n", p); 445 fprintf(stderr, " Help: %s --help\n", p); 446 exit(1); 447 } 448 449 static const char *long_help_msg = 450 "First option must be a mode specifier:\n" 451 " -i Input -o Output -p Pass\n" 452 "Common Options:\n" 453 " -v Verbose filenames -V one dot per file\n" 454 "Create: %p -o [options] < [list of files] > [archive]\n" 455 " -J,-y,-z,--lzma Compress archive with xz/bzip2/gzip/lzma\n" 456 " --format {odc|newc|ustar} Select archive format\n" 457 "List: %p -it < [archive]\n" 458 "Extract: %p -i [options] < [archive]\n"; 459 460 461 /* 462 * Note that the word 'bsdcpio' will always appear in the first line 463 * of output. 464 * 465 * In particular, /bin/sh scripts that need to test for the presence 466 * of bsdcpio can use the following template: 467 * 468 * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \ 469 * echo bsdcpio; else echo not bsdcpio; fi 470 */ 471 static void 472 long_help(void) 473 { 474 const char *prog; 475 const char *p; 476 477 prog = lafe_getprogname(); 478 479 fflush(stderr); 480 481 p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : ""; 482 printf("%s%s: manipulate archive files\n", prog, p); 483 484 for (p = long_help_msg; *p != '\0'; p++) { 485 if (*p == '%') { 486 if (p[1] == 'p') { 487 fputs(prog, stdout); 488 p++; 489 } else 490 putchar('%'); 491 } else 492 putchar(*p); 493 } 494 version(); 495 } 496 497 static void 498 version(void) 499 { 500 fprintf(stdout,"bsdcpio %s -- %s\n", 501 BSDCPIO_VERSION_STRING, 502 archive_version_details()); 503 exit(0); 504 } 505 506 static void 507 mode_out(struct cpio *cpio) 508 { 509 struct archive_entry *entry, *spare; 510 struct lafe_line_reader *lr; 511 const char *p; 512 int r; 513 514 if (cpio->option_append) 515 lafe_errc(1, 0, "Append mode not yet supported."); 516 517 cpio->archive_read_disk = archive_read_disk_new(); 518 if (cpio->archive_read_disk == NULL) 519 lafe_errc(1, 0, "Failed to allocate archive object"); 520 if (cpio->option_follow_links) 521 archive_read_disk_set_symlink_logical(cpio->archive_read_disk); 522 else 523 archive_read_disk_set_symlink_physical(cpio->archive_read_disk); 524 archive_read_disk_set_standard_lookup(cpio->archive_read_disk); 525 526 cpio->archive = archive_write_new(); 527 if (cpio->archive == NULL) 528 lafe_errc(1, 0, "Failed to allocate archive object"); 529 switch (cpio->compress) { 530 case OPTION_GRZIP: 531 r = archive_write_add_filter_grzip(cpio->archive); 532 break; 533 case 'J': 534 r = archive_write_add_filter_xz(cpio->archive); 535 break; 536 case OPTION_LRZIP: 537 r = archive_write_add_filter_lrzip(cpio->archive); 538 break; 539 case OPTION_LZ4: 540 r = archive_write_add_filter_lz4(cpio->archive); 541 break; 542 case OPTION_LZMA: 543 r = archive_write_add_filter_lzma(cpio->archive); 544 break; 545 case OPTION_LZOP: 546 r = archive_write_add_filter_lzop(cpio->archive); 547 break; 548 case 'j': case 'y': 549 r = archive_write_add_filter_bzip2(cpio->archive); 550 break; 551 case 'z': 552 r = archive_write_add_filter_gzip(cpio->archive); 553 break; 554 case 'Z': 555 r = archive_write_add_filter_compress(cpio->archive); 556 break; 557 default: 558 r = archive_write_add_filter_none(cpio->archive); 559 break; 560 } 561 if (r < ARCHIVE_WARN) 562 lafe_errc(1, 0, "Requested compression not available"); 563 switch (cpio->add_filter) { 564 case 0: 565 r = ARCHIVE_OK; 566 break; 567 case OPTION_B64ENCODE: 568 r = archive_write_add_filter_b64encode(cpio->archive); 569 break; 570 case OPTION_UUENCODE: 571 r = archive_write_add_filter_uuencode(cpio->archive); 572 break; 573 } 574 if (r < ARCHIVE_WARN) 575 lafe_errc(1, 0, "Requested filter not available"); 576 r = archive_write_set_format_by_name(cpio->archive, cpio->format); 577 if (r != ARCHIVE_OK) 578 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 579 archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block); 580 cpio->linkresolver = archive_entry_linkresolver_new(); 581 archive_entry_linkresolver_set_strategy(cpio->linkresolver, 582 archive_format(cpio->archive)); 583 if (cpio->passphrase != NULL) 584 r = archive_write_set_passphrase(cpio->archive, 585 cpio->passphrase); 586 else 587 r = archive_write_set_passphrase_callback(cpio->archive, cpio, 588 &passphrase_callback); 589 if (r != ARCHIVE_OK) 590 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 591 592 /* 593 * The main loop: Copy each file into the output archive. 594 */ 595 r = archive_write_open_filename(cpio->archive, cpio->filename); 596 if (r != ARCHIVE_OK) 597 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 598 lr = lafe_line_reader("-", cpio->option_null); 599 while ((p = lafe_line_reader_next(lr)) != NULL) 600 file_to_archive(cpio, p); 601 lafe_line_reader_free(lr); 602 603 /* 604 * The hardlink detection may have queued up a couple of entries 605 * that can now be flushed. 606 */ 607 entry = NULL; 608 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 609 while (entry != NULL) { 610 entry_to_archive(cpio, entry); 611 archive_entry_free(entry); 612 entry = NULL; 613 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 614 } 615 616 r = archive_write_close(cpio->archive); 617 if (cpio->dot) 618 fprintf(stderr, "\n"); 619 if (r != ARCHIVE_OK) 620 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 621 622 if (!cpio->quiet) { 623 int64_t blocks = 624 (archive_filter_bytes(cpio->archive, 0) + 511) 625 / 512; 626 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 627 blocks == 1 ? "block" : "blocks"); 628 } 629 archive_write_free(cpio->archive); 630 } 631 632 static const char * 633 remove_leading_slash(const char *p) 634 { 635 const char *rp; 636 637 /* Remove leading "//./" or "//?/" or "//?/UNC/" 638 * (absolute path prefixes used by Windows API) */ 639 if ((p[0] == '/' || p[0] == '\\') && 640 (p[1] == '/' || p[1] == '\\') && 641 (p[2] == '.' || p[2] == '?') && 642 (p[3] == '/' || p[3] == '\\')) 643 { 644 if (p[2] == '?' && 645 (p[4] == 'U' || p[4] == 'u') && 646 (p[5] == 'N' || p[5] == 'n') && 647 (p[6] == 'C' || p[6] == 'c') && 648 (p[7] == '/' || p[7] == '\\')) 649 p += 8; 650 else 651 p += 4; 652 } 653 do { 654 rp = p; 655 /* Remove leading drive letter from archives created 656 * on Windows. */ 657 if (((p[0] >= 'a' && p[0] <= 'z') || 658 (p[0] >= 'A' && p[0] <= 'Z')) && 659 p[1] == ':') { 660 p += 2; 661 } 662 /* Remove leading "/../", "//", etc. */ 663 while (p[0] == '/' || p[0] == '\\') { 664 if (p[1] == '.' && p[2] == '.' && 665 (p[3] == '/' || p[3] == '\\')) { 666 p += 3; /* Remove "/..", leave "/" 667 * for next pass. */ 668 } else 669 p += 1; /* Remove "/". */ 670 } 671 } while (rp != p); 672 return (p); 673 } 674 675 /* 676 * This is used by both out mode (to copy objects from disk into 677 * an archive) and pass mode (to copy objects from disk to 678 * an archive_write_disk "archive"). 679 */ 680 static int 681 file_to_archive(struct cpio *cpio, const char *srcpath) 682 { 683 const char *destpath; 684 struct archive_entry *entry, *spare; 685 size_t len; 686 int r; 687 688 /* 689 * Create an archive_entry describing the source file. 690 * 691 */ 692 entry = archive_entry_new(); 693 if (entry == NULL) 694 lafe_errc(1, 0, "Couldn't allocate entry"); 695 archive_entry_copy_sourcepath(entry, srcpath); 696 r = archive_read_disk_entry_from_file(cpio->archive_read_disk, 697 entry, -1, NULL); 698 if (r < ARCHIVE_FAILED) 699 lafe_errc(1, 0, "%s", 700 archive_error_string(cpio->archive_read_disk)); 701 if (r < ARCHIVE_OK) 702 lafe_warnc(0, "%s", 703 archive_error_string(cpio->archive_read_disk)); 704 if (r <= ARCHIVE_FAILED) { 705 cpio->return_value = 1; 706 return (r); 707 } 708 709 if (cpio->uid_override >= 0) { 710 archive_entry_set_uid(entry, cpio->uid_override); 711 archive_entry_set_uname(entry, cpio->uname_override); 712 } 713 if (cpio->gid_override >= 0) { 714 archive_entry_set_gid(entry, cpio->gid_override); 715 archive_entry_set_gname(entry, cpio->gname_override); 716 } 717 718 /* 719 * Generate a destination path for this entry. 720 * "destination path" is the name to which it will be copied in 721 * pass mode or the name that will go into the archive in 722 * output mode. 723 */ 724 destpath = srcpath; 725 if (cpio->destdir) { 726 len = strlen(cpio->destdir) + strlen(srcpath) + 8; 727 if (len >= cpio->pass_destpath_alloc) { 728 while (len >= cpio->pass_destpath_alloc) { 729 cpio->pass_destpath_alloc += 512; 730 cpio->pass_destpath_alloc *= 2; 731 } 732 free(cpio->pass_destpath); 733 cpio->pass_destpath = malloc(cpio->pass_destpath_alloc); 734 if (cpio->pass_destpath == NULL) 735 lafe_errc(1, ENOMEM, 736 "Can't allocate path buffer"); 737 } 738 strcpy(cpio->pass_destpath, cpio->destdir); 739 strcat(cpio->pass_destpath, remove_leading_slash(srcpath)); 740 destpath = cpio->pass_destpath; 741 } 742 if (cpio->option_rename) 743 destpath = cpio_rename(destpath); 744 if (destpath == NULL) 745 return (0); 746 archive_entry_copy_pathname(entry, destpath); 747 748 /* 749 * If we're trying to preserve hardlinks, match them here. 750 */ 751 spare = NULL; 752 if (cpio->linkresolver != NULL 753 && archive_entry_filetype(entry) != AE_IFDIR) { 754 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 755 } 756 757 if (entry != NULL) { 758 r = entry_to_archive(cpio, entry); 759 archive_entry_free(entry); 760 if (spare != NULL) { 761 if (r == 0) 762 r = entry_to_archive(cpio, spare); 763 archive_entry_free(spare); 764 } 765 } 766 return (r); 767 } 768 769 static int 770 entry_to_archive(struct cpio *cpio, struct archive_entry *entry) 771 { 772 const char *destpath = archive_entry_pathname(entry); 773 const char *srcpath = archive_entry_sourcepath(entry); 774 int fd = -1; 775 ssize_t bytes_read; 776 int r; 777 778 /* Print out the destination name to the user. */ 779 if (cpio->verbose) 780 fprintf(stderr,"%s", destpath); 781 if (cpio->dot) 782 fprintf(stderr, "."); 783 784 /* 785 * Option_link only makes sense in pass mode and for 786 * regular files. Also note: if a link operation fails 787 * because of cross-device restrictions, we'll fall back 788 * to copy mode for that entry. 789 * 790 * TODO: Test other cpio implementations to see if they 791 * hard-link anything other than regular files here. 792 */ 793 if (cpio->option_link 794 && archive_entry_filetype(entry) == AE_IFREG) 795 { 796 struct archive_entry *t; 797 /* Save the original entry in case we need it later. */ 798 t = archive_entry_clone(entry); 799 if (t == NULL) 800 lafe_errc(1, ENOMEM, "Can't create link"); 801 /* Note: link(2) doesn't create parent directories, 802 * so we use archive_write_header() instead as a 803 * convenience. */ 804 archive_entry_set_hardlink(t, srcpath); 805 /* This is a straight link that carries no data. */ 806 archive_entry_set_size(t, 0); 807 r = archive_write_header(cpio->archive, t); 808 archive_entry_free(t); 809 if (r != ARCHIVE_OK) 810 lafe_warnc(archive_errno(cpio->archive), 811 "%s", archive_error_string(cpio->archive)); 812 if (r == ARCHIVE_FATAL) 813 exit(1); 814 #ifdef EXDEV 815 if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) { 816 /* Cross-device link: Just fall through and use 817 * the original entry to copy the file over. */ 818 lafe_warnc(0, "Copying file instead"); 819 } else 820 #endif 821 return (0); 822 } 823 824 /* 825 * Make sure we can open the file (if necessary) before 826 * trying to write the header. 827 */ 828 if (archive_entry_filetype(entry) == AE_IFREG) { 829 if (archive_entry_size(entry) > 0) { 830 fd = open(srcpath, O_RDONLY | O_BINARY); 831 if (fd < 0) { 832 lafe_warnc(errno, 833 "%s: could not open file", srcpath); 834 goto cleanup; 835 } 836 } 837 } else { 838 archive_entry_set_size(entry, 0); 839 } 840 841 r = archive_write_header(cpio->archive, entry); 842 843 if (r != ARCHIVE_OK) 844 lafe_warnc(archive_errno(cpio->archive), 845 "%s: %s", 846 srcpath, 847 archive_error_string(cpio->archive)); 848 849 if (r == ARCHIVE_FATAL) 850 exit(1); 851 852 if (r >= ARCHIVE_WARN && archive_entry_size(entry) > 0 && fd >= 0) { 853 bytes_read = read(fd, cpio->buff, (unsigned)cpio->buff_size); 854 while (bytes_read > 0) { 855 ssize_t bytes_write; 856 bytes_write = archive_write_data(cpio->archive, 857 cpio->buff, bytes_read); 858 if (bytes_write < 0) 859 lafe_errc(1, archive_errno(cpio->archive), 860 "%s", archive_error_string(cpio->archive)); 861 if (bytes_write < bytes_read) { 862 lafe_warnc(0, 863 "Truncated write; file may have " 864 "grown while being archived."); 865 } 866 bytes_read = read(fd, cpio->buff, 867 (unsigned)cpio->buff_size); 868 } 869 } 870 871 fd = restore_time(cpio, entry, srcpath, fd); 872 873 cleanup: 874 if (cpio->verbose) 875 fprintf(stderr,"\n"); 876 if (fd >= 0) 877 close(fd); 878 return (0); 879 } 880 881 static int 882 restore_time(struct cpio *cpio, struct archive_entry *entry, 883 const char *name, int fd) 884 { 885 #ifndef HAVE_UTIMES 886 static int warned = 0; 887 888 (void)cpio; /* UNUSED */ 889 (void)entry; /* UNUSED */ 890 (void)name; /* UNUSED */ 891 892 if (!warned) 893 lafe_warnc(0, "Can't restore access times on this platform"); 894 warned = 1; 895 return (fd); 896 #else 897 #if defined(_WIN32) && !defined(__CYGWIN__) 898 struct __timeval times[2]; 899 #else 900 struct timeval times[2]; 901 #endif 902 903 if (!cpio->option_atime_restore) 904 return (fd); 905 906 times[1].tv_sec = archive_entry_mtime(entry); 907 times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000; 908 909 times[0].tv_sec = archive_entry_atime(entry); 910 times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000; 911 912 #if defined(HAVE_FUTIMES) && !defined(__CYGWIN__) 913 if (fd >= 0 && futimes(fd, times) == 0) 914 return (fd); 915 #endif 916 /* 917 * Some platform cannot restore access times if the file descriptor 918 * is still opened. 919 */ 920 if (fd >= 0) { 921 close(fd); 922 fd = -1; 923 } 924 925 #ifdef HAVE_LUTIMES 926 if (lutimes(name, times) != 0) 927 #else 928 if ((AE_IFLNK != archive_entry_filetype(entry)) 929 && utimes(name, times) != 0) 930 #endif 931 lafe_warnc(errno, "Can't update time for %s", name); 932 #endif 933 return (fd); 934 } 935 936 937 static void 938 mode_in(struct cpio *cpio) 939 { 940 struct archive *a; 941 struct archive_entry *entry; 942 struct archive *ext; 943 const char *destpath; 944 int r; 945 946 ext = archive_write_disk_new(); 947 if (ext == NULL) 948 lafe_errc(1, 0, "Couldn't allocate restore object"); 949 r = archive_write_disk_set_options(ext, cpio->extract_flags); 950 if (r != ARCHIVE_OK) 951 lafe_errc(1, 0, "%s", archive_error_string(ext)); 952 a = archive_read_new(); 953 if (a == NULL) 954 lafe_errc(1, 0, "Couldn't allocate archive object"); 955 archive_read_support_filter_all(a); 956 archive_read_support_format_all(a); 957 if (cpio->passphrase != NULL) 958 r = archive_read_add_passphrase(a, cpio->passphrase); 959 else 960 r = archive_read_set_passphrase_callback(a, cpio, 961 &passphrase_callback); 962 if (r != ARCHIVE_OK) 963 lafe_errc(1, 0, "%s", archive_error_string(a)); 964 965 if (archive_read_open_filename(a, cpio->filename, 966 cpio->bytes_per_block)) 967 lafe_errc(1, archive_errno(a), 968 "%s", archive_error_string(a)); 969 for (;;) { 970 r = archive_read_next_header(a, &entry); 971 if (r == ARCHIVE_EOF) 972 break; 973 if (r != ARCHIVE_OK) { 974 lafe_errc(1, archive_errno(a), 975 "%s", archive_error_string(a)); 976 } 977 if (archive_match_path_excluded(cpio->matching, entry)) 978 continue; 979 if (cpio->option_rename) { 980 destpath = cpio_rename(archive_entry_pathname(entry)); 981 archive_entry_set_pathname(entry, destpath); 982 } else 983 destpath = archive_entry_pathname(entry); 984 if (destpath == NULL) 985 continue; 986 if (cpio->verbose) 987 fprintf(stderr, "%s\n", destpath); 988 if (cpio->dot) 989 fprintf(stderr, "."); 990 if (cpio->uid_override >= 0) 991 archive_entry_set_uid(entry, cpio->uid_override); 992 if (cpio->gid_override >= 0) 993 archive_entry_set_gid(entry, cpio->gid_override); 994 r = archive_write_header(ext, entry); 995 if (r != ARCHIVE_OK) { 996 fprintf(stderr, "%s: %s\n", 997 archive_entry_pathname(entry), 998 archive_error_string(ext)); 999 } else if (!archive_entry_size_is_set(entry) 1000 || archive_entry_size(entry) > 0) { 1001 r = extract_data(a, ext); 1002 if (r != ARCHIVE_OK) 1003 cpio->return_value = 1; 1004 } 1005 } 1006 r = archive_read_close(a); 1007 if (cpio->dot) 1008 fprintf(stderr, "\n"); 1009 if (r != ARCHIVE_OK) 1010 lafe_errc(1, 0, "%s", archive_error_string(a)); 1011 r = archive_write_close(ext); 1012 if (r != ARCHIVE_OK) 1013 lafe_errc(1, 0, "%s", archive_error_string(ext)); 1014 if (!cpio->quiet) { 1015 int64_t blocks = (archive_filter_bytes(a, 0) + 511) 1016 / 512; 1017 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 1018 blocks == 1 ? "block" : "blocks"); 1019 } 1020 archive_read_free(a); 1021 archive_write_free(ext); 1022 exit(cpio->return_value); 1023 } 1024 1025 /* 1026 * Exits if there's a fatal error. Returns ARCHIVE_OK 1027 * if everything is kosher. 1028 */ 1029 static int 1030 extract_data(struct archive *ar, struct archive *aw) 1031 { 1032 int r; 1033 size_t size; 1034 const void *block; 1035 int64_t offset; 1036 1037 for (;;) { 1038 r = archive_read_data_block(ar, &block, &size, &offset); 1039 if (r == ARCHIVE_EOF) 1040 return (ARCHIVE_OK); 1041 if (r != ARCHIVE_OK) { 1042 lafe_warnc(archive_errno(ar), 1043 "%s", archive_error_string(ar)); 1044 exit(1); 1045 } 1046 r = (int)archive_write_data_block(aw, block, size, offset); 1047 if (r != ARCHIVE_OK) { 1048 lafe_warnc(archive_errno(aw), 1049 "%s", archive_error_string(aw)); 1050 return (r); 1051 } 1052 } 1053 } 1054 1055 static void 1056 mode_list(struct cpio *cpio) 1057 { 1058 struct archive *a; 1059 struct archive_entry *entry; 1060 int r; 1061 1062 a = archive_read_new(); 1063 if (a == NULL) 1064 lafe_errc(1, 0, "Couldn't allocate archive object"); 1065 archive_read_support_filter_all(a); 1066 archive_read_support_format_all(a); 1067 if (cpio->passphrase != NULL) 1068 r = archive_read_add_passphrase(a, cpio->passphrase); 1069 else 1070 r = archive_read_set_passphrase_callback(a, cpio, 1071 &passphrase_callback); 1072 if (r != ARCHIVE_OK) 1073 lafe_errc(1, 0, "%s", archive_error_string(a)); 1074 1075 if (archive_read_open_filename(a, cpio->filename, 1076 cpio->bytes_per_block)) 1077 lafe_errc(1, archive_errno(a), 1078 "%s", archive_error_string(a)); 1079 for (;;) { 1080 r = archive_read_next_header(a, &entry); 1081 if (r == ARCHIVE_EOF) 1082 break; 1083 if (r != ARCHIVE_OK) { 1084 lafe_errc(1, archive_errno(a), 1085 "%s", archive_error_string(a)); 1086 } 1087 if (archive_match_path_excluded(cpio->matching, entry)) 1088 continue; 1089 if (cpio->verbose) 1090 list_item_verbose(cpio, entry); 1091 else 1092 fprintf(stdout, "%s\n", archive_entry_pathname(entry)); 1093 } 1094 r = archive_read_close(a); 1095 if (r != ARCHIVE_OK) 1096 lafe_errc(1, 0, "%s", archive_error_string(a)); 1097 if (!cpio->quiet) { 1098 int64_t blocks = (archive_filter_bytes(a, 0) + 511) 1099 / 512; 1100 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 1101 blocks == 1 ? "block" : "blocks"); 1102 } 1103 archive_read_free(a); 1104 exit(0); 1105 } 1106 1107 /* 1108 * Display information about the current file. 1109 * 1110 * The format here roughly duplicates the output of 'ls -l'. 1111 * This is based on SUSv2, where 'tar tv' is documented as 1112 * listing additional information in an "unspecified format," 1113 * and 'pax -l' is documented as using the same format as 'ls -l'. 1114 */ 1115 static void 1116 list_item_verbose(struct cpio *cpio, struct archive_entry *entry) 1117 { 1118 char size[32]; 1119 char date[32]; 1120 char uids[16], gids[16]; 1121 const char *uname, *gname; 1122 FILE *out = stdout; 1123 const char *fmt; 1124 time_t mtime; 1125 static time_t now; 1126 1127 if (!now) 1128 time(&now); 1129 1130 if (cpio->option_numeric_uid_gid) { 1131 /* Format numeric uid/gid for display. */ 1132 strcpy(uids, cpio_i64toa(archive_entry_uid(entry))); 1133 uname = uids; 1134 strcpy(gids, cpio_i64toa(archive_entry_gid(entry))); 1135 gname = gids; 1136 } else { 1137 /* Use uname if it's present, else lookup name from uid. */ 1138 uname = archive_entry_uname(entry); 1139 if (uname == NULL) 1140 uname = lookup_uname(cpio, (uid_t)archive_entry_uid(entry)); 1141 /* Use gname if it's present, else lookup name from gid. */ 1142 gname = archive_entry_gname(entry); 1143 if (gname == NULL) 1144 gname = lookup_gname(cpio, (uid_t)archive_entry_gid(entry)); 1145 } 1146 1147 /* Print device number or file size. */ 1148 if (archive_entry_filetype(entry) == AE_IFCHR 1149 || archive_entry_filetype(entry) == AE_IFBLK) { 1150 snprintf(size, sizeof(size), "%lu,%lu", 1151 (unsigned long)archive_entry_rdevmajor(entry), 1152 (unsigned long)archive_entry_rdevminor(entry)); 1153 } else { 1154 strcpy(size, cpio_i64toa(archive_entry_size(entry))); 1155 } 1156 1157 /* Format the time using 'ls -l' conventions. */ 1158 mtime = archive_entry_mtime(entry); 1159 #if defined(_WIN32) && !defined(__CYGWIN__) 1160 /* Windows' strftime function does not support %e format. */ 1161 if (mtime - now > 365*86400/2 1162 || mtime - now < -365*86400/2) 1163 fmt = cpio->day_first ? "%d %b %Y" : "%b %d %Y"; 1164 else 1165 fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M"; 1166 #else 1167 if (mtime - now > 365*86400/2 1168 || mtime - now < -365*86400/2) 1169 fmt = cpio->day_first ? "%e %b %Y" : "%b %e %Y"; 1170 else 1171 fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M"; 1172 #endif 1173 strftime(date, sizeof(date), fmt, localtime(&mtime)); 1174 1175 fprintf(out, "%s%3d %-8s %-8s %8s %12s %s", 1176 archive_entry_strmode(entry), 1177 archive_entry_nlink(entry), 1178 uname, gname, size, date, 1179 archive_entry_pathname(entry)); 1180 1181 /* Extra information for links. */ 1182 if (archive_entry_hardlink(entry)) /* Hard link */ 1183 fprintf(out, " link to %s", archive_entry_hardlink(entry)); 1184 else if (archive_entry_symlink(entry)) /* Symbolic link */ 1185 fprintf(out, " -> %s", archive_entry_symlink(entry)); 1186 fprintf(out, "\n"); 1187 } 1188 1189 static void 1190 mode_pass(struct cpio *cpio, const char *destdir) 1191 { 1192 struct lafe_line_reader *lr; 1193 const char *p; 1194 int r; 1195 1196 /* Ensure target dir has a trailing '/' to simplify path surgery. */ 1197 cpio->destdir = malloc(strlen(destdir) + 8); 1198 strcpy(cpio->destdir, destdir); 1199 if (destdir[strlen(destdir) - 1] != '/') 1200 strcat(cpio->destdir, "/"); 1201 1202 cpio->archive = archive_write_disk_new(); 1203 if (cpio->archive == NULL) 1204 lafe_errc(1, 0, "Failed to allocate archive object"); 1205 r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags); 1206 if (r != ARCHIVE_OK) 1207 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 1208 cpio->linkresolver = archive_entry_linkresolver_new(); 1209 archive_write_disk_set_standard_lookup(cpio->archive); 1210 1211 cpio->archive_read_disk = archive_read_disk_new(); 1212 if (cpio->archive_read_disk == NULL) 1213 lafe_errc(1, 0, "Failed to allocate archive object"); 1214 if (cpio->option_follow_links) 1215 archive_read_disk_set_symlink_logical(cpio->archive_read_disk); 1216 else 1217 archive_read_disk_set_symlink_physical(cpio->archive_read_disk); 1218 archive_read_disk_set_standard_lookup(cpio->archive_read_disk); 1219 1220 lr = lafe_line_reader("-", cpio->option_null); 1221 while ((p = lafe_line_reader_next(lr)) != NULL) 1222 file_to_archive(cpio, p); 1223 lafe_line_reader_free(lr); 1224 1225 archive_entry_linkresolver_free(cpio->linkresolver); 1226 r = archive_write_close(cpio->archive); 1227 if (cpio->dot) 1228 fprintf(stderr, "\n"); 1229 if (r != ARCHIVE_OK) 1230 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 1231 1232 if (!cpio->quiet) { 1233 int64_t blocks = 1234 (archive_filter_bytes(cpio->archive, 0) + 511) 1235 / 512; 1236 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 1237 blocks == 1 ? "block" : "blocks"); 1238 } 1239 1240 archive_write_free(cpio->archive); 1241 } 1242 1243 /* 1244 * Prompt for a new name for this entry. Returns a pointer to the 1245 * new name or NULL if the entry should not be copied. This 1246 * implements the semantics defined in POSIX.1-1996, which specifies 1247 * that an input of '.' means the name should be unchanged. GNU cpio 1248 * treats '.' as a literal new name. 1249 */ 1250 static const char * 1251 cpio_rename(const char *name) 1252 { 1253 static char buff[1024]; 1254 FILE *t; 1255 char *p, *ret; 1256 #if defined(_WIN32) && !defined(__CYGWIN__) 1257 FILE *to; 1258 1259 t = fopen("CONIN$", "r"); 1260 if (t == NULL) 1261 return (name); 1262 to = fopen("CONOUT$", "w"); 1263 if (to == NULL) { 1264 fclose(t); 1265 return (name); 1266 } 1267 fprintf(to, "%s (Enter/./(new name))? ", name); 1268 fclose(to); 1269 #else 1270 t = fopen("/dev/tty", "r+"); 1271 if (t == NULL) 1272 return (name); 1273 fprintf(t, "%s (Enter/./(new name))? ", name); 1274 fflush(t); 1275 #endif 1276 1277 p = fgets(buff, sizeof(buff), t); 1278 fclose(t); 1279 if (p == NULL) 1280 /* End-of-file is a blank line. */ 1281 return (NULL); 1282 1283 while (*p == ' ' || *p == '\t') 1284 ++p; 1285 if (*p == '\n' || *p == '\0') 1286 /* Empty line. */ 1287 return (NULL); 1288 if (*p == '.' && p[1] == '\n') 1289 /* Single period preserves original name. */ 1290 return (name); 1291 ret = p; 1292 /* Trim the final newline. */ 1293 while (*p != '\0' && *p != '\n') 1294 ++p; 1295 /* Overwrite the final \n with a null character. */ 1296 *p = '\0'; 1297 return (ret); 1298 } 1299 1300 static void 1301 free_cache(struct name_cache *cache) 1302 { 1303 size_t i; 1304 1305 if (cache != NULL) { 1306 for (i = 0; i < cache->size; i++) 1307 free(cache->cache[i].name); 1308 free(cache); 1309 } 1310 } 1311 1312 /* 1313 * Lookup uname/gname from uid/gid, return NULL if no match. 1314 */ 1315 static const char * 1316 lookup_name(struct cpio *cpio, struct name_cache **name_cache_variable, 1317 int (*lookup_fn)(struct cpio *, const char **, id_t), id_t id) 1318 { 1319 char asnum[16]; 1320 struct name_cache *cache; 1321 const char *name; 1322 int slot; 1323 1324 1325 if (*name_cache_variable == NULL) { 1326 *name_cache_variable = malloc(sizeof(struct name_cache)); 1327 if (*name_cache_variable == NULL) 1328 lafe_errc(1, ENOMEM, "No more memory"); 1329 memset(*name_cache_variable, 0, sizeof(struct name_cache)); 1330 (*name_cache_variable)->size = name_cache_size; 1331 } 1332 1333 cache = *name_cache_variable; 1334 cache->probes++; 1335 1336 slot = id % cache->size; 1337 if (cache->cache[slot].name != NULL) { 1338 if (cache->cache[slot].id == id) { 1339 cache->hits++; 1340 return (cache->cache[slot].name); 1341 } 1342 free(cache->cache[slot].name); 1343 cache->cache[slot].name = NULL; 1344 } 1345 1346 if (lookup_fn(cpio, &name, id) == 0) { 1347 if (name == NULL || name[0] == '\0') { 1348 /* If lookup failed, format it as a number. */ 1349 snprintf(asnum, sizeof(asnum), "%u", (unsigned)id); 1350 name = asnum; 1351 } 1352 cache->cache[slot].name = strdup(name); 1353 if (cache->cache[slot].name != NULL) { 1354 cache->cache[slot].id = id; 1355 return (cache->cache[slot].name); 1356 } 1357 /* 1358 * Conveniently, NULL marks an empty slot, so 1359 * if the strdup() fails, we've just failed to 1360 * cache it. No recovery necessary. 1361 */ 1362 } 1363 return (NULL); 1364 } 1365 1366 static const char * 1367 lookup_uname(struct cpio *cpio, uid_t uid) 1368 { 1369 return (lookup_name(cpio, &cpio->uname_cache, 1370 &lookup_uname_helper, (id_t)uid)); 1371 } 1372 1373 static int 1374 lookup_uname_helper(struct cpio *cpio, const char **name, id_t id) 1375 { 1376 struct passwd *pwent; 1377 1378 (void)cpio; /* UNUSED */ 1379 1380 errno = 0; 1381 pwent = getpwuid((uid_t)id); 1382 if (pwent == NULL) { 1383 *name = NULL; 1384 if (errno != 0 && errno != ENOENT) 1385 lafe_warnc(errno, "getpwuid(%s) failed", 1386 cpio_i64toa((int64_t)id)); 1387 return (errno); 1388 } 1389 1390 *name = pwent->pw_name; 1391 return (0); 1392 } 1393 1394 static const char * 1395 lookup_gname(struct cpio *cpio, gid_t gid) 1396 { 1397 return (lookup_name(cpio, &cpio->gname_cache, 1398 &lookup_gname_helper, (id_t)gid)); 1399 } 1400 1401 static int 1402 lookup_gname_helper(struct cpio *cpio, const char **name, id_t id) 1403 { 1404 struct group *grent; 1405 1406 (void)cpio; /* UNUSED */ 1407 1408 errno = 0; 1409 grent = getgrgid((gid_t)id); 1410 if (grent == NULL) { 1411 *name = NULL; 1412 if (errno != 0) 1413 lafe_warnc(errno, "getgrgid(%s) failed", 1414 cpio_i64toa((int64_t)id)); 1415 return (errno); 1416 } 1417 1418 *name = grent->gr_name; 1419 return (0); 1420 } 1421 1422 /* 1423 * It would be nice to just use printf() for formatting large numbers, 1424 * but the compatibility problems are a big headache. Hence the 1425 * following simple utility function. 1426 */ 1427 const char * 1428 cpio_i64toa(int64_t n0) 1429 { 1430 /* 2^64 =~ 1.8 * 10^19, so 20 decimal digits suffice. 1431 * We also need 1 byte for '-' and 1 for '\0'. 1432 */ 1433 static char buff[22]; 1434 int64_t n = n0 < 0 ? -n0 : n0; 1435 char *p = buff + sizeof(buff); 1436 1437 *--p = '\0'; 1438 do { 1439 *--p = '0' + (int)(n % 10); 1440 n /= 10; 1441 } while (n > 0); 1442 if (n0 < 0) 1443 *--p = '-'; 1444 return p; 1445 } 1446 1447 #define PPBUFF_SIZE 1024 1448 static const char * 1449 passphrase_callback(struct archive *a, void *_client_data) 1450 { 1451 struct cpio *cpio = (struct cpio *)_client_data; 1452 (void)a; /* UNUSED */ 1453 1454 if (cpio->ppbuff == NULL) { 1455 cpio->ppbuff = malloc(PPBUFF_SIZE); 1456 if (cpio->ppbuff == NULL) 1457 lafe_errc(1, errno, "Out of memory"); 1458 } 1459 return lafe_readpassphrase("Enter passphrase:", 1460 cpio->ppbuff, PPBUFF_SIZE); 1461 } 1462 1463 static void 1464 passphrase_free(char *ppbuff) 1465 { 1466 if (ppbuff != NULL) { 1467 memset(ppbuff, 0, PPBUFF_SIZE); 1468 free(ppbuff); 1469 } 1470 } 1471