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