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 30 #include <sys/types.h> 31 #include <archive.h> 32 #include <archive_entry.h> 33 34 #ifdef HAVE_SYS_MKDEV_H 35 #include <sys/mkdev.h> 36 #endif 37 #ifdef HAVE_SYS_STAT_H 38 #include <sys/stat.h> 39 #endif 40 #ifdef HAVE_SYS_TIME_H 41 #include <sys/time.h> 42 #endif 43 #ifdef HAVE_ERRNO_H 44 #include <errno.h> 45 #endif 46 #ifdef HAVE_FCNTL_H 47 #include <fcntl.h> 48 #endif 49 #ifdef HAVE_GRP_H 50 #include <grp.h> 51 #endif 52 #ifdef HAVE_LOCALE_H 53 #include <locale.h> 54 #endif 55 #ifdef HAVE_PWD_H 56 #include <pwd.h> 57 #endif 58 #ifdef HAVE_SIGNAL_H 59 #include <signal.h> 60 #endif 61 #ifdef HAVE_STDARG_H 62 #include <stdarg.h> 63 #endif 64 #ifdef HAVE_STDINT_H 65 #include <stdint.h> 66 #endif 67 #include <stdio.h> 68 #ifdef HAVE_STDLIB_H 69 #include <stdlib.h> 70 #endif 71 #ifdef HAVE_STRING_H 72 #include <string.h> 73 #endif 74 #ifdef HAVE_UNISTD_H 75 #include <unistd.h> 76 #endif 77 #ifdef HAVE_TIME_H 78 #include <time.h> 79 #endif 80 81 #include "cpio.h" 82 #include "err.h" 83 #include "line_reader.h" 84 #include "passphrase.h" 85 86 /* Fixed size of uname/gname caches. */ 87 #define name_cache_size 101 88 89 #ifndef O_BINARY 90 #define O_BINARY 0 91 #endif 92 93 struct name_cache { 94 int probes; 95 int hits; 96 size_t size; 97 struct { 98 id_t id; 99 char *name; 100 } cache[name_cache_size]; 101 }; 102 103 static int extract_data(struct archive *, struct archive *); 104 const char * cpio_i64toa(int64_t); 105 static const char *cpio_rename(const char *name); 106 static int entry_to_archive(struct cpio *, struct archive_entry *); 107 static int file_to_archive(struct cpio *, const char *); 108 static void free_cache(struct name_cache *cache); 109 static void list_item_verbose(struct cpio *, struct archive_entry *); 110 static __LA_NORETURN void long_help(void); 111 static const char *lookup_gname(struct cpio *, gid_t gid); 112 static int lookup_gname_helper(struct cpio *, 113 const char **name, id_t gid); 114 static const char *lookup_uname(struct cpio *, uid_t uid); 115 static int lookup_uname_helper(struct cpio *, 116 const char **name, id_t uid); 117 static __LA_NORETURN void mode_in(struct cpio *); 118 static __LA_NORETURN void mode_list(struct cpio *); 119 static void mode_out(struct cpio *); 120 static void mode_pass(struct cpio *, const char *); 121 static const char *remove_leading_slash(const char *); 122 static int restore_time(struct cpio *, struct archive_entry *, 123 const char *, int fd); 124 static __LA_NORETURN void usage(void); 125 static __LA_NORETURN void version(void); 126 static const char * passphrase_callback(struct archive *, void *); 127 static void passphrase_free(char *); 128 129 int 130 main(int argc, char *argv[]) 131 { 132 static char buff[16384]; 133 struct cpio _cpio; /* Allocated on stack. */ 134 struct cpio *cpio; 135 const char *errmsg; 136 char *tptr; 137 int uid, gid; 138 int opt, t; 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 '6': /* in/out: assume/create 6th edition (PWB) format */ 195 cpio->option_pwb = 1; 196 break; 197 case '7': /* out: create archive using 7th Edition binary format */ 198 cpio->format = "bin"; 199 break; 200 case 'A': /* NetBSD/OpenBSD */ 201 cpio->option_append = 1; 202 break; 203 case 'a': /* POSIX 1997 */ 204 cpio->option_atime_restore = 1; 205 break; 206 case 'B': /* POSIX 1997 */ 207 cpio->bytes_per_block = 5120; 208 break; 209 case OPTION_B64ENCODE: 210 cpio->add_filter = opt; 211 break; 212 case 'C': /* NetBSD/OpenBSD */ 213 errno = 0; 214 tptr = NULL; 215 t = (int)strtol(cpio->argument, &tptr, 10); 216 if (errno || t <= 0 || *(cpio->argument) == '\0' || 217 tptr == NULL || *tptr != '\0') { 218 lafe_errc(1, 0, "Invalid blocksize: %s", 219 cpio->argument); 220 } 221 cpio->bytes_per_block = t; 222 break; 223 case 'c': /* POSIX 1997 */ 224 cpio->format = "odc"; 225 break; 226 case 'd': /* POSIX 1997 */ 227 cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR; 228 break; 229 case 'E': /* NetBSD/OpenBSD */ 230 if (archive_match_include_pattern_from_file( 231 cpio->matching, cpio->argument, 232 cpio->option_null) != ARCHIVE_OK) 233 lafe_errc(1, 0, "Error : %s", 234 archive_error_string(cpio->matching)); 235 break; 236 case 'F': /* NetBSD/OpenBSD/GNU cpio */ 237 cpio->filename = cpio->argument; 238 break; 239 case 'f': /* POSIX 1997 */ 240 if (archive_match_exclude_pattern(cpio->matching, 241 cpio->argument) != ARCHIVE_OK) 242 lafe_errc(1, 0, "Error : %s", 243 archive_error_string(cpio->matching)); 244 break; 245 case OPTION_GRZIP: 246 cpio->compress = opt; 247 break; 248 case 'H': /* GNU cpio (also --format) */ 249 cpio->format = cpio->argument; 250 break; 251 case 'h': 252 long_help(); 253 /* NOTREACHED */ 254 case 'I': /* NetBSD/OpenBSD */ 255 cpio->filename = cpio->argument; 256 break; 257 case 'i': /* POSIX 1997 */ 258 if (cpio->mode != '\0') 259 lafe_errc(1, 0, 260 "Cannot use both -i and -%c", cpio->mode); 261 cpio->mode = opt; 262 break; 263 case 'J': /* GNU tar, others */ 264 cpio->compress = opt; 265 break; 266 case 'j': /* GNU tar, others */ 267 cpio->compress = opt; 268 break; 269 case OPTION_INSECURE: 270 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS; 271 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; 272 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS; 273 break; 274 case 'L': /* GNU cpio */ 275 cpio->option_follow_links = 1; 276 break; 277 case 'l': /* POSIX 1997 */ 278 cpio->option_link = 1; 279 break; 280 case OPTION_LRZIP: 281 case OPTION_LZ4: 282 case OPTION_LZMA: /* GNU tar, others */ 283 case OPTION_LZOP: /* GNU tar, others */ 284 case OPTION_ZSTD: 285 cpio->compress = opt; 286 break; 287 case 'm': /* POSIX 1997 */ 288 cpio->extract_flags |= ARCHIVE_EXTRACT_TIME; 289 break; 290 case 'n': /* GNU cpio */ 291 cpio->option_numeric_uid_gid = 1; 292 break; 293 case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */ 294 cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; 295 break; 296 case 'O': /* GNU cpio */ 297 cpio->filename = cpio->argument; 298 break; 299 case 'o': /* POSIX 1997 */ 300 if (cpio->mode != '\0') 301 lafe_errc(1, 0, 302 "Cannot use both -o and -%c", cpio->mode); 303 cpio->mode = opt; 304 break; 305 case 'p': /* POSIX 1997 */ 306 if (cpio->mode != '\0') 307 lafe_errc(1, 0, 308 "Cannot use both -p and -%c", cpio->mode); 309 cpio->mode = opt; 310 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; 311 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS; 312 break; 313 case OPTION_PASSPHRASE: 314 cpio->passphrase = cpio->argument; 315 break; 316 case OPTION_PRESERVE_OWNER: 317 cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; 318 break; 319 case OPTION_QUIET: /* GNU cpio */ 320 cpio->quiet = 1; 321 break; 322 case 'R': /* GNU cpio, also --owner */ 323 /* TODO: owner_parse should return uname/gname 324 * also; use that to set [ug]name_override. */ 325 errmsg = owner_parse(cpio->argument, &uid, &gid); 326 if (errmsg) { 327 lafe_warnc(-1, "%s", errmsg); 328 usage(); 329 } 330 if (uid != -1) { 331 cpio->uid_override = uid; 332 cpio->uname_override = NULL; 333 } 334 if (gid != -1) { 335 cpio->gid_override = gid; 336 cpio->gname_override = NULL; 337 } 338 break; 339 case 'r': /* POSIX 1997 */ 340 cpio->option_rename = 1; 341 break; 342 case 't': /* POSIX 1997 */ 343 cpio->option_list = 1; 344 break; 345 case 'u': /* POSIX 1997 */ 346 cpio->extract_flags 347 &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 348 break; 349 case OPTION_UUENCODE: 350 cpio->add_filter = opt; 351 break; 352 case 'v': /* POSIX 1997 */ 353 cpio->verbose++; 354 break; 355 case 'V': /* GNU cpio */ 356 cpio->dot++; 357 break; 358 case OPTION_VERSION: /* GNU convention */ 359 version(); 360 /* NOTREACHED */ 361 #if 0 362 /* 363 * cpio_getopt() handles -W specially, so it's not 364 * available here. 365 */ 366 case 'W': /* Obscure, but useful GNU convention. */ 367 break; 368 #endif 369 case 'y': /* tar convention */ 370 cpio->compress = opt; 371 break; 372 case 'Z': /* tar convention */ 373 cpio->compress = opt; 374 break; 375 case 'z': /* tar convention */ 376 cpio->compress = opt; 377 break; 378 default: 379 usage(); 380 } 381 } 382 383 /* 384 * Sanity-check args, error out on nonsensical combinations. 385 */ 386 /* -t implies -i if no mode was specified. */ 387 if (cpio->option_list && cpio->mode == '\0') 388 cpio->mode = 'i'; 389 /* -t requires -i */ 390 if (cpio->option_list && cpio->mode != 'i') 391 lafe_errc(1, 0, "Option -t requires -i"); 392 /* -n requires -it */ 393 if (cpio->option_numeric_uid_gid && !cpio->option_list) 394 lafe_errc(1, 0, "Option -n requires -it"); 395 /* Can only specify format when writing */ 396 if (cpio->format != NULL && cpio->mode != 'o') 397 lafe_errc(1, 0, "Option --format requires -o"); 398 /* -l requires -p */ 399 if (cpio->option_link && cpio->mode != 'p') 400 lafe_errc(1, 0, "Option -l requires -p"); 401 /* -v overrides -V */ 402 if (cpio->dot && cpio->verbose) 403 cpio->dot = 0; 404 /* TODO: Flag other nonsensical combinations. */ 405 406 switch (cpio->mode) { 407 case 'o': 408 if (cpio->format == NULL) { 409 if (cpio->option_pwb) 410 cpio->format = "pwb"; 411 else 412 cpio->format = "cpio"; 413 } 414 mode_out(cpio); 415 break; 416 case 'i': 417 while (*cpio->argv != NULL) { 418 if (archive_match_include_pattern(cpio->matching, 419 *cpio->argv) != ARCHIVE_OK) 420 lafe_errc(1, 0, "Error : %s", 421 archive_error_string(cpio->matching)); 422 --cpio->argc; 423 ++cpio->argv; 424 } 425 if (cpio->option_list) 426 mode_list(cpio); 427 else 428 mode_in(cpio); 429 /* NOTREACHED */ 430 case 'p': 431 if (*cpio->argv == NULL || **cpio->argv == '\0') 432 lafe_errc(1, 0, 433 "-p mode requires a target directory"); 434 mode_pass(cpio, *cpio->argv); 435 break; 436 default: 437 lafe_errc(1, 0, 438 "Must specify at least one of -i, -o, or -p"); 439 } 440 441 archive_match_free(cpio->matching); 442 free_cache(cpio->gname_cache); 443 free_cache(cpio->uname_cache); 444 archive_read_close(cpio->archive_read_disk); 445 archive_read_free(cpio->archive_read_disk); 446 free(cpio->destdir); 447 passphrase_free(cpio->ppbuff); 448 return (cpio->return_value); 449 } 450 451 static void 452 usage(void) 453 { 454 const char *p; 455 456 p = lafe_getprogname(); 457 458 fprintf(stderr, "Brief Usage:\n"); 459 fprintf(stderr, " List: %s -it < archive\n", p); 460 fprintf(stderr, " Extract: %s -i < archive\n", p); 461 fprintf(stderr, " Create: %s -o < filenames > archive\n", p); 462 fprintf(stderr, " Help: %s --help\n", p); 463 exit(1); 464 } 465 466 static const char *long_help_msg = 467 "First option must be a mode specifier:\n" 468 " -i Input -o Output -p Pass\n" 469 "Common Options:\n" 470 " -v Verbose filenames -V one dot per file\n" 471 "Create: %p -o [options] < [list of files] > [archive]\n" 472 " -J,-y,-z,--lzma Compress archive with xz/bzip2/gzip/lzma\n" 473 " --format {pwb|bin|odc|newc|ustar} Select archive format\n" 474 "List: %p -it < [archive]\n" 475 "Extract: %p -i [options] < [archive]\n"; 476 477 478 /* 479 * Note that the word 'bsdcpio' will always appear in the first line 480 * of output. 481 * 482 * In particular, /bin/sh scripts that need to test for the presence 483 * of bsdcpio can use the following template: 484 * 485 * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \ 486 * echo bsdcpio; else echo not bsdcpio; fi 487 */ 488 static void 489 long_help(void) 490 { 491 const char *prog; 492 const char *p; 493 494 prog = lafe_getprogname(); 495 496 fflush(stderr); 497 498 p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : ""; 499 printf("%s%s: manipulate archive files\n", prog, p); 500 501 for (p = long_help_msg; *p != '\0'; p++) { 502 if (*p == '%') { 503 if (p[1] == 'p') { 504 fputs(prog, stdout); 505 p++; 506 } else 507 putchar('%'); 508 } else 509 putchar(*p); 510 } 511 version(); 512 } 513 514 static void 515 version(void) 516 { 517 fprintf(stdout,"bsdcpio %s - %s \n", 518 BSDCPIO_VERSION_STRING, 519 archive_version_details()); 520 exit(0); 521 } 522 523 static void 524 mode_out(struct cpio *cpio) 525 { 526 struct archive_entry *entry, *spare; 527 struct lafe_line_reader *lr; 528 const char *p; 529 int r; 530 531 if (cpio->option_append) 532 lafe_errc(1, 0, "Append mode not yet supported."); 533 534 cpio->archive_read_disk = archive_read_disk_new(); 535 if (cpio->archive_read_disk == NULL) 536 lafe_errc(1, 0, "Failed to allocate archive object"); 537 if (cpio->option_follow_links) 538 archive_read_disk_set_symlink_logical(cpio->archive_read_disk); 539 else 540 archive_read_disk_set_symlink_physical(cpio->archive_read_disk); 541 archive_read_disk_set_standard_lookup(cpio->archive_read_disk); 542 543 cpio->archive = archive_write_new(); 544 if (cpio->archive == NULL) 545 lafe_errc(1, 0, "Failed to allocate archive object"); 546 switch (cpio->compress) { 547 case OPTION_GRZIP: 548 r = archive_write_add_filter_grzip(cpio->archive); 549 break; 550 case 'J': 551 r = archive_write_add_filter_xz(cpio->archive); 552 break; 553 case OPTION_LRZIP: 554 r = archive_write_add_filter_lrzip(cpio->archive); 555 break; 556 case OPTION_LZ4: 557 r = archive_write_add_filter_lz4(cpio->archive); 558 break; 559 case OPTION_LZMA: 560 r = archive_write_add_filter_lzma(cpio->archive); 561 break; 562 case OPTION_LZOP: 563 r = archive_write_add_filter_lzop(cpio->archive); 564 break; 565 case OPTION_ZSTD: 566 r = archive_write_add_filter_zstd(cpio->archive); 567 break; 568 case 'j': case 'y': 569 r = archive_write_add_filter_bzip2(cpio->archive); 570 break; 571 case 'z': 572 r = archive_write_add_filter_gzip(cpio->archive); 573 break; 574 case 'Z': 575 r = archive_write_add_filter_compress(cpio->archive); 576 break; 577 default: 578 r = archive_write_add_filter_none(cpio->archive); 579 break; 580 } 581 if (r < ARCHIVE_WARN) 582 lafe_errc(1, 0, "Requested compression not available"); 583 switch (cpio->add_filter) { 584 case 0: 585 r = ARCHIVE_OK; 586 break; 587 case OPTION_B64ENCODE: 588 r = archive_write_add_filter_b64encode(cpio->archive); 589 break; 590 case OPTION_UUENCODE: 591 r = archive_write_add_filter_uuencode(cpio->archive); 592 break; 593 } 594 if (r < ARCHIVE_WARN) 595 lafe_errc(1, 0, "Requested filter not available"); 596 r = archive_write_set_format_by_name(cpio->archive, cpio->format); 597 if (r != ARCHIVE_OK) 598 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 599 archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block); 600 cpio->linkresolver = archive_entry_linkresolver_new(); 601 archive_entry_linkresolver_set_strategy(cpio->linkresolver, 602 archive_format(cpio->archive)); 603 if (cpio->passphrase != NULL) 604 r = archive_write_set_passphrase(cpio->archive, 605 cpio->passphrase); 606 else 607 r = archive_write_set_passphrase_callback(cpio->archive, cpio, 608 &passphrase_callback); 609 if (r != ARCHIVE_OK) 610 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 611 612 /* 613 * The main loop: Copy each file into the output archive. 614 */ 615 r = archive_write_open_filename(cpio->archive, cpio->filename); 616 if (r != ARCHIVE_OK) 617 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 618 lr = lafe_line_reader("-", cpio->option_null); 619 while ((p = lafe_line_reader_next(lr)) != NULL) 620 file_to_archive(cpio, p); 621 lafe_line_reader_free(lr); 622 623 /* 624 * The hardlink detection may have queued up a couple of entries 625 * that can now be flushed. 626 */ 627 entry = NULL; 628 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 629 while (entry != NULL) { 630 entry_to_archive(cpio, entry); 631 archive_entry_free(entry); 632 entry = NULL; 633 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 634 } 635 636 r = archive_write_close(cpio->archive); 637 if (cpio->dot) 638 fprintf(stderr, "\n"); 639 if (r != ARCHIVE_OK) 640 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 641 642 if (!cpio->quiet) { 643 int64_t blocks = 644 (archive_filter_bytes(cpio->archive, 0) + 511) 645 / 512; 646 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 647 blocks == 1 ? "block" : "blocks"); 648 } 649 archive_write_free(cpio->archive); 650 archive_entry_linkresolver_free(cpio->linkresolver); 651 } 652 653 static const char * 654 remove_leading_slash(const char *p) 655 { 656 const char *rp; 657 658 /* Remove leading "//./" or "//?/" or "//?/UNC/" 659 * (absolute path prefixes used by Windows API) */ 660 if ((p[0] == '/' || p[0] == '\\') && 661 (p[1] == '/' || p[1] == '\\') && 662 (p[2] == '.' || p[2] == '?') && 663 (p[3] == '/' || p[3] == '\\')) 664 { 665 if (p[2] == '?' && 666 (p[4] == 'U' || p[4] == 'u') && 667 (p[5] == 'N' || p[5] == 'n') && 668 (p[6] == 'C' || p[6] == 'c') && 669 (p[7] == '/' || p[7] == '\\')) 670 p += 8; 671 else 672 p += 4; 673 } 674 do { 675 rp = p; 676 /* Remove leading drive letter from archives created 677 * on Windows. */ 678 if (((p[0] >= 'a' && p[0] <= 'z') || 679 (p[0] >= 'A' && p[0] <= 'Z')) && 680 p[1] == ':') { 681 p += 2; 682 } 683 /* Remove leading "/../", "//", etc. */ 684 while (p[0] == '/' || p[0] == '\\') { 685 if (p[1] == '.' && p[2] == '.' && 686 (p[3] == '/' || p[3] == '\\')) { 687 p += 3; /* Remove "/..", leave "/" 688 * for next pass. */ 689 } else 690 p += 1; /* Remove "/". */ 691 } 692 } while (rp != p); 693 return (p); 694 } 695 696 /* 697 * This is used by both out mode (to copy objects from disk into 698 * an archive) and pass mode (to copy objects from disk to 699 * an archive_write_disk "archive"). 700 */ 701 static int 702 file_to_archive(struct cpio *cpio, const char *srcpath) 703 { 704 const char *destpath; 705 struct archive_entry *entry, *spare; 706 size_t len; 707 int r; 708 709 /* 710 * Create an archive_entry describing the source file. 711 * 712 */ 713 entry = archive_entry_new(); 714 if (entry == NULL) 715 lafe_errc(1, 0, "Couldn't allocate entry"); 716 archive_entry_copy_sourcepath(entry, srcpath); 717 r = archive_read_disk_entry_from_file(cpio->archive_read_disk, 718 entry, -1, NULL); 719 if (r < ARCHIVE_FAILED) 720 lafe_errc(1, 0, "%s", 721 archive_error_string(cpio->archive_read_disk)); 722 if (r < ARCHIVE_OK) 723 lafe_warnc(0, "%s", 724 archive_error_string(cpio->archive_read_disk)); 725 if (r <= ARCHIVE_FAILED) { 726 archive_entry_free(entry); 727 cpio->return_value = 1; 728 return (r); 729 } 730 731 if (cpio->uid_override >= 0) { 732 archive_entry_set_uid(entry, cpio->uid_override); 733 archive_entry_set_uname(entry, cpio->uname_override); 734 } 735 if (cpio->gid_override >= 0) { 736 archive_entry_set_gid(entry, cpio->gid_override); 737 archive_entry_set_gname(entry, cpio->gname_override); 738 } 739 740 /* 741 * Generate a destination path for this entry. 742 * "destination path" is the name to which it will be copied in 743 * pass mode or the name that will go into the archive in 744 * output mode. 745 */ 746 destpath = srcpath; 747 if (cpio->destdir) { 748 len = cpio->destdir_len + strlen(srcpath) + 8; 749 if (len >= cpio->pass_destpath_alloc) { 750 while (len >= cpio->pass_destpath_alloc) { 751 cpio->pass_destpath_alloc += 512; 752 cpio->pass_destpath_alloc *= 2; 753 } 754 free(cpio->pass_destpath); 755 cpio->pass_destpath = malloc(cpio->pass_destpath_alloc); 756 if (cpio->pass_destpath == NULL) 757 lafe_errc(1, ENOMEM, 758 "Can't allocate path buffer"); 759 } 760 strcpy(cpio->pass_destpath, cpio->destdir); 761 strcat(cpio->pass_destpath, remove_leading_slash(srcpath)); 762 destpath = cpio->pass_destpath; 763 } 764 if (cpio->option_rename) 765 destpath = cpio_rename(destpath); 766 if (destpath == NULL) { 767 archive_entry_free(entry); 768 return (0); 769 } 770 archive_entry_copy_pathname(entry, destpath); 771 772 /* 773 * If we're trying to preserve hardlinks, match them here. 774 */ 775 spare = NULL; 776 if (cpio->linkresolver != NULL 777 && archive_entry_filetype(entry) != AE_IFDIR) { 778 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 779 } 780 781 if (entry != NULL) { 782 r = entry_to_archive(cpio, entry); 783 archive_entry_free(entry); 784 if (spare != NULL) { 785 if (r == 0) 786 r = entry_to_archive(cpio, spare); 787 archive_entry_free(spare); 788 } 789 } 790 return (r); 791 } 792 793 static int 794 entry_to_archive(struct cpio *cpio, struct archive_entry *entry) 795 { 796 const char *destpath = archive_entry_pathname(entry); 797 const char *srcpath = archive_entry_sourcepath(entry); 798 int fd = -1; 799 ssize_t bytes_read; 800 int r; 801 802 /* Print out the destination name to the user. */ 803 if (cpio->verbose) 804 fprintf(stderr,"%s", destpath); 805 if (cpio->dot) 806 fprintf(stderr, "."); 807 808 /* 809 * Option_link only makes sense in pass mode and for 810 * regular files. Also note: if a link operation fails 811 * because of cross-device restrictions, we'll fall back 812 * to copy mode for that entry. 813 * 814 * TODO: Test other cpio implementations to see if they 815 * hard-link anything other than regular files here. 816 */ 817 if (cpio->option_link 818 && archive_entry_filetype(entry) == AE_IFREG) 819 { 820 struct archive_entry *t; 821 /* Save the original entry in case we need it later. */ 822 t = archive_entry_clone(entry); 823 if (t == NULL) 824 lafe_errc(1, ENOMEM, "Can't create link"); 825 /* Note: link(2) doesn't create parent directories, 826 * so we use archive_write_header() instead as a 827 * convenience. */ 828 archive_entry_set_hardlink(t, srcpath); 829 /* This is a straight link that carries no data. */ 830 archive_entry_set_size(t, 0); 831 r = archive_write_header(cpio->archive, t); 832 archive_entry_free(t); 833 if (r != ARCHIVE_OK) 834 lafe_warnc(archive_errno(cpio->archive), 835 "%s", archive_error_string(cpio->archive)); 836 if (r == ARCHIVE_FATAL) 837 exit(1); 838 #ifdef EXDEV 839 if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) { 840 /* Cross-device link: Just fall through and use 841 * the original entry to copy the file over. */ 842 lafe_warnc(0, "Copying file instead"); 843 } else 844 #endif 845 return (0); 846 } 847 848 /* 849 * Make sure we can open the file (if necessary) before 850 * trying to write the header. 851 */ 852 if (archive_entry_filetype(entry) == AE_IFREG) { 853 if (archive_entry_size(entry) > 0) { 854 fd = open(srcpath, O_RDONLY | O_BINARY); 855 if (fd < 0) { 856 lafe_warnc(errno, 857 "%s: could not open file", srcpath); 858 goto cleanup; 859 } 860 } 861 } else { 862 archive_entry_set_size(entry, 0); 863 } 864 865 r = archive_write_header(cpio->archive, entry); 866 867 if (r != ARCHIVE_OK) 868 lafe_warnc(archive_errno(cpio->archive), 869 "%s: %s", 870 srcpath, 871 archive_error_string(cpio->archive)); 872 873 if (r == ARCHIVE_FATAL) 874 exit(1); 875 876 if (r >= ARCHIVE_WARN && archive_entry_size(entry) > 0 && fd >= 0) { 877 bytes_read = read(fd, cpio->buff, (unsigned)cpio->buff_size); 878 while (bytes_read > 0) { 879 ssize_t bytes_write; 880 bytes_write = archive_write_data(cpio->archive, 881 cpio->buff, bytes_read); 882 if (bytes_write < 0) 883 lafe_errc(1, archive_errno(cpio->archive), 884 "%s", archive_error_string(cpio->archive)); 885 if (bytes_write < bytes_read) { 886 lafe_warnc(0, 887 "Truncated write; file may have " 888 "grown while being archived."); 889 } 890 bytes_read = read(fd, cpio->buff, 891 (unsigned)cpio->buff_size); 892 } 893 } 894 895 fd = restore_time(cpio, entry, srcpath, fd); 896 897 cleanup: 898 if (cpio->verbose) 899 fprintf(stderr,"\n"); 900 if (fd >= 0) 901 close(fd); 902 return (0); 903 } 904 905 static int 906 restore_time(struct cpio *cpio, struct archive_entry *entry, 907 const char *name, int fd) 908 { 909 #ifndef HAVE_UTIMES 910 static int warned = 0; 911 912 (void)cpio; /* UNUSED */ 913 (void)entry; /* UNUSED */ 914 (void)name; /* UNUSED */ 915 916 if (!warned) 917 lafe_warnc(0, "Can't restore access times on this platform"); 918 warned = 1; 919 return (fd); 920 #else 921 #if defined(_WIN32) && !defined(__CYGWIN__) 922 struct __timeval times[2]; 923 #else 924 struct timeval times[2]; 925 #endif 926 927 if (!cpio->option_atime_restore) 928 return (fd); 929 930 times[1].tv_sec = archive_entry_mtime(entry); 931 times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000; 932 933 times[0].tv_sec = archive_entry_atime(entry); 934 times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000; 935 936 #if defined(HAVE_FUTIMES) && !defined(__CYGWIN__) 937 if (fd >= 0 && futimes(fd, times) == 0) 938 return (fd); 939 #endif 940 /* 941 * Some platform cannot restore access times if the file descriptor 942 * is still opened. 943 */ 944 if (fd >= 0) { 945 close(fd); 946 fd = -1; 947 } 948 949 #ifdef HAVE_LUTIMES 950 if (lutimes(name, times) != 0) 951 #else 952 if ((AE_IFLNK != archive_entry_filetype(entry)) 953 && utimes(name, times) != 0) 954 #endif 955 lafe_warnc(errno, "Can't update time for %s", name); 956 #endif 957 return (fd); 958 } 959 960 961 static void 962 mode_in(struct cpio *cpio) 963 { 964 struct archive *a; 965 struct archive_entry *entry; 966 struct archive *ext; 967 const char *destpath; 968 int r; 969 970 ext = archive_write_disk_new(); 971 if (ext == NULL) 972 lafe_errc(1, 0, "Couldn't allocate restore object"); 973 r = archive_write_disk_set_options(ext, cpio->extract_flags); 974 if (r != ARCHIVE_OK) 975 lafe_errc(1, 0, "%s", archive_error_string(ext)); 976 a = archive_read_new(); 977 if (a == NULL) 978 lafe_errc(1, 0, "Couldn't allocate archive object"); 979 archive_read_support_filter_all(a); 980 archive_read_support_format_all(a); 981 if (cpio->option_pwb) 982 archive_read_set_options(a, "pwb"); 983 if (cpio->passphrase != NULL) 984 r = archive_read_add_passphrase(a, cpio->passphrase); 985 else 986 r = archive_read_set_passphrase_callback(a, cpio, 987 &passphrase_callback); 988 if (r != ARCHIVE_OK) 989 lafe_errc(1, 0, "%s", archive_error_string(a)); 990 991 if (archive_read_open_filename(a, cpio->filename, 992 cpio->bytes_per_block)) 993 lafe_errc(1, archive_errno(a), 994 "%s", archive_error_string(a)); 995 for (;;) { 996 r = archive_read_next_header(a, &entry); 997 if (r == ARCHIVE_EOF) 998 break; 999 if (r != ARCHIVE_OK) { 1000 lafe_errc(1, archive_errno(a), 1001 "%s", archive_error_string(a)); 1002 } 1003 if (archive_match_path_excluded(cpio->matching, entry)) 1004 continue; 1005 if (cpio->option_rename) { 1006 destpath = cpio_rename(archive_entry_pathname(entry)); 1007 archive_entry_set_pathname(entry, destpath); 1008 } else 1009 destpath = archive_entry_pathname(entry); 1010 if (destpath == NULL) 1011 continue; 1012 if (cpio->verbose) 1013 fprintf(stderr, "%s\n", destpath); 1014 if (cpio->dot) 1015 fprintf(stderr, "."); 1016 if (cpio->uid_override >= 0) 1017 archive_entry_set_uid(entry, cpio->uid_override); 1018 if (cpio->gid_override >= 0) 1019 archive_entry_set_gid(entry, cpio->gid_override); 1020 r = archive_write_header(ext, entry); 1021 if (r != ARCHIVE_OK) { 1022 fprintf(stderr, "%s: %s\n", 1023 archive_entry_pathname(entry), 1024 archive_error_string(ext)); 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%3d %-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