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
35 #ifdef HAVE_COPYFILE_H
44 #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"
83 #if defined(__APPLE__)
85 #define _PATH_DEFTAPE "-" /* Mac OS has no tape support, default to stdio. */
89 #define _PATH_DEFTAPE "/dev/tape"
93 int _CRT_glob = 0; /* Disable broken CRT globbing. */
96 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
97 static volatile int siginfo_occurred;
100 siginfo_handler(int sig)
102 (void)sig; /* UNUSED */
103 siginfo_occurred = 1;
109 int r = siginfo_occurred;
110 siginfo_occurred = 0;
121 static void long_help(void);
122 static void only_mode(struct bsdtar *, const char *opt,
124 static void set_mode(struct bsdtar *, char opt);
125 static void version(void);
127 /* A basic set of security flags to request from libarchive. */
129 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
130 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
133 main(int argc, char **argv)
135 struct bsdtar *bsdtar, bsdtar_storage;
138 char possible_help_request;
142 * Use a pointer for consistency, but stack-allocated storage
143 * for ease of cleanup.
145 bsdtar = &bsdtar_storage;
146 memset(bsdtar, 0, sizeof(*bsdtar));
147 bsdtar->fd = -1; /* Mark as "unused" */
152 #if defined(HAVE_SIGACTION)
153 { /* Set up signal handling. */
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");
168 /* Ignore SIGPIPE signals. */
169 sa.sa_handler = SIG_IGN;
170 sigaction(SIGPIPE, &sa, NULL);
176 /* Need lafe_progname before calling lafe_warnc. */
178 lafe_progname = "bsdtar";
180 #if defined(_WIN32) && !defined(__CYGWIN__)
181 lafe_progname = strrchr(*argv, '\\');
182 if (strrchr(*argv, '/') > lafe_progname)
184 lafe_progname = strrchr(*argv, '/');
185 if (lafe_progname != NULL)
188 lafe_progname = *argv;
192 if (setlocale(LC_ALL, "") == NULL)
193 lafe_warnc(0, "Failed to set default locale");
195 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
196 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
198 possible_help_request = 0;
200 /* Look up uid of current user for future reference */
201 bsdtar->user_uid = geteuid();
203 /* Default: open tape drive. */
204 bsdtar->filename = getenv("TAPE");
205 if (bsdtar->filename == NULL)
206 bsdtar->filename = _PATH_DEFTAPE;
208 /* Default block size settings. */
209 bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK;
210 /* Allow library to default this unless user specifies -b. */
211 bsdtar->bytes_in_last_block = -1;
213 /* Default: preserve mod time on extract */
214 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
216 /* Default: Perform basic security checks. */
217 bsdtar->extract_flags |= SECURITY;
220 /* On POSIX systems, assume --same-owner and -p when run by
221 * the root user. This doesn't make any sense on Windows. */
222 if (bsdtar->user_uid == 0) {
224 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
226 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
227 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
228 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
229 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
230 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
235 * Enable Mac OS "copyfile()" extension by default.
236 * This has no effect on other platforms.
238 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
239 #ifdef COPYFILE_DISABLE_VAR
240 if (getenv(COPYFILE_DISABLE_VAR))
241 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
243 bsdtar->matching = archive_match_new();
244 if (bsdtar->matching == NULL)
245 lafe_errc(1, errno, "Out of memory");
251 * Comments following each option indicate where that option
252 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
253 * no such comment, then I don't know of anyone else who
254 * implements that option.
256 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
258 case 'B': /* GNU tar */
259 /* libarchive doesn't need this; just ignore it. */
261 case 'b': /* SUSv2 */
262 t = atoi(bsdtar->argument);
263 if (t <= 0 || t > 8192)
265 "Argument to -b is out of range (1..8192)");
266 bsdtar->bytes_per_block = 512 * t;
267 /* Explicit -b forces last block size. */
268 bsdtar->bytes_in_last_block = bsdtar->bytes_per_block;
270 case 'C': /* GNU tar */
271 if (strlen(bsdtar->argument) == 0)
273 "Meaningless option: -C ''");
275 set_chdir(bsdtar, bsdtar->argument);
277 case 'c': /* SUSv2 */
278 set_mode(bsdtar, opt);
280 case OPTION_CHECK_LINKS: /* GNU tar */
281 bsdtar->option_warn_links = 1;
283 case OPTION_CHROOT: /* NetBSD */
284 bsdtar->option_chroot = 1;
286 case OPTION_DISABLE_COPYFILE: /* Mac OS X */
287 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
289 case OPTION_EXCLUDE: /* GNU tar */
290 if (archive_match_exclude_pattern(
291 bsdtar->matching, bsdtar->argument) != ARCHIVE_OK)
293 "Couldn't exclude %s\n", bsdtar->argument);
295 case OPTION_FORMAT: /* GNU tar, others */
296 bsdtar->create_format = bsdtar->argument;
298 case 'f': /* SUSv2 */
299 bsdtar->filename = bsdtar->argument;
301 case OPTION_GID: /* cpio */
302 t = atoi(bsdtar->argument);
305 "Argument to --gid must be positive");
308 case OPTION_GNAME: /* cpio */
309 bsdtar->gname = bsdtar->argument;
311 case 'H': /* BSD convention */
312 bsdtar->symlink_mode = 'H';
314 case 'h': /* Linux Standards Base, gtar; synonym for -L */
315 bsdtar->symlink_mode = 'L';
316 /* Hack: -h by itself is the "help" command. */
317 possible_help_request = 1;
319 case OPTION_HELP: /* GNU tar, others */
323 case 'I': /* GNU tar */
325 * TODO: Allow 'names' to come from an archive,
326 * not just a text file. Design a good UI for
327 * allowing names and mode/owner to be read
328 * from an archive, with contents coming from
329 * disk. This can be used to "refresh" an
330 * archive or to design archives with special
331 * permissions without having to create those
332 * permissions on disk.
334 bsdtar->names_from_file = bsdtar->argument;
338 * No one else has the @archive extension, so
339 * no one else needs this to filter entries
340 * when transforming archives.
342 if (archive_match_include_pattern(bsdtar->matching,
343 bsdtar->argument) != ARCHIVE_OK)
345 "Failed to add %s to inclusion list",
348 case 'j': /* GNU tar */
349 if (bsdtar->create_compression != '\0')
351 "Can't specify both -%c and -%c", opt,
352 bsdtar->create_compression);
353 bsdtar->create_compression = opt;
355 case 'J': /* GNU tar 1.21 and later */
356 if (bsdtar->create_compression != '\0')
358 "Can't specify both -%c and -%c", opt,
359 bsdtar->create_compression);
360 bsdtar->create_compression = opt;
362 case 'k': /* GNU tar */
363 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
365 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
366 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
368 case 'L': /* BSD convention */
369 bsdtar->symlink_mode = 'L';
371 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
372 /* GNU tar 1.13 used -l for --one-file-system */
373 bsdtar->option_warn_links = 1;
375 case OPTION_LZIP: /* GNU tar beginning with 1.23 */
376 case OPTION_LZMA: /* GNU tar beginning with 1.20 */
377 if (bsdtar->create_compression != '\0')
379 "Can't specify both -%c and -%c", opt,
380 bsdtar->create_compression);
381 bsdtar->create_compression = opt;
383 case 'm': /* SUSv2 */
384 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
386 case 'n': /* GNU tar */
387 bsdtar->option_no_subdirs = 1;
390 * Selecting files by time:
391 * --newer-?time='date' Only files newer than 'date'
392 * --newer-?time-than='file' Only files newer than time
393 * on specified file (useful for incremental backups)
394 * TODO: Add corresponding "older" options to reverse these.
396 case OPTION_NEWER_CTIME: /* GNU tar */
397 if (archive_match_include_date(bsdtar->matching,
398 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
399 bsdtar->argument) != ARCHIVE_OK)
400 lafe_errc(1, 0, "Error : %s",
401 archive_error_string(bsdtar->matching));
403 case OPTION_NEWER_CTIME_THAN:
404 if (archive_match_include_file_time(bsdtar->matching,
405 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
406 bsdtar->argument) != ARCHIVE_OK)
407 lafe_errc(1, 0, "Error : %s",
408 archive_error_string(bsdtar->matching));
410 case OPTION_NEWER_MTIME: /* GNU tar */
411 if (archive_match_include_date(bsdtar->matching,
412 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
413 bsdtar->argument) != ARCHIVE_OK)
414 lafe_errc(1, 0, "Error : %s",
415 archive_error_string(bsdtar->matching));
417 case OPTION_NEWER_MTIME_THAN:
418 if (archive_match_include_file_time(bsdtar->matching,
419 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
420 bsdtar->argument) != ARCHIVE_OK)
421 lafe_errc(1, 0, "Error : %s",
422 archive_error_string(bsdtar->matching));
424 case OPTION_NODUMP: /* star */
425 bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP;
427 case OPTION_NO_SAME_OWNER: /* GNU tar */
428 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
430 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
431 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
432 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
433 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
434 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
435 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
437 case OPTION_NULL: /* GNU tar */
438 bsdtar->option_null++;
440 case OPTION_NUMERIC_OWNER: /* GNU tar */
443 bsdtar->option_numeric_owner++;
445 case 'O': /* GNU tar */
446 bsdtar->option_stdout = 1;
448 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
449 option_o = 1; /* Record it and resolve it later. */
451 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
452 bsdtar->readdisk_flags |=
453 ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;
456 bsdtar->option_options = bsdtar->argument;
460 * The common BSD -P option is not necessary, since
461 * our default is to archive symlinks, not follow
462 * them. This is convenient, as -P conflicts with GNU
465 case 'P': /* BSD convention */
466 /* Default behavior, no option necessary. */
469 case 'P': /* GNU tar */
470 bsdtar->extract_flags &= ~SECURITY;
471 bsdtar->option_absolute_paths = 1;
473 case 'p': /* GNU tar, star */
474 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
475 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
476 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
477 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
478 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
480 case OPTION_POSIX: /* GNU tar */
481 bsdtar->create_format = "pax";
483 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
484 bsdtar->option_fast_read = 1;
486 case 'r': /* SUSv2 */
487 set_mode(bsdtar, opt);
489 case 'S': /* NetBSD pax-as-tar */
490 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
492 case 's': /* NetBSD pax-as-tar */
494 add_substitution(bsdtar, bsdtar->argument);
497 "-s is not supported by this version of bsdtar");
501 case OPTION_SAME_OWNER: /* GNU tar */
502 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
504 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
506 bsdtar->strip_components = strtol(bsdtar->argument,
510 "Invalid --strip-components argument: %s",
513 case 'T': /* GNU tar */
514 bsdtar->names_from_file = bsdtar->argument;
516 case 't': /* SUSv2 */
517 set_mode(bsdtar, opt);
520 case OPTION_TOTALS: /* GNU tar */
521 bsdtar->option_totals++;
523 case 'U': /* GNU tar */
524 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
525 bsdtar->option_unlink_first = 1;
527 case 'u': /* SUSv2 */
528 set_mode(bsdtar, opt);
530 case OPTION_UID: /* cpio */
531 t = atoi(bsdtar->argument);
534 "Argument to --uid must be positive");
537 case OPTION_UNAME: /* cpio */
538 bsdtar->uname = bsdtar->argument;
540 case 'v': /* SUSv2 */
543 case OPTION_VERSION: /* GNU convention */
548 * The -W longopt feature is handled inside of
549 * bsdtar_getopt(), so -W is not available here.
551 case 'W': /* Obscure GNU convention. */
554 case 'w': /* SUSv2 */
555 bsdtar->option_interactive = 1;
557 case 'X': /* GNU tar */
558 if (archive_match_exclude_pattern_from_file(
559 bsdtar->matching, bsdtar->argument, 0)
561 lafe_errc(1, 0, "Error : %s",
562 archive_error_string(bsdtar->matching));
564 case 'x': /* SUSv2 */
565 set_mode(bsdtar, opt);
567 case 'y': /* FreeBSD version of GNU tar */
568 if (bsdtar->create_compression != '\0')
570 "Can't specify both -%c and -%c", opt,
571 bsdtar->create_compression);
572 bsdtar->create_compression = opt;
574 case 'Z': /* GNU tar */
575 if (bsdtar->create_compression != '\0')
577 "Can't specify both -%c and -%c", opt,
578 bsdtar->create_compression);
579 bsdtar->create_compression = opt;
581 case 'z': /* GNU tar, star, many others */
582 if (bsdtar->create_compression != '\0')
584 "Can't specify both -%c and -%c", opt,
585 bsdtar->create_compression);
586 bsdtar->create_compression = opt;
588 case OPTION_USE_COMPRESS_PROGRAM:
589 bsdtar->compress_program = bsdtar->argument;
597 * Sanity-check options.
600 /* If no "real" mode was specified, treat -h as --help. */
601 if ((bsdtar->mode == '\0') && possible_help_request) {
606 /* Otherwise, a mode is required. */
607 if (bsdtar->mode == '\0')
609 "Must specify one of -c, -r, -t, -u, -x");
611 /* Check boolean options only permitted in certain modes. */
612 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
613 only_mode(bsdtar, "--one-file-system", "cru");
614 if (bsdtar->option_fast_read)
615 only_mode(bsdtar, "--fast-read", "xt");
616 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP)
617 only_mode(bsdtar, "--nodump", "cru");
619 switch (bsdtar->mode) {
622 * In GNU tar, -o means "old format." The
623 * "ustar" format is the closest thing
624 * supported by libarchive.
626 bsdtar->create_format = "ustar";
627 /* TODO: bsdtar->create_format = "v7"; */
630 /* POSIX-compatible behavior. */
631 bsdtar->option_no_owner = 1;
632 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
635 only_mode(bsdtar, "-o", "xc");
639 if (bsdtar->option_no_subdirs)
640 only_mode(bsdtar, "-n", "cru");
641 if (bsdtar->option_stdout)
642 only_mode(bsdtar, "-O", "xt");
643 if (bsdtar->option_unlink_first)
644 only_mode(bsdtar, "-U", "x");
645 if (bsdtar->option_warn_links)
646 only_mode(bsdtar, "--check-links", "cr");
648 /* Check other parameters only permitted in certain modes. */
649 if (bsdtar->create_compression != '\0') {
651 buff[1] = bsdtar->create_compression;
652 only_mode(bsdtar, buff, "cxt");
654 if (bsdtar->create_format != NULL)
655 only_mode(bsdtar, "--format", "cru");
656 if (bsdtar->symlink_mode != '\0') {
658 buff[1] = bsdtar->symlink_mode;
659 only_mode(bsdtar, buff, "cru");
662 /* Filename "-" implies stdio. */
663 if (strcmp(bsdtar->filename, "-") == 0)
664 bsdtar->filename = NULL;
666 switch(bsdtar->mode) {
684 archive_match_free(bsdtar->matching);
686 cleanup_substitution(bsdtar);
689 if (bsdtar->return_value != 0)
691 "Error exit delayed from previous errors.");
692 return (bsdtar->return_value);
696 set_mode(struct bsdtar *bsdtar, char opt)
698 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
700 "Can't specify both -%c and -%c", opt, bsdtar->mode);
705 * Verify that the mode is correct.
708 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
710 if (strchr(valid_modes, bsdtar->mode) == NULL)
712 "Option %s is not permitted in mode -%c",
724 fprintf(stderr, "Usage:\n");
725 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
726 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
727 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
728 fprintf(stderr, " Help: %s --help\n", p);
735 printf("bsdtar %s - %s\n",
736 BSDTAR_VERSION_STRING,
737 archive_version_string());
741 static const char *long_help_msg =
742 "First option must be a mode specifier:\n"
743 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
745 " -b # Use # 512-byte records per I/O block\n"
746 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
749 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
750 " <file>, <dir> add these items to archive\n"
751 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
752 " --format {ustar|pax|cpio|shar} Select archive format\n"
753 " --exclude <pattern> Skip files that match pattern\n"
754 " -C <dir> Change to <dir> before processing remaining files\n"
755 " @<archive> Add entries from <archive> to output\n"
756 "List: %p -t [options] [<patterns>]\n"
757 " <patterns> If specified, list only entries that match\n"
758 "Extract: %p -x [options] [<patterns>]\n"
759 " <patterns> If specified, extract only entries that match\n"
760 " -k Keep (don't overwrite) existing files\n"
761 " -m Don't restore modification times\n"
762 " -O Write entries to stdout, don't restore to disk\n"
763 " -p Restore permissions (including ACLs, owner, file flags)\n";
767 * Note that the word 'bsdtar' will always appear in the first line
770 * In particular, /bin/sh scripts that need to test for the presence
771 * of bsdtar can use the following template:
773 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
774 * echo bsdtar; else echo not bsdtar; fi
782 prog = lafe_progname;
786 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
787 printf("%s%s: manipulate archive files\n", prog, p);
789 for (p = long_help_msg; *p != '\0'; p++) {