~laurynas-biveinis/percona-xtrabackup/xtrabackup-page-filters

« back to all changes in this revision

Viewing changes to src/libarchive/tar/bsdtar.c

merge parallel compression branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * Copyright (c) 2003-2008 Tim Kientzle
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
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.
 
13
 *
 
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.
 
24
 */
 
25
 
 
26
#include "bsdtar_platform.h"
 
27
__FBSDID("$FreeBSD: src/usr.bin/tar/bsdtar.c,v 1.93 2008/11/08 04:43:24 kientzle Exp $");
 
28
 
 
29
#ifdef HAVE_SYS_PARAM_H
 
30
#include <sys/param.h>
 
31
#endif
 
32
#ifdef HAVE_SYS_STAT_H
 
33
#include <sys/stat.h>
 
34
#endif
 
35
#ifdef HAVE_ERRNO_H
 
36
#include <errno.h>
 
37
#endif
 
38
#ifdef HAVE_FCNTL_H
 
39
#include <fcntl.h>
 
40
#endif
 
41
#ifdef HAVE_LANGINFO_H
 
42
#include <langinfo.h>
 
43
#endif
 
44
#ifdef HAVE_LOCALE_H
 
45
#include <locale.h>
 
46
#endif
 
47
#ifdef HAVE_PATHS_H
 
48
#include <paths.h>
 
49
#endif
 
50
#ifdef HAVE_SIGNAL_H
 
51
#include <signal.h>
 
52
#endif
 
53
#include <stdio.h>
 
54
#ifdef HAVE_STDLIB_H
 
55
#include <stdlib.h>
 
56
#endif
 
57
#ifdef HAVE_STRING_H
 
58
#include <string.h>
 
59
#endif
 
60
#ifdef HAVE_TIME_H
 
61
#include <time.h>
 
62
#endif
 
63
#ifdef HAVE_UNISTD_H
 
64
#include <unistd.h>
 
65
#endif
 
66
#if HAVE_ZLIB_H
 
67
#include <zlib.h>
 
68
#endif
 
69
 
 
70
#include "bsdtar.h"
 
71
#include "err.h"
 
72
 
 
73
/*
 
74
 * Per POSIX.1-1988, tar defaults to reading/writing archives to/from
 
75
 * the default tape device for the system.  Pick something reasonable here.
 
76
 */
 
77
#ifdef __linux
 
78
#define _PATH_DEFTAPE "/dev/st0"
 
79
#endif
 
80
#if defined(_WIN32) && !defined(__CYGWIN__)
 
81
#define _PATH_DEFTAPE "\\\\.\\tape0"
 
82
#endif
 
83
 
 
84
#ifndef _PATH_DEFTAPE
 
85
#define _PATH_DEFTAPE "/dev/tape"
 
86
#endif
 
87
 
 
88
#ifdef __MINGW32__
 
89
int _CRT_glob = 0; /* Disable broken CRT globbing. */
 
90
#endif
 
91
 
 
92
static struct bsdtar *_bsdtar;
 
93
 
 
94
#if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
 
95
static volatile int siginfo_occurred;
 
96
 
 
97
static void
 
98
siginfo_handler(int sig)
 
99
{
 
100
        (void)sig; /* UNUSED */
 
101
        siginfo_occurred = 1;
 
102
}
 
103
 
 
104
int
 
105
need_report(void)
 
106
{
 
107
        int r = siginfo_occurred;
 
108
        siginfo_occurred = 0;
 
109
        return (r);
 
110
}
 
111
#else
 
112
int
 
113
need_report(void)
 
114
{
 
115
        return (0);
 
116
}
 
117
#endif
 
118
 
 
119
/* External function to parse a date/time string */
 
120
time_t get_date(time_t, const char *);
 
121
 
 
122
static void              long_help(void);
 
123
static void              only_mode(struct bsdtar *, const char *opt,
 
124
                             const char *valid);
 
125
static void              set_mode(struct bsdtar *, char opt);
 
126
static void              version(void);
 
127
 
 
128
/* A basic set of security flags to request from libarchive. */
 
129
#define SECURITY                                        \
 
130
        (ARCHIVE_EXTRACT_SECURE_SYMLINKS                \
 
131
         | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
 
132
 
 
133
int
 
134
main(int argc, char **argv)
 
135
{
 
136
        struct bsdtar           *bsdtar, bsdtar_storage;
 
137
        int                      opt, t;
 
138
        char                     option_o;
 
139
        char                     possible_help_request;
 
140
        char                     buff[16];
 
141
        time_t                   now;
 
142
 
 
143
        /*
 
144
         * Use a pointer for consistency, but stack-allocated storage
 
145
         * for ease of cleanup.
 
146
         */
 
147
        _bsdtar = bsdtar = &bsdtar_storage;
 
148
        memset(bsdtar, 0, sizeof(*bsdtar));
 
149
        bsdtar->fd = -1; /* Mark as "unused" */
 
150
        option_o = 0;
 
151
 
 
152
#if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
 
153
        { /* Catch SIGINFO and SIGUSR1, if they exist. */
 
154
                struct sigaction sa;
 
155
                sa.sa_handler = siginfo_handler;
 
156
                sigemptyset(&sa.sa_mask);
 
157
                sa.sa_flags = 0;
 
158
#ifdef SIGINFO
 
159
                if (sigaction(SIGINFO, &sa, NULL))
 
160
                        lafe_errc(1, errno, "sigaction(SIGINFO) failed");
 
161
#endif
 
162
#ifdef SIGUSR1
 
163
                /* ... and treat SIGUSR1 the same way as SIGINFO. */
 
164
                if (sigaction(SIGUSR1, &sa, NULL))
 
165
                        lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
 
166
#endif
 
167
        }
 
168
#endif
 
169
 
 
170
 
 
171
        /* Need lafe_progname before calling lafe_warnc. */
 
172
        if (*argv == NULL)
 
173
                lafe_progname = "bsdtar";
 
174
        else {
 
175
#if defined(_WIN32) && !defined(__CYGWIN__)
 
176
                lafe_progname = strrchr(*argv, '\\');
 
177
#else
 
178
                lafe_progname = strrchr(*argv, '/');
 
179
#endif
 
180
                if (lafe_progname != NULL)
 
181
                        lafe_progname++;
 
182
                else
 
183
                        lafe_progname = *argv;
 
184
        }
 
185
 
 
186
        time(&now);
 
187
 
 
188
#if HAVE_SETLOCALE
 
189
        if (setlocale(LC_ALL, "") == NULL)
 
190
                lafe_warnc(0, "Failed to set default locale");
 
191
#endif
 
192
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
 
193
        bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
 
194
#endif
 
195
        possible_help_request = 0;
 
196
 
 
197
        /* Look up uid of current user for future reference */
 
198
        bsdtar->user_uid = geteuid();
 
199
 
 
200
        /* Default: open tape drive. */
 
201
        bsdtar->filename = getenv("TAPE");
 
202
        if (bsdtar->filename == NULL)
 
203
                bsdtar->filename = _PATH_DEFTAPE;
 
204
 
 
205
        /* Default: preserve mod time on extract */
 
206
        bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
 
207
 
 
208
        /* Default: Perform basic security checks. */
 
209
        bsdtar->extract_flags |= SECURITY;
 
210
 
 
211
#ifndef _WIN32
 
212
        /* On POSIX systems, assume --same-owner and -p when run by
 
213
         * the root user.  This doesn't make any sense on Windows. */
 
214
        if (bsdtar->user_uid == 0) {
 
215
                /* --same-owner */
 
216
                bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
 
217
                /* -p */
 
218
                bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
 
219
                bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
 
220
                bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
 
221
                bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
 
222
        }
 
223
#endif
 
224
 
 
225
        bsdtar->argv = argv;
 
226
        bsdtar->argc = argc;
 
227
 
 
228
        /*
 
229
         * Comments following each option indicate where that option
 
230
         * originated:  SUSv2, POSIX, GNU tar, star, etc.  If there's
 
231
         * no such comment, then I don't know of anyone else who
 
232
         * implements that option.
 
233
         */
 
234
        while ((opt = bsdtar_getopt(bsdtar)) != -1) {
 
235
                switch (opt) {
 
236
                case 'B': /* GNU tar */
 
237
                        /* libarchive doesn't need this; just ignore it. */
 
238
                        break;
 
239
                case 'b': /* SUSv2 */
 
240
                        t = atoi(bsdtar->optarg);
 
241
                        if (t <= 0 || t > 8192)
 
242
                                lafe_errc(1, 0,
 
243
                                    "Argument to -b is out of range (1..8192)");
 
244
                        bsdtar->bytes_per_block = 512 * t;
 
245
                        break;
 
246
                case 'C': /* GNU tar */
 
247
                        set_chdir(bsdtar, bsdtar->optarg);
 
248
                        break;
 
249
                case 'c': /* SUSv2 */
 
250
                        set_mode(bsdtar, opt);
 
251
                        break;
 
252
                case OPTION_CHECK_LINKS: /* GNU tar */
 
253
                        bsdtar->option_warn_links = 1;
 
254
                        break;
 
255
                case OPTION_CHROOT: /* NetBSD */
 
256
                        bsdtar->option_chroot = 1;
 
257
                        break;
 
258
                case OPTION_EXCLUDE: /* GNU tar */
 
259
                        if (lafe_exclude(&bsdtar->matching, bsdtar->optarg))
 
260
                                lafe_errc(1, 0,
 
261
                                    "Couldn't exclude %s\n", bsdtar->optarg);
 
262
                        break;
 
263
                case OPTION_FORMAT: /* GNU tar, others */
 
264
                        bsdtar->create_format = bsdtar->optarg;
 
265
                        break;
 
266
                case OPTION_OPTIONS:
 
267
                        bsdtar->option_options = bsdtar->optarg;
 
268
                        break;
 
269
                case 'f': /* SUSv2 */
 
270
                        bsdtar->filename = bsdtar->optarg;
 
271
                        if (strcmp(bsdtar->filename, "-") == 0)
 
272
                                bsdtar->filename = NULL;
 
273
                        break;
 
274
                case 'H': /* BSD convention */
 
275
                        bsdtar->symlink_mode = 'H';
 
276
                        break;
 
277
                case 'h': /* Linux Standards Base, gtar; synonym for -L */
 
278
                        bsdtar->symlink_mode = 'L';
 
279
                        /* Hack: -h by itself is the "help" command. */
 
280
                        possible_help_request = 1;
 
281
                        break;
 
282
                case OPTION_HELP: /* GNU tar, others */
 
283
                        long_help();
 
284
                        exit(0);
 
285
                        break;
 
286
                case 'I': /* GNU tar */
 
287
                        /*
 
288
                         * TODO: Allow 'names' to come from an archive,
 
289
                         * not just a text file.  Design a good UI for
 
290
                         * allowing names and mode/owner to be read
 
291
                         * from an archive, with contents coming from
 
292
                         * disk.  This can be used to "refresh" an
 
293
                         * archive or to design archives with special
 
294
                         * permissions without having to create those
 
295
                         * permissions on disk.
 
296
                         */
 
297
                        bsdtar->names_from_file = bsdtar->optarg;
 
298
                        break;
 
299
                case OPTION_INCLUDE:
 
300
                        /*
 
301
                         * Noone else has the @archive extension, so
 
302
                         * noone else needs this to filter entries
 
303
                         * when transforming archives.
 
304
                         */
 
305
                        if (lafe_include(&bsdtar->matching, bsdtar->optarg))
 
306
                                lafe_errc(1, 0,
 
307
                                    "Failed to add %s to inclusion list",
 
308
                                    bsdtar->optarg);
 
309
                        break;
 
310
                case 'j': /* GNU tar */
 
311
                        if (bsdtar->create_compression != '\0')
 
312
                                lafe_errc(1, 0,
 
313
                                    "Can't specify both -%c and -%c", opt,
 
314
                                    bsdtar->create_compression);
 
315
                        bsdtar->create_compression = opt;
 
316
                        break;
 
317
                case 'J': /* GNU tar 1.21 and later */
 
318
                        if (bsdtar->create_compression != '\0')
 
319
                                lafe_errc(1, 0,
 
320
                                    "Can't specify both -%c and -%c", opt,
 
321
                                    bsdtar->create_compression);
 
322
                        bsdtar->create_compression = opt;
 
323
                        break;
 
324
                case 'k': /* GNU tar */
 
325
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
 
326
                        break;
 
327
                case OPTION_KEEP_NEWER_FILES: /* GNU tar */
 
328
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
 
329
                        break;
 
330
                case 'L': /* BSD convention */
 
331
                        bsdtar->symlink_mode = 'L';
 
332
                        break;
 
333
                case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
 
334
                        /* GNU tar 1.13  used -l for --one-file-system */
 
335
                        bsdtar->option_warn_links = 1;
 
336
                        break;
 
337
                case OPTION_LZMA:
 
338
                        if (bsdtar->create_compression != '\0')
 
339
                                lafe_errc(1, 0,
 
340
                                    "Can't specify both -%c and -%c", opt,
 
341
                                    bsdtar->create_compression);
 
342
                        bsdtar->create_compression = opt;
 
343
                        break;
 
344
                case 'm': /* SUSv2 */
 
345
                        bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
 
346
                        break;
 
347
                case 'n': /* GNU tar */
 
348
                        bsdtar->option_no_subdirs = 1;
 
349
                        break;
 
350
                /*
 
351
                 * Selecting files by time:
 
352
                 *    --newer-?time='date' Only files newer than 'date'
 
353
                 *    --newer-?time-than='file' Only files newer than time
 
354
                 *         on specified file (useful for incremental backups)
 
355
                 * TODO: Add corresponding "older" options to reverse these.
 
356
                 */
 
357
                case OPTION_NEWER_CTIME: /* GNU tar */
 
358
                        bsdtar->newer_ctime_sec = get_date(now, bsdtar->optarg);
 
359
                        break;
 
360
                case OPTION_NEWER_CTIME_THAN:
 
361
                        {
 
362
                                struct stat st;
 
363
                                if (stat(bsdtar->optarg, &st) != 0)
 
364
                                        lafe_errc(1, 0,
 
365
                                            "Can't open file %s", bsdtar->optarg);
 
366
                                bsdtar->newer_ctime_sec = st.st_ctime;
 
367
                                bsdtar->newer_ctime_nsec =
 
368
                                    ARCHIVE_STAT_CTIME_NANOS(&st);
 
369
                        }
 
370
                        break;
 
371
                case OPTION_NEWER_MTIME: /* GNU tar */
 
372
                        bsdtar->newer_mtime_sec = get_date(now, bsdtar->optarg);
 
373
                        break;
 
374
                case OPTION_NEWER_MTIME_THAN:
 
375
                        {
 
376
                                struct stat st;
 
377
                                if (stat(bsdtar->optarg, &st) != 0)
 
378
                                        lafe_errc(1, 0,
 
379
                                            "Can't open file %s", bsdtar->optarg);
 
380
                                bsdtar->newer_mtime_sec = st.st_mtime;
 
381
                                bsdtar->newer_mtime_nsec =
 
382
                                    ARCHIVE_STAT_MTIME_NANOS(&st);
 
383
                        }
 
384
                        break;
 
385
                case OPTION_NODUMP: /* star */
 
386
                        bsdtar->option_honor_nodump = 1;
 
387
                        break;
 
388
                case OPTION_NO_SAME_OWNER: /* GNU tar */
 
389
                        bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
 
390
                        break;
 
391
                case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
 
392
                        bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
 
393
                        bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
 
394
                        bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
 
395
                        bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
 
396
                        break;
 
397
                case OPTION_NULL: /* GNU tar */
 
398
                        bsdtar->option_null++;
 
399
                        break;
 
400
                case OPTION_NUMERIC_OWNER: /* GNU tar */
 
401
                        bsdtar->option_numeric_owner++;
 
402
                        break;
 
403
                case 'O': /* GNU tar */
 
404
                        bsdtar->option_stdout = 1;
 
405
                        break;
 
406
                case 'o': /* SUSv2 and GNU conflict here, but not fatally */
 
407
                        option_o = 1; /* Record it and resolve it later. */
 
408
                        break;
 
409
                case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
 
410
                        bsdtar->option_dont_traverse_mounts = 1;
 
411
                        break;
 
412
#if 0
 
413
                /*
 
414
                 * The common BSD -P option is not necessary, since
 
415
                 * our default is to archive symlinks, not follow
 
416
                 * them.  This is convenient, as -P conflicts with GNU
 
417
                 * tar anyway.
 
418
                 */
 
419
                case 'P': /* BSD convention */
 
420
                        /* Default behavior, no option necessary. */
 
421
                        break;
 
422
#endif
 
423
                case 'P': /* GNU tar */
 
424
                        bsdtar->extract_flags &= ~SECURITY;
 
425
                        bsdtar->option_absolute_paths = 1;
 
426
                        break;
 
427
                case 'p': /* GNU tar, star */
 
428
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
 
429
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
 
430
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
 
431
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
 
432
                        break;
 
433
                case OPTION_POSIX: /* GNU tar */
 
434
                        bsdtar->create_format = "pax";
 
435
                        break;
 
436
                case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
 
437
                        bsdtar->option_fast_read = 1;
 
438
                        break;
 
439
                case 'r': /* SUSv2 */
 
440
                        set_mode(bsdtar, opt);
 
441
                        break;
 
442
                case 'S': /* NetBSD pax-as-tar */
 
443
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
 
444
                        break;
 
445
                case 's': /* NetBSD pax-as-tar */
 
446
#if HAVE_REGEX_H
 
447
                        add_substitution(bsdtar, bsdtar->optarg);
 
448
#else
 
449
                        lafe_warnc(0,
 
450
                            "-s is not supported by this version of bsdtar");
 
451
                        usage();
 
452
#endif
 
453
                        break;
 
454
                case OPTION_SAME_OWNER: /* GNU tar */
 
455
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
 
456
                        break;
 
457
                case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
 
458
                        bsdtar->strip_components = atoi(bsdtar->optarg);
 
459
                        break;
 
460
                case 'T': /* GNU tar */
 
461
                        bsdtar->names_from_file = bsdtar->optarg;
 
462
                        break;
 
463
                case 't': /* SUSv2 */
 
464
                        set_mode(bsdtar, opt);
 
465
                        bsdtar->verbose++;
 
466
                        break;
 
467
                case OPTION_TOTALS: /* GNU tar */
 
468
                        bsdtar->option_totals++;
 
469
                        break;
 
470
                case 'U': /* GNU tar */
 
471
                        bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
 
472
                        bsdtar->option_unlink_first = 1;
 
473
                        break;
 
474
                case 'u': /* SUSv2 */
 
475
                        set_mode(bsdtar, opt);
 
476
                        break;
 
477
                case 'v': /* SUSv2 */
 
478
                        bsdtar->verbose++;
 
479
                        break;
 
480
                case OPTION_VERSION: /* GNU convention */
 
481
                        version();
 
482
                        break;
 
483
#if 0
 
484
                /*
 
485
                 * The -W longopt feature is handled inside of
 
486
                 * bsdtar_getopt(), so -W is not available here.
 
487
                 */
 
488
                case 'W': /* Obscure GNU convention. */
 
489
                        break;
 
490
#endif
 
491
                case 'w': /* SUSv2 */
 
492
                        bsdtar->option_interactive = 1;
 
493
                        break;
 
494
                case 'X': /* GNU tar */
 
495
                        if (lafe_exclude_from_file(&bsdtar->matching, bsdtar->optarg))
 
496
                                lafe_errc(1, 0,
 
497
                                    "failed to process exclusions from file %s",
 
498
                                    bsdtar->optarg);
 
499
                        break;
 
500
                case 'x': /* SUSv2 */
 
501
                        set_mode(bsdtar, opt);
 
502
                        break;
 
503
                case 'y': /* FreeBSD version of GNU tar */
 
504
                        if (bsdtar->create_compression != '\0')
 
505
                                lafe_errc(1, 0,
 
506
                                    "Can't specify both -%c and -%c", opt,
 
507
                                    bsdtar->create_compression);
 
508
                        bsdtar->create_compression = opt;
 
509
                        break;
 
510
                case 'Z': /* GNU tar */
 
511
                        if (bsdtar->create_compression != '\0')
 
512
                                lafe_errc(1, 0,
 
513
                                    "Can't specify both -%c and -%c", opt,
 
514
                                    bsdtar->create_compression);
 
515
                        bsdtar->create_compression = opt;
 
516
                        break;
 
517
                case 'z': /* GNU tar, star, many others */
 
518
                        if (bsdtar->create_compression != '\0')
 
519
                                lafe_errc(1, 0,
 
520
                                    "Can't specify both -%c and -%c", opt,
 
521
                                    bsdtar->create_compression);
 
522
                        bsdtar->create_compression = opt;
 
523
                        break;
 
524
                case OPTION_USE_COMPRESS_PROGRAM:
 
525
                        bsdtar->compress_program = bsdtar->optarg;
 
526
                        break;
 
527
                default:
 
528
                        usage();
 
529
                }
 
530
        }
 
531
 
 
532
        /*
 
533
         * Sanity-check options.
 
534
         */
 
535
 
 
536
        /* If no "real" mode was specified, treat -h as --help. */
 
537
        if ((bsdtar->mode == '\0') && possible_help_request) {
 
538
                long_help();
 
539
                exit(0);
 
540
        }
 
541
 
 
542
        /* Otherwise, a mode is required. */
 
543
        if (bsdtar->mode == '\0')
 
544
                lafe_errc(1, 0,
 
545
                    "Must specify one of -c, -r, -t, -u, -x");
 
546
 
 
547
        /* Check boolean options only permitted in certain modes. */
 
548
        if (bsdtar->option_dont_traverse_mounts)
 
549
                only_mode(bsdtar, "--one-file-system", "cru");
 
550
        if (bsdtar->option_fast_read)
 
551
                only_mode(bsdtar, "--fast-read", "xt");
 
552
        if (bsdtar->option_honor_nodump)
 
553
                only_mode(bsdtar, "--nodump", "cru");
 
554
        if (option_o > 0) {
 
555
                switch (bsdtar->mode) {
 
556
                case 'c':
 
557
                        /*
 
558
                         * In GNU tar, -o means "old format."  The
 
559
                         * "ustar" format is the closest thing
 
560
                         * supported by libarchive.
 
561
                         */
 
562
                        bsdtar->create_format = "ustar";
 
563
                        /* TODO: bsdtar->create_format = "v7"; */
 
564
                        break;
 
565
                case 'x':
 
566
                        /* POSIX-compatible behavior. */
 
567
                        bsdtar->option_no_owner = 1;
 
568
                        bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
 
569
                        break;
 
570
                default:
 
571
                        only_mode(bsdtar, "-o", "xc");
 
572
                        break;
 
573
                }
 
574
        }
 
575
        if (bsdtar->option_no_subdirs)
 
576
                only_mode(bsdtar, "-n", "cru");
 
577
        if (bsdtar->option_stdout)
 
578
                only_mode(bsdtar, "-O", "xt");
 
579
        if (bsdtar->option_unlink_first)
 
580
                only_mode(bsdtar, "-U", "x");
 
581
        if (bsdtar->option_warn_links)
 
582
                only_mode(bsdtar, "--check-links", "cr");
 
583
 
 
584
        /* Check other parameters only permitted in certain modes. */
 
585
        if (bsdtar->create_compression != '\0') {
 
586
                strcpy(buff, "-?");
 
587
                buff[1] = bsdtar->create_compression;
 
588
                only_mode(bsdtar, buff, "cxt");
 
589
        }
 
590
        if (bsdtar->create_format != NULL)
 
591
                only_mode(bsdtar, "--format", "cru");
 
592
        if (bsdtar->symlink_mode != '\0') {
 
593
                strcpy(buff, "-?");
 
594
                buff[1] = bsdtar->symlink_mode;
 
595
                only_mode(bsdtar, buff, "cru");
 
596
        }
 
597
        if (bsdtar->strip_components != 0)
 
598
                only_mode(bsdtar, "--strip-components", "xt");
 
599
 
 
600
        switch(bsdtar->mode) {
 
601
        case 'c':
 
602
                tar_mode_c(bsdtar);
 
603
                break;
 
604
        case 'r':
 
605
                tar_mode_r(bsdtar);
 
606
                break;
 
607
        case 't':
 
608
                tar_mode_t(bsdtar);
 
609
                break;
 
610
        case 'u':
 
611
                tar_mode_u(bsdtar);
 
612
                break;
 
613
        case 'x':
 
614
                tar_mode_x(bsdtar);
 
615
                break;
 
616
        }
 
617
 
 
618
        lafe_cleanup_exclusions(&bsdtar->matching);
 
619
#if HAVE_REGEX_H
 
620
        cleanup_substitution(bsdtar);
 
621
#endif
 
622
 
 
623
        if (bsdtar->return_value != 0)
 
624
                lafe_warnc(0,
 
625
                    "Error exit delayed from previous errors.");
 
626
        return (bsdtar->return_value);
 
627
}
 
628
 
 
629
static void
 
630
set_mode(struct bsdtar *bsdtar, char opt)
 
631
{
 
632
        if (bsdtar->mode != '\0' && bsdtar->mode != opt)
 
633
                lafe_errc(1, 0,
 
634
                    "Can't specify both -%c and -%c", opt, bsdtar->mode);
 
635
        bsdtar->mode = opt;
 
636
}
 
637
 
 
638
/*
 
639
 * Verify that the mode is correct.
 
640
 */
 
641
static void
 
642
only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
 
643
{
 
644
        if (strchr(valid_modes, bsdtar->mode) == NULL)
 
645
                lafe_errc(1, 0,
 
646
                    "Option %s is not permitted in mode -%c",
 
647
                    opt, bsdtar->mode);
 
648
}
 
649
 
 
650
 
 
651
void
 
652
usage(void)
 
653
{
 
654
        const char      *p;
 
655
 
 
656
        p = lafe_progname;
 
657
 
 
658
        fprintf(stderr, "Usage:\n");
 
659
        fprintf(stderr, "  List:    %s -tf <archive-filename>\n", p);
 
660
        fprintf(stderr, "  Extract: %s -xf <archive-filename>\n", p);
 
661
        fprintf(stderr, "  Create:  %s -cf <archive-filename> [filenames...]\n", p);
 
662
        fprintf(stderr, "  Help:    %s --help\n", p);
 
663
        exit(1);
 
664
}
 
665
 
 
666
static void
 
667
version(void)
 
668
{
 
669
        printf("bsdtar %s - %s\n",
 
670
            BSDTAR_VERSION_STRING,
 
671
            archive_version());
 
672
        exit(0);
 
673
}
 
674
 
 
675
static const char *long_help_msg =
 
676
        "First option must be a mode specifier:\n"
 
677
        "  -c Create  -r Add/Replace  -t List  -u Update  -x Extract\n"
 
678
        "Common Options:\n"
 
679
        "  -b #  Use # 512-byte records per I/O block\n"
 
680
        "  -f <filename>  Location of archive (default " _PATH_DEFTAPE ")\n"
 
681
        "  -v    Verbose\n"
 
682
        "  -w    Interactive\n"
 
683
        "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
 
684
        "  <file>, <dir>  add these items to archive\n"
 
685
        "  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma\n"
 
686
        "  --format {ustar|pax|cpio|shar}  Select archive format\n"
 
687
        "  --exclude <pattern>  Skip files that match pattern\n"
 
688
        "  -C <dir>  Change to <dir> before processing remaining files\n"
 
689
        "  @<archive>  Add entries from <archive> to output\n"
 
690
        "List: %p -t [options] [<patterns>]\n"
 
691
        "  <patterns>  If specified, list only entries that match\n"
 
692
        "Extract: %p -x [options] [<patterns>]\n"
 
693
        "  <patterns>  If specified, extract only entries that match\n"
 
694
        "  -k    Keep (don't overwrite) existing files\n"
 
695
        "  -m    Don't restore modification times\n"
 
696
        "  -O    Write entries to stdout, don't restore to disk\n"
 
697
        "  -p    Restore permissions (including ACLs, owner, file flags)\n";
 
698
 
 
699
 
 
700
/*
 
701
 * Note that the word 'bsdtar' will always appear in the first line
 
702
 * of output.
 
703
 *
 
704
 * In particular, /bin/sh scripts that need to test for the presence
 
705
 * of bsdtar can use the following template:
 
706
 *
 
707
 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
 
708
 *          echo bsdtar; else echo not bsdtar; fi
 
709
 */
 
710
static void
 
711
long_help(void)
 
712
{
 
713
        const char      *prog;
 
714
        const char      *p;
 
715
 
 
716
        prog = lafe_progname;
 
717
 
 
718
        fflush(stderr);
 
719
 
 
720
        p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
 
721
        printf("%s%s: manipulate archive files\n", prog, p);
 
722
 
 
723
        for (p = long_help_msg; *p != '\0'; p++) {
 
724
                if (*p == '%') {
 
725
                        if (p[1] == 'p') {
 
726
                                fputs(prog, stdout);
 
727
                                p++;
 
728
                        } else
 
729
                                putchar('%');
 
730
                } else
 
731
                        putchar(*p);
 
732
        }
 
733
        version();
 
734
}