2 * Copyright (c) 2003-2008 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD$");
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
32 #ifdef HAVE_SYS_STAT_H
35 #ifdef HAVE_COPYFILE_H
44 #ifdef HAVE_LANGINFO_H
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.
78 #define _PATH_DEFTAPE "/dev/st0"
80 #if defined(_WIN32) && !defined(__CYGWIN__)
81 #define _PATH_DEFTAPE "\\\\.\\tape0"
83 #if defined(__APPLE__)
85 #define _PATH_DEFTAPE "-" /* Mac OS has no tape support, default to stdio. */
89 #define _PATH_DEFTAPE "/dev/tape"
93 int _CRT_glob = 0; /* Disable broken CRT globbing. */
96 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
97 static volatile int siginfo_occurred;
100 siginfo_handler(int sig)
102 (void)sig; /* UNUSED */
103 siginfo_occurred = 1;
109 int r = siginfo_occurred;
110 siginfo_occurred = 0;
121 static void long_help(void) __LA_DEAD;
122 static void only_mode(struct bsdtar *, const char *opt,
124 static void set_mode(struct bsdtar *, char opt);
125 static void version(void) __LA_DEAD;
127 /* A basic set of security flags to request from libarchive. */
129 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
130 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
133 main(int argc, char **argv)
135 struct bsdtar *bsdtar, bsdtar_storage;
137 char compression, compression2;
138 const char *compression_name, *compression2_name;
139 const char *compress_program;
140 char possible_help_request;
144 * Use a pointer for consistency, but stack-allocated storage
145 * for ease of cleanup.
147 bsdtar = &bsdtar_storage;
148 memset(bsdtar, 0, sizeof(*bsdtar));
149 bsdtar->fd = -1; /* Mark as "unused" */
153 compression = compression2 = '\0';
154 compression_name = compression2_name = NULL;
155 compress_program = NULL;
157 #if defined(HAVE_SIGACTION)
158 { /* Set up signal handling. */
160 sa.sa_handler = siginfo_handler;
161 sigemptyset(&sa.sa_mask);
164 if (sigaction(SIGINFO, &sa, NULL))
165 lafe_errc(1, errno, "sigaction(SIGINFO) failed");
168 /* ... and treat SIGUSR1 the same way as SIGINFO. */
169 if (sigaction(SIGUSR1, &sa, NULL))
170 lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
173 /* Ignore SIGPIPE signals. */
174 sa.sa_handler = SIG_IGN;
175 sigaction(SIGPIPE, &sa, NULL);
180 /* Set lafe_progname before calling lafe_warnc. */
181 lafe_setprogname(*argv, "bsdtar");
184 if (setlocale(LC_ALL, "") == NULL)
185 lafe_warnc(0, "Failed to set default locale");
187 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
188 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
190 possible_help_request = 0;
192 /* Look up uid of current user for future reference */
193 bsdtar->user_uid = geteuid();
195 /* Default: open tape drive. */
196 bsdtar->filename = getenv("TAPE");
197 if (bsdtar->filename == NULL)
198 bsdtar->filename = _PATH_DEFTAPE;
200 /* Default block size settings. */
201 bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK;
202 /* Allow library to default this unless user specifies -b. */
203 bsdtar->bytes_in_last_block = -1;
205 /* Default: preserve mod time on extract */
206 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
208 /* Default: Perform basic security checks. */
209 bsdtar->extract_flags |= SECURITY;
212 /* On POSIX systems, assume --same-owner and -p when run by
213 * the root user. This doesn't make any sense on Windows. */
214 if (bsdtar->user_uid == 0) {
216 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
218 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
219 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
220 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
221 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
222 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
227 * Enable Mac OS "copyfile()" extension by default.
228 * This has no effect on other platforms.
230 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
231 #ifdef COPYFILE_DISABLE_VAR
232 if (getenv(COPYFILE_DISABLE_VAR))
233 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
235 #if defined(__APPLE__)
237 * On Mac OS ACLs are archived with copyfile() (--mac-metadata)
238 * Translation to NFSv4 ACLs has to be requested explicitly with --acls
240 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL;
243 bsdtar->matching = archive_match_new();
244 if (bsdtar->matching == NULL)
245 lafe_errc(1, errno, "Out of memory");
246 bsdtar->cset = cset_new();
247 if (bsdtar->cset == NULL)
248 lafe_errc(1, errno, "Out of memory");
254 * Comments following each option indicate where that option
255 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
256 * no such comment, then I don't know of anyone else who
257 * implements that option.
259 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
261 case 'a': /* GNU tar */
262 bsdtar->flags |= OPTFLAG_AUTO_COMPRESS;
264 case OPTION_ACLS: /* GNU tar */
265 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
266 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_ACL;
267 bsdtar->flags |= OPTFLAG_ACLS;
269 case 'B': /* GNU tar */
270 /* libarchive doesn't need this; just ignore it. */
272 case 'b': /* SUSv2 */
273 t = atoi(bsdtar->argument);
274 if (t <= 0 || t > 8192)
276 "Argument to -b is out of range (1..8192)");
277 bsdtar->bytes_per_block = 512 * t;
278 /* Explicit -b forces last block size. */
279 bsdtar->bytes_in_last_block = bsdtar->bytes_per_block;
281 case OPTION_B64ENCODE:
282 if (compression2 != '\0')
284 "Can't specify both --uuencode and "
287 compression2_name = "b64encode";
289 case 'C': /* GNU tar */
290 if (strlen(bsdtar->argument) == 0)
292 "Meaningless option: -C ''");
294 set_chdir(bsdtar, bsdtar->argument);
296 case 'c': /* SUSv2 */
297 set_mode(bsdtar, opt);
299 case OPTION_CHECK_LINKS: /* GNU tar */
300 bsdtar->flags |= OPTFLAG_WARN_LINKS;
302 case OPTION_CHROOT: /* NetBSD */
303 bsdtar->flags |= OPTFLAG_CHROOT;
305 case OPTION_CLEAR_NOCHANGE_FFLAGS:
306 bsdtar->extract_flags |=
307 ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS;
309 case OPTION_EXCLUDE: /* GNU tar */
310 if (archive_match_exclude_pattern(
311 bsdtar->matching, bsdtar->argument) != ARCHIVE_OK)
313 "Couldn't exclude %s\n", bsdtar->argument);
316 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
317 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_FFLAGS;
318 bsdtar->flags |= OPTFLAG_FFLAGS;
320 case OPTION_FORMAT: /* GNU tar, others */
321 cset_set_format(bsdtar->cset, bsdtar->argument);
323 case 'f': /* SUSv2 */
324 bsdtar->filename = bsdtar->argument;
326 case OPTION_GID: /* cpio */
327 t = atoi(bsdtar->argument);
330 "Argument to --gid must be positive");
333 case OPTION_GNAME: /* cpio */
334 bsdtar->gname = bsdtar->argument;
337 if (compression != '\0')
339 "Can't specify both -%c and -%c", opt,
342 compression_name = "grzip";
344 case 'H': /* BSD convention */
345 bsdtar->symlink_mode = 'H';
347 case 'h': /* Linux Standards Base, gtar; synonym for -L */
348 bsdtar->symlink_mode = 'L';
349 /* Hack: -h by itself is the "help" command. */
350 possible_help_request = 1;
352 case OPTION_HELP: /* GNU tar, others */
356 case OPTION_HFS_COMPRESSION: /* Mac OS X v10.6 or later */
357 bsdtar->extract_flags |=
358 ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED;
360 case OPTION_IGNORE_ZEROS:
361 bsdtar->flags |= OPTFLAG_IGNORE_ZEROS;
363 case 'I': /* GNU tar */
365 * TODO: Allow 'names' to come from an archive,
366 * not just a text file. Design a good UI for
367 * allowing names and mode/owner to be read
368 * from an archive, with contents coming from
369 * disk. This can be used to "refresh" an
370 * archive or to design archives with special
371 * permissions without having to create those
372 * permissions on disk.
374 bsdtar->names_from_file = bsdtar->argument;
378 * No one else has the @archive extension, so
379 * no one else needs this to filter entries
380 * when transforming archives.
382 if (archive_match_include_pattern(bsdtar->matching,
383 bsdtar->argument) != ARCHIVE_OK)
385 "Failed to add %s to inclusion list",
388 case 'j': /* GNU tar */
389 if (compression != '\0')
391 "Can't specify both -%c and -%c", opt,
394 compression_name = "bzip2";
396 case 'J': /* GNU tar 1.21 and later */
397 if (compression != '\0')
399 "Can't specify both -%c and -%c", opt,
402 compression_name = "xz";
404 case 'k': /* GNU tar */
405 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
407 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
408 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
410 case 'L': /* BSD convention */
411 bsdtar->symlink_mode = 'L';
413 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
414 /* GNU tar 1.13 used -l for --one-file-system */
415 bsdtar->flags |= OPTFLAG_WARN_LINKS;
419 case OPTION_LZIP: /* GNU tar beginning with 1.23 */
420 case OPTION_LZMA: /* GNU tar beginning with 1.20 */
421 case OPTION_LZOP: /* GNU tar beginning with 1.21 */
422 if (compression != '\0')
424 "Can't specify both -%c and -%c", opt,
428 case OPTION_LRZIP: compression_name = "lrzip"; break;
429 case OPTION_LZ4: compression_name = "lz4"; break;
430 case OPTION_LZIP: compression_name = "lzip"; break;
431 case OPTION_LZMA: compression_name = "lzma"; break;
432 case OPTION_LZOP: compression_name = "lzop"; break;
435 case 'm': /* SUSv2 */
436 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
438 case OPTION_MAC_METADATA: /* Mac OS X */
439 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
440 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
441 bsdtar->flags |= OPTFLAG_MAC_METADATA;
443 case 'n': /* GNU tar */
444 bsdtar->flags |= OPTFLAG_NO_SUBDIRS;
447 * Selecting files by time:
448 * --newer-?time='date' Only files newer than 'date'
449 * --newer-?time-than='file' Only files newer than time
450 * on specified file (useful for incremental backups)
452 case OPTION_NEWER_CTIME: /* GNU tar */
453 if (archive_match_include_date(bsdtar->matching,
454 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
455 bsdtar->argument) != ARCHIVE_OK)
456 lafe_errc(1, 0, "Error : %s",
457 archive_error_string(bsdtar->matching));
459 case OPTION_NEWER_CTIME_THAN:
460 if (archive_match_include_file_time(bsdtar->matching,
461 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
462 bsdtar->argument) != ARCHIVE_OK)
463 lafe_errc(1, 0, "Error : %s",
464 archive_error_string(bsdtar->matching));
466 case OPTION_NEWER_MTIME: /* GNU tar */
467 if (archive_match_include_date(bsdtar->matching,
468 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
469 bsdtar->argument) != ARCHIVE_OK)
470 lafe_errc(1, 0, "Error : %s",
471 archive_error_string(bsdtar->matching));
473 case OPTION_NEWER_MTIME_THAN:
474 if (archive_match_include_file_time(bsdtar->matching,
475 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
476 bsdtar->argument) != ARCHIVE_OK)
477 lafe_errc(1, 0, "Error : %s",
478 archive_error_string(bsdtar->matching));
480 case OPTION_NODUMP: /* star */
481 bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP;
483 case OPTION_NOPRESERVE_HFS_COMPRESSION:
484 /* Mac OS X v10.6 or later */
485 bsdtar->extract_flags |=
486 ARCHIVE_EXTRACT_NO_HFS_COMPRESSION;
488 case OPTION_NO_ACLS: /* GNU tar */
489 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
490 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL;
491 bsdtar->flags |= OPTFLAG_NO_ACLS;
493 case OPTION_NO_FFLAGS:
494 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
495 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_FFLAGS;
496 bsdtar->flags |= OPTFLAG_NO_FFLAGS;
498 case OPTION_NO_MAC_METADATA: /* Mac OS X */
499 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
500 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
501 bsdtar->flags |= OPTFLAG_NO_MAC_METADATA;
503 case OPTION_NO_SAME_OWNER: /* GNU tar */
504 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
506 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
507 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
508 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
509 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
510 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
511 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
513 case OPTION_NO_XATTRS: /* GNU tar */
514 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
515 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_XATTR;
516 bsdtar->flags |= OPTFLAG_NO_XATTRS;
518 case OPTION_NULL: /* GNU tar */
519 bsdtar->flags |= OPTFLAG_NULL;
521 case OPTION_NUMERIC_OWNER: /* GNU tar */
524 bsdtar->flags |= OPTFLAG_NUMERIC_OWNER;
526 case 'O': /* GNU tar */
527 bsdtar->flags |= OPTFLAG_STDOUT;
529 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
530 bsdtar->flags |= OPTFLAG_O;
533 * Selecting files by time:
534 * --older-?time='date' Only files older than 'date'
535 * --older-?time-than='file' Only files older than time
538 case OPTION_OLDER_CTIME:
539 if (archive_match_include_date(bsdtar->matching,
540 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
541 bsdtar->argument) != ARCHIVE_OK)
542 lafe_errc(1, 0, "Error : %s",
543 archive_error_string(bsdtar->matching));
545 case OPTION_OLDER_CTIME_THAN:
546 if (archive_match_include_file_time(bsdtar->matching,
547 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
548 bsdtar->argument) != ARCHIVE_OK)
549 lafe_errc(1, 0, "Error : %s",
550 archive_error_string(bsdtar->matching));
552 case OPTION_OLDER_MTIME:
553 if (archive_match_include_date(bsdtar->matching,
554 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
555 bsdtar->argument) != ARCHIVE_OK)
556 lafe_errc(1, 0, "Error : %s",
557 archive_error_string(bsdtar->matching));
559 case OPTION_OLDER_MTIME_THAN:
560 if (archive_match_include_file_time(bsdtar->matching,
561 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
562 bsdtar->argument) != ARCHIVE_OK)
563 lafe_errc(1, 0, "Error : %s",
564 archive_error_string(bsdtar->matching));
566 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
567 bsdtar->readdisk_flags |=
568 ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;
571 bsdtar->option_options = bsdtar->argument;
575 * The common BSD -P option is not necessary, since
576 * our default is to archive symlinks, not follow
577 * them. This is convenient, as -P conflicts with GNU
580 case 'P': /* BSD convention */
581 /* Default behavior, no option necessary. */
584 case 'P': /* GNU tar */
585 bsdtar->extract_flags &= ~SECURITY;
586 bsdtar->flags |= OPTFLAG_ABSOLUTE_PATHS;
588 case 'p': /* GNU tar, star */
589 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
590 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
591 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
592 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
593 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
595 case OPTION_PASSPHRASE:
596 bsdtar->passphrase = bsdtar->argument;
598 case OPTION_POSIX: /* GNU tar */
599 cset_set_format(bsdtar->cset, "pax");
601 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
602 bsdtar->flags |= OPTFLAG_FAST_READ;
604 case 'r': /* SUSv2 */
605 set_mode(bsdtar, opt);
607 case 'S': /* NetBSD pax-as-tar */
608 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
610 case 's': /* NetBSD pax-as-tar */
611 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
612 add_substitution(bsdtar, bsdtar->argument);
615 "-s is not supported by this version of bsdtar");
619 case OPTION_SAME_OWNER: /* GNU tar */
620 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
622 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
624 bsdtar->strip_components = strtol(bsdtar->argument,
628 "Invalid --strip-components argument: %s",
631 case 'T': /* GNU tar */
632 bsdtar->names_from_file = bsdtar->argument;
634 case 't': /* SUSv2 */
635 set_mode(bsdtar, opt);
638 case OPTION_TOTALS: /* GNU tar */
639 bsdtar->flags |= OPTFLAG_TOTALS;
641 case 'U': /* GNU tar */
642 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
643 bsdtar->flags |= OPTFLAG_UNLINK_FIRST;
645 case 'u': /* SUSv2 */
646 set_mode(bsdtar, opt);
648 case OPTION_UID: /* cpio */
649 t = atoi(bsdtar->argument);
652 "Argument to --uid must be positive");
655 case OPTION_UNAME: /* cpio */
656 bsdtar->uname = bsdtar->argument;
658 case OPTION_UUENCODE:
659 if (compression2 != '\0')
661 "Can't specify both --uuencode and "
664 compression2_name = "uuencode";
666 case 'v': /* SUSv2 */
669 case OPTION_VERSION: /* GNU convention */
674 * The -W longopt feature is handled inside of
675 * bsdtar_getopt(), so -W is not available here.
677 case 'W': /* Obscure GNU convention. */
680 case 'w': /* SUSv2 */
681 bsdtar->flags |= OPTFLAG_INTERACTIVE;
683 case 'X': /* GNU tar */
684 if (archive_match_exclude_pattern_from_file(
685 bsdtar->matching, bsdtar->argument, 0)
687 lafe_errc(1, 0, "Error : %s",
688 archive_error_string(bsdtar->matching));
690 case 'x': /* SUSv2 */
691 set_mode(bsdtar, opt);
693 case OPTION_XATTRS: /* GNU tar */
694 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
695 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_XATTR;
696 bsdtar->flags |= OPTFLAG_XATTRS;
698 case 'y': /* FreeBSD version of GNU tar */
699 if (compression != '\0')
701 "Can't specify both -%c and -%c", opt,
704 compression_name = "bzip2";
706 case 'Z': /* GNU tar */
707 if (compression != '\0')
709 "Can't specify both -%c and -%c", opt,
712 compression_name = "compress";
714 case 'z': /* GNU tar, star, many others */
715 if (compression != '\0')
717 "Can't specify both -%c and -%c", opt,
720 compression_name = "gzip";
722 case OPTION_USE_COMPRESS_PROGRAM:
723 compress_program = bsdtar->argument;
731 * Sanity-check options.
734 /* If no "real" mode was specified, treat -h as --help. */
735 if ((bsdtar->mode == '\0') && possible_help_request) {
740 /* Otherwise, a mode is required. */
741 if (bsdtar->mode == '\0')
743 "Must specify one of -c, -r, -t, -u, -x");
745 /* Check boolean options only permitted in certain modes. */
746 if (bsdtar->flags & OPTFLAG_AUTO_COMPRESS)
747 only_mode(bsdtar, "-a", "c");
748 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
749 only_mode(bsdtar, "--one-file-system", "cru");
750 if (bsdtar->flags & OPTFLAG_FAST_READ)
751 only_mode(bsdtar, "--fast-read", "xt");
752 if (bsdtar->extract_flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED)
753 only_mode(bsdtar, "--hfsCompression", "x");
754 if (bsdtar->extract_flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION)
755 only_mode(bsdtar, "--nopreserveHFSCompression", "x");
756 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP)
757 only_mode(bsdtar, "--nodump", "cru");
758 if (bsdtar->flags & OPTFLAG_ACLS)
759 only_mode(bsdtar, "--acls", "crux");
760 if (bsdtar->flags & OPTFLAG_NO_ACLS)
761 only_mode(bsdtar, "--no-acls", "crux");
762 if (bsdtar->flags & OPTFLAG_XATTRS)
763 only_mode(bsdtar, "--xattrs", "crux");
764 if (bsdtar->flags & OPTFLAG_NO_XATTRS)
765 only_mode(bsdtar, "--no-xattrs", "crux");
766 if (bsdtar->flags & OPTFLAG_FFLAGS)
767 only_mode(bsdtar, "--fflags", "crux");
768 if (bsdtar->flags & OPTFLAG_NO_FFLAGS)
769 only_mode(bsdtar, "--no-fflags", "crux");
770 if (bsdtar->flags & OPTFLAG_MAC_METADATA)
771 only_mode(bsdtar, "--mac-metadata", "crux");
772 if (bsdtar->flags & OPTFLAG_NO_MAC_METADATA)
773 only_mode(bsdtar, "--no-mac-metadata", "crux");
774 if (bsdtar->flags & OPTFLAG_O) {
775 switch (bsdtar->mode) {
778 * In GNU tar, -o means "old format." The
779 * "ustar" format is the closest thing
780 * supported by libarchive.
782 cset_set_format(bsdtar->cset, "ustar");
783 /* TODO: bsdtar->create_format = "v7"; */
786 /* POSIX-compatible behavior. */
787 bsdtar->flags |= OPTFLAG_NO_OWNER;
788 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
791 only_mode(bsdtar, "-o", "xc");
795 if (bsdtar->flags & OPTFLAG_NO_SUBDIRS)
796 only_mode(bsdtar, "-n", "cru");
797 if (bsdtar->flags & OPTFLAG_STDOUT)
798 only_mode(bsdtar, "-O", "xt");
799 if (bsdtar->flags & OPTFLAG_UNLINK_FIRST)
800 only_mode(bsdtar, "-U", "x");
801 if (bsdtar->flags & OPTFLAG_WARN_LINKS)
802 only_mode(bsdtar, "--check-links", "cr");
804 if ((bsdtar->flags & OPTFLAG_AUTO_COMPRESS) &&
805 cset_auto_compress(bsdtar->cset, bsdtar->filename)) {
806 /* Ignore specified compressions if auto-compress works. */
810 /* Check other parameters only permitted in certain modes. */
811 if (compress_program != NULL) {
812 only_mode(bsdtar, "--use-compress-program", "cxt");
813 cset_add_filter_program(bsdtar->cset, compress_program);
814 /* Ignore specified compressions. */
818 if (compression != '\0') {
819 switch (compression) {
820 case 'J': case 'j': case 'y': case 'Z': case 'z':
822 buff[1] = compression;
826 strcat(buff, compression_name);
829 only_mode(bsdtar, buff, "cxt");
830 cset_add_filter(bsdtar->cset, compression_name);
832 if (compression2 != '\0') {
834 strcat(buff, compression2_name);
835 only_mode(bsdtar, buff, "cxt");
836 cset_add_filter(bsdtar->cset, compression2_name);
838 if (cset_get_format(bsdtar->cset) != NULL)
839 only_mode(bsdtar, "--format", "cru");
840 if (bsdtar->symlink_mode != '\0') {
842 buff[1] = bsdtar->symlink_mode;
843 only_mode(bsdtar, buff, "cru");
846 /* Filename "-" implies stdio. */
847 if (strcmp(bsdtar->filename, "-") == 0)
848 bsdtar->filename = NULL;
850 switch(bsdtar->mode) {
868 archive_match_free(bsdtar->matching);
869 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
870 cleanup_substitution(bsdtar);
872 cset_free(bsdtar->cset);
873 passphrase_free(bsdtar->ppbuff);
875 if (bsdtar->return_value != 0)
877 "Error exit delayed from previous errors.");
878 return (bsdtar->return_value);
882 set_mode(struct bsdtar *bsdtar, char opt)
884 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
886 "Can't specify both -%c and -%c", opt, bsdtar->mode);
891 * Verify that the mode is correct.
894 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
896 if (strchr(valid_modes, bsdtar->mode) == NULL)
898 "Option %s is not permitted in mode -%c",
908 p = lafe_getprogname();
910 fprintf(stderr, "Usage:\n");
911 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
912 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
913 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
914 fprintf(stderr, " Help: %s --help\n", p);
921 printf("bsdtar %s - %s\n",
922 BSDTAR_VERSION_STRING,
923 archive_version_details());
927 static const char *long_help_msg =
928 "First option must be a mode specifier:\n"
929 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
931 " -b # Use # 512-byte records per I/O block\n"
932 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
935 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
936 " <file>, <dir> add these items to archive\n"
937 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
938 " --format {ustar|pax|cpio|shar} Select archive format\n"
939 " --exclude <pattern> Skip files that match pattern\n"
940 " -C <dir> Change to <dir> before processing remaining files\n"
941 " @<archive> Add entries from <archive> to output\n"
942 "List: %p -t [options] [<patterns>]\n"
943 " <patterns> If specified, list only entries that match\n"
944 "Extract: %p -x [options] [<patterns>]\n"
945 " <patterns> If specified, extract only entries that match\n"
946 " -k Keep (don't overwrite) existing files\n"
947 " -m Don't restore modification times\n"
948 " -O Write entries to stdout, don't restore to disk\n"
949 " -p Restore permissions (including ACLs, owner, file flags)\n";
953 * Note that the word 'bsdtar' will always appear in the first line
956 * In particular, /bin/sh scripts that need to test for the presence
957 * of bsdtar can use the following template:
959 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
960 * echo bsdtar; else echo not bsdtar; fi
968 prog = lafe_getprogname();
972 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
973 printf("%s%s: manipulate archive files\n", prog, p);
975 for (p = long_help_msg; *p != '\0'; p++) {