2004-04-05 21:32:18 +00:00
|
|
|
/*-
|
2008-11-08 04:43:24 +00:00
|
|
|
* Copyright (c) 2003-2008 Tim Kientzle
|
2004-04-05 21:32:18 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:12:17 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-04-05 21:32:18 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bsdtar_platform.h"
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <sys/param.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <sys/stat.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <errno.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <fcntl.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
2004-08-07 03:24:49 +00:00
|
|
|
#ifdef HAVE_LANGINFO_H
|
2004-07-15 03:14:46 +00:00
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_LOCALE_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <locale.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
2004-08-07 03:24:49 +00:00
|
|
|
#ifdef HAVE_PATHS_H
|
|
|
|
#include <paths.h>
|
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <stdio.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <stdlib.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <string.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TIME_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <time.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-04-05 21:32:18 +00:00
|
|
|
#include <unistd.h>
|
2007-03-11 10:36:42 +00:00
|
|
|
#endif
|
2004-08-07 03:24:49 +00:00
|
|
|
#if HAVE_ZLIB_H
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
#include "bsdtar.h"
|
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
/*
|
|
|
|
* Per POSIX.1-1988, tar defaults to reading/writing archives to/from
|
|
|
|
* the default tape device for the system. Pick something reasonable here.
|
|
|
|
*/
|
2005-03-14 00:30:35 +00:00
|
|
|
#ifdef __linux
|
2004-08-07 19:25:34 +00:00
|
|
|
#define _PATH_DEFTAPE "/dev/st0"
|
2004-08-07 17:17:11 +00:00
|
|
|
#endif
|
|
|
|
|
2004-08-07 03:24:49 +00:00
|
|
|
#ifndef _PATH_DEFTAPE
|
2004-08-07 19:25:34 +00:00
|
|
|
#define _PATH_DEFTAPE "/dev/tape"
|
2004-08-07 03:24:49 +00:00
|
|
|
#endif
|
|
|
|
|
2005-03-13 18:36:24 +00:00
|
|
|
/* External function to parse a date/time string (from getdate.y) */
|
|
|
|
time_t get_date(const char *);
|
|
|
|
|
2004-05-17 05:44:53 +00:00
|
|
|
static void long_help(struct bsdtar *);
|
2004-08-08 05:10:10 +00:00
|
|
|
static void only_mode(struct bsdtar *, const char *opt,
|
2004-04-08 19:37:01 +00:00
|
|
|
const char *valid);
|
2004-08-08 05:10:10 +00:00
|
|
|
static void set_mode(struct bsdtar *, char opt);
|
2004-07-26 03:21:41 +00:00
|
|
|
static void version(void);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
/* A basic set of security flags to request from libarchive. */
|
|
|
|
#define SECURITY \
|
|
|
|
(ARCHIVE_EXTRACT_SECURE_SYMLINKS \
|
|
|
|
| ARCHIVE_EXTRACT_SECURE_NODOTDOT)
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct bsdtar *bsdtar, bsdtar_storage;
|
2004-07-17 18:21:00 +00:00
|
|
|
int opt, t;
|
2004-08-03 06:19:08 +00:00
|
|
|
char option_o;
|
2004-06-02 21:01:27 +00:00
|
|
|
char possible_help_request;
|
2004-06-26 22:49:51 +00:00
|
|
|
char buff[16];
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use a pointer for consistency, but stack-allocated storage
|
|
|
|
* for ease of cleanup.
|
|
|
|
*/
|
|
|
|
bsdtar = &bsdtar_storage;
|
|
|
|
memset(bsdtar, 0, sizeof(*bsdtar));
|
|
|
|
bsdtar->fd = -1; /* Mark as "unused" */
|
2004-08-03 06:19:08 +00:00
|
|
|
option_o = 0;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2005-08-02 03:02:55 +00:00
|
|
|
/* Need bsdtar->progname before calling bsdtar_warnc. */
|
|
|
|
if (*argv == NULL)
|
|
|
|
bsdtar->progname = "bsdtar";
|
|
|
|
else {
|
|
|
|
bsdtar->progname = strrchr(*argv, '/');
|
|
|
|
if (bsdtar->progname != NULL)
|
|
|
|
bsdtar->progname++;
|
|
|
|
else
|
|
|
|
bsdtar->progname = *argv;
|
|
|
|
}
|
|
|
|
|
2004-05-17 05:44:53 +00:00
|
|
|
if (setlocale(LC_ALL, "") == NULL)
|
|
|
|
bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
|
2004-08-07 03:24:49 +00:00
|
|
|
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
|
2004-07-15 03:14:46 +00:00
|
|
|
bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
|
|
|
|
#endif
|
2004-06-02 21:01:27 +00:00
|
|
|
possible_help_request = 0;
|
2004-05-17 05:44:53 +00:00
|
|
|
|
2004-07-17 18:21:00 +00:00
|
|
|
/* Look up uid of current user for future reference */
|
2004-04-05 21:32:18 +00:00
|
|
|
bsdtar->user_uid = geteuid();
|
|
|
|
|
|
|
|
/* Default: open tape drive. */
|
|
|
|
bsdtar->filename = getenv("TAPE");
|
|
|
|
if (bsdtar->filename == NULL)
|
|
|
|
bsdtar->filename = _PATH_DEFTAPE;
|
|
|
|
|
|
|
|
/* Default: preserve mod time on extract */
|
|
|
|
bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
|
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
/* Default: Perform basic security checks. */
|
|
|
|
bsdtar->extract_flags |= SECURITY;
|
|
|
|
|
|
|
|
/* Defaults for root user: */
|
|
|
|
if (bsdtar->user_uid == 0) {
|
|
|
|
/* --same-owner */
|
2004-05-19 01:21:11 +00:00
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
|
2007-03-11 10:36:42 +00:00
|
|
|
/* -p */
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
|
|
|
|
}
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
bsdtar->argv = argv;
|
|
|
|
bsdtar->argc = argc;
|
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
/*
|
|
|
|
* Comments following each option indicate where that option
|
|
|
|
* originated: SUSv2, POSIX, GNU tar, star, etc. If there's
|
|
|
|
* no such comment, then I don't know of anyone else who
|
|
|
|
* implements that option.
|
|
|
|
*/
|
2008-11-08 04:43:24 +00:00
|
|
|
while ((opt = bsdtar_getopt(bsdtar)) != -1) {
|
2004-04-05 21:32:18 +00:00
|
|
|
switch (opt) {
|
2004-05-18 17:16:55 +00:00
|
|
|
case 'B': /* GNU tar */
|
2004-06-27 06:29:03 +00:00
|
|
|
/* libarchive doesn't need this; just ignore it. */
|
2004-05-18 17:16:55 +00:00
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'b': /* SUSv2 */
|
2008-11-08 04:43:24 +00:00
|
|
|
t = atoi(bsdtar->optarg);
|
2004-07-17 18:21:00 +00:00
|
|
|
if (t <= 0 || t > 1024)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Argument to -b is out of range (1..1024)");
|
|
|
|
bsdtar->bytes_per_block = 512 * t;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
case 'C': /* GNU tar */
|
2008-11-08 04:43:24 +00:00
|
|
|
set_chdir(bsdtar, bsdtar->optarg);
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'c': /* SUSv2 */
|
2004-08-08 05:10:10 +00:00
|
|
|
set_mode(bsdtar, opt);
|
2004-04-08 19:37:01 +00:00
|
|
|
break;
|
2004-08-03 06:19:08 +00:00
|
|
|
case OPTION_CHECK_LINKS: /* GNU tar */
|
|
|
|
bsdtar->option_warn_links = 1;
|
|
|
|
break;
|
2008-03-15 03:06:46 +00:00
|
|
|
case OPTION_CHROOT: /* NetBSD */
|
|
|
|
bsdtar->option_chroot = 1;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case OPTION_EXCLUDE: /* GNU tar */
|
2008-11-08 04:43:24 +00:00
|
|
|
if (exclude(bsdtar, bsdtar->optarg))
|
2004-07-25 04:15:50 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2008-11-08 04:43:24 +00:00
|
|
|
"Couldn't exclude %s\n", bsdtar->optarg);
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
2007-03-11 10:36:42 +00:00
|
|
|
case OPTION_FORMAT: /* GNU tar, others */
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->create_format = bsdtar->optarg;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
case 'f': /* SUSv2 */
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->filename = bsdtar->optarg;
|
2004-04-05 21:32:18 +00:00
|
|
|
if (strcmp(bsdtar->filename, "-") == 0)
|
|
|
|
bsdtar->filename = NULL;
|
|
|
|
break;
|
|
|
|
case 'H': /* BSD convention */
|
|
|
|
bsdtar->symlink_mode = 'H';
|
|
|
|
break;
|
2004-05-31 20:22:41 +00:00
|
|
|
case 'h': /* Linux Standards Base, gtar; synonym for -L */
|
|
|
|
bsdtar->symlink_mode = 'L';
|
2004-06-02 21:01:27 +00:00
|
|
|
/* Hack: -h by itself is the "help" command. */
|
|
|
|
possible_help_request = 1;
|
2004-04-13 19:45:55 +00:00
|
|
|
break;
|
2007-03-11 10:36:42 +00:00
|
|
|
case OPTION_HELP: /* GNU tar, others */
|
2004-05-17 05:44:53 +00:00
|
|
|
long_help(bsdtar);
|
2004-06-15 00:07:23 +00:00
|
|
|
exit(0);
|
2004-04-08 19:37:01 +00:00
|
|
|
break;
|
2004-08-13 07:23:02 +00:00
|
|
|
case 'I': /* GNU tar */
|
2007-03-11 10:36:42 +00:00
|
|
|
/*
|
|
|
|
* TODO: Allow 'names' to come from an archive,
|
|
|
|
* not just a text file. Design a good UI for
|
|
|
|
* allowing names and mode/owner to be read
|
|
|
|
* from an archive, with contents coming from
|
|
|
|
* disk. This can be used to "refresh" an
|
|
|
|
* archive or to design archives with special
|
|
|
|
* permissions without having to create those
|
|
|
|
* permissions on disk.
|
|
|
|
*/
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->names_from_file = bsdtar->optarg;
|
2004-08-13 07:23:02 +00:00
|
|
|
break;
|
2004-06-07 03:49:48 +00:00
|
|
|
case OPTION_INCLUDE:
|
2007-03-11 10:36:42 +00:00
|
|
|
/*
|
|
|
|
* Noone else has the @archive extension, so
|
|
|
|
* noone else needs this to filter entries
|
|
|
|
* when transforming archives.
|
|
|
|
*/
|
2008-11-08 04:43:24 +00:00
|
|
|
if (include(bsdtar, bsdtar->optarg))
|
2004-07-25 04:15:50 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Failed to add %s to inclusion list",
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->optarg);
|
2004-06-07 03:49:48 +00:00
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'j': /* GNU tar */
|
2004-08-07 03:24:49 +00:00
|
|
|
#if HAVE_LIBBZ2
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->create_compression != '\0')
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2004-04-08 19:37:01 +00:00
|
|
|
"Can't specify both -%c and -%c", opt,
|
|
|
|
bsdtar->create_compression);
|
|
|
|
bsdtar->create_compression = opt;
|
2004-08-07 03:24:49 +00:00
|
|
|
#else
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"bzip2 compression not supported by this version of bsdtar");
|
2004-08-07 03:24:49 +00:00
|
|
|
usage(bsdtar);
|
|
|
|
#endif
|
2004-04-08 19:37:01 +00:00
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'k': /* GNU tar */
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
|
|
|
|
break;
|
2008-05-17 15:55:29 +00:00
|
|
|
case OPTION_KEEP_NEWER_FILES: /* GNU tar */
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'L': /* BSD convention */
|
|
|
|
bsdtar->symlink_mode = 'L';
|
|
|
|
break;
|
2008-01-22 07:23:44 +00:00
|
|
|
case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
|
|
|
|
/* GNU tar 1.13 used -l for --one-file-system */
|
|
|
|
bsdtar->option_warn_links = 1;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
case 'm': /* SUSv2 */
|
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
|
|
|
|
break;
|
|
|
|
case 'n': /* GNU tar */
|
|
|
|
bsdtar->option_no_subdirs = 1;
|
|
|
|
break;
|
2007-03-11 10:36:42 +00:00
|
|
|
/*
|
|
|
|
* Selecting files by time:
|
|
|
|
* --newer-?time='date' Only files newer than 'date'
|
|
|
|
* --newer-?time-than='file' Only files newer than time
|
|
|
|
* on specified file (useful for incremental backups)
|
|
|
|
* TODO: Add corresponding "older" options to reverse these.
|
|
|
|
*/
|
2005-03-13 18:36:24 +00:00
|
|
|
case OPTION_NEWER_CTIME: /* GNU tar */
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->newer_ctime_sec = get_date(bsdtar->optarg);
|
2005-03-13 18:36:24 +00:00
|
|
|
break;
|
|
|
|
case OPTION_NEWER_CTIME_THAN:
|
|
|
|
{
|
|
|
|
struct stat st;
|
2008-11-08 04:43:24 +00:00
|
|
|
if (stat(bsdtar->optarg, &st) != 0)
|
2005-03-13 18:36:24 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2008-11-08 04:43:24 +00:00
|
|
|
"Can't open file %s", bsdtar->optarg);
|
2005-03-13 18:36:24 +00:00
|
|
|
bsdtar->newer_ctime_sec = st.st_ctime;
|
|
|
|
bsdtar->newer_ctime_nsec =
|
|
|
|
ARCHIVE_STAT_CTIME_NANOS(&st);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPTION_NEWER_MTIME: /* GNU tar */
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->newer_mtime_sec = get_date(bsdtar->optarg);
|
2005-03-13 18:36:24 +00:00
|
|
|
break;
|
|
|
|
case OPTION_NEWER_MTIME_THAN:
|
|
|
|
{
|
|
|
|
struct stat st;
|
2008-11-08 04:43:24 +00:00
|
|
|
if (stat(bsdtar->optarg, &st) != 0)
|
2005-03-13 18:36:24 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2008-11-08 04:43:24 +00:00
|
|
|
"Can't open file %s", bsdtar->optarg);
|
2005-03-13 18:36:24 +00:00
|
|
|
bsdtar->newer_mtime_sec = st.st_mtime;
|
|
|
|
bsdtar->newer_mtime_nsec =
|
|
|
|
ARCHIVE_STAT_MTIME_NANOS(&st);
|
|
|
|
}
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case OPTION_NODUMP: /* star */
|
|
|
|
bsdtar->option_honor_nodump = 1;
|
|
|
|
break;
|
2007-03-11 10:36:42 +00:00
|
|
|
case OPTION_NO_SAME_OWNER: /* GNU tar */
|
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
|
|
|
|
break;
|
2004-06-15 06:44:41 +00:00
|
|
|
case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
|
2007-03-11 10:36:42 +00:00
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
|
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
|
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
|
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
|
2004-06-15 06:44:41 +00:00
|
|
|
break;
|
2004-06-27 06:29:03 +00:00
|
|
|
case OPTION_NULL: /* GNU tar */
|
|
|
|
bsdtar->option_null++;
|
|
|
|
break;
|
2008-05-26 17:10:10 +00:00
|
|
|
case OPTION_NUMERIC_OWNER: /* GNU tar */
|
|
|
|
bsdtar->option_numeric_owner++;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'O': /* GNU tar */
|
|
|
|
bsdtar->option_stdout = 1;
|
|
|
|
break;
|
2007-03-11 10:36:42 +00:00
|
|
|
case 'o': /* SUSv2 and GNU conflict here, but not fatally */
|
2004-08-03 06:19:08 +00:00
|
|
|
option_o = 1; /* Record it and resolve it later. */
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
2007-03-11 10:36:42 +00:00
|
|
|
case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
|
2004-06-15 00:28:34 +00:00
|
|
|
bsdtar->option_dont_traverse_mounts = 1;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* The common BSD -P option is not necessary, since
|
|
|
|
* our default is to archive symlinks, not follow
|
|
|
|
* them. This is convenient, as -P conflicts with GNU
|
|
|
|
* tar anyway.
|
|
|
|
*/
|
|
|
|
case 'P': /* BSD convention */
|
|
|
|
/* Default behavior, no option necessary. */
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'P': /* GNU tar */
|
2007-03-11 10:36:42 +00:00
|
|
|
bsdtar->extract_flags &= ~SECURITY;
|
2004-04-05 21:32:18 +00:00
|
|
|
bsdtar->option_absolute_paths = 1;
|
|
|
|
break;
|
|
|
|
case 'p': /* GNU tar, star */
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
|
2004-06-27 23:29:44 +00:00
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
|
2006-03-21 17:03:51 +00:00
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
|
2004-06-27 23:29:44 +00:00
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
2007-09-09 00:07:18 +00:00
|
|
|
case OPTION_POSIX: /* GNU tar */
|
|
|
|
bsdtar->create_format = "pax";
|
|
|
|
break;
|
2008-03-15 03:18:24 +00:00
|
|
|
case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
|
|
|
|
bsdtar->option_fast_read = 1;
|
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'r': /* SUSv2 */
|
2004-08-08 05:10:10 +00:00
|
|
|
set_mode(bsdtar, opt);
|
2004-04-08 19:37:01 +00:00
|
|
|
break;
|
2008-05-26 17:10:10 +00:00
|
|
|
case 'S': /* NetBSD pax-as-tar */
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
|
|
|
|
break;
|
|
|
|
case 's': /* NetBSD pax-as-tar */
|
|
|
|
#if HAVE_REGEX_H
|
2008-11-08 04:43:24 +00:00
|
|
|
add_substitution(bsdtar, bsdtar->optarg);
|
2008-05-26 17:10:10 +00:00
|
|
|
#else
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"-s is not supported by this version of bsdtar");
|
2008-05-26 17:10:10 +00:00
|
|
|
usage(bsdtar);
|
|
|
|
#endif
|
|
|
|
break;
|
2005-03-13 18:36:24 +00:00
|
|
|
case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->strip_components = atoi(bsdtar->optarg);
|
2005-03-13 04:12:30 +00:00
|
|
|
break;
|
2004-06-27 06:29:03 +00:00
|
|
|
case 'T': /* GNU tar */
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->names_from_file = bsdtar->optarg;
|
2004-06-27 06:29:03 +00:00
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 't': /* SUSv2 */
|
2004-08-08 05:10:10 +00:00
|
|
|
set_mode(bsdtar, opt);
|
2004-04-08 19:37:01 +00:00
|
|
|
bsdtar->verbose++;
|
|
|
|
break;
|
2004-08-07 19:25:34 +00:00
|
|
|
case OPTION_TOTALS: /* GNU tar */
|
|
|
|
bsdtar->option_totals++;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'U': /* GNU tar */
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
|
2004-05-17 03:33:06 +00:00
|
|
|
bsdtar->option_unlink_first = 1;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'u': /* SUSv2 */
|
2004-08-08 05:10:10 +00:00
|
|
|
set_mode(bsdtar, opt);
|
2004-04-08 19:37:01 +00:00
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'v': /* SUSv2 */
|
|
|
|
bsdtar->verbose++;
|
|
|
|
break;
|
2007-03-11 10:36:42 +00:00
|
|
|
case OPTION_VERSION: /* GNU convention */
|
2004-07-26 03:21:41 +00:00
|
|
|
version();
|
|
|
|
break;
|
2007-03-11 10:36:42 +00:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* The -W longopt feature is handled inside of
|
2008-11-08 04:43:24 +00:00
|
|
|
* bsdtar_getopt(), so -W is not available here.
|
2007-03-11 10:36:42 +00:00
|
|
|
*/
|
2008-11-08 04:43:24 +00:00
|
|
|
case 'W': /* Obscure GNU convention. */
|
2007-03-11 10:36:42 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'w': /* SUSv2 */
|
|
|
|
bsdtar->option_interactive = 1;
|
|
|
|
break;
|
2004-06-15 05:55:41 +00:00
|
|
|
case 'X': /* GNU tar */
|
2008-11-08 04:43:24 +00:00
|
|
|
if (exclude_from_file(bsdtar, bsdtar->optarg))
|
2004-07-25 04:15:50 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"failed to process exclusions from file %s",
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->optarg);
|
2004-06-15 05:55:41 +00:00
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'x': /* SUSv2 */
|
2004-08-08 05:10:10 +00:00
|
|
|
set_mode(bsdtar, opt);
|
2004-04-08 19:37:01 +00:00
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'y': /* FreeBSD version of GNU tar */
|
2004-08-07 03:24:49 +00:00
|
|
|
#if HAVE_LIBBZ2
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->create_compression != '\0')
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2004-04-08 19:37:01 +00:00
|
|
|
"Can't specify both -%c and -%c", opt,
|
|
|
|
bsdtar->create_compression);
|
2004-04-05 21:32:18 +00:00
|
|
|
bsdtar->create_compression = opt;
|
2004-08-07 03:24:49 +00:00
|
|
|
#else
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"bzip2 compression not supported by this version of bsdtar");
|
2004-08-07 03:24:49 +00:00
|
|
|
usage(bsdtar);
|
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
|
|
|
case 'Z': /* GNU tar */
|
2004-05-30 00:09:43 +00:00
|
|
|
if (bsdtar->create_compression != '\0')
|
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Can't specify both -%c and -%c", opt,
|
|
|
|
bsdtar->create_compression);
|
|
|
|
bsdtar->create_compression = opt;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
2004-06-27 06:29:03 +00:00
|
|
|
case 'z': /* GNU tar, star, many others */
|
2004-08-07 03:24:49 +00:00
|
|
|
#if HAVE_LIBZ
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->create_compression != '\0')
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2004-04-08 19:37:01 +00:00
|
|
|
"Can't specify both -%c and -%c", opt,
|
|
|
|
bsdtar->create_compression);
|
|
|
|
bsdtar->create_compression = opt;
|
2004-08-07 03:24:49 +00:00
|
|
|
#else
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"gzip compression not supported by this version of bsdtar");
|
2004-08-07 03:24:49 +00:00
|
|
|
usage(bsdtar);
|
|
|
|
#endif
|
2004-04-08 19:37:01 +00:00
|
|
|
break;
|
2007-05-29 05:39:10 +00:00
|
|
|
case OPTION_USE_COMPRESS_PROGRAM:
|
2008-11-08 04:43:24 +00:00
|
|
|
bsdtar->compress_program = bsdtar->optarg;
|
2007-05-29 05:39:10 +00:00
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
default:
|
2004-05-17 05:44:53 +00:00
|
|
|
usage(bsdtar);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-08 19:37:01 +00:00
|
|
|
/*
|
|
|
|
* Sanity-check options.
|
|
|
|
*/
|
2007-03-11 10:36:42 +00:00
|
|
|
|
|
|
|
/* If no "real" mode was specified, treat -h as --help. */
|
2004-08-08 05:10:10 +00:00
|
|
|
if ((bsdtar->mode == '\0') && possible_help_request) {
|
2004-06-02 21:01:27 +00:00
|
|
|
long_help(bsdtar);
|
2004-06-15 00:07:23 +00:00
|
|
|
exit(0);
|
2004-06-02 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
2007-03-11 10:36:42 +00:00
|
|
|
/* Otherwise, a mode is required. */
|
2004-08-08 05:10:10 +00:00
|
|
|
if (bsdtar->mode == '\0')
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Must specify one of -c, -r, -t, -u, -x");
|
2004-04-08 19:37:01 +00:00
|
|
|
|
|
|
|
/* Check boolean options only permitted in certain modes. */
|
|
|
|
if (bsdtar->option_dont_traverse_mounts)
|
2006-12-23 07:01:59 +00:00
|
|
|
only_mode(bsdtar, "--one-file-system", "cru");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_fast_read)
|
2004-08-08 05:10:10 +00:00
|
|
|
only_mode(bsdtar, "--fast-read", "xt");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_honor_nodump)
|
2004-08-08 05:10:10 +00:00
|
|
|
only_mode(bsdtar, "--nodump", "cru");
|
2004-08-03 06:19:08 +00:00
|
|
|
if (option_o > 0) {
|
2004-08-08 05:10:10 +00:00
|
|
|
switch (bsdtar->mode) {
|
2004-08-03 06:19:08 +00:00
|
|
|
case 'c':
|
|
|
|
/*
|
|
|
|
* In GNU tar, -o means "old format." The
|
|
|
|
* "ustar" format is the closest thing
|
|
|
|
* supported by libarchive.
|
|
|
|
*/
|
|
|
|
bsdtar->create_format = "ustar";
|
|
|
|
/* TODO: bsdtar->create_format = "v7"; */
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
/* POSIX-compatible behavior. */
|
|
|
|
bsdtar->option_no_owner = 1;
|
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
|
|
|
|
break;
|
|
|
|
default:
|
2004-08-08 05:10:10 +00:00
|
|
|
only_mode(bsdtar, "-o", "xc");
|
2004-08-03 06:19:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-07-17 04:17:50 +00:00
|
|
|
}
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_no_subdirs)
|
2004-08-08 05:10:10 +00:00
|
|
|
only_mode(bsdtar, "-n", "cru");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_stdout)
|
2004-08-27 04:13:15 +00:00
|
|
|
only_mode(bsdtar, "-O", "xt");
|
2006-12-23 07:06:37 +00:00
|
|
|
if (bsdtar->option_unlink_first)
|
2006-12-23 08:16:54 +00:00
|
|
|
only_mode(bsdtar, "-U", "x");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_warn_links)
|
2004-08-08 05:10:10 +00:00
|
|
|
only_mode(bsdtar, "--check-links", "cr");
|
2004-04-08 19:37:01 +00:00
|
|
|
|
2008-03-15 05:08:21 +00:00
|
|
|
/* Check other parameters only permitted in certain modes. */
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->create_compression != '\0') {
|
|
|
|
strcpy(buff, "-?");
|
|
|
|
buff[1] = bsdtar->create_compression;
|
2004-08-08 05:10:10 +00:00
|
|
|
only_mode(bsdtar, buff, "cxt");
|
2004-04-08 19:37:01 +00:00
|
|
|
}
|
|
|
|
if (bsdtar->create_format != NULL)
|
2008-05-02 05:40:05 +00:00
|
|
|
only_mode(bsdtar, "--format", "cru");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->symlink_mode != '\0') {
|
2004-06-27 06:29:03 +00:00
|
|
|
strcpy(buff, "-?");
|
2004-04-08 19:37:01 +00:00
|
|
|
buff[1] = bsdtar->symlink_mode;
|
2004-08-08 05:10:10 +00:00
|
|
|
only_mode(bsdtar, buff, "cru");
|
2004-04-08 19:37:01 +00:00
|
|
|
}
|
2006-12-23 07:06:37 +00:00
|
|
|
if (bsdtar->strip_components != 0)
|
|
|
|
only_mode(bsdtar, "--strip-components", "xt");
|
2004-04-08 19:37:01 +00:00
|
|
|
|
2004-08-08 05:10:10 +00:00
|
|
|
switch(bsdtar->mode) {
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'c':
|
|
|
|
tar_mode_c(bsdtar);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
tar_mode_r(bsdtar);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
tar_mode_t(bsdtar);
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
tar_mode_u(bsdtar);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
tar_mode_x(bsdtar);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-04-15 22:37:54 +00:00
|
|
|
cleanup_exclusions(bsdtar);
|
2008-05-26 17:10:10 +00:00
|
|
|
#if HAVE_REGEX_H
|
|
|
|
cleanup_substitution(bsdtar);
|
|
|
|
#endif
|
|
|
|
|
2007-03-24 03:25:49 +00:00
|
|
|
if (bsdtar->return_value != 0)
|
|
|
|
bsdtar_warnc(bsdtar, 0,
|
|
|
|
"Error exit delayed from previous errors.");
|
2004-05-27 04:30:59 +00:00
|
|
|
return (bsdtar->return_value);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
2004-08-08 05:10:10 +00:00
|
|
|
static void
|
|
|
|
set_mode(struct bsdtar *bsdtar, char opt)
|
|
|
|
{
|
|
|
|
if (bsdtar->mode != '\0' && bsdtar->mode != opt)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Can't specify both -%c and -%c", opt, bsdtar->mode);
|
|
|
|
bsdtar->mode = opt;
|
|
|
|
}
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
/*
|
|
|
|
* Verify that the mode is correct.
|
|
|
|
*/
|
|
|
|
static void
|
2004-08-08 05:10:10 +00:00
|
|
|
only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
2004-08-08 05:10:10 +00:00
|
|
|
if (strchr(valid_modes, bsdtar->mode) == NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Option %s is not permitted in mode -%c",
|
2004-08-08 05:10:10 +00:00
|
|
|
opt, bsdtar->mode);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2004-05-17 05:44:53 +00:00
|
|
|
usage(struct bsdtar *bsdtar)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
2004-05-17 05:44:53 +00:00
|
|
|
p = bsdtar->progname;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-06-15 00:07:23 +00:00
|
|
|
fprintf(stderr, "Usage:\n");
|
|
|
|
fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
|
|
|
|
fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
|
|
|
|
fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
|
|
|
|
fprintf(stderr, " Help: %s --help\n", p);
|
2004-04-05 21:32:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-07-26 03:21:41 +00:00
|
|
|
static void
|
|
|
|
version(void)
|
|
|
|
{
|
2008-01-02 00:27:14 +00:00
|
|
|
printf("bsdtar %s - %s\n",
|
|
|
|
BSDTAR_VERSION_STRING,
|
|
|
|
archive_version());
|
2008-05-02 05:18:47 +00:00
|
|
|
exit(0);
|
2004-07-26 03:21:41 +00:00
|
|
|
}
|
|
|
|
|
2004-07-24 22:13:44 +00:00
|
|
|
static const char *long_help_msg =
|
|
|
|
"First option must be a mode specifier:\n"
|
|
|
|
" -c Create -r Add/Replace -t List -u Update -x Extract\n"
|
|
|
|
"Common Options:\n"
|
|
|
|
" -b # Use # 512-byte records per I/O block\n"
|
|
|
|
" -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
|
|
|
|
" -v Verbose\n"
|
|
|
|
" -w Interactive\n"
|
2004-08-08 05:10:10 +00:00
|
|
|
"Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
|
2004-07-24 22:13:44 +00:00
|
|
|
" <file>, <dir> add these items to archive\n"
|
|
|
|
" -z, -j Compress archive with gzip/bzip2\n"
|
2004-08-08 17:20:23 +00:00
|
|
|
" --format {ustar|pax|cpio|shar} Select archive format\n"
|
2004-07-24 22:13:44 +00:00
|
|
|
" --exclude <pattern> Skip files that match pattern\n"
|
2004-08-08 05:10:10 +00:00
|
|
|
" -C <dir> Change to <dir> before processing remaining files\n"
|
2004-07-24 22:13:44 +00:00
|
|
|
" @<archive> Add entries from <archive> to output\n"
|
|
|
|
"List: %p -t [options] [<patterns>]\n"
|
|
|
|
" <patterns> If specified, list only entries that match\n"
|
|
|
|
"Extract: %p -x [options] [<patterns>]\n"
|
|
|
|
" <patterns> If specified, extract only entries that match\n"
|
|
|
|
" -k Keep (don't overwrite) existing files\n"
|
|
|
|
" -m Don't restore modification times\n"
|
|
|
|
" -O Write entries to stdout, don't restore to disk\n"
|
|
|
|
" -p Restore permissions (including ACLs, owner, file flags)\n";
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
|
2004-06-15 00:07:23 +00:00
|
|
|
/*
|
|
|
|
* Note that the word 'bsdtar' will always appear in the first line
|
|
|
|
* of output.
|
|
|
|
*
|
|
|
|
* In particular, /bin/sh scripts that need to test for the presence
|
|
|
|
* of bsdtar can use the following template:
|
|
|
|
*
|
|
|
|
* if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
|
|
|
|
* echo bsdtar; else echo not bsdtar; fi
|
|
|
|
*/
|
2004-04-05 21:32:18 +00:00
|
|
|
static void
|
2004-05-17 05:44:53 +00:00
|
|
|
long_help(struct bsdtar *bsdtar)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
const char *prog;
|
|
|
|
const char *p;
|
|
|
|
|
2004-05-17 05:44:53 +00:00
|
|
|
prog = bsdtar->progname;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-06-15 00:07:23 +00:00
|
|
|
fflush(stderr);
|
|
|
|
|
2004-07-26 03:21:41 +00:00
|
|
|
p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
|
|
|
|
printf("%s%s: manipulate archive files\n", prog, p);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-07-24 22:13:44 +00:00
|
|
|
for (p = long_help_msg; *p != '\0'; p++) {
|
|
|
|
if (*p == '%') {
|
|
|
|
if (p[1] == 'p') {
|
|
|
|
fputs(prog, stdout);
|
|
|
|
p++;
|
2004-04-05 21:32:18 +00:00
|
|
|
} else
|
2004-07-24 22:13:44 +00:00
|
|
|
putchar('%');
|
|
|
|
} else
|
|
|
|
putchar(*p);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
2006-03-07 06:05:38 +00:00
|
|
|
version();
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|