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" */
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");
170 /* Need lafe_progname before calling lafe_warnc. */
172 lafe_progname = "bsdtar";
174 #if defined(_WIN32) && !defined(__CYGWIN__)
175 lafe_progname = strrchr(*argv, '\\');
177 lafe_progname = strrchr(*argv, '/');
179 if (lafe_progname != NULL)
182 lafe_progname = *argv;
188 if (setlocale(LC_ALL, "") == NULL)
189 lafe_warnc(0, "Failed to set default locale");
191 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
192 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
194 possible_help_request = 0;
196 /* Look up uid of current user for future reference */
197 bsdtar->user_uid = geteuid();
199 /* Default: open tape drive. */
200 bsdtar->filename = getenv("TAPE");
201 if (bsdtar->filename == NULL)
202 bsdtar->filename = _PATH_DEFTAPE;
204 /* Default: preserve mod time on extract */
205 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
207 /* Default: Perform basic security checks. */
208 bsdtar->extract_flags |= SECURITY;
211 /* On POSIX systems, assume --same-owner and -p when run by
212 * the root user. This doesn't make any sense on Windows. */
213 if (bsdtar->user_uid == 0) {
215 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
217 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
218 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
219 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
220 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
228 * Comments following each option indicate where that option
229 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
230 * no such comment, then I don't know of anyone else who
231 * implements that option.
233 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
235 case 'B': /* GNU tar */
236 /* libarchive doesn't need this; just ignore it. */
238 case 'b': /* SUSv2 */
239 t = atoi(bsdtar->optarg);
240 if (t <= 0 || t > 8192)
242 "Argument to -b is out of range (1..8192)");
243 bsdtar->bytes_per_block = 512 * t;
245 case 'C': /* GNU tar */
246 set_chdir(bsdtar, bsdtar->optarg);
248 case 'c': /* SUSv2 */
249 set_mode(bsdtar, opt);
251 case OPTION_CHECK_LINKS: /* GNU tar */
252 bsdtar->option_warn_links = 1;
254 case OPTION_CHROOT: /* NetBSD */
255 bsdtar->option_chroot = 1;
257 case OPTION_EXCLUDE: /* GNU tar */
258 if (lafe_exclude(&bsdtar->matching, bsdtar->optarg))
260 "Couldn't exclude %s\n", bsdtar->optarg);
262 case OPTION_FORMAT: /* GNU tar, others */
263 bsdtar->create_format = bsdtar->optarg;
266 bsdtar->option_options = bsdtar->optarg;
268 case 'f': /* SUSv2 */
269 bsdtar->filename = bsdtar->optarg;
270 if (strcmp(bsdtar->filename, "-") == 0)
271 bsdtar->filename = NULL;
273 case 'H': /* BSD convention */
274 bsdtar->symlink_mode = 'H';
276 case 'h': /* Linux Standards Base, gtar; synonym for -L */
277 bsdtar->symlink_mode = 'L';
278 /* Hack: -h by itself is the "help" command. */
279 possible_help_request = 1;
281 case OPTION_HELP: /* GNU tar, others */
285 case 'I': /* GNU tar */
287 * TODO: Allow 'names' to come from an archive,
288 * not just a text file. Design a good UI for
289 * allowing names and mode/owner to be read
290 * from an archive, with contents coming from
291 * disk. This can be used to "refresh" an
292 * archive or to design archives with special
293 * permissions without having to create those
294 * permissions on disk.
296 bsdtar->names_from_file = bsdtar->optarg;
300 * Noone else has the @archive extension, so
301 * noone else needs this to filter entries
302 * when transforming archives.
304 if (lafe_include(&bsdtar->matching, bsdtar->optarg))
306 "Failed to add %s to inclusion list",
309 case 'j': /* GNU tar */
310 if (bsdtar->create_compression != '\0')
312 "Can't specify both -%c and -%c", opt,
313 bsdtar->create_compression);
314 bsdtar->create_compression = opt;
316 case 'J': /* GNU tar 1.21 and later */
317 if (bsdtar->create_compression != '\0')
319 "Can't specify both -%c and -%c", opt,
320 bsdtar->create_compression);
321 bsdtar->create_compression = opt;
323 case 'k': /* GNU tar */
324 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
326 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
327 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
329 case 'L': /* BSD convention */
330 bsdtar->symlink_mode = 'L';
332 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
333 /* GNU tar 1.13 used -l for --one-file-system */
334 bsdtar->option_warn_links = 1;
337 if (bsdtar->create_compression != '\0')
339 "Can't specify both -%c and -%c", opt,
340 bsdtar->create_compression);
341 bsdtar->create_compression = opt;
343 case 'm': /* SUSv2 */
344 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
346 case 'n': /* GNU tar */
347 bsdtar->option_no_subdirs = 1;
350 * Selecting files by time:
351 * --newer-?time='date' Only files newer than 'date'
352 * --newer-?time-than='file' Only files newer than time
353 * on specified file (useful for incremental backups)
354 * TODO: Add corresponding "older" options to reverse these.
356 case OPTION_NEWER_CTIME: /* GNU tar */
357 bsdtar->newer_ctime_sec = get_date(now, bsdtar->optarg);
359 case OPTION_NEWER_CTIME_THAN:
362 if (stat(bsdtar->optarg, &st) != 0)
364 "Can't open file %s", bsdtar->optarg);
365 bsdtar->newer_ctime_sec = st.st_ctime;
366 bsdtar->newer_ctime_nsec =
367 ARCHIVE_STAT_CTIME_NANOS(&st);
370 case OPTION_NEWER_MTIME: /* GNU tar */
371 bsdtar->newer_mtime_sec = get_date(now, bsdtar->optarg);
373 case OPTION_NEWER_MTIME_THAN:
376 if (stat(bsdtar->optarg, &st) != 0)
378 "Can't open file %s", bsdtar->optarg);
379 bsdtar->newer_mtime_sec = st.st_mtime;
380 bsdtar->newer_mtime_nsec =
381 ARCHIVE_STAT_MTIME_NANOS(&st);
384 case OPTION_NODUMP: /* star */
385 bsdtar->option_honor_nodump = 1;
387 case OPTION_NO_SAME_OWNER: /* GNU tar */
388 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
390 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
391 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
392 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
393 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
394 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
396 case OPTION_NULL: /* GNU tar */
397 bsdtar->option_null++;
399 case OPTION_NUMERIC_OWNER: /* GNU tar */
400 bsdtar->option_numeric_owner++;
402 case 'O': /* GNU tar */
403 bsdtar->option_stdout = 1;
405 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
406 option_o = 1; /* Record it and resolve it later. */
408 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
409 bsdtar->option_dont_traverse_mounts = 1;
413 * The common BSD -P option is not necessary, since
414 * our default is to archive symlinks, not follow
415 * them. This is convenient, as -P conflicts with GNU
418 case 'P': /* BSD convention */
419 /* Default behavior, no option necessary. */
422 case 'P': /* GNU tar */
423 bsdtar->extract_flags &= ~SECURITY;
424 bsdtar->option_absolute_paths = 1;
426 case 'p': /* GNU tar, star */
427 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
428 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
429 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
430 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
432 case OPTION_POSIX: /* GNU tar */
433 bsdtar->create_format = "pax";
435 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
436 bsdtar->option_fast_read = 1;
438 case 'r': /* SUSv2 */
439 set_mode(bsdtar, opt);
441 case 'S': /* NetBSD pax-as-tar */
442 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
444 case 's': /* NetBSD pax-as-tar */
446 add_substitution(bsdtar, bsdtar->optarg);
449 "-s is not supported by this version of bsdtar");
453 case OPTION_SAME_OWNER: /* GNU tar */
454 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
456 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
457 bsdtar->strip_components = atoi(bsdtar->optarg);
459 case 'T': /* GNU tar */
460 bsdtar->names_from_file = bsdtar->optarg;
462 case 't': /* SUSv2 */
463 set_mode(bsdtar, opt);
466 case OPTION_TOTALS: /* GNU tar */
467 bsdtar->option_totals++;
469 case 'U': /* GNU tar */
470 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
471 bsdtar->option_unlink_first = 1;
473 case 'u': /* SUSv2 */
474 set_mode(bsdtar, opt);
476 case 'v': /* SUSv2 */
479 case OPTION_VERSION: /* GNU convention */
484 * The -W longopt feature is handled inside of
485 * bsdtar_getopt(), so -W is not available here.
487 case 'W': /* Obscure GNU convention. */
490 case 'w': /* SUSv2 */
491 bsdtar->option_interactive = 1;
493 case 'X': /* GNU tar */
494 if (lafe_exclude_from_file(&bsdtar->matching, bsdtar->optarg))
496 "failed to process exclusions from file %s",
499 case 'x': /* SUSv2 */
500 set_mode(bsdtar, opt);
502 case 'y': /* FreeBSD version of GNU tar */
503 if (bsdtar->create_compression != '\0')
505 "Can't specify both -%c and -%c", opt,
506 bsdtar->create_compression);
507 bsdtar->create_compression = opt;
509 case 'Z': /* GNU tar */
510 if (bsdtar->create_compression != '\0')
512 "Can't specify both -%c and -%c", opt,
513 bsdtar->create_compression);
514 bsdtar->create_compression = opt;
516 case 'z': /* GNU tar, star, many others */
517 if (bsdtar->create_compression != '\0')
519 "Can't specify both -%c and -%c", opt,
520 bsdtar->create_compression);
521 bsdtar->create_compression = opt;
523 case OPTION_USE_COMPRESS_PROGRAM:
524 bsdtar->compress_program = bsdtar->optarg;
532 * Sanity-check options.
535 /* If no "real" mode was specified, treat -h as --help. */
536 if ((bsdtar->mode == '\0') && possible_help_request) {
541 /* Otherwise, a mode is required. */
542 if (bsdtar->mode == '\0')
544 "Must specify one of -c, -r, -t, -u, -x");
546 /* Check boolean options only permitted in certain modes. */
547 if (bsdtar->option_dont_traverse_mounts)
548 only_mode(bsdtar, "--one-file-system", "cru");
549 if (bsdtar->option_fast_read)
550 only_mode(bsdtar, "--fast-read", "xt");
551 if (bsdtar->option_honor_nodump)
552 only_mode(bsdtar, "--nodump", "cru");
554 switch (bsdtar->mode) {
557 * In GNU tar, -o means "old format." The
558 * "ustar" format is the closest thing
559 * supported by libarchive.
561 bsdtar->create_format = "ustar";
562 /* TODO: bsdtar->create_format = "v7"; */
565 /* POSIX-compatible behavior. */
566 bsdtar->option_no_owner = 1;
567 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
570 only_mode(bsdtar, "-o", "xc");
574 if (bsdtar->option_no_subdirs)
575 only_mode(bsdtar, "-n", "cru");
576 if (bsdtar->option_stdout)
577 only_mode(bsdtar, "-O", "xt");
578 if (bsdtar->option_unlink_first)
579 only_mode(bsdtar, "-U", "x");
580 if (bsdtar->option_warn_links)
581 only_mode(bsdtar, "--check-links", "cr");
583 /* Check other parameters only permitted in certain modes. */
584 if (bsdtar->create_compression != '\0') {
586 buff[1] = bsdtar->create_compression;
587 only_mode(bsdtar, buff, "cxt");
589 if (bsdtar->create_format != NULL)
590 only_mode(bsdtar, "--format", "cru");
591 if (bsdtar->symlink_mode != '\0') {
593 buff[1] = bsdtar->symlink_mode;
594 only_mode(bsdtar, buff, "cru");
596 if (bsdtar->strip_components != 0)
597 only_mode(bsdtar, "--strip-components", "xt");
599 switch(bsdtar->mode) {
617 lafe_cleanup_exclusions(&bsdtar->matching);
619 cleanup_substitution(bsdtar);
622 if (bsdtar->return_value != 0)
624 "Error exit delayed from previous errors.");
625 return (bsdtar->return_value);
629 set_mode(struct bsdtar *bsdtar, char opt)
631 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
633 "Can't specify both -%c and -%c", opt, bsdtar->mode);
638 * Verify that the mode is correct.
641 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
643 if (strchr(valid_modes, bsdtar->mode) == NULL)
645 "Option %s is not permitted in mode -%c",
657 fprintf(stderr, "Usage:\n");
658 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
659 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
660 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
661 fprintf(stderr, " Help: %s --help\n", p);
668 printf("bsdtar %s - %s\n",
669 BSDTAR_VERSION_STRING,
674 static const char *long_help_msg =
675 "First option must be a mode specifier:\n"
676 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
678 " -b # Use # 512-byte records per I/O block\n"
679 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
682 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
683 " <file>, <dir> add these items to archive\n"
684 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
685 " --format {ustar|pax|cpio|shar} Select archive format\n"
686 " --exclude <pattern> Skip files that match pattern\n"
687 " -C <dir> Change to <dir> before processing remaining files\n"
688 " @<archive> Add entries from <archive> to output\n"
689 "List: %p -t [options] [<patterns>]\n"
690 " <patterns> If specified, list only entries that match\n"
691 "Extract: %p -x [options] [<patterns>]\n"
692 " <patterns> If specified, extract only entries that match\n"
693 " -k Keep (don't overwrite) existing files\n"
694 " -m Don't restore modification times\n"
695 " -O Write entries to stdout, don't restore to disk\n"
696 " -p Restore permissions (including ACLs, owner, file flags)\n";
700 * Note that the word 'bsdtar' will always appear in the first line
703 * In particular, /bin/sh scripts that need to test for the presence
704 * of bsdtar can use the following template:
706 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
707 * echo bsdtar; else echo not bsdtar; fi
715 prog = lafe_progname;
719 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
720 printf("%s%s: manipulate archive files\n", prog, p);
722 for (p = long_help_msg; *p != '\0'; p++) {