cpio.c revision 238856
1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer 10228753Smm * in this position and unchanged. 11228753Smm * 2. Redistributions in binary form must reproduce the above copyright 12228753Smm * notice, this list of conditions and the following disclaimer in the 13228753Smm * documentation and/or other materials provided with the distribution. 14228753Smm * 15228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25228753Smm */ 26228753Smm 27228753Smm 28228753Smm#include "cpio_platform.h" 29228763Smm__FBSDID("$FreeBSD: head/contrib/libarchive/cpio/cpio.c 238856 2012-07-28 06:38:44Z mm $"); 30228753Smm 31228753Smm#include <sys/types.h> 32228753Smm#include <archive.h> 33228753Smm#include <archive_entry.h> 34228753Smm 35228753Smm#ifdef HAVE_SYS_MKDEV_H 36228753Smm#include <sys/mkdev.h> 37228753Smm#endif 38228753Smm#ifdef HAVE_SYS_STAT_H 39228753Smm#include <sys/stat.h> 40228753Smm#endif 41228753Smm#ifdef HAVE_SYS_TIME_H 42228753Smm#include <sys/time.h> 43228753Smm#endif 44228753Smm#ifdef HAVE_ERRNO_H 45228753Smm#include <errno.h> 46228753Smm#endif 47228753Smm#ifdef HAVE_FCNTL_H 48228753Smm#include <fcntl.h> 49228753Smm#endif 50228753Smm#ifdef HAVE_GRP_H 51228753Smm#include <grp.h> 52228753Smm#endif 53232153Smm#ifdef HAVE_LOCALE_H 54232153Smm#include <locale.h> 55232153Smm#endif 56228753Smm#ifdef HAVE_PWD_H 57228753Smm#include <pwd.h> 58228753Smm#endif 59232153Smm#ifdef HAVE_SIGNAL_H 60232153Smm#include <signal.h> 61232153Smm#endif 62228753Smm#ifdef HAVE_STDARG_H 63228753Smm#include <stdarg.h> 64228753Smm#endif 65228753Smm#ifdef HAVE_STDINT_H 66228753Smm#include <stdint.h> 67228753Smm#endif 68228753Smm#include <stdio.h> 69228753Smm#ifdef HAVE_STDLIB_H 70228753Smm#include <stdlib.h> 71228753Smm#endif 72228753Smm#ifdef HAVE_STRING_H 73228753Smm#include <string.h> 74228753Smm#endif 75228753Smm#ifdef HAVE_UNISTD_H 76228753Smm#include <unistd.h> 77228753Smm#endif 78228753Smm#ifdef HAVE_TIME_H 79228753Smm#include <time.h> 80228753Smm#endif 81228753Smm 82228753Smm#include "cpio.h" 83228753Smm#include "err.h" 84228753Smm#include "line_reader.h" 85228753Smm 86228753Smm/* Fixed size of uname/gname caches. */ 87228753Smm#define name_cache_size 101 88228753Smm 89228753Smm#ifndef O_BINARY 90228753Smm#define O_BINARY 0 91228753Smm#endif 92228753Smm 93228753Smmstruct name_cache { 94228753Smm int probes; 95228753Smm int hits; 96228753Smm size_t size; 97228753Smm struct { 98228753Smm id_t id; 99228753Smm char *name; 100228753Smm } cache[name_cache_size]; 101228753Smm}; 102228753Smm 103228753Smmstatic int extract_data(struct archive *, struct archive *); 104228753Smmconst char * cpio_i64toa(int64_t); 105228753Smmstatic const char *cpio_rename(const char *name); 106228753Smmstatic int entry_to_archive(struct cpio *, struct archive_entry *); 107228753Smmstatic int file_to_archive(struct cpio *, const char *); 108228753Smmstatic void free_cache(struct name_cache *cache); 109228753Smmstatic void list_item_verbose(struct cpio *, struct archive_entry *); 110228753Smmstatic void long_help(void); 111228753Smmstatic const char *lookup_gname(struct cpio *, gid_t gid); 112228753Smmstatic int lookup_gname_helper(struct cpio *, 113228753Smm const char **name, id_t gid); 114228753Smmstatic const char *lookup_uname(struct cpio *, uid_t uid); 115228753Smmstatic int lookup_uname_helper(struct cpio *, 116228753Smm const char **name, id_t uid); 117228753Smmstatic void mode_in(struct cpio *); 118228753Smmstatic void mode_list(struct cpio *); 119228753Smmstatic void mode_out(struct cpio *); 120228753Smmstatic void mode_pass(struct cpio *, const char *); 121232153Smmstatic const char *remove_leading_slash(const char *); 122228753Smmstatic int restore_time(struct cpio *, struct archive_entry *, 123228753Smm const char *, int fd); 124228753Smmstatic void usage(void); 125228753Smmstatic void version(void); 126228753Smm 127228753Smmint 128228753Smmmain(int argc, char *argv[]) 129228753Smm{ 130228753Smm static char buff[16384]; 131228753Smm struct cpio _cpio; /* Allocated on stack. */ 132228753Smm struct cpio *cpio; 133228753Smm const char *errmsg; 134228753Smm int uid, gid; 135228753Smm int opt; 136228753Smm 137228753Smm cpio = &_cpio; 138228753Smm memset(cpio, 0, sizeof(*cpio)); 139228753Smm cpio->buff = buff; 140228753Smm cpio->buff_size = sizeof(buff); 141228753Smm 142232153Smm#if defined(HAVE_SIGACTION) && defined(SIGPIPE) 143232153Smm { /* Ignore SIGPIPE signals. */ 144232153Smm struct sigaction sa; 145232153Smm sigemptyset(&sa.sa_mask); 146232153Smm sa.sa_flags = 0; 147232153Smm sa.sa_handler = SIG_IGN; 148232153Smm sigaction(SIGPIPE, &sa, NULL); 149232153Smm } 150232153Smm#endif 151232153Smm 152228753Smm /* Need lafe_progname before calling lafe_warnc. */ 153228753Smm if (*argv == NULL) 154228753Smm lafe_progname = "bsdcpio"; 155228753Smm else { 156228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 157228753Smm lafe_progname = strrchr(*argv, '\\'); 158232153Smm if (strrchr(*argv, '/') > lafe_progname) 159232153Smm#endif 160228753Smm lafe_progname = strrchr(*argv, '/'); 161228753Smm if (lafe_progname != NULL) 162228753Smm lafe_progname++; 163228753Smm else 164228753Smm lafe_progname = *argv; 165228753Smm } 166232153Smm#if HAVE_SETLOCALE 167232153Smm if (setlocale(LC_ALL, "") == NULL) 168232153Smm lafe_warnc(0, "Failed to set default locale"); 169232153Smm#endif 170228753Smm 171228753Smm cpio->uid_override = -1; 172228753Smm cpio->gid_override = -1; 173228753Smm cpio->argv = argv; 174228753Smm cpio->argc = argc; 175228753Smm cpio->mode = '\0'; 176228753Smm cpio->verbose = 0; 177228753Smm cpio->compress = '\0'; 178228753Smm cpio->extract_flags = ARCHIVE_EXTRACT_NO_AUTODIR; 179228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 180228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS; 181228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT; 182228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_PERM; 183228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 184228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_ACL; 185228753Smm#if !defined(_WIN32) && !defined(__CYGWIN__) 186228753Smm if (geteuid() == 0) 187228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; 188228753Smm#endif 189228753Smm cpio->bytes_per_block = 512; 190228753Smm cpio->filename = NULL; 191228753Smm 192238856Smm cpio->matching = archive_match_new(); 193238856Smm if (cpio->matching == NULL) 194238856Smm lafe_errc(1, 0, "Out of memory"); 195238856Smm 196228753Smm while ((opt = cpio_getopt(cpio)) != -1) { 197228753Smm switch (opt) { 198228753Smm case '0': /* GNU convention: --null, -0 */ 199228753Smm cpio->option_null = 1; 200228753Smm break; 201228753Smm case 'A': /* NetBSD/OpenBSD */ 202228753Smm cpio->option_append = 1; 203228753Smm break; 204228753Smm case 'a': /* POSIX 1997 */ 205228753Smm cpio->option_atime_restore = 1; 206228753Smm break; 207228753Smm case 'B': /* POSIX 1997 */ 208228753Smm cpio->bytes_per_block = 5120; 209228753Smm break; 210228753Smm case 'C': /* NetBSD/OpenBSD */ 211232153Smm cpio->bytes_per_block = atoi(cpio->argument); 212228753Smm if (cpio->bytes_per_block <= 0) 213232153Smm lafe_errc(1, 0, "Invalid blocksize %s", cpio->argument); 214228753Smm break; 215228753Smm case 'c': /* POSIX 1997 */ 216228753Smm cpio->format = "odc"; 217228753Smm break; 218228753Smm case 'd': /* POSIX 1997 */ 219228753Smm cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR; 220228753Smm break; 221228753Smm case 'E': /* NetBSD/OpenBSD */ 222238856Smm if (archive_match_include_pattern_from_file( 223238856Smm cpio->matching, cpio->argument, 224238856Smm cpio->option_null) != ARCHIVE_OK) 225238856Smm lafe_errc(1, 0, "Error : %s", 226238856Smm archive_error_string(cpio->matching)); 227228753Smm break; 228228753Smm case 'F': /* NetBSD/OpenBSD/GNU cpio */ 229232153Smm cpio->filename = cpio->argument; 230228753Smm break; 231228753Smm case 'f': /* POSIX 1997 */ 232238856Smm if (archive_match_exclude_pattern(cpio->matching, 233238856Smm cpio->argument) != ARCHIVE_OK) 234238856Smm lafe_errc(1, 0, "Error : %s", 235238856Smm archive_error_string(cpio->matching)); 236228753Smm break; 237228753Smm case 'H': /* GNU cpio (also --format) */ 238232153Smm cpio->format = cpio->argument; 239228753Smm break; 240228753Smm case 'h': 241228753Smm long_help(); 242228753Smm break; 243228753Smm case 'I': /* NetBSD/OpenBSD */ 244232153Smm cpio->filename = cpio->argument; 245228753Smm break; 246228753Smm case 'i': /* POSIX 1997 */ 247228753Smm if (cpio->mode != '\0') 248228753Smm lafe_errc(1, 0, 249228753Smm "Cannot use both -i and -%c", cpio->mode); 250228753Smm cpio->mode = opt; 251228753Smm break; 252228753Smm case 'J': /* GNU tar, others */ 253228753Smm cpio->compress = opt; 254228753Smm break; 255228753Smm case 'j': /* GNU tar, others */ 256228753Smm cpio->compress = opt; 257228753Smm break; 258228753Smm case OPTION_INSECURE: 259228753Smm cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS; 260228753Smm cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; 261228753Smm break; 262228753Smm case 'L': /* GNU cpio */ 263228753Smm cpio->option_follow_links = 1; 264228753Smm break; 265228753Smm case 'l': /* POSIX 1997 */ 266228753Smm cpio->option_link = 1; 267228753Smm break; 268228753Smm case OPTION_LZMA: /* GNU tar, others */ 269228753Smm cpio->compress = opt; 270228753Smm break; 271228753Smm case 'm': /* POSIX 1997 */ 272228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_TIME; 273228753Smm break; 274228753Smm case 'n': /* GNU cpio */ 275228753Smm cpio->option_numeric_uid_gid = 1; 276228753Smm break; 277228753Smm case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */ 278228753Smm cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; 279228753Smm break; 280228753Smm case 'O': /* GNU cpio */ 281232153Smm cpio->filename = cpio->argument; 282228753Smm break; 283228753Smm case 'o': /* POSIX 1997 */ 284228753Smm if (cpio->mode != '\0') 285228753Smm lafe_errc(1, 0, 286228753Smm "Cannot use both -o and -%c", cpio->mode); 287228753Smm cpio->mode = opt; 288228753Smm break; 289228753Smm case 'p': /* POSIX 1997 */ 290228753Smm if (cpio->mode != '\0') 291228753Smm lafe_errc(1, 0, 292228753Smm "Cannot use both -p and -%c", cpio->mode); 293228753Smm cpio->mode = opt; 294228753Smm cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; 295228753Smm break; 296228753Smm case OPTION_PRESERVE_OWNER: 297228753Smm cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; 298228753Smm break; 299228753Smm case OPTION_QUIET: /* GNU cpio */ 300228753Smm cpio->quiet = 1; 301228753Smm break; 302228753Smm case 'R': /* GNU cpio, also --owner */ 303228777Smm /* TODO: owner_parse should return uname/gname 304228777Smm * also; use that to set [ug]name_override. */ 305232153Smm errmsg = owner_parse(cpio->argument, &uid, &gid); 306228753Smm if (errmsg) { 307228753Smm lafe_warnc(-1, "%s", errmsg); 308228753Smm usage(); 309228753Smm } 310228777Smm if (uid != -1) { 311228753Smm cpio->uid_override = uid; 312228777Smm cpio->uname_override = NULL; 313228777Smm } 314228777Smm if (gid != -1) { 315228753Smm cpio->gid_override = gid; 316228777Smm cpio->gname_override = NULL; 317228777Smm } 318228753Smm break; 319228753Smm case 'r': /* POSIX 1997 */ 320228753Smm cpio->option_rename = 1; 321228753Smm break; 322228753Smm case 't': /* POSIX 1997 */ 323228753Smm cpio->option_list = 1; 324228753Smm break; 325228753Smm case 'u': /* POSIX 1997 */ 326228753Smm cpio->extract_flags 327228753Smm &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 328228753Smm break; 329228753Smm case 'v': /* POSIX 1997 */ 330228753Smm cpio->verbose++; 331228753Smm break; 332232153Smm case 'V': /* GNU cpio */ 333232153Smm cpio->dot++; 334232153Smm break; 335228753Smm case OPTION_VERSION: /* GNU convention */ 336228753Smm version(); 337228753Smm break; 338228753Smm#if 0 339228753Smm /* 340228753Smm * cpio_getopt() handles -W specially, so it's not 341228753Smm * available here. 342228753Smm */ 343228753Smm case 'W': /* Obscure, but useful GNU convention. */ 344228753Smm break; 345228753Smm#endif 346228753Smm case 'y': /* tar convention */ 347228753Smm cpio->compress = opt; 348228753Smm break; 349228753Smm case 'Z': /* tar convention */ 350228753Smm cpio->compress = opt; 351228753Smm break; 352228753Smm case 'z': /* tar convention */ 353228753Smm cpio->compress = opt; 354228753Smm break; 355228753Smm default: 356228753Smm usage(); 357228753Smm } 358228753Smm } 359228753Smm 360228753Smm /* 361228753Smm * Sanity-check args, error out on nonsensical combinations. 362228753Smm */ 363228753Smm /* -t implies -i if no mode was specified. */ 364228753Smm if (cpio->option_list && cpio->mode == '\0') 365228753Smm cpio->mode = 'i'; 366228753Smm /* -t requires -i */ 367228753Smm if (cpio->option_list && cpio->mode != 'i') 368228753Smm lafe_errc(1, 0, "Option -t requires -i"); 369228753Smm /* -n requires -it */ 370228753Smm if (cpio->option_numeric_uid_gid && !cpio->option_list) 371228753Smm lafe_errc(1, 0, "Option -n requires -it"); 372228753Smm /* Can only specify format when writing */ 373228753Smm if (cpio->format != NULL && cpio->mode != 'o') 374228753Smm lafe_errc(1, 0, "Option --format requires -o"); 375228753Smm /* -l requires -p */ 376228753Smm if (cpio->option_link && cpio->mode != 'p') 377228753Smm lafe_errc(1, 0, "Option -l requires -p"); 378232153Smm /* -v overrides -V */ 379232153Smm if (cpio->dot && cpio->verbose) 380232153Smm cpio->dot = 0; 381228753Smm /* TODO: Flag other nonsensical combinations. */ 382228753Smm 383228753Smm switch (cpio->mode) { 384228753Smm case 'o': 385228753Smm /* TODO: Implement old binary format in libarchive, 386228753Smm use that here. */ 387228753Smm if (cpio->format == NULL) 388228753Smm cpio->format = "odc"; /* Default format */ 389228753Smm 390228753Smm mode_out(cpio); 391228753Smm break; 392228753Smm case 'i': 393228753Smm while (*cpio->argv != NULL) { 394238856Smm if (archive_match_include_pattern(cpio->matching, 395238856Smm *cpio->argv) != ARCHIVE_OK) 396238856Smm lafe_errc(1, 0, "Error : %s", 397238856Smm archive_error_string(cpio->matching)); 398228753Smm --cpio->argc; 399228753Smm ++cpio->argv; 400228753Smm } 401228753Smm if (cpio->option_list) 402228753Smm mode_list(cpio); 403228753Smm else 404228753Smm mode_in(cpio); 405228753Smm break; 406228753Smm case 'p': 407228753Smm if (*cpio->argv == NULL || **cpio->argv == '\0') 408228753Smm lafe_errc(1, 0, 409228753Smm "-p mode requires a target directory"); 410228753Smm mode_pass(cpio, *cpio->argv); 411228753Smm break; 412228753Smm default: 413228753Smm lafe_errc(1, 0, 414228753Smm "Must specify at least one of -i, -o, or -p"); 415228753Smm } 416228753Smm 417238856Smm archive_match_free(cpio->matching); 418228753Smm free_cache(cpio->gname_cache); 419228753Smm free_cache(cpio->uname_cache); 420228753Smm return (cpio->return_value); 421228753Smm} 422228753Smm 423228753Smmstatic void 424228753Smmusage(void) 425228753Smm{ 426228753Smm const char *p; 427228753Smm 428228753Smm p = lafe_progname; 429228753Smm 430228753Smm fprintf(stderr, "Brief Usage:\n"); 431228753Smm fprintf(stderr, " List: %s -it < archive\n", p); 432228753Smm fprintf(stderr, " Extract: %s -i < archive\n", p); 433228753Smm fprintf(stderr, " Create: %s -o < filenames > archive\n", p); 434228753Smm fprintf(stderr, " Help: %s --help\n", p); 435228753Smm exit(1); 436228753Smm} 437228753Smm 438228753Smmstatic const char *long_help_msg = 439228753Smm "First option must be a mode specifier:\n" 440228753Smm " -i Input -o Output -p Pass\n" 441228753Smm "Common Options:\n" 442232153Smm " -v Verbose filenames -V one dot per file\n" 443228753Smm "Create: %p -o [options] < [list of files] > [archive]\n" 444228753Smm " -J,-y,-z,--lzma Compress archive with xz/bzip2/gzip/lzma\n" 445228753Smm " --format {odc|newc|ustar} Select archive format\n" 446228753Smm "List: %p -it < [archive]\n" 447228753Smm "Extract: %p -i [options] < [archive]\n"; 448228753Smm 449228753Smm 450228753Smm/* 451228753Smm * Note that the word 'bsdcpio' will always appear in the first line 452228753Smm * of output. 453228753Smm * 454228753Smm * In particular, /bin/sh scripts that need to test for the presence 455228753Smm * of bsdcpio can use the following template: 456228753Smm * 457228753Smm * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \ 458228753Smm * echo bsdcpio; else echo not bsdcpio; fi 459228753Smm */ 460228753Smmstatic void 461228753Smmlong_help(void) 462228753Smm{ 463228753Smm const char *prog; 464228753Smm const char *p; 465228753Smm 466228753Smm prog = lafe_progname; 467228753Smm 468228753Smm fflush(stderr); 469228753Smm 470228753Smm p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : ""; 471228753Smm printf("%s%s: manipulate archive files\n", prog, p); 472228753Smm 473228753Smm for (p = long_help_msg; *p != '\0'; p++) { 474228753Smm if (*p == '%') { 475228753Smm if (p[1] == 'p') { 476228753Smm fputs(prog, stdout); 477228753Smm p++; 478228753Smm } else 479228753Smm putchar('%'); 480228753Smm } else 481228753Smm putchar(*p); 482228753Smm } 483228753Smm version(); 484228753Smm} 485228753Smm 486228753Smmstatic void 487228753Smmversion(void) 488228753Smm{ 489228753Smm fprintf(stdout,"bsdcpio %s -- %s\n", 490228753Smm BSDCPIO_VERSION_STRING, 491232153Smm archive_version_string()); 492228753Smm exit(0); 493228753Smm} 494228753Smm 495228753Smmstatic void 496228753Smmmode_out(struct cpio *cpio) 497228753Smm{ 498228753Smm struct archive_entry *entry, *spare; 499228753Smm struct lafe_line_reader *lr; 500228753Smm const char *p; 501228753Smm int r; 502228753Smm 503228753Smm if (cpio->option_append) 504228753Smm lafe_errc(1, 0, "Append mode not yet supported."); 505228753Smm 506228753Smm cpio->archive_read_disk = archive_read_disk_new(); 507228753Smm if (cpio->archive_read_disk == NULL) 508228753Smm lafe_errc(1, 0, "Failed to allocate archive object"); 509228753Smm if (cpio->option_follow_links) 510228753Smm archive_read_disk_set_symlink_logical(cpio->archive_read_disk); 511228753Smm else 512228753Smm archive_read_disk_set_symlink_physical(cpio->archive_read_disk); 513228753Smm archive_read_disk_set_standard_lookup(cpio->archive_read_disk); 514228753Smm 515228753Smm cpio->archive = archive_write_new(); 516228753Smm if (cpio->archive == NULL) 517228753Smm lafe_errc(1, 0, "Failed to allocate archive object"); 518228753Smm switch (cpio->compress) { 519228753Smm case 'J': 520228753Smm r = archive_write_set_compression_xz(cpio->archive); 521228753Smm break; 522228753Smm case OPTION_LZMA: 523228753Smm r = archive_write_set_compression_lzma(cpio->archive); 524228753Smm break; 525228753Smm case 'j': case 'y': 526228753Smm r = archive_write_set_compression_bzip2(cpio->archive); 527228753Smm break; 528228753Smm case 'z': 529228753Smm r = archive_write_set_compression_gzip(cpio->archive); 530228753Smm break; 531228753Smm case 'Z': 532228753Smm r = archive_write_set_compression_compress(cpio->archive); 533228753Smm break; 534228753Smm default: 535228753Smm r = archive_write_set_compression_none(cpio->archive); 536228753Smm break; 537228753Smm } 538228753Smm if (r < ARCHIVE_WARN) 539228753Smm lafe_errc(1, 0, "Requested compression not available"); 540228753Smm r = archive_write_set_format_by_name(cpio->archive, cpio->format); 541228753Smm if (r != ARCHIVE_OK) 542228753Smm lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 543228753Smm archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block); 544228753Smm cpio->linkresolver = archive_entry_linkresolver_new(); 545228753Smm archive_entry_linkresolver_set_strategy(cpio->linkresolver, 546228753Smm archive_format(cpio->archive)); 547228753Smm 548228753Smm /* 549228753Smm * The main loop: Copy each file into the output archive. 550228753Smm */ 551228753Smm r = archive_write_open_file(cpio->archive, cpio->filename); 552228753Smm if (r != ARCHIVE_OK) 553228753Smm lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 554228753Smm lr = lafe_line_reader("-", cpio->option_null); 555228753Smm while ((p = lafe_line_reader_next(lr)) != NULL) 556228753Smm file_to_archive(cpio, p); 557228753Smm lafe_line_reader_free(lr); 558228753Smm 559228753Smm /* 560228753Smm * The hardlink detection may have queued up a couple of entries 561228753Smm * that can now be flushed. 562228753Smm */ 563228753Smm entry = NULL; 564228753Smm archive_entry_linkify(cpio->linkresolver, &entry, &spare); 565228753Smm while (entry != NULL) { 566228753Smm entry_to_archive(cpio, entry); 567228753Smm archive_entry_free(entry); 568228753Smm entry = NULL; 569228753Smm archive_entry_linkify(cpio->linkresolver, &entry, &spare); 570228753Smm } 571228753Smm 572228753Smm r = archive_write_close(cpio->archive); 573232153Smm if (cpio->dot) 574232153Smm fprintf(stderr, "\n"); 575228753Smm if (r != ARCHIVE_OK) 576228753Smm lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 577228753Smm 578228753Smm if (!cpio->quiet) { 579228753Smm int64_t blocks = 580228753Smm (archive_position_uncompressed(cpio->archive) + 511) 581228753Smm / 512; 582228753Smm fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 583228753Smm blocks == 1 ? "block" : "blocks"); 584228753Smm } 585232153Smm archive_write_free(cpio->archive); 586228753Smm} 587228753Smm 588232153Smmstatic const char * 589232153Smmremove_leading_slash(const char *p) 590232153Smm{ 591232153Smm const char *rp; 592232153Smm 593232153Smm /* Remove leading "//./" or "//?/" or "//?/UNC/" 594232153Smm * (absolute path prefixes used by Windows API) */ 595232153Smm if ((p[0] == '/' || p[0] == '\\') && 596232153Smm (p[1] == '/' || p[1] == '\\') && 597232153Smm (p[2] == '.' || p[2] == '?') && 598232153Smm (p[3] == '/' || p[3] == '\\')) 599232153Smm { 600232153Smm if (p[2] == '?' && 601232153Smm (p[4] == 'U' || p[4] == 'u') && 602232153Smm (p[5] == 'N' || p[5] == 'n') && 603232153Smm (p[6] == 'C' || p[6] == 'c') && 604232153Smm (p[7] == '/' || p[7] == '\\')) 605232153Smm p += 8; 606232153Smm else 607232153Smm p += 4; 608232153Smm } 609232153Smm do { 610232153Smm rp = p; 611232153Smm /* Remove leading drive letter from archives created 612232153Smm * on Windows. */ 613232153Smm if (((p[0] >= 'a' && p[0] <= 'z') || 614232153Smm (p[0] >= 'A' && p[0] <= 'Z')) && 615232153Smm p[1] == ':') { 616232153Smm p += 2; 617232153Smm } 618232153Smm /* Remove leading "/../", "//", etc. */ 619232153Smm while (p[0] == '/' || p[0] == '\\') { 620232153Smm if (p[1] == '.' && p[2] == '.' && 621232153Smm (p[3] == '/' || p[3] == '\\')) { 622232153Smm p += 3; /* Remove "/..", leave "/" 623232153Smm * for next pass. */ 624232153Smm } else 625232153Smm p += 1; /* Remove "/". */ 626232153Smm } 627232153Smm } while (rp != p); 628232153Smm return (p); 629232153Smm} 630232153Smm 631228753Smm/* 632228753Smm * This is used by both out mode (to copy objects from disk into 633228753Smm * an archive) and pass mode (to copy objects from disk to 634228753Smm * an archive_write_disk "archive"). 635228753Smm */ 636228753Smmstatic int 637228753Smmfile_to_archive(struct cpio *cpio, const char *srcpath) 638228753Smm{ 639228753Smm const char *destpath; 640228753Smm struct archive_entry *entry, *spare; 641228753Smm size_t len; 642228753Smm int r; 643228753Smm 644228753Smm /* 645228753Smm * Create an archive_entry describing the source file. 646228753Smm * 647228753Smm */ 648228753Smm entry = archive_entry_new(); 649228753Smm if (entry == NULL) 650228753Smm lafe_errc(1, 0, "Couldn't allocate entry"); 651228753Smm archive_entry_copy_sourcepath(entry, srcpath); 652228753Smm r = archive_read_disk_entry_from_file(cpio->archive_read_disk, 653228753Smm entry, -1, NULL); 654228753Smm if (r < ARCHIVE_FAILED) 655228753Smm lafe_errc(1, 0, "%s", 656228753Smm archive_error_string(cpio->archive_read_disk)); 657228753Smm if (r < ARCHIVE_OK) 658228753Smm lafe_warnc(0, "%s", 659228753Smm archive_error_string(cpio->archive_read_disk)); 660228753Smm if (r <= ARCHIVE_FAILED) { 661228753Smm cpio->return_value = 1; 662228753Smm return (r); 663228753Smm } 664228753Smm 665228777Smm if (cpio->uid_override >= 0) { 666228753Smm archive_entry_set_uid(entry, cpio->uid_override); 667228777Smm archive_entry_set_uname(entry, cpio->uname_override); 668228777Smm } 669228777Smm if (cpio->gid_override >= 0) { 670228753Smm archive_entry_set_gid(entry, cpio->gid_override); 671228777Smm archive_entry_set_gname(entry, cpio->gname_override); 672228777Smm } 673228753Smm 674228753Smm /* 675228753Smm * Generate a destination path for this entry. 676228753Smm * "destination path" is the name to which it will be copied in 677228753Smm * pass mode or the name that will go into the archive in 678228753Smm * output mode. 679228753Smm */ 680228753Smm destpath = srcpath; 681228753Smm if (cpio->destdir) { 682228753Smm len = strlen(cpio->destdir) + strlen(srcpath) + 8; 683228753Smm if (len >= cpio->pass_destpath_alloc) { 684228753Smm while (len >= cpio->pass_destpath_alloc) { 685228753Smm cpio->pass_destpath_alloc += 512; 686228753Smm cpio->pass_destpath_alloc *= 2; 687228753Smm } 688228753Smm free(cpio->pass_destpath); 689228753Smm cpio->pass_destpath = malloc(cpio->pass_destpath_alloc); 690228753Smm if (cpio->pass_destpath == NULL) 691228753Smm lafe_errc(1, ENOMEM, 692228753Smm "Can't allocate path buffer"); 693228753Smm } 694228753Smm strcpy(cpio->pass_destpath, cpio->destdir); 695232153Smm strcat(cpio->pass_destpath, remove_leading_slash(srcpath)); 696228753Smm destpath = cpio->pass_destpath; 697228753Smm } 698228753Smm if (cpio->option_rename) 699228753Smm destpath = cpio_rename(destpath); 700228753Smm if (destpath == NULL) 701228753Smm return (0); 702228753Smm archive_entry_copy_pathname(entry, destpath); 703228753Smm 704228753Smm /* 705228753Smm * If we're trying to preserve hardlinks, match them here. 706228753Smm */ 707228753Smm spare = NULL; 708228753Smm if (cpio->linkresolver != NULL 709228753Smm && archive_entry_filetype(entry) != AE_IFDIR) { 710228753Smm archive_entry_linkify(cpio->linkresolver, &entry, &spare); 711228753Smm } 712228753Smm 713228753Smm if (entry != NULL) { 714228753Smm r = entry_to_archive(cpio, entry); 715228753Smm archive_entry_free(entry); 716228753Smm if (spare != NULL) { 717228753Smm if (r == 0) 718228753Smm r = entry_to_archive(cpio, spare); 719228753Smm archive_entry_free(spare); 720228753Smm } 721228753Smm } 722228753Smm return (r); 723228753Smm} 724228753Smm 725228753Smmstatic int 726228753Smmentry_to_archive(struct cpio *cpio, struct archive_entry *entry) 727228753Smm{ 728228753Smm const char *destpath = archive_entry_pathname(entry); 729228753Smm const char *srcpath = archive_entry_sourcepath(entry); 730228753Smm int fd = -1; 731228753Smm ssize_t bytes_read; 732228753Smm int r; 733228753Smm 734228753Smm /* Print out the destination name to the user. */ 735228753Smm if (cpio->verbose) 736228753Smm fprintf(stderr,"%s", destpath); 737232153Smm if (cpio->dot) 738232153Smm fprintf(stderr, "."); 739228753Smm 740228753Smm /* 741228753Smm * Option_link only makes sense in pass mode and for 742228753Smm * regular files. Also note: if a link operation fails 743228753Smm * because of cross-device restrictions, we'll fall back 744228753Smm * to copy mode for that entry. 745228753Smm * 746228753Smm * TODO: Test other cpio implementations to see if they 747228753Smm * hard-link anything other than regular files here. 748228753Smm */ 749228753Smm if (cpio->option_link 750228753Smm && archive_entry_filetype(entry) == AE_IFREG) 751228753Smm { 752228753Smm struct archive_entry *t; 753228753Smm /* Save the original entry in case we need it later. */ 754228753Smm t = archive_entry_clone(entry); 755228753Smm if (t == NULL) 756228753Smm lafe_errc(1, ENOMEM, "Can't create link"); 757228753Smm /* Note: link(2) doesn't create parent directories, 758228753Smm * so we use archive_write_header() instead as a 759228753Smm * convenience. */ 760228753Smm archive_entry_set_hardlink(t, srcpath); 761228753Smm /* This is a straight link that carries no data. */ 762228753Smm archive_entry_set_size(t, 0); 763228753Smm r = archive_write_header(cpio->archive, t); 764228753Smm archive_entry_free(t); 765228753Smm if (r != ARCHIVE_OK) 766228753Smm lafe_warnc(archive_errno(cpio->archive), 767228753Smm "%s", archive_error_string(cpio->archive)); 768228753Smm if (r == ARCHIVE_FATAL) 769228753Smm exit(1); 770228753Smm#ifdef EXDEV 771228753Smm if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) { 772228753Smm /* Cross-device link: Just fall through and use 773228753Smm * the original entry to copy the file over. */ 774228753Smm lafe_warnc(0, "Copying file instead"); 775228753Smm } else 776228753Smm#endif 777228753Smm return (0); 778228753Smm } 779228753Smm 780228753Smm /* 781228753Smm * Make sure we can open the file (if necessary) before 782228753Smm * trying to write the header. 783228753Smm */ 784228753Smm if (archive_entry_filetype(entry) == AE_IFREG) { 785228753Smm if (archive_entry_size(entry) > 0) { 786228753Smm fd = open(srcpath, O_RDONLY | O_BINARY); 787228753Smm if (fd < 0) { 788228753Smm lafe_warnc(errno, 789228753Smm "%s: could not open file", srcpath); 790228753Smm goto cleanup; 791228753Smm } 792228753Smm } 793228753Smm } else { 794228753Smm archive_entry_set_size(entry, 0); 795228753Smm } 796228753Smm 797228753Smm r = archive_write_header(cpio->archive, entry); 798228753Smm 799228753Smm if (r != ARCHIVE_OK) 800228753Smm lafe_warnc(archive_errno(cpio->archive), 801228753Smm "%s: %s", 802228753Smm srcpath, 803228753Smm archive_error_string(cpio->archive)); 804228753Smm 805228753Smm if (r == ARCHIVE_FATAL) 806228753Smm exit(1); 807228753Smm 808232153Smm if (r >= ARCHIVE_WARN && archive_entry_size(entry) > 0 && fd >= 0) { 809228753Smm bytes_read = read(fd, cpio->buff, cpio->buff_size); 810228753Smm while (bytes_read > 0) { 811228753Smm r = archive_write_data(cpio->archive, 812228753Smm cpio->buff, bytes_read); 813228753Smm if (r < 0) 814228753Smm lafe_errc(1, archive_errno(cpio->archive), 815228753Smm "%s", archive_error_string(cpio->archive)); 816228753Smm if (r < bytes_read) { 817228753Smm lafe_warnc(0, 818228753Smm "Truncated write; file may have grown while being archived."); 819228753Smm } 820228753Smm bytes_read = read(fd, cpio->buff, cpio->buff_size); 821228753Smm } 822228753Smm } 823228753Smm 824228753Smm fd = restore_time(cpio, entry, srcpath, fd); 825228753Smm 826228753Smmcleanup: 827228753Smm if (cpio->verbose) 828228753Smm fprintf(stderr,"\n"); 829228753Smm if (fd >= 0) 830228753Smm close(fd); 831228753Smm return (0); 832228753Smm} 833228753Smm 834228753Smmstatic int 835228753Smmrestore_time(struct cpio *cpio, struct archive_entry *entry, 836228753Smm const char *name, int fd) 837228753Smm{ 838228753Smm#ifndef HAVE_UTIMES 839228753Smm static int warned = 0; 840228753Smm 841228753Smm (void)cpio; /* UNUSED */ 842228753Smm (void)entry; /* UNUSED */ 843228753Smm (void)name; /* UNUSED */ 844228753Smm 845228753Smm if (!warned) 846228753Smm lafe_warnc(0, "Can't restore access times on this platform"); 847228753Smm warned = 1; 848228753Smm return (fd); 849228753Smm#else 850228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 851228753Smm struct __timeval times[2]; 852228753Smm#else 853228753Smm struct timeval times[2]; 854228753Smm#endif 855228753Smm 856228753Smm if (!cpio->option_atime_restore) 857228753Smm return (fd); 858228753Smm 859228753Smm times[1].tv_sec = archive_entry_mtime(entry); 860228753Smm times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000; 861228753Smm 862228753Smm times[0].tv_sec = archive_entry_atime(entry); 863228753Smm times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000; 864228753Smm 865228753Smm#if defined(HAVE_FUTIMES) && !defined(__CYGWIN__) 866228753Smm if (fd >= 0 && futimes(fd, times) == 0) 867228753Smm return (fd); 868228753Smm#endif 869228753Smm /* 870228753Smm * Some platform cannot restore access times if the file descriptor 871228753Smm * is still opened. 872228753Smm */ 873228753Smm if (fd >= 0) { 874228753Smm close(fd); 875228753Smm fd = -1; 876228753Smm } 877228753Smm 878228753Smm#ifdef HAVE_LUTIMES 879228753Smm if (lutimes(name, times) != 0) 880228753Smm#else 881228753Smm if ((AE_IFLNK != archive_entry_filetype(entry)) 882228753Smm && utimes(name, times) != 0) 883228753Smm#endif 884228753Smm lafe_warnc(errno, "Can't update time for %s", name); 885228753Smm#endif 886228753Smm return (fd); 887228753Smm} 888228753Smm 889228753Smm 890228753Smmstatic void 891228753Smmmode_in(struct cpio *cpio) 892228753Smm{ 893228753Smm struct archive *a; 894228753Smm struct archive_entry *entry; 895228753Smm struct archive *ext; 896228753Smm const char *destpath; 897228753Smm int r; 898228753Smm 899228753Smm ext = archive_write_disk_new(); 900228753Smm if (ext == NULL) 901228753Smm lafe_errc(1, 0, "Couldn't allocate restore object"); 902228753Smm r = archive_write_disk_set_options(ext, cpio->extract_flags); 903228753Smm if (r != ARCHIVE_OK) 904228753Smm lafe_errc(1, 0, "%s", archive_error_string(ext)); 905228753Smm a = archive_read_new(); 906228753Smm if (a == NULL) 907228753Smm lafe_errc(1, 0, "Couldn't allocate archive object"); 908232153Smm archive_read_support_filter_all(a); 909228753Smm archive_read_support_format_all(a); 910228753Smm 911228753Smm if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block)) 912228753Smm lafe_errc(1, archive_errno(a), 913228753Smm "%s", archive_error_string(a)); 914228753Smm for (;;) { 915228753Smm r = archive_read_next_header(a, &entry); 916228753Smm if (r == ARCHIVE_EOF) 917228753Smm break; 918228753Smm if (r != ARCHIVE_OK) { 919228753Smm lafe_errc(1, archive_errno(a), 920228753Smm "%s", archive_error_string(a)); 921228753Smm } 922238856Smm if (archive_match_path_excluded(cpio->matching, entry)) 923228753Smm continue; 924228753Smm if (cpio->option_rename) { 925228753Smm destpath = cpio_rename(archive_entry_pathname(entry)); 926228753Smm archive_entry_set_pathname(entry, destpath); 927228753Smm } else 928228753Smm destpath = archive_entry_pathname(entry); 929228753Smm if (destpath == NULL) 930228753Smm continue; 931228753Smm if (cpio->verbose) 932232153Smm fprintf(stderr, "%s\n", destpath); 933232153Smm if (cpio->dot) 934232153Smm fprintf(stderr, "."); 935228753Smm if (cpio->uid_override >= 0) 936228753Smm archive_entry_set_uid(entry, cpio->uid_override); 937228753Smm if (cpio->gid_override >= 0) 938228753Smm archive_entry_set_gid(entry, cpio->gid_override); 939228753Smm r = archive_write_header(ext, entry); 940228753Smm if (r != ARCHIVE_OK) { 941228753Smm fprintf(stderr, "%s: %s\n", 942228753Smm archive_entry_pathname(entry), 943228753Smm archive_error_string(ext)); 944232153Smm } else if (!archive_entry_size_is_set(entry) 945232153Smm || archive_entry_size(entry) > 0) { 946228753Smm r = extract_data(a, ext); 947228753Smm if (r != ARCHIVE_OK) 948228753Smm cpio->return_value = 1; 949228753Smm } 950228753Smm } 951228753Smm r = archive_read_close(a); 952232153Smm if (cpio->dot) 953232153Smm fprintf(stderr, "\n"); 954228753Smm if (r != ARCHIVE_OK) 955228753Smm lafe_errc(1, 0, "%s", archive_error_string(a)); 956228753Smm r = archive_write_close(ext); 957228753Smm if (r != ARCHIVE_OK) 958228753Smm lafe_errc(1, 0, "%s", archive_error_string(ext)); 959228753Smm if (!cpio->quiet) { 960228753Smm int64_t blocks = (archive_position_uncompressed(a) + 511) 961228753Smm / 512; 962228753Smm fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 963228753Smm blocks == 1 ? "block" : "blocks"); 964228753Smm } 965232153Smm archive_read_free(a); 966232153Smm archive_write_free(ext); 967228753Smm exit(cpio->return_value); 968228753Smm} 969228753Smm 970228753Smm/* 971228753Smm * Exits if there's a fatal error. Returns ARCHIVE_OK 972228753Smm * if everything is kosher. 973228753Smm */ 974228753Smmstatic int 975228753Smmextract_data(struct archive *ar, struct archive *aw) 976228753Smm{ 977228753Smm int r; 978228753Smm size_t size; 979228753Smm const void *block; 980232153Smm int64_t offset; 981228753Smm 982228753Smm for (;;) { 983228753Smm r = archive_read_data_block(ar, &block, &size, &offset); 984228753Smm if (r == ARCHIVE_EOF) 985228753Smm return (ARCHIVE_OK); 986228753Smm if (r != ARCHIVE_OK) { 987228753Smm lafe_warnc(archive_errno(ar), 988228753Smm "%s", archive_error_string(ar)); 989228753Smm exit(1); 990228753Smm } 991228753Smm r = archive_write_data_block(aw, block, size, offset); 992228753Smm if (r != ARCHIVE_OK) { 993228753Smm lafe_warnc(archive_errno(aw), 994228753Smm "%s", archive_error_string(aw)); 995228753Smm return (r); 996228753Smm } 997228753Smm } 998228753Smm} 999228753Smm 1000228753Smmstatic void 1001228753Smmmode_list(struct cpio *cpio) 1002228753Smm{ 1003228753Smm struct archive *a; 1004228753Smm struct archive_entry *entry; 1005228753Smm int r; 1006228753Smm 1007228753Smm a = archive_read_new(); 1008228753Smm if (a == NULL) 1009228753Smm lafe_errc(1, 0, "Couldn't allocate archive object"); 1010232153Smm archive_read_support_filter_all(a); 1011228753Smm archive_read_support_format_all(a); 1012228753Smm 1013228753Smm if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block)) 1014228753Smm lafe_errc(1, archive_errno(a), 1015228753Smm "%s", archive_error_string(a)); 1016228753Smm for (;;) { 1017228753Smm r = archive_read_next_header(a, &entry); 1018228753Smm if (r == ARCHIVE_EOF) 1019228753Smm break; 1020228753Smm if (r != ARCHIVE_OK) { 1021228753Smm lafe_errc(1, archive_errno(a), 1022228753Smm "%s", archive_error_string(a)); 1023228753Smm } 1024238856Smm if (archive_match_path_excluded(cpio->matching, entry)) 1025228753Smm continue; 1026228753Smm if (cpio->verbose) 1027228753Smm list_item_verbose(cpio, entry); 1028228753Smm else 1029228753Smm fprintf(stdout, "%s\n", archive_entry_pathname(entry)); 1030228753Smm } 1031228753Smm r = archive_read_close(a); 1032228753Smm if (r != ARCHIVE_OK) 1033228753Smm lafe_errc(1, 0, "%s", archive_error_string(a)); 1034228753Smm if (!cpio->quiet) { 1035228753Smm int64_t blocks = (archive_position_uncompressed(a) + 511) 1036228753Smm / 512; 1037228753Smm fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 1038228753Smm blocks == 1 ? "block" : "blocks"); 1039228753Smm } 1040232153Smm archive_read_free(a); 1041228753Smm exit(0); 1042228753Smm} 1043228753Smm 1044228753Smm/* 1045228753Smm * Display information about the current file. 1046228753Smm * 1047228753Smm * The format here roughly duplicates the output of 'ls -l'. 1048228753Smm * This is based on SUSv2, where 'tar tv' is documented as 1049228753Smm * listing additional information in an "unspecified format," 1050228753Smm * and 'pax -l' is documented as using the same format as 'ls -l'. 1051228753Smm */ 1052228753Smmstatic void 1053228753Smmlist_item_verbose(struct cpio *cpio, struct archive_entry *entry) 1054228753Smm{ 1055228753Smm char size[32]; 1056228753Smm char date[32]; 1057228753Smm char uids[16], gids[16]; 1058228753Smm const char *uname, *gname; 1059228753Smm FILE *out = stdout; 1060228753Smm const char *fmt; 1061228753Smm time_t mtime; 1062228753Smm static time_t now; 1063228753Smm 1064228753Smm if (!now) 1065228753Smm time(&now); 1066228753Smm 1067228753Smm if (cpio->option_numeric_uid_gid) { 1068228753Smm /* Format numeric uid/gid for display. */ 1069228753Smm strcpy(uids, cpio_i64toa(archive_entry_uid(entry))); 1070228753Smm uname = uids; 1071228753Smm strcpy(gids, cpio_i64toa(archive_entry_gid(entry))); 1072228753Smm gname = gids; 1073228753Smm } else { 1074228753Smm /* Use uname if it's present, else lookup name from uid. */ 1075228753Smm uname = archive_entry_uname(entry); 1076228753Smm if (uname == NULL) 1077232153Smm uname = lookup_uname(cpio, (uid_t)archive_entry_uid(entry)); 1078228753Smm /* Use gname if it's present, else lookup name from gid. */ 1079228753Smm gname = archive_entry_gname(entry); 1080228753Smm if (gname == NULL) 1081232153Smm gname = lookup_gname(cpio, (uid_t)archive_entry_gid(entry)); 1082228753Smm } 1083228753Smm 1084228753Smm /* Print device number or file size. */ 1085228753Smm if (archive_entry_filetype(entry) == AE_IFCHR 1086228753Smm || archive_entry_filetype(entry) == AE_IFBLK) { 1087228753Smm snprintf(size, sizeof(size), "%lu,%lu", 1088228753Smm (unsigned long)archive_entry_rdevmajor(entry), 1089228753Smm (unsigned long)archive_entry_rdevminor(entry)); 1090228753Smm } else { 1091228753Smm strcpy(size, cpio_i64toa(archive_entry_size(entry))); 1092228753Smm } 1093228753Smm 1094228753Smm /* Format the time using 'ls -l' conventions. */ 1095228753Smm mtime = archive_entry_mtime(entry); 1096228753Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1097228753Smm /* Windows' strftime function does not support %e format. */ 1098228753Smm if (mtime - now > 365*86400/2 1099228753Smm || mtime - now < -365*86400/2) 1100228753Smm fmt = cpio->day_first ? "%d %b %Y" : "%b %d %Y"; 1101228753Smm else 1102228753Smm fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M"; 1103228753Smm#else 1104228753Smm if (abs(mtime - now) > (365/2)*86400) 1105228753Smm fmt = cpio->day_first ? "%e %b %Y" : "%b %e %Y"; 1106228753Smm else 1107228753Smm fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M"; 1108228753Smm#endif 1109228753Smm strftime(date, sizeof(date), fmt, localtime(&mtime)); 1110228753Smm 1111228753Smm fprintf(out, "%s%3d %-8s %-8s %8s %12s %s", 1112228753Smm archive_entry_strmode(entry), 1113228753Smm archive_entry_nlink(entry), 1114228753Smm uname, gname, size, date, 1115228753Smm archive_entry_pathname(entry)); 1116228753Smm 1117228753Smm /* Extra information for links. */ 1118228753Smm if (archive_entry_hardlink(entry)) /* Hard link */ 1119228753Smm fprintf(out, " link to %s", archive_entry_hardlink(entry)); 1120228753Smm else if (archive_entry_symlink(entry)) /* Symbolic link */ 1121228753Smm fprintf(out, " -> %s", archive_entry_symlink(entry)); 1122228753Smm fprintf(out, "\n"); 1123228753Smm} 1124228753Smm 1125228753Smmstatic void 1126228753Smmmode_pass(struct cpio *cpio, const char *destdir) 1127228753Smm{ 1128228753Smm struct lafe_line_reader *lr; 1129228753Smm const char *p; 1130228753Smm int r; 1131228753Smm 1132228753Smm /* Ensure target dir has a trailing '/' to simplify path surgery. */ 1133228753Smm cpio->destdir = malloc(strlen(destdir) + 8); 1134228753Smm strcpy(cpio->destdir, destdir); 1135228753Smm if (destdir[strlen(destdir) - 1] != '/') 1136228753Smm strcat(cpio->destdir, "/"); 1137228753Smm 1138228753Smm cpio->archive = archive_write_disk_new(); 1139228753Smm if (cpio->archive == NULL) 1140228753Smm lafe_errc(1, 0, "Failed to allocate archive object"); 1141228753Smm r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags); 1142228753Smm if (r != ARCHIVE_OK) 1143228753Smm lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 1144228753Smm cpio->linkresolver = archive_entry_linkresolver_new(); 1145228753Smm archive_write_disk_set_standard_lookup(cpio->archive); 1146228753Smm 1147228753Smm cpio->archive_read_disk = archive_read_disk_new(); 1148228753Smm if (cpio->archive_read_disk == NULL) 1149228753Smm lafe_errc(1, 0, "Failed to allocate archive object"); 1150228753Smm if (cpio->option_follow_links) 1151228753Smm archive_read_disk_set_symlink_logical(cpio->archive_read_disk); 1152228753Smm else 1153228753Smm archive_read_disk_set_symlink_physical(cpio->archive_read_disk); 1154228753Smm archive_read_disk_set_standard_lookup(cpio->archive_read_disk); 1155228753Smm 1156228753Smm lr = lafe_line_reader("-", cpio->option_null); 1157228753Smm while ((p = lafe_line_reader_next(lr)) != NULL) 1158228753Smm file_to_archive(cpio, p); 1159228753Smm lafe_line_reader_free(lr); 1160228753Smm 1161228753Smm archive_entry_linkresolver_free(cpio->linkresolver); 1162228753Smm r = archive_write_close(cpio->archive); 1163232153Smm if (cpio->dot) 1164232153Smm fprintf(stderr, "\n"); 1165228753Smm if (r != ARCHIVE_OK) 1166228753Smm lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 1167228753Smm 1168228753Smm if (!cpio->quiet) { 1169228753Smm int64_t blocks = 1170228753Smm (archive_position_uncompressed(cpio->archive) + 511) 1171228753Smm / 512; 1172228753Smm fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 1173228753Smm blocks == 1 ? "block" : "blocks"); 1174228753Smm } 1175228753Smm 1176232153Smm archive_write_free(cpio->archive); 1177228753Smm} 1178228753Smm 1179228753Smm/* 1180228753Smm * Prompt for a new name for this entry. Returns a pointer to the 1181228753Smm * new name or NULL if the entry should not be copied. This 1182228753Smm * implements the semantics defined in POSIX.1-1996, which specifies 1183228753Smm * that an input of '.' means the name should be unchanged. GNU cpio 1184228753Smm * treats '.' as a literal new name. 1185228753Smm */ 1186228753Smmstatic const char * 1187228753Smmcpio_rename(const char *name) 1188228753Smm{ 1189228753Smm static char buff[1024]; 1190228753Smm FILE *t; 1191228753Smm char *p, *ret; 1192232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 1193232153Smm FILE *to; 1194228753Smm 1195232153Smm t = fopen("CONIN$", "r"); 1196232153Smm if (t == NULL) 1197232153Smm return (name); 1198232153Smm to = fopen("CONOUT$", "w"); 1199232153Smm if (to == NULL) 1200232153Smm return (name); 1201232153Smm fprintf(to, "%s (Enter/./(new name))? ", name); 1202232153Smm fclose(to); 1203232153Smm#else 1204228753Smm t = fopen("/dev/tty", "r+"); 1205228753Smm if (t == NULL) 1206228753Smm return (name); 1207228753Smm fprintf(t, "%s (Enter/./(new name))? ", name); 1208228753Smm fflush(t); 1209232153Smm#endif 1210228753Smm 1211228753Smm p = fgets(buff, sizeof(buff), t); 1212228753Smm fclose(t); 1213228753Smm if (p == NULL) 1214228753Smm /* End-of-file is a blank line. */ 1215228753Smm return (NULL); 1216228753Smm 1217228753Smm while (*p == ' ' || *p == '\t') 1218228753Smm ++p; 1219228753Smm if (*p == '\n' || *p == '\0') 1220228753Smm /* Empty line. */ 1221228753Smm return (NULL); 1222228753Smm if (*p == '.' && p[1] == '\n') 1223228753Smm /* Single period preserves original name. */ 1224228753Smm return (name); 1225228753Smm ret = p; 1226228753Smm /* Trim the final newline. */ 1227228753Smm while (*p != '\0' && *p != '\n') 1228228753Smm ++p; 1229228753Smm /* Overwrite the final \n with a null character. */ 1230228753Smm *p = '\0'; 1231228753Smm return (ret); 1232228753Smm} 1233228753Smm 1234228753Smmstatic void 1235228753Smmfree_cache(struct name_cache *cache) 1236228753Smm{ 1237228753Smm size_t i; 1238228753Smm 1239228753Smm if (cache != NULL) { 1240228753Smm for (i = 0; i < cache->size; i++) 1241228753Smm free(cache->cache[i].name); 1242228753Smm free(cache); 1243228753Smm } 1244228753Smm} 1245228753Smm 1246228753Smm/* 1247228753Smm * Lookup uname/gname from uid/gid, return NULL if no match. 1248228753Smm */ 1249228753Smmstatic const char * 1250228753Smmlookup_name(struct cpio *cpio, struct name_cache **name_cache_variable, 1251228753Smm int (*lookup_fn)(struct cpio *, const char **, id_t), id_t id) 1252228753Smm{ 1253228753Smm char asnum[16]; 1254228753Smm struct name_cache *cache; 1255228753Smm const char *name; 1256228753Smm int slot; 1257228753Smm 1258228753Smm 1259228753Smm if (*name_cache_variable == NULL) { 1260228753Smm *name_cache_variable = malloc(sizeof(struct name_cache)); 1261228753Smm if (*name_cache_variable == NULL) 1262228753Smm lafe_errc(1, ENOMEM, "No more memory"); 1263228753Smm memset(*name_cache_variable, 0, sizeof(struct name_cache)); 1264228753Smm (*name_cache_variable)->size = name_cache_size; 1265228753Smm } 1266228753Smm 1267228753Smm cache = *name_cache_variable; 1268228753Smm cache->probes++; 1269228753Smm 1270228753Smm slot = id % cache->size; 1271228753Smm if (cache->cache[slot].name != NULL) { 1272228753Smm if (cache->cache[slot].id == id) { 1273228753Smm cache->hits++; 1274228753Smm return (cache->cache[slot].name); 1275228753Smm } 1276228753Smm free(cache->cache[slot].name); 1277228753Smm cache->cache[slot].name = NULL; 1278228753Smm } 1279228753Smm 1280228753Smm if (lookup_fn(cpio, &name, id) == 0) { 1281228753Smm if (name == NULL || name[0] == '\0') { 1282228753Smm /* If lookup failed, format it as a number. */ 1283228753Smm snprintf(asnum, sizeof(asnum), "%u", (unsigned)id); 1284228753Smm name = asnum; 1285228753Smm } 1286228753Smm cache->cache[slot].name = strdup(name); 1287228753Smm if (cache->cache[slot].name != NULL) { 1288228753Smm cache->cache[slot].id = id; 1289228753Smm return (cache->cache[slot].name); 1290228753Smm } 1291228753Smm /* 1292228753Smm * Conveniently, NULL marks an empty slot, so 1293228753Smm * if the strdup() fails, we've just failed to 1294228753Smm * cache it. No recovery necessary. 1295228753Smm */ 1296228753Smm } 1297228753Smm return (NULL); 1298228753Smm} 1299228753Smm 1300228753Smmstatic const char * 1301228753Smmlookup_uname(struct cpio *cpio, uid_t uid) 1302228753Smm{ 1303228753Smm return (lookup_name(cpio, &cpio->uname_cache, 1304228753Smm &lookup_uname_helper, (id_t)uid)); 1305228753Smm} 1306228753Smm 1307228753Smmstatic int 1308228753Smmlookup_uname_helper(struct cpio *cpio, const char **name, id_t id) 1309228753Smm{ 1310228753Smm struct passwd *pwent; 1311228753Smm 1312228753Smm (void)cpio; /* UNUSED */ 1313228753Smm 1314228753Smm errno = 0; 1315228753Smm pwent = getpwuid((uid_t)id); 1316228753Smm if (pwent == NULL) { 1317228753Smm *name = NULL; 1318228753Smm if (errno != 0 && errno != ENOENT) 1319238856Smm lafe_warnc(errno, "getpwuid(%s) failed", 1320238856Smm cpio_i64toa((int64_t)id)); 1321228753Smm return (errno); 1322228753Smm } 1323228753Smm 1324228753Smm *name = pwent->pw_name; 1325228753Smm return (0); 1326228753Smm} 1327228753Smm 1328228753Smmstatic const char * 1329228753Smmlookup_gname(struct cpio *cpio, gid_t gid) 1330228753Smm{ 1331228753Smm return (lookup_name(cpio, &cpio->gname_cache, 1332228753Smm &lookup_gname_helper, (id_t)gid)); 1333228753Smm} 1334228753Smm 1335228753Smmstatic int 1336228753Smmlookup_gname_helper(struct cpio *cpio, const char **name, id_t id) 1337228753Smm{ 1338228753Smm struct group *grent; 1339228753Smm 1340228753Smm (void)cpio; /* UNUSED */ 1341228753Smm 1342228753Smm errno = 0; 1343228753Smm grent = getgrgid((gid_t)id); 1344228753Smm if (grent == NULL) { 1345228753Smm *name = NULL; 1346228753Smm if (errno != 0) 1347238856Smm lafe_warnc(errno, "getgrgid(%s) failed", 1348238856Smm cpio_i64toa((int64_t)id)); 1349228753Smm return (errno); 1350228753Smm } 1351228753Smm 1352228753Smm *name = grent->gr_name; 1353228753Smm return (0); 1354228753Smm} 1355228753Smm 1356228753Smm/* 1357228753Smm * It would be nice to just use printf() for formatting large numbers, 1358228753Smm * but the compatibility problems are a big headache. Hence the 1359228753Smm * following simple utility function. 1360228753Smm */ 1361228753Smmconst char * 1362228753Smmcpio_i64toa(int64_t n0) 1363228753Smm{ 1364232153Smm /* 2^64 =~ 1.8 * 10^19, so 20 decimal digits suffice. 1365232153Smm * We also need 1 byte for '-' and 1 for '\0'. 1366232153Smm */ 1367228753Smm static char buff[22]; 1368228753Smm int64_t n = n0 < 0 ? -n0 : n0; 1369228753Smm char *p = buff + sizeof(buff); 1370228753Smm 1371228753Smm *--p = '\0'; 1372228753Smm do { 1373228753Smm *--p = '0' + (int)(n % 10); 1374228753Smm n /= 10; 1375228753Smm } while (n > 0); 1376228753Smm if (n0 < 0) 1377228753Smm *--p = '-'; 1378228753Smm return p; 1379228753Smm} 1380