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
41 #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"
89 #define _PATH_DEFTAPE "/dev/tape"
93 int _CRT_glob
= 0; /* Disable broken CRT globbing. */
96 static struct bsdtar
*_bsdtar
;
98 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
99 static volatile int siginfo_occurred
;
102 siginfo_handler(int sig
)
104 (void)sig
; /* UNUSED */
105 siginfo_occurred
= 1;
111 int r
= siginfo_occurred
;
112 siginfo_occurred
= 0;
123 /* External function to parse a date/time string */
124 time_t get_date(time_t, const char *);
126 static void long_help(void);
127 static void only_mode(struct bsdtar
*, const char *opt
,
129 static void set_mode(struct bsdtar
*, char opt
);
130 static void version(void);
132 /* A basic set of security flags to request from libarchive. */
134 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
135 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
138 main(int argc
, char **argv
)
140 struct bsdtar
*bsdtar
, bsdtar_storage
;
143 char possible_help_request
;
148 * Use a pointer for consistency, but stack-allocated storage
149 * for ease of cleanup.
151 _bsdtar
= bsdtar
= &bsdtar_storage
;
152 memset(bsdtar
, 0, sizeof(*bsdtar
));
153 bsdtar
->fd
= -1; /* Mark as "unused" */
156 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
157 { /* Catch SIGINFO and SIGUSR1, if they exist. */
159 sa
.sa_handler
= siginfo_handler
;
160 sigemptyset(&sa
.sa_mask
);
163 if (sigaction(SIGINFO
, &sa
, NULL
))
164 lafe_errc(1, errno
, "sigaction(SIGINFO) failed");
167 /* ... and treat SIGUSR1 the same way as SIGINFO. */
168 if (sigaction(SIGUSR1
, &sa
, NULL
))
169 lafe_errc(1, errno
, "sigaction(SIGUSR1) failed");
175 /* Need lafe_progname before calling lafe_warnc. */
177 lafe_progname
= "bsdtar";
179 #if defined(_WIN32) && !defined(__CYGWIN__)
180 lafe_progname
= strrchr(*argv
, '\\');
182 lafe_progname
= strrchr(*argv
, '/');
184 if (lafe_progname
!= NULL
)
187 lafe_progname
= *argv
;
193 if (setlocale(LC_ALL
, "") == NULL
)
194 lafe_warnc(0, "Failed to set default locale");
196 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
197 bsdtar
->day_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
199 possible_help_request
= 0;
201 /* Look up uid of current user for future reference */
202 bsdtar
->user_uid
= geteuid();
204 /* Default: open tape drive. */
205 bsdtar
->filename
= getenv("TAPE");
206 if (bsdtar
->filename
== NULL
)
207 bsdtar
->filename
= _PATH_DEFTAPE
;
209 /* Default: preserve mod time on extract */
210 bsdtar
->extract_flags
= ARCHIVE_EXTRACT_TIME
;
212 /* Default: Perform basic security checks. */
213 bsdtar
->extract_flags
|= SECURITY
;
216 /* On POSIX systems, assume --same-owner and -p when run by
217 * the root user. This doesn't make any sense on Windows. */
218 if (bsdtar
->user_uid
== 0) {
220 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_OWNER
;
222 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_PERM
;
223 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_ACL
;
224 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_XATTR
;
225 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_FFLAGS
;
233 * Comments following each option indicate where that option
234 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
235 * no such comment, then I don't know of anyone else who
236 * implements that option.
238 while ((opt
= bsdtar_getopt(bsdtar
)) != -1) {
240 case 'B': /* GNU tar */
241 /* libarchive doesn't need this; just ignore it. */
243 case 'b': /* SUSv2 */
244 t
= atoi(bsdtar
->optarg
);
245 if (t
<= 0 || t
> 8192)
247 "Argument to -b is out of range (1..8192)");
248 bsdtar
->bytes_per_block
= 512 * t
;
250 case 'C': /* GNU tar */
251 set_chdir(bsdtar
, bsdtar
->optarg
);
253 case 'c': /* SUSv2 */
254 set_mode(bsdtar
, opt
);
256 case OPTION_CHECK_LINKS
: /* GNU tar */
257 bsdtar
->option_warn_links
= 1;
259 case OPTION_CHROOT
: /* NetBSD */
260 bsdtar
->option_chroot
= 1;
262 case OPTION_EXCLUDE
: /* GNU tar */
263 if (lafe_exclude(&bsdtar
->matching
, bsdtar
->optarg
))
265 "Couldn't exclude %s\n", bsdtar
->optarg
);
267 case OPTION_FORMAT
: /* GNU tar, others */
268 bsdtar
->create_format
= bsdtar
->optarg
;
271 bsdtar
->option_options
= bsdtar
->optarg
;
273 case 'f': /* SUSv2 */
274 bsdtar
->filename
= bsdtar
->optarg
;
275 if (strcmp(bsdtar
->filename
, "-") == 0)
276 bsdtar
->filename
= NULL
;
278 case 'H': /* BSD convention */
279 bsdtar
->symlink_mode
= 'H';
281 case 'h': /* Linux Standards Base, gtar; synonym for -L */
282 bsdtar
->symlink_mode
= 'L';
283 /* Hack: -h by itself is the "help" command. */
284 possible_help_request
= 1;
286 case OPTION_HELP
: /* GNU tar, others */
290 case 'I': /* GNU tar */
292 * TODO: Allow 'names' to come from an archive,
293 * not just a text file. Design a good UI for
294 * allowing names and mode/owner to be read
295 * from an archive, with contents coming from
296 * disk. This can be used to "refresh" an
297 * archive or to design archives with special
298 * permissions without having to create those
299 * permissions on disk.
301 bsdtar
->names_from_file
= bsdtar
->optarg
;
305 * Noone else has the @archive extension, so
306 * noone else needs this to filter entries
307 * when transforming archives.
309 if (lafe_include(&bsdtar
->matching
, bsdtar
->optarg
))
311 "Failed to add %s to inclusion list",
314 case 'j': /* GNU tar */
315 if (bsdtar
->create_compression
!= '\0')
317 "Can't specify both -%c and -%c", opt
,
318 bsdtar
->create_compression
);
319 bsdtar
->create_compression
= opt
;
321 case 'J': /* GNU tar 1.21 and later */
322 if (bsdtar
->create_compression
!= '\0')
324 "Can't specify both -%c and -%c", opt
,
325 bsdtar
->create_compression
);
326 bsdtar
->create_compression
= opt
;
328 case 'k': /* GNU tar */
329 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_NO_OVERWRITE
;
331 case OPTION_KEEP_NEWER_FILES
: /* GNU tar */
332 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER
;
334 case 'L': /* BSD convention */
335 bsdtar
->symlink_mode
= 'L';
337 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
338 /* GNU tar 1.13 used -l for --one-file-system */
339 bsdtar
->option_warn_links
= 1;
342 if (bsdtar
->create_compression
!= '\0')
344 "Can't specify both -%c and -%c", opt
,
345 bsdtar
->create_compression
);
346 bsdtar
->create_compression
= opt
;
348 case 'm': /* SUSv2 */
349 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_TIME
;
351 case 'n': /* GNU tar */
352 bsdtar
->option_no_subdirs
= 1;
355 * Selecting files by time:
356 * --newer-?time='date' Only files newer than 'date'
357 * --newer-?time-than='file' Only files newer than time
358 * on specified file (useful for incremental backups)
359 * TODO: Add corresponding "older" options to reverse these.
361 case OPTION_NEWER_CTIME
: /* GNU tar */
362 bsdtar
->newer_ctime_sec
= get_date(now
, bsdtar
->optarg
);
364 case OPTION_NEWER_CTIME_THAN
:
367 if (stat(bsdtar
->optarg
, &st
) != 0)
369 "Can't open file %s", bsdtar
->optarg
);
370 bsdtar
->newer_ctime_sec
= st
.st_ctime
;
371 bsdtar
->newer_ctime_nsec
=
372 ARCHIVE_STAT_CTIME_NANOS(&st
);
375 case OPTION_NEWER_MTIME
: /* GNU tar */
376 bsdtar
->newer_mtime_sec
= get_date(now
, bsdtar
->optarg
);
378 case OPTION_NEWER_MTIME_THAN
:
381 if (stat(bsdtar
->optarg
, &st
) != 0)
383 "Can't open file %s", bsdtar
->optarg
);
384 bsdtar
->newer_mtime_sec
= st
.st_mtime
;
385 bsdtar
->newer_mtime_nsec
=
386 ARCHIVE_STAT_MTIME_NANOS(&st
);
389 case OPTION_NODUMP
: /* star */
390 bsdtar
->option_honor_nodump
= 1;
392 case OPTION_NO_SAME_OWNER
: /* GNU tar */
393 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
395 case OPTION_NO_SAME_PERMISSIONS
: /* GNU tar */
396 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_PERM
;
397 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_ACL
;
398 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_XATTR
;
399 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_FFLAGS
;
401 case OPTION_NULL
: /* GNU tar */
402 bsdtar
->option_null
++;
404 case OPTION_NUMERIC_OWNER
: /* GNU tar */
405 bsdtar
->option_numeric_owner
++;
407 case 'O': /* GNU tar */
408 bsdtar
->option_stdout
= 1;
410 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
411 option_o
= 1; /* Record it and resolve it later. */
413 case OPTION_ONE_FILE_SYSTEM
: /* GNU tar */
414 bsdtar
->option_dont_traverse_mounts
= 1;
418 * The common BSD -P option is not necessary, since
419 * our default is to archive symlinks, not follow
420 * them. This is convenient, as -P conflicts with GNU
423 case 'P': /* BSD convention */
424 /* Default behavior, no option necessary. */
427 case 'P': /* GNU tar */
428 bsdtar
->extract_flags
&= ~SECURITY
;
429 bsdtar
->option_absolute_paths
= 1;
431 case 'p': /* GNU tar, star */
432 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_PERM
;
433 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_ACL
;
434 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_XATTR
;
435 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_FFLAGS
;
437 case OPTION_POSIX
: /* GNU tar */
438 bsdtar
->create_format
= "pax";
440 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
441 bsdtar
->option_fast_read
= 1;
443 case 'r': /* SUSv2 */
444 set_mode(bsdtar
, opt
);
446 case 'S': /* NetBSD pax-as-tar */
447 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_SPARSE
;
449 case 's': /* NetBSD pax-as-tar */
451 add_substitution(bsdtar
, bsdtar
->optarg
);
454 "-s is not supported by this version of bsdtar");
458 case OPTION_SAME_OWNER
: /* GNU tar */
459 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_OWNER
;
461 case OPTION_STRIP_COMPONENTS
: /* GNU tar 1.15 */
462 bsdtar
->strip_components
= atoi(bsdtar
->optarg
);
464 case 'T': /* GNU tar */
465 bsdtar
->names_from_file
= bsdtar
->optarg
;
467 case 't': /* SUSv2 */
468 set_mode(bsdtar
, opt
);
471 case OPTION_TOTALS
: /* GNU tar */
472 bsdtar
->option_totals
++;
474 case 'U': /* GNU tar */
475 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_UNLINK
;
476 bsdtar
->option_unlink_first
= 1;
478 case 'u': /* SUSv2 */
479 set_mode(bsdtar
, opt
);
481 case 'v': /* SUSv2 */
484 case OPTION_VERSION
: /* GNU convention */
489 * The -W longopt feature is handled inside of
490 * bsdtar_getopt(), so -W is not available here.
492 case 'W': /* Obscure GNU convention. */
495 case 'w': /* SUSv2 */
496 bsdtar
->option_interactive
= 1;
498 case 'X': /* GNU tar */
499 if (lafe_exclude_from_file(&bsdtar
->matching
, bsdtar
->optarg
))
501 "failed to process exclusions from file %s",
504 case 'x': /* SUSv2 */
505 set_mode(bsdtar
, opt
);
507 case 'y': /* FreeBSD version of GNU tar */
508 if (bsdtar
->create_compression
!= '\0')
510 "Can't specify both -%c and -%c", opt
,
511 bsdtar
->create_compression
);
512 bsdtar
->create_compression
= opt
;
514 case 'Z': /* GNU tar */
515 if (bsdtar
->create_compression
!= '\0')
517 "Can't specify both -%c and -%c", opt
,
518 bsdtar
->create_compression
);
519 bsdtar
->create_compression
= opt
;
521 case 'z': /* GNU tar, star, many others */
522 if (bsdtar
->create_compression
!= '\0')
524 "Can't specify both -%c and -%c", opt
,
525 bsdtar
->create_compression
);
526 bsdtar
->create_compression
= opt
;
528 case OPTION_USE_COMPRESS_PROGRAM
:
529 bsdtar
->compress_program
= bsdtar
->optarg
;
537 * Sanity-check options.
540 /* If no "real" mode was specified, treat -h as --help. */
541 if ((bsdtar
->mode
== '\0') && possible_help_request
) {
546 /* Otherwise, a mode is required. */
547 if (bsdtar
->mode
== '\0')
549 "Must specify one of -c, -r, -t, -u, -x");
551 /* Check boolean options only permitted in certain modes. */
552 if (bsdtar
->option_dont_traverse_mounts
)
553 only_mode(bsdtar
, "--one-file-system", "cru");
554 if (bsdtar
->option_fast_read
)
555 only_mode(bsdtar
, "--fast-read", "xt");
556 if (bsdtar
->option_honor_nodump
)
557 only_mode(bsdtar
, "--nodump", "cru");
559 switch (bsdtar
->mode
) {
562 * In GNU tar, -o means "old format." The
563 * "ustar" format is the closest thing
564 * supported by libarchive.
566 bsdtar
->create_format
= "ustar";
567 /* TODO: bsdtar->create_format = "v7"; */
570 /* POSIX-compatible behavior. */
571 bsdtar
->option_no_owner
= 1;
572 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
575 only_mode(bsdtar
, "-o", "xc");
579 if (bsdtar
->option_no_subdirs
)
580 only_mode(bsdtar
, "-n", "cru");
581 if (bsdtar
->option_stdout
)
582 only_mode(bsdtar
, "-O", "xt");
583 if (bsdtar
->option_unlink_first
)
584 only_mode(bsdtar
, "-U", "x");
585 if (bsdtar
->option_warn_links
)
586 only_mode(bsdtar
, "--check-links", "cr");
588 /* Check other parameters only permitted in certain modes. */
589 if (bsdtar
->create_compression
!= '\0') {
591 buff
[1] = bsdtar
->create_compression
;
592 only_mode(bsdtar
, buff
, "cxt");
594 if (bsdtar
->create_format
!= NULL
)
595 only_mode(bsdtar
, "--format", "cru");
596 if (bsdtar
->symlink_mode
!= '\0') {
598 buff
[1] = bsdtar
->symlink_mode
;
599 only_mode(bsdtar
, buff
, "cru");
601 if (bsdtar
->strip_components
!= 0)
602 only_mode(bsdtar
, "--strip-components", "xt");
604 switch(bsdtar
->mode
) {
622 lafe_cleanup_exclusions(&bsdtar
->matching
);
624 cleanup_substitution(bsdtar
);
627 if (bsdtar
->return_value
!= 0)
629 "Error exit delayed from previous errors.");
630 return (bsdtar
->return_value
);
634 set_mode(struct bsdtar
*bsdtar
, char opt
)
636 if (bsdtar
->mode
!= '\0' && bsdtar
->mode
!= opt
)
638 "Can't specify both -%c and -%c", opt
, bsdtar
->mode
);
643 * Verify that the mode is correct.
646 only_mode(struct bsdtar
*bsdtar
, const char *opt
, const char *valid_modes
)
648 if (strchr(valid_modes
, bsdtar
->mode
) == NULL
)
650 "Option %s is not permitted in mode -%c",
662 fprintf(stderr
, "Usage:\n");
663 fprintf(stderr
, " List: %s -tf <archive-filename>\n", p
);
664 fprintf(stderr
, " Extract: %s -xf <archive-filename>\n", p
);
665 fprintf(stderr
, " Create: %s -cf <archive-filename> [filenames...]\n", p
);
666 fprintf(stderr
, " Help: %s --help\n", p
);
673 printf("bsdtar %s - %s\n",
674 BSDTAR_VERSION_STRING
,
679 static const char *long_help_msg
=
680 "First option must be a mode specifier:\n"
681 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
683 " -b # Use # 512-byte records per I/O block\n"
684 " -f <filename> Location of archive (default " _PATH_DEFTAPE
")\n"
687 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
688 " <file>, <dir> add these items to archive\n"
689 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
690 " --format {ustar|pax|cpio|shar} Select archive format\n"
691 " --exclude <pattern> Skip files that match pattern\n"
692 " -C <dir> Change to <dir> before processing remaining files\n"
693 " @<archive> Add entries from <archive> to output\n"
694 "List: %p -t [options] [<patterns>]\n"
695 " <patterns> If specified, list only entries that match\n"
696 "Extract: %p -x [options] [<patterns>]\n"
697 " <patterns> If specified, extract only entries that match\n"
698 " -k Keep (don't overwrite) existing files\n"
699 " -m Don't restore modification times\n"
700 " -O Write entries to stdout, don't restore to disk\n"
701 " -p Restore permissions (including ACLs, owner, file flags)\n";
705 * Note that the word 'bsdtar' will always appear in the first line
708 * In particular, /bin/sh scripts that need to test for the presence
709 * of bsdtar can use the following template:
711 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
712 * echo bsdtar; else echo not bsdtar; fi
720 prog
= lafe_progname
;
724 p
= (strcmp(prog
,"bsdtar") != 0) ? "(bsdtar)" : "";
725 printf("%s%s: manipulate archive files\n", prog
, p
);
727 for (p
= long_help_msg
; *p
!= '\0'; p
++) {