]> Dogcows Code - chaz/tar/blob - src/tar.c
tar: imply --xattrs when --xattrs-{inc,exc}lude used
[chaz/tar] / src / tar.c
1 /* A tar (tape archiver) program.
2
3 Copyright 1988, 1992-1997, 1999-2001, 2003-2007, 2012-2014 Free
4 Software Foundation, Inc.
5
6 Written by John Gilmore, starting 1985-08-25.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <system.h>
22
23 #include <fnmatch.h>
24 #include <argp.h>
25 #include <argp-namefrob.h>
26 #include <argp-fmtstream.h>
27 #include <argp-version-etc.h>
28
29 #include <signal.h>
30 #if ! defined SIGCHLD && defined SIGCLD
31 # define SIGCHLD SIGCLD
32 #endif
33
34 /* The following causes "common.h" to produce definitions of all the global
35 variables, rather than just "extern" declarations of them. GNU tar does
36 depend on the system loader to preset all GLOBAL variables to neutral (or
37 zero) values; explicit initialization is usually not done. */
38 #define GLOBAL
39 #include "common.h"
40
41 #include <argmatch.h>
42 #include <closeout.h>
43 #include <configmake.h>
44 #include <exitfail.h>
45 #include <parse-datetime.h>
46 #include <rmt.h>
47 #include <rmt-command.h>
48 #include <prepargs.h>
49 #include <quotearg.h>
50 #include <version-etc.h>
51 #include <xstrtol.h>
52 #include <stdopen.h>
53 #include <priv-set.h>
54 #include <savedir.h>
55
56 /* Local declarations. */
57
58 #ifndef DEFAULT_ARCHIVE_FORMAT
59 # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
60 #endif
61
62 #ifndef DEFAULT_ARCHIVE
63 # define DEFAULT_ARCHIVE "tar.out"
64 #endif
65
66 #ifndef DEFAULT_BLOCKING
67 # define DEFAULT_BLOCKING 20
68 #endif
69
70 /* Print a message if not all links are dumped */
71 static int check_links_option;
72
73 /* Number of allocated tape drive names. */
74 static size_t allocated_archive_names;
75
76 \f
77 /* Miscellaneous. */
78
79 /* Name of option using stdin. */
80 static const char *stdin_used_by;
81
82 /* Doesn't return if stdin already requested. */
83 void
84 request_stdin (const char *option)
85 {
86 if (stdin_used_by)
87 USAGE_ERROR ((0, 0, _("Options '%s' and '%s' both want standard input"),
88 stdin_used_by, option));
89
90 stdin_used_by = option;
91 }
92
93 extern int rpmatch (char const *response);
94
95 /* Returns true if and only if the user typed an affirmative response. */
96 int
97 confirm (const char *message_action, const char *message_name)
98 {
99 static FILE *confirm_file;
100 static int confirm_file_EOF;
101 bool status = false;
102
103 if (!confirm_file)
104 {
105 if (archive == 0 || stdin_used_by)
106 {
107 confirm_file = fopen (TTY_NAME, "r");
108 if (! confirm_file)
109 open_fatal (TTY_NAME);
110 }
111 else
112 {
113 request_stdin ("-w");
114 confirm_file = stdin;
115 }
116 }
117
118 fprintf (stdlis, "%s %s?", message_action, quote (message_name));
119 fflush (stdlis);
120
121 if (!confirm_file_EOF)
122 {
123 char *response = NULL;
124 size_t response_size = 0;
125 if (getline (&response, &response_size, confirm_file) < 0)
126 confirm_file_EOF = 1;
127 else
128 status = rpmatch (response) > 0;
129 free (response);
130 }
131
132 if (confirm_file_EOF)
133 {
134 fputc ('\n', stdlis);
135 fflush (stdlis);
136 }
137
138 return status;
139 }
140
141 static struct fmttab {
142 char const *name;
143 enum archive_format fmt;
144 } const fmttab[] = {
145 { "v7", V7_FORMAT },
146 { "oldgnu", OLDGNU_FORMAT },
147 { "ustar", USTAR_FORMAT },
148 { "posix", POSIX_FORMAT },
149 #if 0 /* not fully supported yet */
150 { "star", STAR_FORMAT },
151 #endif
152 { "gnu", GNU_FORMAT },
153 { "pax", POSIX_FORMAT }, /* An alias for posix */
154 { NULL, 0 }
155 };
156
157 static void
158 set_archive_format (char const *name)
159 {
160 struct fmttab const *p;
161
162 for (p = fmttab; strcmp (p->name, name) != 0; )
163 if (! (++p)->name)
164 USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
165 quotearg_colon (name)));
166
167 archive_format = p->fmt;
168 }
169
170 static void
171 set_xattr_option (int value)
172 {
173 if (value == 1)
174 set_archive_format ("posix");
175 xattrs_option = value;
176 }
177
178 const char *
179 archive_format_string (enum archive_format fmt)
180 {
181 struct fmttab const *p;
182
183 for (p = fmttab; p->name; p++)
184 if (p->fmt == fmt)
185 return p->name;
186 return "unknown?";
187 }
188
189 #define FORMAT_MASK(n) (1<<(n))
190
191 static void
192 assert_format(unsigned fmt_mask)
193 {
194 if ((FORMAT_MASK (archive_format) & fmt_mask) == 0)
195 USAGE_ERROR ((0, 0,
196 _("GNU features wanted on incompatible archive format")));
197 }
198
199 const char *
200 subcommand_string (enum subcommand c)
201 {
202 switch (c)
203 {
204 case UNKNOWN_SUBCOMMAND:
205 return "unknown?";
206
207 case APPEND_SUBCOMMAND:
208 return "-r";
209
210 case CAT_SUBCOMMAND:
211 return "-A";
212
213 case CREATE_SUBCOMMAND:
214 return "-c";
215
216 case DELETE_SUBCOMMAND:
217 return "-D";
218
219 case DIFF_SUBCOMMAND:
220 return "-d";
221
222 case EXTRACT_SUBCOMMAND:
223 return "-x";
224
225 case LIST_SUBCOMMAND:
226 return "-t";
227
228 case UPDATE_SUBCOMMAND:
229 return "-u";
230
231 case TEST_LABEL_SUBCOMMAND:
232 return "--test-label";
233 }
234 abort ();
235 }
236
237 static void
238 tar_list_quoting_styles (struct obstack *stk, char const *prefix)
239 {
240 int i;
241 size_t prefixlen = strlen (prefix);
242
243 for (i = 0; quoting_style_args[i]; i++)
244 {
245 obstack_grow (stk, prefix, prefixlen);
246 obstack_grow (stk, quoting_style_args[i],
247 strlen (quoting_style_args[i]));
248 obstack_1grow (stk, '\n');
249 }
250 }
251
252 static void
253 tar_set_quoting_style (char *arg)
254 {
255 int i;
256
257 for (i = 0; quoting_style_args[i]; i++)
258 if (strcmp (arg, quoting_style_args[i]) == 0)
259 {
260 set_quoting_style (NULL, i);
261 return;
262 }
263 FATAL_ERROR ((0, 0,
264 _("Unknown quoting style '%s'. Try '%s --quoting-style=help' to get a list."), arg, program_invocation_short_name));
265 }
266
267 \f
268 /* Options. */
269
270 enum
271 {
272 ACLS_OPTION = CHAR_MAX + 1,
273 ANCHORED_OPTION,
274 ATIME_PRESERVE_OPTION,
275 BACKUP_OPTION,
276 CHECK_DEVICE_OPTION,
277 CHECKPOINT_OPTION,
278 CHECKPOINT_ACTION_OPTION,
279 DELAY_DIRECTORY_RESTORE_OPTION,
280 HARD_DEREFERENCE_OPTION,
281 DELETE_OPTION,
282 EXCLUDE_BACKUPS_OPTION,
283 EXCLUDE_CACHES_OPTION,
284 EXCLUDE_CACHES_UNDER_OPTION,
285 EXCLUDE_CACHES_ALL_OPTION,
286 EXCLUDE_OPTION,
287 EXCLUDE_TAG_OPTION,
288 EXCLUDE_TAG_UNDER_OPTION,
289 EXCLUDE_TAG_ALL_OPTION,
290 EXCLUDE_VCS_OPTION,
291 FORCE_LOCAL_OPTION,
292 FULL_TIME_OPTION,
293 GROUP_OPTION,
294 IGNORE_CASE_OPTION,
295 IGNORE_COMMAND_ERROR_OPTION,
296 IGNORE_FAILED_READ_OPTION,
297 INDEX_FILE_OPTION,
298 KEEP_DIRECTORY_SYMLINK_OPTION,
299 KEEP_NEWER_FILES_OPTION,
300 LEVEL_OPTION,
301 LZIP_OPTION,
302 LZMA_OPTION,
303 LZOP_OPTION,
304 MODE_OPTION,
305 MTIME_OPTION,
306 NEWER_MTIME_OPTION,
307 NO_ACLS_OPTION,
308 NO_ANCHORED_OPTION,
309 NO_AUTO_COMPRESS_OPTION,
310 NO_CHECK_DEVICE_OPTION,
311 NO_DELAY_DIRECTORY_RESTORE_OPTION,
312 NO_IGNORE_CASE_OPTION,
313 NO_IGNORE_COMMAND_ERROR_OPTION,
314 NO_NULL_OPTION,
315 NO_OVERWRITE_DIR_OPTION,
316 NO_QUOTE_CHARS_OPTION,
317 NO_RECURSION_OPTION,
318 NO_SAME_OWNER_OPTION,
319 NO_SAME_PERMISSIONS_OPTION,
320 NO_SEEK_OPTION,
321 NO_SELINUX_CONTEXT_OPTION,
322 NO_UNQUOTE_OPTION,
323 NO_WILDCARDS_MATCH_SLASH_OPTION,
324 NO_WILDCARDS_OPTION,
325 NO_XATTR_OPTION,
326 NULL_OPTION,
327 NUMERIC_OWNER_OPTION,
328 OCCURRENCE_OPTION,
329 OLD_ARCHIVE_OPTION,
330 ONE_FILE_SYSTEM_OPTION,
331 ONE_TOP_LEVEL_OPTION,
332 OVERWRITE_DIR_OPTION,
333 OVERWRITE_OPTION,
334 OWNER_OPTION,
335 PAX_OPTION,
336 POSIX_OPTION,
337 PRESERVE_OPTION,
338 QUOTE_CHARS_OPTION,
339 QUOTING_STYLE_OPTION,
340 RECORD_SIZE_OPTION,
341 RECURSION_OPTION,
342 RECURSIVE_UNLINK_OPTION,
343 REMOVE_FILES_OPTION,
344 RESTRICT_OPTION,
345 RMT_COMMAND_OPTION,
346 RSH_COMMAND_OPTION,
347 SAME_OWNER_OPTION,
348 SELINUX_CONTEXT_OPTION,
349 SHOW_DEFAULTS_OPTION,
350 SHOW_OMITTED_DIRS_OPTION,
351 SHOW_SNAPSHOT_FIELD_RANGES_OPTION,
352 SHOW_TRANSFORMED_NAMES_OPTION,
353 SKIP_OLD_FILES_OPTION,
354 SORT_OPTION,
355 SPARSE_VERSION_OPTION,
356 STRIP_COMPONENTS_OPTION,
357 SUFFIX_OPTION,
358 TEST_LABEL_OPTION,
359 TOTALS_OPTION,
360 TO_COMMAND_OPTION,
361 TRANSFORM_OPTION,
362 UNQUOTE_OPTION,
363 UTC_OPTION,
364 VOLNO_FILE_OPTION,
365 WARNING_OPTION,
366 WILDCARDS_MATCH_SLASH_OPTION,
367 WILDCARDS_OPTION,
368 XATTR_OPTION,
369 XATTR_EXCLUDE,
370 XATTR_INCLUDE
371 };
372
373 const char *argp_program_version = "tar (" PACKAGE_NAME ") " VERSION;
374 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
375 static char const doc[] = N_("\
376 GNU 'tar' saves many files together into a single tape or disk archive, \
377 and can restore individual files from the archive.\n\
378 \n\
379 Examples:\n\
380 tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
381 tar -tvf archive.tar # List all files in archive.tar verbosely.\n\
382 tar -xf archive.tar # Extract all files from archive.tar.\n")
383 "\v"
384 N_("The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
385 The version control may be set with --backup or VERSION_CONTROL, values are:\n\n\
386 none, off never make backups\n\
387 t, numbered make numbered backups\n\
388 nil, existing numbered if numbered backups exist, simple otherwise\n\
389 never, simple always make simple backups\n");
390
391
392 /* NOTE:
393
394 Available option letters are DEQY and eqy. Consider the following
395 assignments:
396
397 [For Solaris tar compatibility =/= Is it important at all?]
398 e exit immediately with a nonzero exit status if unexpected errors occur
399 E use extended headers (--format=posix)
400
401 [q alias for --occurrence=1 =/= this would better be used for quiet?]
402
403 y per-file gzip compression
404 Y per-block gzip compression.
405
406 Additionally, the 'n' letter is assigned for option --seek, which
407 is probably not needed and should be marked as deprecated, so that
408 -n may become available in the future.
409 */
410
411 static struct argp_option options[] = {
412 #define GRID 10
413 {NULL, 0, NULL, 0,
414 N_("Main operation mode:"), GRID },
415
416 {"list", 't', 0, 0,
417 N_("list the contents of an archive"), GRID+1 },
418 {"extract", 'x', 0, 0,
419 N_("extract files from an archive"), GRID+1 },
420 {"get", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
421 {"create", 'c', 0, 0,
422 N_("create a new archive"), GRID+1 },
423 {"diff", 'd', 0, 0,
424 N_("find differences between archive and file system"), GRID+1 },
425 {"compare", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
426 {"append", 'r', 0, 0,
427 N_("append files to the end of an archive"), GRID+1 },
428 {"update", 'u', 0, 0,
429 N_("only append files newer than copy in archive"), GRID+1 },
430 {"catenate", 'A', 0, 0,
431 N_("append tar files to an archive"), GRID+1 },
432 {"concatenate", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
433 {"delete", DELETE_OPTION, 0, 0,
434 N_("delete from the archive (not on mag tapes!)"), GRID+1 },
435 {"test-label", TEST_LABEL_OPTION, NULL, 0,
436 N_("test the archive volume label and exit"), GRID+1 },
437 #undef GRID
438
439 #define GRID 20
440 {NULL, 0, NULL, 0,
441 N_("Operation modifiers:"), GRID },
442
443 {"sparse", 'S', 0, 0,
444 N_("handle sparse files efficiently"), GRID+1 },
445 {"sparse-version", SPARSE_VERSION_OPTION, N_("MAJOR[.MINOR]"), 0,
446 N_("set version of the sparse format to use (implies --sparse)"), GRID+1},
447 {"incremental", 'G', 0, 0,
448 N_("handle old GNU-format incremental backup"), GRID+1 },
449 {"listed-incremental", 'g', N_("FILE"), 0,
450 N_("handle new GNU-format incremental backup"), GRID+1 },
451 {"level", LEVEL_OPTION, N_("NUMBER"), 0,
452 N_("dump level for created listed-incremental archive"), GRID+1 },
453 {"ignore-failed-read", IGNORE_FAILED_READ_OPTION, 0, 0,
454 N_("do not exit with nonzero on unreadable files"), GRID+1 },
455 {"occurrence", OCCURRENCE_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,
456 N_("process only the NUMBERth occurrence of each file in the archive;"
457 " this option is valid only in conjunction with one of the subcommands"
458 " --delete, --diff, --extract or --list and when a list of files"
459 " is given either on the command line or via the -T option;"
460 " NUMBER defaults to 1"), GRID+1 },
461 {"seek", 'n', NULL, 0,
462 N_("archive is seekable"), GRID+1 },
463 {"no-seek", NO_SEEK_OPTION, NULL, 0,
464 N_("archive is not seekable"), GRID+1 },
465 {"no-check-device", NO_CHECK_DEVICE_OPTION, NULL, 0,
466 N_("do not check device numbers when creating incremental archives"),
467 GRID+1 },
468 {"check-device", CHECK_DEVICE_OPTION, NULL, 0,
469 N_("check device numbers when creating incremental archives (default)"),
470 GRID+1 },
471 #undef GRID
472
473 #define GRID 30
474 {NULL, 0, NULL, 0,
475 N_("Overwrite control:"), GRID },
476
477 {"verify", 'W', 0, 0,
478 N_("attempt to verify the archive after writing it"), GRID+1 },
479 {"remove-files", REMOVE_FILES_OPTION, 0, 0,
480 N_("remove files after adding them to the archive"), GRID+1 },
481 {"keep-old-files", 'k', 0, 0,
482 N_("don't replace existing files when extracting, "
483 "treat them as errors"), GRID+1 },
484 {"skip-old-files", SKIP_OLD_FILES_OPTION, 0, 0,
485 N_("don't replace existing files when extracting, silently skip over them"),
486 GRID+1 },
487 {"keep-newer-files", KEEP_NEWER_FILES_OPTION, 0, 0,
488 N_("don't replace existing files that are newer than their archive copies"), GRID+1 },
489 {"overwrite", OVERWRITE_OPTION, 0, 0,
490 N_("overwrite existing files when extracting"), GRID+1 },
491 {"unlink-first", 'U', 0, 0,
492 N_("remove each file prior to extracting over it"), GRID+1 },
493 {"recursive-unlink", RECURSIVE_UNLINK_OPTION, 0, 0,
494 N_("empty hierarchies prior to extracting directory"), GRID+1 },
495 {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION, 0, 0,
496 N_("preserve metadata of existing directories"), GRID+1 },
497 {"overwrite-dir", OVERWRITE_DIR_OPTION, 0, 0,
498 N_("overwrite metadata of existing directories when extracting (default)"),
499 GRID+1 },
500 {"keep-directory-symlink", KEEP_DIRECTORY_SYMLINK_OPTION, 0, 0,
501 N_("preserve existing symlinks to directories when extracting"),
502 GRID+1 },
503 {"one-top-level", ONE_TOP_LEVEL_OPTION, N_("DIR"), OPTION_ARG_OPTIONAL,
504 N_("create a subdirectory to avoid having loose files extracted"),
505 GRID+1 },
506 #undef GRID
507
508 #define GRID 40
509 {NULL, 0, NULL, 0,
510 N_("Select output stream:"), GRID },
511
512 {"to-stdout", 'O', 0, 0,
513 N_("extract files to standard output"), GRID+1 },
514 {"to-command", TO_COMMAND_OPTION, N_("COMMAND"), 0,
515 N_("pipe extracted files to another program"), GRID+1 },
516 {"ignore-command-error", IGNORE_COMMAND_ERROR_OPTION, 0, 0,
517 N_("ignore exit codes of children"), GRID+1 },
518 {"no-ignore-command-error", NO_IGNORE_COMMAND_ERROR_OPTION, 0, 0,
519 N_("treat non-zero exit codes of children as error"), GRID+1 },
520 #undef GRID
521
522 #define GRID 50
523 {NULL, 0, NULL, 0,
524 N_("Handling of file attributes:"), GRID },
525
526 {"owner", OWNER_OPTION, N_("NAME"), 0,
527 N_("force NAME as owner for added files"), GRID+1 },
528 {"group", GROUP_OPTION, N_("NAME"), 0,
529 N_("force NAME as group for added files"), GRID+1 },
530 {"mtime", MTIME_OPTION, N_("DATE-OR-FILE"), 0,
531 N_("set mtime for added files from DATE-OR-FILE"), GRID+1 },
532 {"mode", MODE_OPTION, N_("CHANGES"), 0,
533 N_("force (symbolic) mode CHANGES for added files"), GRID+1 },
534 {"atime-preserve", ATIME_PRESERVE_OPTION,
535 N_("METHOD"), OPTION_ARG_OPTIONAL,
536 N_("preserve access times on dumped files, either by restoring the times"
537 " after reading (METHOD='replace'; default) or by not setting the times"
538 " in the first place (METHOD='system')"), GRID+1 },
539 {"touch", 'm', 0, 0,
540 N_("don't extract file modified time"), GRID+1 },
541 {"same-owner", SAME_OWNER_OPTION, 0, 0,
542 N_("try extracting files with the same ownership as exists in the archive (default for superuser)"), GRID+1 },
543 {"no-same-owner", NO_SAME_OWNER_OPTION, 0, 0,
544 N_("extract files as yourself (default for ordinary users)"), GRID+1 },
545 {"numeric-owner", NUMERIC_OWNER_OPTION, 0, 0,
546 N_("always use numbers for user/group names"), GRID+1 },
547 {"preserve-permissions", 'p', 0, 0,
548 N_("extract information about file permissions (default for superuser)"),
549 GRID+1 },
550 {"same-permissions", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
551 {"no-same-permissions", NO_SAME_PERMISSIONS_OPTION, 0, 0,
552 N_("apply the user's umask when extracting permissions from the archive (default for ordinary users)"), GRID+1 },
553 {"preserve-order", 's', 0, 0,
554 N_("member arguments are listed in the same order as the "
555 "files in the archive"), GRID+1 },
556 {"same-order", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
557 {"preserve", PRESERVE_OPTION, 0, 0,
558 N_("same as both -p and -s"), GRID+1 },
559 {"delay-directory-restore", DELAY_DIRECTORY_RESTORE_OPTION, 0, 0,
560 N_("delay setting modification times and permissions of extracted"
561 " directories until the end of extraction"), GRID+1 },
562 {"no-delay-directory-restore", NO_DELAY_DIRECTORY_RESTORE_OPTION, 0, 0,
563 N_("cancel the effect of --delay-directory-restore option"), GRID+1 },
564 {"sort", SORT_OPTION, N_("ORDER"), 0,
565 #if D_INO_IN_DIRENT
566 N_("directory sorting order: none (default), name or inode"
567 #else
568 N_("directory sorting order: none (default) or name"
569 #endif
570 ), GRID+1 },
571 #undef GRID
572
573 #define GRID 55
574 {NULL, 0, NULL, 0,
575 N_("Handling of extended file attributes:"), GRID },
576
577 {"xattrs", XATTR_OPTION, 0, 0,
578 N_("Enable extended attributes support"), GRID+1 },
579 {"no-xattrs", NO_XATTR_OPTION, 0, 0,
580 N_("Disable extended attributes support"), GRID+1 },
581 {"xattrs-include", XATTR_INCLUDE, N_("MASK"), 0,
582 N_("specify the include pattern for xattr keys"), GRID+1 },
583 {"xattrs-exclude", XATTR_EXCLUDE, N_("MASK"), 0,
584 N_("specify the exclude pattern for xattr keys"), GRID+1 },
585 {"selinux", SELINUX_CONTEXT_OPTION, 0, 0,
586 N_("Enable the SELinux context support"), GRID+1 },
587 {"no-selinux", NO_SELINUX_CONTEXT_OPTION, 0, 0,
588 N_("Disable the SELinux context support"), GRID+1 },
589 {"acls", ACLS_OPTION, 0, 0,
590 N_("Enable the POSIX ACLs support"), GRID+1 },
591 {"no-acls", NO_ACLS_OPTION, 0, 0,
592 N_("Disable the POSIX ACLs support"), GRID+1 },
593 #undef GRID
594
595 #define GRID 60
596 {NULL, 0, NULL, 0,
597 N_("Device selection and switching:"), GRID },
598
599 {"file", 'f', N_("ARCHIVE"), 0,
600 N_("use archive file or device ARCHIVE"), GRID+1 },
601 {"force-local", FORCE_LOCAL_OPTION, 0, 0,
602 N_("archive file is local even if it has a colon"), GRID+1 },
603 {"rmt-command", RMT_COMMAND_OPTION, N_("COMMAND"), 0,
604 N_("use given rmt COMMAND instead of rmt"), GRID+1 },
605 {"rsh-command", RSH_COMMAND_OPTION, N_("COMMAND"), 0,
606 N_("use remote COMMAND instead of rsh"), GRID+1 },
607 #ifdef DEVICE_PREFIX
608 {"-[0-7][lmh]", 0, NULL, OPTION_DOC, /* It is OK, since 'name' will never be
609 translated */
610 N_("specify drive and density"), GRID+1 },
611 #endif
612 {NULL, '0', NULL, OPTION_HIDDEN, NULL, GRID+1 },
613 {NULL, '1', NULL, OPTION_HIDDEN, NULL, GRID+1 },
614 {NULL, '2', NULL, OPTION_HIDDEN, NULL, GRID+1 },
615 {NULL, '3', NULL, OPTION_HIDDEN, NULL, GRID+1 },
616 {NULL, '4', NULL, OPTION_HIDDEN, NULL, GRID+1 },
617 {NULL, '5', NULL, OPTION_HIDDEN, NULL, GRID+1 },
618 {NULL, '6', NULL, OPTION_HIDDEN, NULL, GRID+1 },
619 {NULL, '7', NULL, OPTION_HIDDEN, NULL, GRID+1 },
620 {NULL, '8', NULL, OPTION_HIDDEN, NULL, GRID+1 },
621 {NULL, '9', NULL, OPTION_HIDDEN, NULL, GRID+1 },
622
623 {"multi-volume", 'M', 0, 0,
624 N_("create/list/extract multi-volume archive"), GRID+1 },
625 {"tape-length", 'L', N_("NUMBER"), 0,
626 N_("change tape after writing NUMBER x 1024 bytes"), GRID+1 },
627 {"info-script", 'F', N_("NAME"), 0,
628 N_("run script at end of each tape (implies -M)"), GRID+1 },
629 {"new-volume-script", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
630 {"volno-file", VOLNO_FILE_OPTION, N_("FILE"), 0,
631 N_("use/update the volume number in FILE"), GRID+1 },
632 #undef GRID
633
634 #define GRID 70
635 {NULL, 0, NULL, 0,
636 N_("Device blocking:"), GRID },
637
638 {"blocking-factor", 'b', N_("BLOCKS"), 0,
639 N_("BLOCKS x 512 bytes per record"), GRID+1 },
640 {"record-size", RECORD_SIZE_OPTION, N_("NUMBER"), 0,
641 N_("NUMBER of bytes per record, multiple of 512"), GRID+1 },
642 {"ignore-zeros", 'i', 0, 0,
643 N_("ignore zeroed blocks in archive (means EOF)"), GRID+1 },
644 {"read-full-records", 'B', 0, 0,
645 N_("reblock as we read (for 4.2BSD pipes)"), GRID+1 },
646 #undef GRID
647
648 #define GRID 80
649 {NULL, 0, NULL, 0,
650 N_("Archive format selection:"), GRID },
651
652 {"format", 'H', N_("FORMAT"), 0,
653 N_("create archive of the given format"), GRID+1 },
654
655 {NULL, 0, NULL, 0, N_("FORMAT is one of the following:"), GRID+2 },
656 {" v7", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("old V7 tar format"),
657 GRID+3 },
658 {" oldgnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
659 N_("GNU format as per tar <= 1.12"), GRID+3 },
660 {" gnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
661 N_("GNU tar 1.13.x format"), GRID+3 },
662 {" ustar", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
663 N_("POSIX 1003.1-1988 (ustar) format"), GRID+3 },
664 {" pax", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
665 N_("POSIX 1003.1-2001 (pax) format"), GRID+3 },
666 {" posix", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("same as pax"), GRID+3 },
667
668 {"old-archive", OLD_ARCHIVE_OPTION, 0, 0, /* FIXME */
669 N_("same as --format=v7"), GRID+8 },
670 {"portability", 0, 0, OPTION_ALIAS, NULL, GRID+8 },
671 {"posix", POSIX_OPTION, 0, 0,
672 N_("same as --format=posix"), GRID+8 },
673 {"pax-option", PAX_OPTION, N_("keyword[[:]=value][,keyword[[:]=value]]..."), 0,
674 N_("control pax keywords"), GRID+8 },
675 {"label", 'V', N_("TEXT"), 0,
676 N_("create archive with volume name TEXT; at list/extract time, use TEXT as a globbing pattern for volume name"), GRID+8 },
677 #undef GRID
678
679 #define GRID 90
680 {NULL, 0, NULL, 0,
681 N_("Compression options:"), GRID },
682 {"auto-compress", 'a', 0, 0,
683 N_("use archive suffix to determine the compression program"), GRID+1 },
684 {"no-auto-compress", NO_AUTO_COMPRESS_OPTION, 0, 0,
685 N_("do not use archive suffix to determine the compression program"),
686 GRID+1 },
687 {"use-compress-program", 'I', N_("PROG"), 0,
688 N_("filter through PROG (must accept -d)"), GRID+1 },
689 /* Note: docstrings for the options below are generated by tar_help_filter */
690 {"bzip2", 'j', 0, 0, NULL, GRID+1 },
691 {"gzip", 'z', 0, 0, NULL, GRID+1 },
692 {"gunzip", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
693 {"ungzip", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
694 {"compress", 'Z', 0, 0, NULL, GRID+1 },
695 {"uncompress", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
696 {"lzip", LZIP_OPTION, 0, 0, NULL, GRID+1 },
697 {"lzma", LZMA_OPTION, 0, 0, NULL, GRID+1 },
698 {"lzop", LZOP_OPTION, 0, 0, NULL, GRID+1 },
699 {"xz", 'J', 0, 0, NULL, GRID+1 },
700 #undef GRID
701
702 #define GRID 100
703 {NULL, 0, NULL, 0,
704 N_("Local file selection:"), GRID },
705
706 {"add-file", ARGP_KEY_ARG, N_("FILE"), 0,
707 N_("add given FILE to the archive (useful if its name starts with a dash)"), GRID+1 },
708 {"directory", 'C', N_("DIR"), 0,
709 N_("change to directory DIR"), GRID+1 },
710 {"files-from", 'T', N_("FILE"), 0,
711 N_("get names to extract or create from FILE"), GRID+1 },
712 {"null", NULL_OPTION, 0, 0,
713 N_("-T reads null-terminated names, disable -C"), GRID+1 },
714 {"no-null", NO_NULL_OPTION, 0, 0,
715 N_("disable the effect of the previous --null option"), GRID+1 },
716 {"unquote", UNQUOTE_OPTION, 0, 0,
717 N_("unquote filenames read with -T (default)"), GRID+1 },
718 {"no-unquote", NO_UNQUOTE_OPTION, 0, 0,
719 N_("do not unquote filenames read with -T"), GRID+1 },
720 {"exclude", EXCLUDE_OPTION, N_("PATTERN"), 0,
721 N_("exclude files, given as a PATTERN"), GRID+1 },
722 {"exclude-from", 'X', N_("FILE"), 0,
723 N_("exclude patterns listed in FILE"), GRID+1 },
724 {"exclude-caches", EXCLUDE_CACHES_OPTION, 0, 0,
725 N_("exclude contents of directories containing CACHEDIR.TAG, "
726 "except for the tag file itself"), GRID+1 },
727 {"exclude-caches-under", EXCLUDE_CACHES_UNDER_OPTION, 0, 0,
728 N_("exclude everything under directories containing CACHEDIR.TAG"),
729 GRID+1 },
730 {"exclude-caches-all", EXCLUDE_CACHES_ALL_OPTION, 0, 0,
731 N_("exclude directories containing CACHEDIR.TAG"), GRID+1 },
732 {"exclude-tag", EXCLUDE_TAG_OPTION, N_("FILE"), 0,
733 N_("exclude contents of directories containing FILE, except"
734 " for FILE itself"), GRID+1 },
735 {"exclude-tag-under", EXCLUDE_TAG_UNDER_OPTION, N_("FILE"), 0,
736 N_("exclude everything under directories containing FILE"), GRID+1 },
737 {"exclude-tag-all", EXCLUDE_TAG_ALL_OPTION, N_("FILE"), 0,
738 N_("exclude directories containing FILE"), GRID+1 },
739 {"exclude-vcs", EXCLUDE_VCS_OPTION, NULL, 0,
740 N_("exclude version control system directories"), GRID+1 },
741 {"exclude-backups", EXCLUDE_BACKUPS_OPTION, NULL, 0,
742 N_("exclude backup and lock files"), GRID+1 },
743 {"no-recursion", NO_RECURSION_OPTION, 0, 0,
744 N_("avoid descending automatically in directories"), GRID+1 },
745 {"one-file-system", ONE_FILE_SYSTEM_OPTION, 0, 0,
746 N_("stay in local file system when creating archive"), GRID+1 },
747 {"recursion", RECURSION_OPTION, 0, 0,
748 N_("recurse into directories (default)"), GRID+1 },
749 {"absolute-names", 'P', 0, 0,
750 N_("don't strip leading '/'s from file names"), GRID+1 },
751 {"dereference", 'h', 0, 0,
752 N_("follow symlinks; archive and dump the files they point to"), GRID+1 },
753 {"hard-dereference", HARD_DEREFERENCE_OPTION, 0, 0,
754 N_("follow hard links; archive and dump the files they refer to"), GRID+1 },
755 {"starting-file", 'K', N_("MEMBER-NAME"), 0,
756 N_("begin at member MEMBER-NAME when reading the archive"), GRID+1 },
757 {"newer", 'N', N_("DATE-OR-FILE"), 0,
758 N_("only store files newer than DATE-OR-FILE"), GRID+1 },
759 {"after-date", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
760 {"newer-mtime", NEWER_MTIME_OPTION, N_("DATE"), 0,
761 N_("compare date and time when data changed only"), GRID+1 },
762 {"backup", BACKUP_OPTION, N_("CONTROL"), OPTION_ARG_OPTIONAL,
763 N_("backup before removal, choose version CONTROL"), GRID+1 },
764 {"suffix", SUFFIX_OPTION, N_("STRING"), 0,
765 N_("backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX)"), GRID+1 },
766 #undef GRID
767
768 #define GRID 110
769 {NULL, 0, NULL, 0,
770 N_("File name transformations:"), GRID },
771 {"strip-components", STRIP_COMPONENTS_OPTION, N_("NUMBER"), 0,
772 N_("strip NUMBER leading components from file names on extraction"),
773 GRID+1 },
774 {"transform", TRANSFORM_OPTION, N_("EXPRESSION"), 0,
775 N_("use sed replace EXPRESSION to transform file names"), GRID+1 },
776 {"xform", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
777 #undef GRID
778
779 #define GRID 120
780 {NULL, 0, NULL, 0,
781 N_("File name matching options (affect both exclude and include patterns):"),
782 GRID },
783 {"ignore-case", IGNORE_CASE_OPTION, 0, 0,
784 N_("ignore case"), GRID+1 },
785 {"anchored", ANCHORED_OPTION, 0, 0,
786 N_("patterns match file name start"), GRID+1 },
787 {"no-anchored", NO_ANCHORED_OPTION, 0, 0,
788 N_("patterns match after any '/' (default for exclusion)"), GRID+1 },
789 {"no-ignore-case", NO_IGNORE_CASE_OPTION, 0, 0,
790 N_("case sensitive matching (default)"), GRID+1 },
791 {"wildcards", WILDCARDS_OPTION, 0, 0,
792 N_("use wildcards (default for exclusion)"), GRID+1 },
793 {"no-wildcards", NO_WILDCARDS_OPTION, 0, 0,
794 N_("verbatim string matching"), GRID+1 },
795 {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
796 N_("wildcards do not match '/'"), GRID+1 },
797 {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
798 N_("wildcards match '/' (default for exclusion)"), GRID+1 },
799 #undef GRID
800
801 #define GRID 130
802 {NULL, 0, NULL, 0,
803 N_("Informative output:"), GRID },
804
805 {"verbose", 'v', 0, 0,
806 N_("verbosely list files processed"), GRID+1 },
807 {"warning", WARNING_OPTION, N_("KEYWORD"), 0,
808 N_("warning control"), GRID+1 },
809 {"checkpoint", CHECKPOINT_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,
810 N_("display progress messages every NUMBERth record (default 10)"),
811 GRID+1 },
812 {"checkpoint-action", CHECKPOINT_ACTION_OPTION, N_("ACTION"), 0,
813 N_("execute ACTION on each checkpoint"),
814 GRID+1 },
815 {"check-links", 'l', 0, 0,
816 N_("print a message if not all links are dumped"), GRID+1 },
817 {"totals", TOTALS_OPTION, N_("SIGNAL"), OPTION_ARG_OPTIONAL,
818 N_("print total bytes after processing the archive; "
819 "with an argument - print total bytes when this SIGNAL is delivered; "
820 "Allowed signals are: SIGHUP, SIGQUIT, SIGINT, SIGUSR1 and SIGUSR2; "
821 "the names without SIG prefix are also accepted"), GRID+1 },
822 {"utc", UTC_OPTION, 0, 0,
823 N_("print file modification times in UTC"), GRID+1 },
824 {"full-time", FULL_TIME_OPTION, 0, 0,
825 N_("print file time to its full resolution"), GRID+1 },
826 {"index-file", INDEX_FILE_OPTION, N_("FILE"), 0,
827 N_("send verbose output to FILE"), GRID+1 },
828 {"block-number", 'R', 0, 0,
829 N_("show block number within archive with each message"), GRID+1 },
830 {"interactive", 'w', 0, 0,
831 N_("ask for confirmation for every action"), GRID+1 },
832 {"confirmation", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
833 {"show-defaults", SHOW_DEFAULTS_OPTION, 0, 0,
834 N_("show tar defaults"), GRID+1 },
835 {"show-snapshot-field-ranges", SHOW_SNAPSHOT_FIELD_RANGES_OPTION, 0, 0,
836 N_("show valid ranges for snapshot-file fields"), GRID+1 },
837 {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION, 0, 0,
838 N_("when listing or extracting, list each directory that does not match search criteria"), GRID+1 },
839 {"show-transformed-names", SHOW_TRANSFORMED_NAMES_OPTION, 0, 0,
840 N_("show file or archive names after transformation"),
841 GRID+1 },
842 {"show-stored-names", 0, 0, OPTION_ALIAS, NULL, GRID+1 },
843 {"quoting-style", QUOTING_STYLE_OPTION, N_("STYLE"), 0,
844 N_("set name quoting style; see below for valid STYLE values"), GRID+1 },
845 {"quote-chars", QUOTE_CHARS_OPTION, N_("STRING"), 0,
846 N_("additionally quote characters from STRING"), GRID+1 },
847 {"no-quote-chars", NO_QUOTE_CHARS_OPTION, N_("STRING"), 0,
848 N_("disable quoting for characters from STRING"), GRID+1 },
849 #undef GRID
850
851 #define GRID 140
852 {NULL, 0, NULL, 0,
853 N_("Compatibility options:"), GRID },
854
855 {NULL, 'o', 0, 0,
856 N_("when creating, same as --old-archive; when extracting, same as --no-same-owner"), GRID+1 },
857 #undef GRID
858
859 #define GRID 150
860 {NULL, 0, NULL, 0,
861 N_("Other options:"), GRID },
862
863 {"restrict", RESTRICT_OPTION, 0, 0,
864 N_("disable use of some potentially harmful options"), -1 },
865 #undef GRID
866
867 {0, 0, 0, 0, 0, 0}
868 };
869
870 static char const *const atime_preserve_args[] =
871 {
872 "replace", "system", NULL
873 };
874
875 static enum atime_preserve const atime_preserve_types[] =
876 {
877 replace_atime_preserve, system_atime_preserve
878 };
879
880 /* Make sure atime_preserve_types has as much entries as atime_preserve_args
881 (minus 1 for NULL guard) */
882 ARGMATCH_VERIFY (atime_preserve_args, atime_preserve_types);
883
884 /* Wildcard matching settings */
885 enum wildcards
886 {
887 default_wildcards, /* For exclusion == enable_wildcards,
888 for inclusion == disable_wildcards */
889 disable_wildcards,
890 enable_wildcards
891 };
892
893 struct tar_args /* Variables used during option parsing */
894 {
895 struct textual_date *textual_date; /* Keeps the arguments to --newer-mtime
896 and/or --date option if they are
897 textual dates */
898 enum wildcards wildcards; /* Wildcard settings (--wildcards/
899 --no-wildcards) */
900 int matching_flags; /* exclude_fnmatch options */
901 int include_anchored; /* Pattern anchoring options used for
902 file inclusion */
903 bool o_option; /* True if -o option was given */
904 bool pax_option; /* True if --pax-option was given */
905 char const *backup_suffix_string; /* --suffix option argument */
906 char const *version_control_string; /* --backup option argument */
907 bool input_files; /* True if some input files where given */
908 int compress_autodetect; /* True if compression autodetection should
909 be attempted when creating archives */
910 };
911
912 \f
913 #define MAKE_EXCL_OPTIONS(args) \
914 ((((args)->wildcards != disable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
915 | (args)->matching_flags \
916 | recursion_option)
917
918 #define MAKE_INCL_OPTIONS(args) \
919 ((((args)->wildcards == enable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
920 | (args)->include_anchored \
921 | (args)->matching_flags \
922 | recursion_option)
923
924 static char const * const vcs_file_table[] = {
925 /* CVS: */
926 "CVS",
927 ".cvsignore",
928 /* RCS: */
929 "RCS",
930 /* SCCS: */
931 "SCCS",
932 /* SVN: */
933 ".svn",
934 /* git: */
935 ".git",
936 ".gitignore",
937 /* Arch: */
938 ".arch-ids",
939 "{arch}",
940 "=RELEASE-ID",
941 "=meta-update",
942 "=update",
943 /* Bazaar */
944 ".bzr",
945 ".bzrignore",
946 ".bzrtags",
947 /* Mercurial */
948 ".hg",
949 ".hgignore",
950 ".hgtags",
951 /* darcs */
952 "_darcs",
953 NULL
954 };
955
956 static char const * const backup_file_table[] = {
957 ".#*",
958 "*~",
959 "#*#",
960 NULL
961 };
962
963 static void
964 add_exclude_array (char const * const * fv, int opts)
965 {
966 int i;
967
968 for (i = 0; fv[i]; i++)
969 add_exclude (excluded, fv[i], opts);
970 }
971
972 \f
973 static char *
974 format_default_settings (void)
975 {
976 return xasprintf (
977 "--format=%s -f%s -b%d --quoting-style=%s --rmt-command=%s"
978 #ifdef REMOTE_SHELL
979 " --rsh-command=%s"
980 #endif
981 ,
982 archive_format_string (DEFAULT_ARCHIVE_FORMAT),
983 DEFAULT_ARCHIVE, DEFAULT_BLOCKING,
984 quoting_style_args[DEFAULT_QUOTING_STYLE],
985 DEFAULT_RMT_COMMAND
986 #ifdef REMOTE_SHELL
987 , REMOTE_SHELL
988 #endif
989 );
990 }
991
992 \f
993 static void
994 set_subcommand_option (enum subcommand subcommand)
995 {
996 if (subcommand_option != UNKNOWN_SUBCOMMAND
997 && subcommand_option != subcommand)
998 USAGE_ERROR ((0, 0,
999 _("You may not specify more than one '-Acdtrux', '--delete' or '--test-label' option")));
1000
1001 subcommand_option = subcommand;
1002 }
1003
1004 static void
1005 set_use_compress_program_option (const char *string)
1006 {
1007 if (use_compress_program_option
1008 && strcmp (use_compress_program_option, string) != 0)
1009 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
1010
1011 use_compress_program_option = string;
1012 }
1013 \f
1014 static void
1015 sigstat (int signo)
1016 {
1017 compute_duration ();
1018 print_total_stats ();
1019 #ifndef HAVE_SIGACTION
1020 signal (signo, sigstat);
1021 #endif
1022 }
1023
1024 static void
1025 stat_on_signal (int signo)
1026 {
1027 #ifdef HAVE_SIGACTION
1028 # ifndef SA_RESTART
1029 # define SA_RESTART 0
1030 # endif
1031 struct sigaction act;
1032 act.sa_handler = sigstat;
1033 sigemptyset (&act.sa_mask);
1034 act.sa_flags = SA_RESTART;
1035 sigaction (signo, &act, NULL);
1036 #else
1037 signal (signo, sigstat);
1038 #endif
1039 }
1040
1041 static void
1042 set_stat_signal (const char *name)
1043 {
1044 static struct sigtab
1045 {
1046 char const *name;
1047 int signo;
1048 } const sigtab[] = {
1049 { "SIGUSR1", SIGUSR1 },
1050 { "USR1", SIGUSR1 },
1051 { "SIGUSR2", SIGUSR2 },
1052 { "USR2", SIGUSR2 },
1053 { "SIGHUP", SIGHUP },
1054 { "HUP", SIGHUP },
1055 { "SIGINT", SIGINT },
1056 { "INT", SIGINT },
1057 { "SIGQUIT", SIGQUIT },
1058 { "QUIT", SIGQUIT }
1059 };
1060 struct sigtab const *p;
1061
1062 for (p = sigtab; p < sigtab + sizeof (sigtab) / sizeof (sigtab[0]); p++)
1063 if (strcmp (p->name, name) == 0)
1064 {
1065 stat_on_signal (p->signo);
1066 return;
1067 }
1068 FATAL_ERROR ((0, 0, _("Unknown signal name: %s"), name));
1069 }
1070
1071 \f
1072 struct textual_date
1073 {
1074 struct textual_date *next;
1075 struct timespec ts;
1076 const char *option;
1077 char *date;
1078 };
1079
1080 static int
1081 get_date_or_file (struct tar_args *args, const char *option,
1082 const char *str, struct timespec *ts)
1083 {
1084 if (FILE_SYSTEM_PREFIX_LEN (str) != 0
1085 || ISSLASH (*str)
1086 || *str == '.')
1087 {
1088 struct stat st;
1089 if (stat (str, &st) != 0)
1090 {
1091 stat_error (str);
1092 USAGE_ERROR ((0, 0, _("Date sample file not found")));
1093 }
1094 *ts = get_stat_mtime (&st);
1095 }
1096 else
1097 {
1098 if (! parse_datetime (ts, str, NULL))
1099 {
1100 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
1101 tartime (*ts, false), quote (str)));
1102 ts->tv_nsec = 0;
1103 return 1;
1104 }
1105 else
1106 {
1107 struct textual_date *p = xmalloc (sizeof (*p));
1108 p->ts = *ts;
1109 p->option = option;
1110 p->date = xstrdup (str);
1111 p->next = args->textual_date;
1112 args->textual_date = p;
1113 }
1114 }
1115 return 0;
1116 }
1117
1118 static void
1119 report_textual_dates (struct tar_args *args)
1120 {
1121 struct textual_date *p;
1122 for (p = args->textual_date; p; )
1123 {
1124 struct textual_date *next = p->next;
1125 if (verbose_option)
1126 {
1127 char const *treated_as = tartime (p->ts, true);
1128 if (strcmp (p->date, treated_as) != 0)
1129 WARN ((0, 0, _("Option %s: Treating date '%s' as %s"),
1130 p->option, p->date, treated_as));
1131 }
1132 free (p->date);
1133 free (p);
1134 p = next;
1135 }
1136 }
1137
1138 \f
1139 static bool files_from_option; /* When set, tar will not refuse to create
1140 empty archives */
1141
1142 /* Default density numbers for [0-9][lmh] device specifications */
1143
1144 #if defined DEVICE_PREFIX && !defined DENSITY_LETTER
1145 # ifndef LOW_DENSITY_NUM
1146 # define LOW_DENSITY_NUM 0
1147 # endif
1148
1149 # ifndef MID_DENSITY_NUM
1150 # define MID_DENSITY_NUM 8
1151 # endif
1152
1153 # ifndef HIGH_DENSITY_NUM
1154 # define HIGH_DENSITY_NUM 16
1155 # endif
1156 #endif
1157
1158 \f
1159 static char *
1160 tar_help_filter (int key, const char *text, void *input)
1161 {
1162 struct obstack stk;
1163 char *s;
1164
1165 switch (key)
1166 {
1167 default:
1168 s = (char*) text;
1169 break;
1170
1171 case 'j':
1172 s = xasprintf (_("filter the archive through %s"), BZIP2_PROGRAM);
1173 break;
1174
1175 case 'z':
1176 s = xasprintf (_("filter the archive through %s"), GZIP_PROGRAM);
1177 break;
1178
1179 case 'Z':
1180 s = xasprintf (_("filter the archive through %s"), COMPRESS_PROGRAM);
1181 break;
1182
1183 case LZIP_OPTION:
1184 s = xasprintf (_("filter the archive through %s"), LZIP_PROGRAM);
1185 break;
1186
1187 case LZMA_OPTION:
1188 s = xasprintf (_("filter the archive through %s"), LZMA_PROGRAM);
1189 break;
1190
1191 case LZOP_OPTION:
1192 s = xasprintf (_("filter the archive through %s"), LZOP_PROGRAM);
1193
1194 case 'J':
1195 s = xasprintf (_("filter the archive through %s"), XZ_PROGRAM);
1196 break;
1197
1198 case ARGP_KEY_HELP_EXTRA:
1199 {
1200 const char *tstr;
1201
1202 obstack_init (&stk);
1203 tstr = _("Valid arguments for the --quoting-style option are:");
1204 obstack_grow (&stk, tstr, strlen (tstr));
1205 obstack_grow (&stk, "\n\n", 2);
1206 tar_list_quoting_styles (&stk, " ");
1207 tstr = _("\n*This* tar defaults to:\n");
1208 obstack_grow (&stk, tstr, strlen (tstr));
1209 s = format_default_settings ();
1210 obstack_grow (&stk, s, strlen (s));
1211 obstack_1grow (&stk, '\n');
1212 obstack_1grow (&stk, 0);
1213 s = xstrdup (obstack_finish (&stk));
1214 obstack_free (&stk, NULL);
1215 }
1216 }
1217 return s;
1218 }
1219 \f
1220 static char *
1221 expand_pax_option (struct tar_args *targs, const char *arg)
1222 {
1223 struct obstack stk;
1224 char *res;
1225
1226 obstack_init (&stk);
1227 while (*arg)
1228 {
1229 size_t seglen = strcspn (arg, ",");
1230 char *p = memchr (arg, '=', seglen);
1231 if (p)
1232 {
1233 size_t len = p - arg + 1;
1234 obstack_grow (&stk, arg, len);
1235 len = seglen - len;
1236 for (++p; *p && isspace ((unsigned char) *p); p++)
1237 len--;
1238 if (*p == '{' && p[len-1] == '}')
1239 {
1240 struct timespec ts;
1241 char *tmp = xmalloc (len);
1242 memcpy (tmp, p + 1, len-2);
1243 tmp[len-2] = 0;
1244 if (get_date_or_file (targs, "--pax-option", tmp, &ts) == 0)
1245 {
1246 char buf[TIMESPEC_STRSIZE_BOUND];
1247 char const *s = code_timespec (ts, buf);
1248 obstack_grow (&stk, s, strlen (s));
1249 }
1250 else
1251 obstack_grow (&stk, p, len);
1252 free (tmp);
1253 }
1254 else
1255 obstack_grow (&stk, p, len);
1256 }
1257 else
1258 obstack_grow (&stk, arg, seglen);
1259
1260 arg += seglen;
1261 if (*arg)
1262 {
1263 obstack_1grow (&stk, *arg);
1264 arg++;
1265 }
1266 }
1267 obstack_1grow (&stk, 0);
1268 res = xstrdup (obstack_finish (&stk));
1269 obstack_free (&stk, NULL);
1270 return res;
1271 }
1272
1273 \f
1274 static uintmax_t
1275 parse_owner_group (char *arg, uintmax_t field_max, char const **name_option)
1276 {
1277 uintmax_t u = UINTMAX_MAX;
1278 char *end;
1279 char const *name = 0;
1280 char const *invalid_num = 0;
1281 char *colon = strchr (arg, ':');
1282
1283 if (colon)
1284 {
1285 char const *num = colon + 1;
1286 *colon = '\0';
1287 if (*arg)
1288 name = arg;
1289 if (num && (! (xstrtoumax (num, &end, 10, &u, "") == LONGINT_OK
1290 && u <= field_max)))
1291 invalid_num = num;
1292 }
1293 else
1294 {
1295 uintmax_t u1;
1296 switch ('0' <= *arg && *arg <= '9'
1297 ? xstrtoumax (arg, &end, 10, &u1, "")
1298 : LONGINT_INVALID)
1299 {
1300 default:
1301 name = arg;
1302 break;
1303
1304 case LONGINT_OK:
1305 if (u1 <= field_max)
1306 {
1307 u = u1;
1308 break;
1309 }
1310 /* Fall through. */
1311 case LONGINT_OVERFLOW:
1312 invalid_num = arg;
1313 break;
1314 }
1315 }
1316
1317 if (invalid_num)
1318 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (invalid_num),
1319 _("Invalid owner or group ID")));
1320 if (name)
1321 *name_option = name;
1322 return u;
1323 }
1324
1325 #define TAR_SIZE_SUFFIXES "bBcGgkKMmPTtw"
1326
1327 /* Either NL or NUL, as decided by the --null option. */
1328 static char filename_terminator;
1329
1330 static char const *const sort_mode_arg[] = {
1331 "none",
1332 "name",
1333 "inode",
1334 NULL
1335 };
1336
1337 static int sort_mode_flag[] = {
1338 SAVEDIR_SORT_NONE,
1339 SAVEDIR_SORT_NAME,
1340 SAVEDIR_SORT_INODE
1341 };
1342
1343 ARGMATCH_VERIFY (sort_mode_arg, sort_mode_flag);
1344
1345 static error_t
1346 parse_opt (int key, char *arg, struct argp_state *state)
1347 {
1348 struct tar_args *args = state->input;
1349
1350 switch (key)
1351 {
1352 case ARGP_KEY_ARG:
1353 /* File name or non-parsed option, because of ARGP_IN_ORDER */
1354 name_add_name (arg, MAKE_INCL_OPTIONS (args));
1355 args->input_files = true;
1356 break;
1357
1358 case 'A':
1359 set_subcommand_option (CAT_SUBCOMMAND);
1360 break;
1361
1362 case 'a':
1363 args->compress_autodetect = true;
1364 break;
1365
1366 case NO_AUTO_COMPRESS_OPTION:
1367 args->compress_autodetect = false;
1368 break;
1369
1370 case 'b':
1371 {
1372 uintmax_t u;
1373 if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
1374 && u == (blocking_factor = u)
1375 && 0 < blocking_factor
1376 && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
1377 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1378 _("Invalid blocking factor")));
1379 }
1380 break;
1381
1382 case 'B':
1383 /* Try to reblock input records. For reading 4.2BSD pipes. */
1384
1385 /* It would surely make sense to exchange -B and -R, but it seems
1386 that -B has been used for a long while in Sun tar and most
1387 BSD-derived systems. This is a consequence of the block/record
1388 terminology confusion. */
1389
1390 read_full_records_option = true;
1391 break;
1392
1393 case 'c':
1394 set_subcommand_option (CREATE_SUBCOMMAND);
1395 break;
1396
1397 case 'C':
1398 name_add_dir (arg);
1399 break;
1400
1401 case 'd':
1402 set_subcommand_option (DIFF_SUBCOMMAND);
1403 break;
1404
1405 case 'f':
1406 if (archive_names == allocated_archive_names)
1407 archive_name_array = x2nrealloc (archive_name_array,
1408 &allocated_archive_names,
1409 sizeof (archive_name_array[0]));
1410
1411 archive_name_array[archive_names++] = arg;
1412 break;
1413
1414 case 'F':
1415 /* Since -F is only useful with -M, make it implied. Run this
1416 script at the end of each tape. */
1417
1418 info_script_option = arg;
1419 multi_volume_option = true;
1420 break;
1421
1422 case FULL_TIME_OPTION:
1423 full_time_option = true;
1424 break;
1425
1426 case 'g':
1427 listed_incremental_option = arg;
1428 after_date_option = true;
1429 /* Fall through. */
1430
1431 case 'G':
1432 /* We are making an incremental dump (FIXME: are we?); save
1433 directories at the beginning of the archive, and include in each
1434 directory its contents. */
1435
1436 incremental_option = true;
1437 break;
1438
1439 case 'h':
1440 /* Follow symbolic links. */
1441 dereference_option = true;
1442 break;
1443
1444 case HARD_DEREFERENCE_OPTION:
1445 hard_dereference_option = true;
1446 break;
1447
1448 case 'i':
1449 /* Ignore zero blocks (eofs). This can't be the default,
1450 because Unix tar writes two blocks of zeros, then pads out
1451 the record with garbage. */
1452
1453 ignore_zeros_option = true;
1454 break;
1455
1456 case 'j':
1457 set_use_compress_program_option (BZIP2_PROGRAM);
1458 break;
1459
1460 case 'J':
1461 set_use_compress_program_option (XZ_PROGRAM);
1462 break;
1463
1464 case 'k':
1465 /* Don't replace existing files. */
1466 old_files_option = KEEP_OLD_FILES;
1467 break;
1468
1469 case 'K':
1470 starting_file_option = true;
1471 addname (arg, 0, true, NULL);
1472 break;
1473
1474 case ONE_FILE_SYSTEM_OPTION:
1475 /* When dumping directories, don't dump files/subdirectories
1476 that are on other filesystems. */
1477 one_file_system_option = true;
1478 break;
1479
1480 case ONE_TOP_LEVEL_OPTION:
1481 one_top_level_option = true;
1482 one_top_level_dir = arg;
1483 break;
1484
1485 case 'l':
1486 check_links_option = 1;
1487 break;
1488
1489 case 'L':
1490 {
1491 uintmax_t u;
1492 char *p;
1493
1494 if (xstrtoumax (arg, &p, 10, &u, TAR_SIZE_SUFFIXES) != LONGINT_OK)
1495 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1496 _("Invalid tape length")));
1497 if (p > arg && !strchr (TAR_SIZE_SUFFIXES, p[-1]))
1498 tape_length_option = 1024 * (tarlong) u;
1499 else
1500 tape_length_option = (tarlong) u;
1501 multi_volume_option = true;
1502 }
1503 break;
1504
1505 case LEVEL_OPTION:
1506 {
1507 char *p;
1508 incremental_level = strtoul (arg, &p, 10);
1509 if (*p)
1510 USAGE_ERROR ((0, 0, _("Invalid incremental level value")));
1511 }
1512 break;
1513
1514 case LZIP_OPTION:
1515 set_use_compress_program_option (LZIP_PROGRAM);
1516 break;
1517
1518 case LZMA_OPTION:
1519 set_use_compress_program_option (LZMA_PROGRAM);
1520 break;
1521
1522 case LZOP_OPTION:
1523 set_use_compress_program_option (LZOP_PROGRAM);
1524 break;
1525
1526 case 'm':
1527 touch_option = true;
1528 break;
1529
1530 case 'M':
1531 /* Make multivolume archive: when we can't write any more into
1532 the archive, re-open it, and continue writing. */
1533
1534 multi_volume_option = true;
1535 break;
1536
1537 case MTIME_OPTION:
1538 get_date_or_file (args, "--mtime", arg, &mtime_option);
1539 set_mtime_option = true;
1540 break;
1541
1542 case 'n':
1543 seek_option = 1;
1544 break;
1545
1546 case NO_SEEK_OPTION:
1547 seek_option = 0;
1548 break;
1549
1550 case 'N':
1551 after_date_option = true;
1552 /* Fall through. */
1553
1554 case NEWER_MTIME_OPTION:
1555 if (NEWER_OPTION_INITIALIZED (newer_mtime_option))
1556 USAGE_ERROR ((0, 0, _("More than one threshold date")));
1557 get_date_or_file (args,
1558 key == NEWER_MTIME_OPTION ? "--newer-mtime"
1559 : "--after-date", arg, &newer_mtime_option);
1560 break;
1561
1562 case 'o':
1563 args->o_option = true;
1564 break;
1565
1566 case 'O':
1567 to_stdout_option = true;
1568 break;
1569
1570 case 'p':
1571 same_permissions_option = true;
1572 break;
1573
1574 case 'P':
1575 absolute_names_option = true;
1576 break;
1577
1578 case 'r':
1579 set_subcommand_option (APPEND_SUBCOMMAND);
1580 break;
1581
1582 case 'R':
1583 /* Print block numbers for debugging bad tar archives. */
1584
1585 /* It would surely make sense to exchange -B and -R, but it seems
1586 that -B has been used for a long while in Sun tar and most
1587 BSD-derived systems. This is a consequence of the block/record
1588 terminology confusion. */
1589
1590 block_number_option = true;
1591 break;
1592
1593 case 's':
1594 /* Names to extract are sorted. */
1595
1596 same_order_option = true;
1597 break;
1598
1599 case 'S':
1600 sparse_option = true;
1601 break;
1602
1603 case SKIP_OLD_FILES_OPTION:
1604 old_files_option = SKIP_OLD_FILES;
1605 break;
1606
1607 case SPARSE_VERSION_OPTION:
1608 sparse_option = true;
1609 {
1610 char *p;
1611 tar_sparse_major = strtoul (arg, &p, 10);
1612 if (*p)
1613 {
1614 if (*p != '.')
1615 USAGE_ERROR ((0, 0, _("Invalid sparse version value")));
1616 tar_sparse_minor = strtoul (p + 1, &p, 10);
1617 if (*p)
1618 USAGE_ERROR ((0, 0, _("Invalid sparse version value")));
1619 }
1620 }
1621 break;
1622
1623 case 't':
1624 set_subcommand_option (LIST_SUBCOMMAND);
1625 verbose_option++;
1626 break;
1627
1628 case TEST_LABEL_OPTION:
1629 set_subcommand_option (TEST_LABEL_SUBCOMMAND);
1630 break;
1631
1632 case 'T':
1633 name_add_file (arg, filename_terminator);
1634 /* Indicate we've been given -T option. This is for backward
1635 compatibility only, so that `tar cfT archive /dev/null will
1636 succeed */
1637 files_from_option = true;
1638 break;
1639
1640 case 'u':
1641 set_subcommand_option (UPDATE_SUBCOMMAND);
1642 break;
1643
1644 case 'U':
1645 old_files_option = UNLINK_FIRST_OLD_FILES;
1646 break;
1647
1648 case UTC_OPTION:
1649 utc_option = true;
1650 break;
1651
1652 case 'v':
1653 verbose_option++;
1654 warning_option |= WARN_VERBOSE_WARNINGS;
1655 break;
1656
1657 case 'V':
1658 volume_label_option = arg;
1659 break;
1660
1661 case 'w':
1662 interactive_option = true;
1663 break;
1664
1665 case 'W':
1666 verify_option = true;
1667 break;
1668
1669 case 'x':
1670 set_subcommand_option (EXTRACT_SUBCOMMAND);
1671 break;
1672
1673 case 'X':
1674 if (add_exclude_file (add_exclude, excluded, arg,
1675 MAKE_EXCL_OPTIONS (args), '\n')
1676 != 0)
1677 {
1678 int e = errno;
1679 FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
1680 }
1681 break;
1682
1683 case 'z':
1684 set_use_compress_program_option (GZIP_PROGRAM);
1685 break;
1686
1687 case 'Z':
1688 set_use_compress_program_option (COMPRESS_PROGRAM);
1689 break;
1690
1691 case ANCHORED_OPTION:
1692 args->matching_flags |= EXCLUDE_ANCHORED;
1693 break;
1694
1695 case ATIME_PRESERVE_OPTION:
1696 atime_preserve_option =
1697 (arg
1698 ? XARGMATCH ("--atime-preserve", arg,
1699 atime_preserve_args, atime_preserve_types)
1700 : replace_atime_preserve);
1701 if (! O_NOATIME && atime_preserve_option == system_atime_preserve)
1702 FATAL_ERROR ((0, 0,
1703 _("--atime-preserve='system' is not supported"
1704 " on this platform")));
1705 break;
1706
1707 case CHECK_DEVICE_OPTION:
1708 check_device_option = true;
1709 break;
1710
1711 case NO_CHECK_DEVICE_OPTION:
1712 check_device_option = false;
1713 break;
1714
1715 case CHECKPOINT_OPTION:
1716 if (arg)
1717 {
1718 char *p;
1719
1720 if (*arg == '.')
1721 {
1722 checkpoint_compile_action (".");
1723 arg++;
1724 }
1725 checkpoint_option = strtoul (arg, &p, 0);
1726 if (*p)
1727 FATAL_ERROR ((0, 0,
1728 _("--checkpoint value is not an integer")));
1729 }
1730 else
1731 checkpoint_option = DEFAULT_CHECKPOINT;
1732 break;
1733
1734 case CHECKPOINT_ACTION_OPTION:
1735 checkpoint_compile_action (arg);
1736 break;
1737
1738 case BACKUP_OPTION:
1739 backup_option = true;
1740 if (arg)
1741 args->version_control_string = arg;
1742 break;
1743
1744 case DELAY_DIRECTORY_RESTORE_OPTION:
1745 delay_directory_restore_option = true;
1746 break;
1747
1748 case NO_DELAY_DIRECTORY_RESTORE_OPTION:
1749 delay_directory_restore_option = false;
1750 break;
1751
1752 case DELETE_OPTION:
1753 set_subcommand_option (DELETE_SUBCOMMAND);
1754 break;
1755
1756 case EXCLUDE_BACKUPS_OPTION:
1757 add_exclude_array (backup_file_table, EXCLUDE_WILDCARDS);
1758 break;
1759
1760 case EXCLUDE_OPTION:
1761 add_exclude (excluded, arg, MAKE_EXCL_OPTIONS (args));
1762 break;
1763
1764 case EXCLUDE_CACHES_OPTION:
1765 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_contents,
1766 cachedir_file_p);
1767 break;
1768
1769 case EXCLUDE_CACHES_UNDER_OPTION:
1770 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_under,
1771 cachedir_file_p);
1772 break;
1773
1774 case EXCLUDE_CACHES_ALL_OPTION:
1775 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_all,
1776 cachedir_file_p);
1777 break;
1778
1779 case EXCLUDE_TAG_OPTION:
1780 add_exclusion_tag (arg, exclusion_tag_contents, NULL);
1781 break;
1782
1783 case EXCLUDE_TAG_UNDER_OPTION:
1784 add_exclusion_tag (arg, exclusion_tag_under, NULL);
1785 break;
1786
1787 case EXCLUDE_TAG_ALL_OPTION:
1788 add_exclusion_tag (arg, exclusion_tag_all, NULL);
1789 break;
1790
1791 case EXCLUDE_VCS_OPTION:
1792 add_exclude_array (vcs_file_table, 0);
1793 break;
1794
1795 case FORCE_LOCAL_OPTION:
1796 force_local_option = true;
1797 break;
1798
1799 case 'H':
1800 set_archive_format (arg);
1801 break;
1802
1803 case INDEX_FILE_OPTION:
1804 index_file_name = arg;
1805 break;
1806
1807 case IGNORE_CASE_OPTION:
1808 args->matching_flags |= FNM_CASEFOLD;
1809 break;
1810
1811 case IGNORE_COMMAND_ERROR_OPTION:
1812 ignore_command_error_option = true;
1813 break;
1814
1815 case IGNORE_FAILED_READ_OPTION:
1816 ignore_failed_read_option = true;
1817 break;
1818
1819 case KEEP_DIRECTORY_SYMLINK_OPTION:
1820 keep_directory_symlink_option = true;
1821 break;
1822
1823 case KEEP_NEWER_FILES_OPTION:
1824 old_files_option = KEEP_NEWER_FILES;
1825 break;
1826
1827 case GROUP_OPTION:
1828 {
1829 uintmax_t u = parse_owner_group (arg, TYPE_MAXIMUM (gid_t),
1830 &group_name_option);
1831 if (u == UINTMAX_MAX)
1832 {
1833 group_option = -1;
1834 if (group_name_option)
1835 gname_to_gid (group_name_option, &group_option);
1836 }
1837 else
1838 group_option = u;
1839 }
1840 break;
1841
1842 case MODE_OPTION:
1843 mode_option = mode_compile (arg);
1844 if (!mode_option)
1845 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
1846 initial_umask = umask (0);
1847 umask (initial_umask);
1848 break;
1849
1850 case NO_ANCHORED_OPTION:
1851 args->include_anchored = 0; /* Clear the default for comman line args */
1852 args->matching_flags &= ~ EXCLUDE_ANCHORED;
1853 break;
1854
1855 case NO_IGNORE_CASE_OPTION:
1856 args->matching_flags &= ~ FNM_CASEFOLD;
1857 break;
1858
1859 case NO_IGNORE_COMMAND_ERROR_OPTION:
1860 ignore_command_error_option = false;
1861 break;
1862
1863 case NO_OVERWRITE_DIR_OPTION:
1864 old_files_option = NO_OVERWRITE_DIR_OLD_FILES;
1865 break;
1866
1867 case NO_QUOTE_CHARS_OPTION:
1868 for (;*arg; arg++)
1869 set_char_quoting (NULL, *arg, 0);
1870 break;
1871
1872 case NO_WILDCARDS_OPTION:
1873 args->wildcards = disable_wildcards;
1874 break;
1875
1876 case NO_WILDCARDS_MATCH_SLASH_OPTION:
1877 args->matching_flags |= FNM_FILE_NAME;
1878 break;
1879
1880 case NULL_OPTION:
1881 filename_terminator = '\0';
1882 break;
1883
1884 case NO_NULL_OPTION:
1885 filename_terminator = '\n';
1886 break;
1887
1888 case NUMERIC_OWNER_OPTION:
1889 numeric_owner_option = true;
1890 break;
1891
1892 case OCCURRENCE_OPTION:
1893 if (!arg)
1894 occurrence_option = 1;
1895 else
1896 {
1897 uintmax_t u;
1898 if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK)
1899 occurrence_option = u;
1900 else
1901 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1902 _("Invalid number")));
1903 }
1904 break;
1905
1906 case OLD_ARCHIVE_OPTION:
1907 set_archive_format ("v7");
1908 break;
1909
1910 case OVERWRITE_DIR_OPTION:
1911 old_files_option = DEFAULT_OLD_FILES;
1912 break;
1913
1914 case OVERWRITE_OPTION:
1915 old_files_option = OVERWRITE_OLD_FILES;
1916 break;
1917
1918 case OWNER_OPTION:
1919 {
1920 uintmax_t u = parse_owner_group (arg, TYPE_MAXIMUM (uid_t),
1921 &owner_name_option);
1922 if (u == UINTMAX_MAX)
1923 {
1924 owner_option = -1;
1925 if (owner_name_option)
1926 uname_to_uid (owner_name_option, &owner_option);
1927 }
1928 else
1929 owner_option = u;
1930 }
1931 break;
1932
1933 case QUOTE_CHARS_OPTION:
1934 for (;*arg; arg++)
1935 set_char_quoting (NULL, *arg, 1);
1936 break;
1937
1938 case QUOTING_STYLE_OPTION:
1939 tar_set_quoting_style (arg);
1940 break;
1941
1942 case PAX_OPTION:
1943 {
1944 char *tmp = expand_pax_option (args, arg);
1945 args->pax_option = true;
1946 xheader_set_option (tmp);
1947 free (tmp);
1948 }
1949 break;
1950
1951 case POSIX_OPTION:
1952 set_archive_format ("posix");
1953 break;
1954
1955 case PRESERVE_OPTION:
1956 /* FIXME: What it is good for? */
1957 same_permissions_option = true;
1958 same_order_option = true;
1959 WARN ((0, 0, _("The --preserve option is deprecated, "
1960 "use --preserve-permissions --preserve-order instead")));
1961 break;
1962
1963 case RECORD_SIZE_OPTION:
1964 {
1965 uintmax_t u;
1966
1967 if (! (xstrtoumax (arg, NULL, 10, &u, TAR_SIZE_SUFFIXES) == LONGINT_OK
1968 && u == (size_t) u))
1969 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1970 _("Invalid record size")));
1971 record_size = u;
1972 if (record_size % BLOCKSIZE != 0)
1973 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1974 BLOCKSIZE));
1975 blocking_factor = record_size / BLOCKSIZE;
1976 }
1977 break;
1978
1979 case RECURSIVE_UNLINK_OPTION:
1980 recursive_unlink_option = true;
1981 break;
1982
1983 case REMOVE_FILES_OPTION:
1984 remove_files_option = true;
1985 break;
1986
1987 case RESTRICT_OPTION:
1988 restrict_option = true;
1989 break;
1990
1991 case RMT_COMMAND_OPTION:
1992 rmt_command = arg;
1993 break;
1994
1995 case RSH_COMMAND_OPTION:
1996 rsh_command_option = arg;
1997 break;
1998
1999 case SHOW_DEFAULTS_OPTION:
2000 {
2001 char *s = format_default_settings ();
2002 printf ("%s\n", s);
2003 close_stdout ();
2004 free (s);
2005 exit (0);
2006 }
2007
2008 case SHOW_SNAPSHOT_FIELD_RANGES_OPTION:
2009 show_snapshot_field_ranges ();
2010 close_stdout ();
2011 exit (0);
2012
2013 case STRIP_COMPONENTS_OPTION:
2014 {
2015 uintmax_t u;
2016 if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
2017 && u == (size_t) u))
2018 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
2019 _("Invalid number of elements")));
2020 strip_name_components = u;
2021 }
2022 break;
2023
2024 case SHOW_OMITTED_DIRS_OPTION:
2025 show_omitted_dirs_option = true;
2026 break;
2027
2028 case SHOW_TRANSFORMED_NAMES_OPTION:
2029 show_transformed_names_option = true;
2030 break;
2031
2032 case SORT_OPTION:
2033 savedir_sort_order = XARGMATCH ("--sort", arg,
2034 sort_mode_arg, sort_mode_flag);
2035 break;
2036
2037 case SUFFIX_OPTION:
2038 backup_option = true;
2039 args->backup_suffix_string = arg;
2040 break;
2041
2042 case TO_COMMAND_OPTION:
2043 if (to_command_option)
2044 USAGE_ERROR ((0, 0, _("Only one --to-command option allowed")));
2045 to_command_option = arg;
2046 break;
2047
2048 case TOTALS_OPTION:
2049 if (arg)
2050 set_stat_signal (arg);
2051 else
2052 totals_option = true;
2053 break;
2054
2055 case TRANSFORM_OPTION:
2056 set_transform_expr (arg);
2057 break;
2058
2059 case 'I':
2060 set_use_compress_program_option (arg);
2061 break;
2062
2063 case VOLNO_FILE_OPTION:
2064 volno_file_option = arg;
2065 break;
2066
2067 case WILDCARDS_OPTION:
2068 args->wildcards = enable_wildcards;
2069 break;
2070
2071 case WILDCARDS_MATCH_SLASH_OPTION:
2072 args->matching_flags &= ~ FNM_FILE_NAME;
2073 break;
2074
2075 case NO_RECURSION_OPTION:
2076 recursion_option = 0;
2077 break;
2078
2079 case NO_SAME_OWNER_OPTION:
2080 same_owner_option = -1;
2081 break;
2082
2083 case NO_SAME_PERMISSIONS_OPTION:
2084 same_permissions_option = -1;
2085 break;
2086
2087 case ACLS_OPTION:
2088 set_archive_format ("posix");
2089 acls_option = 1;
2090 break;
2091
2092 case NO_ACLS_OPTION:
2093 acls_option = -1;
2094 break;
2095
2096 case SELINUX_CONTEXT_OPTION:
2097 set_archive_format ("posix");
2098 selinux_context_option = 1;
2099 break;
2100
2101 case NO_SELINUX_CONTEXT_OPTION:
2102 selinux_context_option = -1;
2103 break;
2104
2105 case XATTR_OPTION:
2106 set_xattr_option (1);
2107 break;
2108
2109 case NO_XATTR_OPTION:
2110 set_xattr_option (-1);
2111 break;
2112
2113 case XATTR_INCLUDE:
2114 case XATTR_EXCLUDE:
2115 set_xattr_option (1);
2116 xattrs_mask_add (arg, (key == XATTR_INCLUDE));
2117 break;
2118
2119 case RECURSION_OPTION:
2120 recursion_option = FNM_LEADING_DIR;
2121 break;
2122
2123 case SAME_OWNER_OPTION:
2124 same_owner_option = 1;
2125 break;
2126
2127 case UNQUOTE_OPTION:
2128 unquote_option = true;
2129 break;
2130
2131 case NO_UNQUOTE_OPTION:
2132 unquote_option = false;
2133 break;
2134
2135 case WARNING_OPTION:
2136 set_warning_option (arg);
2137 break;
2138
2139 case '0':
2140 case '1':
2141 case '2':
2142 case '3':
2143 case '4':
2144 case '5':
2145 case '6':
2146 case '7':
2147
2148 #ifdef DEVICE_PREFIX
2149 {
2150 int device = key - '0';
2151 int density;
2152 static char buf[sizeof DEVICE_PREFIX + 10];
2153 char *cursor;
2154
2155 if (arg[1])
2156 argp_error (state, _("Malformed density argument: %s"), quote (arg));
2157
2158 strcpy (buf, DEVICE_PREFIX);
2159 cursor = buf + strlen (buf);
2160
2161 #ifdef DENSITY_LETTER
2162
2163 sprintf (cursor, "%d%c", device, arg[0]);
2164
2165 #else /* not DENSITY_LETTER */
2166
2167 switch (arg[0])
2168 {
2169 case 'l':
2170 device += LOW_DENSITY_NUM;
2171 break;
2172
2173 case 'm':
2174 device += MID_DENSITY_NUM;
2175 break;
2176
2177 case 'h':
2178 device += HIGH_DENSITY_NUM;
2179 break;
2180
2181 default:
2182 argp_error (state, _("Unknown density: '%c'"), arg[0]);
2183 }
2184 sprintf (cursor, "%d", device);
2185
2186 #endif /* not DENSITY_LETTER */
2187
2188 if (archive_names == allocated_archive_names)
2189 archive_name_array = x2nrealloc (archive_name_array,
2190 &allocated_archive_names,
2191 sizeof (archive_name_array[0]));
2192 archive_name_array[archive_names++] = xstrdup (buf);
2193 }
2194 break;
2195
2196 #else /* not DEVICE_PREFIX */
2197
2198 argp_error (state,
2199 _("Options '-[0-7][lmh]' not supported by *this* tar"));
2200
2201 #endif /* not DEVICE_PREFIX */
2202
2203 default:
2204 return ARGP_ERR_UNKNOWN;
2205 }
2206 return 0;
2207 }
2208
2209 static struct argp argp = {
2210 options,
2211 parse_opt,
2212 N_("[FILE]..."),
2213 doc,
2214 NULL,
2215 tar_help_filter,
2216 NULL
2217 };
2218
2219 void
2220 usage (int status)
2221 {
2222 argp_help (&argp, stderr, ARGP_HELP_SEE, (char*) program_name);
2223 close_stdout ();
2224 exit (status);
2225 }
2226
2227 /* Parse the options for tar. */
2228
2229 static struct argp_option *
2230 find_argp_option (struct argp_option *o, int letter)
2231 {
2232 for (;
2233 !(o->name == NULL
2234 && o->key == 0
2235 && o->arg == 0
2236 && o->flags == 0
2237 && o->doc == NULL); o++)
2238 if (o->key == letter)
2239 return o;
2240 return NULL;
2241 }
2242
2243 static const char *tar_authors[] = {
2244 "John Gilmore",
2245 "Jay Fenlason",
2246 NULL
2247 };
2248 \f
2249 /* Subcommand classes */
2250 #define SUBCL_READ 0x01 /* subcommand reads from the archive */
2251 #define SUBCL_WRITE 0x02 /* subcommand writes to the archive */
2252 #define SUBCL_UPDATE 0x04 /* subcommand updates existing archive */
2253 #define SUBCL_TEST 0x08 /* subcommand tests archive header or meta-info */
2254 #define SUBCL_OCCUR 0x10 /* subcommand allows the use of the occurrence
2255 option */
2256
2257 static int subcommand_class[] = {
2258 /* UNKNOWN_SUBCOMMAND */ 0,
2259 /* APPEND_SUBCOMMAND */ SUBCL_WRITE|SUBCL_UPDATE,
2260 /* CAT_SUBCOMMAND */ SUBCL_WRITE,
2261 /* CREATE_SUBCOMMAND */ SUBCL_WRITE,
2262 /* DELETE_SUBCOMMAND */ SUBCL_WRITE|SUBCL_UPDATE|SUBCL_OCCUR,
2263 /* DIFF_SUBCOMMAND */ SUBCL_READ|SUBCL_OCCUR,
2264 /* EXTRACT_SUBCOMMAND */ SUBCL_READ|SUBCL_OCCUR,
2265 /* LIST_SUBCOMMAND */ SUBCL_READ|SUBCL_OCCUR,
2266 /* UPDATE_SUBCOMMAND */ SUBCL_WRITE|SUBCL_UPDATE,
2267 /* TEST_LABEL_SUBCOMMAND */ SUBCL_TEST
2268 };
2269
2270 /* Return t if the subcommand_option is in class(es) f */
2271 #define IS_SUBCOMMAND_CLASS(f) (subcommand_class[subcommand_option] & (f))
2272
2273 static struct tar_args args;
2274
2275 static void
2276 option_conflict_error (const char *a, const char *b)
2277 {
2278 /* TRANSLATORS: Both %s in this statement are replaced with
2279 option names. */
2280 USAGE_ERROR ((0, 0, _("'%s' cannot be used with '%s'"), a, b));
2281 }
2282
2283 static void
2284 decode_options (int argc, char **argv)
2285 {
2286 int idx;
2287
2288 argp_version_setup ("tar", tar_authors);
2289
2290 /* Set some default option values. */
2291 args.textual_date = NULL;
2292 args.wildcards = default_wildcards;
2293 args.matching_flags = 0;
2294 args.include_anchored = EXCLUDE_ANCHORED;
2295 args.o_option = false;
2296 args.pax_option = false;
2297 args.backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
2298 args.version_control_string = 0;
2299 args.input_files = false;
2300 args.compress_autodetect = false;
2301
2302 subcommand_option = UNKNOWN_SUBCOMMAND;
2303 archive_format = DEFAULT_FORMAT;
2304 blocking_factor = DEFAULT_BLOCKING;
2305 record_size = DEFAULT_BLOCKING * BLOCKSIZE;
2306 excluded = new_exclude ();
2307 newer_mtime_option.tv_sec = TYPE_MINIMUM (time_t);
2308 newer_mtime_option.tv_nsec = -1;
2309 recursion_option = FNM_LEADING_DIR;
2310 unquote_option = true;
2311 tar_sparse_major = 1;
2312 tar_sparse_minor = 0;
2313
2314 savedir_sort_order = SAVEDIR_SORT_NONE;
2315
2316 owner_option = -1; owner_name_option = NULL;
2317 group_option = -1; group_name_option = NULL;
2318
2319 check_device_option = true;
2320
2321 incremental_level = -1;
2322
2323 seek_option = -1;
2324
2325 /* Convert old-style tar call by exploding option element and rearranging
2326 options accordingly. */
2327
2328 if (argc > 1 && argv[1][0] != '-')
2329 {
2330 int new_argc; /* argc value for rearranged arguments */
2331 char **new_argv; /* argv value for rearranged arguments */
2332 char *const *in; /* cursor into original argv */
2333 char **out; /* cursor into rearranged argv */
2334 const char *letter; /* cursor into old option letters */
2335 char buffer[3]; /* constructed option buffer */
2336
2337 /* Initialize a constructed option. */
2338
2339 buffer[0] = '-';
2340 buffer[2] = '\0';
2341
2342 /* Allocate a new argument array, and copy program name in it. */
2343
2344 new_argc = argc - 1 + strlen (argv[1]);
2345 new_argv = xmalloc ((new_argc + 1) * sizeof (char *));
2346 in = argv;
2347 out = new_argv;
2348 *out++ = *in++;
2349
2350 /* Copy each old letter option as a separate option, and have the
2351 corresponding argument moved next to it. */
2352
2353 for (letter = *in++; *letter; letter++)
2354 {
2355 struct argp_option *opt;
2356
2357 buffer[1] = *letter;
2358 *out++ = xstrdup (buffer);
2359 opt = find_argp_option (options, *letter);
2360 if (opt && opt->arg)
2361 {
2362 if (in < argv + argc)
2363 *out++ = *in++;
2364 else
2365 USAGE_ERROR ((0, 0, _("Old option '%c' requires an argument."),
2366 *letter));
2367 }
2368 }
2369
2370 /* Copy all remaining options. */
2371
2372 while (in < argv + argc)
2373 *out++ = *in++;
2374 *out = 0;
2375
2376 /* Replace the old option list by the new one. */
2377
2378 argc = new_argc;
2379 argv = new_argv;
2380 }
2381
2382 /* Parse all options and non-options as they appear. */
2383
2384 prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
2385
2386 if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &idx, &args))
2387 exit (TAREXIT_FAILURE);
2388
2389 /* Special handling for 'o' option:
2390
2391 GNU tar used to say "output old format".
2392 UNIX98 tar says don't chown files after extracting (we use
2393 "--no-same-owner" for this).
2394
2395 The old GNU tar semantics is retained when used with --create
2396 option, otherwise UNIX98 semantics is assumed */
2397
2398 if (args.o_option)
2399 {
2400 if (subcommand_option == CREATE_SUBCOMMAND)
2401 {
2402 /* GNU Tar <= 1.13 compatibility */
2403 set_archive_format ("v7");
2404 }
2405 else
2406 {
2407 /* UNIX98 compatibility */
2408 same_owner_option = -1;
2409 }
2410 }
2411
2412 /* Handle operands after any "--" argument. */
2413 for (; idx < argc; idx++)
2414 {
2415 name_add_name (argv[idx], MAKE_INCL_OPTIONS (&args));
2416 args.input_files = true;
2417 }
2418
2419 /* Warn about implicit use of the wildcards in command line arguments.
2420 See TODO */
2421 warn_regex_usage = args.wildcards == default_wildcards;
2422
2423 /* Derive option values and check option consistency. */
2424
2425 if (archive_format == DEFAULT_FORMAT)
2426 {
2427 if (args.pax_option)
2428 archive_format = POSIX_FORMAT;
2429 else
2430 archive_format = DEFAULT_ARCHIVE_FORMAT;
2431 }
2432
2433 if ((volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
2434 || incremental_option
2435 || multi_volume_option
2436 || sparse_option)
2437 assert_format (FORMAT_MASK (OLDGNU_FORMAT)
2438 | FORMAT_MASK (GNU_FORMAT)
2439 | FORMAT_MASK (POSIX_FORMAT));
2440
2441 if (occurrence_option)
2442 {
2443 if (!args.input_files)
2444 USAGE_ERROR ((0, 0,
2445 _("--occurrence is meaningless without a file list")));
2446 if (!IS_SUBCOMMAND_CLASS (SUBCL_OCCUR))
2447 option_conflict_error ("--occurrence",
2448 subcommand_string (subcommand_option));
2449 }
2450
2451 if (archive_names == 0)
2452 {
2453 /* If no archive file name given, try TAPE from the environment, or
2454 else, DEFAULT_ARCHIVE from the configuration process. */
2455
2456 archive_names = 1;
2457 archive_name_array[0] = getenv ("TAPE");
2458 if (! archive_name_array[0])
2459 archive_name_array[0] = DEFAULT_ARCHIVE;
2460 }
2461
2462 /* Allow multiple archives only with '-M'. */
2463
2464 if (archive_names > 1 && !multi_volume_option)
2465 USAGE_ERROR ((0, 0,
2466 _("Multiple archive files require '-M' option")));
2467
2468 if (listed_incremental_option
2469 && NEWER_OPTION_INITIALIZED (newer_mtime_option))
2470 option_conflict_error ("--listed-incremental", "--newer");
2471
2472 if (incremental_level != -1 && !listed_incremental_option)
2473 WARN ((0, 0,
2474 _("--level is meaningless without --listed-incremental")));
2475
2476 if (volume_label_option)
2477 {
2478 if (archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
2479 {
2480 size_t volume_label_max_len =
2481 (sizeof current_header->header.name
2482 - 1 /* for trailing '\0' */
2483 - (multi_volume_option
2484 ? (sizeof " Volume "
2485 - 1 /* for null at end of " Volume " */
2486 + INT_STRLEN_BOUND (int) /* for volume number */
2487 - 1 /* for sign, as 0 <= volno */)
2488 : 0));
2489 if (volume_label_max_len < strlen (volume_label_option))
2490 USAGE_ERROR ((0, 0,
2491 ngettext ("%s: Volume label is too long (limit is %lu byte)",
2492 "%s: Volume label is too long (limit is %lu bytes)",
2493 volume_label_max_len),
2494 quotearg_colon (volume_label_option),
2495 (unsigned long) volume_label_max_len));
2496 }
2497 /* else FIXME
2498 Label length in PAX format is limited by the volume size. */
2499 }
2500
2501 if (verify_option)
2502 {
2503 if (multi_volume_option)
2504 USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
2505 if (use_compress_program_option)
2506 USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
2507 if (!IS_SUBCOMMAND_CLASS (SUBCL_WRITE))
2508 option_conflict_error ("--verify",
2509 subcommand_string (subcommand_option));
2510 }
2511
2512 if (use_compress_program_option)
2513 {
2514 if (multi_volume_option)
2515 USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
2516 if (IS_SUBCOMMAND_CLASS (SUBCL_UPDATE))
2517 USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
2518 if (subcommand_option == CAT_SUBCOMMAND)
2519 USAGE_ERROR ((0, 0, _("Cannot concatenate compressed archives")));
2520 }
2521
2522 /* It is no harm to use --pax-option on non-pax archives in archive
2523 reading mode. It may even be useful, since it allows to override
2524 file attributes from tar headers. Therefore I allow such usage.
2525 --gray */
2526 if (args.pax_option
2527 && archive_format != POSIX_FORMAT
2528 && !IS_SUBCOMMAND_CLASS (SUBCL_READ))
2529 USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
2530
2531 /* star creates non-POSIX typed archives with xattr support, so allow the
2532 extra headers when reading */
2533 if ((acls_option > 0)
2534 && archive_format != POSIX_FORMAT
2535 && !IS_SUBCOMMAND_CLASS (SUBCL_READ))
2536 USAGE_ERROR ((0, 0, _("--acls can be used only on POSIX archives")));
2537
2538 if ((selinux_context_option > 0)
2539 && archive_format != POSIX_FORMAT
2540 && !IS_SUBCOMMAND_CLASS (SUBCL_READ))
2541 USAGE_ERROR ((0, 0, _("--selinux can be used only on POSIX archives")));
2542
2543 if ((xattrs_option > 0)
2544 && archive_format != POSIX_FORMAT
2545 && !IS_SUBCOMMAND_CLASS (SUBCL_READ))
2546 USAGE_ERROR ((0, 0, _("--xattrs can be used only on POSIX archives")));
2547
2548 if (starting_file_option && !IS_SUBCOMMAND_CLASS (SUBCL_READ))
2549 option_conflict_error ("--starting-file",
2550 subcommand_string (subcommand_option));
2551
2552 if (same_order_option && !IS_SUBCOMMAND_CLASS (SUBCL_READ))
2553 option_conflict_error ("--same-order",
2554 subcommand_string (subcommand_option));
2555
2556 if (one_top_level_option)
2557 {
2558 char *base;
2559
2560 if (absolute_names_option)
2561 option_conflict_error ("--one-top-level", "--absolute-names");
2562
2563 if (!one_top_level_dir)
2564 {
2565 /* If the user wants to guarantee that everything is under one
2566 directory, determine its name now and let it be created later. */
2567 base = base_name (archive_name_array[0]);
2568 one_top_level_dir = strip_compression_suffix (base);
2569 free (base);
2570
2571 if (!one_top_level_dir)
2572 USAGE_ERROR ((0, 0,
2573 _("Cannot deduce top-level directory name; "
2574 "please set it explicitly with --one-top-level=DIR")));
2575 }
2576 }
2577
2578 /* If ready to unlink hierarchies, so we are for simpler files. */
2579 if (recursive_unlink_option)
2580 old_files_option = UNLINK_FIRST_OLD_FILES;
2581
2582 /* Flags for accessing files to be read from or copied into. POSIX says
2583 O_NONBLOCK has unspecified effect on most types of files, but in
2584 practice it never harms and sometimes helps. */
2585 {
2586 int base_open_flags =
2587 (O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
2588 | (dereference_option ? 0 : O_NOFOLLOW)
2589 | (atime_preserve_option == system_atime_preserve ? O_NOATIME : 0));
2590 open_read_flags = O_RDONLY | base_open_flags;
2591 open_searchdir_flags = O_SEARCH | O_DIRECTORY | base_open_flags;
2592 }
2593 fstatat_flags = dereference_option ? 0 : AT_SYMLINK_NOFOLLOW;
2594
2595 if (subcommand_option == TEST_LABEL_SUBCOMMAND)
2596 {
2597 /* --test-label is silent if the user has specified the label name to
2598 compare against. */
2599 if (!args.input_files)
2600 verbose_option++;
2601 }
2602 else if (utc_option)
2603 verbose_option = 2;
2604
2605 if (tape_length_option && tape_length_option < record_size)
2606 USAGE_ERROR ((0, 0, _("Volume length cannot be less than record size")));
2607
2608 if (same_order_option && listed_incremental_option)
2609 option_conflict_error ("--preserve-order", "--listed-incremental");
2610
2611 /* Forbid using -c with no input files whatsoever. Check that '-f -',
2612 explicit or implied, is used correctly. */
2613
2614 switch (subcommand_option)
2615 {
2616 case CREATE_SUBCOMMAND:
2617 if (!args.input_files && !files_from_option)
2618 USAGE_ERROR ((0, 0,
2619 _("Cowardly refusing to create an empty archive")));
2620 if (args.compress_autodetect && archive_names
2621 && strcmp (archive_name_array[0], "-"))
2622 set_compression_program_by_suffix (archive_name_array[0],
2623 use_compress_program_option);
2624 break;
2625
2626 case EXTRACT_SUBCOMMAND:
2627 case LIST_SUBCOMMAND:
2628 case DIFF_SUBCOMMAND:
2629 case TEST_LABEL_SUBCOMMAND:
2630 for (archive_name_cursor = archive_name_array;
2631 archive_name_cursor < archive_name_array + archive_names;
2632 archive_name_cursor++)
2633 if (!strcmp (*archive_name_cursor, "-"))
2634 request_stdin ("-f");
2635 break;
2636
2637 case CAT_SUBCOMMAND:
2638 case UPDATE_SUBCOMMAND:
2639 case APPEND_SUBCOMMAND:
2640 for (archive_name_cursor = archive_name_array;
2641 archive_name_cursor < archive_name_array + archive_names;
2642 archive_name_cursor++)
2643 if (!strcmp (*archive_name_cursor, "-"))
2644 USAGE_ERROR ((0, 0,
2645 _("Options '-Aru' are incompatible with '-f -'")));
2646
2647 default:
2648 break;
2649 }
2650
2651 /* Initialize stdlis */
2652 if (index_file_name)
2653 {
2654 stdlis = fopen (index_file_name, "w");
2655 if (! stdlis)
2656 open_fatal (index_file_name);
2657 }
2658 else
2659 stdlis = to_stdout_option ? stderr : stdout;
2660
2661 archive_name_cursor = archive_name_array;
2662
2663 /* Prepare for generating backup names. */
2664
2665 if (args.backup_suffix_string)
2666 simple_backup_suffix = xstrdup (args.backup_suffix_string);
2667
2668 if (backup_option)
2669 {
2670 backup_type = xget_version ("--backup", args.version_control_string);
2671 /* No backup is needed either if explicitely disabled or if
2672 the extracted files are not being written to disk. */
2673 if (backup_type == no_backups || EXTRACT_OVER_PIPE)
2674 backup_option = false;
2675 }
2676
2677 checkpoint_finish_compile ();
2678
2679 report_textual_dates (&args);
2680 }
2681
2682 void
2683 more_options (int argc, char **argv)
2684 {
2685 int idx;
2686 if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER,
2687 &idx, &args))
2688 exit (TAREXIT_FAILURE);
2689 }
2690 \f
2691 /* Tar proper. */
2692
2693 /* Main routine for tar. */
2694 int
2695 main (int argc, char **argv)
2696 {
2697 set_start_time ();
2698 set_program_name (argv[0]);
2699
2700 setlocale (LC_ALL, "");
2701 bindtextdomain (PACKAGE, LOCALEDIR);
2702 textdomain (PACKAGE);
2703
2704 exit_failure = TAREXIT_FAILURE;
2705 exit_status = TAREXIT_SUCCESS;
2706 error_hook = checkpoint_flush_actions;
2707
2708 filename_terminator = '\n';
2709 set_quoting_style (0, DEFAULT_QUOTING_STYLE);
2710
2711 /* Make sure we have first three descriptors available */
2712 stdopen ();
2713
2714 /* Pre-allocate a few structures. */
2715
2716 allocated_archive_names = 10;
2717 archive_name_array =
2718 xmalloc (sizeof (const char *) * allocated_archive_names);
2719 archive_names = 0;
2720
2721 /* System V fork+wait does not work if SIGCHLD is ignored. */
2722 signal (SIGCHLD, SIG_DFL);
2723
2724 /* Try to disable the ability to unlink a directory. */
2725 priv_set_remove_linkdir ();
2726
2727 /* Decode options. */
2728
2729 decode_options (argc, argv);
2730
2731 name_init ();
2732
2733 /* Main command execution. */
2734
2735 if (volno_file_option)
2736 init_volume_number ();
2737
2738 switch (subcommand_option)
2739 {
2740 case UNKNOWN_SUBCOMMAND:
2741 USAGE_ERROR ((0, 0,
2742 _("You must specify one of the '-Acdtrux', '--delete' or '--test-label' options")));
2743
2744 case CAT_SUBCOMMAND:
2745 case UPDATE_SUBCOMMAND:
2746 case APPEND_SUBCOMMAND:
2747 update_archive ();
2748 break;
2749
2750 case DELETE_SUBCOMMAND:
2751 delete_archive_members ();
2752 break;
2753
2754 case CREATE_SUBCOMMAND:
2755 create_archive ();
2756 break;
2757
2758 case EXTRACT_SUBCOMMAND:
2759 extr_init ();
2760 read_and (extract_archive);
2761
2762 /* FIXME: should extract_finish () even if an ordinary signal is
2763 received. */
2764 extract_finish ();
2765
2766 break;
2767
2768 case LIST_SUBCOMMAND:
2769 read_and (list_archive);
2770 break;
2771
2772 case DIFF_SUBCOMMAND:
2773 diff_init ();
2774 read_and (diff_archive);
2775 break;
2776
2777 case TEST_LABEL_SUBCOMMAND:
2778 test_archive_label ();
2779 }
2780
2781 checkpoint_finish ();
2782
2783 if (totals_option)
2784 print_total_stats ();
2785
2786 if (check_links_option)
2787 check_links ();
2788
2789 if (volno_file_option)
2790 closeout_volume_number ();
2791
2792 /* Dispose of allocated memory, and return. */
2793
2794 free (archive_name_array);
2795 xattrs_clear_setup ();
2796 name_term ();
2797
2798 if (exit_status == TAREXIT_FAILURE)
2799 error (0, 0, _("Exiting with failure status due to previous errors"));
2800
2801 if (stdlis == stdout)
2802 close_stdout ();
2803 else if (ferror (stderr) || fclose (stderr) != 0)
2804 set_exit_status (TAREXIT_FAILURE);
2805
2806 return exit_status;
2807 }
2808
2809 void
2810 tar_stat_init (struct tar_stat_info *st)
2811 {
2812 memset (st, 0, sizeof (*st));
2813 }
2814
2815 /* Close the stream or file descriptor associated with ST, and remove
2816 all traces of it from ST. Return true if successful, false (with a
2817 diagnostic) otherwise. */
2818 bool
2819 tar_stat_close (struct tar_stat_info *st)
2820 {
2821 int status = (st->dirstream ? closedir (st->dirstream)
2822 : 0 < st->fd ? close (st->fd)
2823 : 0);
2824 st->dirstream = 0;
2825 st->fd = 0;
2826
2827 if (status == 0)
2828 return true;
2829 else
2830 {
2831 close_diag (st->orig_file_name);
2832 return false;
2833 }
2834 }
2835
2836 void
2837 tar_stat_destroy (struct tar_stat_info *st)
2838 {
2839 tar_stat_close (st);
2840 xheader_xattr_free (st->xattr_map, st->xattr_map_size);
2841 free (st->orig_file_name);
2842 free (st->file_name);
2843 free (st->link_name);
2844 free (st->uname);
2845 free (st->gname);
2846 free (st->cntx_name);
2847 free (st->acls_a_ptr);
2848 free (st->acls_d_ptr);
2849 free (st->sparse_map);
2850 free (st->dumpdir);
2851 xheader_destroy (&st->xhdr);
2852 memset (st, 0, sizeof (*st));
2853 }
2854
2855 /* Format mask for all available formats that support nanosecond
2856 timestamp resolution. */
2857 #define NS_PRECISION_FORMAT_MASK FORMAT_MASK (POSIX_FORMAT)
2858
2859 /* Same as timespec_cmp, but ignore nanoseconds if current archive
2860 format does not provide sufficient resolution. */
2861 int
2862 tar_timespec_cmp (struct timespec a, struct timespec b)
2863 {
2864 if (!(FORMAT_MASK (current_format) & NS_PRECISION_FORMAT_MASK))
2865 a.tv_nsec = b.tv_nsec = 0;
2866 return timespec_cmp (a, b);
2867 }
2868
2869 /* Set tar exit status to VAL, unless it is already indicating
2870 a more serious condition. This relies on the fact that the
2871 values of TAREXIT_ constants are ranged by severity. */
2872 void
2873 set_exit_status (int val)
2874 {
2875 if (val > exit_status)
2876 exit_status = val;
2877 }
This page took 0.178287 seconds and 4 git commands to generate.