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"
88 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
89 static volatile int siginfo_occurred;
92 siginfo_handler(int sig)
94 (void)sig; /* UNUSED */
101 int r = siginfo_occurred;
102 siginfo_occurred = 0;
113 /* External function to parse a date/time string */
114 time_t get_date(time_t, const char *);
116 static void long_help(void);
117 static void only_mode(struct bsdtar *, const char *opt,
119 static void set_mode(struct bsdtar *, char opt);
120 static void version(void);
122 /* A basic set of security flags to request from libarchive. */
124 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
125 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
128 main(int argc, char **argv)
130 struct bsdtar *bsdtar, bsdtar_storage;
133 char possible_help_request;
138 * Use a pointer for consistency, but stack-allocated storage
139 * for ease of cleanup.
141 bsdtar = &bsdtar_storage;
142 memset(bsdtar, 0, sizeof(*bsdtar));
143 bsdtar->fd = -1; /* Mark as "unused" */
146 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
147 { /* Catch SIGINFO and SIGUSR1, if they exist. */
149 sa.sa_handler = siginfo_handler;
150 sigemptyset(&sa.sa_mask);
153 if (sigaction(SIGINFO, &sa, NULL))
154 bsdtar_errc(1, errno, "sigaction(SIGINFO) failed");
157 /* ... and treat SIGUSR1 the same way as SIGINFO. */
158 if (sigaction(SIGUSR1, &sa, NULL))
159 bsdtar_errc(1, errno, "sigaction(SIGUSR1) failed");
164 /* Need bsdtar_progname before calling bsdtar_warnc. */
166 bsdtar_progname = "bsdtar";
168 #if defined(_WIN32) && !defined(__CYGWIN__)
169 bsdtar_progname = strrchr(*argv, '\\');
171 bsdtar_progname = strrchr(*argv, '/');
173 if (bsdtar_progname != NULL)
176 bsdtar_progname = *argv;
181 if (setlocale(LC_ALL, "") == NULL)
182 bsdtar_warnc(0, "Failed to set default locale");
183 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
184 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
186 possible_help_request = 0;
188 /* Look up uid of current user for future reference */
189 bsdtar->user_uid = geteuid();
191 /* Default: open tape drive. */
192 bsdtar->filename = getenv("TAPE");
193 if (bsdtar->filename == NULL)
194 bsdtar->filename = _PATH_DEFTAPE;
196 /* Default: preserve mod time on extract */
197 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
199 /* Default: Perform basic security checks. */
200 bsdtar->extract_flags |= SECURITY;
203 /* On POSIX systems, assume --same-owner and -p when run by
204 * the root user. This doesn't make any sense on Windows. */
205 if (bsdtar->user_uid == 0) {
207 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
209 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
210 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
211 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
212 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
220 * Comments following each option indicate where that option
221 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
222 * no such comment, then I don't know of anyone else who
223 * implements that option.
225 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
227 case 'B': /* GNU tar */
228 /* libarchive doesn't need this; just ignore it. */
230 case 'b': /* SUSv2 */
231 t = atoi(bsdtar->optarg);
232 if (t <= 0 || t > 8192)
234 "Argument to -b is out of range (1..8192)");
235 bsdtar->bytes_per_block = 512 * t;
237 case 'C': /* GNU tar */
238 set_chdir(bsdtar, bsdtar->optarg);
240 case 'c': /* SUSv2 */
241 set_mode(bsdtar, opt);
243 case OPTION_CHECK_LINKS: /* GNU tar */
244 bsdtar->option_warn_links = 1;
246 case OPTION_CHROOT: /* NetBSD */
247 bsdtar->option_chroot = 1;
249 case OPTION_EXCLUDE: /* GNU tar */
250 if (exclude(bsdtar, bsdtar->optarg))
252 "Couldn't exclude %s\n", bsdtar->optarg);
254 case OPTION_FORMAT: /* GNU tar, others */
255 bsdtar->create_format = bsdtar->optarg;
258 bsdtar->option_options = bsdtar->optarg;
260 case 'f': /* SUSv2 */
261 bsdtar->filename = bsdtar->optarg;
262 if (strcmp(bsdtar->filename, "-") == 0)
263 bsdtar->filename = NULL;
265 case 'H': /* BSD convention */
266 bsdtar->symlink_mode = 'H';
268 case 'h': /* Linux Standards Base, gtar; synonym for -L */
269 bsdtar->symlink_mode = 'L';
270 /* Hack: -h by itself is the "help" command. */
271 possible_help_request = 1;
273 case OPTION_HELP: /* GNU tar, others */
277 case 'I': /* GNU tar */
279 * TODO: Allow 'names' to come from an archive,
280 * not just a text file. Design a good UI for
281 * allowing names and mode/owner to be read
282 * from an archive, with contents coming from
283 * disk. This can be used to "refresh" an
284 * archive or to design archives with special
285 * permissions without having to create those
286 * permissions on disk.
288 bsdtar->names_from_file = bsdtar->optarg;
292 * Noone else has the @archive extension, so
293 * noone else needs this to filter entries
294 * when transforming archives.
296 if (include(bsdtar, bsdtar->optarg))
298 "Failed to add %s to inclusion list",
301 case 'j': /* GNU tar */
303 if (bsdtar->create_compression != '\0')
305 "Can't specify both -%c and -%c", opt,
306 bsdtar->create_compression);
307 bsdtar->create_compression = opt;
310 "bzip2 compression not supported by this version of bsdtar");
314 case 'J': /* GNU tar 1.21 and later */
316 if (bsdtar->create_compression != '\0')
318 "Can't specify both -%c and -%c", opt,
319 bsdtar->create_compression);
320 bsdtar->create_compression = opt;
323 "xz compression not supported by this version of bsdtar");
327 case 'k': /* GNU tar */
328 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
330 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
331 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
333 case 'L': /* BSD convention */
334 bsdtar->symlink_mode = 'L';
336 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
337 /* GNU tar 1.13 used -l for --one-file-system */
338 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;
349 "lzma compression not supported by this version of bsdtar");
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 */
410 bsdtar->option_numeric_owner++;
412 case 'O': /* GNU tar */
413 bsdtar->option_stdout = 1;
415 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
416 option_o = 1; /* Record it and resolve it later. */
418 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
419 bsdtar->option_dont_traverse_mounts = 1;
423 * The common BSD -P option is not necessary, since
424 * our default is to archive symlinks, not follow
425 * them. This is convenient, as -P conflicts with GNU
428 case 'P': /* BSD convention */
429 /* Default behavior, no option necessary. */
432 case 'P': /* GNU tar */
433 bsdtar->extract_flags &= ~SECURITY;
434 bsdtar->option_absolute_paths = 1;
436 case 'p': /* GNU tar, star */
437 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
438 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
439 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
440 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
442 case OPTION_POSIX: /* GNU tar */
443 bsdtar->create_format = "pax";
445 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
446 bsdtar->option_fast_read = 1;
448 case 'r': /* SUSv2 */
449 set_mode(bsdtar, opt);
451 case 'S': /* NetBSD pax-as-tar */
452 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
454 case 's': /* NetBSD pax-as-tar */
456 add_substitution(bsdtar, bsdtar->optarg);
459 "-s is not supported by this version of bsdtar");
463 case OPTION_SAME_OWNER: /* GNU tar */
464 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
466 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
467 bsdtar->strip_components = atoi(bsdtar->optarg);
469 case 'T': /* GNU tar */
470 bsdtar->names_from_file = bsdtar->optarg;
472 case 't': /* SUSv2 */
473 set_mode(bsdtar, opt);
476 case OPTION_TOTALS: /* GNU tar */
477 bsdtar->option_totals++;
479 case 'U': /* GNU tar */
480 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
481 bsdtar->option_unlink_first = 1;
483 case 'u': /* SUSv2 */
484 set_mode(bsdtar, opt);
486 case 'v': /* SUSv2 */
489 case OPTION_VERSION: /* GNU convention */
494 * The -W longopt feature is handled inside of
495 * bsdtar_getopt(), so -W is not available here.
497 case 'W': /* Obscure GNU convention. */
500 case 'w': /* SUSv2 */
501 bsdtar->option_interactive = 1;
503 case 'X': /* GNU tar */
504 if (exclude_from_file(bsdtar, bsdtar->optarg))
506 "failed to process exclusions from file %s",
509 case 'x': /* SUSv2 */
510 set_mode(bsdtar, opt);
512 case 'y': /* FreeBSD version of GNU tar */
514 if (bsdtar->create_compression != '\0')
516 "Can't specify both -%c and -%c", opt,
517 bsdtar->create_compression);
518 bsdtar->create_compression = opt;
521 "bzip2 compression not supported by this version of bsdtar");
525 case 'Z': /* GNU tar */
526 if (bsdtar->create_compression != '\0')
528 "Can't specify both -%c and -%c", opt,
529 bsdtar->create_compression);
530 bsdtar->create_compression = opt;
532 case 'z': /* GNU tar, star, many others */
534 if (bsdtar->create_compression != '\0')
536 "Can't specify both -%c and -%c", opt,
537 bsdtar->create_compression);
538 bsdtar->create_compression = opt;
541 "gzip compression not supported by this version of bsdtar");
545 case OPTION_USE_COMPRESS_PROGRAM:
546 bsdtar->compress_program = bsdtar->optarg;
554 * Sanity-check options.
557 /* If no "real" mode was specified, treat -h as --help. */
558 if ((bsdtar->mode == '\0') && possible_help_request) {
563 /* Otherwise, a mode is required. */
564 if (bsdtar->mode == '\0')
566 "Must specify one of -c, -r, -t, -u, -x");
568 /* Check boolean options only permitted in certain modes. */
569 if (bsdtar->option_dont_traverse_mounts)
570 only_mode(bsdtar, "--one-file-system", "cru");
571 if (bsdtar->option_fast_read)
572 only_mode(bsdtar, "--fast-read", "xt");
573 if (bsdtar->option_honor_nodump)
574 only_mode(bsdtar, "--nodump", "cru");
576 switch (bsdtar->mode) {
579 * In GNU tar, -o means "old format." The
580 * "ustar" format is the closest thing
581 * supported by libarchive.
583 bsdtar->create_format = "ustar";
584 /* TODO: bsdtar->create_format = "v7"; */
587 /* POSIX-compatible behavior. */
588 bsdtar->option_no_owner = 1;
589 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
592 only_mode(bsdtar, "-o", "xc");
596 if (bsdtar->option_no_subdirs)
597 only_mode(bsdtar, "-n", "cru");
598 if (bsdtar->option_stdout)
599 only_mode(bsdtar, "-O", "xt");
600 if (bsdtar->option_unlink_first)
601 only_mode(bsdtar, "-U", "x");
602 if (bsdtar->option_warn_links)
603 only_mode(bsdtar, "--check-links", "cr");
605 /* Check other parameters only permitted in certain modes. */
606 if (bsdtar->create_compression != '\0') {
608 buff[1] = bsdtar->create_compression;
609 only_mode(bsdtar, buff, "cxt");
611 if (bsdtar->create_format != NULL)
612 only_mode(bsdtar, "--format", "cru");
613 if (bsdtar->symlink_mode != '\0') {
615 buff[1] = bsdtar->symlink_mode;
616 only_mode(bsdtar, buff, "cru");
618 if (bsdtar->strip_components != 0)
619 only_mode(bsdtar, "--strip-components", "xt");
621 switch(bsdtar->mode) {
639 cleanup_exclusions(bsdtar);
641 cleanup_substitution(bsdtar);
644 if (bsdtar->return_value != 0)
646 "Error exit delayed from previous errors.");
647 return (bsdtar->return_value);
651 set_mode(struct bsdtar *bsdtar, char opt)
653 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
655 "Can't specify both -%c and -%c", opt, bsdtar->mode);
660 * Verify that the mode is correct.
663 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
665 if (strchr(valid_modes, bsdtar->mode) == NULL)
667 "Option %s is not permitted in mode -%c",
679 fprintf(stderr, "Usage:\n");
680 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
681 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
682 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
683 fprintf(stderr, " Help: %s --help\n", p);
690 printf("bsdtar %s - %s\n",
691 BSDTAR_VERSION_STRING,
696 static const char *long_help_msg =
697 "First option must be a mode specifier:\n"
698 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
700 " -b # Use # 512-byte records per I/O block\n"
701 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
704 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
705 " <file>, <dir> add these items to archive\n"
706 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
707 " --format {ustar|pax|cpio|shar} Select archive format\n"
708 " --exclude <pattern> Skip files that match pattern\n"
709 " -C <dir> Change to <dir> before processing remaining files\n"
710 " @<archive> Add entries from <archive> to output\n"
711 "List: %p -t [options] [<patterns>]\n"
712 " <patterns> If specified, list only entries that match\n"
713 "Extract: %p -x [options] [<patterns>]\n"
714 " <patterns> If specified, extract only entries that match\n"
715 " -k Keep (don't overwrite) existing files\n"
716 " -m Don't restore modification times\n"
717 " -O Write entries to stdout, don't restore to disk\n"
718 " -p Restore permissions (including ACLs, owner, file flags)\n";
722 * Note that the word 'bsdtar' will always appear in the first line
725 * In particular, /bin/sh scripts that need to test for the presence
726 * of bsdtar can use the following template:
728 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
729 * echo bsdtar; else echo not bsdtar; fi
737 prog = bsdtar_progname;
741 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
742 printf("%s%s: manipulate archive files\n", prog, p);
744 for (p = long_help_msg; *p != '\0'; p++) {