xref: /dragonfly/contrib/libarchive/tar/bsdtar.c (revision 4d0c54c1)
1 /*-
2  * Copyright (c) 2003-2008 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/tar/bsdtar.c,v 1.93 2008/11/08 04:43:24 kientzle Exp $");
28 
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
31 #endif
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35 #ifdef HAVE_COPYFILE_H
36 #include <copyfile.h>
37 #endif
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #include <fcntl.h>
43 #endif
44 #ifdef HAVE_LANGINFO_H
45 #include <langinfo.h>
46 #endif
47 #ifdef HAVE_LOCALE_H
48 #include <locale.h>
49 #endif
50 #ifdef HAVE_PATHS_H
51 #include <paths.h>
52 #endif
53 #ifdef HAVE_SIGNAL_H
54 #include <signal.h>
55 #endif
56 #include <stdio.h>
57 #ifdef HAVE_STDLIB_H
58 #include <stdlib.h>
59 #endif
60 #ifdef HAVE_STRING_H
61 #include <string.h>
62 #endif
63 #ifdef HAVE_TIME_H
64 #include <time.h>
65 #endif
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
69 
70 #include "bsdtar.h"
71 #include "err.h"
72 
73 /*
74  * Per POSIX.1-1988, tar defaults to reading/writing archives to/from
75  * the default tape device for the system.  Pick something reasonable here.
76  */
77 #ifdef __linux
78 #define	_PATH_DEFTAPE "/dev/st0"
79 #endif
80 #if defined(_WIN32) && !defined(__CYGWIN__)
81 #define	_PATH_DEFTAPE "\\\\.\\tape0"
82 #endif
83 #if defined(__APPLE__)
84 #undef _PATH_DEFTAPE
85 #define	_PATH_DEFTAPE "-"  /* Mac OS has no tape support, default to stdio. */
86 #endif
87 
88 #ifndef _PATH_DEFTAPE
89 #define	_PATH_DEFTAPE "/dev/tape"
90 #endif
91 
92 #ifdef __MINGW32__
93 int _CRT_glob = 0; /* Disable broken CRT globbing. */
94 #endif
95 
96 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
97 static volatile int siginfo_occurred;
98 
99 static void
100 siginfo_handler(int sig)
101 {
102 	(void)sig; /* UNUSED */
103 	siginfo_occurred = 1;
104 }
105 
106 int
107 need_report(void)
108 {
109 	int r = siginfo_occurred;
110 	siginfo_occurred = 0;
111 	return (r);
112 }
113 #else
114 int
115 need_report(void)
116 {
117 	return (0);
118 }
119 #endif
120 
121 static void		 long_help(void);
122 static void		 only_mode(struct bsdtar *, const char *opt,
123 			     const char *valid);
124 static void		 set_mode(struct bsdtar *, char opt);
125 static void		 version(void);
126 
127 /* A basic set of security flags to request from libarchive. */
128 #define	SECURITY					\
129 	(ARCHIVE_EXTRACT_SECURE_SYMLINKS		\
130 	 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
131 
132 int
133 main(int argc, char **argv)
134 {
135 	struct bsdtar		*bsdtar, bsdtar_storage;
136 	int			 opt, t;
137 	char			 option_o;
138 	char			 possible_help_request;
139 	char			 buff[16];
140 
141 	/*
142 	 * Use a pointer for consistency, but stack-allocated storage
143 	 * for ease of cleanup.
144 	 */
145 	bsdtar = &bsdtar_storage;
146 	memset(bsdtar, 0, sizeof(*bsdtar));
147 	bsdtar->fd = -1; /* Mark as "unused" */
148 	bsdtar->gid = -1;
149 	bsdtar->uid = -1;
150 	option_o = 0;
151 
152 #if defined(HAVE_SIGACTION)
153 	{ /* Set up signal handling. */
154 		struct sigaction sa;
155 		sa.sa_handler = siginfo_handler;
156 		sigemptyset(&sa.sa_mask);
157 		sa.sa_flags = 0;
158 #ifdef SIGINFO
159 		if (sigaction(SIGINFO, &sa, NULL))
160 			lafe_errc(1, errno, "sigaction(SIGINFO) failed");
161 #endif
162 #ifdef SIGUSR1
163 		/* ... and treat SIGUSR1 the same way as SIGINFO. */
164 		if (sigaction(SIGUSR1, &sa, NULL))
165 			lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
166 #endif
167 #ifdef SIGPIPE
168 		/* Ignore SIGPIPE signals. */
169 		sa.sa_handler = SIG_IGN;
170 		sigaction(SIGPIPE, &sa, NULL);
171 #endif
172 	}
173 #endif
174 
175 
176 	/* Need lafe_progname before calling lafe_warnc. */
177 	if (*argv == NULL)
178 		lafe_progname = "bsdtar";
179 	else {
180 #if defined(_WIN32) && !defined(__CYGWIN__)
181 		lafe_progname = strrchr(*argv, '\\');
182 		if (strrchr(*argv, '/') > lafe_progname)
183 #endif
184 		lafe_progname = strrchr(*argv, '/');
185 		if (lafe_progname != NULL)
186 			lafe_progname++;
187 		else
188 			lafe_progname = *argv;
189 	}
190 
191 #if HAVE_SETLOCALE
192 	if (setlocale(LC_ALL, "") == NULL)
193 		lafe_warnc(0, "Failed to set default locale");
194 #endif
195 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
196 	bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
197 #endif
198 	possible_help_request = 0;
199 
200 	/* Look up uid of current user for future reference */
201 	bsdtar->user_uid = geteuid();
202 
203 	/* Default: open tape drive. */
204 	bsdtar->filename = getenv("TAPE");
205 	if (bsdtar->filename == NULL)
206 		bsdtar->filename = _PATH_DEFTAPE;
207 
208 	/* Default block size settings. */
209 	bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK;
210 	/* Allow library to default this unless user specifies -b. */
211 	bsdtar->bytes_in_last_block = -1;
212 
213 	/* Default: preserve mod time on extract */
214 	bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
215 
216 	/* Default: Perform basic security checks. */
217 	bsdtar->extract_flags |= SECURITY;
218 
219 #ifndef _WIN32
220 	/* On POSIX systems, assume --same-owner and -p when run by
221 	 * the root user.  This doesn't make any sense on Windows. */
222 	if (bsdtar->user_uid == 0) {
223 		/* --same-owner */
224 		bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
225 		/* -p */
226 		bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
227 		bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
228 		bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
229 		bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
230 		bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
231 	}
232 #endif
233 
234 	/*
235 	 * Enable Mac OS "copyfile()" extension by default.
236 	 * This has no effect on other platforms.
237 	 */
238 	bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
239 #ifdef COPYFILE_DISABLE_VAR
240 	if (getenv(COPYFILE_DISABLE_VAR))
241 		bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
242 #endif
243 	bsdtar->matching = archive_match_new();
244 	if (bsdtar->matching == NULL)
245 		lafe_errc(1, errno, "Out of memory");
246 
247 	bsdtar->argv = argv;
248 	bsdtar->argc = argc;
249 
250 	/*
251 	 * Comments following each option indicate where that option
252 	 * originated:  SUSv2, POSIX, GNU tar, star, etc.  If there's
253 	 * no such comment, then I don't know of anyone else who
254 	 * implements that option.
255 	 */
256 	while ((opt = bsdtar_getopt(bsdtar)) != -1) {
257 		switch (opt) {
258 		case 'B': /* GNU tar */
259 			/* libarchive doesn't need this; just ignore it. */
260 			break;
261 		case 'b': /* SUSv2 */
262 			t = atoi(bsdtar->argument);
263 			if (t <= 0 || t > 8192)
264 				lafe_errc(1, 0,
265 				    "Argument to -b is out of range (1..8192)");
266 			bsdtar->bytes_per_block = 512 * t;
267 			/* Explicit -b forces last block size. */
268 			bsdtar->bytes_in_last_block = bsdtar->bytes_per_block;
269 			break;
270 		case 'C': /* GNU tar */
271 			if (strlen(bsdtar->argument) == 0)
272 				lafe_errc(1, 0,
273 				    "Meaningless option: -C ''");
274 
275 			set_chdir(bsdtar, bsdtar->argument);
276 			break;
277 		case 'c': /* SUSv2 */
278 			set_mode(bsdtar, opt);
279 			break;
280 		case OPTION_CHECK_LINKS: /* GNU tar */
281 			bsdtar->option_warn_links = 1;
282 			break;
283 		case OPTION_CHROOT: /* NetBSD */
284 			bsdtar->option_chroot = 1;
285 			break;
286 		case OPTION_DISABLE_COPYFILE: /* Mac OS X */
287 			bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
288 			break;
289 		case OPTION_EXCLUDE: /* GNU tar */
290 			if (archive_match_exclude_pattern(
291 			    bsdtar->matching, bsdtar->argument) != ARCHIVE_OK)
292 				lafe_errc(1, 0,
293 				    "Couldn't exclude %s\n", bsdtar->argument);
294 			break;
295 		case OPTION_FORMAT: /* GNU tar, others */
296 			bsdtar->create_format = bsdtar->argument;
297 			break;
298 		case 'f': /* SUSv2 */
299 			bsdtar->filename = bsdtar->argument;
300 			break;
301 		case OPTION_GID: /* cpio */
302 			t = atoi(bsdtar->argument);
303 			if (t < 0)
304 				lafe_errc(1, 0,
305 				    "Argument to --gid must be positive");
306 			bsdtar->gid = t;
307 			break;
308 		case OPTION_GNAME: /* cpio */
309 			bsdtar->gname = bsdtar->argument;
310 			break;
311 		case 'H': /* BSD convention */
312 			bsdtar->symlink_mode = 'H';
313 			break;
314 		case 'h': /* Linux Standards Base, gtar; synonym for -L */
315 			bsdtar->symlink_mode = 'L';
316 			/* Hack: -h by itself is the "help" command. */
317 			possible_help_request = 1;
318 			break;
319 		case OPTION_HELP: /* GNU tar, others */
320 			long_help();
321 			exit(0);
322 			break;
323 		case 'I': /* GNU tar */
324 			/*
325 			 * TODO: Allow 'names' to come from an archive,
326 			 * not just a text file.  Design a good UI for
327 			 * allowing names and mode/owner to be read
328 			 * from an archive, with contents coming from
329 			 * disk.  This can be used to "refresh" an
330 			 * archive or to design archives with special
331 			 * permissions without having to create those
332 			 * permissions on disk.
333 			 */
334 			bsdtar->names_from_file = bsdtar->argument;
335 			break;
336 		case OPTION_INCLUDE:
337 			/*
338 			 * No one else has the @archive extension, so
339 			 * no one else needs this to filter entries
340 			 * when transforming archives.
341 			 */
342 			if (archive_match_include_pattern(bsdtar->matching,
343 			    bsdtar->argument) != ARCHIVE_OK)
344 				lafe_errc(1, 0,
345 				    "Failed to add %s to inclusion list",
346 				    bsdtar->argument);
347 			break;
348 		case 'j': /* GNU tar */
349 			if (bsdtar->create_compression != '\0')
350 				lafe_errc(1, 0,
351 				    "Can't specify both -%c and -%c", opt,
352 				    bsdtar->create_compression);
353 			bsdtar->create_compression = opt;
354 			break;
355 		case 'J': /* GNU tar 1.21 and later */
356 			if (bsdtar->create_compression != '\0')
357 				lafe_errc(1, 0,
358 				    "Can't specify both -%c and -%c", opt,
359 				    bsdtar->create_compression);
360 			bsdtar->create_compression = opt;
361 			break;
362 		case 'k': /* GNU tar */
363 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
364 			break;
365 		case OPTION_KEEP_NEWER_FILES: /* GNU tar */
366 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
367 			break;
368 		case 'L': /* BSD convention */
369 			bsdtar->symlink_mode = 'L';
370 			break;
371 	        case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
372 			/* GNU tar 1.13  used -l for --one-file-system */
373 			bsdtar->option_warn_links = 1;
374 			break;
375 		case OPTION_LZIP: /* GNU tar beginning with 1.23 */
376 		case OPTION_LZMA: /* GNU tar beginning with 1.20 */
377 			if (bsdtar->create_compression != '\0')
378 				lafe_errc(1, 0,
379 				    "Can't specify both -%c and -%c", opt,
380 				    bsdtar->create_compression);
381 			bsdtar->create_compression = opt;
382 			break;
383 		case 'm': /* SUSv2 */
384 			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
385 			break;
386 		case 'n': /* GNU tar */
387 			bsdtar->option_no_subdirs = 1;
388 			break;
389 	        /*
390 		 * Selecting files by time:
391 		 *    --newer-?time='date' Only files newer than 'date'
392 		 *    --newer-?time-than='file' Only files newer than time
393 		 *         on specified file (useful for incremental backups)
394 		 * TODO: Add corresponding "older" options to reverse these.
395 		 */
396 		case OPTION_NEWER_CTIME: /* GNU tar */
397 			if (archive_match_include_date(bsdtar->matching,
398 			    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
399 			    bsdtar->argument) != ARCHIVE_OK)
400 				lafe_errc(1, 0, "Error : %s",
401 				    archive_error_string(bsdtar->matching));
402 			break;
403 		case OPTION_NEWER_CTIME_THAN:
404 			if (archive_match_include_file_time(bsdtar->matching,
405 			    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
406 			    bsdtar->argument) != ARCHIVE_OK)
407 				lafe_errc(1, 0, "Error : %s",
408 				    archive_error_string(bsdtar->matching));
409 			break;
410 		case OPTION_NEWER_MTIME: /* GNU tar */
411 			if (archive_match_include_date(bsdtar->matching,
412 			    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
413 			    bsdtar->argument) != ARCHIVE_OK)
414 				lafe_errc(1, 0, "Error : %s",
415 				    archive_error_string(bsdtar->matching));
416 			break;
417 		case OPTION_NEWER_MTIME_THAN:
418 			if (archive_match_include_file_time(bsdtar->matching,
419 			    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
420 			    bsdtar->argument) != ARCHIVE_OK)
421 				lafe_errc(1, 0, "Error : %s",
422 				    archive_error_string(bsdtar->matching));
423 			break;
424 		case OPTION_NODUMP: /* star */
425 			bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP;
426 			break;
427 		case OPTION_NO_SAME_OWNER: /* GNU tar */
428 			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
429 			break;
430 		case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
431 			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
432 			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
433 			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
434 			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
435 			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
436 			break;
437 		case OPTION_NULL: /* GNU tar */
438 			bsdtar->option_null++;
439 			break;
440 		case OPTION_NUMERIC_OWNER: /* GNU tar */
441 			bsdtar->uname = "";
442 			bsdtar->gname = "";
443 			bsdtar->option_numeric_owner++;
444 			break;
445 		case 'O': /* GNU tar */
446 			bsdtar->option_stdout = 1;
447 			break;
448 		case 'o': /* SUSv2 and GNU conflict here, but not fatally */
449 			option_o = 1; /* Record it and resolve it later. */
450 			break;
451 		case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
452 			bsdtar->readdisk_flags |=
453 			    ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;
454 			break;
455 		case OPTION_OPTIONS:
456 			bsdtar->option_options = bsdtar->argument;
457 			break;
458 #if 0
459 		/*
460 		 * The common BSD -P option is not necessary, since
461 		 * our default is to archive symlinks, not follow
462 		 * them.  This is convenient, as -P conflicts with GNU
463 		 * tar anyway.
464 		 */
465 		case 'P': /* BSD convention */
466 			/* Default behavior, no option necessary. */
467 			break;
468 #endif
469 		case 'P': /* GNU tar */
470 			bsdtar->extract_flags &= ~SECURITY;
471 			bsdtar->option_absolute_paths = 1;
472 			break;
473 		case 'p': /* GNU tar, star */
474 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
475 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
476 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
477 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
478 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
479 			break;
480 		case OPTION_POSIX: /* GNU tar */
481 			bsdtar->create_format = "pax";
482 			break;
483 		case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
484 			bsdtar->option_fast_read = 1;
485 			break;
486 		case 'r': /* SUSv2 */
487 			set_mode(bsdtar, opt);
488 			break;
489 		case 'S': /* NetBSD pax-as-tar */
490 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
491 			break;
492 		case 's': /* NetBSD pax-as-tar */
493 #if HAVE_REGEX_H
494 			add_substitution(bsdtar, bsdtar->argument);
495 #else
496 			lafe_warnc(0,
497 			    "-s is not supported by this version of bsdtar");
498 			usage();
499 #endif
500 			break;
501 		case OPTION_SAME_OWNER: /* GNU tar */
502 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
503 			break;
504 		case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
505 			errno = 0;
506 			bsdtar->strip_components = strtol(bsdtar->argument,
507 			    NULL, 0);
508 			if (errno)
509 				lafe_errc(1, 0,
510 				    "Invalid --strip-components argument: %s",
511 				    bsdtar->argument);
512 			break;
513 		case 'T': /* GNU tar */
514 			bsdtar->names_from_file = bsdtar->argument;
515 			break;
516 		case 't': /* SUSv2 */
517 			set_mode(bsdtar, opt);
518 			bsdtar->verbose++;
519 			break;
520 		case OPTION_TOTALS: /* GNU tar */
521 			bsdtar->option_totals++;
522 			break;
523 		case 'U': /* GNU tar */
524 			bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
525 			bsdtar->option_unlink_first = 1;
526 			break;
527 		case 'u': /* SUSv2 */
528 			set_mode(bsdtar, opt);
529 			break;
530 		case OPTION_UID: /* cpio */
531 			t = atoi(bsdtar->argument);
532 			if (t < 0)
533 				lafe_errc(1, 0,
534 				    "Argument to --uid must be positive");
535 			bsdtar->uid = t;
536 			break;
537 		case OPTION_UNAME: /* cpio */
538 			bsdtar->uname = bsdtar->argument;
539 			break;
540 		case 'v': /* SUSv2 */
541 			bsdtar->verbose++;
542 			break;
543 		case OPTION_VERSION: /* GNU convention */
544 			version();
545 			break;
546 #if 0
547 		/*
548 		 * The -W longopt feature is handled inside of
549 		 * bsdtar_getopt(), so -W is not available here.
550 		 */
551 		case 'W': /* Obscure GNU convention. */
552 			break;
553 #endif
554 		case 'w': /* SUSv2 */
555 			bsdtar->option_interactive = 1;
556 			break;
557 		case 'X': /* GNU tar */
558 			if (archive_match_exclude_pattern_from_file(
559 			    bsdtar->matching, bsdtar->argument, 0)
560 			    != ARCHIVE_OK)
561 				lafe_errc(1, 0, "Error : %s",
562 				    archive_error_string(bsdtar->matching));
563 			break;
564 		case 'x': /* SUSv2 */
565 			set_mode(bsdtar, opt);
566 			break;
567 		case 'y': /* FreeBSD version of GNU tar */
568 			if (bsdtar->create_compression != '\0')
569 				lafe_errc(1, 0,
570 				    "Can't specify both -%c and -%c", opt,
571 				    bsdtar->create_compression);
572 			bsdtar->create_compression = opt;
573 			break;
574 		case 'Z': /* GNU tar */
575 			if (bsdtar->create_compression != '\0')
576 				lafe_errc(1, 0,
577 				    "Can't specify both -%c and -%c", opt,
578 				    bsdtar->create_compression);
579 			bsdtar->create_compression = opt;
580 			break;
581 		case 'z': /* GNU tar, star, many others */
582 			if (bsdtar->create_compression != '\0')
583 				lafe_errc(1, 0,
584 				    "Can't specify both -%c and -%c", opt,
585 				    bsdtar->create_compression);
586 			bsdtar->create_compression = opt;
587 			break;
588 		case OPTION_USE_COMPRESS_PROGRAM:
589 			bsdtar->compress_program = bsdtar->argument;
590 			break;
591 		default:
592 			usage();
593 		}
594 	}
595 
596 	/*
597 	 * Sanity-check options.
598 	 */
599 
600 	/* If no "real" mode was specified, treat -h as --help. */
601 	if ((bsdtar->mode == '\0') && possible_help_request) {
602 		long_help();
603 		exit(0);
604 	}
605 
606 	/* Otherwise, a mode is required. */
607 	if (bsdtar->mode == '\0')
608 		lafe_errc(1, 0,
609 		    "Must specify one of -c, -r, -t, -u, -x");
610 
611 	/* Check boolean options only permitted in certain modes. */
612 	if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
613 		only_mode(bsdtar, "--one-file-system", "cru");
614 	if (bsdtar->option_fast_read)
615 		only_mode(bsdtar, "--fast-read", "xt");
616 	if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP)
617 		only_mode(bsdtar, "--nodump", "cru");
618 	if (option_o > 0) {
619 		switch (bsdtar->mode) {
620 		case 'c':
621 			/*
622 			 * In GNU tar, -o means "old format."  The
623 			 * "ustar" format is the closest thing
624 			 * supported by libarchive.
625 			 */
626 			bsdtar->create_format = "ustar";
627 			/* TODO: bsdtar->create_format = "v7"; */
628 			break;
629 		case 'x':
630 			/* POSIX-compatible behavior. */
631 			bsdtar->option_no_owner = 1;
632 			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
633 			break;
634 		default:
635 			only_mode(bsdtar, "-o", "xc");
636 			break;
637 		}
638 	}
639 	if (bsdtar->option_no_subdirs)
640 		only_mode(bsdtar, "-n", "cru");
641 	if (bsdtar->option_stdout)
642 		only_mode(bsdtar, "-O", "xt");
643 	if (bsdtar->option_unlink_first)
644 		only_mode(bsdtar, "-U", "x");
645 	if (bsdtar->option_warn_links)
646 		only_mode(bsdtar, "--check-links", "cr");
647 
648 	/* Check other parameters only permitted in certain modes. */
649 	if (bsdtar->create_compression != '\0') {
650 		strcpy(buff, "-?");
651 		buff[1] = bsdtar->create_compression;
652 		only_mode(bsdtar, buff, "cxt");
653 	}
654 	if (bsdtar->create_format != NULL)
655 		only_mode(bsdtar, "--format", "cru");
656 	if (bsdtar->symlink_mode != '\0') {
657 		strcpy(buff, "-?");
658 		buff[1] = bsdtar->symlink_mode;
659 		only_mode(bsdtar, buff, "cru");
660 	}
661 
662 	/* Filename "-" implies stdio. */
663 	if (strcmp(bsdtar->filename, "-") == 0)
664 		bsdtar->filename = NULL;
665 
666 	switch(bsdtar->mode) {
667 	case 'c':
668 		tar_mode_c(bsdtar);
669 		break;
670 	case 'r':
671 		tar_mode_r(bsdtar);
672 		break;
673 	case 't':
674 		tar_mode_t(bsdtar);
675 		break;
676 	case 'u':
677 		tar_mode_u(bsdtar);
678 		break;
679 	case 'x':
680 		tar_mode_x(bsdtar);
681 		break;
682 	}
683 
684 	archive_match_free(bsdtar->matching);
685 #if HAVE_REGEX_H
686 	cleanup_substitution(bsdtar);
687 #endif
688 
689 	if (bsdtar->return_value != 0)
690 		lafe_warnc(0,
691 		    "Error exit delayed from previous errors.");
692 	return (bsdtar->return_value);
693 }
694 
695 static void
696 set_mode(struct bsdtar *bsdtar, char opt)
697 {
698 	if (bsdtar->mode != '\0' && bsdtar->mode != opt)
699 		lafe_errc(1, 0,
700 		    "Can't specify both -%c and -%c", opt, bsdtar->mode);
701 	bsdtar->mode = opt;
702 }
703 
704 /*
705  * Verify that the mode is correct.
706  */
707 static void
708 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
709 {
710 	if (strchr(valid_modes, bsdtar->mode) == NULL)
711 		lafe_errc(1, 0,
712 		    "Option %s is not permitted in mode -%c",
713 		    opt, bsdtar->mode);
714 }
715 
716 
717 void
718 usage(void)
719 {
720 	const char	*p;
721 
722 	p = lafe_progname;
723 
724 	fprintf(stderr, "Usage:\n");
725 	fprintf(stderr, "  List:    %s -tf <archive-filename>\n", p);
726 	fprintf(stderr, "  Extract: %s -xf <archive-filename>\n", p);
727 	fprintf(stderr, "  Create:  %s -cf <archive-filename> [filenames...]\n", p);
728 	fprintf(stderr, "  Help:    %s --help\n", p);
729 	exit(1);
730 }
731 
732 static void
733 version(void)
734 {
735 	printf("bsdtar %s - %s\n",
736 	    BSDTAR_VERSION_STRING,
737 	    archive_version_string());
738 	exit(0);
739 }
740 
741 static const char *long_help_msg =
742 	"First option must be a mode specifier:\n"
743 	"  -c Create  -r Add/Replace  -t List  -u Update  -x Extract\n"
744 	"Common Options:\n"
745 	"  -b #  Use # 512-byte records per I/O block\n"
746 	"  -f <filename>  Location of archive (default " _PATH_DEFTAPE ")\n"
747 	"  -v    Verbose\n"
748 	"  -w    Interactive\n"
749 	"Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
750 	"  <file>, <dir>  add these items to archive\n"
751 	"  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma\n"
752 	"  --format {ustar|pax|cpio|shar}  Select archive format\n"
753 	"  --exclude <pattern>  Skip files that match pattern\n"
754 	"  -C <dir>  Change to <dir> before processing remaining files\n"
755 	"  @<archive>  Add entries from <archive> to output\n"
756 	"List: %p -t [options] [<patterns>]\n"
757 	"  <patterns>  If specified, list only entries that match\n"
758 	"Extract: %p -x [options] [<patterns>]\n"
759 	"  <patterns>  If specified, extract only entries that match\n"
760 	"  -k    Keep (don't overwrite) existing files\n"
761 	"  -m    Don't restore modification times\n"
762 	"  -O    Write entries to stdout, don't restore to disk\n"
763 	"  -p    Restore permissions (including ACLs, owner, file flags)\n";
764 
765 
766 /*
767  * Note that the word 'bsdtar' will always appear in the first line
768  * of output.
769  *
770  * In particular, /bin/sh scripts that need to test for the presence
771  * of bsdtar can use the following template:
772  *
773  * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
774  *          echo bsdtar; else echo not bsdtar; fi
775  */
776 static void
777 long_help(void)
778 {
779 	const char	*prog;
780 	const char	*p;
781 
782 	prog = lafe_progname;
783 
784 	fflush(stderr);
785 
786 	p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
787 	printf("%s%s: manipulate archive files\n", prog, p);
788 
789 	for (p = long_help_msg; *p != '\0'; p++) {
790 		if (*p == '%') {
791 			if (p[1] == 'p') {
792 				fputs(prog, stdout);
793 				p++;
794 			} else
795 				putchar('%');
796 		} else
797 			putchar(*p);
798 	}
799 	version();
800 }
801