2004-04-05 21:32:18 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003-2004 Tim Kientzle
|
|
|
|
* 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
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 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$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <archive.h>
|
|
|
|
#include <archive_entry.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#ifdef HAVE_GETOPT_LONG
|
|
|
|
#include <getopt.h>
|
2004-06-26 22:49:51 +00:00
|
|
|
#else
|
|
|
|
struct option {
|
|
|
|
const char *name;
|
|
|
|
int has_arg;
|
|
|
|
int *flag;
|
|
|
|
int val;
|
|
|
|
};
|
|
|
|
#define no_argument 0
|
|
|
|
#define required_argument 1
|
2004-04-05 21:32:18 +00:00
|
|
|
#endif
|
|
|
|
#include <locale.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "bsdtar.h"
|
|
|
|
|
2004-06-26 22:49:51 +00:00
|
|
|
static int bsdtar_getopt(struct bsdtar *, const char *optstring,
|
|
|
|
const struct option **poption);
|
2004-05-17 05:44:53 +00:00
|
|
|
static void long_help(struct bsdtar *);
|
|
|
|
static void only_mode(struct bsdtar *, char mode, const char *opt,
|
2004-04-08 19:37:01 +00:00
|
|
|
const char *valid);
|
2004-05-17 05:44:53 +00:00
|
|
|
static char ** rewrite_argv(struct bsdtar *,
|
|
|
|
int *argc, char ** src_argv,
|
2004-04-05 21:32:18 +00:00
|
|
|
const char *optstring);
|
|
|
|
|
2004-06-26 22:49:51 +00:00
|
|
|
/*
|
|
|
|
* The leading '+' here forces the GNU version of getopt() (as well as
|
|
|
|
* both the GNU and BSD versions of getopt_long) to stop at the first
|
|
|
|
* non-option. Otherwise, GNU getopt() permutes the arguments and
|
|
|
|
* screws up -C processing.
|
|
|
|
*/
|
|
|
|
const char *tar_opts = "+Bb:C:cF:f:HhjkLlmnOoPprtT:UuvW:wX:xyZz";
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/*
|
2004-06-26 22:49:51 +00:00
|
|
|
* Most of these long options are deliberately not documented. They
|
|
|
|
* are provided only to make life easier for people who also use GNU tar.
|
|
|
|
* The only long options documented in the manual page are the ones
|
|
|
|
* with no corresponding short option, such as --exclude, --nodump,
|
|
|
|
* and --fast-read.
|
2004-04-05 21:32:18 +00:00
|
|
|
*
|
2004-06-26 22:49:51 +00:00
|
|
|
* On systems that lack getopt_long, long options can be specified
|
|
|
|
* using -W longopt and -W longopt=value, e.g. "-W nodump" is the same
|
|
|
|
* as "--nodump" and "-W exclude=pattern" is the same as "--exclude
|
|
|
|
* pattern". This does not rely the GNU getopt() "W;" extension, so
|
|
|
|
* should work correctly on any system with a POSIX-compliant getopt().
|
2004-04-05 21:32:18 +00:00
|
|
|
*/
|
|
|
|
|
2004-06-26 22:49:51 +00:00
|
|
|
/* Fake short equivalents for long options that otherwise lack them. */
|
2004-04-05 21:32:18 +00:00
|
|
|
#define OPTION_EXCLUDE 1
|
|
|
|
#define OPTION_FAST_READ 2
|
2004-06-27 06:29:03 +00:00
|
|
|
#define OPTION_HELP 3
|
|
|
|
#define OPTION_INCLUDE 4
|
|
|
|
#define OPTION_NODUMP 5
|
|
|
|
#define OPTION_NO_SAME_PERMISSIONS 6
|
|
|
|
#define OPTION_NULL 7
|
|
|
|
#define OPTION_ONE_FILE_SYSTEM 8
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
const struct option tar_longopts[] = {
|
2004-05-05 21:06:37 +00:00
|
|
|
{ "absolute-paths", no_argument, NULL, 'P' },
|
|
|
|
{ "append", no_argument, NULL, 'r' },
|
|
|
|
{ "block-size", required_argument, NULL, 'b' },
|
|
|
|
{ "bunzip2", no_argument, NULL, 'j' },
|
|
|
|
{ "bzip", no_argument, NULL, 'j' },
|
|
|
|
{ "bzip2", no_argument, NULL, 'j' },
|
|
|
|
{ "cd", required_argument, NULL, 'C' },
|
|
|
|
{ "confirmation", no_argument, NULL, 'w' },
|
|
|
|
{ "create", no_argument, NULL, 'c' },
|
2004-06-11 01:35:56 +00:00
|
|
|
{ "dereference", no_argument, NULL, 'L' },
|
2004-05-05 21:06:37 +00:00
|
|
|
{ "directory", required_argument, NULL, 'C' },
|
|
|
|
{ "exclude", required_argument, NULL, OPTION_EXCLUDE },
|
2004-06-15 05:55:41 +00:00
|
|
|
{ "exclude-from", required_argument, NULL, 'X' },
|
2004-05-05 21:06:37 +00:00
|
|
|
{ "extract", no_argument, NULL, 'x' },
|
|
|
|
{ "fast-read", no_argument, NULL, OPTION_FAST_READ },
|
|
|
|
{ "file", required_argument, NULL, 'f' },
|
|
|
|
{ "format", required_argument, NULL, 'F' },
|
|
|
|
{ "gunzip", no_argument, NULL, 'z' },
|
|
|
|
{ "gzip", no_argument, NULL, 'z' },
|
|
|
|
{ "help", no_argument, NULL, OPTION_HELP },
|
2004-06-07 03:49:48 +00:00
|
|
|
{ "include", required_argument, NULL, OPTION_INCLUDE },
|
2004-05-05 21:06:37 +00:00
|
|
|
{ "interactive", no_argument, NULL, 'w' },
|
|
|
|
{ "keep-old-files", no_argument, NULL, 'k' },
|
|
|
|
{ "list", no_argument, NULL, 't' },
|
|
|
|
{ "modification-time", no_argument, NULL, 'm' },
|
|
|
|
{ "nodump", no_argument, NULL, OPTION_NODUMP },
|
|
|
|
{ "norecurse", no_argument, NULL, 'n' },
|
2004-06-28 14:02:22 +00:00
|
|
|
{ "no-recursion", no_argument, NULL, 'n' },
|
2004-05-22 17:49:54 +00:00
|
|
|
{ "no-same-owner", no_argument, NULL, 'o' },
|
2004-06-15 06:44:41 +00:00
|
|
|
{ "no-same-permissions",no_argument, NULL, OPTION_NO_SAME_PERMISSIONS },
|
2004-06-27 06:29:03 +00:00
|
|
|
{ "null", no_argument, NULL, OPTION_NULL },
|
2004-06-15 00:28:34 +00:00
|
|
|
{ "one-file-system", no_argument, NULL, OPTION_ONE_FILE_SYSTEM },
|
2004-05-05 21:06:37 +00:00
|
|
|
{ "preserve-permissions", no_argument, NULL, 'p' },
|
2004-05-18 17:16:55 +00:00
|
|
|
{ "read-full-blocks", no_argument, NULL, 'B' },
|
2004-05-05 21:06:37 +00:00
|
|
|
{ "same-permissions", no_argument, NULL, 'p' },
|
|
|
|
{ "to-stdout", no_argument, NULL, 'O' },
|
|
|
|
{ "unlink", no_argument, NULL, 'U' },
|
|
|
|
{ "unlink-first", no_argument, NULL, 'U' },
|
|
|
|
{ "update", no_argument, NULL, 'u' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
2004-04-05 21:32:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct bsdtar *bsdtar, bsdtar_storage;
|
2004-06-26 22:49:51 +00:00
|
|
|
const struct option *option;
|
2004-04-05 21:32:18 +00:00
|
|
|
struct passwd *pwent;
|
2004-04-08 19:37:01 +00:00
|
|
|
int opt;
|
|
|
|
char mode;
|
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-05-17 05:44:53 +00:00
|
|
|
if (setlocale(LC_ALL, "") == NULL)
|
|
|
|
bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
|
|
|
|
mode = '\0';
|
2004-06-02 21:01:27 +00:00
|
|
|
possible_help_request = 0;
|
2004-05-17 05:44:53 +00:00
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
/* Look up uid/uname of current user for future reference */
|
|
|
|
bsdtar->user_uid = geteuid();
|
|
|
|
bsdtar->user_uname = NULL;
|
|
|
|
if ((pwent = getpwuid(bsdtar->user_uid))) {
|
|
|
|
bsdtar->user_uname = (char *)malloc(strlen(pwent->pw_name)+1);
|
|
|
|
if (bsdtar->user_uname)
|
|
|
|
strcpy(bsdtar->user_uname, pwent->pw_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
if (bsdtar->user_uid == 0)
|
2004-05-19 01:21:11 +00:00
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar->progname = strrchr(*argv, '/');
|
|
|
|
if (bsdtar->progname != NULL)
|
|
|
|
bsdtar->progname++;
|
|
|
|
else
|
|
|
|
bsdtar->progname = *argv;
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
/* Rewrite traditional-style tar arguments, if used. */
|
2004-05-17 05:44:53 +00:00
|
|
|
argv = rewrite_argv(bsdtar, &argc, argv, tar_opts);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
bsdtar->argv = argv;
|
|
|
|
bsdtar->argc = argc;
|
|
|
|
|
|
|
|
/* Process all remaining arguments now. */
|
2004-06-26 22:49:51 +00:00
|
|
|
while ((opt = bsdtar_getopt(bsdtar, tar_opts, &option)) != -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 */
|
|
|
|
bsdtar->bytes_per_block = 512 * atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'C': /* GNU tar */
|
2004-06-26 22:49:51 +00:00
|
|
|
/* Defer first -C until after -f is opened. */
|
2004-04-05 21:32:18 +00:00
|
|
|
bsdtar->start_dir = optarg;
|
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'c': /* SUSv2 */
|
|
|
|
if (mode != '\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, mode);
|
|
|
|
mode = opt;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case OPTION_EXCLUDE: /* GNU tar */
|
|
|
|
exclude(bsdtar, optarg);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
bsdtar->create_format = optarg;
|
|
|
|
break;
|
|
|
|
case 'f': /* SUSv2 */
|
|
|
|
bsdtar->filename = optarg;
|
|
|
|
if (strcmp(bsdtar->filename, "-") == 0)
|
|
|
|
bsdtar->filename = NULL;
|
|
|
|
break;
|
|
|
|
case OPTION_FAST_READ: /* GNU tar */
|
|
|
|
bsdtar->option_fast_read = 1;
|
|
|
|
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;
|
|
|
|
case OPTION_HELP:
|
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-06-07 03:49:48 +00:00
|
|
|
case OPTION_INCLUDE:
|
|
|
|
include(bsdtar, optarg);
|
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'j': /* GNU tar */
|
|
|
|
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;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'k': /* GNU tar */
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
|
|
|
|
break;
|
|
|
|
case 'L': /* BSD convention */
|
|
|
|
bsdtar->symlink_mode = 'L';
|
|
|
|
break;
|
2004-06-15 00:28:34 +00:00
|
|
|
case 'l': /* SUSv2; note that GNU -l conflicts */
|
2004-04-05 21:32:18 +00:00
|
|
|
bsdtar->option_warn_links = 1;
|
|
|
|
break;
|
|
|
|
case 'm': /* SUSv2 */
|
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
|
|
|
|
break;
|
|
|
|
case 'n': /* GNU tar */
|
|
|
|
bsdtar->option_no_subdirs = 1;
|
|
|
|
break;
|
|
|
|
case OPTION_NODUMP: /* star */
|
|
|
|
bsdtar->option_honor_nodump = 1;
|
|
|
|
break;
|
2004-06-15 06:44:41 +00:00
|
|
|
case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
|
2004-06-15 07:16:44 +00:00
|
|
|
/*
|
|
|
|
* This is always the default in FreeBSD's
|
|
|
|
* version of GNU tar; it's also the default
|
|
|
|
* behavior for bsdtar, so treat the
|
|
|
|
* command-line option as a no-op.
|
|
|
|
*/
|
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;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'O': /* GNU tar */
|
|
|
|
bsdtar->option_stdout = 1;
|
|
|
|
break;
|
2004-06-15 00:28:34 +00:00
|
|
|
case 'o': /* SUSv2; note that GNU -o conflicts */
|
2004-04-05 21:32:18 +00:00
|
|
|
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
|
|
|
|
break;
|
2004-06-15 00:28:34 +00:00
|
|
|
case OPTION_ONE_FILE_SYSTEM: /* -l in GNU tar */
|
|
|
|
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 */
|
|
|
|
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;
|
|
|
|
bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
|
2004-04-05 21:32:18 +00:00
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'r': /* SUSv2 */
|
|
|
|
if (mode != '\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, mode);
|
|
|
|
mode = opt;
|
|
|
|
break;
|
2004-06-27 06:29:03 +00:00
|
|
|
case 'T': /* GNU tar */
|
|
|
|
bsdtar->names_from_file = optarg;
|
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 't': /* SUSv2 */
|
|
|
|
if (mode != '\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, mode);
|
|
|
|
mode = opt;
|
|
|
|
bsdtar->verbose++;
|
|
|
|
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 */
|
|
|
|
if (mode != '\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, mode);
|
|
|
|
mode = opt;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'v': /* SUSv2 */
|
|
|
|
bsdtar->verbose++;
|
|
|
|
break;
|
|
|
|
case 'w': /* SUSv2 */
|
|
|
|
bsdtar->option_interactive = 1;
|
|
|
|
break;
|
2004-06-15 05:55:41 +00:00
|
|
|
case 'X': /* GNU tar */
|
|
|
|
exclude_from_file(bsdtar, optarg);
|
|
|
|
break;
|
2004-04-08 19:37:01 +00:00
|
|
|
case 'x': /* SUSv2 */
|
|
|
|
if (mode != '\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, mode);
|
|
|
|
mode = opt;
|
|
|
|
break;
|
2004-04-05 21:32:18 +00:00
|
|
|
case 'y': /* FreeBSD version of GNU tar */
|
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;
|
|
|
|
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-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;
|
|
|
|
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.
|
|
|
|
*/
|
2004-06-02 21:01:27 +00:00
|
|
|
if (mode == '\0' && possible_help_request) {
|
|
|
|
long_help(bsdtar);
|
2004-06-15 00:07:23 +00:00
|
|
|
exit(0);
|
2004-06-02 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
2004-04-08 19:37:01 +00:00
|
|
|
if (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_absolute_paths)
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, "-P", "xcru");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_dont_traverse_mounts)
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, "-X", "cru");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_fast_read)
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, "--fast-read", "xt");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_honor_nodump)
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, "--nodump", "cru");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_no_subdirs)
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, "-n", "cru");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_stdout)
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, "-O", "x");
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->option_warn_links)
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, "-l", "cr");
|
2004-04-08 19:37:01 +00:00
|
|
|
|
|
|
|
/* Check other parameters only permitted in certain modes. */
|
2004-05-30 00:09:43 +00:00
|
|
|
if (bsdtar->create_compression == 'Z' && mode == 'c') {
|
|
|
|
bsdtar_warnc(bsdtar, 0, ".Z compression not supported");
|
|
|
|
usage(bsdtar);
|
|
|
|
}
|
2004-04-08 19:37:01 +00:00
|
|
|
if (bsdtar->create_compression != '\0') {
|
|
|
|
strcpy(buff, "-?");
|
|
|
|
buff[1] = bsdtar->create_compression;
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, buff, "cxt");
|
2004-04-08 19:37:01 +00:00
|
|
|
}
|
|
|
|
if (bsdtar->create_format != NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, "-F", "c");
|
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-05-17 05:44:53 +00:00
|
|
|
only_mode(bsdtar, mode, buff, "cru");
|
2004-04-08 19:37:01 +00:00
|
|
|
}
|
|
|
|
|
2004-04-05 21:32:18 +00:00
|
|
|
bsdtar->argc -= optind;
|
|
|
|
bsdtar->argv += optind;
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bsdtar->user_uname != NULL)
|
|
|
|
free(bsdtar->user_uname);
|
|
|
|
|
2004-04-15 22:37:54 +00:00
|
|
|
cleanup_exclusions(bsdtar);
|
2004-05-27 04:30:59 +00:00
|
|
|
return (bsdtar->return_value);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that the mode is correct.
|
|
|
|
*/
|
|
|
|
static void
|
2004-05-17 05:44:53 +00:00
|
|
|
only_mode(struct bsdtar *bsdtar, char mode,
|
|
|
|
const char *opt, const char *valid_modes)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
if (strchr(valid_modes, mode) == NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Option %s is not permitted in mode -%c",
|
2004-04-05 21:32:18 +00:00
|
|
|
opt, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Convert traditional tar arguments into new-style.
|
|
|
|
* For example,
|
|
|
|
* tar tvfb file.tar 32 --exclude FOO
|
2004-06-26 22:49:51 +00:00
|
|
|
* will be converted to
|
2004-04-05 21:32:18 +00:00
|
|
|
* tar -t -v -f file.tar -b 32 --exclude FOO
|
|
|
|
*
|
|
|
|
* This requires building a new argv array. The initial bundled word
|
|
|
|
* gets expanded into a new string that looks like "-t\0-v\0-f\0-b\0".
|
|
|
|
* The new argv array has pointers into this string intermingled with
|
|
|
|
* pointers to the existing arguments. Arguments are moved to
|
|
|
|
* immediately follow their options.
|
|
|
|
*
|
|
|
|
* The optstring argument here is the same one passed to getopt(3).
|
|
|
|
* It is used to determine which option letters have trailing arguments.
|
|
|
|
*/
|
|
|
|
char **
|
2004-05-17 05:44:53 +00:00
|
|
|
rewrite_argv(struct bsdtar *bsdtar, int *argc, char ** src_argv,
|
|
|
|
const char *optstring)
|
2004-04-05 21:32:18 +00:00
|
|
|
{
|
|
|
|
char **new_argv, **dest_argv;
|
|
|
|
const char *p;
|
|
|
|
char *src, *dest;
|
|
|
|
|
|
|
|
if (src_argv[1] == NULL || src_argv[1][0] == '-')
|
|
|
|
return (src_argv);
|
|
|
|
|
|
|
|
*argc += strlen(src_argv[1]) - 1;
|
|
|
|
new_argv = malloc((*argc + 1) * sizeof(new_argv[0]));
|
|
|
|
if (new_argv == NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, errno, "No Memory");
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
dest_argv = new_argv;
|
|
|
|
*dest_argv++ = *src_argv++;
|
|
|
|
|
|
|
|
dest = malloc(strlen(*src_argv) * 3);
|
|
|
|
if (dest == NULL)
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, errno, "No memory");
|
2004-04-05 21:32:18 +00:00
|
|
|
for (src = *src_argv++; *src != '\0'; src++) {
|
|
|
|
*dest_argv++ = dest;
|
|
|
|
*dest++ = '-';
|
|
|
|
*dest++ = *src;
|
|
|
|
*dest++ = '\0';
|
|
|
|
/* If option takes an argument, insert that into the list. */
|
|
|
|
for (p = optstring; p != NULL && *p != '\0'; p++) {
|
|
|
|
if (*p != *src)
|
|
|
|
continue;
|
|
|
|
if (p[1] != ':') /* No arg required, done. */
|
|
|
|
break;
|
|
|
|
if (*src_argv == NULL) /* No arg available? Error. */
|
2004-05-17 05:44:53 +00:00
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
2004-04-05 21:32:18 +00:00
|
|
|
"Option %c requires an argument",
|
|
|
|
*src);
|
|
|
|
*dest_argv++ = *src_argv++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy remaining arguments, including trailing NULL. */
|
|
|
|
while ((*dest_argv++ = *src_argv++) != NULL)
|
|
|
|
;
|
|
|
|
|
|
|
|
return (new_argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2004-06-02 21:01:27 +00:00
|
|
|
#ifdef HAVE_GETOPT_LONG
|
2004-06-15 00:07:23 +00:00
|
|
|
fprintf(stderr, " Help: %s --help\n", p);
|
2004-06-02 21:01:27 +00:00
|
|
|
#else
|
2004-06-15 00:07:23 +00:00
|
|
|
fprintf(stderr, " Help: %s -h\n", p);
|
2004-06-02 21:01:27 +00:00
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
"Create: %p -c [options] [<file> | <dir> | @<archive> | C=<dir> ]\n",
|
|
|
|
" <file>, <dir> add these items to archive\n",
|
|
|
|
" -z, -j Compress archive with gzip/bzip2\n",
|
|
|
|
" -F {ustar|pax|cpio|shar} Select archive format\n",
|
2004-06-15 00:07:23 +00:00
|
|
|
#ifdef HAVE_GETOPT_LONG
|
2004-04-05 21:32:18 +00:00
|
|
|
" --exclude <pattern> Skip files that match pattern\n",
|
2004-06-26 22:49:51 +00:00
|
|
|
#else
|
|
|
|
" -W exclude=<pattern> Skip files that match pattern\n",
|
2004-06-15 00:07:23 +00:00
|
|
|
#endif
|
2004-04-05 21:32:18 +00:00
|
|
|
" C=<dir> Change to <dir> before processing remaining files\n",
|
|
|
|
" @<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",
|
2004-07-12 13:13:42 +00:00
|
|
|
" -k Keep (don't overwrite) existing files\n",
|
2004-04-05 21:32:18 +00:00
|
|
|
" -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",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
const char **msg;
|
|
|
|
|
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);
|
|
|
|
if (strcmp(prog,"bsdtar")!=0)
|
|
|
|
p = "(bsdtar)";
|
|
|
|
else
|
|
|
|
p = "";
|
|
|
|
|
2004-07-04 18:13:01 +00:00
|
|
|
fprintf(stdout, "%s%s: manipulate archive files\n", prog, p);
|
2004-04-05 21:32:18 +00:00
|
|
|
|
|
|
|
for (msg = long_help_msg; *msg != NULL; msg++) {
|
|
|
|
for (p = *msg; p != NULL; p++) {
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
else if (*p == '%') {
|
|
|
|
if (p[1] == 'p') {
|
2004-07-04 18:13:01 +00:00
|
|
|
fputs(prog, stdout);
|
2004-04-05 21:32:18 +00:00
|
|
|
p++;
|
|
|
|
} else
|
|
|
|
putchar('%');
|
|
|
|
} else
|
|
|
|
putchar(*p);
|
|
|
|
}
|
|
|
|
}
|
2004-06-15 00:07:23 +00:00
|
|
|
fflush(stderr);
|
2004-04-05 21:32:18 +00:00
|
|
|
}
|
2004-06-26 22:49:51 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
bsdtar_getopt(struct bsdtar *bsdtar, const char *optstring,
|
|
|
|
const struct option **poption)
|
|
|
|
{
|
|
|
|
char *p, *q;
|
|
|
|
const struct option *option;
|
|
|
|
int opt;
|
|
|
|
int option_index;
|
|
|
|
size_t option_length;
|
|
|
|
|
|
|
|
option_index = -1;
|
|
|
|
*poption = NULL;
|
|
|
|
|
|
|
|
#ifdef HAVE_GETOPT_LONG
|
|
|
|
opt = getopt_long(bsdtar->argc, bsdtar->argv, optstring,
|
|
|
|
tar_longopts, &option_index);
|
|
|
|
if (option_index > -1)
|
|
|
|
*poption = tar_longopts + option_index;
|
|
|
|
#else
|
|
|
|
opt = getopt(bsdtar->argc, bsdtar->argv, optstring);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Support long options through -W longopt=value */
|
|
|
|
if (opt == 'W') {
|
|
|
|
p = optarg;
|
|
|
|
q = strchr(optarg, '=');
|
|
|
|
if (q != NULL) {
|
|
|
|
option_length = q - p;
|
|
|
|
optarg = q + 1;
|
|
|
|
} else {
|
|
|
|
option_length = strlen(p);
|
|
|
|
optarg = NULL;
|
|
|
|
}
|
|
|
|
option = tar_longopts;
|
|
|
|
while (option->name != NULL &&
|
|
|
|
(strlen(option->name) < option_length ||
|
|
|
|
strncmp(p, option->name, option_length) != 0 )) {
|
|
|
|
option++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option->name != NULL) {
|
|
|
|
*poption = option;
|
|
|
|
opt = option->val;
|
|
|
|
|
|
|
|
/* Check if there's another match. */
|
|
|
|
option++;
|
|
|
|
while (option->name != NULL &&
|
|
|
|
(strlen(option->name) < option_length ||
|
|
|
|
strncmp(p, option->name, option_length) != 0)) {
|
|
|
|
option++;
|
|
|
|
}
|
|
|
|
if (option->name != NULL)
|
|
|
|
bsdtar_errc(bsdtar, 1, 0,
|
|
|
|
"Ambiguous option %s "
|
|
|
|
"(matches both %s and %s)",
|
|
|
|
p, (*poption)->name, option->name);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
opt = '?';
|
|
|
|
/* TODO: Set up a fake 'struct option' for
|
|
|
|
* error reporting... ? ? ? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (opt);
|
|
|
|
}
|