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"
85 #define _PATH_DEFTAPE "/dev/tape"
89 int _CRT_glob
= 0; /* Disable broken CRT globbing. */
92 static struct bsdtar
*_bsdtar
;
94 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
95 static volatile int siginfo_occurred
;
98 siginfo_handler(int sig
)
100 (void)sig
; /* UNUSED */
101 siginfo_occurred
= 1;
107 int r
= siginfo_occurred
;
108 siginfo_occurred
= 0;
119 /* External function to parse a date/time string */
120 time_t get_date(time_t, const char *);
122 static void long_help(void);
123 static void only_mode(struct bsdtar
*, const char *opt
,
125 static void set_mode(struct bsdtar
*, char opt
);
126 static void version(void);
128 /* A basic set of security flags to request from libarchive. */
130 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
131 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
134 main(int argc
, char **argv
)
136 struct bsdtar
*bsdtar
, bsdtar_storage
;
139 char possible_help_request
;
144 * Use a pointer for consistency, but stack-allocated storage
145 * for ease of cleanup.
147 _bsdtar
= bsdtar
= &bsdtar_storage
;
148 memset(bsdtar
, 0, sizeof(*bsdtar
));
149 bsdtar
->fd
= -1; /* Mark as "unused" */
152 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
153 { /* Catch SIGINFO and SIGUSR1, if they exist. */
155 sa
.sa_handler
= siginfo_handler
;
156 sigemptyset(&sa
.sa_mask
);
159 if (sigaction(SIGINFO
, &sa
, NULL
))
160 lafe_errc(1, errno
, "sigaction(SIGINFO) failed");
163 /* ... and treat SIGUSR1 the same way as SIGINFO. */
164 if (sigaction(SIGUSR1
, &sa
, NULL
))
165 lafe_errc(1, errno
, "sigaction(SIGUSR1) failed");
171 /* Need lafe_progname before calling lafe_warnc. */
173 lafe_progname
= "bsdtar";
175 #if defined(_WIN32) && !defined(__CYGWIN__)
176 lafe_progname
= strrchr(*argv
, '\\');
178 lafe_progname
= strrchr(*argv
, '/');
180 if (lafe_progname
!= NULL
)
183 lafe_progname
= *argv
;
189 if (setlocale(LC_ALL
, "") == NULL
)
190 lafe_warnc(0, "Failed to set default locale");
192 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
193 bsdtar
->day_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
195 possible_help_request
= 0;
197 /* Look up uid of current user for future reference */
198 bsdtar
->user_uid
= geteuid();
200 /* Default: open tape drive. */
201 bsdtar
->filename
= getenv("TAPE");
202 if (bsdtar
->filename
== NULL
)
203 bsdtar
->filename
= _PATH_DEFTAPE
;
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
;
229 * Comments following each option indicate where that option
230 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
231 * no such comment, then I don't know of anyone else who
232 * implements that option.
234 while ((opt
= bsdtar_getopt(bsdtar
)) != -1) {
236 case 'B': /* GNU tar */
237 /* libarchive doesn't need this; just ignore it. */
239 case 'b': /* SUSv2 */
240 t
= atoi(bsdtar
->optarg
);
241 if (t
<= 0 || t
> 8192)
243 "Argument to -b is out of range (1..8192)");
244 bsdtar
->bytes_per_block
= 512 * t
;
246 case 'C': /* GNU tar */
247 set_chdir(bsdtar
, bsdtar
->optarg
);
249 case 'c': /* SUSv2 */
250 set_mode(bsdtar
, opt
);
252 case OPTION_CHECK_LINKS
: /* GNU tar */
253 bsdtar
->option_warn_links
= 1;
255 case OPTION_CHROOT
: /* NetBSD */
256 bsdtar
->option_chroot
= 1;
258 case OPTION_EXCLUDE
: /* GNU tar */
259 if (lafe_exclude(&bsdtar
->matching
, bsdtar
->optarg
))
261 "Couldn't exclude %s\n", bsdtar
->optarg
);
263 case OPTION_FORMAT
: /* GNU tar, others */
264 bsdtar
->create_format
= bsdtar
->optarg
;
267 bsdtar
->option_options
= bsdtar
->optarg
;
269 case 'f': /* SUSv2 */
270 bsdtar
->filename
= bsdtar
->optarg
;
271 if (strcmp(bsdtar
->filename
, "-") == 0)
272 bsdtar
->filename
= NULL
;
274 case 'H': /* BSD convention */
275 bsdtar
->symlink_mode
= 'H';
277 case 'h': /* Linux Standards Base, gtar; synonym for -L */
278 bsdtar
->symlink_mode
= 'L';
279 /* Hack: -h by itself is the "help" command. */
280 possible_help_request
= 1;
282 case OPTION_HELP
: /* GNU tar, others */
286 case 'I': /* GNU tar */
288 * TODO: Allow 'names' to come from an archive,
289 * not just a text file. Design a good UI for
290 * allowing names and mode/owner to be read
291 * from an archive, with contents coming from
292 * disk. This can be used to "refresh" an
293 * archive or to design archives with special
294 * permissions without having to create those
295 * permissions on disk.
297 bsdtar
->names_from_file
= bsdtar
->optarg
;
301 * Noone else has the @archive extension, so
302 * noone else needs this to filter entries
303 * when transforming archives.
305 if (lafe_include(&bsdtar
->matching
, bsdtar
->optarg
))
307 "Failed to add %s to inclusion list",
310 case 'j': /* GNU tar */
311 if (bsdtar
->create_compression
!= '\0')
313 "Can't specify both -%c and -%c", opt
,
314 bsdtar
->create_compression
);
315 bsdtar
->create_compression
= opt
;
317 case 'J': /* GNU tar 1.21 and later */
318 if (bsdtar
->create_compression
!= '\0')
320 "Can't specify both -%c and -%c", opt
,
321 bsdtar
->create_compression
);
322 bsdtar
->create_compression
= opt
;
324 case 'k': /* GNU tar */
325 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_NO_OVERWRITE
;
327 case OPTION_KEEP_NEWER_FILES
: /* GNU tar */
328 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER
;
330 case 'L': /* BSD convention */
331 bsdtar
->symlink_mode
= 'L';
333 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
334 /* GNU tar 1.13 used -l for --one-file-system */
335 bsdtar
->option_warn_links
= 1;
338 if (bsdtar
->create_compression
!= '\0')
340 "Can't specify both -%c and -%c", opt
,
341 bsdtar
->create_compression
);
342 bsdtar
->create_compression
= opt
;
344 case 'm': /* SUSv2 */
345 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_TIME
;
347 case 'n': /* GNU tar */
348 bsdtar
->option_no_subdirs
= 1;
351 * Selecting files by time:
352 * --newer-?time='date' Only files newer than 'date'
353 * --newer-?time-than='file' Only files newer than time
354 * on specified file (useful for incremental backups)
355 * TODO: Add corresponding "older" options to reverse these.
357 case OPTION_NEWER_CTIME
: /* GNU tar */
358 bsdtar
->newer_ctime_sec
= get_date(now
, bsdtar
->optarg
);
360 case OPTION_NEWER_CTIME_THAN
:
363 if (stat(bsdtar
->optarg
, &st
) != 0)
365 "Can't open file %s", bsdtar
->optarg
);
366 bsdtar
->newer_ctime_sec
= st
.st_ctime
;
367 bsdtar
->newer_ctime_nsec
=
368 ARCHIVE_STAT_CTIME_NANOS(&st
);
371 case OPTION_NEWER_MTIME
: /* GNU tar */
372 bsdtar
->newer_mtime_sec
= get_date(now
, bsdtar
->optarg
);
374 case OPTION_NEWER_MTIME_THAN
:
377 if (stat(bsdtar
->optarg
, &st
) != 0)
379 "Can't open file %s", bsdtar
->optarg
);
380 bsdtar
->newer_mtime_sec
= st
.st_mtime
;
381 bsdtar
->newer_mtime_nsec
=
382 ARCHIVE_STAT_MTIME_NANOS(&st
);
385 case OPTION_NODUMP
: /* star */
386 bsdtar
->option_honor_nodump
= 1;
388 case OPTION_NO_SAME_OWNER
: /* GNU tar */
389 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
391 case OPTION_NO_SAME_PERMISSIONS
: /* GNU tar */
392 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_PERM
;
393 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_ACL
;
394 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_XATTR
;
395 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_FFLAGS
;
397 case OPTION_NULL
: /* GNU tar */
398 bsdtar
->option_null
++;
400 case OPTION_NUMERIC_OWNER
: /* GNU tar */
401 bsdtar
->option_numeric_owner
++;
403 case 'O': /* GNU tar */
404 bsdtar
->option_stdout
= 1;
406 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
407 option_o
= 1; /* Record it and resolve it later. */
409 case OPTION_ONE_FILE_SYSTEM
: /* GNU tar */
410 bsdtar
->option_dont_traverse_mounts
= 1;
414 * The common BSD -P option is not necessary, since
415 * our default is to archive symlinks, not follow
416 * them. This is convenient, as -P conflicts with GNU
419 case 'P': /* BSD convention */
420 /* Default behavior, no option necessary. */
423 case 'P': /* GNU tar */
424 bsdtar
->extract_flags
&= ~SECURITY
;
425 bsdtar
->option_absolute_paths
= 1;
427 case 'p': /* GNU tar, star */
428 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_PERM
;
429 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_ACL
;
430 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_XATTR
;
431 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_FFLAGS
;
433 case OPTION_POSIX
: /* GNU tar */
434 bsdtar
->create_format
= "pax";
436 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
437 bsdtar
->option_fast_read
= 1;
439 case 'r': /* SUSv2 */
440 set_mode(bsdtar
, opt
);
442 case 'S': /* NetBSD pax-as-tar */
443 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_SPARSE
;
445 case 's': /* NetBSD pax-as-tar */
447 add_substitution(bsdtar
, bsdtar
->optarg
);
450 "-s is not supported by this version of bsdtar");
454 case OPTION_SAME_OWNER
: /* GNU tar */
455 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_OWNER
;
457 case OPTION_STRIP_COMPONENTS
: /* GNU tar 1.15 */
458 bsdtar
->strip_components
= atoi(bsdtar
->optarg
);
460 case 'T': /* GNU tar */
461 bsdtar
->names_from_file
= bsdtar
->optarg
;
463 case 't': /* SUSv2 */
464 set_mode(bsdtar
, opt
);
467 case OPTION_TOTALS
: /* GNU tar */
468 bsdtar
->option_totals
++;
470 case 'U': /* GNU tar */
471 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_UNLINK
;
472 bsdtar
->option_unlink_first
= 1;
474 case 'u': /* SUSv2 */
475 set_mode(bsdtar
, opt
);
477 case 'v': /* SUSv2 */
480 case OPTION_VERSION
: /* GNU convention */
485 * The -W longopt feature is handled inside of
486 * bsdtar_getopt(), so -W is not available here.
488 case 'W': /* Obscure GNU convention. */
491 case 'w': /* SUSv2 */
492 bsdtar
->option_interactive
= 1;
494 case 'X': /* GNU tar */
495 if (lafe_exclude_from_file(&bsdtar
->matching
, bsdtar
->optarg
))
497 "failed to process exclusions from file %s",
500 case 'x': /* SUSv2 */
501 set_mode(bsdtar
, opt
);
503 case 'y': /* FreeBSD version of GNU tar */
504 if (bsdtar
->create_compression
!= '\0')
506 "Can't specify both -%c and -%c", opt
,
507 bsdtar
->create_compression
);
508 bsdtar
->create_compression
= opt
;
510 case 'Z': /* GNU tar */
511 if (bsdtar
->create_compression
!= '\0')
513 "Can't specify both -%c and -%c", opt
,
514 bsdtar
->create_compression
);
515 bsdtar
->create_compression
= opt
;
517 case 'z': /* GNU tar, star, many others */
518 if (bsdtar
->create_compression
!= '\0')
520 "Can't specify both -%c and -%c", opt
,
521 bsdtar
->create_compression
);
522 bsdtar
->create_compression
= opt
;
524 case OPTION_USE_COMPRESS_PROGRAM
:
525 bsdtar
->compress_program
= bsdtar
->optarg
;
533 * Sanity-check options.
536 /* If no "real" mode was specified, treat -h as --help. */
537 if ((bsdtar
->mode
== '\0') && possible_help_request
) {
542 /* Otherwise, a mode is required. */
543 if (bsdtar
->mode
== '\0')
545 "Must specify one of -c, -r, -t, -u, -x");
547 /* Check boolean options only permitted in certain modes. */
548 if (bsdtar
->option_dont_traverse_mounts
)
549 only_mode(bsdtar
, "--one-file-system", "cru");
550 if (bsdtar
->option_fast_read
)
551 only_mode(bsdtar
, "--fast-read", "xt");
552 if (bsdtar
->option_honor_nodump
)
553 only_mode(bsdtar
, "--nodump", "cru");
555 switch (bsdtar
->mode
) {
558 * In GNU tar, -o means "old format." The
559 * "ustar" format is the closest thing
560 * supported by libarchive.
562 bsdtar
->create_format
= "ustar";
563 /* TODO: bsdtar->create_format = "v7"; */
566 /* POSIX-compatible behavior. */
567 bsdtar
->option_no_owner
= 1;
568 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
571 only_mode(bsdtar
, "-o", "xc");
575 if (bsdtar
->option_no_subdirs
)
576 only_mode(bsdtar
, "-n", "cru");
577 if (bsdtar
->option_stdout
)
578 only_mode(bsdtar
, "-O", "xt");
579 if (bsdtar
->option_unlink_first
)
580 only_mode(bsdtar
, "-U", "x");
581 if (bsdtar
->option_warn_links
)
582 only_mode(bsdtar
, "--check-links", "cr");
584 /* Check other parameters only permitted in certain modes. */
585 if (bsdtar
->create_compression
!= '\0') {
587 buff
[1] = bsdtar
->create_compression
;
588 only_mode(bsdtar
, buff
, "cxt");
590 if (bsdtar
->create_format
!= NULL
)
591 only_mode(bsdtar
, "--format", "cru");
592 if (bsdtar
->symlink_mode
!= '\0') {
594 buff
[1] = bsdtar
->symlink_mode
;
595 only_mode(bsdtar
, buff
, "cru");
597 if (bsdtar
->strip_components
!= 0)
598 only_mode(bsdtar
, "--strip-components", "xt");
600 switch(bsdtar
->mode
) {
618 lafe_cleanup_exclusions(&bsdtar
->matching
);
620 cleanup_substitution(bsdtar
);
623 if (bsdtar
->return_value
!= 0)
625 "Error exit delayed from previous errors.");
626 return (bsdtar
->return_value
);
630 set_mode(struct bsdtar
*bsdtar
, char opt
)
632 if (bsdtar
->mode
!= '\0' && bsdtar
->mode
!= opt
)
634 "Can't specify both -%c and -%c", opt
, bsdtar
->mode
);
639 * Verify that the mode is correct.
642 only_mode(struct bsdtar
*bsdtar
, const char *opt
, const char *valid_modes
)
644 if (strchr(valid_modes
, bsdtar
->mode
) == NULL
)
646 "Option %s is not permitted in mode -%c",
658 fprintf(stderr
, "Usage:\n");
659 fprintf(stderr
, " List: %s -tf <archive-filename>\n", p
);
660 fprintf(stderr
, " Extract: %s -xf <archive-filename>\n", p
);
661 fprintf(stderr
, " Create: %s -cf <archive-filename> [filenames...]\n", p
);
662 fprintf(stderr
, " Help: %s --help\n", p
);
669 printf("bsdtar %s - %s\n",
670 BSDTAR_VERSION_STRING
,
675 static const char *long_help_msg
=
676 "First option must be a mode specifier:\n"
677 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
679 " -b # Use # 512-byte records per I/O block\n"
680 " -f <filename> Location of archive (default " _PATH_DEFTAPE
")\n"
683 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
684 " <file>, <dir> add these items to archive\n"
685 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
686 " --format {ustar|pax|cpio|shar} Select archive format\n"
687 " --exclude <pattern> Skip files that match pattern\n"
688 " -C <dir> Change to <dir> before processing remaining files\n"
689 " @<archive> Add entries from <archive> to output\n"
690 "List: %p -t [options] [<patterns>]\n"
691 " <patterns> If specified, list only entries that match\n"
692 "Extract: %p -x [options] [<patterns>]\n"
693 " <patterns> If specified, extract only entries that match\n"
694 " -k Keep (don't overwrite) existing files\n"
695 " -m Don't restore modification times\n"
696 " -O Write entries to stdout, don't restore to disk\n"
697 " -p Restore permissions (including ACLs, owner, file flags)\n";
701 * Note that the word 'bsdtar' will always appear in the first line
704 * In particular, /bin/sh scripts that need to test for the presence
705 * of bsdtar can use the following template:
707 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
708 * echo bsdtar; else echo not bsdtar; fi
716 prog
= lafe_progname
;
720 p
= (strcmp(prog
,"bsdtar") != 0) ? "(bsdtar)" : "";
721 printf("%s%s: manipulate archive files\n", prog
, p
);
723 for (p
= long_help_msg
; *p
!= '\0'; p
++) {