1 /* A tar (tape archiver) program.
3 Copyright 1988, 1992-1997, 1999-2001, 2003-2007, 2012-2014 Free
4 Software Foundation, Inc.
6 Written by John Gilmore, starting 1985-08-25.
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
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.
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/>. */
25 #include <argp-namefrob.h>
26 #include <argp-fmtstream.h>
27 #include <argp-version-etc.h>
30 #if ! defined SIGCHLD && defined SIGCLD
31 # define SIGCHLD SIGCLD
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. */
43 #include <configmake.h>
45 #include <parse-datetime.h>
47 #include <rmt-command.h>
50 #include <version-etc.h>
56 /* Local declarations. */
58 #ifndef DEFAULT_ARCHIVE_FORMAT
59 # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
62 #ifndef DEFAULT_ARCHIVE
63 # define DEFAULT_ARCHIVE "tar.out"
66 #ifndef DEFAULT_BLOCKING
67 # define DEFAULT_BLOCKING 20
70 /* Print a message if not all links are dumped */
71 static int check_links_option
;
73 /* Number of allocated tape drive names. */
74 static size_t allocated_archive_names
;
79 /* Name of option using stdin. */
80 static const char *stdin_used_by
;
82 /* Doesn't return if stdin already requested. */
84 request_stdin (const char *option
)
87 USAGE_ERROR ((0, 0, _("Options '%s' and '%s' both want standard input"),
88 stdin_used_by
, option
));
90 stdin_used_by
= option
;
93 extern int rpmatch (char const *response
);
95 /* Returns true if and only if the user typed an affirmative response. */
97 confirm (const char *message_action
, const char *message_name
)
99 static FILE *confirm_file
;
100 static int confirm_file_EOF
;
105 if (archive
== 0 || stdin_used_by
)
107 confirm_file
= fopen (TTY_NAME
, "r");
109 open_fatal (TTY_NAME
);
113 request_stdin ("-w");
114 confirm_file
= stdin
;
118 fprintf (stdlis
, "%s %s?", message_action
, quote (message_name
));
121 if (!confirm_file_EOF
)
123 char *response
= NULL
;
124 size_t response_size
= 0;
125 if (getline (&response
, &response_size
, confirm_file
) < 0)
126 confirm_file_EOF
= 1;
128 status
= rpmatch (response
) > 0;
132 if (confirm_file_EOF
)
134 fputc ('\n', stdlis
);
141 static struct fmttab
{
143 enum archive_format fmt
;
146 { "oldgnu", OLDGNU_FORMAT
},
147 { "ustar", USTAR_FORMAT
},
148 { "posix", POSIX_FORMAT
},
149 #if 0 /* not fully supported yet */
150 { "star", STAR_FORMAT
},
152 { "gnu", GNU_FORMAT
},
153 { "pax", POSIX_FORMAT
}, /* An alias for posix */
158 set_archive_format (char const *name
)
160 struct fmttab
const *p
;
162 for (p
= fmttab
; strcmp (p
->name
, name
) != 0; )
164 USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
165 quotearg_colon (name
)));
167 archive_format
= p
->fmt
;
171 set_xattr_option (int value
)
174 set_archive_format ("posix");
175 xattrs_option
= value
;
179 archive_format_string (enum archive_format fmt
)
181 struct fmttab
const *p
;
183 for (p
= fmttab
; p
->name
; p
++)
189 #define FORMAT_MASK(n) (1<<(n))
192 assert_format(unsigned fmt_mask
)
194 if ((FORMAT_MASK (archive_format
) & fmt_mask
) == 0)
196 _("GNU features wanted on incompatible archive format")));
200 subcommand_string (enum subcommand c
)
204 case UNKNOWN_SUBCOMMAND
:
207 case APPEND_SUBCOMMAND
:
213 case CREATE_SUBCOMMAND
:
216 case DELETE_SUBCOMMAND
:
219 case DIFF_SUBCOMMAND
:
222 case EXTRACT_SUBCOMMAND
:
225 case LIST_SUBCOMMAND
:
228 case UPDATE_SUBCOMMAND
:
231 case TEST_LABEL_SUBCOMMAND
:
232 return "--test-label";
238 tar_list_quoting_styles (struct obstack
*stk
, char const *prefix
)
241 size_t prefixlen
= strlen (prefix
);
243 for (i
= 0; quoting_style_args
[i
]; i
++)
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');
253 tar_set_quoting_style (char *arg
)
257 for (i
= 0; quoting_style_args
[i
]; i
++)
258 if (strcmp (arg
, quoting_style_args
[i
]) == 0)
260 set_quoting_style (NULL
, i
);
264 _("Unknown quoting style '%s'. Try '%s --quoting-style=help' to get a list."), arg
, program_invocation_short_name
));
272 ACLS_OPTION
= CHAR_MAX
+ 1,
274 ATIME_PRESERVE_OPTION
,
278 CHECKPOINT_ACTION_OPTION
,
279 DELAY_DIRECTORY_RESTORE_OPTION
,
280 HARD_DEREFERENCE_OPTION
,
282 EXCLUDE_BACKUPS_OPTION
,
283 EXCLUDE_CACHES_OPTION
,
284 EXCLUDE_CACHES_UNDER_OPTION
,
285 EXCLUDE_CACHES_ALL_OPTION
,
288 EXCLUDE_TAG_UNDER_OPTION
,
289 EXCLUDE_TAG_ALL_OPTION
,
295 IGNORE_COMMAND_ERROR_OPTION
,
296 IGNORE_FAILED_READ_OPTION
,
298 KEEP_DIRECTORY_SYMLINK_OPTION
,
299 KEEP_NEWER_FILES_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
,
315 NO_OVERWRITE_DIR_OPTION
,
316 NO_QUOTE_CHARS_OPTION
,
318 NO_SAME_OWNER_OPTION
,
319 NO_SAME_PERMISSIONS_OPTION
,
321 NO_SELINUX_CONTEXT_OPTION
,
323 NO_WILDCARDS_MATCH_SLASH_OPTION
,
327 NUMERIC_OWNER_OPTION
,
330 ONE_FILE_SYSTEM_OPTION
,
331 ONE_TOP_LEVEL_OPTION
,
332 OVERWRITE_DIR_OPTION
,
339 QUOTING_STYLE_OPTION
,
342 RECURSIVE_UNLINK_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
,
355 SPARSE_VERSION_OPTION
,
356 STRIP_COMPONENTS_OPTION
,
366 WILDCARDS_MATCH_SLASH_OPTION
,
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\
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")
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");
394 Available option letters are DEQY and eqy. Consider the following
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)
401 [q alias for --occurrence=1 =/= this would better be used for quiet?]
403 y per-file gzip compression
404 Y per-block gzip compression.
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.
411 static struct argp_option options
[] = {
414 N_("Main operation mode:"), GRID
},
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 },
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 },
441 N_("Operation modifiers:"), GRID
},
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"),
468 {"check-device", CHECK_DEVICE_OPTION
, NULL
, 0,
469 N_("check device numbers when creating incremental archives (default)"),
475 N_("Overwrite control:"), GRID
},
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"),
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)"),
500 {"keep-directory-symlink", KEEP_DIRECTORY_SYMLINK_OPTION
, 0, 0,
501 N_("preserve existing symlinks to directories when extracting"),
503 {"one-top-level", ONE_TOP_LEVEL_OPTION
, N_("DIR"), OPTION_ARG_OPTIONAL
,
504 N_("create a subdirectory to avoid having loose files extracted"),
510 N_("Select output stream:"), GRID
},
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 },
524 N_("Handling of file attributes:"), GRID
},
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 },
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)"),
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,
566 N_("directory sorting order: none (default), name or inode"
568 N_("directory sorting order: none (default) or name"
575 N_("Handling of extended file attributes:"), GRID
},
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 },
597 N_("Device selection and switching:"), GRID
},
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 },
608 {"-[0-7][lmh]", 0, NULL
, OPTION_DOC
, /* It is OK, since 'name' will never be
610 N_("specify drive and density"), GRID
+1 },
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 },
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 },
636 N_("Device blocking:"), GRID
},
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 },
650 N_("Archive format selection:"), GRID
},
652 {"format", 'H', N_("FORMAT"), 0,
653 N_("create archive of the given format"), GRID
+1 },
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"),
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 },
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 },
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"),
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 },
704 N_("Local file selection:"), GRID
},
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"),
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 },
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"),
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 },
781 N_("File name matching options (affect both exclude and include patterns):"),
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 },
803 N_("Informative output:"), GRID
},
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)"),
812 {"checkpoint-action", CHECKPOINT_ACTION_OPTION
, N_("ACTION"), 0,
813 N_("execute ACTION on each checkpoint"),
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"),
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 },
853 N_("Compatibility options:"), GRID
},
856 N_("when creating, same as --old-archive; when extracting, same as --no-same-owner"), GRID
+1 },
861 N_("Other options:"), GRID
},
863 {"restrict", RESTRICT_OPTION
, 0, 0,
864 N_("disable use of some potentially harmful options"), -1 },
870 static char const *const atime_preserve_args
[] =
872 "replace", "system", NULL
875 static enum atime_preserve
const atime_preserve_types
[] =
877 replace_atime_preserve
, system_atime_preserve
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
);
884 /* Wildcard matching settings */
887 default_wildcards
, /* For exclusion == enable_wildcards,
888 for inclusion == disable_wildcards */
893 struct tar_args
/* Variables used during option parsing */
895 struct textual_date
*textual_date
; /* Keeps the arguments to --newer-mtime
896 and/or --date option if they are
898 enum wildcards wildcards
; /* Wildcard settings (--wildcards/
900 int matching_flags
; /* exclude_fnmatch options */
901 int include_anchored
; /* Pattern anchoring options used for
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 */
913 #define MAKE_EXCL_OPTIONS(args) \
914 ((((args)->wildcards != disable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
915 | (args)->matching_flags \
918 #define MAKE_INCL_OPTIONS(args) \
919 ((((args)->wildcards == enable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
920 | (args)->include_anchored \
921 | (args)->matching_flags \
924 static char const * const vcs_file_table
[] = {
956 static char const * const backup_file_table
[] = {
964 add_exclude_array (char const * const * fv
, int opts
)
968 for (i
= 0; fv
[i
]; i
++)
969 add_exclude (excluded
, fv
[i
], opts
);
974 format_default_settings (void)
977 "--format=%s -f%s -b%d --quoting-style=%s --rmt-command=%s"
982 archive_format_string (DEFAULT_ARCHIVE_FORMAT
),
983 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
,
984 quoting_style_args
[DEFAULT_QUOTING_STYLE
],
994 set_subcommand_option (enum subcommand subcommand
)
996 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
997 && subcommand_option
!= subcommand
)
999 _("You may not specify more than one '-Acdtrux', '--delete' or '--test-label' option")));
1001 subcommand_option
= subcommand
;
1005 set_use_compress_program_option (const char *string
)
1007 if (use_compress_program_option
1008 && strcmp (use_compress_program_option
, string
) != 0)
1009 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
1011 use_compress_program_option
= string
;
1017 compute_duration ();
1018 print_total_stats ();
1019 #ifndef HAVE_SIGACTION
1020 signal (signo
, sigstat
);
1025 stat_on_signal (int signo
)
1027 #ifdef HAVE_SIGACTION
1029 # define SA_RESTART 0
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
);
1037 signal (signo
, sigstat
);
1042 set_stat_signal (const char *name
)
1044 static struct sigtab
1048 } const sigtab
[] = {
1049 { "SIGUSR1", SIGUSR1
},
1050 { "USR1", SIGUSR1
},
1051 { "SIGUSR2", SIGUSR2
},
1052 { "USR2", SIGUSR2
},
1053 { "SIGHUP", SIGHUP
},
1055 { "SIGINT", SIGINT
},
1057 { "SIGQUIT", SIGQUIT
},
1060 struct sigtab
const *p
;
1062 for (p
= sigtab
; p
< sigtab
+ sizeof (sigtab
) / sizeof (sigtab
[0]); p
++)
1063 if (strcmp (p
->name
, name
) == 0)
1065 stat_on_signal (p
->signo
);
1068 FATAL_ERROR ((0, 0, _("Unknown signal name: %s"), name
));
1074 struct textual_date
*next
;
1081 get_date_or_file (struct tar_args
*args
, const char *option
,
1082 const char *str
, struct timespec
*ts
)
1084 if (FILE_SYSTEM_PREFIX_LEN (str
) != 0
1089 if (stat (str
, &st
) != 0)
1092 USAGE_ERROR ((0, 0, _("Date sample file not found")));
1094 *ts
= get_stat_mtime (&st
);
1098 if (! parse_datetime (ts
, str
, NULL
))
1100 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
1101 tartime (*ts
, false), quote (str
)));
1107 struct textual_date
*p
= xmalloc (sizeof (*p
));
1110 p
->date
= xstrdup (str
);
1111 p
->next
= args
->textual_date
;
1112 args
->textual_date
= p
;
1119 report_textual_dates (struct tar_args
*args
)
1121 struct textual_date
*p
;
1122 for (p
= args
->textual_date
; p
; )
1124 struct textual_date
*next
= p
->next
;
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
));
1139 static bool files_from_option
; /* When set, tar will not refuse to create
1142 /* Default density numbers for [0-9][lmh] device specifications */
1144 #if defined DEVICE_PREFIX && !defined DENSITY_LETTER
1145 # ifndef LOW_DENSITY_NUM
1146 # define LOW_DENSITY_NUM 0
1149 # ifndef MID_DENSITY_NUM
1150 # define MID_DENSITY_NUM 8
1153 # ifndef HIGH_DENSITY_NUM
1154 # define HIGH_DENSITY_NUM 16
1160 tar_help_filter (int key
, const char *text
, void *input
)
1172 s
= xasprintf (_("filter the archive through %s"), BZIP2_PROGRAM
);
1176 s
= xasprintf (_("filter the archive through %s"), GZIP_PROGRAM
);
1180 s
= xasprintf (_("filter the archive through %s"), COMPRESS_PROGRAM
);
1184 s
= xasprintf (_("filter the archive through %s"), LZIP_PROGRAM
);
1188 s
= xasprintf (_("filter the archive through %s"), LZMA_PROGRAM
);
1192 s
= xasprintf (_("filter the archive through %s"), LZOP_PROGRAM
);
1195 s
= xasprintf (_("filter the archive through %s"), XZ_PROGRAM
);
1198 case ARGP_KEY_HELP_EXTRA
:
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
);
1221 expand_pax_option (struct tar_args
*targs
, const char *arg
)
1226 obstack_init (&stk
);
1229 size_t seglen
= strcspn (arg
, ",");
1230 char *p
= memchr (arg
, '=', seglen
);
1233 size_t len
= p
- arg
+ 1;
1234 obstack_grow (&stk
, arg
, len
);
1236 for (++p
; *p
&& isspace ((unsigned char) *p
); p
++)
1238 if (*p
== '{' && p
[len
-1] == '}')
1241 char *tmp
= xmalloc (len
);
1242 memcpy (tmp
, p
+ 1, len
-2);
1244 if (get_date_or_file (targs
, "--pax-option", tmp
, &ts
) == 0)
1246 char buf
[TIMESPEC_STRSIZE_BOUND
];
1247 char const *s
= code_timespec (ts
, buf
);
1248 obstack_grow (&stk
, s
, strlen (s
));
1251 obstack_grow (&stk
, p
, len
);
1255 obstack_grow (&stk
, p
, len
);
1258 obstack_grow (&stk
, arg
, seglen
);
1263 obstack_1grow (&stk
, *arg
);
1267 obstack_1grow (&stk
, 0);
1268 res
= xstrdup (obstack_finish (&stk
));
1269 obstack_free (&stk
, NULL
);
1275 parse_owner_group (char *arg
, uintmax_t field_max
, char const **name_option
)
1277 uintmax_t u
= UINTMAX_MAX
;
1279 char const *name
= 0;
1280 char const *invalid_num
= 0;
1281 char *colon
= strchr (arg
, ':');
1285 char const *num
= colon
+ 1;
1289 if (num
&& (! (xstrtoumax (num
, &end
, 10, &u
, "") == LONGINT_OK
1290 && u
<= field_max
)))
1296 switch ('0' <= *arg
&& *arg
<= '9'
1297 ? xstrtoumax (arg
, &end
, 10, &u1
, "")
1305 if (u1
<= field_max
)
1311 case LONGINT_OVERFLOW
:
1318 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (invalid_num
),
1319 _("Invalid owner or group ID")));
1321 *name_option
= name
;
1325 #define TAR_SIZE_SUFFIXES "bBcGgkKMmPTtw"
1327 /* Either NL or NUL, as decided by the --null option. */
1328 static char filename_terminator
;
1330 static char const *const sort_mode_arg
[] = {
1337 static int sort_mode_flag
[] = {
1343 ARGMATCH_VERIFY (sort_mode_arg
, sort_mode_flag
);
1346 parse_opt (int key
, char *arg
, struct argp_state
*state
)
1348 struct tar_args
*args
= state
->input
;
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;
1359 set_subcommand_option (CAT_SUBCOMMAND
);
1363 args
->compress_autodetect
= true;
1366 case NO_AUTO_COMPRESS_OPTION
:
1367 args
->compress_autodetect
= false;
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")));
1383 /* Try to reblock input records. For reading 4.2BSD pipes. */
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. */
1390 read_full_records_option
= true;
1394 set_subcommand_option (CREATE_SUBCOMMAND
);
1402 set_subcommand_option (DIFF_SUBCOMMAND
);
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]));
1411 archive_name_array
[archive_names
++] = arg
;
1415 /* Since -F is only useful with -M, make it implied. Run this
1416 script at the end of each tape. */
1418 info_script_option
= arg
;
1419 multi_volume_option
= true;
1422 case FULL_TIME_OPTION
:
1423 full_time_option
= true;
1427 listed_incremental_option
= arg
;
1428 after_date_option
= true;
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. */
1436 incremental_option
= true;
1440 /* Follow symbolic links. */
1441 dereference_option
= true;
1444 case HARD_DEREFERENCE_OPTION
:
1445 hard_dereference_option
= true;
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. */
1453 ignore_zeros_option
= true;
1457 set_use_compress_program_option (BZIP2_PROGRAM
);
1461 set_use_compress_program_option (XZ_PROGRAM
);
1465 /* Don't replace existing files. */
1466 old_files_option
= KEEP_OLD_FILES
;
1470 starting_file_option
= true;
1471 addname (arg
, 0, true, NULL
);
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;
1480 case ONE_TOP_LEVEL_OPTION
:
1481 one_top_level_option
= true;
1482 one_top_level_dir
= arg
;
1486 check_links_option
= 1;
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
;
1500 tape_length_option
= (tarlong
) u
;
1501 multi_volume_option
= true;
1508 incremental_level
= strtoul (arg
, &p
, 10);
1510 USAGE_ERROR ((0, 0, _("Invalid incremental level value")));
1515 set_use_compress_program_option (LZIP_PROGRAM
);
1519 set_use_compress_program_option (LZMA_PROGRAM
);
1523 set_use_compress_program_option (LZOP_PROGRAM
);
1527 touch_option
= true;
1531 /* Make multivolume archive: when we can't write any more into
1532 the archive, re-open it, and continue writing. */
1534 multi_volume_option
= true;
1538 get_date_or_file (args
, "--mtime", arg
, &mtime_option
);
1539 set_mtime_option
= true;
1546 case NO_SEEK_OPTION
:
1551 after_date_option
= true;
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
);
1563 args
->o_option
= true;
1567 to_stdout_option
= true;
1571 same_permissions_option
= true;
1575 absolute_names_option
= true;
1579 set_subcommand_option (APPEND_SUBCOMMAND
);
1583 /* Print block numbers for debugging bad tar archives. */
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. */
1590 block_number_option
= true;
1594 /* Names to extract are sorted. */
1596 same_order_option
= true;
1600 sparse_option
= true;
1603 case SKIP_OLD_FILES_OPTION
:
1604 old_files_option
= SKIP_OLD_FILES
;
1607 case SPARSE_VERSION_OPTION
:
1608 sparse_option
= true;
1611 tar_sparse_major
= strtoul (arg
, &p
, 10);
1615 USAGE_ERROR ((0, 0, _("Invalid sparse version value")));
1616 tar_sparse_minor
= strtoul (p
+ 1, &p
, 10);
1618 USAGE_ERROR ((0, 0, _("Invalid sparse version value")));
1624 set_subcommand_option (LIST_SUBCOMMAND
);
1628 case TEST_LABEL_OPTION
:
1629 set_subcommand_option (TEST_LABEL_SUBCOMMAND
);
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
1637 files_from_option
= true;
1641 set_subcommand_option (UPDATE_SUBCOMMAND
);
1645 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1654 warning_option
|= WARN_VERBOSE_WARNINGS
;
1658 volume_label_option
= arg
;
1662 interactive_option
= true;
1666 verify_option
= true;
1670 set_subcommand_option (EXTRACT_SUBCOMMAND
);
1674 if (add_exclude_file (add_exclude
, excluded
, arg
,
1675 MAKE_EXCL_OPTIONS (args
), '\n')
1679 FATAL_ERROR ((0, e
, "%s", quotearg_colon (arg
)));
1684 set_use_compress_program_option (GZIP_PROGRAM
);
1688 set_use_compress_program_option (COMPRESS_PROGRAM
);
1691 case ANCHORED_OPTION
:
1692 args
->matching_flags
|= EXCLUDE_ANCHORED
;
1695 case ATIME_PRESERVE_OPTION
:
1696 atime_preserve_option
=
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
)
1703 _("--atime-preserve='system' is not supported"
1704 " on this platform")));
1707 case CHECK_DEVICE_OPTION
:
1708 check_device_option
= true;
1711 case NO_CHECK_DEVICE_OPTION
:
1712 check_device_option
= false;
1715 case CHECKPOINT_OPTION
:
1722 checkpoint_compile_action (".");
1725 checkpoint_option
= strtoul (arg
, &p
, 0);
1728 _("--checkpoint value is not an integer")));
1731 checkpoint_option
= DEFAULT_CHECKPOINT
;
1734 case CHECKPOINT_ACTION_OPTION
:
1735 checkpoint_compile_action (arg
);
1739 backup_option
= true;
1741 args
->version_control_string
= arg
;
1744 case DELAY_DIRECTORY_RESTORE_OPTION
:
1745 delay_directory_restore_option
= true;
1748 case NO_DELAY_DIRECTORY_RESTORE_OPTION
:
1749 delay_directory_restore_option
= false;
1753 set_subcommand_option (DELETE_SUBCOMMAND
);
1756 case EXCLUDE_BACKUPS_OPTION
:
1757 add_exclude_array (backup_file_table
, EXCLUDE_WILDCARDS
);
1760 case EXCLUDE_OPTION
:
1761 add_exclude (excluded
, arg
, MAKE_EXCL_OPTIONS (args
));
1764 case EXCLUDE_CACHES_OPTION
:
1765 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_contents
,
1769 case EXCLUDE_CACHES_UNDER_OPTION
:
1770 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_under
,
1774 case EXCLUDE_CACHES_ALL_OPTION
:
1775 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_all
,
1779 case EXCLUDE_TAG_OPTION
:
1780 add_exclusion_tag (arg
, exclusion_tag_contents
, NULL
);
1783 case EXCLUDE_TAG_UNDER_OPTION
:
1784 add_exclusion_tag (arg
, exclusion_tag_under
, NULL
);
1787 case EXCLUDE_TAG_ALL_OPTION
:
1788 add_exclusion_tag (arg
, exclusion_tag_all
, NULL
);
1791 case EXCLUDE_VCS_OPTION
:
1792 add_exclude_array (vcs_file_table
, 0);
1795 case FORCE_LOCAL_OPTION
:
1796 force_local_option
= true;
1800 set_archive_format (arg
);
1803 case INDEX_FILE_OPTION
:
1804 index_file_name
= arg
;
1807 case IGNORE_CASE_OPTION
:
1808 args
->matching_flags
|= FNM_CASEFOLD
;
1811 case IGNORE_COMMAND_ERROR_OPTION
:
1812 ignore_command_error_option
= true;
1815 case IGNORE_FAILED_READ_OPTION
:
1816 ignore_failed_read_option
= true;
1819 case KEEP_DIRECTORY_SYMLINK_OPTION
:
1820 keep_directory_symlink_option
= true;
1823 case KEEP_NEWER_FILES_OPTION
:
1824 old_files_option
= KEEP_NEWER_FILES
;
1829 uintmax_t u
= parse_owner_group (arg
, TYPE_MAXIMUM (gid_t
),
1830 &group_name_option
);
1831 if (u
== UINTMAX_MAX
)
1834 if (group_name_option
)
1835 gname_to_gid (group_name_option
, &group_option
);
1843 mode_option
= mode_compile (arg
);
1845 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
1846 initial_umask
= umask (0);
1847 umask (initial_umask
);
1850 case NO_ANCHORED_OPTION
:
1851 args
->include_anchored
= 0; /* Clear the default for comman line args */
1852 args
->matching_flags
&= ~ EXCLUDE_ANCHORED
;
1855 case NO_IGNORE_CASE_OPTION
:
1856 args
->matching_flags
&= ~ FNM_CASEFOLD
;
1859 case NO_IGNORE_COMMAND_ERROR_OPTION
:
1860 ignore_command_error_option
= false;
1863 case NO_OVERWRITE_DIR_OPTION
:
1864 old_files_option
= NO_OVERWRITE_DIR_OLD_FILES
;
1867 case NO_QUOTE_CHARS_OPTION
:
1869 set_char_quoting (NULL
, *arg
, 0);
1872 case NO_WILDCARDS_OPTION
:
1873 args
->wildcards
= disable_wildcards
;
1876 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
1877 args
->matching_flags
|= FNM_FILE_NAME
;
1881 filename_terminator
= '\0';
1884 case NO_NULL_OPTION
:
1885 filename_terminator
= '\n';
1888 case NUMERIC_OWNER_OPTION
:
1889 numeric_owner_option
= true;
1892 case OCCURRENCE_OPTION
:
1894 occurrence_option
= 1;
1898 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
1899 occurrence_option
= u
;
1901 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1902 _("Invalid number")));
1906 case OLD_ARCHIVE_OPTION
:
1907 set_archive_format ("v7");
1910 case OVERWRITE_DIR_OPTION
:
1911 old_files_option
= DEFAULT_OLD_FILES
;
1914 case OVERWRITE_OPTION
:
1915 old_files_option
= OVERWRITE_OLD_FILES
;
1920 uintmax_t u
= parse_owner_group (arg
, TYPE_MAXIMUM (uid_t
),
1921 &owner_name_option
);
1922 if (u
== UINTMAX_MAX
)
1925 if (owner_name_option
)
1926 uname_to_uid (owner_name_option
, &owner_option
);
1933 case QUOTE_CHARS_OPTION
:
1935 set_char_quoting (NULL
, *arg
, 1);
1938 case QUOTING_STYLE_OPTION
:
1939 tar_set_quoting_style (arg
);
1944 char *tmp
= expand_pax_option (args
, arg
);
1945 args
->pax_option
= true;
1946 xheader_set_option (tmp
);
1952 set_archive_format ("posix");
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")));
1963 case RECORD_SIZE_OPTION
:
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")));
1972 if (record_size
% BLOCKSIZE
!= 0)
1973 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1975 blocking_factor
= record_size
/ BLOCKSIZE
;
1979 case RECURSIVE_UNLINK_OPTION
:
1980 recursive_unlink_option
= true;
1983 case REMOVE_FILES_OPTION
:
1984 remove_files_option
= true;
1987 case RESTRICT_OPTION
:
1988 restrict_option
= true;
1991 case RMT_COMMAND_OPTION
:
1995 case RSH_COMMAND_OPTION
:
1996 rsh_command_option
= arg
;
1999 case SHOW_DEFAULTS_OPTION
:
2001 char *s
= format_default_settings ();
2008 case SHOW_SNAPSHOT_FIELD_RANGES_OPTION
:
2009 show_snapshot_field_ranges ();
2013 case STRIP_COMPONENTS_OPTION
:
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
;
2024 case SHOW_OMITTED_DIRS_OPTION
:
2025 show_omitted_dirs_option
= true;
2028 case SHOW_TRANSFORMED_NAMES_OPTION
:
2029 show_transformed_names_option
= true;
2033 savedir_sort_order
= XARGMATCH ("--sort", arg
,
2034 sort_mode_arg
, sort_mode_flag
);
2038 backup_option
= true;
2039 args
->backup_suffix_string
= arg
;
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
;
2050 set_stat_signal (arg
);
2052 totals_option
= true;
2055 case TRANSFORM_OPTION
:
2056 set_transform_expr (arg
);
2060 set_use_compress_program_option (arg
);
2063 case VOLNO_FILE_OPTION
:
2064 volno_file_option
= arg
;
2067 case WILDCARDS_OPTION
:
2068 args
->wildcards
= enable_wildcards
;
2071 case WILDCARDS_MATCH_SLASH_OPTION
:
2072 args
->matching_flags
&= ~ FNM_FILE_NAME
;
2075 case NO_RECURSION_OPTION
:
2076 recursion_option
= 0;
2079 case NO_SAME_OWNER_OPTION
:
2080 same_owner_option
= -1;
2083 case NO_SAME_PERMISSIONS_OPTION
:
2084 same_permissions_option
= -1;
2088 set_archive_format ("posix");
2092 case NO_ACLS_OPTION
:
2096 case SELINUX_CONTEXT_OPTION
:
2097 set_archive_format ("posix");
2098 selinux_context_option
= 1;
2101 case NO_SELINUX_CONTEXT_OPTION
:
2102 selinux_context_option
= -1;
2106 set_xattr_option (1);
2109 case NO_XATTR_OPTION
:
2110 set_xattr_option (-1);
2115 set_xattr_option (1);
2116 xattrs_mask_add (arg
, (key
== XATTR_INCLUDE
));
2119 case RECURSION_OPTION
:
2120 recursion_option
= FNM_LEADING_DIR
;
2123 case SAME_OWNER_OPTION
:
2124 same_owner_option
= 1;
2127 case UNQUOTE_OPTION
:
2128 unquote_option
= true;
2131 case NO_UNQUOTE_OPTION
:
2132 unquote_option
= false;
2135 case WARNING_OPTION
:
2136 set_warning_option (arg
);
2148 #ifdef DEVICE_PREFIX
2150 int device
= key
- '0';
2152 static char buf
[sizeof DEVICE_PREFIX
+ 10];
2156 argp_error (state
, _("Malformed density argument: %s"), quote (arg
));
2158 strcpy (buf
, DEVICE_PREFIX
);
2159 cursor
= buf
+ strlen (buf
);
2161 #ifdef DENSITY_LETTER
2163 sprintf (cursor
, "%d%c", device
, arg
[0]);
2165 #else /* not DENSITY_LETTER */
2170 device
+= LOW_DENSITY_NUM
;
2174 device
+= MID_DENSITY_NUM
;
2178 device
+= HIGH_DENSITY_NUM
;
2182 argp_error (state
, _("Unknown density: '%c'"), arg
[0]);
2184 sprintf (cursor
, "%d", device
);
2186 #endif /* not DENSITY_LETTER */
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
);
2196 #else /* not DEVICE_PREFIX */
2199 _("Options '-[0-7][lmh]' not supported by *this* tar"));
2201 #endif /* not DEVICE_PREFIX */
2204 return ARGP_ERR_UNKNOWN
;
2209 static struct argp argp
= {
2222 argp_help (&argp
, stderr
, ARGP_HELP_SEE
, (char*) program_name
);
2227 /* Parse the options for tar. */
2229 static struct argp_option
*
2230 find_argp_option (struct argp_option
*o
, int letter
)
2237 && o
->doc
== NULL
); o
++)
2238 if (o
->key
== letter
)
2243 static const char *tar_authors
[] = {
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
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
2270 /* Return t if the subcommand_option is in class(es) f */
2271 #define IS_SUBCOMMAND_CLASS(f) (subcommand_class[subcommand_option] & (f))
2273 static struct tar_args args
;
2276 option_conflict_error (const char *a
, const char *b
)
2278 /* TRANSLATORS: Both %s in this statement are replaced with
2280 USAGE_ERROR ((0, 0, _("'%s' cannot be used with '%s'"), a
, b
));
2284 decode_options (int argc
, char **argv
)
2288 argp_version_setup ("tar", tar_authors
);
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;
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;
2314 savedir_sort_order
= SAVEDIR_SORT_NONE
;
2316 owner_option
= -1; owner_name_option
= NULL
;
2317 group_option
= -1; group_name_option
= NULL
;
2319 check_device_option
= true;
2321 incremental_level
= -1;
2325 /* Convert old-style tar call by exploding option element and rearranging
2326 options accordingly. */
2328 if (argc
> 1 && argv
[1][0] != '-')
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 */
2337 /* Initialize a constructed option. */
2342 /* Allocate a new argument array, and copy program name in it. */
2344 new_argc
= argc
- 1 + strlen (argv
[1]);
2345 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (char *));
2350 /* Copy each old letter option as a separate option, and have the
2351 corresponding argument moved next to it. */
2353 for (letter
= *in
++; *letter
; letter
++)
2355 struct argp_option
*opt
;
2357 buffer
[1] = *letter
;
2358 *out
++ = xstrdup (buffer
);
2359 opt
= find_argp_option (options
, *letter
);
2360 if (opt
&& opt
->arg
)
2362 if (in
< argv
+ argc
)
2365 USAGE_ERROR ((0, 0, _("Old option '%c' requires an argument."),
2370 /* Copy all remaining options. */
2372 while (in
< argv
+ argc
)
2376 /* Replace the old option list by the new one. */
2382 /* Parse all options and non-options as they appear. */
2384 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
2386 if (argp_parse (&argp
, argc
, argv
, ARGP_IN_ORDER
, &idx
, &args
))
2387 exit (TAREXIT_FAILURE
);
2389 /* Special handling for 'o' option:
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).
2395 The old GNU tar semantics is retained when used with --create
2396 option, otherwise UNIX98 semantics is assumed */
2400 if (subcommand_option
== CREATE_SUBCOMMAND
)
2402 /* GNU Tar <= 1.13 compatibility */
2403 set_archive_format ("v7");
2407 /* UNIX98 compatibility */
2408 same_owner_option
= -1;
2412 /* Handle operands after any "--" argument. */
2413 for (; idx
< argc
; idx
++)
2415 name_add_name (argv
[idx
], MAKE_INCL_OPTIONS (&args
));
2416 args
.input_files
= true;
2419 /* Warn about implicit use of the wildcards in command line arguments.
2421 warn_regex_usage
= args
.wildcards
== default_wildcards
;
2423 /* Derive option values and check option consistency. */
2425 if (archive_format
== DEFAULT_FORMAT
)
2427 if (args
.pax_option
)
2428 archive_format
= POSIX_FORMAT
;
2430 archive_format
= DEFAULT_ARCHIVE_FORMAT
;
2433 if ((volume_label_option
&& subcommand_option
== CREATE_SUBCOMMAND
)
2434 || incremental_option
2435 || multi_volume_option
2437 assert_format (FORMAT_MASK (OLDGNU_FORMAT
)
2438 | FORMAT_MASK (GNU_FORMAT
)
2439 | FORMAT_MASK (POSIX_FORMAT
));
2441 if (occurrence_option
)
2443 if (!args
.input_files
)
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
));
2451 if (archive_names
== 0)
2453 /* If no archive file name given, try TAPE from the environment, or
2454 else, DEFAULT_ARCHIVE from the configuration process. */
2457 archive_name_array
[0] = getenv ("TAPE");
2458 if (! archive_name_array
[0])
2459 archive_name_array
[0] = DEFAULT_ARCHIVE
;
2462 /* Allow multiple archives only with '-M'. */
2464 if (archive_names
> 1 && !multi_volume_option
)
2466 _("Multiple archive files require '-M' option")));
2468 if (listed_incremental_option
2469 && NEWER_OPTION_INITIALIZED (newer_mtime_option
))
2470 option_conflict_error ("--listed-incremental", "--newer");
2472 if (incremental_level
!= -1 && !listed_incremental_option
)
2474 _("--level is meaningless without --listed-incremental")));
2476 if (volume_label_option
)
2478 if (archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
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 */)
2489 if (volume_label_max_len
< strlen (volume_label_option
))
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
));
2498 Label length in PAX format is limited by the volume size. */
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
));
2512 if (use_compress_program_option
)
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")));
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.
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")));
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")));
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")));
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")));
2548 if (starting_file_option
&& !IS_SUBCOMMAND_CLASS (SUBCL_READ
))
2549 option_conflict_error ("--starting-file",
2550 subcommand_string (subcommand_option
));
2552 if (same_order_option
&& !IS_SUBCOMMAND_CLASS (SUBCL_READ
))
2553 option_conflict_error ("--same-order",
2554 subcommand_string (subcommand_option
));
2556 if (one_top_level_option
)
2560 if (absolute_names_option
)
2561 option_conflict_error ("--one-top-level", "--absolute-names");
2563 if (!one_top_level_dir
)
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
);
2571 if (!one_top_level_dir
)
2573 _("Cannot deduce top-level directory name; "
2574 "please set it explicitly with --one-top-level=DIR")));
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
;
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. */
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
;
2593 fstatat_flags
= dereference_option
? 0 : AT_SYMLINK_NOFOLLOW
;
2595 if (subcommand_option
== TEST_LABEL_SUBCOMMAND
)
2597 /* --test-label is silent if the user has specified the label name to
2599 if (!args
.input_files
)
2602 else if (utc_option
)
2605 if (tape_length_option
&& tape_length_option
< record_size
)
2606 USAGE_ERROR ((0, 0, _("Volume length cannot be less than record size")));
2608 if (same_order_option
&& listed_incremental_option
)
2609 option_conflict_error ("--preserve-order", "--listed-incremental");
2611 /* Forbid using -c with no input files whatsoever. Check that '-f -',
2612 explicit or implied, is used correctly. */
2614 switch (subcommand_option
)
2616 case CREATE_SUBCOMMAND
:
2617 if (!args
.input_files
&& !files_from_option
)
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
);
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");
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
, "-"))
2645 _("Options '-Aru' are incompatible with '-f -'")));
2651 /* Initialize stdlis */
2652 if (index_file_name
)
2654 stdlis
= fopen (index_file_name
, "w");
2656 open_fatal (index_file_name
);
2659 stdlis
= to_stdout_option
? stderr
: stdout
;
2661 archive_name_cursor
= archive_name_array
;
2663 /* Prepare for generating backup names. */
2665 if (args
.backup_suffix_string
)
2666 simple_backup_suffix
= xstrdup (args
.backup_suffix_string
);
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;
2677 checkpoint_finish_compile ();
2679 report_textual_dates (&args
);
2683 more_options (int argc
, char **argv
)
2686 if (argp_parse (&argp
, argc
, argv
, ARGP_IN_ORDER
,
2688 exit (TAREXIT_FAILURE
);
2693 /* Main routine for tar. */
2695 main (int argc
, char **argv
)
2698 set_program_name (argv
[0]);
2700 setlocale (LC_ALL
, "");
2701 bindtextdomain (PACKAGE
, LOCALEDIR
);
2702 textdomain (PACKAGE
);
2704 exit_failure
= TAREXIT_FAILURE
;
2705 exit_status
= TAREXIT_SUCCESS
;
2706 error_hook
= checkpoint_flush_actions
;
2708 filename_terminator
= '\n';
2709 set_quoting_style (0, DEFAULT_QUOTING_STYLE
);
2711 /* Make sure we have first three descriptors available */
2714 /* Pre-allocate a few structures. */
2716 allocated_archive_names
= 10;
2717 archive_name_array
=
2718 xmalloc (sizeof (const char *) * allocated_archive_names
);
2721 /* System V fork+wait does not work if SIGCHLD is ignored. */
2722 signal (SIGCHLD
, SIG_DFL
);
2724 /* Try to disable the ability to unlink a directory. */
2725 priv_set_remove_linkdir ();
2727 /* Decode options. */
2729 decode_options (argc
, argv
);
2733 /* Main command execution. */
2735 if (volno_file_option
)
2736 init_volume_number ();
2738 switch (subcommand_option
)
2740 case UNKNOWN_SUBCOMMAND
:
2742 _("You must specify one of the '-Acdtrux', '--delete' or '--test-label' options")));
2744 case CAT_SUBCOMMAND
:
2745 case UPDATE_SUBCOMMAND
:
2746 case APPEND_SUBCOMMAND
:
2750 case DELETE_SUBCOMMAND
:
2751 delete_archive_members ();
2754 case CREATE_SUBCOMMAND
:
2758 case EXTRACT_SUBCOMMAND
:
2760 read_and (extract_archive
);
2762 /* FIXME: should extract_finish () even if an ordinary signal is
2768 case LIST_SUBCOMMAND
:
2769 read_and (list_archive
);
2772 case DIFF_SUBCOMMAND
:
2774 read_and (diff_archive
);
2777 case TEST_LABEL_SUBCOMMAND
:
2778 test_archive_label ();
2781 checkpoint_finish ();
2784 print_total_stats ();
2786 if (check_links_option
)
2789 if (volno_file_option
)
2790 closeout_volume_number ();
2792 /* Dispose of allocated memory, and return. */
2794 free (archive_name_array
);
2795 xattrs_clear_setup ();
2798 if (exit_status
== TAREXIT_FAILURE
)
2799 error (0, 0, _("Exiting with failure status due to previous errors"));
2801 if (stdlis
== stdout
)
2803 else if (ferror (stderr
) || fclose (stderr
) != 0)
2804 set_exit_status (TAREXIT_FAILURE
);
2810 tar_stat_init (struct tar_stat_info
*st
)
2812 memset (st
, 0, sizeof (*st
));
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. */
2819 tar_stat_close (struct tar_stat_info
*st
)
2821 int status
= (st
->dirstream
? closedir (st
->dirstream
)
2822 : 0 < st
->fd
? close (st
->fd
)
2831 close_diag (st
->orig_file_name
);
2837 tar_stat_destroy (struct tar_stat_info
*st
)
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
);
2846 free (st
->cntx_name
);
2847 free (st
->acls_a_ptr
);
2848 free (st
->acls_d_ptr
);
2849 free (st
->sparse_map
);
2851 xheader_destroy (&st
->xhdr
);
2852 memset (st
, 0, sizeof (*st
));
2855 /* Format mask for all available formats that support nanosecond
2856 timestamp resolution. */
2857 #define NS_PRECISION_FORMAT_MASK FORMAT_MASK (POSIX_FORMAT)
2859 /* Same as timespec_cmp, but ignore nanoseconds if current archive
2860 format does not provide sufficient resolution. */
2862 tar_timespec_cmp (struct timespec a
, struct timespec b
)
2864 if (!(FORMAT_MASK (current_format
) & NS_PRECISION_FORMAT_MASK
))
2865 a
.tv_nsec
= b
.tv_nsec
= 0;
2866 return timespec_cmp (a
, b
);
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. */
2873 set_exit_status (int val
)
2875 if (val
> exit_status
)