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
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" */
154 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
155 { /* Catch SIGINFO and SIGUSR1, if they exist. */
157 sa.sa_handler = siginfo_handler;
158 sigemptyset(&sa.sa_mask);
161 if (sigaction(SIGINFO, &sa, NULL))
162 lafe_errc(1, errno, "sigaction(SIGINFO) failed");
165 /* ... and treat SIGUSR1 the same way as SIGINFO. */
166 if (sigaction(SIGUSR1, &sa, NULL))
167 lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
173 /* Need lafe_progname before calling lafe_warnc. */
175 lafe_progname = "bsdtar";
177 #if defined(_WIN32) && !defined(__CYGWIN__)
178 lafe_progname = strrchr(*argv, '\\');
180 lafe_progname = strrchr(*argv, '/');
182 if (lafe_progname != NULL)
185 lafe_progname = *argv;
191 if (setlocale(LC_ALL, "") == NULL)
192 lafe_warnc(0, "Failed to set default locale");
194 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
195 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
197 possible_help_request = 0;
199 /* Look up uid of current user for future reference */
200 bsdtar->user_uid = geteuid();
202 /* Default: open tape drive. */
203 bsdtar->filename = getenv("TAPE");
204 if (bsdtar->filename == NULL)
205 bsdtar->filename = _PATH_DEFTAPE;
207 /* Default: preserve mod time on extract */
208 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
210 /* Default: Perform basic security checks. */
211 bsdtar->extract_flags |= SECURITY;
214 /* On POSIX systems, assume --same-owner and -p when run by
215 * the root user. This doesn't make any sense on Windows. */
216 if (bsdtar->user_uid == 0) {
218 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
220 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
221 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
222 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
223 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
231 * Comments following each option indicate where that option
232 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
233 * no such comment, then I don't know of anyone else who
234 * implements that option.
236 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
238 case 'B': /* GNU tar */
239 /* libarchive doesn't need this; just ignore it. */
241 case 'b': /* SUSv2 */
242 t = atoi(bsdtar->optarg);
243 if (t <= 0 || t > 8192)
245 "Argument to -b is out of range (1..8192)");
246 bsdtar->bytes_per_block = 512 * t;
248 case 'C': /* GNU tar */
249 set_chdir(bsdtar, bsdtar->optarg);
251 case 'c': /* SUSv2 */
252 set_mode(bsdtar, opt);
254 case OPTION_CHECK_LINKS: /* GNU tar */
255 bsdtar->option_warn_links = 1;
257 case OPTION_CHROOT: /* NetBSD */
258 bsdtar->option_chroot = 1;
260 case OPTION_EXCLUDE: /* GNU tar */
261 if (lafe_exclude(&bsdtar->matching, bsdtar->optarg))
263 "Couldn't exclude %s\n", bsdtar->optarg);
265 case OPTION_FORMAT: /* GNU tar, others */
266 bsdtar->create_format = bsdtar->optarg;
268 case 'f': /* SUSv2 */
269 bsdtar->filename = bsdtar->optarg;
270 if (strcmp(bsdtar->filename, "-") == 0)
271 bsdtar->filename = NULL;
273 case OPTION_GID: /* cpio */
274 t = atoi(bsdtar->optarg);
277 "Argument to --gid must be positive");
280 case OPTION_GNAME: /* cpio */
281 bsdtar->gname = bsdtar->optarg;
283 case 'H': /* BSD convention */
284 bsdtar->symlink_mode = 'H';
286 case 'h': /* Linux Standards Base, gtar; synonym for -L */
287 bsdtar->symlink_mode = 'L';
288 /* Hack: -h by itself is the "help" command. */
289 possible_help_request = 1;
291 case OPTION_HELP: /* GNU tar, others */
295 case 'I': /* GNU tar */
297 * TODO: Allow 'names' to come from an archive,
298 * not just a text file. Design a good UI for
299 * allowing names and mode/owner to be read
300 * from an archive, with contents coming from
301 * disk. This can be used to "refresh" an
302 * archive or to design archives with special
303 * permissions without having to create those
304 * permissions on disk.
306 bsdtar->names_from_file = bsdtar->optarg;
310 * Noone else has the @archive extension, so
311 * noone else needs this to filter entries
312 * when transforming archives.
314 if (lafe_include(&bsdtar->matching, bsdtar->optarg))
316 "Failed to add %s to inclusion list",
319 case 'j': /* GNU tar */
320 if (bsdtar->create_compression != '\0')
322 "Can't specify both -%c and -%c", opt,
323 bsdtar->create_compression);
324 bsdtar->create_compression = opt;
326 case 'J': /* GNU tar 1.21 and later */
327 if (bsdtar->create_compression != '\0')
329 "Can't specify both -%c and -%c", opt,
330 bsdtar->create_compression);
331 bsdtar->create_compression = opt;
333 case 'k': /* GNU tar */
334 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
336 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
337 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
339 case 'L': /* BSD convention */
340 bsdtar->symlink_mode = 'L';
342 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
343 /* GNU tar 1.13 used -l for --one-file-system */
344 bsdtar->option_warn_links = 1;
347 if (bsdtar->create_compression != '\0')
349 "Can't specify both -%c and -%c", opt,
350 bsdtar->create_compression);
351 bsdtar->create_compression = opt;
353 case 'm': /* SUSv2 */
354 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
356 case 'n': /* GNU tar */
357 bsdtar->option_no_subdirs = 1;
360 * Selecting files by time:
361 * --newer-?time='date' Only files newer than 'date'
362 * --newer-?time-than='file' Only files newer than time
363 * on specified file (useful for incremental backups)
364 * TODO: Add corresponding "older" options to reverse these.
366 case OPTION_NEWER_CTIME: /* GNU tar */
367 bsdtar->newer_ctime_sec = get_date(now, bsdtar->optarg);
369 case OPTION_NEWER_CTIME_THAN:
372 if (stat(bsdtar->optarg, &st) != 0)
374 "Can't open file %s", bsdtar->optarg);
375 bsdtar->newer_ctime_sec = st.st_ctime;
376 bsdtar->newer_ctime_nsec =
377 ARCHIVE_STAT_CTIME_NANOS(&st);
380 case OPTION_NEWER_MTIME: /* GNU tar */
381 bsdtar->newer_mtime_sec = get_date(now, bsdtar->optarg);
383 case OPTION_NEWER_MTIME_THAN:
386 if (stat(bsdtar->optarg, &st) != 0)
388 "Can't open file %s", bsdtar->optarg);
389 bsdtar->newer_mtime_sec = st.st_mtime;
390 bsdtar->newer_mtime_nsec =
391 ARCHIVE_STAT_MTIME_NANOS(&st);
394 case OPTION_NODUMP: /* star */
395 bsdtar->option_honor_nodump = 1;
397 case OPTION_NO_SAME_OWNER: /* GNU tar */
398 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
400 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
401 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
402 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
403 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
404 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
406 case OPTION_NULL: /* GNU tar */
407 bsdtar->option_null++;
409 case OPTION_NUMERIC_OWNER: /* GNU tar */
413 case 'O': /* GNU tar */
414 bsdtar->option_stdout = 1;
416 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
417 option_o = 1; /* Record it and resolve it later. */
419 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
420 bsdtar->option_dont_traverse_mounts = 1;
423 bsdtar->option_options = bsdtar->optarg;
427 * The common BSD -P option is not necessary, since
428 * our default is to archive symlinks, not follow
429 * them. This is convenient, as -P conflicts with GNU
432 case 'P': /* BSD convention */
433 /* Default behavior, no option necessary. */
436 case 'P': /* GNU tar */
437 bsdtar->extract_flags &= ~SECURITY;
438 bsdtar->option_absolute_paths = 1;
440 case 'p': /* GNU tar, star */
441 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
442 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
443 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
444 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
446 case OPTION_POSIX: /* GNU tar */
447 bsdtar->create_format = "pax";
449 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
450 bsdtar->option_fast_read = 1;
452 case 'r': /* SUSv2 */
453 set_mode(bsdtar, opt);
455 case 'S': /* NetBSD pax-as-tar */
456 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
458 case 's': /* NetBSD pax-as-tar */
460 add_substitution(bsdtar, bsdtar->optarg);
463 "-s is not supported by this version of bsdtar");
467 case OPTION_SAME_OWNER: /* GNU tar */
468 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
470 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
471 bsdtar->strip_components = atoi(bsdtar->optarg);
473 case 'T': /* GNU tar */
474 bsdtar->names_from_file = bsdtar->optarg;
476 case 't': /* SUSv2 */
477 set_mode(bsdtar, opt);
480 case OPTION_TOTALS: /* GNU tar */
481 bsdtar->option_totals++;
483 case 'U': /* GNU tar */
484 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
485 bsdtar->option_unlink_first = 1;
487 case 'u': /* SUSv2 */
488 set_mode(bsdtar, opt);
490 case OPTION_UID: /* cpio */
491 t = atoi(bsdtar->optarg);
494 "Argument to --uid must be positive");
497 case OPTION_UNAME: /* cpio */
498 bsdtar->uname = bsdtar->optarg;
500 case 'v': /* SUSv2 */
503 case OPTION_VERSION: /* GNU convention */
508 * The -W longopt feature is handled inside of
509 * bsdtar_getopt(), so -W is not available here.
511 case 'W': /* Obscure GNU convention. */
514 case 'w': /* SUSv2 */
515 bsdtar->option_interactive = 1;
517 case 'X': /* GNU tar */
518 if (lafe_exclude_from_file(&bsdtar->matching, bsdtar->optarg))
520 "failed to process exclusions from file %s",
523 case 'x': /* SUSv2 */
524 set_mode(bsdtar, opt);
526 case 'y': /* FreeBSD version of GNU tar */
527 if (bsdtar->create_compression != '\0')
529 "Can't specify both -%c and -%c", opt,
530 bsdtar->create_compression);
531 bsdtar->create_compression = opt;
533 case 'Z': /* GNU tar */
534 if (bsdtar->create_compression != '\0')
536 "Can't specify both -%c and -%c", opt,
537 bsdtar->create_compression);
538 bsdtar->create_compression = opt;
540 case 'z': /* GNU tar, star, many others */
541 if (bsdtar->create_compression != '\0')
543 "Can't specify both -%c and -%c", opt,
544 bsdtar->create_compression);
545 bsdtar->create_compression = opt;
547 case OPTION_USE_COMPRESS_PROGRAM:
548 bsdtar->compress_program = bsdtar->optarg;
556 * Sanity-check options.
559 /* If no "real" mode was specified, treat -h as --help. */
560 if ((bsdtar->mode == '\0') && possible_help_request) {
565 /* Otherwise, a mode is required. */
566 if (bsdtar->mode == '\0')
568 "Must specify one of -c, -r, -t, -u, -x");
570 /* Check boolean options only permitted in certain modes. */
571 if (bsdtar->option_dont_traverse_mounts)
572 only_mode(bsdtar, "--one-file-system", "cru");
573 if (bsdtar->option_fast_read)
574 only_mode(bsdtar, "--fast-read", "xt");
575 if (bsdtar->option_honor_nodump)
576 only_mode(bsdtar, "--nodump", "cru");
578 switch (bsdtar->mode) {
581 * In GNU tar, -o means "old format." The
582 * "ustar" format is the closest thing
583 * supported by libarchive.
585 bsdtar->create_format = "ustar";
586 /* TODO: bsdtar->create_format = "v7"; */
589 /* POSIX-compatible behavior. */
590 bsdtar->option_no_owner = 1;
591 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
594 only_mode(bsdtar, "-o", "xc");
598 if (bsdtar->option_no_subdirs)
599 only_mode(bsdtar, "-n", "cru");
600 if (bsdtar->option_stdout)
601 only_mode(bsdtar, "-O", "xt");
602 if (bsdtar->option_unlink_first)
603 only_mode(bsdtar, "-U", "x");
604 if (bsdtar->option_warn_links)
605 only_mode(bsdtar, "--check-links", "cr");
607 /* Check other parameters only permitted in certain modes. */
608 if (bsdtar->create_compression != '\0') {
610 buff[1] = bsdtar->create_compression;
611 only_mode(bsdtar, buff, "cxt");
613 if (bsdtar->create_format != NULL)
614 only_mode(bsdtar, "--format", "cru");
615 if (bsdtar->symlink_mode != '\0') {
617 buff[1] = bsdtar->symlink_mode;
618 only_mode(bsdtar, buff, "cru");
620 if (bsdtar->strip_components != 0)
621 only_mode(bsdtar, "--strip-components", "xt");
623 switch(bsdtar->mode) {
641 lafe_cleanup_exclusions(&bsdtar->matching);
643 cleanup_substitution(bsdtar);
646 if (bsdtar->return_value != 0)
648 "Error exit delayed from previous errors.");
649 return (bsdtar->return_value);
653 set_mode(struct bsdtar *bsdtar, char opt)
655 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
657 "Can't specify both -%c and -%c", opt, bsdtar->mode);
662 * Verify that the mode is correct.
665 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
667 if (strchr(valid_modes, bsdtar->mode) == NULL)
669 "Option %s is not permitted in mode -%c",
681 fprintf(stderr, "Usage:\n");
682 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
683 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
684 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
685 fprintf(stderr, " Help: %s --help\n", p);
692 printf("bsdtar %s - %s\n",
693 BSDTAR_VERSION_STRING,
698 static const char *long_help_msg =
699 "First option must be a mode specifier:\n"
700 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
702 " -b # Use # 512-byte records per I/O block\n"
703 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
706 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
707 " <file>, <dir> add these items to archive\n"
708 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
709 " --format {ustar|pax|cpio|shar} Select archive format\n"
710 " --exclude <pattern> Skip files that match pattern\n"
711 " -C <dir> Change to <dir> before processing remaining files\n"
712 " @<archive> Add entries from <archive> to output\n"
713 "List: %p -t [options] [<patterns>]\n"
714 " <patterns> If specified, list only entries that match\n"
715 "Extract: %p -x [options] [<patterns>]\n"
716 " <patterns> If specified, extract only entries that match\n"
717 " -k Keep (don't overwrite) existing files\n"
718 " -m Don't restore modification times\n"
719 " -O Write entries to stdout, don't restore to disk\n"
720 " -p Restore permissions (including ACLs, owner, file flags)\n";
724 * Note that the word 'bsdtar' will always appear in the first line
727 * In particular, /bin/sh scripts that need to test for the presence
728 * of bsdtar can use the following template:
730 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
731 * echo bsdtar; else echo not bsdtar; fi
739 prog = lafe_progname;
743 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
744 printf("%s%s: manipulate archive files\n", prog, p);
746 for (p = long_help_msg; *p != '\0'; p++) {