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