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"
78 #define _PATH_DEFTAPE "/dev/tape"
81 /* External function to parse a date/time string (from getdate.y) */
82 time_t get_date(const char *);
84 static void long_help(struct bsdtar *);
85 static void only_mode(struct bsdtar *, const char *opt,
87 static void set_mode(struct bsdtar *, char opt);
88 static void version(void);
90 /* A basic set of security flags to request from libarchive. */
92 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
93 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
96 main(int argc, char **argv)
98 struct bsdtar *bsdtar, bsdtar_storage;
101 char possible_help_request;
105 * Use a pointer for consistency, but stack-allocated storage
106 * for ease of cleanup.
108 bsdtar = &bsdtar_storage;
109 memset(bsdtar, 0, sizeof(*bsdtar));
110 bsdtar->fd = -1; /* Mark as "unused" */
113 /* Need bsdtar->progname before calling bsdtar_warnc. */
115 bsdtar->progname = "bsdtar";
117 bsdtar->progname = strrchr(*argv, '/');
118 if (bsdtar->progname != NULL)
121 bsdtar->progname = *argv;
124 if (setlocale(LC_ALL, "") == NULL)
125 bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
126 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
127 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
129 possible_help_request = 0;
131 /* Look up uid of current user for future reference */
132 bsdtar->user_uid = geteuid();
134 /* Default: open tape drive. */
135 bsdtar->filename = getenv("TAPE");
136 if (bsdtar->filename == NULL)
137 bsdtar->filename = _PATH_DEFTAPE;
139 /* Default: preserve mod time on extract */
140 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
142 /* Default: Perform basic security checks. */
143 bsdtar->extract_flags |= SECURITY;
145 /* Defaults for root user: */
146 if (bsdtar->user_uid == 0) {
148 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
150 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
151 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
152 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
153 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
160 * Comments following each option indicate where that option
161 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
162 * no such comment, then I don't know of anyone else who
163 * implements that option.
165 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
167 case 'B': /* GNU tar */
168 /* libarchive doesn't need this; just ignore it. */
170 case 'b': /* SUSv2 */
171 t = atoi(bsdtar->optarg);
172 if (t <= 0 || t > 1024)
173 bsdtar_errc(bsdtar, 1, 0,
174 "Argument to -b is out of range (1..1024)");
175 bsdtar->bytes_per_block = 512 * t;
177 case 'C': /* GNU tar */
178 set_chdir(bsdtar, bsdtar->optarg);
180 case 'c': /* SUSv2 */
181 set_mode(bsdtar, opt);
183 case OPTION_CHECK_LINKS: /* GNU tar */
184 bsdtar->option_warn_links = 1;
186 case OPTION_CHROOT: /* NetBSD */
187 bsdtar->option_chroot = 1;
189 case OPTION_EXCLUDE: /* GNU tar */
190 if (exclude(bsdtar, bsdtar->optarg))
191 bsdtar_errc(bsdtar, 1, 0,
192 "Couldn't exclude %s\n", bsdtar->optarg);
194 case OPTION_FORMAT: /* GNU tar, others */
195 bsdtar->create_format = bsdtar->optarg;
197 case 'f': /* SUSv2 */
198 bsdtar->filename = bsdtar->optarg;
199 if (strcmp(bsdtar->filename, "-") == 0)
200 bsdtar->filename = NULL;
202 case 'H': /* BSD convention */
203 bsdtar->symlink_mode = 'H';
205 case 'h': /* Linux Standards Base, gtar; synonym for -L */
206 bsdtar->symlink_mode = 'L';
207 /* Hack: -h by itself is the "help" command. */
208 possible_help_request = 1;
210 case OPTION_HELP: /* GNU tar, others */
214 case 'I': /* GNU tar */
216 * TODO: Allow 'names' to come from an archive,
217 * not just a text file. Design a good UI for
218 * allowing names and mode/owner to be read
219 * from an archive, with contents coming from
220 * disk. This can be used to "refresh" an
221 * archive or to design archives with special
222 * permissions without having to create those
223 * permissions on disk.
225 bsdtar->names_from_file = bsdtar->optarg;
229 * Noone else has the @archive extension, so
230 * noone else needs this to filter entries
231 * when transforming archives.
233 if (include(bsdtar, bsdtar->optarg))
234 bsdtar_errc(bsdtar, 1, 0,
235 "Failed to add %s to inclusion list",
238 case 'j': /* GNU tar */
240 if (bsdtar->create_compression != '\0')
241 bsdtar_errc(bsdtar, 1, 0,
242 "Can't specify both -%c and -%c", opt,
243 bsdtar->create_compression);
244 bsdtar->create_compression = opt;
246 bsdtar_warnc(bsdtar, 0,
247 "bzip2 compression not supported by this version of bsdtar");
251 case 'k': /* GNU tar */
252 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
254 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
255 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
257 case 'L': /* BSD convention */
258 bsdtar->symlink_mode = 'L';
260 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
261 /* GNU tar 1.13 used -l for --one-file-system */
262 bsdtar->option_warn_links = 1;
264 case 'm': /* SUSv2 */
265 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
267 case 'n': /* GNU tar */
268 bsdtar->option_no_subdirs = 1;
271 * Selecting files by time:
272 * --newer-?time='date' Only files newer than 'date'
273 * --newer-?time-than='file' Only files newer than time
274 * on specified file (useful for incremental backups)
275 * TODO: Add corresponding "older" options to reverse these.
277 case OPTION_NEWER_CTIME: /* GNU tar */
278 bsdtar->newer_ctime_sec = get_date(bsdtar->optarg);
280 case OPTION_NEWER_CTIME_THAN:
283 if (stat(bsdtar->optarg, &st) != 0)
284 bsdtar_errc(bsdtar, 1, 0,
285 "Can't open file %s", bsdtar->optarg);
286 bsdtar->newer_ctime_sec = st.st_ctime;
287 bsdtar->newer_ctime_nsec =
288 ARCHIVE_STAT_CTIME_NANOS(&st);
291 case OPTION_NEWER_MTIME: /* GNU tar */
292 bsdtar->newer_mtime_sec = get_date(bsdtar->optarg);
294 case OPTION_NEWER_MTIME_THAN:
297 if (stat(bsdtar->optarg, &st) != 0)
298 bsdtar_errc(bsdtar, 1, 0,
299 "Can't open file %s", bsdtar->optarg);
300 bsdtar->newer_mtime_sec = st.st_mtime;
301 bsdtar->newer_mtime_nsec =
302 ARCHIVE_STAT_MTIME_NANOS(&st);
305 case OPTION_NODUMP: /* star */
306 bsdtar->option_honor_nodump = 1;
308 case OPTION_NO_SAME_OWNER: /* GNU tar */
309 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
311 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
312 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
313 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
314 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
315 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
317 case OPTION_NULL: /* GNU tar */
318 bsdtar->option_null++;
320 case OPTION_NUMERIC_OWNER: /* GNU tar */
321 bsdtar->option_numeric_owner++;
323 case 'O': /* GNU tar */
324 bsdtar->option_stdout = 1;
326 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
327 option_o = 1; /* Record it and resolve it later. */
329 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
330 bsdtar->option_dont_traverse_mounts = 1;
334 * The common BSD -P option is not necessary, since
335 * our default is to archive symlinks, not follow
336 * them. This is convenient, as -P conflicts with GNU
339 case 'P': /* BSD convention */
340 /* Default behavior, no option necessary. */
343 case 'P': /* GNU tar */
344 bsdtar->extract_flags &= ~SECURITY;
345 bsdtar->option_absolute_paths = 1;
347 case 'p': /* GNU tar, star */
348 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
349 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
350 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
351 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
353 case OPTION_POSIX: /* GNU tar */
354 bsdtar->create_format = "pax";
356 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
357 bsdtar->option_fast_read = 1;
359 case 'r': /* SUSv2 */
360 set_mode(bsdtar, opt);
362 case 'S': /* NetBSD pax-as-tar */
363 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
365 case 's': /* NetBSD pax-as-tar */
367 add_substitution(bsdtar, bsdtar->optarg);
369 bsdtar_warnc(bsdtar, 0,
370 "-s is not supported by this version of bsdtar");
374 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
375 bsdtar->strip_components = atoi(bsdtar->optarg);
377 case 'T': /* GNU tar */
378 bsdtar->names_from_file = bsdtar->optarg;
380 case 't': /* SUSv2 */
381 set_mode(bsdtar, opt);
384 case OPTION_TOTALS: /* GNU tar */
385 bsdtar->option_totals++;
387 case 'U': /* GNU tar */
388 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
389 bsdtar->option_unlink_first = 1;
391 case 'u': /* SUSv2 */
392 set_mode(bsdtar, opt);
394 case 'v': /* SUSv2 */
397 case OPTION_VERSION: /* GNU convention */
402 * The -W longopt feature is handled inside of
403 * bsdtar_getopt(), so -W is not available here.
405 case 'W': /* Obscure GNU convention. */
408 case 'w': /* SUSv2 */
409 bsdtar->option_interactive = 1;
411 case 'X': /* GNU tar */
412 if (exclude_from_file(bsdtar, bsdtar->optarg))
413 bsdtar_errc(bsdtar, 1, 0,
414 "failed to process exclusions from file %s",
417 case 'x': /* SUSv2 */
418 set_mode(bsdtar, opt);
420 case 'y': /* FreeBSD version of GNU tar */
422 if (bsdtar->create_compression != '\0')
423 bsdtar_errc(bsdtar, 1, 0,
424 "Can't specify both -%c and -%c", opt,
425 bsdtar->create_compression);
426 bsdtar->create_compression = opt;
428 bsdtar_warnc(bsdtar, 0,
429 "bzip2 compression not supported by this version of bsdtar");
433 case 'Z': /* GNU tar */
434 if (bsdtar->create_compression != '\0')
435 bsdtar_errc(bsdtar, 1, 0,
436 "Can't specify both -%c and -%c", opt,
437 bsdtar->create_compression);
438 bsdtar->create_compression = opt;
440 case 'z': /* GNU tar, star, many others */
442 if (bsdtar->create_compression != '\0')
443 bsdtar_errc(bsdtar, 1, 0,
444 "Can't specify both -%c and -%c", opt,
445 bsdtar->create_compression);
446 bsdtar->create_compression = opt;
448 bsdtar_warnc(bsdtar, 0,
449 "gzip compression not supported by this version of bsdtar");
453 case OPTION_USE_COMPRESS_PROGRAM:
454 bsdtar->compress_program = bsdtar->optarg;
462 * Sanity-check options.
465 /* If no "real" mode was specified, treat -h as --help. */
466 if ((bsdtar->mode == '\0') && possible_help_request) {
471 /* Otherwise, a mode is required. */
472 if (bsdtar->mode == '\0')
473 bsdtar_errc(bsdtar, 1, 0,
474 "Must specify one of -c, -r, -t, -u, -x");
476 /* Check boolean options only permitted in certain modes. */
477 if (bsdtar->option_dont_traverse_mounts)
478 only_mode(bsdtar, "--one-file-system", "cru");
479 if (bsdtar->option_fast_read)
480 only_mode(bsdtar, "--fast-read", "xt");
481 if (bsdtar->option_honor_nodump)
482 only_mode(bsdtar, "--nodump", "cru");
484 switch (bsdtar->mode) {
487 * In GNU tar, -o means "old format." The
488 * "ustar" format is the closest thing
489 * supported by libarchive.
491 bsdtar->create_format = "ustar";
492 /* TODO: bsdtar->create_format = "v7"; */
495 /* POSIX-compatible behavior. */
496 bsdtar->option_no_owner = 1;
497 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
500 only_mode(bsdtar, "-o", "xc");
504 if (bsdtar->option_no_subdirs)
505 only_mode(bsdtar, "-n", "cru");
506 if (bsdtar->option_stdout)
507 only_mode(bsdtar, "-O", "xt");
508 if (bsdtar->option_unlink_first)
509 only_mode(bsdtar, "-U", "x");
510 if (bsdtar->option_warn_links)
511 only_mode(bsdtar, "--check-links", "cr");
513 /* Check other parameters only permitted in certain modes. */
514 if (bsdtar->create_compression != '\0') {
516 buff[1] = bsdtar->create_compression;
517 only_mode(bsdtar, buff, "cxt");
519 if (bsdtar->create_format != NULL)
520 only_mode(bsdtar, "--format", "cru");
521 if (bsdtar->symlink_mode != '\0') {
523 buff[1] = bsdtar->symlink_mode;
524 only_mode(bsdtar, buff, "cru");
526 if (bsdtar->strip_components != 0)
527 only_mode(bsdtar, "--strip-components", "xt");
529 switch(bsdtar->mode) {
547 cleanup_exclusions(bsdtar);
549 cleanup_substitution(bsdtar);
552 if (bsdtar->return_value != 0)
553 bsdtar_warnc(bsdtar, 0,
554 "Error exit delayed from previous errors.");
555 return (bsdtar->return_value);
559 set_mode(struct bsdtar *bsdtar, char opt)
561 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
562 bsdtar_errc(bsdtar, 1, 0,
563 "Can't specify both -%c and -%c", opt, bsdtar->mode);
568 * Verify that the mode is correct.
571 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
573 if (strchr(valid_modes, bsdtar->mode) == NULL)
574 bsdtar_errc(bsdtar, 1, 0,
575 "Option %s is not permitted in mode -%c",
581 usage(struct bsdtar *bsdtar)
585 p = bsdtar->progname;
587 fprintf(stderr, "Usage:\n");
588 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
589 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
590 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
591 fprintf(stderr, " Help: %s --help\n", p);
598 printf("bsdtar %s - %s\n",
599 BSDTAR_VERSION_STRING,
604 static const char *long_help_msg =
605 "First option must be a mode specifier:\n"
606 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
608 " -b # Use # 512-byte records per I/O block\n"
609 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
612 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
613 " <file>, <dir> add these items to archive\n"
614 " -z, -j Compress archive with gzip/bzip2\n"
615 " --format {ustar|pax|cpio|shar} Select archive format\n"
616 " --exclude <pattern> Skip files that match pattern\n"
617 " -C <dir> Change to <dir> before processing remaining files\n"
618 " @<archive> Add entries from <archive> to output\n"
619 "List: %p -t [options] [<patterns>]\n"
620 " <patterns> If specified, list only entries that match\n"
621 "Extract: %p -x [options] [<patterns>]\n"
622 " <patterns> If specified, extract only entries that match\n"
623 " -k Keep (don't overwrite) existing files\n"
624 " -m Don't restore modification times\n"
625 " -O Write entries to stdout, don't restore to disk\n"
626 " -p Restore permissions (including ACLs, owner, file flags)\n";
630 * Note that the word 'bsdtar' will always appear in the first line
633 * In particular, /bin/sh scripts that need to test for the presence
634 * of bsdtar can use the following template:
636 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
637 * echo bsdtar; else echo not bsdtar; fi
640 long_help(struct bsdtar *bsdtar)
645 prog = bsdtar->progname;
649 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
650 printf("%s%s: manipulate archive files\n", prog, p);
652 for (p = long_help_msg; *p != '\0'; p++) {