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: src/usr.bin/tar/bsdtar.c,v 1.93 2008/11/08 04:43:24 kientzle Exp $");
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)
132 static char const * const vcs_files[] = {
142 ".git", ".gitignore", ".gitattributes", ".gitmodules",
144 ".arch-ids", "{arch}", "=RELEASE-ID", "=meta-update", "=update",
146 ".bzr", ".bzrignore", ".bzrtags",
148 ".hg", ".hgignore", ".hgtags",
155 main(int argc, char **argv)
157 struct bsdtar *bsdtar, bsdtar_storage;
159 char compression, compression2;
160 const char *compression_name, *compression2_name;
161 const char *compress_program;
163 char possible_help_request;
167 * Use a pointer for consistency, but stack-allocated storage
168 * for ease of cleanup.
170 bsdtar = &bsdtar_storage;
171 memset(bsdtar, 0, sizeof(*bsdtar));
172 bsdtar->fd = -1; /* Mark as "unused" */
176 compression = compression2 = '\0';
177 compression_name = compression2_name = NULL;
178 compress_program = NULL;
180 #if defined(HAVE_SIGACTION)
181 { /* Set up signal handling. */
183 sa.sa_handler = siginfo_handler;
184 sigemptyset(&sa.sa_mask);
187 if (sigaction(SIGINFO, &sa, NULL))
188 lafe_errc(1, errno, "sigaction(SIGINFO) failed");
191 /* ... and treat SIGUSR1 the same way as SIGINFO. */
192 if (sigaction(SIGUSR1, &sa, NULL))
193 lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
196 /* Ignore SIGPIPE signals. */
197 sa.sa_handler = SIG_IGN;
198 sigaction(SIGPIPE, &sa, NULL);
203 /* Set lafe_progname before calling lafe_warnc. */
204 lafe_setprogname(*argv, "bsdtar");
207 if (setlocale(LC_ALL, "") == NULL)
208 lafe_warnc(0, "Failed to set default locale");
210 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
211 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
213 possible_help_request = 0;
215 /* Look up uid of current user for future reference */
216 bsdtar->user_uid = geteuid();
218 /* Default: open tape drive. */
219 bsdtar->filename = getenv("TAPE");
220 if (bsdtar->filename == NULL)
221 bsdtar->filename = _PATH_DEFTAPE;
223 /* Default block size settings. */
224 bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK;
225 /* Allow library to default this unless user specifies -b. */
226 bsdtar->bytes_in_last_block = -1;
228 /* Default: preserve mod time on extract */
229 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
231 /* Default: Perform basic security checks. */
232 bsdtar->extract_flags |= SECURITY;
235 /* On POSIX systems, assume --same-owner and -p when run by
236 * the root user. This doesn't make any sense on Windows. */
237 if (bsdtar->user_uid == 0) {
239 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
241 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
242 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
243 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
244 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
245 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
250 * Enable Mac OS "copyfile()" extension by default.
251 * This has no effect on other platforms.
253 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
254 #ifdef COPYFILE_DISABLE_VAR
255 if (getenv(COPYFILE_DISABLE_VAR))
256 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
258 #if defined(__APPLE__)
260 * On Mac OS ACLs are archived with copyfile() (--mac-metadata)
261 * Translation to NFSv4 ACLs has to be requested explicitly with --acls
263 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL;
266 bsdtar->matching = archive_match_new();
267 if (bsdtar->matching == NULL)
268 lafe_errc(1, errno, "Out of memory");
269 bsdtar->cset = cset_new();
270 if (bsdtar->cset == NULL)
271 lafe_errc(1, errno, "Out of memory");
277 * Comments following each option indicate where that option
278 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
279 * no such comment, then I don't know of anyone else who
280 * implements that option.
282 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
284 case 'a': /* GNU tar */
285 bsdtar->flags |= OPTFLAG_AUTO_COMPRESS;
287 case OPTION_ACLS: /* GNU tar */
288 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
289 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_ACL;
290 bsdtar->flags |= OPTFLAG_ACLS;
292 case 'B': /* GNU tar */
293 /* libarchive doesn't need this; just ignore it. */
295 case 'b': /* SUSv2 */
298 t = (int)strtol(bsdtar->argument, &tptr, 10);
299 if (errno || t <= 0 || t > 8192 ||
300 *(bsdtar->argument) == '\0' || tptr == NULL ||
302 lafe_errc(1, 0, "Invalid or out of range "
303 "(1..8192) argument to -b");
305 bsdtar->bytes_per_block = 512 * t;
306 /* Explicit -b forces last block size. */
307 bsdtar->bytes_in_last_block = bsdtar->bytes_per_block;
309 case OPTION_B64ENCODE:
310 if (compression2 != '\0')
312 "Can't specify both --uuencode and "
315 compression2_name = "b64encode";
317 case 'C': /* GNU tar */
318 if (strlen(bsdtar->argument) == 0)
320 "Meaningless option: -C ''");
322 set_chdir(bsdtar, bsdtar->argument);
324 case 'c': /* SUSv2 */
325 set_mode(bsdtar, opt);
327 case OPTION_CHECK_LINKS: /* GNU tar */
328 bsdtar->flags |= OPTFLAG_WARN_LINKS;
330 case OPTION_CHROOT: /* NetBSD */
331 bsdtar->flags |= OPTFLAG_CHROOT;
333 case OPTION_CLEAR_NOCHANGE_FFLAGS:
334 bsdtar->extract_flags |=
335 ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS;
337 case OPTION_EXCLUDE: /* GNU tar */
338 if (archive_match_exclude_pattern(
339 bsdtar->matching, bsdtar->argument) != ARCHIVE_OK)
341 "Couldn't exclude %s\n", bsdtar->argument);
343 case OPTION_EXCLUDE_VCS: /* GNU tar */
344 for(t=0; vcs_files[t]; t++) {
345 if (archive_match_exclude_pattern(
347 vcs_files[t]) != ARCHIVE_OK)
348 lafe_errc(1, 0, "Couldn't "
349 "exclude %s\n", vcs_files[t]);
353 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
354 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_FFLAGS;
355 bsdtar->flags |= OPTFLAG_FFLAGS;
357 case OPTION_FORMAT: /* GNU tar, others */
358 cset_set_format(bsdtar->cset, bsdtar->argument);
360 case 'f': /* SUSv2 */
361 bsdtar->filename = bsdtar->argument;
363 case OPTION_GID: /* cpio */
366 t = (int)strtol(bsdtar->argument, &tptr, 10);
367 if (errno || t < 0 || *(bsdtar->argument) == '\0' ||
368 tptr == NULL || *tptr != '\0') {
369 lafe_errc(1, 0, "Invalid argument to --gid");
373 case OPTION_GNAME: /* cpio */
374 bsdtar->gname = bsdtar->argument;
377 if (compression != '\0')
379 "Can't specify both -%c and -%c", opt,
382 compression_name = "grzip";
384 case 'H': /* BSD convention */
385 bsdtar->symlink_mode = 'H';
387 case 'h': /* Linux Standards Base, gtar; synonym for -L */
388 bsdtar->symlink_mode = 'L';
389 /* Hack: -h by itself is the "help" command. */
390 possible_help_request = 1;
392 case OPTION_HELP: /* GNU tar, others */
396 case OPTION_HFS_COMPRESSION: /* Mac OS X v10.6 or later */
397 bsdtar->extract_flags |=
398 ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED;
400 case OPTION_IGNORE_ZEROS:
401 bsdtar->flags |= OPTFLAG_IGNORE_ZEROS;
403 case 'I': /* GNU tar */
405 * TODO: Allow 'names' to come from an archive,
406 * not just a text file. Design a good UI for
407 * allowing names and mode/owner to be read
408 * from an archive, with contents coming from
409 * disk. This can be used to "refresh" an
410 * archive or to design archives with special
411 * permissions without having to create those
412 * permissions on disk.
414 bsdtar->names_from_file = bsdtar->argument;
418 * No one else has the @archive extension, so
419 * no one else needs this to filter entries
420 * when transforming archives.
422 if (archive_match_include_pattern(bsdtar->matching,
423 bsdtar->argument) != ARCHIVE_OK)
425 "Failed to add %s to inclusion list",
428 case 'j': /* GNU tar */
429 if (compression != '\0')
431 "Can't specify both -%c and -%c", opt,
434 compression_name = "bzip2";
436 case 'J': /* GNU tar 1.21 and later */
437 if (compression != '\0')
439 "Can't specify both -%c and -%c", opt,
442 compression_name = "xz";
444 case 'k': /* GNU tar */
445 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
447 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
448 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
450 case 'L': /* BSD convention */
451 bsdtar->symlink_mode = 'L';
453 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
454 /* GNU tar 1.13 used -l for --one-file-system */
455 bsdtar->flags |= OPTFLAG_WARN_LINKS;
459 case OPTION_LZIP: /* GNU tar beginning with 1.23 */
460 case OPTION_LZMA: /* GNU tar beginning with 1.20 */
461 case OPTION_LZOP: /* GNU tar beginning with 1.21 */
463 if (compression != '\0')
465 "Can't specify both -%c and -%c", opt,
469 case OPTION_LRZIP: compression_name = "lrzip"; break;
470 case OPTION_LZ4: compression_name = "lz4"; break;
471 case OPTION_LZIP: compression_name = "lzip"; break;
472 case OPTION_LZMA: compression_name = "lzma"; break;
473 case OPTION_LZOP: compression_name = "lzop"; break;
474 case OPTION_ZSTD: compression_name = "zstd"; break;
477 case 'm': /* SUSv2 */
478 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
480 case OPTION_MAC_METADATA: /* Mac OS X */
481 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
482 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
483 bsdtar->flags |= OPTFLAG_MAC_METADATA;
485 case 'n': /* GNU tar */
486 bsdtar->flags |= OPTFLAG_NO_SUBDIRS;
489 * Selecting files by time:
490 * --newer-?time='date' Only files newer than 'date'
491 * --newer-?time-than='file' Only files newer than time
492 * on specified file (useful for incremental backups)
494 case OPTION_NEWER_CTIME: /* GNU tar */
495 if (archive_match_include_date(bsdtar->matching,
496 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
497 bsdtar->argument) != ARCHIVE_OK)
498 lafe_errc(1, 0, "Error : %s",
499 archive_error_string(bsdtar->matching));
501 case OPTION_NEWER_CTIME_THAN:
502 if (archive_match_include_file_time(bsdtar->matching,
503 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
504 bsdtar->argument) != ARCHIVE_OK)
505 lafe_errc(1, 0, "Error : %s",
506 archive_error_string(bsdtar->matching));
508 case OPTION_NEWER_MTIME: /* GNU tar */
509 if (archive_match_include_date(bsdtar->matching,
510 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
511 bsdtar->argument) != ARCHIVE_OK)
512 lafe_errc(1, 0, "Error : %s",
513 archive_error_string(bsdtar->matching));
515 case OPTION_NEWER_MTIME_THAN:
516 if (archive_match_include_file_time(bsdtar->matching,
517 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
518 bsdtar->argument) != ARCHIVE_OK)
519 lafe_errc(1, 0, "Error : %s",
520 archive_error_string(bsdtar->matching));
522 case OPTION_NODUMP: /* star */
523 bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP;
525 case OPTION_NOPRESERVE_HFS_COMPRESSION:
526 /* Mac OS X v10.6 or later */
527 bsdtar->extract_flags |=
528 ARCHIVE_EXTRACT_NO_HFS_COMPRESSION;
530 case OPTION_NO_ACLS: /* GNU tar */
531 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
532 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL;
533 bsdtar->flags |= OPTFLAG_NO_ACLS;
535 case OPTION_NO_FFLAGS:
536 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
537 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_FFLAGS;
538 bsdtar->flags |= OPTFLAG_NO_FFLAGS;
540 case OPTION_NO_MAC_METADATA: /* Mac OS X */
541 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
542 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
543 bsdtar->flags |= OPTFLAG_NO_MAC_METADATA;
545 case OPTION_NO_SAFE_WRITES:
546 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_SAFE_WRITES;
548 case OPTION_NO_SAME_OWNER: /* GNU tar */
549 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
551 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
552 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
553 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
554 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
555 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
556 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
558 case OPTION_NO_XATTRS: /* GNU tar */
559 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
560 bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_XATTR;
561 bsdtar->flags |= OPTFLAG_NO_XATTRS;
563 case OPTION_NULL: /* GNU tar */
564 bsdtar->flags |= OPTFLAG_NULL;
566 case OPTION_NUMERIC_OWNER: /* GNU tar */
569 bsdtar->flags |= OPTFLAG_NUMERIC_OWNER;
571 case 'O': /* GNU tar */
572 bsdtar->flags |= OPTFLAG_STDOUT;
574 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
575 bsdtar->flags |= OPTFLAG_O;
578 * Selecting files by time:
579 * --older-?time='date' Only files older than 'date'
580 * --older-?time-than='file' Only files older than time
583 case OPTION_OLDER_CTIME:
584 if (archive_match_include_date(bsdtar->matching,
585 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
586 bsdtar->argument) != ARCHIVE_OK)
587 lafe_errc(1, 0, "Error : %s",
588 archive_error_string(bsdtar->matching));
590 case OPTION_OLDER_CTIME_THAN:
591 if (archive_match_include_file_time(bsdtar->matching,
592 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
593 bsdtar->argument) != ARCHIVE_OK)
594 lafe_errc(1, 0, "Error : %s",
595 archive_error_string(bsdtar->matching));
597 case OPTION_OLDER_MTIME:
598 if (archive_match_include_date(bsdtar->matching,
599 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
600 bsdtar->argument) != ARCHIVE_OK)
601 lafe_errc(1, 0, "Error : %s",
602 archive_error_string(bsdtar->matching));
604 case OPTION_OLDER_MTIME_THAN:
605 if (archive_match_include_file_time(bsdtar->matching,
606 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
607 bsdtar->argument) != ARCHIVE_OK)
608 lafe_errc(1, 0, "Error : %s",
609 archive_error_string(bsdtar->matching));
611 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
612 bsdtar->readdisk_flags |=
613 ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;
616 bsdtar->option_options = bsdtar->argument;
620 * The common BSD -P option is not necessary, since
621 * our default is to archive symlinks, not follow
622 * them. This is convenient, as -P conflicts with GNU
625 case 'P': /* BSD convention */
626 /* Default behavior, no option necessary. */
629 case 'P': /* GNU tar */
630 bsdtar->extract_flags &= ~SECURITY;
631 bsdtar->flags |= OPTFLAG_ABSOLUTE_PATHS;
633 case 'p': /* GNU tar, star */
634 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
635 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
636 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
637 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
638 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
640 case OPTION_PASSPHRASE:
641 bsdtar->passphrase = bsdtar->argument;
643 case OPTION_POSIX: /* GNU tar */
644 cset_set_format(bsdtar->cset, "pax");
646 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
647 bsdtar->flags |= OPTFLAG_FAST_READ;
649 case 'r': /* SUSv2 */
650 set_mode(bsdtar, opt);
652 case 'S': /* NetBSD pax-as-tar */
653 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
655 case 's': /* NetBSD pax-as-tar */
656 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
657 add_substitution(bsdtar, bsdtar->argument);
660 "-s is not supported by this version of bsdtar");
664 case OPTION_SAFE_WRITES:
665 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SAFE_WRITES;
667 case OPTION_SAME_OWNER: /* GNU tar */
668 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
670 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
673 t = (int)strtol(bsdtar->argument, &tptr, 10);
674 if (errno || t < 0 || *(bsdtar->argument) == '\0' ||
675 tptr == NULL || *tptr != '\0') {
676 lafe_errc(1, 0, "Invalid argument to "
677 "--strip-components");
679 bsdtar->strip_components = t;
681 case 'T': /* GNU tar */
682 bsdtar->names_from_file = bsdtar->argument;
684 case 't': /* SUSv2 */
685 set_mode(bsdtar, opt);
688 case OPTION_TOTALS: /* GNU tar */
689 bsdtar->flags |= OPTFLAG_TOTALS;
691 case 'U': /* GNU tar */
692 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
693 bsdtar->flags |= OPTFLAG_UNLINK_FIRST;
695 case 'u': /* SUSv2 */
696 set_mode(bsdtar, opt);
698 case OPTION_UID: /* cpio */
701 t = (int)strtol(bsdtar->argument, &tptr, 10);
702 if (errno || t < 0 || *(bsdtar->argument) == '\0' ||
703 tptr == NULL || *tptr != '\0') {
704 lafe_errc(1, 0, "Invalid argument to --uid");
708 case OPTION_UNAME: /* cpio */
709 bsdtar->uname = bsdtar->argument;
711 case OPTION_UUENCODE:
712 if (compression2 != '\0')
714 "Can't specify both --uuencode and "
717 compression2_name = "uuencode";
719 case 'v': /* SUSv2 */
722 case OPTION_VERSION: /* GNU convention */
727 * The -W longopt feature is handled inside of
728 * bsdtar_getopt(), so -W is not available here.
730 case 'W': /* Obscure GNU convention. */
733 case 'w': /* SUSv2 */
734 bsdtar->flags |= OPTFLAG_INTERACTIVE;
736 case 'X': /* GNU tar */
737 if (archive_match_exclude_pattern_from_file(
738 bsdtar->matching, bsdtar->argument, 0)
740 lafe_errc(1, 0, "Error : %s",
741 archive_error_string(bsdtar->matching));
743 case 'x': /* SUSv2 */
744 set_mode(bsdtar, opt);
746 case OPTION_XATTRS: /* GNU tar */
747 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
748 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_XATTR;
749 bsdtar->flags |= OPTFLAG_XATTRS;
751 case 'y': /* FreeBSD version of GNU tar */
752 if (compression != '\0')
754 "Can't specify both -%c and -%c", opt,
757 compression_name = "bzip2";
759 case 'Z': /* GNU tar */
760 if (compression != '\0')
762 "Can't specify both -%c and -%c", opt,
765 compression_name = "compress";
767 case 'z': /* GNU tar, star, many others */
768 if (compression != '\0')
770 "Can't specify both -%c and -%c", opt,
773 compression_name = "gzip";
775 case OPTION_USE_COMPRESS_PROGRAM:
776 compress_program = bsdtar->argument;
784 * Sanity-check options.
787 /* If no "real" mode was specified, treat -h as --help. */
788 if ((bsdtar->mode == '\0') && possible_help_request) {
793 /* Otherwise, a mode is required. */
794 if (bsdtar->mode == '\0')
796 "Must specify one of -c, -r, -t, -u, -x");
798 /* Check boolean options only permitted in certain modes. */
799 if (bsdtar->flags & OPTFLAG_AUTO_COMPRESS)
800 only_mode(bsdtar, "-a", "c");
801 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
802 only_mode(bsdtar, "--one-file-system", "cru");
803 if (bsdtar->flags & OPTFLAG_FAST_READ)
804 only_mode(bsdtar, "--fast-read", "xt");
805 if (bsdtar->extract_flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED)
806 only_mode(bsdtar, "--hfsCompression", "x");
807 if (bsdtar->extract_flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION)
808 only_mode(bsdtar, "--nopreserveHFSCompression", "x");
809 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP)
810 only_mode(bsdtar, "--nodump", "cru");
811 if (bsdtar->flags & OPTFLAG_ACLS)
812 only_mode(bsdtar, "--acls", "crux");
813 if (bsdtar->flags & OPTFLAG_NO_ACLS)
814 only_mode(bsdtar, "--no-acls", "crux");
815 if (bsdtar->flags & OPTFLAG_XATTRS)
816 only_mode(bsdtar, "--xattrs", "crux");
817 if (bsdtar->flags & OPTFLAG_NO_XATTRS)
818 only_mode(bsdtar, "--no-xattrs", "crux");
819 if (bsdtar->flags & OPTFLAG_FFLAGS)
820 only_mode(bsdtar, "--fflags", "crux");
821 if (bsdtar->flags & OPTFLAG_NO_FFLAGS)
822 only_mode(bsdtar, "--no-fflags", "crux");
823 if (bsdtar->flags & OPTFLAG_MAC_METADATA)
824 only_mode(bsdtar, "--mac-metadata", "crux");
825 if (bsdtar->flags & OPTFLAG_NO_MAC_METADATA)
826 only_mode(bsdtar, "--no-mac-metadata", "crux");
827 if (bsdtar->flags & OPTFLAG_O) {
828 switch (bsdtar->mode) {
831 * In GNU tar, -o means "old format." The
832 * "ustar" format is the closest thing
833 * supported by libarchive.
835 cset_set_format(bsdtar->cset, "ustar");
836 /* TODO: bsdtar->create_format = "v7"; */
839 /* POSIX-compatible behavior. */
840 bsdtar->flags |= OPTFLAG_NO_OWNER;
841 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
844 only_mode(bsdtar, "-o", "xc");
848 if (bsdtar->flags & OPTFLAG_STDOUT)
849 only_mode(bsdtar, "-O", "xt");
850 if (bsdtar->flags & OPTFLAG_UNLINK_FIRST)
851 only_mode(bsdtar, "-U", "x");
852 if (bsdtar->flags & OPTFLAG_WARN_LINKS)
853 only_mode(bsdtar, "--check-links", "cr");
855 if ((bsdtar->flags & OPTFLAG_AUTO_COMPRESS) &&
856 cset_auto_compress(bsdtar->cset, bsdtar->filename)) {
857 /* Ignore specified compressions if auto-compress works. */
861 /* Check other parameters only permitted in certain modes. */
862 if (compress_program != NULL) {
863 only_mode(bsdtar, "--use-compress-program", "cxt");
864 cset_add_filter_program(bsdtar->cset, compress_program);
865 /* Ignore specified compressions. */
869 if (compression != '\0') {
870 switch (compression) {
871 case 'J': case 'j': case 'y': case 'Z': case 'z':
873 buff[1] = compression;
877 strcat(buff, compression_name);
880 only_mode(bsdtar, buff, "cxt");
881 cset_add_filter(bsdtar->cset, compression_name);
883 if (compression2 != '\0') {
885 strcat(buff, compression2_name);
886 only_mode(bsdtar, buff, "cxt");
887 cset_add_filter(bsdtar->cset, compression2_name);
889 if (cset_get_format(bsdtar->cset) != NULL)
890 only_mode(bsdtar, "--format", "cru");
891 if (bsdtar->symlink_mode != '\0') {
893 buff[1] = bsdtar->symlink_mode;
894 only_mode(bsdtar, buff, "cru");
898 * When creating an archive from a directory tree, the directory
899 * walking code will already avoid entering directories when
900 * recursive inclusion of directory content is disabled, therefore
901 * changing the matching behavior has no effect for creation modes.
902 * It is relevant for extraction or listing.
904 archive_match_set_inclusion_recursion(bsdtar->matching,
905 !(bsdtar->flags & OPTFLAG_NO_SUBDIRS));
907 /* Filename "-" implies stdio. */
908 if (strcmp(bsdtar->filename, "-") == 0)
909 bsdtar->filename = NULL;
911 switch(bsdtar->mode) {
929 archive_match_free(bsdtar->matching);
930 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
931 cleanup_substitution(bsdtar);
933 cset_free(bsdtar->cset);
934 passphrase_free(bsdtar->ppbuff);
936 if (bsdtar->return_value != 0)
938 "Error exit delayed from previous errors.");
939 return (bsdtar->return_value);
943 set_mode(struct bsdtar *bsdtar, char opt)
945 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
947 "Can't specify both -%c and -%c", opt, bsdtar->mode);
952 * Verify that the mode is correct.
955 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
957 if (strchr(valid_modes, bsdtar->mode) == NULL)
959 "Option %s is not permitted in mode -%c",
969 p = lafe_getprogname();
971 fprintf(stderr, "Usage:\n");
972 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
973 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
974 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
975 fprintf(stderr, " Help: %s --help\n", p);
982 printf("bsdtar %s - %s \n",
983 BSDTAR_VERSION_STRING,
984 archive_version_details());
988 static const char *long_help_msg =
989 "First option must be a mode specifier:\n"
990 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
992 " -b # Use # 512-byte records per I/O block\n"
993 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
996 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
997 " <file>, <dir> add these items to archive\n"
998 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
999 " --format {ustar|pax|cpio|shar} Select archive format\n"
1000 " --exclude <pattern> Skip files that match pattern\n"
1001 " -C <dir> Change to <dir> before processing remaining files\n"
1002 " @<archive> Add entries from <archive> to output\n"
1003 "List: %p -t [options] [<patterns>]\n"
1004 " <patterns> If specified, list only entries that match\n"
1005 "Extract: %p -x [options] [<patterns>]\n"
1006 " <patterns> If specified, extract only entries that match\n"
1007 " -k Keep (don't overwrite) existing files\n"
1008 " -m Don't restore modification times\n"
1009 " -O Write entries to stdout, don't restore to disk\n"
1010 " -p Restore permissions (including ACLs, owner, file flags)\n";
1014 * Note that the word 'bsdtar' will always appear in the first line
1017 * In particular, /bin/sh scripts that need to test for the presence
1018 * of bsdtar can use the following template:
1020 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
1021 * echo bsdtar; else echo not bsdtar; fi
1029 prog = lafe_getprogname();
1033 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
1034 printf("%s%s: manipulate archive files\n", prog, p);
1036 for (p = long_help_msg; *p != '\0'; p++) {
1039 fputs(prog, stdout);