xref: /dflybsd-src/contrib/libarchive/cpio/cpio.c (revision afd311f52496a4b5c3df02ea6d4bdab591886c60)
160b4ad09SPeter Avalos /*-
260b4ad09SPeter Avalos  * Copyright (c) 2003-2007 Tim Kientzle
360b4ad09SPeter Avalos  * All rights reserved.
460b4ad09SPeter Avalos  *
560b4ad09SPeter Avalos  * Redistribution and use in source and binary forms, with or without
660b4ad09SPeter Avalos  * modification, are permitted provided that the following conditions
760b4ad09SPeter Avalos  * are met:
860b4ad09SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
960b4ad09SPeter Avalos  *    notice, this list of conditions and the following disclaimer
1060b4ad09SPeter Avalos  *    in this position and unchanged.
1160b4ad09SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1260b4ad09SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1360b4ad09SPeter Avalos  *    documentation and/or other materials provided with the distribution.
1460b4ad09SPeter Avalos  *
1560b4ad09SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1660b4ad09SPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1760b4ad09SPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1860b4ad09SPeter Avalos  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1960b4ad09SPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2060b4ad09SPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2160b4ad09SPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2260b4ad09SPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2360b4ad09SPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2460b4ad09SPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2560b4ad09SPeter Avalos  */
2660b4ad09SPeter Avalos 
2760b4ad09SPeter Avalos 
2860b4ad09SPeter Avalos #include "cpio_platform.h"
298029ab02SPeter Avalos __FBSDID("$FreeBSD: src/usr.bin/cpio/cpio.c,v 1.15 2008/12/06 07:30:40 kientzle Exp $");
3060b4ad09SPeter Avalos 
3160b4ad09SPeter Avalos #include <sys/types.h>
3260b4ad09SPeter Avalos #include <archive.h>
3360b4ad09SPeter Avalos #include <archive_entry.h>
3460b4ad09SPeter Avalos 
358029ab02SPeter Avalos #ifdef HAVE_SYS_MKDEV_H
368029ab02SPeter Avalos #include <sys/mkdev.h>
378029ab02SPeter Avalos #endif
3860b4ad09SPeter Avalos #ifdef HAVE_SYS_STAT_H
3960b4ad09SPeter Avalos #include <sys/stat.h>
4060b4ad09SPeter Avalos #endif
418029ab02SPeter Avalos #ifdef HAVE_SYS_TIME_H
428029ab02SPeter Avalos #include <sys/time.h>
438029ab02SPeter Avalos #endif
4460b4ad09SPeter Avalos #ifdef HAVE_ERRNO_H
4560b4ad09SPeter Avalos #include <errno.h>
4660b4ad09SPeter Avalos #endif
4760b4ad09SPeter Avalos #ifdef HAVE_FCNTL_H
4860b4ad09SPeter Avalos #include <fcntl.h>
4960b4ad09SPeter Avalos #endif
508029ab02SPeter Avalos #ifdef HAVE_GRP_H
518029ab02SPeter Avalos #include <grp.h>
528029ab02SPeter Avalos #endif
53c09f92d2SPeter Avalos #ifdef HAVE_LOCALE_H
54c09f92d2SPeter Avalos #include <locale.h>
55c09f92d2SPeter Avalos #endif
568029ab02SPeter Avalos #ifdef HAVE_PWD_H
578029ab02SPeter Avalos #include <pwd.h>
588029ab02SPeter Avalos #endif
59c09f92d2SPeter Avalos #ifdef HAVE_SIGNAL_H
60c09f92d2SPeter Avalos #include <signal.h>
61c09f92d2SPeter Avalos #endif
6260b4ad09SPeter Avalos #ifdef HAVE_STDARG_H
6360b4ad09SPeter Avalos #include <stdarg.h>
6460b4ad09SPeter Avalos #endif
659c82a63eSPeter Avalos #ifdef HAVE_STDINT_H
669c82a63eSPeter Avalos #include <stdint.h>
679c82a63eSPeter Avalos #endif
6860b4ad09SPeter Avalos #include <stdio.h>
6960b4ad09SPeter Avalos #ifdef HAVE_STDLIB_H
7060b4ad09SPeter Avalos #include <stdlib.h>
7160b4ad09SPeter Avalos #endif
7260b4ad09SPeter Avalos #ifdef HAVE_STRING_H
7360b4ad09SPeter Avalos #include <string.h>
7460b4ad09SPeter Avalos #endif
7560b4ad09SPeter Avalos #ifdef HAVE_UNISTD_H
7660b4ad09SPeter Avalos #include <unistd.h>
7760b4ad09SPeter Avalos #endif
788029ab02SPeter Avalos #ifdef HAVE_TIME_H
798029ab02SPeter Avalos #include <time.h>
808029ab02SPeter Avalos #endif
8160b4ad09SPeter Avalos 
8260b4ad09SPeter Avalos #include "cpio.h"
839c82a63eSPeter Avalos #include "err.h"
849c82a63eSPeter Avalos #include "line_reader.h"
856b384f39SPeter Avalos #include "passphrase.h"
8660b4ad09SPeter Avalos 
878029ab02SPeter Avalos /* Fixed size of uname/gname caches. */
888029ab02SPeter Avalos #define	name_cache_size 101
898029ab02SPeter Avalos 
909c82a63eSPeter Avalos #ifndef O_BINARY
919c82a63eSPeter Avalos #define O_BINARY 0
929c82a63eSPeter Avalos #endif
939c82a63eSPeter Avalos 
948029ab02SPeter Avalos struct name_cache {
958029ab02SPeter Avalos 	int	probes;
968029ab02SPeter Avalos 	int	hits;
978029ab02SPeter Avalos 	size_t	size;
988029ab02SPeter Avalos 	struct {
998029ab02SPeter Avalos 		id_t id;
1008029ab02SPeter Avalos 		char *name;
1018029ab02SPeter Avalos 	} cache[name_cache_size];
1028029ab02SPeter Avalos };
1038029ab02SPeter Avalos 
1049c82a63eSPeter Avalos static int	extract_data(struct archive *, struct archive *);
1059c82a63eSPeter Avalos const char *	cpio_i64toa(int64_t);
10660b4ad09SPeter Avalos static const char *cpio_rename(const char *name);
10760b4ad09SPeter Avalos static int	entry_to_archive(struct cpio *, struct archive_entry *);
10860b4ad09SPeter Avalos static int	file_to_archive(struct cpio *, const char *);
1098029ab02SPeter Avalos static void	free_cache(struct name_cache *cache);
1108029ab02SPeter Avalos static void	list_item_verbose(struct cpio *, struct archive_entry *);
111e95abc47Szrj static void	long_help(void) __LA_DEAD;
1128029ab02SPeter Avalos static const char *lookup_gname(struct cpio *, gid_t gid);
1138029ab02SPeter Avalos static int	lookup_gname_helper(struct cpio *,
1148029ab02SPeter Avalos 		    const char **name, id_t gid);
1158029ab02SPeter Avalos static const char *lookup_uname(struct cpio *, uid_t uid);
1168029ab02SPeter Avalos static int	lookup_uname_helper(struct cpio *,
1178029ab02SPeter Avalos 		    const char **name, id_t uid);
118e95abc47Szrj static void	mode_in(struct cpio *) __LA_DEAD;
119e95abc47Szrj static void	mode_list(struct cpio *) __LA_DEAD;
12060b4ad09SPeter Avalos static void	mode_out(struct cpio *);
12160b4ad09SPeter Avalos static void	mode_pass(struct cpio *, const char *);
12259bf7050SPeter Avalos static const char *remove_leading_slash(const char *);
1238029ab02SPeter Avalos static int	restore_time(struct cpio *, struct archive_entry *,
12460b4ad09SPeter Avalos 		    const char *, int fd);
125e95abc47Szrj static void	usage(void) __LA_DEAD;
126e95abc47Szrj static void	version(void) __LA_DEAD;
1276b384f39SPeter Avalos static const char * passphrase_callback(struct archive *, void *);
1286b384f39SPeter Avalos static void	passphrase_free(char *);
12960b4ad09SPeter Avalos 
13060b4ad09SPeter Avalos int
main(int argc,char * argv[])13160b4ad09SPeter Avalos main(int argc, char *argv[])
13260b4ad09SPeter Avalos {
13360b4ad09SPeter Avalos 	static char buff[16384];
13460b4ad09SPeter Avalos 	struct cpio _cpio; /* Allocated on stack. */
13560b4ad09SPeter Avalos 	struct cpio *cpio;
1369c82a63eSPeter Avalos 	const char *errmsg;
137085658deSDaniel Fojt 	char *tptr;
13860b4ad09SPeter Avalos 	int uid, gid;
139085658deSDaniel Fojt 	int opt, t;
14060b4ad09SPeter Avalos 
14160b4ad09SPeter Avalos 	cpio = &_cpio;
14260b4ad09SPeter Avalos 	memset(cpio, 0, sizeof(*cpio));
14360b4ad09SPeter Avalos 	cpio->buff = buff;
14460b4ad09SPeter Avalos 	cpio->buff_size = sizeof(buff);
14560b4ad09SPeter Avalos 
146c09f92d2SPeter Avalos #if defined(HAVE_SIGACTION) && defined(SIGPIPE)
147c09f92d2SPeter Avalos 	{ /* Ignore SIGPIPE signals. */
148c09f92d2SPeter Avalos 		struct sigaction sa;
149c09f92d2SPeter Avalos 		sigemptyset(&sa.sa_mask);
150c09f92d2SPeter Avalos 		sa.sa_flags = 0;
151c09f92d2SPeter Avalos 		sa.sa_handler = SIG_IGN;
152c09f92d2SPeter Avalos 		sigaction(SIGPIPE, &sa, NULL);
153c09f92d2SPeter Avalos 	}
154c09f92d2SPeter Avalos #endif
155c09f92d2SPeter Avalos 
1566b384f39SPeter Avalos 	/* Set lafe_progname before calling lafe_warnc. */
1576b384f39SPeter Avalos 	lafe_setprogname(*argv, "bsdcpio");
1586b384f39SPeter Avalos 
159c09f92d2SPeter Avalos #if HAVE_SETLOCALE
160c09f92d2SPeter Avalos 	if (setlocale(LC_ALL, "") == NULL)
161c09f92d2SPeter Avalos 		lafe_warnc(0, "Failed to set default locale");
162c09f92d2SPeter Avalos #endif
16360b4ad09SPeter Avalos 
16460b4ad09SPeter Avalos 	cpio->uid_override = -1;
16560b4ad09SPeter Avalos 	cpio->gid_override = -1;
16660b4ad09SPeter Avalos 	cpio->argv = argv;
16760b4ad09SPeter Avalos 	cpio->argc = argc;
16860b4ad09SPeter Avalos 	cpio->mode = '\0';
16960b4ad09SPeter Avalos 	cpio->verbose = 0;
17060b4ad09SPeter Avalos 	cpio->compress = '\0';
17160b4ad09SPeter Avalos 	cpio->extract_flags = ARCHIVE_EXTRACT_NO_AUTODIR;
17260b4ad09SPeter Avalos 	cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
17360b4ad09SPeter Avalos 	cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS;
17460b4ad09SPeter Avalos 	cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT;
1756b384f39SPeter Avalos 	cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
1768029ab02SPeter Avalos 	cpio->extract_flags |= ARCHIVE_EXTRACT_PERM;
1778029ab02SPeter Avalos 	cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
1788029ab02SPeter Avalos 	cpio->extract_flags |= ARCHIVE_EXTRACT_ACL;
1799c82a63eSPeter Avalos #if !defined(_WIN32) && !defined(__CYGWIN__)
1808029ab02SPeter Avalos 	if (geteuid() == 0)
1818029ab02SPeter Avalos 		cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
1829c82a63eSPeter Avalos #endif
18360b4ad09SPeter Avalos 	cpio->bytes_per_block = 512;
18460b4ad09SPeter Avalos 	cpio->filename = NULL;
18560b4ad09SPeter Avalos 
18659bf7050SPeter Avalos 	cpio->matching = archive_match_new();
18759bf7050SPeter Avalos 	if (cpio->matching == NULL)
18859bf7050SPeter Avalos 		lafe_errc(1, 0, "Out of memory");
18959bf7050SPeter Avalos 
19060b4ad09SPeter Avalos 	while ((opt = cpio_getopt(cpio)) != -1) {
19160b4ad09SPeter Avalos 		switch (opt) {
19260b4ad09SPeter Avalos 		case '0': /* GNU convention: --null, -0 */
1939c82a63eSPeter Avalos 			cpio->option_null = 1;
19460b4ad09SPeter Avalos 			break;
195*50f8aa9cSAntonio Huete Jimenez 		case '6': /* in/out: assume/create 6th edition (PWB) format */
196*50f8aa9cSAntonio Huete Jimenez 			cpio->option_pwb = 1;
197*50f8aa9cSAntonio Huete Jimenez 			break;
198*50f8aa9cSAntonio Huete Jimenez 		case '7': /* out: create archive using 7th Edition binary format */
199*50f8aa9cSAntonio Huete Jimenez 			cpio->format = "bin";
200*50f8aa9cSAntonio Huete Jimenez 			break;
20160b4ad09SPeter Avalos 		case 'A': /* NetBSD/OpenBSD */
20260b4ad09SPeter Avalos 			cpio->option_append = 1;
20360b4ad09SPeter Avalos 			break;
20460b4ad09SPeter Avalos 		case 'a': /* POSIX 1997 */
20560b4ad09SPeter Avalos 			cpio->option_atime_restore = 1;
20660b4ad09SPeter Avalos 			break;
20760b4ad09SPeter Avalos 		case 'B': /* POSIX 1997 */
20860b4ad09SPeter Avalos 			cpio->bytes_per_block = 5120;
20960b4ad09SPeter Avalos 			break;
210d4d8193eSPeter Avalos 		case OPTION_B64ENCODE:
211d4d8193eSPeter Avalos 			cpio->add_filter = opt;
212d4d8193eSPeter Avalos 			break;
21360b4ad09SPeter Avalos 		case 'C': /* NetBSD/OpenBSD */
214085658deSDaniel Fojt 			errno = 0;
215085658deSDaniel Fojt 			tptr = NULL;
216085658deSDaniel Fojt 			t = (int)strtol(cpio->argument, &tptr, 10);
217085658deSDaniel Fojt 			if (errno || t <= 0 || *(cpio->argument) == '\0' ||
218085658deSDaniel Fojt 			    tptr == NULL || *tptr != '\0') {
219085658deSDaniel Fojt 				lafe_errc(1, 0, "Invalid blocksize: %s",
220085658deSDaniel Fojt 				    cpio->argument);
221085658deSDaniel Fojt 			}
222085658deSDaniel Fojt 			cpio->bytes_per_block = t;
22360b4ad09SPeter Avalos 			break;
22460b4ad09SPeter Avalos 		case 'c': /* POSIX 1997 */
22560b4ad09SPeter Avalos 			cpio->format = "odc";
22660b4ad09SPeter Avalos 			break;
22760b4ad09SPeter Avalos 		case 'd': /* POSIX 1997 */
22860b4ad09SPeter Avalos 			cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR;
22960b4ad09SPeter Avalos 			break;
23060b4ad09SPeter Avalos 		case 'E': /* NetBSD/OpenBSD */
23159bf7050SPeter Avalos 			if (archive_match_include_pattern_from_file(
23259bf7050SPeter Avalos 			    cpio->matching, cpio->argument,
23359bf7050SPeter Avalos 			    cpio->option_null) != ARCHIVE_OK)
23459bf7050SPeter Avalos 				lafe_errc(1, 0, "Error : %s",
23559bf7050SPeter Avalos 				    archive_error_string(cpio->matching));
23660b4ad09SPeter Avalos 			break;
23760b4ad09SPeter Avalos 		case 'F': /* NetBSD/OpenBSD/GNU cpio */
238c09f92d2SPeter Avalos 			cpio->filename = cpio->argument;
23960b4ad09SPeter Avalos 			break;
24060b4ad09SPeter Avalos 		case 'f': /* POSIX 1997 */
24159bf7050SPeter Avalos 			if (archive_match_exclude_pattern(cpio->matching,
24259bf7050SPeter Avalos 			    cpio->argument) != ARCHIVE_OK)
24359bf7050SPeter Avalos 				lafe_errc(1, 0, "Error : %s",
24459bf7050SPeter Avalos 				    archive_error_string(cpio->matching));
24560b4ad09SPeter Avalos 			break;
246d4d8193eSPeter Avalos 		case OPTION_GRZIP:
247d4d8193eSPeter Avalos 			cpio->compress = opt;
248d4d8193eSPeter Avalos 			break;
24960b4ad09SPeter Avalos 		case 'H': /* GNU cpio (also --format) */
250c09f92d2SPeter Avalos 			cpio->format = cpio->argument;
25160b4ad09SPeter Avalos 			break;
25260b4ad09SPeter Avalos 		case 'h':
25360b4ad09SPeter Avalos 			long_help();
25460b4ad09SPeter Avalos 			break;
25560b4ad09SPeter Avalos 		case 'I': /* NetBSD/OpenBSD */
256c09f92d2SPeter Avalos 			cpio->filename = cpio->argument;
25760b4ad09SPeter Avalos 			break;
25860b4ad09SPeter Avalos 		case 'i': /* POSIX 1997 */
2598029ab02SPeter Avalos 			if (cpio->mode != '\0')
2609c82a63eSPeter Avalos 				lafe_errc(1, 0,
2618029ab02SPeter Avalos 				    "Cannot use both -i and -%c", cpio->mode);
26260b4ad09SPeter Avalos 			cpio->mode = opt;
26360b4ad09SPeter Avalos 			break;
2649c82a63eSPeter Avalos 		case 'J': /* GNU tar, others */
2659c82a63eSPeter Avalos 			cpio->compress = opt;
2669c82a63eSPeter Avalos 			break;
2679c82a63eSPeter Avalos 		case 'j': /* GNU tar, others */
2689c82a63eSPeter Avalos 			cpio->compress = opt;
2699c82a63eSPeter Avalos 			break;
27060b4ad09SPeter Avalos 		case OPTION_INSECURE:
27160b4ad09SPeter Avalos 			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS;
27260b4ad09SPeter Avalos 			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
2736b384f39SPeter Avalos 			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
27460b4ad09SPeter Avalos 			break;
27560b4ad09SPeter Avalos 		case 'L': /* GNU cpio */
27660b4ad09SPeter Avalos 			cpio->option_follow_links = 1;
27760b4ad09SPeter Avalos 			break;
27860b4ad09SPeter Avalos 		case 'l': /* POSIX 1997 */
27960b4ad09SPeter Avalos 			cpio->option_link = 1;
28060b4ad09SPeter Avalos 			break;
281d4d8193eSPeter Avalos 		case OPTION_LRZIP:
2826b384f39SPeter Avalos 		case OPTION_LZ4:
2839c82a63eSPeter Avalos 		case OPTION_LZMA: /* GNU tar, others */
284d4d8193eSPeter Avalos 		case OPTION_LZOP: /* GNU tar, others */
285e95abc47Szrj 		case OPTION_ZSTD:
2869c82a63eSPeter Avalos 			cpio->compress = opt;
2879c82a63eSPeter Avalos 			break;
28860b4ad09SPeter Avalos 		case 'm': /* POSIX 1997 */
28960b4ad09SPeter Avalos 			cpio->extract_flags |= ARCHIVE_EXTRACT_TIME;
29060b4ad09SPeter Avalos 			break;
2918029ab02SPeter Avalos 		case 'n': /* GNU cpio */
2928029ab02SPeter Avalos 			cpio->option_numeric_uid_gid = 1;
2938029ab02SPeter Avalos 			break;
2948029ab02SPeter Avalos 		case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */
2958029ab02SPeter Avalos 			cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
2968029ab02SPeter Avalos 			break;
29760b4ad09SPeter Avalos 		case 'O': /* GNU cpio */
298c09f92d2SPeter Avalos 			cpio->filename = cpio->argument;
29960b4ad09SPeter Avalos 			break;
30060b4ad09SPeter Avalos 		case 'o': /* POSIX 1997 */
3018029ab02SPeter Avalos 			if (cpio->mode != '\0')
3029c82a63eSPeter Avalos 				lafe_errc(1, 0,
3038029ab02SPeter Avalos 				    "Cannot use both -o and -%c", cpio->mode);
30460b4ad09SPeter Avalos 			cpio->mode = opt;
30560b4ad09SPeter Avalos 			break;
30660b4ad09SPeter Avalos 		case 'p': /* POSIX 1997 */
3078029ab02SPeter Avalos 			if (cpio->mode != '\0')
3089c82a63eSPeter Avalos 				lafe_errc(1, 0,
3098029ab02SPeter Avalos 				    "Cannot use both -p and -%c", cpio->mode);
31060b4ad09SPeter Avalos 			cpio->mode = opt;
31160b4ad09SPeter Avalos 			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
312761afed6SPeter Avalos 			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
31360b4ad09SPeter Avalos 			break;
3146b384f39SPeter Avalos 		case OPTION_PASSPHRASE:
3156b384f39SPeter Avalos 			cpio->passphrase = cpio->argument;
3166b384f39SPeter Avalos 			break;
3179c82a63eSPeter Avalos 		case OPTION_PRESERVE_OWNER:
3189c82a63eSPeter Avalos 			cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
3199c82a63eSPeter Avalos 			break;
32060b4ad09SPeter Avalos 		case OPTION_QUIET: /* GNU cpio */
32160b4ad09SPeter Avalos 			cpio->quiet = 1;
32260b4ad09SPeter Avalos 			break;
32360b4ad09SPeter Avalos 		case 'R': /* GNU cpio, also --owner */
324c09f92d2SPeter Avalos 			/* TODO: owner_parse should return uname/gname
325c09f92d2SPeter Avalos 			 * also; use that to set [ug]name_override. */
326c09f92d2SPeter Avalos 			errmsg = owner_parse(cpio->argument, &uid, &gid);
3279c82a63eSPeter Avalos 			if (errmsg) {
3289c82a63eSPeter Avalos 				lafe_warnc(-1, "%s", errmsg);
32960b4ad09SPeter Avalos 				usage();
3309c82a63eSPeter Avalos 			}
331c09f92d2SPeter Avalos 			if (uid != -1) {
33260b4ad09SPeter Avalos 				cpio->uid_override = uid;
333c09f92d2SPeter Avalos 				cpio->uname_override = NULL;
334c09f92d2SPeter Avalos 			}
335c09f92d2SPeter Avalos 			if (gid != -1) {
33660b4ad09SPeter Avalos 				cpio->gid_override = gid;
337c09f92d2SPeter Avalos 				cpio->gname_override = NULL;
338c09f92d2SPeter Avalos 			}
33960b4ad09SPeter Avalos 			break;
34060b4ad09SPeter Avalos 		case 'r': /* POSIX 1997 */
34160b4ad09SPeter Avalos 			cpio->option_rename = 1;
34260b4ad09SPeter Avalos 			break;
34360b4ad09SPeter Avalos 		case 't': /* POSIX 1997 */
34460b4ad09SPeter Avalos 			cpio->option_list = 1;
34560b4ad09SPeter Avalos 			break;
34660b4ad09SPeter Avalos 		case 'u': /* POSIX 1997 */
34760b4ad09SPeter Avalos 			cpio->extract_flags
34860b4ad09SPeter Avalos 			    &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
34960b4ad09SPeter Avalos 			break;
350d4d8193eSPeter Avalos 		case OPTION_UUENCODE:
351d4d8193eSPeter Avalos 			cpio->add_filter = opt;
352d4d8193eSPeter Avalos 			break;
35360b4ad09SPeter Avalos 		case 'v': /* POSIX 1997 */
35460b4ad09SPeter Avalos 			cpio->verbose++;
35560b4ad09SPeter Avalos 			break;
356c09f92d2SPeter Avalos 		case 'V': /* GNU cpio */
357c09f92d2SPeter Avalos 			cpio->dot++;
358c09f92d2SPeter Avalos 			break;
35960b4ad09SPeter Avalos 		case OPTION_VERSION: /* GNU convention */
36060b4ad09SPeter Avalos 			version();
36160b4ad09SPeter Avalos 			break;
36260b4ad09SPeter Avalos #if 0
36360b4ad09SPeter Avalos 	        /*
36460b4ad09SPeter Avalos 		 * cpio_getopt() handles -W specially, so it's not
36560b4ad09SPeter Avalos 		 * available here.
36660b4ad09SPeter Avalos 		 */
36760b4ad09SPeter Avalos 		case 'W': /* Obscure, but useful GNU convention. */
36860b4ad09SPeter Avalos 			break;
36960b4ad09SPeter Avalos #endif
37060b4ad09SPeter Avalos 		case 'y': /* tar convention */
37160b4ad09SPeter Avalos 			cpio->compress = opt;
37260b4ad09SPeter Avalos 			break;
37360b4ad09SPeter Avalos 		case 'Z': /* tar convention */
37460b4ad09SPeter Avalos 			cpio->compress = opt;
37560b4ad09SPeter Avalos 			break;
37660b4ad09SPeter Avalos 		case 'z': /* tar convention */
37760b4ad09SPeter Avalos 			cpio->compress = opt;
37860b4ad09SPeter Avalos 			break;
37960b4ad09SPeter Avalos 		default:
38060b4ad09SPeter Avalos 			usage();
38160b4ad09SPeter Avalos 		}
38260b4ad09SPeter Avalos 	}
38360b4ad09SPeter Avalos 
3848029ab02SPeter Avalos 	/*
3858029ab02SPeter Avalos 	 * Sanity-check args, error out on nonsensical combinations.
3868029ab02SPeter Avalos 	 */
3878029ab02SPeter Avalos 	/* -t implies -i if no mode was specified. */
3888029ab02SPeter Avalos 	if (cpio->option_list && cpio->mode == '\0')
3898029ab02SPeter Avalos 		cpio->mode = 'i';
3908029ab02SPeter Avalos 	/* -t requires -i */
3918029ab02SPeter Avalos 	if (cpio->option_list && cpio->mode != 'i')
3929c82a63eSPeter Avalos 		lafe_errc(1, 0, "Option -t requires -i");
3938029ab02SPeter Avalos 	/* -n requires -it */
3948029ab02SPeter Avalos 	if (cpio->option_numeric_uid_gid && !cpio->option_list)
3959c82a63eSPeter Avalos 		lafe_errc(1, 0, "Option -n requires -it");
3968029ab02SPeter Avalos 	/* Can only specify format when writing */
3978029ab02SPeter Avalos 	if (cpio->format != NULL && cpio->mode != 'o')
3989c82a63eSPeter Avalos 		lafe_errc(1, 0, "Option --format requires -o");
3998029ab02SPeter Avalos 	/* -l requires -p */
4008029ab02SPeter Avalos 	if (cpio->option_link && cpio->mode != 'p')
4019c82a63eSPeter Avalos 		lafe_errc(1, 0, "Option -l requires -p");
402c09f92d2SPeter Avalos 	/* -v overrides -V */
403c09f92d2SPeter Avalos 	if (cpio->dot && cpio->verbose)
404c09f92d2SPeter Avalos 		cpio->dot = 0;
4058029ab02SPeter Avalos 	/* TODO: Flag other nonsensical combinations. */
40660b4ad09SPeter Avalos 
40760b4ad09SPeter Avalos 	switch (cpio->mode) {
40860b4ad09SPeter Avalos 	case 'o':
409*50f8aa9cSAntonio Huete Jimenez 		if (cpio->format == NULL) {
410*50f8aa9cSAntonio Huete Jimenez 			if (cpio->option_pwb)
411*50f8aa9cSAntonio Huete Jimenez 				cpio->format = "pwb";
412*50f8aa9cSAntonio Huete Jimenez 			else
413*50f8aa9cSAntonio Huete Jimenez 				cpio->format = "cpio";
414*50f8aa9cSAntonio Huete Jimenez 		}
41560b4ad09SPeter Avalos 		mode_out(cpio);
41660b4ad09SPeter Avalos 		break;
41760b4ad09SPeter Avalos 	case 'i':
41860b4ad09SPeter Avalos 		while (*cpio->argv != NULL) {
41959bf7050SPeter Avalos 			if (archive_match_include_pattern(cpio->matching,
42059bf7050SPeter Avalos 			    *cpio->argv) != ARCHIVE_OK)
42159bf7050SPeter Avalos 				lafe_errc(1, 0, "Error : %s",
42259bf7050SPeter Avalos 				    archive_error_string(cpio->matching));
42360b4ad09SPeter Avalos 			--cpio->argc;
42460b4ad09SPeter Avalos 			++cpio->argv;
42560b4ad09SPeter Avalos 		}
42660b4ad09SPeter Avalos 		if (cpio->option_list)
42760b4ad09SPeter Avalos 			mode_list(cpio);
42860b4ad09SPeter Avalos 		else
42960b4ad09SPeter Avalos 			mode_in(cpio);
43060b4ad09SPeter Avalos 		break;
43160b4ad09SPeter Avalos 	case 'p':
43260b4ad09SPeter Avalos 		if (*cpio->argv == NULL || **cpio->argv == '\0')
4339c82a63eSPeter Avalos 			lafe_errc(1, 0,
43460b4ad09SPeter Avalos 			    "-p mode requires a target directory");
43560b4ad09SPeter Avalos 		mode_pass(cpio, *cpio->argv);
43660b4ad09SPeter Avalos 		break;
43760b4ad09SPeter Avalos 	default:
4389c82a63eSPeter Avalos 		lafe_errc(1, 0,
43960b4ad09SPeter Avalos 		    "Must specify at least one of -i, -o, or -p");
44060b4ad09SPeter Avalos 	}
44160b4ad09SPeter Avalos 
44259bf7050SPeter Avalos 	archive_match_free(cpio->matching);
4438029ab02SPeter Avalos 	free_cache(cpio->gname_cache);
4448029ab02SPeter Avalos 	free_cache(cpio->uname_cache);
445d4d8193eSPeter Avalos 	free(cpio->destdir);
4466b384f39SPeter Avalos 	passphrase_free(cpio->ppbuff);
4479c82a63eSPeter Avalos 	return (cpio->return_value);
44860b4ad09SPeter Avalos }
44960b4ad09SPeter Avalos 
4509c82a63eSPeter Avalos static void
usage(void)45160b4ad09SPeter Avalos usage(void)
45260b4ad09SPeter Avalos {
45360b4ad09SPeter Avalos 	const char	*p;
45460b4ad09SPeter Avalos 
4556b384f39SPeter Avalos 	p = lafe_getprogname();
45660b4ad09SPeter Avalos 
45760b4ad09SPeter Avalos 	fprintf(stderr, "Brief Usage:\n");
45860b4ad09SPeter Avalos 	fprintf(stderr, "  List:    %s -it < archive\n", p);
45960b4ad09SPeter Avalos 	fprintf(stderr, "  Extract: %s -i < archive\n", p);
46060b4ad09SPeter Avalos 	fprintf(stderr, "  Create:  %s -o < filenames > archive\n", p);
46160b4ad09SPeter Avalos 	fprintf(stderr, "  Help:    %s --help\n", p);
46260b4ad09SPeter Avalos 	exit(1);
46360b4ad09SPeter Avalos }
46460b4ad09SPeter Avalos 
46560b4ad09SPeter Avalos static const char *long_help_msg =
46660b4ad09SPeter Avalos 	"First option must be a mode specifier:\n"
46760b4ad09SPeter Avalos 	"  -i Input  -o Output  -p Pass\n"
46860b4ad09SPeter Avalos 	"Common Options:\n"
469c09f92d2SPeter Avalos 	"  -v Verbose filenames     -V  one dot per file\n"
47060b4ad09SPeter Avalos 	"Create: %p -o [options]  < [list of files] > [archive]\n"
4719c82a63eSPeter Avalos 	"  -J,-y,-z,--lzma  Compress archive with xz/bzip2/gzip/lzma\n"
472*50f8aa9cSAntonio Huete Jimenez 	"  --format {pwb|bin|odc|newc|ustar}  Select archive format\n"
47360b4ad09SPeter Avalos 	"List: %p -it < [archive]\n"
47460b4ad09SPeter Avalos 	"Extract: %p -i [options] < [archive]\n";
47560b4ad09SPeter Avalos 
47660b4ad09SPeter Avalos 
47760b4ad09SPeter Avalos /*
47860b4ad09SPeter Avalos  * Note that the word 'bsdcpio' will always appear in the first line
47960b4ad09SPeter Avalos  * of output.
48060b4ad09SPeter Avalos  *
48160b4ad09SPeter Avalos  * In particular, /bin/sh scripts that need to test for the presence
48260b4ad09SPeter Avalos  * of bsdcpio can use the following template:
48360b4ad09SPeter Avalos  *
48460b4ad09SPeter Avalos  * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \
48560b4ad09SPeter Avalos  *          echo bsdcpio; else echo not bsdcpio; fi
48660b4ad09SPeter Avalos  */
48760b4ad09SPeter Avalos static void
long_help(void)48860b4ad09SPeter Avalos long_help(void)
48960b4ad09SPeter Avalos {
49060b4ad09SPeter Avalos 	const char	*prog;
49160b4ad09SPeter Avalos 	const char	*p;
49260b4ad09SPeter Avalos 
4936b384f39SPeter Avalos 	prog = lafe_getprogname();
49460b4ad09SPeter Avalos 
49560b4ad09SPeter Avalos 	fflush(stderr);
49660b4ad09SPeter Avalos 
49760b4ad09SPeter Avalos 	p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : "";
49860b4ad09SPeter Avalos 	printf("%s%s: manipulate archive files\n", prog, p);
49960b4ad09SPeter Avalos 
50060b4ad09SPeter Avalos 	for (p = long_help_msg; *p != '\0'; p++) {
50160b4ad09SPeter Avalos 		if (*p == '%') {
50260b4ad09SPeter Avalos 			if (p[1] == 'p') {
50360b4ad09SPeter Avalos 				fputs(prog, stdout);
50460b4ad09SPeter Avalos 				p++;
50560b4ad09SPeter Avalos 			} else
50660b4ad09SPeter Avalos 				putchar('%');
50760b4ad09SPeter Avalos 		} else
50860b4ad09SPeter Avalos 			putchar(*p);
50960b4ad09SPeter Avalos 	}
51060b4ad09SPeter Avalos 	version();
51160b4ad09SPeter Avalos }
51260b4ad09SPeter Avalos 
51360b4ad09SPeter Avalos static void
version(void)51460b4ad09SPeter Avalos version(void)
51560b4ad09SPeter Avalos {
516e95abc47Szrj 	fprintf(stdout,"bsdcpio %s - %s \n",
51760b4ad09SPeter Avalos 	    BSDCPIO_VERSION_STRING,
5186b384f39SPeter Avalos 	    archive_version_details());
51960b4ad09SPeter Avalos 	exit(0);
52060b4ad09SPeter Avalos }
52160b4ad09SPeter Avalos 
52260b4ad09SPeter Avalos static void
mode_out(struct cpio * cpio)52360b4ad09SPeter Avalos mode_out(struct cpio *cpio)
52460b4ad09SPeter Avalos {
52560b4ad09SPeter Avalos 	struct archive_entry *entry, *spare;
5269c82a63eSPeter Avalos 	struct lafe_line_reader *lr;
52760b4ad09SPeter Avalos 	const char *p;
52860b4ad09SPeter Avalos 	int r;
52960b4ad09SPeter Avalos 
53060b4ad09SPeter Avalos 	if (cpio->option_append)
5319c82a63eSPeter Avalos 		lafe_errc(1, 0, "Append mode not yet supported.");
5329c82a63eSPeter Avalos 
5339c82a63eSPeter Avalos 	cpio->archive_read_disk = archive_read_disk_new();
5349c82a63eSPeter Avalos 	if (cpio->archive_read_disk == NULL)
5359c82a63eSPeter Avalos 		lafe_errc(1, 0, "Failed to allocate archive object");
5369c82a63eSPeter Avalos 	if (cpio->option_follow_links)
5379c82a63eSPeter Avalos 		archive_read_disk_set_symlink_logical(cpio->archive_read_disk);
5389c82a63eSPeter Avalos 	else
5399c82a63eSPeter Avalos 		archive_read_disk_set_symlink_physical(cpio->archive_read_disk);
5409c82a63eSPeter Avalos 	archive_read_disk_set_standard_lookup(cpio->archive_read_disk);
5419c82a63eSPeter Avalos 
54260b4ad09SPeter Avalos 	cpio->archive = archive_write_new();
54360b4ad09SPeter Avalos 	if (cpio->archive == NULL)
5449c82a63eSPeter Avalos 		lafe_errc(1, 0, "Failed to allocate archive object");
54560b4ad09SPeter Avalos 	switch (cpio->compress) {
546d4d8193eSPeter Avalos 	case OPTION_GRZIP:
547d4d8193eSPeter Avalos 		r = archive_write_add_filter_grzip(cpio->archive);
548d4d8193eSPeter Avalos 		break;
5499c82a63eSPeter Avalos 	case 'J':
550d4d8193eSPeter Avalos 		r = archive_write_add_filter_xz(cpio->archive);
551d4d8193eSPeter Avalos 		break;
552d4d8193eSPeter Avalos 	case OPTION_LRZIP:
553d4d8193eSPeter Avalos 		r = archive_write_add_filter_lrzip(cpio->archive);
5549c82a63eSPeter Avalos 		break;
5556b384f39SPeter Avalos 	case OPTION_LZ4:
5566b384f39SPeter Avalos 		r = archive_write_add_filter_lz4(cpio->archive);
5576b384f39SPeter Avalos 		break;
5589c82a63eSPeter Avalos 	case OPTION_LZMA:
559d4d8193eSPeter Avalos 		r = archive_write_add_filter_lzma(cpio->archive);
560d4d8193eSPeter Avalos 		break;
561d4d8193eSPeter Avalos 	case OPTION_LZOP:
562d4d8193eSPeter Avalos 		r = archive_write_add_filter_lzop(cpio->archive);
5639c82a63eSPeter Avalos 		break;
564e95abc47Szrj 	case OPTION_ZSTD:
565e95abc47Szrj 		r = archive_write_add_filter_zstd(cpio->archive);
566e95abc47Szrj 		break;
56760b4ad09SPeter Avalos 	case 'j': case 'y':
568d4d8193eSPeter Avalos 		r = archive_write_add_filter_bzip2(cpio->archive);
56960b4ad09SPeter Avalos 		break;
57060b4ad09SPeter Avalos 	case 'z':
571d4d8193eSPeter Avalos 		r = archive_write_add_filter_gzip(cpio->archive);
57260b4ad09SPeter Avalos 		break;
57360b4ad09SPeter Avalos 	case 'Z':
574d4d8193eSPeter Avalos 		r = archive_write_add_filter_compress(cpio->archive);
57560b4ad09SPeter Avalos 		break;
57660b4ad09SPeter Avalos 	default:
577d4d8193eSPeter Avalos 		r = archive_write_add_filter_none(cpio->archive);
57860b4ad09SPeter Avalos 		break;
57960b4ad09SPeter Avalos 	}
5809c82a63eSPeter Avalos 	if (r < ARCHIVE_WARN)
5819c82a63eSPeter Avalos 		lafe_errc(1, 0, "Requested compression not available");
582d4d8193eSPeter Avalos 	switch (cpio->add_filter) {
583d4d8193eSPeter Avalos 	case 0:
584d4d8193eSPeter Avalos 		r = ARCHIVE_OK;
585d4d8193eSPeter Avalos 		break;
586d4d8193eSPeter Avalos 	case OPTION_B64ENCODE:
587d4d8193eSPeter Avalos 		r = archive_write_add_filter_b64encode(cpio->archive);
588d4d8193eSPeter Avalos 		break;
589d4d8193eSPeter Avalos 	case OPTION_UUENCODE:
590d4d8193eSPeter Avalos 		r = archive_write_add_filter_uuencode(cpio->archive);
591d4d8193eSPeter Avalos 		break;
592d4d8193eSPeter Avalos 	}
593d4d8193eSPeter Avalos 	if (r < ARCHIVE_WARN)
594d4d8193eSPeter Avalos 		lafe_errc(1, 0, "Requested filter not available");
59560b4ad09SPeter Avalos 	r = archive_write_set_format_by_name(cpio->archive, cpio->format);
59660b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
5979c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
59860b4ad09SPeter Avalos 	archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block);
59960b4ad09SPeter Avalos 	cpio->linkresolver = archive_entry_linkresolver_new();
60060b4ad09SPeter Avalos 	archive_entry_linkresolver_set_strategy(cpio->linkresolver,
60160b4ad09SPeter Avalos 	    archive_format(cpio->archive));
6026b384f39SPeter Avalos 	if (cpio->passphrase != NULL)
6036b384f39SPeter Avalos 		r = archive_write_set_passphrase(cpio->archive,
6046b384f39SPeter Avalos 			cpio->passphrase);
6056b384f39SPeter Avalos 	else
6066b384f39SPeter Avalos 		r = archive_write_set_passphrase_callback(cpio->archive, cpio,
6076b384f39SPeter Avalos 			&passphrase_callback);
6086b384f39SPeter Avalos 	if (r != ARCHIVE_OK)
6096b384f39SPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
61060b4ad09SPeter Avalos 
6119c82a63eSPeter Avalos 	/*
6129c82a63eSPeter Avalos 	 * The main loop:  Copy each file into the output archive.
6139c82a63eSPeter Avalos 	 */
614d4d8193eSPeter Avalos 	r = archive_write_open_filename(cpio->archive, cpio->filename);
61560b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
6169c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
6179c82a63eSPeter Avalos 	lr = lafe_line_reader("-", cpio->option_null);
6189c82a63eSPeter Avalos 	while ((p = lafe_line_reader_next(lr)) != NULL)
61960b4ad09SPeter Avalos 		file_to_archive(cpio, p);
6209c82a63eSPeter Avalos 	lafe_line_reader_free(lr);
62160b4ad09SPeter Avalos 
62260b4ad09SPeter Avalos 	/*
62360b4ad09SPeter Avalos 	 * The hardlink detection may have queued up a couple of entries
62460b4ad09SPeter Avalos 	 * that can now be flushed.
62560b4ad09SPeter Avalos 	 */
62660b4ad09SPeter Avalos 	entry = NULL;
62760b4ad09SPeter Avalos 	archive_entry_linkify(cpio->linkresolver, &entry, &spare);
62860b4ad09SPeter Avalos 	while (entry != NULL) {
62960b4ad09SPeter Avalos 		entry_to_archive(cpio, entry);
63060b4ad09SPeter Avalos 		archive_entry_free(entry);
63160b4ad09SPeter Avalos 		entry = NULL;
63260b4ad09SPeter Avalos 		archive_entry_linkify(cpio->linkresolver, &entry, &spare);
63360b4ad09SPeter Avalos 	}
63460b4ad09SPeter Avalos 
63560b4ad09SPeter Avalos 	r = archive_write_close(cpio->archive);
636c09f92d2SPeter Avalos 	if (cpio->dot)
637c09f92d2SPeter Avalos 		fprintf(stderr, "\n");
63860b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
6399c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
64060b4ad09SPeter Avalos 
64160b4ad09SPeter Avalos 	if (!cpio->quiet) {
6429c82a63eSPeter Avalos 		int64_t blocks =
643d4d8193eSPeter Avalos 			(archive_filter_bytes(cpio->archive, 0) + 511)
64460b4ad09SPeter Avalos 			/ 512;
6459c82a63eSPeter Avalos 		fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
64660b4ad09SPeter Avalos 		    blocks == 1 ? "block" : "blocks");
64760b4ad09SPeter Avalos 	}
648c09f92d2SPeter Avalos 	archive_write_free(cpio->archive);
649e95abc47Szrj 	archive_entry_linkresolver_free(cpio->linkresolver);
65060b4ad09SPeter Avalos }
65160b4ad09SPeter Avalos 
65259bf7050SPeter Avalos static const char *
remove_leading_slash(const char * p)65359bf7050SPeter Avalos remove_leading_slash(const char *p)
65459bf7050SPeter Avalos {
65559bf7050SPeter Avalos 	const char *rp;
65659bf7050SPeter Avalos 
65759bf7050SPeter Avalos 	/* Remove leading "//./" or "//?/" or "//?/UNC/"
65859bf7050SPeter Avalos 	 * (absolute path prefixes used by Windows API) */
65959bf7050SPeter Avalos 	if ((p[0] == '/' || p[0] == '\\') &&
66059bf7050SPeter Avalos 	    (p[1] == '/' || p[1] == '\\') &&
66159bf7050SPeter Avalos 	    (p[2] == '.' || p[2] == '?') &&
66259bf7050SPeter Avalos 	    (p[3] == '/' || p[3] == '\\'))
66359bf7050SPeter Avalos 	{
66459bf7050SPeter Avalos 		if (p[2] == '?' &&
66559bf7050SPeter Avalos 		    (p[4] == 'U' || p[4] == 'u') &&
66659bf7050SPeter Avalos 		    (p[5] == 'N' || p[5] == 'n') &&
66759bf7050SPeter Avalos 		    (p[6] == 'C' || p[6] == 'c') &&
66859bf7050SPeter Avalos 		    (p[7] == '/' || p[7] == '\\'))
66959bf7050SPeter Avalos 			p += 8;
67059bf7050SPeter Avalos 		else
67159bf7050SPeter Avalos 			p += 4;
67259bf7050SPeter Avalos 	}
67359bf7050SPeter Avalos 	do {
67459bf7050SPeter Avalos 		rp = p;
67559bf7050SPeter Avalos 		/* Remove leading drive letter from archives created
67659bf7050SPeter Avalos 		 * on Windows. */
67759bf7050SPeter Avalos 		if (((p[0] >= 'a' && p[0] <= 'z') ||
67859bf7050SPeter Avalos 		     (p[0] >= 'A' && p[0] <= 'Z')) &&
67959bf7050SPeter Avalos 			 p[1] == ':') {
68059bf7050SPeter Avalos 			p += 2;
68159bf7050SPeter Avalos 		}
68259bf7050SPeter Avalos 		/* Remove leading "/../", "//", etc. */
68359bf7050SPeter Avalos 		while (p[0] == '/' || p[0] == '\\') {
68459bf7050SPeter Avalos 			if (p[1] == '.' && p[2] == '.' &&
68559bf7050SPeter Avalos 				(p[3] == '/' || p[3] == '\\')) {
68659bf7050SPeter Avalos 				p += 3; /* Remove "/..", leave "/"
68759bf7050SPeter Avalos 					 * for next pass. */
68859bf7050SPeter Avalos 			} else
68959bf7050SPeter Avalos 				p += 1; /* Remove "/". */
69059bf7050SPeter Avalos 		}
69159bf7050SPeter Avalos 	} while (rp != p);
69259bf7050SPeter Avalos 	return (p);
69359bf7050SPeter Avalos }
69459bf7050SPeter Avalos 
69560b4ad09SPeter Avalos /*
69660b4ad09SPeter Avalos  * This is used by both out mode (to copy objects from disk into
69760b4ad09SPeter Avalos  * an archive) and pass mode (to copy objects from disk to
69860b4ad09SPeter Avalos  * an archive_write_disk "archive").
69960b4ad09SPeter Avalos  */
70060b4ad09SPeter Avalos static int
file_to_archive(struct cpio * cpio,const char * srcpath)70160b4ad09SPeter Avalos file_to_archive(struct cpio *cpio, const char *srcpath)
70260b4ad09SPeter Avalos {
70360b4ad09SPeter Avalos 	const char *destpath;
70460b4ad09SPeter Avalos 	struct archive_entry *entry, *spare;
70560b4ad09SPeter Avalos 	size_t len;
70660b4ad09SPeter Avalos 	int r;
70760b4ad09SPeter Avalos 
70860b4ad09SPeter Avalos 	/*
70960b4ad09SPeter Avalos 	 * Create an archive_entry describing the source file.
7108029ab02SPeter Avalos 	 *
71160b4ad09SPeter Avalos 	 */
71260b4ad09SPeter Avalos 	entry = archive_entry_new();
71360b4ad09SPeter Avalos 	if (entry == NULL)
7149c82a63eSPeter Avalos 		lafe_errc(1, 0, "Couldn't allocate entry");
71560b4ad09SPeter Avalos 	archive_entry_copy_sourcepath(entry, srcpath);
7169c82a63eSPeter Avalos 	r = archive_read_disk_entry_from_file(cpio->archive_read_disk,
7179c82a63eSPeter Avalos 	    entry, -1, NULL);
7189c82a63eSPeter Avalos 	if (r < ARCHIVE_FAILED)
7199c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s",
7209c82a63eSPeter Avalos 		    archive_error_string(cpio->archive_read_disk));
7219c82a63eSPeter Avalos 	if (r < ARCHIVE_OK)
7229c82a63eSPeter Avalos 		lafe_warnc(0, "%s",
7239c82a63eSPeter Avalos 		    archive_error_string(cpio->archive_read_disk));
7249c82a63eSPeter Avalos 	if (r <= ARCHIVE_FAILED) {
725e95abc47Szrj 		archive_entry_free(entry);
7269c82a63eSPeter Avalos 		cpio->return_value = 1;
7279c82a63eSPeter Avalos 		return (r);
72860b4ad09SPeter Avalos 	}
72960b4ad09SPeter Avalos 
730c09f92d2SPeter Avalos 	if (cpio->uid_override >= 0) {
7319c82a63eSPeter Avalos 		archive_entry_set_uid(entry, cpio->uid_override);
732c09f92d2SPeter Avalos 		archive_entry_set_uname(entry, cpio->uname_override);
733c09f92d2SPeter Avalos 	}
734c09f92d2SPeter Avalos 	if (cpio->gid_override >= 0) {
7359c82a63eSPeter Avalos 		archive_entry_set_gid(entry, cpio->gid_override);
736c09f92d2SPeter Avalos 		archive_entry_set_gname(entry, cpio->gname_override);
737c09f92d2SPeter Avalos 	}
73860b4ad09SPeter Avalos 
73960b4ad09SPeter Avalos 	/*
74060b4ad09SPeter Avalos 	 * Generate a destination path for this entry.
74160b4ad09SPeter Avalos 	 * "destination path" is the name to which it will be copied in
74260b4ad09SPeter Avalos 	 * pass mode or the name that will go into the archive in
74360b4ad09SPeter Avalos 	 * output mode.
74460b4ad09SPeter Avalos 	 */
74560b4ad09SPeter Avalos 	destpath = srcpath;
74660b4ad09SPeter Avalos 	if (cpio->destdir) {
747085658deSDaniel Fojt 		len = cpio->destdir_len + strlen(srcpath) + 8;
74860b4ad09SPeter Avalos 		if (len >= cpio->pass_destpath_alloc) {
74960b4ad09SPeter Avalos 			while (len >= cpio->pass_destpath_alloc) {
75060b4ad09SPeter Avalos 				cpio->pass_destpath_alloc += 512;
75160b4ad09SPeter Avalos 				cpio->pass_destpath_alloc *= 2;
75260b4ad09SPeter Avalos 			}
75360b4ad09SPeter Avalos 			free(cpio->pass_destpath);
75460b4ad09SPeter Avalos 			cpio->pass_destpath = malloc(cpio->pass_destpath_alloc);
75560b4ad09SPeter Avalos 			if (cpio->pass_destpath == NULL)
7569c82a63eSPeter Avalos 				lafe_errc(1, ENOMEM,
75760b4ad09SPeter Avalos 				    "Can't allocate path buffer");
75860b4ad09SPeter Avalos 		}
75960b4ad09SPeter Avalos 		strcpy(cpio->pass_destpath, cpio->destdir);
76059bf7050SPeter Avalos 		strcat(cpio->pass_destpath, remove_leading_slash(srcpath));
76160b4ad09SPeter Avalos 		destpath = cpio->pass_destpath;
76260b4ad09SPeter Avalos 	}
76360b4ad09SPeter Avalos 	if (cpio->option_rename)
76460b4ad09SPeter Avalos 		destpath = cpio_rename(destpath);
765085658deSDaniel Fojt 	if (destpath == NULL) {
766085658deSDaniel Fojt 		archive_entry_free(entry);
76760b4ad09SPeter Avalos 		return (0);
768085658deSDaniel Fojt 	}
76960b4ad09SPeter Avalos 	archive_entry_copy_pathname(entry, destpath);
77060b4ad09SPeter Avalos 
77160b4ad09SPeter Avalos 	/*
77260b4ad09SPeter Avalos 	 * If we're trying to preserve hardlinks, match them here.
77360b4ad09SPeter Avalos 	 */
77460b4ad09SPeter Avalos 	spare = NULL;
77560b4ad09SPeter Avalos 	if (cpio->linkresolver != NULL
7769c82a63eSPeter Avalos 	    && archive_entry_filetype(entry) != AE_IFDIR) {
77760b4ad09SPeter Avalos 		archive_entry_linkify(cpio->linkresolver, &entry, &spare);
77860b4ad09SPeter Avalos 	}
77960b4ad09SPeter Avalos 
78060b4ad09SPeter Avalos 	if (entry != NULL) {
78160b4ad09SPeter Avalos 		r = entry_to_archive(cpio, entry);
78260b4ad09SPeter Avalos 		archive_entry_free(entry);
78360b4ad09SPeter Avalos 		if (spare != NULL) {
78460b4ad09SPeter Avalos 			if (r == 0)
78560b4ad09SPeter Avalos 				r = entry_to_archive(cpio, spare);
78660b4ad09SPeter Avalos 			archive_entry_free(spare);
78760b4ad09SPeter Avalos 		}
7889c82a63eSPeter Avalos 	}
78960b4ad09SPeter Avalos 	return (r);
79060b4ad09SPeter Avalos }
79160b4ad09SPeter Avalos 
79260b4ad09SPeter Avalos static int
entry_to_archive(struct cpio * cpio,struct archive_entry * entry)79360b4ad09SPeter Avalos entry_to_archive(struct cpio *cpio, struct archive_entry *entry)
79460b4ad09SPeter Avalos {
79560b4ad09SPeter Avalos 	const char *destpath = archive_entry_pathname(entry);
79660b4ad09SPeter Avalos 	const char *srcpath = archive_entry_sourcepath(entry);
79760b4ad09SPeter Avalos 	int fd = -1;
79860b4ad09SPeter Avalos 	ssize_t bytes_read;
79960b4ad09SPeter Avalos 	int r;
80060b4ad09SPeter Avalos 
80160b4ad09SPeter Avalos 	/* Print out the destination name to the user. */
80260b4ad09SPeter Avalos 	if (cpio->verbose)
80360b4ad09SPeter Avalos 		fprintf(stderr,"%s", destpath);
804c09f92d2SPeter Avalos 	if (cpio->dot)
805c09f92d2SPeter Avalos 		fprintf(stderr, ".");
80660b4ad09SPeter Avalos 
80760b4ad09SPeter Avalos 	/*
80860b4ad09SPeter Avalos 	 * Option_link only makes sense in pass mode and for
80960b4ad09SPeter Avalos 	 * regular files.  Also note: if a link operation fails
81060b4ad09SPeter Avalos 	 * because of cross-device restrictions, we'll fall back
81160b4ad09SPeter Avalos 	 * to copy mode for that entry.
81260b4ad09SPeter Avalos 	 *
81360b4ad09SPeter Avalos 	 * TODO: Test other cpio implementations to see if they
81460b4ad09SPeter Avalos 	 * hard-link anything other than regular files here.
81560b4ad09SPeter Avalos 	 */
81660b4ad09SPeter Avalos 	if (cpio->option_link
81760b4ad09SPeter Avalos 	    && archive_entry_filetype(entry) == AE_IFREG)
81860b4ad09SPeter Avalos 	{
81960b4ad09SPeter Avalos 		struct archive_entry *t;
82060b4ad09SPeter Avalos 		/* Save the original entry in case we need it later. */
82160b4ad09SPeter Avalos 		t = archive_entry_clone(entry);
82260b4ad09SPeter Avalos 		if (t == NULL)
8239c82a63eSPeter Avalos 			lafe_errc(1, ENOMEM, "Can't create link");
82460b4ad09SPeter Avalos 		/* Note: link(2) doesn't create parent directories,
82560b4ad09SPeter Avalos 		 * so we use archive_write_header() instead as a
82660b4ad09SPeter Avalos 		 * convenience. */
82760b4ad09SPeter Avalos 		archive_entry_set_hardlink(t, srcpath);
82860b4ad09SPeter Avalos 		/* This is a straight link that carries no data. */
82960b4ad09SPeter Avalos 		archive_entry_set_size(t, 0);
83060b4ad09SPeter Avalos 		r = archive_write_header(cpio->archive, t);
83160b4ad09SPeter Avalos 		archive_entry_free(t);
83260b4ad09SPeter Avalos 		if (r != ARCHIVE_OK)
8339c82a63eSPeter Avalos 			lafe_warnc(archive_errno(cpio->archive),
8349c82a63eSPeter Avalos 			    "%s", archive_error_string(cpio->archive));
83560b4ad09SPeter Avalos 		if (r == ARCHIVE_FATAL)
83660b4ad09SPeter Avalos 			exit(1);
83760b4ad09SPeter Avalos #ifdef EXDEV
83860b4ad09SPeter Avalos 		if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) {
83960b4ad09SPeter Avalos 			/* Cross-device link:  Just fall through and use
84060b4ad09SPeter Avalos 			 * the original entry to copy the file over. */
8419c82a63eSPeter Avalos 			lafe_warnc(0, "Copying file instead");
84260b4ad09SPeter Avalos 		} else
84360b4ad09SPeter Avalos #endif
84460b4ad09SPeter Avalos 		return (0);
84560b4ad09SPeter Avalos 	}
84660b4ad09SPeter Avalos 
84760b4ad09SPeter Avalos 	/*
84860b4ad09SPeter Avalos 	 * Make sure we can open the file (if necessary) before
84960b4ad09SPeter Avalos 	 * trying to write the header.
85060b4ad09SPeter Avalos 	 */
85160b4ad09SPeter Avalos 	if (archive_entry_filetype(entry) == AE_IFREG) {
85260b4ad09SPeter Avalos 		if (archive_entry_size(entry) > 0) {
8539c82a63eSPeter Avalos 			fd = open(srcpath, O_RDONLY | O_BINARY);
85460b4ad09SPeter Avalos 			if (fd < 0) {
8559c82a63eSPeter Avalos 				lafe_warnc(errno,
85660b4ad09SPeter Avalos 				    "%s: could not open file", srcpath);
85760b4ad09SPeter Avalos 				goto cleanup;
85860b4ad09SPeter Avalos 			}
85960b4ad09SPeter Avalos 		}
86060b4ad09SPeter Avalos 	} else {
86160b4ad09SPeter Avalos 		archive_entry_set_size(entry, 0);
86260b4ad09SPeter Avalos 	}
86360b4ad09SPeter Avalos 
86460b4ad09SPeter Avalos 	r = archive_write_header(cpio->archive, entry);
86560b4ad09SPeter Avalos 
86660b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
8679c82a63eSPeter Avalos 		lafe_warnc(archive_errno(cpio->archive),
86860b4ad09SPeter Avalos 		    "%s: %s",
8698029ab02SPeter Avalos 		    srcpath,
87060b4ad09SPeter Avalos 		    archive_error_string(cpio->archive));
87160b4ad09SPeter Avalos 
87260b4ad09SPeter Avalos 	if (r == ARCHIVE_FATAL)
87360b4ad09SPeter Avalos 		exit(1);
87460b4ad09SPeter Avalos 
875c09f92d2SPeter Avalos 	if (r >= ARCHIVE_WARN && archive_entry_size(entry) > 0 && fd >= 0) {
876d4d8193eSPeter Avalos 		bytes_read = read(fd, cpio->buff, (unsigned)cpio->buff_size);
87760b4ad09SPeter Avalos 		while (bytes_read > 0) {
878d4d8193eSPeter Avalos 			ssize_t bytes_write;
879d4d8193eSPeter Avalos 			bytes_write = archive_write_data(cpio->archive,
88060b4ad09SPeter Avalos 			    cpio->buff, bytes_read);
881d4d8193eSPeter Avalos 			if (bytes_write < 0)
8829c82a63eSPeter Avalos 				lafe_errc(1, archive_errno(cpio->archive),
8839c82a63eSPeter Avalos 				    "%s", archive_error_string(cpio->archive));
884d4d8193eSPeter Avalos 			if (bytes_write < bytes_read) {
8859c82a63eSPeter Avalos 				lafe_warnc(0,
886d4d8193eSPeter Avalos 				    "Truncated write; file may have "
887d4d8193eSPeter Avalos 				    "grown while being archived.");
88860b4ad09SPeter Avalos 			}
889d4d8193eSPeter Avalos 			bytes_read = read(fd, cpio->buff,
890d4d8193eSPeter Avalos 			    (unsigned)cpio->buff_size);
89160b4ad09SPeter Avalos 		}
89260b4ad09SPeter Avalos 	}
89360b4ad09SPeter Avalos 
8948029ab02SPeter Avalos 	fd = restore_time(cpio, entry, srcpath, fd);
89560b4ad09SPeter Avalos 
89660b4ad09SPeter Avalos cleanup:
89760b4ad09SPeter Avalos 	if (cpio->verbose)
89860b4ad09SPeter Avalos 		fprintf(stderr,"\n");
89960b4ad09SPeter Avalos 	if (fd >= 0)
90060b4ad09SPeter Avalos 		close(fd);
90160b4ad09SPeter Avalos 	return (0);
90260b4ad09SPeter Avalos }
90360b4ad09SPeter Avalos 
9048029ab02SPeter Avalos static int
restore_time(struct cpio * cpio,struct archive_entry * entry,const char * name,int fd)90560b4ad09SPeter Avalos restore_time(struct cpio *cpio, struct archive_entry *entry,
90660b4ad09SPeter Avalos     const char *name, int fd)
90760b4ad09SPeter Avalos {
90860b4ad09SPeter Avalos #ifndef HAVE_UTIMES
90960b4ad09SPeter Avalos 	static int warned = 0;
91060b4ad09SPeter Avalos 
91160b4ad09SPeter Avalos 	(void)cpio; /* UNUSED */
91260b4ad09SPeter Avalos 	(void)entry; /* UNUSED */
91360b4ad09SPeter Avalos 	(void)name; /* UNUSED */
91460b4ad09SPeter Avalos 
91560b4ad09SPeter Avalos 	if (!warned)
9169c82a63eSPeter Avalos 		lafe_warnc(0, "Can't restore access times on this platform");
91760b4ad09SPeter Avalos 	warned = 1;
9188029ab02SPeter Avalos 	return (fd);
9198029ab02SPeter Avalos #else
9208029ab02SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
9218029ab02SPeter Avalos 	struct __timeval times[2];
92260b4ad09SPeter Avalos #else
92360b4ad09SPeter Avalos 	struct timeval times[2];
9248029ab02SPeter Avalos #endif
92560b4ad09SPeter Avalos 
92660b4ad09SPeter Avalos 	if (!cpio->option_atime_restore)
9278029ab02SPeter Avalos 		return (fd);
92860b4ad09SPeter Avalos 
92960b4ad09SPeter Avalos         times[1].tv_sec = archive_entry_mtime(entry);
93060b4ad09SPeter Avalos         times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000;
93160b4ad09SPeter Avalos 
93260b4ad09SPeter Avalos         times[0].tv_sec = archive_entry_atime(entry);
93360b4ad09SPeter Avalos         times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000;
93460b4ad09SPeter Avalos 
9359c82a63eSPeter Avalos #if defined(HAVE_FUTIMES) && !defined(__CYGWIN__)
93660b4ad09SPeter Avalos         if (fd >= 0 && futimes(fd, times) == 0)
9378029ab02SPeter Avalos 		return (fd);
93860b4ad09SPeter Avalos #endif
9398029ab02SPeter Avalos 	/*
9408029ab02SPeter Avalos 	 * Some platform cannot restore access times if the file descriptor
9418029ab02SPeter Avalos 	 * is still opened.
9428029ab02SPeter Avalos 	 */
9438029ab02SPeter Avalos 	if (fd >= 0) {
9448029ab02SPeter Avalos 		close(fd);
9458029ab02SPeter Avalos 		fd = -1;
9468029ab02SPeter Avalos 	}
94760b4ad09SPeter Avalos 
94860b4ad09SPeter Avalos #ifdef HAVE_LUTIMES
94960b4ad09SPeter Avalos         if (lutimes(name, times) != 0)
95060b4ad09SPeter Avalos #else
9519c82a63eSPeter Avalos         if ((AE_IFLNK != archive_entry_filetype(entry))
9529c82a63eSPeter Avalos 			&& utimes(name, times) != 0)
95360b4ad09SPeter Avalos #endif
9549c82a63eSPeter Avalos                 lafe_warnc(errno, "Can't update time for %s", name);
95560b4ad09SPeter Avalos #endif
9568029ab02SPeter Avalos 	return (fd);
95760b4ad09SPeter Avalos }
95860b4ad09SPeter Avalos 
95960b4ad09SPeter Avalos 
96060b4ad09SPeter Avalos static void
mode_in(struct cpio * cpio)96160b4ad09SPeter Avalos mode_in(struct cpio *cpio)
96260b4ad09SPeter Avalos {
96360b4ad09SPeter Avalos 	struct archive *a;
96460b4ad09SPeter Avalos 	struct archive_entry *entry;
96560b4ad09SPeter Avalos 	struct archive *ext;
96660b4ad09SPeter Avalos 	const char *destpath;
96760b4ad09SPeter Avalos 	int r;
96860b4ad09SPeter Avalos 
96960b4ad09SPeter Avalos 	ext = archive_write_disk_new();
97060b4ad09SPeter Avalos 	if (ext == NULL)
9719c82a63eSPeter Avalos 		lafe_errc(1, 0, "Couldn't allocate restore object");
97260b4ad09SPeter Avalos 	r = archive_write_disk_set_options(ext, cpio->extract_flags);
97360b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
9749c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(ext));
97560b4ad09SPeter Avalos 	a = archive_read_new();
97660b4ad09SPeter Avalos 	if (a == NULL)
9779c82a63eSPeter Avalos 		lafe_errc(1, 0, "Couldn't allocate archive object");
978c09f92d2SPeter Avalos 	archive_read_support_filter_all(a);
97960b4ad09SPeter Avalos 	archive_read_support_format_all(a);
980*50f8aa9cSAntonio Huete Jimenez 	if (cpio->option_pwb)
981*50f8aa9cSAntonio Huete Jimenez 		archive_read_set_options(a, "pwb");
9826b384f39SPeter Avalos 	if (cpio->passphrase != NULL)
9836b384f39SPeter Avalos 		r = archive_read_add_passphrase(a, cpio->passphrase);
9846b384f39SPeter Avalos 	else
9856b384f39SPeter Avalos 		r = archive_read_set_passphrase_callback(a, cpio,
9866b384f39SPeter Avalos 			&passphrase_callback);
9876b384f39SPeter Avalos 	if (r != ARCHIVE_OK)
9886b384f39SPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(a));
98960b4ad09SPeter Avalos 
990d4d8193eSPeter Avalos 	if (archive_read_open_filename(a, cpio->filename,
991d4d8193eSPeter Avalos 					cpio->bytes_per_block))
9929c82a63eSPeter Avalos 		lafe_errc(1, archive_errno(a),
9939c82a63eSPeter Avalos 		    "%s", archive_error_string(a));
99460b4ad09SPeter Avalos 	for (;;) {
99560b4ad09SPeter Avalos 		r = archive_read_next_header(a, &entry);
99660b4ad09SPeter Avalos 		if (r == ARCHIVE_EOF)
99760b4ad09SPeter Avalos 			break;
99860b4ad09SPeter Avalos 		if (r != ARCHIVE_OK) {
9999c82a63eSPeter Avalos 			lafe_errc(1, archive_errno(a),
10009c82a63eSPeter Avalos 			    "%s", archive_error_string(a));
100160b4ad09SPeter Avalos 		}
100259bf7050SPeter Avalos 		if (archive_match_path_excluded(cpio->matching, entry))
100360b4ad09SPeter Avalos 			continue;
100460b4ad09SPeter Avalos 		if (cpio->option_rename) {
100560b4ad09SPeter Avalos 			destpath = cpio_rename(archive_entry_pathname(entry));
100660b4ad09SPeter Avalos 			archive_entry_set_pathname(entry, destpath);
100760b4ad09SPeter Avalos 		} else
100860b4ad09SPeter Avalos 			destpath = archive_entry_pathname(entry);
100960b4ad09SPeter Avalos 		if (destpath == NULL)
101060b4ad09SPeter Avalos 			continue;
101160b4ad09SPeter Avalos 		if (cpio->verbose)
1012c09f92d2SPeter Avalos 			fprintf(stderr, "%s\n", destpath);
1013c09f92d2SPeter Avalos 		if (cpio->dot)
1014c09f92d2SPeter Avalos 			fprintf(stderr, ".");
101560b4ad09SPeter Avalos 		if (cpio->uid_override >= 0)
101660b4ad09SPeter Avalos 			archive_entry_set_uid(entry, cpio->uid_override);
101760b4ad09SPeter Avalos 		if (cpio->gid_override >= 0)
101860b4ad09SPeter Avalos 			archive_entry_set_gid(entry, cpio->gid_override);
101960b4ad09SPeter Avalos 		r = archive_write_header(ext, entry);
102060b4ad09SPeter Avalos 		if (r != ARCHIVE_OK) {
102160b4ad09SPeter Avalos 			fprintf(stderr, "%s: %s\n",
102260b4ad09SPeter Avalos 			    archive_entry_pathname(entry),
102360b4ad09SPeter Avalos 			    archive_error_string(ext));
102455c601bbSPeter Avalos 		} else if (!archive_entry_size_is_set(entry)
102555c601bbSPeter Avalos 		    || archive_entry_size(entry) > 0) {
10269c82a63eSPeter Avalos 			r = extract_data(a, ext);
10279c82a63eSPeter Avalos 			if (r != ARCHIVE_OK)
10289c82a63eSPeter Avalos 				cpio->return_value = 1;
102960b4ad09SPeter Avalos 		}
103060b4ad09SPeter Avalos 	}
103160b4ad09SPeter Avalos 	r = archive_read_close(a);
1032c09f92d2SPeter Avalos 	if (cpio->dot)
1033c09f92d2SPeter Avalos 		fprintf(stderr, "\n");
103460b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
10359c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(a));
103660b4ad09SPeter Avalos 	r = archive_write_close(ext);
103760b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
10389c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(ext));
103960b4ad09SPeter Avalos 	if (!cpio->quiet) {
1040d4d8193eSPeter Avalos 		int64_t blocks = (archive_filter_bytes(a, 0) + 511)
104160b4ad09SPeter Avalos 			      / 512;
10429c82a63eSPeter Avalos 		fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
104360b4ad09SPeter Avalos 		    blocks == 1 ? "block" : "blocks");
104460b4ad09SPeter Avalos 	}
1045c09f92d2SPeter Avalos 	archive_read_free(a);
1046c09f92d2SPeter Avalos 	archive_write_free(ext);
10479c82a63eSPeter Avalos 	exit(cpio->return_value);
104860b4ad09SPeter Avalos }
104960b4ad09SPeter Avalos 
10509c82a63eSPeter Avalos /*
10519c82a63eSPeter Avalos  * Exits if there's a fatal error.  Returns ARCHIVE_OK
10529c82a63eSPeter Avalos  * if everything is kosher.
10539c82a63eSPeter Avalos  */
105460b4ad09SPeter Avalos static int
extract_data(struct archive * ar,struct archive * aw)10559c82a63eSPeter Avalos extract_data(struct archive *ar, struct archive *aw)
105660b4ad09SPeter Avalos {
105760b4ad09SPeter Avalos 	int r;
105860b4ad09SPeter Avalos 	size_t size;
105960b4ad09SPeter Avalos 	const void *block;
1060c09f92d2SPeter Avalos 	int64_t offset;
106160b4ad09SPeter Avalos 
106260b4ad09SPeter Avalos 	for (;;) {
106360b4ad09SPeter Avalos 		r = archive_read_data_block(ar, &block, &size, &offset);
106460b4ad09SPeter Avalos 		if (r == ARCHIVE_EOF)
106560b4ad09SPeter Avalos 			return (ARCHIVE_OK);
106660b4ad09SPeter Avalos 		if (r != ARCHIVE_OK) {
10679c82a63eSPeter Avalos 			lafe_warnc(archive_errno(ar),
106860b4ad09SPeter Avalos 			    "%s", archive_error_string(ar));
10699c82a63eSPeter Avalos 			exit(1);
107060b4ad09SPeter Avalos 		}
1071d4d8193eSPeter Avalos 		r = (int)archive_write_data_block(aw, block, size, offset);
107260b4ad09SPeter Avalos 		if (r != ARCHIVE_OK) {
10739c82a63eSPeter Avalos 			lafe_warnc(archive_errno(aw),
10749c82a63eSPeter Avalos 			    "%s", archive_error_string(aw));
107560b4ad09SPeter Avalos 			return (r);
107660b4ad09SPeter Avalos 		}
107760b4ad09SPeter Avalos 	}
107860b4ad09SPeter Avalos }
107960b4ad09SPeter Avalos 
108060b4ad09SPeter Avalos static void
mode_list(struct cpio * cpio)108160b4ad09SPeter Avalos mode_list(struct cpio *cpio)
108260b4ad09SPeter Avalos {
108360b4ad09SPeter Avalos 	struct archive *a;
108460b4ad09SPeter Avalos 	struct archive_entry *entry;
108560b4ad09SPeter Avalos 	int r;
108660b4ad09SPeter Avalos 
108760b4ad09SPeter Avalos 	a = archive_read_new();
108860b4ad09SPeter Avalos 	if (a == NULL)
10899c82a63eSPeter Avalos 		lafe_errc(1, 0, "Couldn't allocate archive object");
1090c09f92d2SPeter Avalos 	archive_read_support_filter_all(a);
109160b4ad09SPeter Avalos 	archive_read_support_format_all(a);
1092*50f8aa9cSAntonio Huete Jimenez 	if (cpio->option_pwb)
1093*50f8aa9cSAntonio Huete Jimenez 		archive_read_set_options(a, "pwb");
10946b384f39SPeter Avalos 	if (cpio->passphrase != NULL)
10956b384f39SPeter Avalos 		r = archive_read_add_passphrase(a, cpio->passphrase);
10966b384f39SPeter Avalos 	else
10976b384f39SPeter Avalos 		r = archive_read_set_passphrase_callback(a, cpio,
10986b384f39SPeter Avalos 			&passphrase_callback);
10996b384f39SPeter Avalos 	if (r != ARCHIVE_OK)
11006b384f39SPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(a));
110160b4ad09SPeter Avalos 
1102d4d8193eSPeter Avalos 	if (archive_read_open_filename(a, cpio->filename,
1103d4d8193eSPeter Avalos 					cpio->bytes_per_block))
11049c82a63eSPeter Avalos 		lafe_errc(1, archive_errno(a),
11059c82a63eSPeter Avalos 		    "%s", archive_error_string(a));
110660b4ad09SPeter Avalos 	for (;;) {
110760b4ad09SPeter Avalos 		r = archive_read_next_header(a, &entry);
110860b4ad09SPeter Avalos 		if (r == ARCHIVE_EOF)
110960b4ad09SPeter Avalos 			break;
111060b4ad09SPeter Avalos 		if (r != ARCHIVE_OK) {
11119c82a63eSPeter Avalos 			lafe_errc(1, archive_errno(a),
11129c82a63eSPeter Avalos 			    "%s", archive_error_string(a));
111360b4ad09SPeter Avalos 		}
111459bf7050SPeter Avalos 		if (archive_match_path_excluded(cpio->matching, entry))
111560b4ad09SPeter Avalos 			continue;
11168029ab02SPeter Avalos 		if (cpio->verbose)
11178029ab02SPeter Avalos 			list_item_verbose(cpio, entry);
11188029ab02SPeter Avalos 		else
111960b4ad09SPeter Avalos 			fprintf(stdout, "%s\n", archive_entry_pathname(entry));
112060b4ad09SPeter Avalos 	}
112160b4ad09SPeter Avalos 	r = archive_read_close(a);
112260b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
11239c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(a));
112460b4ad09SPeter Avalos 	if (!cpio->quiet) {
1125d4d8193eSPeter Avalos 		int64_t blocks = (archive_filter_bytes(a, 0) + 511)
112660b4ad09SPeter Avalos 			      / 512;
11279c82a63eSPeter Avalos 		fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
112860b4ad09SPeter Avalos 		    blocks == 1 ? "block" : "blocks");
112960b4ad09SPeter Avalos 	}
1130c09f92d2SPeter Avalos 	archive_read_free(a);
113160b4ad09SPeter Avalos 	exit(0);
113260b4ad09SPeter Avalos }
113360b4ad09SPeter Avalos 
11348029ab02SPeter Avalos /*
11358029ab02SPeter Avalos  * Display information about the current file.
11368029ab02SPeter Avalos  *
11378029ab02SPeter Avalos  * The format here roughly duplicates the output of 'ls -l'.
11388029ab02SPeter Avalos  * This is based on SUSv2, where 'tar tv' is documented as
11398029ab02SPeter Avalos  * listing additional information in an "unspecified format,"
11408029ab02SPeter Avalos  * and 'pax -l' is documented as using the same format as 'ls -l'.
11418029ab02SPeter Avalos  */
11428029ab02SPeter Avalos static void
list_item_verbose(struct cpio * cpio,struct archive_entry * entry)11438029ab02SPeter Avalos list_item_verbose(struct cpio *cpio, struct archive_entry *entry)
11448029ab02SPeter Avalos {
11458029ab02SPeter Avalos 	char			 size[32];
11468029ab02SPeter Avalos 	char			 date[32];
11478029ab02SPeter Avalos 	char			 uids[16], gids[16];
11488029ab02SPeter Avalos 	const char 		*uname, *gname;
11498029ab02SPeter Avalos 	FILE			*out = stdout;
11508029ab02SPeter Avalos 	const char		*fmt;
11519c82a63eSPeter Avalos 	time_t			 mtime;
11528029ab02SPeter Avalos 	static time_t		 now;
1153085658deSDaniel Fojt 	struct tm		*ltime;
1154085658deSDaniel Fojt #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
1155085658deSDaniel Fojt 	struct tm		tmbuf;
1156085658deSDaniel Fojt #endif
1157085658deSDaniel Fojt #if defined(HAVE__LOCALTIME64_S)
1158085658deSDaniel Fojt 	errno_t			terr;
1159085658deSDaniel Fojt 	__time64_t		tmptime;
1160085658deSDaniel Fojt #endif
11618029ab02SPeter Avalos 
11628029ab02SPeter Avalos 	if (!now)
11638029ab02SPeter Avalos 		time(&now);
11648029ab02SPeter Avalos 
11658029ab02SPeter Avalos 	if (cpio->option_numeric_uid_gid) {
11668029ab02SPeter Avalos 		/* Format numeric uid/gid for display. */
11679c82a63eSPeter Avalos 		strcpy(uids, cpio_i64toa(archive_entry_uid(entry)));
11688029ab02SPeter Avalos 		uname = uids;
11699c82a63eSPeter Avalos 		strcpy(gids, cpio_i64toa(archive_entry_gid(entry)));
11708029ab02SPeter Avalos 		gname = gids;
11718029ab02SPeter Avalos 	} else {
11728029ab02SPeter Avalos 		/* Use uname if it's present, else lookup name from uid. */
11738029ab02SPeter Avalos 		uname = archive_entry_uname(entry);
11748029ab02SPeter Avalos 		if (uname == NULL)
1175c09f92d2SPeter Avalos 			uname = lookup_uname(cpio, (uid_t)archive_entry_uid(entry));
11768029ab02SPeter Avalos 		/* Use gname if it's present, else lookup name from gid. */
11778029ab02SPeter Avalos 		gname = archive_entry_gname(entry);
11788029ab02SPeter Avalos 		if (gname == NULL)
1179c09f92d2SPeter Avalos 			gname = lookup_gname(cpio, (uid_t)archive_entry_gid(entry));
11808029ab02SPeter Avalos 	}
11818029ab02SPeter Avalos 
11828029ab02SPeter Avalos 	/* Print device number or file size. */
11839c82a63eSPeter Avalos 	if (archive_entry_filetype(entry) == AE_IFCHR
11849c82a63eSPeter Avalos 	    || archive_entry_filetype(entry) == AE_IFBLK) {
11858029ab02SPeter Avalos 		snprintf(size, sizeof(size), "%lu,%lu",
11869c82a63eSPeter Avalos 		    (unsigned long)archive_entry_rdevmajor(entry),
11879c82a63eSPeter Avalos 		    (unsigned long)archive_entry_rdevminor(entry));
11888029ab02SPeter Avalos 	} else {
11899c82a63eSPeter Avalos 		strcpy(size, cpio_i64toa(archive_entry_size(entry)));
11908029ab02SPeter Avalos 	}
11918029ab02SPeter Avalos 
11928029ab02SPeter Avalos 	/* Format the time using 'ls -l' conventions. */
11939c82a63eSPeter Avalos 	mtime = archive_entry_mtime(entry);
11948029ab02SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
11958029ab02SPeter Avalos 	/* Windows' strftime function does not support %e format. */
11969c82a63eSPeter Avalos 	if (mtime - now > 365*86400/2
11979c82a63eSPeter Avalos 		|| mtime - now < -365*86400/2)
11988029ab02SPeter Avalos 		fmt = cpio->day_first ? "%d %b  %Y" : "%b %d  %Y";
11998029ab02SPeter Avalos 	else
12008029ab02SPeter Avalos 		fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M";
12018029ab02SPeter Avalos #else
12026b384f39SPeter Avalos 	if (mtime - now > 365*86400/2
12036b384f39SPeter Avalos 		|| mtime - now < -365*86400/2)
12048029ab02SPeter Avalos 		fmt = cpio->day_first ? "%e %b  %Y" : "%b %e  %Y";
12058029ab02SPeter Avalos 	else
12068029ab02SPeter Avalos 		fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
12078029ab02SPeter Avalos #endif
1208085658deSDaniel Fojt #if defined(HAVE_LOCALTIME_R)
1209085658deSDaniel Fojt 	ltime = localtime_r(&mtime, &tmbuf);
1210085658deSDaniel Fojt #elif defined(HAVE__LOCALTIME64_S)
1211085658deSDaniel Fojt 	tmptime = mtime;
1212085658deSDaniel Fojt 	terr = _localtime64_s(&tmbuf, &tmptime);
1213085658deSDaniel Fojt 	if (terr)
1214085658deSDaniel Fojt 		ltime = NULL;
1215085658deSDaniel Fojt 	else
1216085658deSDaniel Fojt 		ltime = &tmbuf;
1217085658deSDaniel Fojt #else
1218085658deSDaniel Fojt 	ltime = localtime(&mtime);
1219085658deSDaniel Fojt #endif
1220085658deSDaniel Fojt 	strftime(date, sizeof(date), fmt, ltime);
12218029ab02SPeter Avalos 
12228029ab02SPeter Avalos 	fprintf(out, "%s%3d %-8s %-8s %8s %12s %s",
12238029ab02SPeter Avalos 	    archive_entry_strmode(entry),
12248029ab02SPeter Avalos 	    archive_entry_nlink(entry),
12258029ab02SPeter Avalos 	    uname, gname, size, date,
12268029ab02SPeter Avalos 	    archive_entry_pathname(entry));
12278029ab02SPeter Avalos 
12288029ab02SPeter Avalos 	/* Extra information for links. */
12298029ab02SPeter Avalos 	if (archive_entry_hardlink(entry)) /* Hard link */
12308029ab02SPeter Avalos 		fprintf(out, " link to %s", archive_entry_hardlink(entry));
12318029ab02SPeter Avalos 	else if (archive_entry_symlink(entry)) /* Symbolic link */
12328029ab02SPeter Avalos 		fprintf(out, " -> %s", archive_entry_symlink(entry));
12338029ab02SPeter Avalos 	fprintf(out, "\n");
12348029ab02SPeter Avalos }
12358029ab02SPeter Avalos 
123660b4ad09SPeter Avalos static void
mode_pass(struct cpio * cpio,const char * destdir)123760b4ad09SPeter Avalos mode_pass(struct cpio *cpio, const char *destdir)
123860b4ad09SPeter Avalos {
12399c82a63eSPeter Avalos 	struct lafe_line_reader *lr;
124060b4ad09SPeter Avalos 	const char *p;
124160b4ad09SPeter Avalos 	int r;
124260b4ad09SPeter Avalos 
124360b4ad09SPeter Avalos 	/* Ensure target dir has a trailing '/' to simplify path surgery. */
1244085658deSDaniel Fojt 	cpio->destdir_len = strlen(destdir);
1245085658deSDaniel Fojt 	cpio->destdir = malloc(cpio->destdir_len + 8);
1246085658deSDaniel Fojt 	memcpy(cpio->destdir, destdir, cpio->destdir_len);
1247085658deSDaniel Fojt 	if (cpio->destdir_len == 0 || destdir[cpio->destdir_len - 1] != '/')
1248085658deSDaniel Fojt 		cpio->destdir[cpio->destdir_len++] = '/';
1249085658deSDaniel Fojt 	cpio->destdir[cpio->destdir_len] = '\0';
125060b4ad09SPeter Avalos 
125160b4ad09SPeter Avalos 	cpio->archive = archive_write_disk_new();
125260b4ad09SPeter Avalos 	if (cpio->archive == NULL)
12539c82a63eSPeter Avalos 		lafe_errc(1, 0, "Failed to allocate archive object");
125460b4ad09SPeter Avalos 	r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags);
125560b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
12569c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
125760b4ad09SPeter Avalos 	cpio->linkresolver = archive_entry_linkresolver_new();
125860b4ad09SPeter Avalos 	archive_write_disk_set_standard_lookup(cpio->archive);
12599c82a63eSPeter Avalos 
12609c82a63eSPeter Avalos 	cpio->archive_read_disk = archive_read_disk_new();
12619c82a63eSPeter Avalos 	if (cpio->archive_read_disk == NULL)
12629c82a63eSPeter Avalos 		lafe_errc(1, 0, "Failed to allocate archive object");
12639c82a63eSPeter Avalos 	if (cpio->option_follow_links)
12649c82a63eSPeter Avalos 		archive_read_disk_set_symlink_logical(cpio->archive_read_disk);
12659c82a63eSPeter Avalos 	else
12669c82a63eSPeter Avalos 		archive_read_disk_set_symlink_physical(cpio->archive_read_disk);
12679c82a63eSPeter Avalos 	archive_read_disk_set_standard_lookup(cpio->archive_read_disk);
12689c82a63eSPeter Avalos 
12699c82a63eSPeter Avalos 	lr = lafe_line_reader("-", cpio->option_null);
12709c82a63eSPeter Avalos 	while ((p = lafe_line_reader_next(lr)) != NULL)
127160b4ad09SPeter Avalos 		file_to_archive(cpio, p);
12729c82a63eSPeter Avalos 	lafe_line_reader_free(lr);
127360b4ad09SPeter Avalos 
127460b4ad09SPeter Avalos 	archive_entry_linkresolver_free(cpio->linkresolver);
127560b4ad09SPeter Avalos 	r = archive_write_close(cpio->archive);
1276c09f92d2SPeter Avalos 	if (cpio->dot)
1277c09f92d2SPeter Avalos 		fprintf(stderr, "\n");
127860b4ad09SPeter Avalos 	if (r != ARCHIVE_OK)
12799c82a63eSPeter Avalos 		lafe_errc(1, 0, "%s", archive_error_string(cpio->archive));
12808029ab02SPeter Avalos 
12818029ab02SPeter Avalos 	if (!cpio->quiet) {
12829c82a63eSPeter Avalos 		int64_t blocks =
1283d4d8193eSPeter Avalos 			(archive_filter_bytes(cpio->archive, 0) + 511)
12848029ab02SPeter Avalos 			/ 512;
12859c82a63eSPeter Avalos 		fprintf(stderr, "%lu %s\n", (unsigned long)blocks,
12868029ab02SPeter Avalos 		    blocks == 1 ? "block" : "blocks");
12878029ab02SPeter Avalos 	}
12888029ab02SPeter Avalos 
1289c09f92d2SPeter Avalos 	archive_write_free(cpio->archive);
1290e95abc47Szrj 	free(cpio->pass_destpath);
129160b4ad09SPeter Avalos }
129260b4ad09SPeter Avalos 
129360b4ad09SPeter Avalos /*
129460b4ad09SPeter Avalos  * Prompt for a new name for this entry.  Returns a pointer to the
129560b4ad09SPeter Avalos  * new name or NULL if the entry should not be copied.  This
129660b4ad09SPeter Avalos  * implements the semantics defined in POSIX.1-1996, which specifies
129760b4ad09SPeter Avalos  * that an input of '.' means the name should be unchanged.  GNU cpio
129860b4ad09SPeter Avalos  * treats '.' as a literal new name.
129960b4ad09SPeter Avalos  */
130060b4ad09SPeter Avalos static const char *
cpio_rename(const char * name)130160b4ad09SPeter Avalos cpio_rename(const char *name)
130260b4ad09SPeter Avalos {
130360b4ad09SPeter Avalos 	static char buff[1024];
130460b4ad09SPeter Avalos 	FILE *t;
130560b4ad09SPeter Avalos 	char *p, *ret;
130659bf7050SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
130759bf7050SPeter Avalos 	FILE *to;
130860b4ad09SPeter Avalos 
130959bf7050SPeter Avalos 	t = fopen("CONIN$", "r");
131059bf7050SPeter Avalos 	if (t == NULL)
131159bf7050SPeter Avalos 		return (name);
131259bf7050SPeter Avalos 	to = fopen("CONOUT$", "w");
13136b384f39SPeter Avalos 	if (to == NULL) {
13146b384f39SPeter Avalos 		fclose(t);
131559bf7050SPeter Avalos 		return (name);
13166b384f39SPeter Avalos 	}
131759bf7050SPeter Avalos 	fprintf(to, "%s (Enter/./(new name))? ", name);
131859bf7050SPeter Avalos 	fclose(to);
131959bf7050SPeter Avalos #else
132060b4ad09SPeter Avalos 	t = fopen("/dev/tty", "r+");
132160b4ad09SPeter Avalos 	if (t == NULL)
132260b4ad09SPeter Avalos 		return (name);
132360b4ad09SPeter Avalos 	fprintf(t, "%s (Enter/./(new name))? ", name);
132460b4ad09SPeter Avalos 	fflush(t);
132559bf7050SPeter Avalos #endif
132660b4ad09SPeter Avalos 
132760b4ad09SPeter Avalos 	p = fgets(buff, sizeof(buff), t);
132860b4ad09SPeter Avalos 	fclose(t);
132960b4ad09SPeter Avalos 	if (p == NULL)
133060b4ad09SPeter Avalos 		/* End-of-file is a blank line. */
133160b4ad09SPeter Avalos 		return (NULL);
133260b4ad09SPeter Avalos 
133360b4ad09SPeter Avalos 	while (*p == ' ' || *p == '\t')
133460b4ad09SPeter Avalos 		++p;
133560b4ad09SPeter Avalos 	if (*p == '\n' || *p == '\0')
133660b4ad09SPeter Avalos 		/* Empty line. */
133760b4ad09SPeter Avalos 		return (NULL);
133860b4ad09SPeter Avalos 	if (*p == '.' && p[1] == '\n')
133960b4ad09SPeter Avalos 		/* Single period preserves original name. */
134060b4ad09SPeter Avalos 		return (name);
134160b4ad09SPeter Avalos 	ret = p;
134260b4ad09SPeter Avalos 	/* Trim the final newline. */
134360b4ad09SPeter Avalos 	while (*p != '\0' && *p != '\n')
134460b4ad09SPeter Avalos 		++p;
134560b4ad09SPeter Avalos 	/* Overwrite the final \n with a null character. */
134660b4ad09SPeter Avalos 	*p = '\0';
134760b4ad09SPeter Avalos 	return (ret);
134860b4ad09SPeter Avalos }
134960b4ad09SPeter Avalos 
13508029ab02SPeter Avalos static void
free_cache(struct name_cache * cache)13518029ab02SPeter Avalos free_cache(struct name_cache *cache)
13528029ab02SPeter Avalos {
13538029ab02SPeter Avalos 	size_t i;
13548029ab02SPeter Avalos 
13558029ab02SPeter Avalos 	if (cache != NULL) {
13568029ab02SPeter Avalos 		for (i = 0; i < cache->size; i++)
13578029ab02SPeter Avalos 			free(cache->cache[i].name);
13588029ab02SPeter Avalos 		free(cache);
13598029ab02SPeter Avalos 	}
13608029ab02SPeter Avalos }
13618029ab02SPeter Avalos 
13628029ab02SPeter Avalos /*
13638029ab02SPeter Avalos  * Lookup uname/gname from uid/gid, return NULL if no match.
13648029ab02SPeter Avalos  */
13658029ab02SPeter Avalos static const char *
lookup_name(struct cpio * cpio,struct name_cache ** name_cache_variable,int (* lookup_fn)(struct cpio *,const char **,id_t),id_t id)13668029ab02SPeter Avalos lookup_name(struct cpio *cpio, struct name_cache **name_cache_variable,
13678029ab02SPeter Avalos     int (*lookup_fn)(struct cpio *, const char **, id_t), id_t id)
13688029ab02SPeter Avalos {
13698029ab02SPeter Avalos 	char asnum[16];
13708029ab02SPeter Avalos 	struct name_cache	*cache;
13718029ab02SPeter Avalos 	const char *name;
13728029ab02SPeter Avalos 	int slot;
13738029ab02SPeter Avalos 
13748029ab02SPeter Avalos 
13758029ab02SPeter Avalos 	if (*name_cache_variable == NULL) {
1376e95abc47Szrj 		*name_cache_variable = calloc(1, sizeof(struct name_cache));
13778029ab02SPeter Avalos 		if (*name_cache_variable == NULL)
13789c82a63eSPeter Avalos 			lafe_errc(1, ENOMEM, "No more memory");
13798029ab02SPeter Avalos 		(*name_cache_variable)->size = name_cache_size;
13808029ab02SPeter Avalos 	}
13818029ab02SPeter Avalos 
13828029ab02SPeter Avalos 	cache = *name_cache_variable;
13838029ab02SPeter Avalos 	cache->probes++;
13848029ab02SPeter Avalos 
13858029ab02SPeter Avalos 	slot = id % cache->size;
13868029ab02SPeter Avalos 	if (cache->cache[slot].name != NULL) {
13878029ab02SPeter Avalos 		if (cache->cache[slot].id == id) {
13888029ab02SPeter Avalos 			cache->hits++;
13898029ab02SPeter Avalos 			return (cache->cache[slot].name);
13908029ab02SPeter Avalos 		}
13918029ab02SPeter Avalos 		free(cache->cache[slot].name);
13928029ab02SPeter Avalos 		cache->cache[slot].name = NULL;
13938029ab02SPeter Avalos 	}
13948029ab02SPeter Avalos 
1395e95abc47Szrj 	if (lookup_fn(cpio, &name, id)) {
13968029ab02SPeter Avalos 		/* If lookup failed, format it as a number. */
13978029ab02SPeter Avalos 		snprintf(asnum, sizeof(asnum), "%u", (unsigned)id);
13988029ab02SPeter Avalos 		name = asnum;
13998029ab02SPeter Avalos 	}
1400e95abc47Szrj 
14018029ab02SPeter Avalos 	cache->cache[slot].name = strdup(name);
14028029ab02SPeter Avalos 	if (cache->cache[slot].name != NULL) {
14038029ab02SPeter Avalos 		cache->cache[slot].id = id;
14048029ab02SPeter Avalos 		return (cache->cache[slot].name);
14058029ab02SPeter Avalos 	}
1406e95abc47Szrj 
14078029ab02SPeter Avalos 	/*
14088029ab02SPeter Avalos 	 * Conveniently, NULL marks an empty slot, so
14098029ab02SPeter Avalos 	 * if the strdup() fails, we've just failed to
14108029ab02SPeter Avalos 	 * cache it.  No recovery necessary.
14118029ab02SPeter Avalos 	 */
14128029ab02SPeter Avalos 	return (NULL);
14138029ab02SPeter Avalos }
14148029ab02SPeter Avalos 
14158029ab02SPeter Avalos static const char *
lookup_uname(struct cpio * cpio,uid_t uid)14168029ab02SPeter Avalos lookup_uname(struct cpio *cpio, uid_t uid)
14178029ab02SPeter Avalos {
14188029ab02SPeter Avalos 	return (lookup_name(cpio, &cpio->uname_cache,
14198029ab02SPeter Avalos 		    &lookup_uname_helper, (id_t)uid));
14208029ab02SPeter Avalos }
14218029ab02SPeter Avalos 
14228029ab02SPeter Avalos static int
lookup_uname_helper(struct cpio * cpio,const char ** name,id_t id)14238029ab02SPeter Avalos lookup_uname_helper(struct cpio *cpio, const char **name, id_t id)
14248029ab02SPeter Avalos {
14258029ab02SPeter Avalos 	struct passwd	*pwent;
14268029ab02SPeter Avalos 
14278029ab02SPeter Avalos 	(void)cpio; /* UNUSED */
14288029ab02SPeter Avalos 
14298029ab02SPeter Avalos 	errno = 0;
14308029ab02SPeter Avalos 	pwent = getpwuid((uid_t)id);
14318029ab02SPeter Avalos 	if (pwent == NULL) {
1432e95abc47Szrj 		if (errno && errno != ENOENT)
143359bf7050SPeter Avalos 			lafe_warnc(errno, "getpwuid(%s) failed",
143459bf7050SPeter Avalos 			    cpio_i64toa((int64_t)id));
1435e95abc47Szrj 		return 1;
14368029ab02SPeter Avalos 	}
14378029ab02SPeter Avalos 
14388029ab02SPeter Avalos 	*name = pwent->pw_name;
1439e95abc47Szrj 	return 0;
14408029ab02SPeter Avalos }
14418029ab02SPeter Avalos 
14428029ab02SPeter Avalos static const char *
lookup_gname(struct cpio * cpio,gid_t gid)14438029ab02SPeter Avalos lookup_gname(struct cpio *cpio, gid_t gid)
14448029ab02SPeter Avalos {
14458029ab02SPeter Avalos 	return (lookup_name(cpio, &cpio->gname_cache,
14468029ab02SPeter Avalos 		    &lookup_gname_helper, (id_t)gid));
14478029ab02SPeter Avalos }
14488029ab02SPeter Avalos 
14498029ab02SPeter Avalos static int
lookup_gname_helper(struct cpio * cpio,const char ** name,id_t id)14508029ab02SPeter Avalos lookup_gname_helper(struct cpio *cpio, const char **name, id_t id)
14518029ab02SPeter Avalos {
14528029ab02SPeter Avalos 	struct group	*grent;
14538029ab02SPeter Avalos 
14548029ab02SPeter Avalos 	(void)cpio; /* UNUSED */
14558029ab02SPeter Avalos 
14568029ab02SPeter Avalos 	errno = 0;
14578029ab02SPeter Avalos 	grent = getgrgid((gid_t)id);
14588029ab02SPeter Avalos 	if (grent == NULL) {
1459e95abc47Szrj 		if (errno && errno != ENOENT)
146059bf7050SPeter Avalos 			lafe_warnc(errno, "getgrgid(%s) failed",
146159bf7050SPeter Avalos 			    cpio_i64toa((int64_t)id));
1462e95abc47Szrj 		return 1;
14638029ab02SPeter Avalos 	}
14648029ab02SPeter Avalos 
14658029ab02SPeter Avalos 	*name = grent->gr_name;
1466e95abc47Szrj 	return 0;
14678029ab02SPeter Avalos }
14689c82a63eSPeter Avalos 
14699c82a63eSPeter Avalos /*
14709c82a63eSPeter Avalos  * It would be nice to just use printf() for formatting large numbers,
14719c82a63eSPeter Avalos  * but the compatibility problems are a big headache.  Hence the
14729c82a63eSPeter Avalos  * following simple utility function.
14739c82a63eSPeter Avalos  */
14749c82a63eSPeter Avalos const char *
cpio_i64toa(int64_t n0)14759c82a63eSPeter Avalos cpio_i64toa(int64_t n0)
14769c82a63eSPeter Avalos {
1477c09f92d2SPeter Avalos 	/* 2^64 =~ 1.8 * 10^19, so 20 decimal digits suffice.
1478c09f92d2SPeter Avalos 	 * We also need 1 byte for '-' and 1 for '\0'.
1479c09f92d2SPeter Avalos 	 */
14809c82a63eSPeter Avalos 	static char buff[22];
14819c82a63eSPeter Avalos 	int64_t n = n0 < 0 ? -n0 : n0;
14829c82a63eSPeter Avalos 	char *p = buff + sizeof(buff);
14839c82a63eSPeter Avalos 
14849c82a63eSPeter Avalos 	*--p = '\0';
14859c82a63eSPeter Avalos 	do {
14869c82a63eSPeter Avalos 		*--p = '0' + (int)(n % 10);
14879c82a63eSPeter Avalos 		n /= 10;
14889c82a63eSPeter Avalos 	} while (n > 0);
14899c82a63eSPeter Avalos 	if (n0 < 0)
14909c82a63eSPeter Avalos 		*--p = '-';
14919c82a63eSPeter Avalos 	return p;
14929c82a63eSPeter Avalos }
14936b384f39SPeter Avalos 
14946b384f39SPeter Avalos #define PPBUFF_SIZE 1024
14956b384f39SPeter Avalos static const char *
passphrase_callback(struct archive * a,void * _client_data)14966b384f39SPeter Avalos passphrase_callback(struct archive *a, void *_client_data)
14976b384f39SPeter Avalos {
14986b384f39SPeter Avalos 	struct cpio *cpio = (struct cpio *)_client_data;
14996b384f39SPeter Avalos 	(void)a; /* UNUSED */
15006b384f39SPeter Avalos 
15016b384f39SPeter Avalos 	if (cpio->ppbuff == NULL) {
15026b384f39SPeter Avalos 		cpio->ppbuff = malloc(PPBUFF_SIZE);
15036b384f39SPeter Avalos 		if (cpio->ppbuff == NULL)
15046b384f39SPeter Avalos 			lafe_errc(1, errno, "Out of memory");
15056b384f39SPeter Avalos 	}
15066b384f39SPeter Avalos 	return lafe_readpassphrase("Enter passphrase:",
15076b384f39SPeter Avalos 		cpio->ppbuff, PPBUFF_SIZE);
15086b384f39SPeter Avalos }
15096b384f39SPeter Avalos 
15106b384f39SPeter Avalos static void
passphrase_free(char * ppbuff)15116b384f39SPeter Avalos passphrase_free(char *ppbuff)
15126b384f39SPeter Avalos {
15136b384f39SPeter Avalos 	if (ppbuff != NULL) {
15146b384f39SPeter Avalos 		memset(ppbuff, 0, PPBUFF_SIZE);
15156b384f39SPeter Avalos 		free(ppbuff);
15166b384f39SPeter Avalos 	}
15176b384f39SPeter Avalos }
1518