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"
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" */
117 /* Make sure open() function will be used with a binary mode. */
118 _set_fmode(_O_BINARY);
121 /* Need bsdtar->progname before calling bsdtar_warnc. */
123 bsdtar->progname = "bsdtar";
126 bsdtar->progname = strrchr(*argv, '\\');
128 bsdtar->progname = strrchr(*argv, '/');
130 if (bsdtar->progname != NULL)
133 bsdtar->progname = *argv;
138 if (setlocale(LC_ALL, "") == NULL)
139 bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
140 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
141 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
143 possible_help_request = 0;
145 /* Look up uid of current user for future reference */
146 bsdtar->user_uid = geteuid();
148 /* Default: open tape drive. */
149 bsdtar->filename = getenv("TAPE");
150 if (bsdtar->filename == NULL)
151 bsdtar->filename = _PATH_DEFTAPE;
153 /* Default: preserve mod time on extract */
154 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
156 /* Default: Perform basic security checks. */
157 bsdtar->extract_flags |= SECURITY;
159 /* Defaults for root user: */
160 if (bsdtar_is_privileged(bsdtar)) {
162 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
164 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
165 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
166 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
167 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
170 /* Windows cannot set UNIX like uid/gid. */
171 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
178 * Comments following each option indicate where that option
179 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
180 * no such comment, then I don't know of anyone else who
181 * implements that option.
183 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
185 case 'B': /* GNU tar */
186 /* libarchive doesn't need this; just ignore it. */
188 case 'b': /* SUSv2 */
189 t = atoi(bsdtar->optarg);
190 if (t <= 0 || t > 1024)
191 bsdtar_errc(bsdtar, 1, 0,
192 "Argument to -b is out of range (1..1024)");
193 bsdtar->bytes_per_block = 512 * t;
195 case 'C': /* GNU tar */
196 set_chdir(bsdtar, bsdtar->optarg);
198 case 'c': /* SUSv2 */
199 set_mode(bsdtar, opt);
201 case OPTION_CHECK_LINKS: /* GNU tar */
202 bsdtar->option_warn_links = 1;
204 case OPTION_CHROOT: /* NetBSD */
205 bsdtar->option_chroot = 1;
207 case OPTION_EXCLUDE: /* GNU tar */
208 if (exclude(bsdtar, bsdtar->optarg))
209 bsdtar_errc(bsdtar, 1, 0,
210 "Couldn't exclude %s\n", bsdtar->optarg);
212 case OPTION_FORMAT: /* GNU tar, others */
213 bsdtar->create_format = bsdtar->optarg;
215 case OPTION_FORMAT_OPTIONS:
216 bsdtar->option_format_options = bsdtar->optarg;
218 case 'f': /* SUSv2 */
219 bsdtar->filename = bsdtar->optarg;
220 if (strcmp(bsdtar->filename, "-") == 0)
221 bsdtar->filename = NULL;
223 case 'H': /* BSD convention */
224 bsdtar->symlink_mode = 'H';
226 case 'h': /* Linux Standards Base, gtar; synonym for -L */
227 bsdtar->symlink_mode = 'L';
228 /* Hack: -h by itself is the "help" command. */
229 possible_help_request = 1;
231 case OPTION_HELP: /* GNU tar, others */
235 case 'I': /* GNU tar */
237 * TODO: Allow 'names' to come from an archive,
238 * not just a text file. Design a good UI for
239 * allowing names and mode/owner to be read
240 * from an archive, with contents coming from
241 * disk. This can be used to "refresh" an
242 * archive or to design archives with special
243 * permissions without having to create those
244 * permissions on disk.
246 bsdtar->names_from_file = bsdtar->optarg;
250 * Noone else has the @archive extension, so
251 * noone else needs this to filter entries
252 * when transforming archives.
254 if (include(bsdtar, bsdtar->optarg))
255 bsdtar_errc(bsdtar, 1, 0,
256 "Failed to add %s to inclusion list",
259 case 'j': /* GNU tar */
261 if (bsdtar->create_compression != '\0')
262 bsdtar_errc(bsdtar, 1, 0,
263 "Can't specify both -%c and -%c", opt,
264 bsdtar->create_compression);
265 bsdtar->create_compression = opt;
267 bsdtar_warnc(bsdtar, 0,
268 "bzip2 compression not supported by this version of bsdtar");
272 case 'k': /* GNU tar */
273 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
275 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
276 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
278 case 'L': /* BSD convention */
279 bsdtar->symlink_mode = 'L';
281 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
282 /* GNU tar 1.13 used -l for --one-file-system */
283 bsdtar->option_warn_links = 1;
285 case 'm': /* SUSv2 */
286 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
288 case 'n': /* GNU tar */
289 bsdtar->option_no_subdirs = 1;
292 * Selecting files by time:
293 * --newer-?time='date' Only files newer than 'date'
294 * --newer-?time-than='file' Only files newer than time
295 * on specified file (useful for incremental backups)
296 * TODO: Add corresponding "older" options to reverse these.
298 case OPTION_NEWER_CTIME: /* GNU tar */
299 bsdtar->newer_ctime_sec = get_date(now, bsdtar->optarg);
301 case OPTION_NEWER_CTIME_THAN:
304 if (stat(bsdtar->optarg, &st) != 0)
305 bsdtar_errc(bsdtar, 1, 0,
306 "Can't open file %s", bsdtar->optarg);
307 bsdtar->newer_ctime_sec = st.st_ctime;
308 bsdtar->newer_ctime_nsec =
309 ARCHIVE_STAT_CTIME_NANOS(&st);
312 case OPTION_NEWER_MTIME: /* GNU tar */
313 bsdtar->newer_mtime_sec = get_date(now, bsdtar->optarg);
315 case OPTION_NEWER_MTIME_THAN:
318 if (stat(bsdtar->optarg, &st) != 0)
319 bsdtar_errc(bsdtar, 1, 0,
320 "Can't open file %s", bsdtar->optarg);
321 bsdtar->newer_mtime_sec = st.st_mtime;
322 bsdtar->newer_mtime_nsec =
323 ARCHIVE_STAT_MTIME_NANOS(&st);
326 case OPTION_NODUMP: /* star */
327 bsdtar->option_honor_nodump = 1;
329 case OPTION_NO_SAME_OWNER: /* GNU tar */
330 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
332 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
333 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
334 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
335 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
336 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
338 case OPTION_NULL: /* GNU tar */
339 bsdtar->option_null++;
341 case OPTION_NUMERIC_OWNER: /* GNU tar */
342 bsdtar->option_numeric_owner++;
344 case 'O': /* GNU tar */
345 bsdtar->option_stdout = 1;
347 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
348 option_o = 1; /* Record it and resolve it later. */
350 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
351 bsdtar->option_dont_traverse_mounts = 1;
355 * The common BSD -P option is not necessary, since
356 * our default is to archive symlinks, not follow
357 * them. This is convenient, as -P conflicts with GNU
360 case 'P': /* BSD convention */
361 /* Default behavior, no option necessary. */
364 case 'P': /* GNU tar */
365 bsdtar->extract_flags &= ~SECURITY;
366 bsdtar->option_absolute_paths = 1;
368 case 'p': /* GNU tar, star */
369 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
370 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
371 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
372 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
374 case OPTION_POSIX: /* GNU tar */
375 bsdtar->create_format = "pax";
377 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
378 bsdtar->option_fast_read = 1;
380 case 'r': /* SUSv2 */
381 set_mode(bsdtar, opt);
383 case 'S': /* NetBSD pax-as-tar */
384 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
386 case 's': /* NetBSD pax-as-tar */
388 add_substitution(bsdtar, bsdtar->optarg);
390 bsdtar_warnc(bsdtar, 0,
391 "-s is not supported by this version of bsdtar");
395 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
396 bsdtar->strip_components = atoi(bsdtar->optarg);
398 case 'T': /* GNU tar */
399 bsdtar->names_from_file = bsdtar->optarg;
401 case 't': /* SUSv2 */
402 set_mode(bsdtar, opt);
405 case OPTION_TOTALS: /* GNU tar */
406 bsdtar->option_totals++;
408 case 'U': /* GNU tar */
409 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
410 bsdtar->option_unlink_first = 1;
412 case 'u': /* SUSv2 */
413 set_mode(bsdtar, opt);
415 case 'v': /* SUSv2 */
418 case OPTION_VERSION: /* GNU convention */
423 * The -W longopt feature is handled inside of
424 * bsdtar_getopt(), so -W is not available here.
426 case 'W': /* Obscure GNU convention. */
429 case 'w': /* SUSv2 */
430 bsdtar->option_interactive = 1;
432 case 'X': /* GNU tar */
433 if (exclude_from_file(bsdtar, bsdtar->optarg))
434 bsdtar_errc(bsdtar, 1, 0,
435 "failed to process exclusions from file %s",
438 case 'x': /* SUSv2 */
439 set_mode(bsdtar, opt);
441 case 'y': /* FreeBSD version of GNU tar */
443 if (bsdtar->create_compression != '\0')
444 bsdtar_errc(bsdtar, 1, 0,
445 "Can't specify both -%c and -%c", opt,
446 bsdtar->create_compression);
447 bsdtar->create_compression = opt;
449 bsdtar_warnc(bsdtar, 0,
450 "bzip2 compression not supported by this version of bsdtar");
454 case 'Z': /* GNU tar */
455 if (bsdtar->create_compression != '\0')
456 bsdtar_errc(bsdtar, 1, 0,
457 "Can't specify both -%c and -%c", opt,
458 bsdtar->create_compression);
459 bsdtar->create_compression = opt;
461 case 'z': /* GNU tar, star, many others */
463 if (bsdtar->create_compression != '\0')
464 bsdtar_errc(bsdtar, 1, 0,
465 "Can't specify both -%c and -%c", opt,
466 bsdtar->create_compression);
467 bsdtar->create_compression = opt;
469 bsdtar_warnc(bsdtar, 0,
470 "gzip compression not supported by this version of bsdtar");
474 case OPTION_USE_COMPRESS_PROGRAM:
475 bsdtar->compress_program = bsdtar->optarg;
483 * Sanity-check options.
486 /* If no "real" mode was specified, treat -h as --help. */
487 if ((bsdtar->mode == '\0') && possible_help_request) {
492 /* Otherwise, a mode is required. */
493 if (bsdtar->mode == '\0')
494 bsdtar_errc(bsdtar, 1, 0,
495 "Must specify one of -c, -r, -t, -u, -x");
497 /* Check boolean options only permitted in certain modes. */
498 if (bsdtar->option_dont_traverse_mounts)
499 only_mode(bsdtar, "--one-file-system", "cru");
500 if (bsdtar->option_fast_read)
501 only_mode(bsdtar, "--fast-read", "xt");
502 if (bsdtar->option_honor_nodump)
503 only_mode(bsdtar, "--nodump", "cru");
505 switch (bsdtar->mode) {
508 * In GNU tar, -o means "old format." The
509 * "ustar" format is the closest thing
510 * supported by libarchive.
512 bsdtar->create_format = "ustar";
513 /* TODO: bsdtar->create_format = "v7"; */
516 /* POSIX-compatible behavior. */
517 bsdtar->option_no_owner = 1;
518 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
521 only_mode(bsdtar, "-o", "xc");
525 if (bsdtar->option_no_subdirs)
526 only_mode(bsdtar, "-n", "cru");
527 if (bsdtar->option_stdout)
528 only_mode(bsdtar, "-O", "xt");
529 if (bsdtar->option_unlink_first)
530 only_mode(bsdtar, "-U", "x");
531 if (bsdtar->option_warn_links)
532 only_mode(bsdtar, "--check-links", "cr");
534 /* Check other parameters only permitted in certain modes. */
535 if (bsdtar->create_compression != '\0') {
537 buff[1] = bsdtar->create_compression;
538 only_mode(bsdtar, buff, "cxt");
540 if (bsdtar->create_format != NULL)
541 only_mode(bsdtar, "--format", "cru");
542 if (bsdtar->symlink_mode != '\0') {
544 buff[1] = bsdtar->symlink_mode;
545 only_mode(bsdtar, buff, "cru");
547 if (bsdtar->strip_components != 0)
548 only_mode(bsdtar, "--strip-components", "xt");
550 switch(bsdtar->mode) {
568 cleanup_exclusions(bsdtar);
570 cleanup_substitution(bsdtar);
573 if (bsdtar->return_value != 0)
574 bsdtar_warnc(bsdtar, 0,
575 "Error exit delayed from previous errors.");
576 return (bsdtar->return_value);
580 set_mode(struct bsdtar *bsdtar, char opt)
582 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
583 bsdtar_errc(bsdtar, 1, 0,
584 "Can't specify both -%c and -%c", opt, bsdtar->mode);
589 * Verify that the mode is correct.
592 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
594 if (strchr(valid_modes, bsdtar->mode) == NULL)
595 bsdtar_errc(bsdtar, 1, 0,
596 "Option %s is not permitted in mode -%c",
602 usage(struct bsdtar *bsdtar)
606 p = bsdtar->progname;
608 fprintf(stderr, "Usage:\n");
609 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
610 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
611 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
612 fprintf(stderr, " Help: %s --help\n", p);
619 printf("bsdtar %s - %s\n",
620 BSDTAR_VERSION_STRING,
625 static const char *long_help_msg =
626 "First option must be a mode specifier:\n"
627 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
629 " -b # Use # 512-byte records per I/O block\n"
630 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
633 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
634 " <file>, <dir> add these items to archive\n"
635 " -z, -j Compress archive with gzip/bzip2\n"
636 " --format {ustar|pax|cpio|shar} Select archive format\n"
637 " --exclude <pattern> Skip files that match pattern\n"
638 " -C <dir> Change to <dir> before processing remaining files\n"
639 " @<archive> Add entries from <archive> to output\n"
640 "List: %p -t [options] [<patterns>]\n"
641 " <patterns> If specified, list only entries that match\n"
642 "Extract: %p -x [options] [<patterns>]\n"
643 " <patterns> If specified, extract only entries that match\n"
644 " -k Keep (don't overwrite) existing files\n"
645 " -m Don't restore modification times\n"
646 " -O Write entries to stdout, don't restore to disk\n"
647 " -p Restore permissions (including ACLs, owner, file flags)\n";
651 * Note that the word 'bsdtar' will always appear in the first line
654 * In particular, /bin/sh scripts that need to test for the presence
655 * of bsdtar can use the following template:
657 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
658 * echo bsdtar; else echo not bsdtar; fi
661 long_help(struct bsdtar *bsdtar)
666 prog = bsdtar->progname;
670 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
671 printf("%s%s: manipulate archive files\n", prog, p);
673 for (p = long_help_msg; *p != '\0'; p++) {