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
71 * Per POSIX.1-1988, tar defaults to reading/writing archives to/from
72 * the default tape device for the system. Pick something reasonable here.
75 #define _PATH_DEFTAPE "/dev/st0"
77 #if defined(_WIN32) && !defined(__CYGWIN__)
78 #define _PATH_DEFTAPE "\\\\.\\tape0"
82 #define _PATH_DEFTAPE "/dev/tape"
86 int _CRT_glob = 0; /* Disable broken CRT globbing. */
89 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
90 static volatile int siginfo_occurred;
93 siginfo_handler(int sig)
95 (void)sig; /* UNUSED */
102 int r = siginfo_occurred;
103 siginfo_occurred = 0;
114 /* External function to parse a date/time string */
115 time_t get_date(time_t, const char *);
117 static void long_help(void);
118 static void only_mode(struct bsdtar *, const char *opt,
120 static void set_mode(struct bsdtar *, char opt);
121 static void version(void);
123 /* A basic set of security flags to request from libarchive. */
125 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
126 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
129 main(int argc, char **argv)
131 struct bsdtar *bsdtar, bsdtar_storage;
134 char possible_help_request;
139 * Use a pointer for consistency, but stack-allocated storage
140 * for ease of cleanup.
142 bsdtar = &bsdtar_storage;
143 memset(bsdtar, 0, sizeof(*bsdtar));
144 bsdtar->fd = -1; /* Mark as "unused" */
147 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
148 { /* Catch SIGINFO and SIGUSR1, if they exist. */
150 sa.sa_handler = siginfo_handler;
151 sigemptyset(&sa.sa_mask);
154 if (sigaction(SIGINFO, &sa, NULL))
155 bsdtar_errc(1, errno, "sigaction(SIGINFO) failed");
158 /* ... and treat SIGUSR1 the same way as SIGINFO. */
159 if (sigaction(SIGUSR1, &sa, NULL))
160 bsdtar_errc(1, errno, "sigaction(SIGUSR1) failed");
165 /* Need bsdtar_progname before calling bsdtar_warnc. */
167 bsdtar_progname = "bsdtar";
169 #if defined(_WIN32) && !defined(__CYGWIN__)
170 bsdtar_progname = strrchr(*argv, '\\');
172 bsdtar_progname = strrchr(*argv, '/');
174 if (bsdtar_progname != NULL)
177 bsdtar_progname = *argv;
183 if (setlocale(LC_ALL, "") == NULL)
184 bsdtar_warnc(0, "Failed to set default locale");
186 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
187 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
189 possible_help_request = 0;
191 /* Look up uid of current user for future reference */
192 bsdtar->user_uid = geteuid();
194 /* Default: open tape drive. */
195 bsdtar->filename = getenv("TAPE");
196 if (bsdtar->filename == NULL)
197 bsdtar->filename = _PATH_DEFTAPE;
199 /* Default: preserve mod time on extract */
200 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
202 /* Default: Perform basic security checks. */
203 bsdtar->extract_flags |= SECURITY;
206 /* On POSIX systems, assume --same-owner and -p when run by
207 * the root user. This doesn't make any sense on Windows. */
208 if (bsdtar->user_uid == 0) {
210 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
212 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
213 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
214 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
215 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
223 * Comments following each option indicate where that option
224 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
225 * no such comment, then I don't know of anyone else who
226 * implements that option.
228 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
230 case 'B': /* GNU tar */
231 /* libarchive doesn't need this; just ignore it. */
233 case 'b': /* SUSv2 */
234 t = atoi(bsdtar->optarg);
235 if (t <= 0 || t > 8192)
237 "Argument to -b is out of range (1..8192)");
238 bsdtar->bytes_per_block = 512 * t;
240 case 'C': /* GNU tar */
241 set_chdir(bsdtar, bsdtar->optarg);
243 case 'c': /* SUSv2 */
244 set_mode(bsdtar, opt);
246 case OPTION_CHECK_LINKS: /* GNU tar */
247 bsdtar->option_warn_links = 1;
249 case OPTION_CHROOT: /* NetBSD */
250 bsdtar->option_chroot = 1;
252 case OPTION_EXCLUDE: /* GNU tar */
253 if (lafe_exclude(&bsdtar->matching, bsdtar->optarg))
255 "Couldn't exclude %s\n", bsdtar->optarg);
257 case OPTION_FORMAT: /* GNU tar, others */
258 bsdtar->create_format = bsdtar->optarg;
261 bsdtar->option_options = bsdtar->optarg;
263 case 'f': /* SUSv2 */
264 bsdtar->filename = bsdtar->optarg;
265 if (strcmp(bsdtar->filename, "-") == 0)
266 bsdtar->filename = NULL;
268 case 'H': /* BSD convention */
269 bsdtar->symlink_mode = 'H';
271 case 'h': /* Linux Standards Base, gtar; synonym for -L */
272 bsdtar->symlink_mode = 'L';
273 /* Hack: -h by itself is the "help" command. */
274 possible_help_request = 1;
276 case OPTION_HELP: /* GNU tar, others */
280 case 'I': /* GNU tar */
282 * TODO: Allow 'names' to come from an archive,
283 * not just a text file. Design a good UI for
284 * allowing names and mode/owner to be read
285 * from an archive, with contents coming from
286 * disk. This can be used to "refresh" an
287 * archive or to design archives with special
288 * permissions without having to create those
289 * permissions on disk.
291 bsdtar->names_from_file = bsdtar->optarg;
295 * Noone else has the @archive extension, so
296 * noone else needs this to filter entries
297 * when transforming archives.
299 if (lafe_include(&bsdtar->matching, bsdtar->optarg))
301 "Failed to add %s to inclusion list",
304 case 'j': /* GNU tar */
305 if (bsdtar->create_compression != '\0')
307 "Can't specify both -%c and -%c", opt,
308 bsdtar->create_compression);
309 bsdtar->create_compression = opt;
311 case 'J': /* GNU tar 1.21 and later */
312 if (bsdtar->create_compression != '\0')
314 "Can't specify both -%c and -%c", opt,
315 bsdtar->create_compression);
316 bsdtar->create_compression = opt;
318 case 'k': /* GNU tar */
319 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
321 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
322 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
324 case 'L': /* BSD convention */
325 bsdtar->symlink_mode = 'L';
327 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
328 /* GNU tar 1.13 used -l for --one-file-system */
329 bsdtar->option_warn_links = 1;
332 if (bsdtar->create_compression != '\0')
334 "Can't specify both -%c and -%c", opt,
335 bsdtar->create_compression);
336 bsdtar->create_compression = opt;
338 case 'm': /* SUSv2 */
339 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
341 case 'n': /* GNU tar */
342 bsdtar->option_no_subdirs = 1;
345 * Selecting files by time:
346 * --newer-?time='date' Only files newer than 'date'
347 * --newer-?time-than='file' Only files newer than time
348 * on specified file (useful for incremental backups)
349 * TODO: Add corresponding "older" options to reverse these.
351 case OPTION_NEWER_CTIME: /* GNU tar */
352 bsdtar->newer_ctime_sec = get_date(now, bsdtar->optarg);
354 case OPTION_NEWER_CTIME_THAN:
357 if (stat(bsdtar->optarg, &st) != 0)
359 "Can't open file %s", bsdtar->optarg);
360 bsdtar->newer_ctime_sec = st.st_ctime;
361 bsdtar->newer_ctime_nsec =
362 ARCHIVE_STAT_CTIME_NANOS(&st);
365 case OPTION_NEWER_MTIME: /* GNU tar */
366 bsdtar->newer_mtime_sec = get_date(now, bsdtar->optarg);
368 case OPTION_NEWER_MTIME_THAN:
371 if (stat(bsdtar->optarg, &st) != 0)
373 "Can't open file %s", bsdtar->optarg);
374 bsdtar->newer_mtime_sec = st.st_mtime;
375 bsdtar->newer_mtime_nsec =
376 ARCHIVE_STAT_MTIME_NANOS(&st);
379 case OPTION_NODUMP: /* star */
380 bsdtar->option_honor_nodump = 1;
382 case OPTION_NO_SAME_OWNER: /* GNU tar */
383 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
385 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
386 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
387 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
388 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
389 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
391 case OPTION_NULL: /* GNU tar */
392 bsdtar->option_null++;
394 case OPTION_NUMERIC_OWNER: /* GNU tar */
395 bsdtar->option_numeric_owner++;
397 case 'O': /* GNU tar */
398 bsdtar->option_stdout = 1;
400 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
401 option_o = 1; /* Record it and resolve it later. */
403 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
404 bsdtar->option_dont_traverse_mounts = 1;
408 * The common BSD -P option is not necessary, since
409 * our default is to archive symlinks, not follow
410 * them. This is convenient, as -P conflicts with GNU
413 case 'P': /* BSD convention */
414 /* Default behavior, no option necessary. */
417 case 'P': /* GNU tar */
418 bsdtar->extract_flags &= ~SECURITY;
419 bsdtar->option_absolute_paths = 1;
421 case 'p': /* GNU tar, star */
422 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
423 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
424 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
425 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
427 case OPTION_POSIX: /* GNU tar */
428 bsdtar->create_format = "pax";
430 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
431 bsdtar->option_fast_read = 1;
433 case 'r': /* SUSv2 */
434 set_mode(bsdtar, opt);
436 case 'S': /* NetBSD pax-as-tar */
437 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
439 case 's': /* NetBSD pax-as-tar */
441 add_substitution(bsdtar, bsdtar->optarg);
444 "-s is not supported by this version of bsdtar");
448 case OPTION_SAME_OWNER: /* GNU tar */
449 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
451 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
452 bsdtar->strip_components = atoi(bsdtar->optarg);
454 case 'T': /* GNU tar */
455 bsdtar->names_from_file = bsdtar->optarg;
457 case 't': /* SUSv2 */
458 set_mode(bsdtar, opt);
461 case OPTION_TOTALS: /* GNU tar */
462 bsdtar->option_totals++;
464 case 'U': /* GNU tar */
465 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
466 bsdtar->option_unlink_first = 1;
468 case 'u': /* SUSv2 */
469 set_mode(bsdtar, opt);
471 case 'v': /* SUSv2 */
474 case OPTION_VERSION: /* GNU convention */
479 * The -W longopt feature is handled inside of
480 * bsdtar_getopt(), so -W is not available here.
482 case 'W': /* Obscure GNU convention. */
485 case 'w': /* SUSv2 */
486 bsdtar->option_interactive = 1;
488 case 'X': /* GNU tar */
489 if (lafe_exclude_from_file(&bsdtar->matching, bsdtar->optarg))
491 "failed to process exclusions from file %s",
494 case 'x': /* SUSv2 */
495 set_mode(bsdtar, opt);
497 case 'y': /* FreeBSD version of GNU tar */
498 if (bsdtar->create_compression != '\0')
500 "Can't specify both -%c and -%c", opt,
501 bsdtar->create_compression);
502 bsdtar->create_compression = opt;
504 case 'Z': /* GNU tar */
505 if (bsdtar->create_compression != '\0')
507 "Can't specify both -%c and -%c", opt,
508 bsdtar->create_compression);
509 bsdtar->create_compression = opt;
511 case 'z': /* GNU tar, star, many others */
512 if (bsdtar->create_compression != '\0')
514 "Can't specify both -%c and -%c", opt,
515 bsdtar->create_compression);
516 bsdtar->create_compression = opt;
518 case OPTION_USE_COMPRESS_PROGRAM:
519 bsdtar->compress_program = bsdtar->optarg;
527 * Sanity-check options.
530 /* If no "real" mode was specified, treat -h as --help. */
531 if ((bsdtar->mode == '\0') && possible_help_request) {
536 /* Otherwise, a mode is required. */
537 if (bsdtar->mode == '\0')
539 "Must specify one of -c, -r, -t, -u, -x");
541 /* Check boolean options only permitted in certain modes. */
542 if (bsdtar->option_dont_traverse_mounts)
543 only_mode(bsdtar, "--one-file-system", "cru");
544 if (bsdtar->option_fast_read)
545 only_mode(bsdtar, "--fast-read", "xt");
546 if (bsdtar->option_honor_nodump)
547 only_mode(bsdtar, "--nodump", "cru");
549 switch (bsdtar->mode) {
552 * In GNU tar, -o means "old format." The
553 * "ustar" format is the closest thing
554 * supported by libarchive.
556 bsdtar->create_format = "ustar";
557 /* TODO: bsdtar->create_format = "v7"; */
560 /* POSIX-compatible behavior. */
561 bsdtar->option_no_owner = 1;
562 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
565 only_mode(bsdtar, "-o", "xc");
569 if (bsdtar->option_no_subdirs)
570 only_mode(bsdtar, "-n", "cru");
571 if (bsdtar->option_stdout)
572 only_mode(bsdtar, "-O", "xt");
573 if (bsdtar->option_unlink_first)
574 only_mode(bsdtar, "-U", "x");
575 if (bsdtar->option_warn_links)
576 only_mode(bsdtar, "--check-links", "cr");
578 /* Check other parameters only permitted in certain modes. */
579 if (bsdtar->create_compression != '\0') {
581 buff[1] = bsdtar->create_compression;
582 only_mode(bsdtar, buff, "cxt");
584 if (bsdtar->create_format != NULL)
585 only_mode(bsdtar, "--format", "cru");
586 if (bsdtar->symlink_mode != '\0') {
588 buff[1] = bsdtar->symlink_mode;
589 only_mode(bsdtar, buff, "cru");
591 if (bsdtar->strip_components != 0)
592 only_mode(bsdtar, "--strip-components", "xt");
594 switch(bsdtar->mode) {
612 lafe_cleanup_exclusions(&bsdtar->matching);
614 cleanup_substitution(bsdtar);
617 if (bsdtar->return_value != 0)
619 "Error exit delayed from previous errors.");
620 return (bsdtar->return_value);
624 set_mode(struct bsdtar *bsdtar, char opt)
626 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
628 "Can't specify both -%c and -%c", opt, bsdtar->mode);
633 * Verify that the mode is correct.
636 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
638 if (strchr(valid_modes, bsdtar->mode) == NULL)
640 "Option %s is not permitted in mode -%c",
652 fprintf(stderr, "Usage:\n");
653 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
654 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
655 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
656 fprintf(stderr, " Help: %s --help\n", p);
663 printf("bsdtar %s - %s\n",
664 BSDTAR_VERSION_STRING,
669 static const char *long_help_msg =
670 "First option must be a mode specifier:\n"
671 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
673 " -b # Use # 512-byte records per I/O block\n"
674 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
677 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
678 " <file>, <dir> add these items to archive\n"
679 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
680 " --format {ustar|pax|cpio|shar} Select archive format\n"
681 " --exclude <pattern> Skip files that match pattern\n"
682 " -C <dir> Change to <dir> before processing remaining files\n"
683 " @<archive> Add entries from <archive> to output\n"
684 "List: %p -t [options] [<patterns>]\n"
685 " <patterns> If specified, list only entries that match\n"
686 "Extract: %p -x [options] [<patterns>]\n"
687 " <patterns> If specified, extract only entries that match\n"
688 " -k Keep (don't overwrite) existing files\n"
689 " -m Don't restore modification times\n"
690 " -O Write entries to stdout, don't restore to disk\n"
691 " -p Restore permissions (including ACLs, owner, file flags)\n";
695 * Note that the word 'bsdtar' will always appear in the first line
698 * In particular, /bin/sh scripts that need to test for the presence
699 * of bsdtar can use the following template:
701 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
702 * echo bsdtar; else echo not bsdtar; fi
710 prog = bsdtar_progname;
714 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
715 printf("%s%s: manipulate archive files\n", prog, p);
717 for (p = long_help_msg; *p != '\0'; p++) {