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