]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/tar/bsdtar.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / libarchive / tar / bsdtar.c
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$");
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_COPYFILE_H
36 #include <copyfile.h>
37 #endif
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #include <fcntl.h>
43 #endif
44 #ifdef HAVE_LANGINFO_H
45 #include <langinfo.h>
46 #endif
47 #ifdef HAVE_LOCALE_H
48 #include <locale.h>
49 #endif
50 #ifdef HAVE_PATHS_H
51 #include <paths.h>
52 #endif
53 #ifdef HAVE_SIGNAL_H
54 #include <signal.h>
55 #endif
56 #include <stdio.h>
57 #ifdef HAVE_STDLIB_H
58 #include <stdlib.h>
59 #endif
60 #ifdef HAVE_STRING_H
61 #include <string.h>
62 #endif
63 #ifdef HAVE_TIME_H
64 #include <time.h>
65 #endif
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.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 #if defined(__APPLE__)
84 #undef _PATH_DEFTAPE
85 #define _PATH_DEFTAPE "-"  /* Mac OS has no tape support, default to stdio. */
86 #endif
87
88 #ifndef _PATH_DEFTAPE
89 #define _PATH_DEFTAPE "/dev/tape"
90 #endif
91
92 #ifdef __MINGW32__
93 int _CRT_glob = 0; /* Disable broken CRT globbing. */
94 #endif
95
96 #if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
97 static volatile int siginfo_occurred;
98
99 static void
100 siginfo_handler(int sig)
101 {
102         (void)sig; /* UNUSED */
103         siginfo_occurred = 1;
104 }
105
106 int
107 need_report(void)
108 {
109         int r = siginfo_occurred;
110         siginfo_occurred = 0;
111         return (r);
112 }
113 #else
114 int
115 need_report(void)
116 {
117         return (0);
118 }
119 #endif
120
121 static void              long_help(void) __LA_DEAD;
122 static void              only_mode(struct bsdtar *, const char *opt,
123                              const char *valid);
124 static void              set_mode(struct bsdtar *, char opt);
125 static void              version(void) __LA_DEAD;
126
127 /* A basic set of security flags to request from libarchive. */
128 #define SECURITY                                        \
129         (ARCHIVE_EXTRACT_SECURE_SYMLINKS                \
130          | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
131
132 static char const * const vcs_files[] = {
133   /* CVS */
134   "CVS", ".cvsignore",
135   /* RCS */
136   "RCS",
137   /* SCCS */
138   "SCCS",
139   /* SVN */
140   ".svn",
141   /* git */
142   ".git", ".gitignore", ".gitattributes", ".gitmodules",
143   /* Arch */
144   ".arch-ids", "{arch}", "=RELEASE-ID", "=meta-update", "=update",
145   /* Bazaar */
146   ".bzr", ".bzrignore", ".bzrtags",
147   /* Mercurial */
148   ".hg", ".hgignore", ".hgtags",
149   /* darcs */
150   "_darcs",
151   NULL
152 };
153
154 int
155 main(int argc, char **argv)
156 {
157         struct bsdtar           *bsdtar, bsdtar_storage;
158         int                      opt, t;
159         char                     compression, compression2;
160         const char              *compression_name, *compression2_name;
161         const char              *compress_program;
162         char                    *tptr;
163         char                     possible_help_request;
164         char                     buff[16];
165
166         /*
167          * Use a pointer for consistency, but stack-allocated storage
168          * for ease of cleanup.
169          */
170         bsdtar = &bsdtar_storage;
171         memset(bsdtar, 0, sizeof(*bsdtar));
172         bsdtar->fd = -1; /* Mark as "unused" */
173         bsdtar->gid = -1;
174         bsdtar->uid = -1;
175         bsdtar->flags = 0;
176         compression = compression2 = '\0';
177         compression_name = compression2_name = NULL;
178         compress_program = NULL;
179
180 #if defined(HAVE_SIGACTION)
181         { /* Set up signal handling. */
182                 struct sigaction sa;
183                 sa.sa_handler = siginfo_handler;
184                 sigemptyset(&sa.sa_mask);
185                 sa.sa_flags = 0;
186 #ifdef SIGINFO
187                 if (sigaction(SIGINFO, &sa, NULL))
188                         lafe_errc(1, errno, "sigaction(SIGINFO) failed");
189 #endif
190 #ifdef SIGUSR1
191                 /* ... and treat SIGUSR1 the same way as SIGINFO. */
192                 if (sigaction(SIGUSR1, &sa, NULL))
193                         lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
194 #endif
195 #ifdef SIGPIPE
196                 /* Ignore SIGPIPE signals. */
197                 sa.sa_handler = SIG_IGN;
198                 sigaction(SIGPIPE, &sa, NULL);
199 #endif
200         }
201 #endif
202
203         /* Set lafe_progname before calling lafe_warnc. */
204         lafe_setprogname(*argv, "bsdtar");
205
206 #if HAVE_SETLOCALE
207         if (setlocale(LC_ALL, "") == NULL)
208                 lafe_warnc(0, "Failed to set default locale");
209 #endif
210 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
211         bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
212 #endif
213         possible_help_request = 0;
214
215         /* Look up uid of current user for future reference */
216         bsdtar->user_uid = geteuid();
217
218         /* Default: open tape drive. */
219         bsdtar->filename = getenv("TAPE");
220         if (bsdtar->filename == NULL)
221                 bsdtar->filename = _PATH_DEFTAPE;
222
223         /* Default block size settings. */
224         bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK;
225         /* Allow library to default this unless user specifies -b. */
226         bsdtar->bytes_in_last_block = -1;
227
228         /* Default: preserve mod time on extract */
229         bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
230
231         /* Default: Perform basic security checks. */
232         bsdtar->extract_flags |= SECURITY;
233
234 #ifndef _WIN32
235         /* On POSIX systems, assume --same-owner and -p when run by
236          * the root user.  This doesn't make any sense on Windows. */
237         if (bsdtar->user_uid == 0) {
238                 /* --same-owner */
239                 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
240                 /* -p */
241                 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
242                 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
243                 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
244                 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
245                 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
246         }
247 #endif
248
249         /*
250          * Enable Mac OS "copyfile()" extension by default.
251          * This has no effect on other platforms.
252          */
253         bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
254 #ifdef COPYFILE_DISABLE_VAR
255         if (getenv(COPYFILE_DISABLE_VAR))
256                 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
257 #endif
258 #if defined(__APPLE__)
259         /*
260          * On Mac OS ACLs are archived with copyfile() (--mac-metadata)
261          * Translation to NFSv4 ACLs has to be requested explicitly with --acls
262          */
263         bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL;
264 #endif
265
266         bsdtar->matching = archive_match_new();
267         if (bsdtar->matching == NULL)
268                 lafe_errc(1, errno, "Out of memory");
269         bsdtar->cset = cset_new();
270         if (bsdtar->cset == NULL)
271                 lafe_errc(1, errno, "Out of memory");
272
273         bsdtar->argv = argv;
274         bsdtar->argc = argc;
275
276         /*
277          * Comments following each option indicate where that option
278          * originated:  SUSv2, POSIX, GNU tar, star, etc.  If there's
279          * no such comment, then I don't know of anyone else who
280          * implements that option.
281          */
282         while ((opt = bsdtar_getopt(bsdtar)) != -1) {
283                 switch (opt) {
284                 case 'a': /* GNU tar */
285                         bsdtar->flags |= OPTFLAG_AUTO_COMPRESS;
286                         break;
287                 case OPTION_ACLS: /* GNU tar */
288                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
289                         bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_ACL;
290                         bsdtar->flags |= OPTFLAG_ACLS;
291                         break;
292                 case 'B': /* GNU tar */
293                         /* libarchive doesn't need this; just ignore it. */
294                         break;
295                 case 'b': /* SUSv2 */
296                         errno = 0;
297                         tptr = NULL;
298                         t = (int)strtol(bsdtar->argument, &tptr, 10);
299                         if (errno || t <= 0 || t > 8192 ||
300                             *(bsdtar->argument) == '\0' || tptr == NULL ||
301                             *tptr != '\0') {
302                                 lafe_errc(1, 0, "Invalid or out of range "
303                                     "(1..8192) argument to -b");
304                         }
305                         bsdtar->bytes_per_block = 512 * t;
306                         /* Explicit -b forces last block size. */
307                         bsdtar->bytes_in_last_block = bsdtar->bytes_per_block;
308                         break;
309                 case OPTION_B64ENCODE:
310                         if (compression2 != '\0')
311                                 lafe_errc(1, 0,
312                                     "Can't specify both --uuencode and "
313                                     "--b64encode");
314                         compression2 = opt;
315                         compression2_name = "b64encode";
316                         break;
317                 case 'C': /* GNU tar */
318                         if (strlen(bsdtar->argument) == 0)
319                                 lafe_errc(1, 0,
320                                     "Meaningless option: -C ''");
321
322                         set_chdir(bsdtar, bsdtar->argument);
323                         break;
324                 case 'c': /* SUSv2 */
325                         set_mode(bsdtar, opt);
326                         break;
327                 case OPTION_CHECK_LINKS: /* GNU tar */
328                         bsdtar->flags |= OPTFLAG_WARN_LINKS;
329                         break;
330                 case OPTION_CHROOT: /* NetBSD */
331                         bsdtar->flags |= OPTFLAG_CHROOT;
332                         break;
333                 case OPTION_CLEAR_NOCHANGE_FFLAGS:
334                         bsdtar->extract_flags |=
335                             ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS;
336                         break;
337                 case OPTION_EXCLUDE: /* GNU tar */
338                         if (archive_match_exclude_pattern(
339                             bsdtar->matching, bsdtar->argument) != ARCHIVE_OK)
340                                 lafe_errc(1, 0,
341                                     "Couldn't exclude %s\n", bsdtar->argument);
342                         break;
343                 case OPTION_EXCLUDE_VCS: /* GNU tar */
344                         for(t=0; vcs_files[t]; t++) {
345                                 if (archive_match_exclude_pattern(
346                                     bsdtar->matching,
347                                     vcs_files[t]) != ARCHIVE_OK)
348                                         lafe_errc(1, 0, "Couldn't "
349                                             "exclude %s\n", vcs_files[t]);
350                         }
351                         break;
352                 case OPTION_FFLAGS:
353                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
354                         bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_FFLAGS;
355                         bsdtar->flags |= OPTFLAG_FFLAGS;
356                         break;
357                 case OPTION_FORMAT: /* GNU tar, others */
358                         cset_set_format(bsdtar->cset, bsdtar->argument);
359                         break;
360                 case 'f': /* SUSv2 */
361                         bsdtar->filename = bsdtar->argument;
362                         break;
363                 case OPTION_GID: /* cpio */
364                         errno = 0;
365                         tptr = NULL;
366                         t = (int)strtol(bsdtar->argument, &tptr, 10);
367                         if (errno || t < 0 || *(bsdtar->argument) == '\0' ||
368                             tptr == NULL || *tptr != '\0') {
369                                 lafe_errc(1, 0, "Invalid argument to --gid");
370                         }
371                         bsdtar->gid = t;
372                         break;
373                 case OPTION_GNAME: /* cpio */
374                         bsdtar->gname = bsdtar->argument;
375                         break;
376                 case OPTION_GRZIP:
377                         if (compression != '\0')
378                                 lafe_errc(1, 0,
379                                     "Can't specify both -%c and -%c", opt,
380                                     compression);
381                         compression = opt;
382                         compression_name = "grzip";
383                         break;
384                 case 'H': /* BSD convention */
385                         bsdtar->symlink_mode = 'H';
386                         break;
387                 case 'h': /* Linux Standards Base, gtar; synonym for -L */
388                         bsdtar->symlink_mode = 'L';
389                         /* Hack: -h by itself is the "help" command. */
390                         possible_help_request = 1;
391                         break;
392                 case OPTION_HELP: /* GNU tar, others */
393                         long_help();
394                         exit(0);
395                         break;
396                 case OPTION_HFS_COMPRESSION: /* Mac OS X v10.6 or later */
397                         bsdtar->extract_flags |=
398                             ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED;
399                         break;
400                 case OPTION_IGNORE_ZEROS:
401                         bsdtar->flags |= OPTFLAG_IGNORE_ZEROS;
402                         break;
403                 case 'I': /* GNU tar */
404                         /*
405                          * TODO: Allow 'names' to come from an archive,
406                          * not just a text file.  Design a good UI for
407                          * allowing names and mode/owner to be read
408                          * from an archive, with contents coming from
409                          * disk.  This can be used to "refresh" an
410                          * archive or to design archives with special
411                          * permissions without having to create those
412                          * permissions on disk.
413                          */
414                         bsdtar->names_from_file = bsdtar->argument;
415                         break;
416                 case OPTION_INCLUDE:
417                         /*
418                          * No one else has the @archive extension, so
419                          * no one else needs this to filter entries
420                          * when transforming archives.
421                          */
422                         if (archive_match_include_pattern(bsdtar->matching,
423                             bsdtar->argument) != ARCHIVE_OK)
424                                 lafe_errc(1, 0,
425                                     "Failed to add %s to inclusion list",
426                                     bsdtar->argument);
427                         break;
428                 case 'j': /* GNU tar */
429                         if (compression != '\0')
430                                 lafe_errc(1, 0,
431                                     "Can't specify both -%c and -%c", opt,
432                                     compression);
433                         compression = opt;
434                         compression_name = "bzip2";
435                         break;
436                 case 'J': /* GNU tar 1.21 and later */
437                         if (compression != '\0')
438                                 lafe_errc(1, 0,
439                                     "Can't specify both -%c and -%c", opt,
440                                     compression);
441                         compression = opt;
442                         compression_name = "xz";
443                         break;
444                 case 'k': /* GNU tar */
445                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
446                         break;
447                 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
448                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
449                         break;
450                 case 'L': /* BSD convention */
451                         bsdtar->symlink_mode = 'L';
452                         break;
453                 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
454                         /* GNU tar 1.13  used -l for --one-file-system */
455                         bsdtar->flags |= OPTFLAG_WARN_LINKS;
456                         break;
457                 case OPTION_LRZIP:
458                 case OPTION_LZ4:
459                 case OPTION_LZIP: /* GNU tar beginning with 1.23 */
460                 case OPTION_LZMA: /* GNU tar beginning with 1.20 */
461                 case OPTION_LZOP: /* GNU tar beginning with 1.21 */
462                 case OPTION_ZSTD:
463                         if (compression != '\0')
464                                 lafe_errc(1, 0,
465                                     "Can't specify both -%c and -%c", opt,
466                                     compression);
467                         compression = opt;
468                         switch (opt) {
469                         case OPTION_LRZIP: compression_name = "lrzip"; break;
470                         case OPTION_LZ4:  compression_name = "lz4"; break;
471                         case OPTION_LZIP: compression_name = "lzip"; break;
472                         case OPTION_LZMA: compression_name = "lzma"; break;
473                         case OPTION_LZOP: compression_name = "lzop"; break;
474                         case OPTION_ZSTD: compression_name = "zstd"; break;
475                         }
476                         break;
477                 case 'm': /* SUSv2 */
478                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
479                         break;
480                 case OPTION_MAC_METADATA: /* Mac OS X */
481                         bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
482                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
483                         bsdtar->flags |= OPTFLAG_MAC_METADATA;
484                         break;
485                 case 'n': /* GNU tar */
486                         bsdtar->flags |= OPTFLAG_NO_SUBDIRS;
487                         break;
488                 /*
489                  * Selecting files by time:
490                  *    --newer-?time='date' Only files newer than 'date'
491                  *    --newer-?time-than='file' Only files newer than time
492                  *         on specified file (useful for incremental backups)
493                  */
494                 case OPTION_NEWER_CTIME: /* GNU tar */
495                         if (archive_match_include_date(bsdtar->matching,
496                             ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
497                             bsdtar->argument) != ARCHIVE_OK)
498                                 lafe_errc(1, 0, "Error : %s",
499                                     archive_error_string(bsdtar->matching));
500                         break;
501                 case OPTION_NEWER_CTIME_THAN:
502                         if (archive_match_include_file_time(bsdtar->matching,
503                             ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
504                             bsdtar->argument) != ARCHIVE_OK)
505                                 lafe_errc(1, 0, "Error : %s",
506                                     archive_error_string(bsdtar->matching));
507                         break;
508                 case OPTION_NEWER_MTIME: /* GNU tar */
509                         if (archive_match_include_date(bsdtar->matching,
510                             ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
511                             bsdtar->argument) != ARCHIVE_OK)
512                                 lafe_errc(1, 0, "Error : %s",
513                                     archive_error_string(bsdtar->matching));
514                         break;
515                 case OPTION_NEWER_MTIME_THAN:
516                         if (archive_match_include_file_time(bsdtar->matching,
517                             ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
518                             bsdtar->argument) != ARCHIVE_OK)
519                                 lafe_errc(1, 0, "Error : %s",
520                                     archive_error_string(bsdtar->matching));
521                         break;
522                 case OPTION_NODUMP: /* star */
523                         bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP;
524                         break;
525                 case OPTION_NOPRESERVE_HFS_COMPRESSION:
526                         /* Mac OS X v10.6 or later */
527                         bsdtar->extract_flags |=
528                             ARCHIVE_EXTRACT_NO_HFS_COMPRESSION;
529                         break;
530                 case OPTION_NO_ACLS: /* GNU tar */
531                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
532                         bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_ACL;
533                         bsdtar->flags |= OPTFLAG_NO_ACLS;
534                         break;
535                 case OPTION_NO_FFLAGS:
536                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
537                         bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_FFLAGS;
538                         bsdtar->flags |= OPTFLAG_NO_FFLAGS;
539                         break;
540                 case OPTION_NO_MAC_METADATA: /* Mac OS X */
541                         bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
542                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
543                         bsdtar->flags |= OPTFLAG_NO_MAC_METADATA;
544                         break;
545                 case OPTION_NO_SAFE_WRITES:
546                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_SAFE_WRITES;
547                         break;
548                 case OPTION_NO_SAME_OWNER: /* GNU tar */
549                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
550                         break;
551                 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
552                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
553                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
554                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
555                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
556                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
557                         break;
558                 case OPTION_NO_XATTRS: /* GNU tar */
559                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
560                         bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_XATTR;
561                         bsdtar->flags |= OPTFLAG_NO_XATTRS;
562                         break;
563                 case OPTION_NULL: /* GNU tar */
564                         bsdtar->flags |= OPTFLAG_NULL;
565                         break;
566                 case OPTION_NUMERIC_OWNER: /* GNU tar */
567                         bsdtar->uname = "";
568                         bsdtar->gname = "";
569                         bsdtar->flags |= OPTFLAG_NUMERIC_OWNER;
570                         break;
571                 case 'O': /* GNU tar */
572                         bsdtar->flags |= OPTFLAG_STDOUT;
573                         break;
574                 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
575                         bsdtar->flags |= OPTFLAG_O;
576                         break;
577                 /*
578                  * Selecting files by time:
579                  *    --older-?time='date' Only files older than 'date'
580                  *    --older-?time-than='file' Only files older than time
581                  *         on specified file
582                  */
583                 case OPTION_OLDER_CTIME:
584                         if (archive_match_include_date(bsdtar->matching,
585                             ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
586                             bsdtar->argument) != ARCHIVE_OK)
587                                 lafe_errc(1, 0, "Error : %s",
588                                     archive_error_string(bsdtar->matching));
589                         break;
590                 case OPTION_OLDER_CTIME_THAN:
591                         if (archive_match_include_file_time(bsdtar->matching,
592                             ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
593                             bsdtar->argument) != ARCHIVE_OK)
594                                 lafe_errc(1, 0, "Error : %s",
595                                     archive_error_string(bsdtar->matching));
596                         break;
597                 case OPTION_OLDER_MTIME:
598                         if (archive_match_include_date(bsdtar->matching,
599                             ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
600                             bsdtar->argument) != ARCHIVE_OK)
601                                 lafe_errc(1, 0, "Error : %s",
602                                     archive_error_string(bsdtar->matching));
603                         break;
604                 case OPTION_OLDER_MTIME_THAN:
605                         if (archive_match_include_file_time(bsdtar->matching,
606                             ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
607                             bsdtar->argument) != ARCHIVE_OK)
608                                 lafe_errc(1, 0, "Error : %s",
609                                     archive_error_string(bsdtar->matching));
610                         break;
611                 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
612                         bsdtar->readdisk_flags |=
613                             ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;
614                         break;
615                 case OPTION_OPTIONS:
616                         bsdtar->option_options = bsdtar->argument;
617                         break;
618 #if 0
619                 /*
620                  * The common BSD -P option is not necessary, since
621                  * our default is to archive symlinks, not follow
622                  * them.  This is convenient, as -P conflicts with GNU
623                  * tar anyway.
624                  */
625                 case 'P': /* BSD convention */
626                         /* Default behavior, no option necessary. */
627                         break;
628 #endif
629                 case 'P': /* GNU tar */
630                         bsdtar->extract_flags &= ~SECURITY;
631                         bsdtar->flags |= OPTFLAG_ABSOLUTE_PATHS;
632                         break;
633                 case 'p': /* GNU tar, star */
634                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
635                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
636                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
637                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
638                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
639                         break;
640                 case OPTION_PASSPHRASE:
641                         bsdtar->passphrase = bsdtar->argument;
642                         break;
643                 case OPTION_POSIX: /* GNU tar */
644                         cset_set_format(bsdtar->cset, "pax");
645                         break;
646                 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
647                         bsdtar->flags |= OPTFLAG_FAST_READ;
648                         break;
649                 case 'r': /* SUSv2 */
650                         set_mode(bsdtar, opt);
651                         break;
652                 case 'S': /* NetBSD pax-as-tar */
653                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
654                         break;
655                 case 's': /* NetBSD pax-as-tar */
656 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
657                         add_substitution(bsdtar, bsdtar->argument);
658 #else
659                         lafe_warnc(0,
660                             "-s is not supported by this version of bsdtar");
661                         usage();
662 #endif
663                         break;
664                 case OPTION_SAFE_WRITES:
665                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_SAFE_WRITES;
666                         break;
667                 case OPTION_SAME_OWNER: /* GNU tar */
668                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
669                         break;
670                 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
671                         errno = 0;
672                         tptr = NULL;
673                         t = (int)strtol(bsdtar->argument, &tptr, 10);
674                         if (errno || t < 0 || *(bsdtar->argument) == '\0' ||
675                             tptr == NULL || *tptr != '\0') {
676                                 lafe_errc(1, 0, "Invalid argument to "
677                                     "--strip-components");
678                         }
679                         bsdtar->strip_components = t;
680                         break;
681                 case 'T': /* GNU tar */
682                         bsdtar->names_from_file = bsdtar->argument;
683                         break;
684                 case 't': /* SUSv2 */
685                         set_mode(bsdtar, opt);
686                         bsdtar->verbose++;
687                         break;
688                 case OPTION_TOTALS: /* GNU tar */
689                         bsdtar->flags |= OPTFLAG_TOTALS;
690                         break;
691                 case 'U': /* GNU tar */
692                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
693                         bsdtar->flags |= OPTFLAG_UNLINK_FIRST;
694                         break;
695                 case 'u': /* SUSv2 */
696                         set_mode(bsdtar, opt);
697                         break;
698                 case OPTION_UID: /* cpio */
699                         errno = 0;
700                         tptr = NULL;
701                         t = (int)strtol(bsdtar->argument, &tptr, 10);
702                         if (errno || t < 0 || *(bsdtar->argument) == '\0' ||
703                             tptr == NULL || *tptr != '\0') {
704                                 lafe_errc(1, 0, "Invalid argument to --uid");
705                         }
706                         bsdtar->uid = t;
707                         break;
708                 case OPTION_UNAME: /* cpio */
709                         bsdtar->uname = bsdtar->argument;
710                         break;
711                 case OPTION_UUENCODE:
712                         if (compression2 != '\0')
713                                 lafe_errc(1, 0,
714                                     "Can't specify both --uuencode and "
715                                     "--b64encode");
716                         compression2 = opt;
717                         compression2_name = "uuencode";
718                         break;
719                 case 'v': /* SUSv2 */
720                         bsdtar->verbose++;
721                         break;
722                 case OPTION_VERSION: /* GNU convention */
723                         version();
724                         break;
725 #if 0
726                 /*
727                  * The -W longopt feature is handled inside of
728                  * bsdtar_getopt(), so -W is not available here.
729                  */
730                 case 'W': /* Obscure GNU convention. */
731                         break;
732 #endif
733                 case 'w': /* SUSv2 */
734                         bsdtar->flags |= OPTFLAG_INTERACTIVE;
735                         break;
736                 case 'X': /* GNU tar */
737                         if (archive_match_exclude_pattern_from_file(
738                             bsdtar->matching, bsdtar->argument, 0)
739                             != ARCHIVE_OK)
740                                 lafe_errc(1, 0, "Error : %s",
741                                     archive_error_string(bsdtar->matching));
742                         break;
743                 case 'x': /* SUSv2 */
744                         set_mode(bsdtar, opt);
745                         break;
746                 case OPTION_XATTRS: /* GNU tar */
747                         bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
748                         bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_NO_XATTR;
749                         bsdtar->flags |= OPTFLAG_XATTRS;
750                         break;
751                 case 'y': /* FreeBSD version of GNU tar */
752                         if (compression != '\0')
753                                 lafe_errc(1, 0,
754                                     "Can't specify both -%c and -%c", opt,
755                                     compression);
756                         compression = opt;
757                         compression_name = "bzip2";
758                         break;
759                 case 'Z': /* GNU tar */
760                         if (compression != '\0')
761                                 lafe_errc(1, 0,
762                                     "Can't specify both -%c and -%c", opt,
763                                     compression);
764                         compression = opt;
765                         compression_name = "compress";
766                         break;
767                 case 'z': /* GNU tar, star, many others */
768                         if (compression != '\0')
769                                 lafe_errc(1, 0,
770                                     "Can't specify both -%c and -%c", opt,
771                                     compression);
772                         compression = opt;
773                         compression_name = "gzip";
774                         break;
775                 case OPTION_USE_COMPRESS_PROGRAM:
776                         compress_program = bsdtar->argument;
777                         break;
778                 default:
779                         usage();
780                 }
781         }
782
783         /*
784          * Sanity-check options.
785          */
786
787         /* If no "real" mode was specified, treat -h as --help. */
788         if ((bsdtar->mode == '\0') && possible_help_request) {
789                 long_help();
790                 exit(0);
791         }
792
793         /* Otherwise, a mode is required. */
794         if (bsdtar->mode == '\0')
795                 lafe_errc(1, 0,
796                     "Must specify one of -c, -r, -t, -u, -x");
797
798         /* Check boolean options only permitted in certain modes. */
799         if (bsdtar->flags & OPTFLAG_AUTO_COMPRESS)
800                 only_mode(bsdtar, "-a", "c");
801         if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
802                 only_mode(bsdtar, "--one-file-system", "cru");
803         if (bsdtar->flags & OPTFLAG_FAST_READ)
804                 only_mode(bsdtar, "--fast-read", "xt");
805         if (bsdtar->extract_flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED)
806                 only_mode(bsdtar, "--hfsCompression", "x");
807         if (bsdtar->extract_flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION)
808                 only_mode(bsdtar, "--nopreserveHFSCompression", "x");
809         if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP)
810                 only_mode(bsdtar, "--nodump", "cru");
811         if (bsdtar->flags & OPTFLAG_ACLS)
812                 only_mode(bsdtar, "--acls", "crux");
813         if (bsdtar->flags & OPTFLAG_NO_ACLS)
814                 only_mode(bsdtar, "--no-acls", "crux");
815         if (bsdtar->flags & OPTFLAG_XATTRS)
816                 only_mode(bsdtar, "--xattrs", "crux");
817         if (bsdtar->flags & OPTFLAG_NO_XATTRS)
818                 only_mode(bsdtar, "--no-xattrs", "crux");
819         if (bsdtar->flags & OPTFLAG_FFLAGS)
820                 only_mode(bsdtar, "--fflags", "crux");
821         if (bsdtar->flags & OPTFLAG_NO_FFLAGS)
822                 only_mode(bsdtar, "--no-fflags", "crux");
823         if (bsdtar->flags & OPTFLAG_MAC_METADATA)
824                 only_mode(bsdtar, "--mac-metadata", "crux");
825         if (bsdtar->flags & OPTFLAG_NO_MAC_METADATA)
826                 only_mode(bsdtar, "--no-mac-metadata", "crux");
827         if (bsdtar->flags & OPTFLAG_O) {
828                 switch (bsdtar->mode) {
829                 case 'c':
830                         /*
831                          * In GNU tar, -o means "old format."  The
832                          * "ustar" format is the closest thing
833                          * supported by libarchive.
834                          */
835                         cset_set_format(bsdtar->cset, "ustar");
836                         /* TODO: bsdtar->create_format = "v7"; */
837                         break;
838                 case 'x':
839                         /* POSIX-compatible behavior. */
840                         bsdtar->flags |= OPTFLAG_NO_OWNER;
841                         bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
842                         break;
843                 default:
844                         only_mode(bsdtar, "-o", "xc");
845                         break;
846                 }
847         }
848         if (bsdtar->flags & OPTFLAG_STDOUT)
849                 only_mode(bsdtar, "-O", "xt");
850         if (bsdtar->flags & OPTFLAG_UNLINK_FIRST)
851                 only_mode(bsdtar, "-U", "x");
852         if (bsdtar->flags & OPTFLAG_WARN_LINKS)
853                 only_mode(bsdtar, "--check-links", "cr");
854
855         if ((bsdtar->flags & OPTFLAG_AUTO_COMPRESS) &&
856             cset_auto_compress(bsdtar->cset, bsdtar->filename)) {
857                 /* Ignore specified compressions if auto-compress works. */
858                 compression = '\0';
859                 compression2 = '\0';
860         }
861         /* Check other parameters only permitted in certain modes. */
862         if (compress_program != NULL) {
863                 only_mode(bsdtar, "--use-compress-program", "cxt");
864                 cset_add_filter_program(bsdtar->cset, compress_program);
865                 /* Ignore specified compressions. */
866                 compression = '\0';
867                 compression2 = '\0';
868         }
869         if (compression != '\0') {
870                 switch (compression) {
871                 case 'J': case 'j': case 'y': case 'Z': case 'z':
872                         strcpy(buff, "-?");
873                         buff[1] = compression;
874                         break;
875                 default:
876                         strcpy(buff, "--");
877                         strcat(buff, compression_name);
878                         break;
879                 }
880                 only_mode(bsdtar, buff, "cxt");
881                 cset_add_filter(bsdtar->cset, compression_name);
882         }
883         if (compression2 != '\0') {
884                 strcpy(buff, "--");
885                 strcat(buff, compression2_name);
886                 only_mode(bsdtar, buff, "cxt");
887                 cset_add_filter(bsdtar->cset, compression2_name);
888         }
889         if (cset_get_format(bsdtar->cset) != NULL)
890                 only_mode(bsdtar, "--format", "cru");
891         if (bsdtar->symlink_mode != '\0') {
892                 strcpy(buff, "-?");
893                 buff[1] = bsdtar->symlink_mode;
894                 only_mode(bsdtar, buff, "cru");
895         }
896
897         /*
898          * When creating an archive from a directory tree, the directory
899          * walking code will already avoid entering directories when
900          * recursive inclusion of directory content is disabled, therefore
901          * changing the matching behavior has no effect for creation modes.
902          * It is relevant for extraction or listing.
903          */
904         archive_match_set_inclusion_recursion(bsdtar->matching,
905                                               !(bsdtar->flags & OPTFLAG_NO_SUBDIRS));
906
907         /* Filename "-" implies stdio. */
908         if (strcmp(bsdtar->filename, "-") == 0)
909                 bsdtar->filename = NULL;
910
911         switch(bsdtar->mode) {
912         case 'c':
913                 tar_mode_c(bsdtar);
914                 break;
915         case 'r':
916                 tar_mode_r(bsdtar);
917                 break;
918         case 't':
919                 tar_mode_t(bsdtar);
920                 break;
921         case 'u':
922                 tar_mode_u(bsdtar);
923                 break;
924         case 'x':
925                 tar_mode_x(bsdtar);
926                 break;
927         }
928
929         archive_match_free(bsdtar->matching);
930 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
931         cleanup_substitution(bsdtar);
932 #endif
933         cset_free(bsdtar->cset);
934         passphrase_free(bsdtar->ppbuff);
935
936         if (bsdtar->return_value != 0)
937                 lafe_warnc(0,
938                     "Error exit delayed from previous errors.");
939         return (bsdtar->return_value);
940 }
941
942 static void
943 set_mode(struct bsdtar *bsdtar, char opt)
944 {
945         if (bsdtar->mode != '\0' && bsdtar->mode != opt)
946                 lafe_errc(1, 0,
947                     "Can't specify both -%c and -%c", opt, bsdtar->mode);
948         bsdtar->mode = opt;
949 }
950
951 /*
952  * Verify that the mode is correct.
953  */
954 static void
955 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
956 {
957         if (strchr(valid_modes, bsdtar->mode) == NULL)
958                 lafe_errc(1, 0,
959                     "Option %s is not permitted in mode -%c",
960                     opt, bsdtar->mode);
961 }
962
963
964 void
965 usage(void)
966 {
967         const char      *p;
968
969         p = lafe_getprogname();
970
971         fprintf(stderr, "Usage:\n");
972         fprintf(stderr, "  List:    %s -tf <archive-filename>\n", p);
973         fprintf(stderr, "  Extract: %s -xf <archive-filename>\n", p);
974         fprintf(stderr, "  Create:  %s -cf <archive-filename> [filenames...]\n", p);
975         fprintf(stderr, "  Help:    %s --help\n", p);
976         exit(1);
977 }
978
979 static void
980 version(void)
981 {
982         printf("bsdtar %s - %s \n",
983             BSDTAR_VERSION_STRING,
984             archive_version_details());
985         exit(0);
986 }
987
988 static const char *long_help_msg =
989         "First option must be a mode specifier:\n"
990         "  -c Create  -r Add/Replace  -t List  -u Update  -x Extract\n"
991         "Common Options:\n"
992         "  -b #  Use # 512-byte records per I/O block\n"
993         "  -f <filename>  Location of archive (default " _PATH_DEFTAPE ")\n"
994         "  -v    Verbose\n"
995         "  -w    Interactive\n"
996         "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
997         "  <file>, <dir>  add these items to archive\n"
998         "  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma\n"
999         "  --format {ustar|pax|cpio|shar}  Select archive format\n"
1000         "  --exclude <pattern>  Skip files that match pattern\n"
1001         "  -C <dir>  Change to <dir> before processing remaining files\n"
1002         "  @<archive>  Add entries from <archive> to output\n"
1003         "List: %p -t [options] [<patterns>]\n"
1004         "  <patterns>  If specified, list only entries that match\n"
1005         "Extract: %p -x [options] [<patterns>]\n"
1006         "  <patterns>  If specified, extract only entries that match\n"
1007         "  -k    Keep (don't overwrite) existing files\n"
1008         "  -m    Don't restore modification times\n"
1009         "  -O    Write entries to stdout, don't restore to disk\n"
1010         "  -p    Restore permissions (including ACLs, owner, file flags)\n";
1011
1012
1013 /*
1014  * Note that the word 'bsdtar' will always appear in the first line
1015  * of output.
1016  *
1017  * In particular, /bin/sh scripts that need to test for the presence
1018  * of bsdtar can use the following template:
1019  *
1020  * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
1021  *          echo bsdtar; else echo not bsdtar; fi
1022  */
1023 static void
1024 long_help(void)
1025 {
1026         const char      *prog;
1027         const char      *p;
1028
1029         prog = lafe_getprogname();
1030
1031         fflush(stderr);
1032
1033         p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
1034         printf("%s%s: manipulate archive files\n", prog, p);
1035
1036         for (p = long_help_msg; *p != '\0'; p++) {
1037                 if (*p == '%') {
1038                         if (p[1] == 'p') {
1039                                 fputs(prog, stdout);
1040                                 p++;
1041                         } else
1042                                 putchar('%');
1043                 } else
1044                         putchar(*p);
1045         }
1046         version();
1047 }