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);
122 static void only_mode(struct bsdtar
*, const char *opt
,
124 static void set_mode(struct bsdtar
*, char opt
);
125 static void version(void);
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 option_a
, option_o
;
141 char possible_help_request
;
145 * Use a pointer for consistency, but stack-allocated storage
146 * for ease of cleanup.
148 bsdtar
= &bsdtar_storage
;
149 memset(bsdtar
, 0, sizeof(*bsdtar
));
150 bsdtar
->fd
= -1; /* Mark as "unused" */
153 option_a
= option_o
= 0;
154 compression
= compression2
= '\0';
155 compression_name
= compression2_name
= NULL
;
156 compress_program
= NULL
;
158 #if defined(HAVE_SIGACTION)
159 { /* Set up signal handling. */
161 sa
.sa_handler
= siginfo_handler
;
162 sigemptyset(&sa
.sa_mask
);
165 if (sigaction(SIGINFO
, &sa
, NULL
))
166 lafe_errc(1, errno
, "sigaction(SIGINFO) failed");
169 /* ... and treat SIGUSR1 the same way as SIGINFO. */
170 if (sigaction(SIGUSR1
, &sa
, NULL
))
171 lafe_errc(1, errno
, "sigaction(SIGUSR1) failed");
174 /* Ignore SIGPIPE signals. */
175 sa
.sa_handler
= SIG_IGN
;
176 sigaction(SIGPIPE
, &sa
, NULL
);
181 /* Set lafe_progname before calling lafe_warnc. */
182 lafe_setprogname(*argv
, "bsdtar");
185 if (setlocale(LC_ALL
, "") == NULL
)
186 lafe_warnc(0, "Failed to set default locale");
188 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
189 bsdtar
->day_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
191 possible_help_request
= 0;
193 /* Look up uid of current user for future reference */
194 bsdtar
->user_uid
= geteuid();
196 /* Default: open tape drive. */
197 bsdtar
->filename
= getenv("TAPE");
198 if (bsdtar
->filename
== NULL
)
199 bsdtar
->filename
= _PATH_DEFTAPE
;
201 /* Default block size settings. */
202 bsdtar
->bytes_per_block
= DEFAULT_BYTES_PER_BLOCK
;
203 /* Allow library to default this unless user specifies -b. */
204 bsdtar
->bytes_in_last_block
= -1;
206 /* Default: preserve mod time on extract */
207 bsdtar
->extract_flags
= ARCHIVE_EXTRACT_TIME
;
209 /* Default: Perform basic security checks. */
210 bsdtar
->extract_flags
|= SECURITY
;
213 /* On POSIX systems, assume --same-owner and -p when run by
214 * the root user. This doesn't make any sense on Windows. */
215 if (bsdtar
->user_uid
== 0) {
217 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_OWNER
;
219 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_PERM
;
220 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_ACL
;
221 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_XATTR
;
222 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_FFLAGS
;
223 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_MAC_METADATA
;
228 * Enable Mac OS "copyfile()" extension by default.
229 * This has no effect on other platforms.
231 bsdtar
->readdisk_flags
|= ARCHIVE_READDISK_MAC_COPYFILE
;
232 #ifdef COPYFILE_DISABLE_VAR
233 if (getenv(COPYFILE_DISABLE_VAR
))
234 bsdtar
->readdisk_flags
&= ~ARCHIVE_READDISK_MAC_COPYFILE
;
236 bsdtar
->matching
= archive_match_new();
237 if (bsdtar
->matching
== NULL
)
238 lafe_errc(1, errno
, "Out of memory");
239 bsdtar
->cset
= cset_new();
240 if (bsdtar
->cset
== NULL
)
241 lafe_errc(1, errno
, "Out of memory");
247 * Comments following each option indicate where that option
248 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
249 * no such comment, then I don't know of anyone else who
250 * implements that option.
252 while ((opt
= bsdtar_getopt(bsdtar
)) != -1) {
254 case 'a': /* GNU tar */
255 option_a
= 1; /* Record it and resolve it later. */
257 case 'B': /* GNU tar */
258 /* libarchive doesn't need this; just ignore it. */
260 case 'b': /* SUSv2 */
261 t
= atoi(bsdtar
->argument
);
262 if (t
<= 0 || t
> 8192)
264 "Argument to -b is out of range (1..8192)");
265 bsdtar
->bytes_per_block
= 512 * t
;
266 /* Explicit -b forces last block size. */
267 bsdtar
->bytes_in_last_block
= bsdtar
->bytes_per_block
;
269 case OPTION_B64ENCODE
:
270 if (compression2
!= '\0')
272 "Can't specify both --uuencode and "
275 compression2_name
= "b64encode";
277 case 'C': /* GNU tar */
278 if (strlen(bsdtar
->argument
) == 0)
280 "Meaningless option: -C ''");
282 set_chdir(bsdtar
, bsdtar
->argument
);
284 case 'c': /* SUSv2 */
285 set_mode(bsdtar
, opt
);
287 case OPTION_CHECK_LINKS
: /* GNU tar */
288 bsdtar
->option_warn_links
= 1;
290 case OPTION_CHROOT
: /* NetBSD */
291 bsdtar
->option_chroot
= 1;
293 case OPTION_CLEAR_NOCHANGE_FFLAGS
:
294 bsdtar
->extract_flags
|=
295 ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS
;
297 case OPTION_DISABLE_COPYFILE
: /* Mac OS X */
298 bsdtar
->readdisk_flags
&= ~ARCHIVE_READDISK_MAC_COPYFILE
;
300 case OPTION_EXCLUDE
: /* GNU tar */
301 if (archive_match_exclude_pattern(
302 bsdtar
->matching
, bsdtar
->argument
) != ARCHIVE_OK
)
304 "Couldn't exclude %s\n", bsdtar
->argument
);
306 case OPTION_FORMAT
: /* GNU tar, others */
307 cset_set_format(bsdtar
->cset
, bsdtar
->argument
);
309 case 'f': /* SUSv2 */
310 bsdtar
->filename
= bsdtar
->argument
;
312 case OPTION_GID
: /* cpio */
313 t
= atoi(bsdtar
->argument
);
316 "Argument to --gid must be positive");
319 case OPTION_GNAME
: /* cpio */
320 bsdtar
->gname
= bsdtar
->argument
;
323 if (compression
!= '\0')
325 "Can't specify both -%c and -%c", opt
,
328 compression_name
= "grzip";
330 case 'H': /* BSD convention */
331 bsdtar
->symlink_mode
= 'H';
333 case 'h': /* Linux Standards Base, gtar; synonym for -L */
334 bsdtar
->symlink_mode
= 'L';
335 /* Hack: -h by itself is the "help" command. */
336 possible_help_request
= 1;
338 case OPTION_HELP
: /* GNU tar, others */
342 case OPTION_HFS_COMPRESSION
: /* Mac OS X v10.6 or later */
343 bsdtar
->extract_flags
|=
344 ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED
;
346 case OPTION_IGNORE_ZEROS
:
347 bsdtar
->option_ignore_zeros
= 1;
349 case 'I': /* GNU tar */
351 * TODO: Allow 'names' to come from an archive,
352 * not just a text file. Design a good UI for
353 * allowing names and mode/owner to be read
354 * from an archive, with contents coming from
355 * disk. This can be used to "refresh" an
356 * archive or to design archives with special
357 * permissions without having to create those
358 * permissions on disk.
360 bsdtar
->names_from_file
= bsdtar
->argument
;
364 * No one else has the @archive extension, so
365 * no one else needs this to filter entries
366 * when transforming archives.
368 if (archive_match_include_pattern(bsdtar
->matching
,
369 bsdtar
->argument
) != ARCHIVE_OK
)
371 "Failed to add %s to inclusion list",
374 case 'j': /* GNU tar */
375 if (compression
!= '\0')
377 "Can't specify both -%c and -%c", opt
,
380 compression_name
= "bzip2";
382 case 'J': /* GNU tar 1.21 and later */
383 if (compression
!= '\0')
385 "Can't specify both -%c and -%c", opt
,
388 compression_name
= "xz";
390 case 'k': /* GNU tar */
391 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_NO_OVERWRITE
;
393 case OPTION_KEEP_NEWER_FILES
: /* GNU tar */
394 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER
;
396 case 'L': /* BSD convention */
397 bsdtar
->symlink_mode
= 'L';
399 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
400 /* GNU tar 1.13 used -l for --one-file-system */
401 bsdtar
->option_warn_links
= 1;
405 case OPTION_LZIP
: /* GNU tar beginning with 1.23 */
406 case OPTION_LZMA
: /* GNU tar beginning with 1.20 */
407 case OPTION_LZOP
: /* GNU tar beginning with 1.21 */
408 if (compression
!= '\0')
410 "Can't specify both -%c and -%c", opt
,
414 case OPTION_LRZIP
: compression_name
= "lrzip"; break;
415 case OPTION_LZ4
: compression_name
= "lz4"; break;
416 case OPTION_LZIP
: compression_name
= "lzip"; break;
417 case OPTION_LZMA
: compression_name
= "lzma"; break;
418 case OPTION_LZOP
: compression_name
= "lzop"; break;
421 case 'm': /* SUSv2 */
422 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_TIME
;
424 case 'n': /* GNU tar */
425 bsdtar
->option_no_subdirs
= 1;
428 * Selecting files by time:
429 * --newer-?time='date' Only files newer than 'date'
430 * --newer-?time-than='file' Only files newer than time
431 * on specified file (useful for incremental backups)
433 case OPTION_NEWER_CTIME
: /* GNU tar */
434 if (archive_match_include_date(bsdtar
->matching
,
435 ARCHIVE_MATCH_CTIME
| ARCHIVE_MATCH_NEWER
,
436 bsdtar
->argument
) != ARCHIVE_OK
)
437 lafe_errc(1, 0, "Error : %s",
438 archive_error_string(bsdtar
->matching
));
440 case OPTION_NEWER_CTIME_THAN
:
441 if (archive_match_include_file_time(bsdtar
->matching
,
442 ARCHIVE_MATCH_CTIME
| ARCHIVE_MATCH_NEWER
,
443 bsdtar
->argument
) != ARCHIVE_OK
)
444 lafe_errc(1, 0, "Error : %s",
445 archive_error_string(bsdtar
->matching
));
447 case OPTION_NEWER_MTIME
: /* GNU tar */
448 if (archive_match_include_date(bsdtar
->matching
,
449 ARCHIVE_MATCH_MTIME
| ARCHIVE_MATCH_NEWER
,
450 bsdtar
->argument
) != ARCHIVE_OK
)
451 lafe_errc(1, 0, "Error : %s",
452 archive_error_string(bsdtar
->matching
));
454 case OPTION_NEWER_MTIME_THAN
:
455 if (archive_match_include_file_time(bsdtar
->matching
,
456 ARCHIVE_MATCH_MTIME
| ARCHIVE_MATCH_NEWER
,
457 bsdtar
->argument
) != ARCHIVE_OK
)
458 lafe_errc(1, 0, "Error : %s",
459 archive_error_string(bsdtar
->matching
));
461 case OPTION_NODUMP
: /* star */
462 bsdtar
->readdisk_flags
|= ARCHIVE_READDISK_HONOR_NODUMP
;
464 case OPTION_NOPRESERVE_HFS_COMPRESSION
:
465 /* Mac OS X v10.6 or later */
466 bsdtar
->extract_flags
|=
467 ARCHIVE_EXTRACT_NO_HFS_COMPRESSION
;
469 case OPTION_NO_SAME_OWNER
: /* GNU tar */
470 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
472 case OPTION_NO_SAME_PERMISSIONS
: /* GNU tar */
473 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_PERM
;
474 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_ACL
;
475 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_XATTR
;
476 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_FFLAGS
;
477 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_MAC_METADATA
;
479 case OPTION_NO_XATTR
: /* Issue #131 */
480 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_XATTR
;
481 bsdtar
->readdisk_flags
|= ARCHIVE_READDISK_NO_XATTR
;
483 case OPTION_NULL
: /* GNU tar */
484 bsdtar
->option_null
++;
486 case OPTION_NUMERIC_OWNER
: /* GNU tar */
489 bsdtar
->option_numeric_owner
++;
491 case 'O': /* GNU tar */
492 bsdtar
->option_stdout
= 1;
494 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
495 option_o
= 1; /* Record it and resolve it later. */
498 * Selecting files by time:
499 * --older-?time='date' Only files older than 'date'
500 * --older-?time-than='file' Only files older than time
503 case OPTION_OLDER_CTIME
:
504 if (archive_match_include_date(bsdtar
->matching
,
505 ARCHIVE_MATCH_CTIME
| ARCHIVE_MATCH_OLDER
,
506 bsdtar
->argument
) != ARCHIVE_OK
)
507 lafe_errc(1, 0, "Error : %s",
508 archive_error_string(bsdtar
->matching
));
510 case OPTION_OLDER_CTIME_THAN
:
511 if (archive_match_include_file_time(bsdtar
->matching
,
512 ARCHIVE_MATCH_CTIME
| ARCHIVE_MATCH_OLDER
,
513 bsdtar
->argument
) != ARCHIVE_OK
)
514 lafe_errc(1, 0, "Error : %s",
515 archive_error_string(bsdtar
->matching
));
517 case OPTION_OLDER_MTIME
:
518 if (archive_match_include_date(bsdtar
->matching
,
519 ARCHIVE_MATCH_MTIME
| ARCHIVE_MATCH_OLDER
,
520 bsdtar
->argument
) != ARCHIVE_OK
)
521 lafe_errc(1, 0, "Error : %s",
522 archive_error_string(bsdtar
->matching
));
524 case OPTION_OLDER_MTIME_THAN
:
525 if (archive_match_include_file_time(bsdtar
->matching
,
526 ARCHIVE_MATCH_MTIME
| ARCHIVE_MATCH_OLDER
,
527 bsdtar
->argument
) != ARCHIVE_OK
)
528 lafe_errc(1, 0, "Error : %s",
529 archive_error_string(bsdtar
->matching
));
531 case OPTION_ONE_FILE_SYSTEM
: /* GNU tar */
532 bsdtar
->readdisk_flags
|=
533 ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS
;
536 bsdtar
->option_options
= bsdtar
->argument
;
540 * The common BSD -P option is not necessary, since
541 * our default is to archive symlinks, not follow
542 * them. This is convenient, as -P conflicts with GNU
545 case 'P': /* BSD convention */
546 /* Default behavior, no option necessary. */
549 case 'P': /* GNU tar */
550 bsdtar
->extract_flags
&= ~SECURITY
;
551 bsdtar
->option_absolute_paths
= 1;
553 case 'p': /* GNU tar, star */
554 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_PERM
;
555 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_ACL
;
556 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_XATTR
;
557 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_FFLAGS
;
558 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_MAC_METADATA
;
560 case OPTION_PASSPHRASE
:
561 bsdtar
->passphrase
= bsdtar
->argument
;
563 case OPTION_POSIX
: /* GNU tar */
564 cset_set_format(bsdtar
->cset
, "pax");
566 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
567 bsdtar
->option_fast_read
= 1;
569 case 'r': /* SUSv2 */
570 set_mode(bsdtar
, opt
);
572 case 'S': /* NetBSD pax-as-tar */
573 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_SPARSE
;
575 case 's': /* NetBSD pax-as-tar */
576 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
577 add_substitution(bsdtar
, bsdtar
->argument
);
580 "-s is not supported by this version of bsdtar");
584 case OPTION_SAME_OWNER
: /* GNU tar */
585 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_OWNER
;
587 case OPTION_STRIP_COMPONENTS
: /* GNU tar 1.15 */
589 bsdtar
->strip_components
= strtol(bsdtar
->argument
,
593 "Invalid --strip-components argument: %s",
596 case 'T': /* GNU tar */
597 bsdtar
->names_from_file
= bsdtar
->argument
;
599 case 't': /* SUSv2 */
600 set_mode(bsdtar
, opt
);
603 case OPTION_TOTALS
: /* GNU tar */
604 bsdtar
->option_totals
++;
606 case 'U': /* GNU tar */
607 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_UNLINK
;
608 bsdtar
->option_unlink_first
= 1;
610 case 'u': /* SUSv2 */
611 set_mode(bsdtar
, opt
);
613 case OPTION_UID
: /* cpio */
614 t
= atoi(bsdtar
->argument
);
617 "Argument to --uid must be positive");
620 case OPTION_UNAME
: /* cpio */
621 bsdtar
->uname
= bsdtar
->argument
;
623 case OPTION_UUENCODE
:
624 if (compression2
!= '\0')
626 "Can't specify both --uuencode and "
629 compression2_name
= "uuencode";
631 case 'v': /* SUSv2 */
634 case OPTION_VERSION
: /* GNU convention */
639 * The -W longopt feature is handled inside of
640 * bsdtar_getopt(), so -W is not available here.
642 case 'W': /* Obscure GNU convention. */
645 case 'w': /* SUSv2 */
646 bsdtar
->option_interactive
= 1;
648 case 'X': /* GNU tar */
649 if (archive_match_exclude_pattern_from_file(
650 bsdtar
->matching
, bsdtar
->argument
, 0)
652 lafe_errc(1, 0, "Error : %s",
653 archive_error_string(bsdtar
->matching
));
655 case 'x': /* SUSv2 */
656 set_mode(bsdtar
, opt
);
658 case 'y': /* FreeBSD version of GNU tar */
659 if (compression
!= '\0')
661 "Can't specify both -%c and -%c", opt
,
664 compression_name
= "bzip2";
666 case 'Z': /* GNU tar */
667 if (compression
!= '\0')
669 "Can't specify both -%c and -%c", opt
,
672 compression_name
= "compress";
674 case 'z': /* GNU tar, star, many others */
675 if (compression
!= '\0')
677 "Can't specify both -%c and -%c", opt
,
680 compression_name
= "gzip";
682 case OPTION_USE_COMPRESS_PROGRAM
:
683 compress_program
= bsdtar
->argument
;
691 * Sanity-check options.
694 /* If no "real" mode was specified, treat -h as --help. */
695 if ((bsdtar
->mode
== '\0') && possible_help_request
) {
700 /* Otherwise, a mode is required. */
701 if (bsdtar
->mode
== '\0')
703 "Must specify one of -c, -r, -t, -u, -x");
705 /* Check boolean options only permitted in certain modes. */
707 only_mode(bsdtar
, "-a", "c");
708 if (bsdtar
->readdisk_flags
& ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS
)
709 only_mode(bsdtar
, "--one-file-system", "cru");
710 if (bsdtar
->option_fast_read
)
711 only_mode(bsdtar
, "--fast-read", "xt");
712 if (bsdtar
->extract_flags
& ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED
)
713 only_mode(bsdtar
, "--hfsCompression", "x");
714 if (bsdtar
->extract_flags
& ARCHIVE_EXTRACT_NO_HFS_COMPRESSION
)
715 only_mode(bsdtar
, "--nopreserveHFSCompression", "x");
716 if (bsdtar
->readdisk_flags
& ARCHIVE_READDISK_HONOR_NODUMP
)
717 only_mode(bsdtar
, "--nodump", "cru");
718 if (bsdtar
->readdisk_flags
& ARCHIVE_READDISK_NO_XATTR
)
719 only_mode(bsdtar
, "--no-xattr", "crux");
721 switch (bsdtar
->mode
) {
724 * In GNU tar, -o means "old format." The
725 * "ustar" format is the closest thing
726 * supported by libarchive.
728 cset_set_format(bsdtar
->cset
, "ustar");
729 /* TODO: bsdtar->create_format = "v7"; */
732 /* POSIX-compatible behavior. */
733 bsdtar
->option_no_owner
= 1;
734 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
737 only_mode(bsdtar
, "-o", "xc");
741 if (bsdtar
->option_no_subdirs
)
742 only_mode(bsdtar
, "-n", "cru");
743 if (bsdtar
->option_stdout
)
744 only_mode(bsdtar
, "-O", "xt");
745 if (bsdtar
->option_unlink_first
)
746 only_mode(bsdtar
, "-U", "x");
747 if (bsdtar
->option_warn_links
)
748 only_mode(bsdtar
, "--check-links", "cr");
750 if (option_a
&& cset_auto_compress(bsdtar
->cset
, bsdtar
->filename
)) {
751 /* Ignore specified compressions if auto-compress works. */
755 /* Check other parameters only permitted in certain modes. */
756 if (compress_program
!= NULL
) {
757 only_mode(bsdtar
, "--use-compress-program", "cxt");
758 cset_add_filter_program(bsdtar
->cset
, compress_program
);
759 /* Ignore specified compressions. */
763 if (compression
!= '\0') {
764 switch (compression
) {
765 case 'J': case 'j': case 'y': case 'Z': case 'z':
767 buff
[1] = compression
;
771 strcat(buff
, compression_name
);
774 only_mode(bsdtar
, buff
, "cxt");
775 cset_add_filter(bsdtar
->cset
, compression_name
);
777 if (compression2
!= '\0') {
779 strcat(buff
, compression2_name
);
780 only_mode(bsdtar
, buff
, "cxt");
781 cset_add_filter(bsdtar
->cset
, compression2_name
);
783 if (cset_get_format(bsdtar
->cset
) != NULL
)
784 only_mode(bsdtar
, "--format", "cru");
785 if (bsdtar
->symlink_mode
!= '\0') {
787 buff
[1] = bsdtar
->symlink_mode
;
788 only_mode(bsdtar
, buff
, "cru");
791 /* Filename "-" implies stdio. */
792 if (strcmp(bsdtar
->filename
, "-") == 0)
793 bsdtar
->filename
= NULL
;
795 switch(bsdtar
->mode
) {
813 archive_match_free(bsdtar
->matching
);
814 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
815 cleanup_substitution(bsdtar
);
817 cset_free(bsdtar
->cset
);
818 passphrase_free(bsdtar
->ppbuff
);
820 if (bsdtar
->return_value
!= 0)
822 "Error exit delayed from previous errors.");
823 return (bsdtar
->return_value
);
827 set_mode(struct bsdtar
*bsdtar
, char opt
)
829 if (bsdtar
->mode
!= '\0' && bsdtar
->mode
!= opt
)
831 "Can't specify both -%c and -%c", opt
, bsdtar
->mode
);
836 * Verify that the mode is correct.
839 only_mode(struct bsdtar
*bsdtar
, const char *opt
, const char *valid_modes
)
841 if (strchr(valid_modes
, bsdtar
->mode
) == NULL
)
843 "Option %s is not permitted in mode -%c",
853 p
= lafe_getprogname();
855 fprintf(stderr
, "Usage:\n");
856 fprintf(stderr
, " List: %s -tf <archive-filename>\n", p
);
857 fprintf(stderr
, " Extract: %s -xf <archive-filename>\n", p
);
858 fprintf(stderr
, " Create: %s -cf <archive-filename> [filenames...]\n", p
);
859 fprintf(stderr
, " Help: %s --help\n", p
);
866 printf("bsdtar %s - %s\n",
867 BSDTAR_VERSION_STRING
,
868 archive_version_details());
872 static const char *long_help_msg
=
873 "First option must be a mode specifier:\n"
874 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
876 " -b # Use # 512-byte records per I/O block\n"
877 " -f <filename> Location of archive (default " _PATH_DEFTAPE
")\n"
880 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
881 " <file>, <dir> add these items to archive\n"
882 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
883 " --format {ustar|pax|cpio|shar} Select archive format\n"
884 " --exclude <pattern> Skip files that match pattern\n"
885 " -C <dir> Change to <dir> before processing remaining files\n"
886 " @<archive> Add entries from <archive> to output\n"
887 "List: %p -t [options] [<patterns>]\n"
888 " <patterns> If specified, list only entries that match\n"
889 "Extract: %p -x [options] [<patterns>]\n"
890 " <patterns> If specified, extract only entries that match\n"
891 " -k Keep (don't overwrite) existing files\n"
892 " -m Don't restore modification times\n"
893 " -O Write entries to stdout, don't restore to disk\n"
894 " -p Restore permissions (including ACLs, owner, file flags)\n";
898 * Note that the word 'bsdtar' will always appear in the first line
901 * In particular, /bin/sh scripts that need to test for the presence
902 * of bsdtar can use the following template:
904 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
905 * echo bsdtar; else echo not bsdtar; fi
913 prog
= lafe_getprogname();
917 p
= (strcmp(prog
,"bsdtar") != 0) ? "(bsdtar)" : "";
918 printf("%s%s: manipulate archive files\n", prog
, p
);
920 for (p
= long_help_msg
; *p
!= '\0'; p
++) {