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
70 * Per POSIX.1-1988, tar defaults to reading/writing archives to/from
71 * the default tape device for the system. Pick something reasonable here.
74 #define _PATH_DEFTAPE "/dev/st0"
76 #if defined(_WIN32) && !defined(__CYGWIN__)
77 #define _PATH_DEFTAPE "\\\\.\\tape0"
81 #define _PATH_DEFTAPE "/dev/tape"
84 /* External function to parse a date/time string (from getdate.y) */
85 time_t get_date(time_t, const char *);
87 static void long_help(struct bsdtar *);
88 static void only_mode(struct bsdtar *, const char *opt,
90 static void set_mode(struct bsdtar *, char opt);
91 static void version(void);
93 /* A basic set of security flags to request from libarchive. */
95 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
96 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
99 main(int argc, char **argv)
101 struct bsdtar *bsdtar, bsdtar_storage;
104 char possible_help_request;
109 * Use a pointer for consistency, but stack-allocated storage
110 * for ease of cleanup.
112 bsdtar = &bsdtar_storage;
113 memset(bsdtar, 0, sizeof(*bsdtar));
114 bsdtar->fd = -1; /* Mark as "unused" */
116 #if defined(_WIN32) && !defined(__CYGWIN__)
117 /* Make sure open() function will be used with a binary mode. */
118 /* on cygwin, we need something similar, but instead link against */
119 /* a special startup object, binmode.o */
120 _set_fmode(_O_BINARY);
123 /* Need bsdtar->progname before calling bsdtar_warnc. */
125 bsdtar->progname = "bsdtar";
127 #if defined(_WIN32) && !defined(__CYGWIN__)
128 bsdtar->progname = strrchr(*argv, '\\');
130 bsdtar->progname = strrchr(*argv, '/');
132 if (bsdtar->progname != NULL)
135 bsdtar->progname = *argv;
140 if (setlocale(LC_ALL, "") == NULL)
141 bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
142 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
143 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
145 possible_help_request = 0;
147 /* Look up uid of current user for future reference */
148 bsdtar->user_uid = geteuid();
150 /* Default: open tape drive. */
151 bsdtar->filename = getenv("TAPE");
152 if (bsdtar->filename == NULL)
153 bsdtar->filename = _PATH_DEFTAPE;
155 /* Default: preserve mod time on extract */
156 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
158 /* Default: Perform basic security checks. */
159 bsdtar->extract_flags |= SECURITY;
161 /* Defaults for root user: */
162 if (bsdtar_is_privileged(bsdtar)) {
164 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
166 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
167 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
168 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
169 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
176 * Comments following each option indicate where that option
177 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
178 * no such comment, then I don't know of anyone else who
179 * implements that option.
181 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
183 case 'B': /* GNU tar */
184 /* libarchive doesn't need this; just ignore it. */
186 case 'b': /* SUSv2 */
187 t = atoi(bsdtar->optarg);
188 if (t <= 0 || t > 1024)
189 bsdtar_errc(bsdtar, 1, 0,
190 "Argument to -b is out of range (1..1024)");
191 bsdtar->bytes_per_block = 512 * t;
193 case 'C': /* GNU tar */
194 set_chdir(bsdtar, bsdtar->optarg);
196 case 'c': /* SUSv2 */
197 set_mode(bsdtar, opt);
199 case OPTION_CHECK_LINKS: /* GNU tar */
200 bsdtar->option_warn_links = 1;
202 case OPTION_CHROOT: /* NetBSD */
203 bsdtar->option_chroot = 1;
205 case OPTION_EXCLUDE: /* GNU tar */
206 if (exclude(bsdtar, bsdtar->optarg))
207 bsdtar_errc(bsdtar, 1, 0,
208 "Couldn't exclude %s\n", bsdtar->optarg);
210 case OPTION_FORMAT: /* GNU tar, others */
211 bsdtar->create_format = bsdtar->optarg;
214 bsdtar->option_options = bsdtar->optarg;
216 case 'f': /* SUSv2 */
217 bsdtar->filename = bsdtar->optarg;
218 if (strcmp(bsdtar->filename, "-") == 0)
219 bsdtar->filename = NULL;
221 case 'H': /* BSD convention */
222 bsdtar->symlink_mode = 'H';
224 case 'h': /* Linux Standards Base, gtar; synonym for -L */
225 bsdtar->symlink_mode = 'L';
226 /* Hack: -h by itself is the "help" command. */
227 possible_help_request = 1;
229 case OPTION_HELP: /* GNU tar, others */
233 case 'I': /* GNU tar */
235 * TODO: Allow 'names' to come from an archive,
236 * not just a text file. Design a good UI for
237 * allowing names and mode/owner to be read
238 * from an archive, with contents coming from
239 * disk. This can be used to "refresh" an
240 * archive or to design archives with special
241 * permissions without having to create those
242 * permissions on disk.
244 bsdtar->names_from_file = bsdtar->optarg;
248 * Noone else has the @archive extension, so
249 * noone else needs this to filter entries
250 * when transforming archives.
252 if (include(bsdtar, bsdtar->optarg))
253 bsdtar_errc(bsdtar, 1, 0,
254 "Failed to add %s to inclusion list",
257 case 'j': /* GNU tar */
259 if (bsdtar->create_compression != '\0')
260 bsdtar_errc(bsdtar, 1, 0,
261 "Can't specify both -%c and -%c", opt,
262 bsdtar->create_compression);
263 bsdtar->create_compression = opt;
265 bsdtar_warnc(bsdtar, 0,
266 "bzip2 compression not supported by this version of bsdtar");
270 case 'J': /* GNU tar 1.21 and later */
272 if (bsdtar->create_compression != '\0')
273 bsdtar_errc(bsdtar, 1, 0,
274 "Can't specify both -%c and -%c", opt,
275 bsdtar->create_compression);
276 bsdtar->create_compression = opt;
278 bsdtar_warnc(bsdtar, 0,
279 "xz compression not supported by this version of bsdtar");
283 case 'k': /* GNU tar */
284 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
286 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
287 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
289 case 'L': /* BSD convention */
290 bsdtar->symlink_mode = 'L';
292 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
293 /* GNU tar 1.13 used -l for --one-file-system */
294 bsdtar->option_warn_links = 1;
298 if (bsdtar->create_compression != '\0')
299 bsdtar_errc(bsdtar, 1, 0,
300 "Can't specify both -%c and -%c", opt,
301 bsdtar->create_compression);
302 bsdtar->create_compression = opt;
304 bsdtar_warnc(bsdtar, 0,
305 "lzma compression not supported by this version of bsdtar");
309 case 'm': /* SUSv2 */
310 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
312 case 'n': /* GNU tar */
313 bsdtar->option_no_subdirs = 1;
316 * Selecting files by time:
317 * --newer-?time='date' Only files newer than 'date'
318 * --newer-?time-than='file' Only files newer than time
319 * on specified file (useful for incremental backups)
320 * TODO: Add corresponding "older" options to reverse these.
322 case OPTION_NEWER_CTIME: /* GNU tar */
323 bsdtar->newer_ctime_sec = get_date(now, bsdtar->optarg);
325 case OPTION_NEWER_CTIME_THAN:
328 if (stat(bsdtar->optarg, &st) != 0)
329 bsdtar_errc(bsdtar, 1, 0,
330 "Can't open file %s", bsdtar->optarg);
331 bsdtar->newer_ctime_sec = st.st_ctime;
332 bsdtar->newer_ctime_nsec =
333 ARCHIVE_STAT_CTIME_NANOS(&st);
336 case OPTION_NEWER_MTIME: /* GNU tar */
337 bsdtar->newer_mtime_sec = get_date(now, bsdtar->optarg);
339 case OPTION_NEWER_MTIME_THAN:
342 if (stat(bsdtar->optarg, &st) != 0)
343 bsdtar_errc(bsdtar, 1, 0,
344 "Can't open file %s", bsdtar->optarg);
345 bsdtar->newer_mtime_sec = st.st_mtime;
346 bsdtar->newer_mtime_nsec =
347 ARCHIVE_STAT_MTIME_NANOS(&st);
350 case OPTION_NODUMP: /* star */
351 bsdtar->option_honor_nodump = 1;
353 case OPTION_NO_SAME_OWNER: /* GNU tar */
354 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
356 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
357 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
358 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
359 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
360 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
362 case OPTION_NULL: /* GNU tar */
363 bsdtar->option_null++;
365 case OPTION_NUMERIC_OWNER: /* GNU tar */
366 bsdtar->option_numeric_owner++;
368 case 'O': /* GNU tar */
369 bsdtar->option_stdout = 1;
371 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
372 option_o = 1; /* Record it and resolve it later. */
374 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
375 bsdtar->option_dont_traverse_mounts = 1;
379 * The common BSD -P option is not necessary, since
380 * our default is to archive symlinks, not follow
381 * them. This is convenient, as -P conflicts with GNU
384 case 'P': /* BSD convention */
385 /* Default behavior, no option necessary. */
388 case 'P': /* GNU tar */
389 bsdtar->extract_flags &= ~SECURITY;
390 bsdtar->option_absolute_paths = 1;
392 case 'p': /* GNU tar, star */
393 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
394 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
395 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
396 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
398 case OPTION_POSIX: /* GNU tar */
399 bsdtar->create_format = "pax";
401 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
402 bsdtar->option_fast_read = 1;
404 case 'r': /* SUSv2 */
405 set_mode(bsdtar, opt);
407 case 'S': /* NetBSD pax-as-tar */
408 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
410 case 's': /* NetBSD pax-as-tar */
412 add_substitution(bsdtar, bsdtar->optarg);
414 bsdtar_warnc(bsdtar, 0,
415 "-s is not supported by this version of bsdtar");
419 case OPTION_SAME_OWNER: /* GNU tar */
420 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
422 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
423 bsdtar->strip_components = atoi(bsdtar->optarg);
425 case 'T': /* GNU tar */
426 bsdtar->names_from_file = bsdtar->optarg;
428 case 't': /* SUSv2 */
429 set_mode(bsdtar, opt);
432 case OPTION_TOTALS: /* GNU tar */
433 bsdtar->option_totals++;
435 case 'U': /* GNU tar */
436 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
437 bsdtar->option_unlink_first = 1;
439 case 'u': /* SUSv2 */
440 set_mode(bsdtar, opt);
442 case 'v': /* SUSv2 */
445 case OPTION_VERSION: /* GNU convention */
450 * The -W longopt feature is handled inside of
451 * bsdtar_getopt(), so -W is not available here.
453 case 'W': /* Obscure GNU convention. */
456 case 'w': /* SUSv2 */
457 bsdtar->option_interactive = 1;
459 case 'X': /* GNU tar */
460 if (exclude_from_file(bsdtar, bsdtar->optarg))
461 bsdtar_errc(bsdtar, 1, 0,
462 "failed to process exclusions from file %s",
465 case 'x': /* SUSv2 */
466 set_mode(bsdtar, opt);
468 case 'y': /* FreeBSD version of GNU tar */
470 if (bsdtar->create_compression != '\0')
471 bsdtar_errc(bsdtar, 1, 0,
472 "Can't specify both -%c and -%c", opt,
473 bsdtar->create_compression);
474 bsdtar->create_compression = opt;
476 bsdtar_warnc(bsdtar, 0,
477 "bzip2 compression not supported by this version of bsdtar");
481 case 'Z': /* GNU tar */
482 if (bsdtar->create_compression != '\0')
483 bsdtar_errc(bsdtar, 1, 0,
484 "Can't specify both -%c and -%c", opt,
485 bsdtar->create_compression);
486 bsdtar->create_compression = opt;
488 case 'z': /* GNU tar, star, many others */
490 if (bsdtar->create_compression != '\0')
491 bsdtar_errc(bsdtar, 1, 0,
492 "Can't specify both -%c and -%c", opt,
493 bsdtar->create_compression);
494 bsdtar->create_compression = opt;
496 bsdtar_warnc(bsdtar, 0,
497 "gzip compression not supported by this version of bsdtar");
501 case OPTION_USE_COMPRESS_PROGRAM:
502 bsdtar->compress_program = bsdtar->optarg;
510 * Sanity-check options.
513 /* If no "real" mode was specified, treat -h as --help. */
514 if ((bsdtar->mode == '\0') && possible_help_request) {
519 /* Otherwise, a mode is required. */
520 if (bsdtar->mode == '\0')
521 bsdtar_errc(bsdtar, 1, 0,
522 "Must specify one of -c, -r, -t, -u, -x");
524 /* Check boolean options only permitted in certain modes. */
525 if (bsdtar->option_dont_traverse_mounts)
526 only_mode(bsdtar, "--one-file-system", "cru");
527 if (bsdtar->option_fast_read)
528 only_mode(bsdtar, "--fast-read", "xt");
529 if (bsdtar->option_honor_nodump)
530 only_mode(bsdtar, "--nodump", "cru");
532 switch (bsdtar->mode) {
535 * In GNU tar, -o means "old format." The
536 * "ustar" format is the closest thing
537 * supported by libarchive.
539 bsdtar->create_format = "ustar";
540 /* TODO: bsdtar->create_format = "v7"; */
543 /* POSIX-compatible behavior. */
544 bsdtar->option_no_owner = 1;
545 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
548 only_mode(bsdtar, "-o", "xc");
552 if (bsdtar->option_no_subdirs)
553 only_mode(bsdtar, "-n", "cru");
554 if (bsdtar->option_stdout)
555 only_mode(bsdtar, "-O", "xt");
556 if (bsdtar->option_unlink_first)
557 only_mode(bsdtar, "-U", "x");
558 if (bsdtar->option_warn_links)
559 only_mode(bsdtar, "--check-links", "cr");
561 /* Check other parameters only permitted in certain modes. */
562 if (bsdtar->create_compression != '\0') {
564 buff[1] = bsdtar->create_compression;
565 only_mode(bsdtar, buff, "cxt");
567 if (bsdtar->create_format != NULL)
568 only_mode(bsdtar, "--format", "cru");
569 if (bsdtar->symlink_mode != '\0') {
571 buff[1] = bsdtar->symlink_mode;
572 only_mode(bsdtar, buff, "cru");
574 if (bsdtar->strip_components != 0)
575 only_mode(bsdtar, "--strip-components", "xt");
577 switch(bsdtar->mode) {
595 cleanup_exclusions(bsdtar);
597 cleanup_substitution(bsdtar);
600 if (bsdtar->return_value != 0)
601 bsdtar_warnc(bsdtar, 0,
602 "Error exit delayed from previous errors.");
603 return (bsdtar->return_value);
607 set_mode(struct bsdtar *bsdtar, char opt)
609 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
610 bsdtar_errc(bsdtar, 1, 0,
611 "Can't specify both -%c and -%c", opt, bsdtar->mode);
616 * Verify that the mode is correct.
619 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
621 if (strchr(valid_modes, bsdtar->mode) == NULL)
622 bsdtar_errc(bsdtar, 1, 0,
623 "Option %s is not permitted in mode -%c",
629 usage(struct bsdtar *bsdtar)
633 p = bsdtar->progname;
635 fprintf(stderr, "Usage:\n");
636 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
637 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
638 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
639 fprintf(stderr, " Help: %s --help\n", p);
646 printf("bsdtar %s - %s\n",
647 BSDTAR_VERSION_STRING,
652 static const char *long_help_msg =
653 "First option must be a mode specifier:\n"
654 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
656 " -b # Use # 512-byte records per I/O block\n"
657 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
660 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
661 " <file>, <dir> add these items to archive\n"
662 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
663 " --format {ustar|pax|cpio|shar} Select archive format\n"
664 " --exclude <pattern> Skip files that match pattern\n"
665 " -C <dir> Change to <dir> before processing remaining files\n"
666 " @<archive> Add entries from <archive> to output\n"
667 "List: %p -t [options] [<patterns>]\n"
668 " <patterns> If specified, list only entries that match\n"
669 "Extract: %p -x [options] [<patterns>]\n"
670 " <patterns> If specified, extract only entries that match\n"
671 " -k Keep (don't overwrite) existing files\n"
672 " -m Don't restore modification times\n"
673 " -O Write entries to stdout, don't restore to disk\n"
674 " -p Restore permissions (including ACLs, owner, file flags)\n";
678 * Note that the word 'bsdtar' will always appear in the first line
681 * In particular, /bin/sh scripts that need to test for the presence
682 * of bsdtar can use the following template:
684 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
685 * echo bsdtar; else echo not bsdtar; fi
688 long_help(struct bsdtar *bsdtar)
693 prog = bsdtar->progname;
697 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
698 printf("%s%s: manipulate archive files\n", prog, p);
700 for (p = long_help_msg; *p != '\0'; p++) {