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