1 /* A tar (tape archiver) program.
3 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
4 2001, 2003, 2004, 2005 Free 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 2, 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, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 #if ! defined SIGCHLD && defined SIGCLD
29 # define SIGCHLD SIGCLD
32 /* The following causes "common.h" to produce definitions of all the global
33 variables, rather than just "extern" declarations of them. GNU tar does
34 depend on the system loader to preset all GLOBAL variables to neutral (or
35 zero) values; explicit initialization is usually not done. */
40 #include <localedir.h>
47 /* Local declarations. */
49 #ifndef DEFAULT_ARCHIVE_FORMAT
50 # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
53 #ifndef DEFAULT_ARCHIVE
54 # define DEFAULT_ARCHIVE "tar.out"
57 #ifndef DEFAULT_BLOCKING
58 # define DEFAULT_BLOCKING 20
64 /* Name of option using stdin. */
65 static const char *stdin_used_by
;
67 /* Doesn't return if stdin already requested. */
69 request_stdin (const char *option
)
72 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
73 stdin_used_by
, option
));
75 stdin_used_by
= option
;
78 extern int rpmatch (char const *response
);
80 /* Returns true if and only if the user typed an affirmative response. */
82 confirm (const char *message_action
, const char *message_name
)
84 static FILE *confirm_file
;
85 static int confirm_file_EOF
;
90 if (archive
== 0 || stdin_used_by
)
92 confirm_file
= fopen (TTY_NAME
, "r");
94 open_fatal (TTY_NAME
);
103 fprintf (stdlis
, "%s %s?", message_action
, quote (message_name
));
106 if (!confirm_file_EOF
)
108 char *response
= NULL
;
109 size_t response_size
= 0;
110 if (getline (&response
, &response_size
, confirm_file
) < 0)
111 confirm_file_EOF
= 1;
113 status
= rpmatch (response
) > 0;
117 if (confirm_file_EOF
)
119 fputc ('\n', stdlis
);
126 static struct fmttab
{
128 enum archive_format fmt
;
131 { "oldgnu", OLDGNU_FORMAT
},
132 { "ustar", USTAR_FORMAT
},
133 { "posix", POSIX_FORMAT
},
134 #if 0 /* not fully supported yet */
135 { "star", STAR_FORMAT
},
137 { "gnu", GNU_FORMAT
},
138 { "pax", POSIX_FORMAT
}, /* An alias for posix */
143 set_archive_format (char const *name
)
145 struct fmttab
const *p
;
147 for (p
= fmttab
; strcmp (p
->name
, name
) != 0; )
149 USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
150 quotearg_colon (name
)));
152 archive_format
= p
->fmt
;
156 archive_format_string (enum archive_format fmt
)
158 struct fmttab
const *p
;
160 for (p
= fmttab
; p
->name
; p
++)
166 #define FORMAT_MASK(n) (1<<(n))
169 assert_format(unsigned fmt_mask
)
171 if ((FORMAT_MASK (archive_format
) & fmt_mask
) == 0)
173 _("GNU features wanted on incompatible archive format")));
182 ANCHORED_OPTION
= CHAR_MAX
+ 1,
183 ATIME_PRESERVE_OPTION
,
189 EXCLUDE_CACHES_OPTION
,
194 IGNORE_COMMAND_ERROR_OPTION
,
195 IGNORE_FAILED_READ_OPTION
,
197 KEEP_NEWER_FILES_OPTION
,
202 NO_IGNORE_CASE_OPTION
,
203 NO_IGNORE_COMMAND_ERROR_OPTION
,
204 NO_OVERWRITE_DIR_OPTION
,
206 NO_SAME_OWNER_OPTION
,
207 NO_SAME_PERMISSIONS_OPTION
,
210 NO_WILDCARDS_MATCH_SLASH_OPTION
,
212 NUMERIC_OWNER_OPTION
,
215 ONE_FILE_SYSTEM_OPTION
,
223 RECURSIVE_UNLINK_OPTION
,
228 SHOW_DEFAULTS_OPTION
,
229 SHOW_OMITTED_DIRS_OPTION
,
230 SHOW_STORED_NAMES_OPTION
,
231 STRIP_COMPONENTS_OPTION
,
238 USE_COMPRESS_PROGRAM_OPTION
,
243 WILDCARDS_MATCH_SLASH_OPTION
246 const char *argp_program_version
= "tar (" PACKAGE_NAME
") " VERSION
;
247 const char *argp_program_bug_address
= "<" PACKAGE_BUGREPORT
">";
248 static char doc
[] = N_("GNU `tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.\n\
251 tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
252 tar -tvf archive.tar # List all files in archive.tar verbosely.\n\
253 tar -xf archive.tar # Extract all files from archive.tar.\n\
254 \vThe backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
255 The version control may be set with --backup or VERSION_CONTROL, values are:\n\n\
256 none, off never make backups\n\
257 t, numbered make numbered backups\n\
258 nil, existing numbered if numbered backups exist, simple otherwise\n\
259 never, simple always make simple backups\n");
264 Available option letters are DEIJQY and aeqy. Consider the following
267 [For Solaris tar compatibility =/= Is it important at all?]
268 e exit immediately with a nonzero exit status if unexpected errors occur
269 E use extended headers (--format=posix)
271 [q alias for --occurrence=1 =/= this would better be used for quiet?]
272 [I same as T =/= will harm star compatibility]
274 y per-file gzip compression
275 Y per-block gzip compression */
277 static struct argp_option options
[] = {
279 N_("Main operation mode:"), 0},
282 N_("list the contents of an archive"), 10 },
283 {"extract", 'x', 0, 0,
284 N_("extract files from an archive"), 10 },
285 {"get", 0, 0, OPTION_ALIAS
, NULL
, 0 },
286 {"create", 'c', 0, 0,
287 N_("create a new archive"), 10 },
289 N_("find differences between archive and file system"), 10 },
290 {"compare", 0, 0, OPTION_ALIAS
, NULL
, 10},
291 {"append", 'r', 0, 0,
292 N_("append files to the end of an archive"), 10 },
293 {"update", 'u', 0, 0,
294 N_("only append files newer than copy in archive"), 10 },
295 {"catenate", 'A', 0, 0,
296 N_("append tar files to an archive"), 10 },
297 {"concatenate", 0, 0, OPTION_ALIAS
, NULL
, 10},
298 {"delete", DELETE_OPTION
, 0, 0,
299 N_("delete from the archive (not on mag tapes!)"), 10 },
300 {"test-label", TEST_LABEL_OPTION
, NULL
, 0,
301 N_("Test archive volume label and exit"), 10 },
304 N_("Operation modifiers:"), 20},
306 {"sparse", 'S', 0, 0,
307 N_("handle sparse files efficiently"), 21 },
308 {"incremental", 'G', 0, 0,
309 N_("handle old GNU-format incremental backup"), 21 },
310 {"listed-incremental", 'g', N_("FILE"), 0,
311 N_("handle new GNU-format incremental backup"), 21 },
312 {"ignore-failed-read", IGNORE_FAILED_READ_OPTION
, 0, 0,
313 N_("do not exit with nonzero on unreadable files"), 21 },
314 {"occurrence", OCCURRENCE_OPTION
, N_("NUMBER"), OPTION_ARG_OPTIONAL
,
315 N_("process only the NUMBERth occurrence of each file in the archive. This option is valid only in conjunction with one of the subcommands --delete, --diff, --extract or --list and when a list of files is given either on the command line or via -T option. NUMBER defaults to 1."), 21 },
316 {"seek", 'n', NULL
, 0,
317 N_("archive is seekable"), 21 },
320 N_("Overwrite control:"), 30},
322 {"verify", 'W', 0, 0,
323 N_("attempt to verify the archive after writing it"), 31 },
324 {"remove-files", REMOVE_FILES_OPTION
, 0, 0,
325 N_("remove files after adding them to the archive"), 31 },
326 {"keep-old-files", 'k', 0, 0,
327 N_("don't replace existing files when extracting"), 31 },
328 {"keep-newer-files", KEEP_NEWER_FILES_OPTION
, 0, 0,
329 N_("don't replace existing files that are newer than their archive copies"), 31 },
330 {"overwrite", OVERWRITE_OPTION
, 0, 0,
331 N_("overwrite existing files when extracting"), 31 },
332 {"unlink-first", 'U', 0, 0,
333 N_("remove each file prior to extracting over it"), 31 },
334 {"recursive-unlink", RECURSIVE_UNLINK_OPTION
, 0, 0,
335 N_("empty hierarchies prior to extracting directory"), 31 },
336 {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION
, 0, 0,
337 N_("preserve metadata of existing directories"), 31 },
340 N_("Select output stream:"), 40},
342 {"to-stdout", 'O', 0, 0,
343 N_("extract files to standard output"), 41 },
344 {"to-command", TO_COMMAND_OPTION
, N_("COMMAND"), 0,
345 N_("pipe extracted files to another program"), 41 },
346 {"ignore-command-error", IGNORE_COMMAND_ERROR_OPTION
, 0, 0,
347 N_("ignore exit codes of children"), 41 },
348 {"no-ignore-command-error", NO_IGNORE_COMMAND_ERROR_OPTION
, 0, 0,
349 N_("treat non-zero exit codes of children as error"), 41 },
352 N_("Handling of file attributes:"), 50 },
354 {"owner", OWNER_OPTION
, N_("NAME"), 0,
355 N_("force NAME as owner for added files"), 51 },
356 {"group", GROUP_OPTION
, N_("NAME"), 0,
357 N_("force NAME as group for added files"), 51 },
358 {"mode", MODE_OPTION
, N_("CHANGES"), 0,
359 N_("force (symbolic) mode CHANGES for added files"), 51 },
360 {"atime-preserve", ATIME_PRESERVE_OPTION
, 0, 0,
361 N_("don't change access times on dumped files"), 51 },
363 N_("don't extract file modified time"), 51 },
364 {"same-owner", SAME_OWNER_OPTION
, 0, 0,
365 N_("try extracting files with the same ownership"), 51 },
366 {"no-same-owner", NO_SAME_OWNER_OPTION
, 0, 0,
367 N_("extract files as yourself"), 51 },
368 {"numeric-owner", NUMERIC_OWNER_OPTION
, 0, 0,
369 N_("always use numbers for user/group names"), 51 },
370 {"preserve-permissions", 'p', 0, 0,
371 N_("extract information about file permissions (default for superuser)"),
373 {"same-permissions", 0, 0, OPTION_ALIAS
, NULL
, 51 },
374 {"no-same-permissions", NO_SAME_PERMISSIONS_OPTION
, 0, 0,
375 N_("apply the user's umask when extracting permissions from the archive (default for ordinary users)"), 51 },
376 {"preserve-order", 's', 0, 0,
377 N_("sort names to extract to match archive"), 51 },
378 {"same-order", 0, 0, OPTION_ALIAS
, NULL
, 51 },
379 {"preserve", PRESERVE_OPTION
, 0, 0,
380 N_("same as both -p and -s"), 51 },
383 N_("Device selection and switching:"), 60 },
385 {"file", 'f', N_("ARCHIVE"), 0,
386 N_("use archive file or device ARCHIVE"), 61 },
387 {"force-local", FORCE_LOCAL_OPTION
, 0, 0,
388 N_("archive file is local even if it has a colon"), 61 },
389 {"rmt-command", RMT_COMMAND_OPTION
, N_("COMMAND"), 0,
390 N_("use given rmt COMMAND instead of rmt"), 61 },
391 {"rsh-command", RSH_COMMAND_OPTION
, N_("COMMAND"), 0,
392 N_("use remote COMMAND instead of rsh"), 61 },
394 {"-[0-7][lmh]", 0, NULL
, OPTION_DOC
, /* It is OK, since `name' will never be
396 N_("specify drive and density"), 61 },
398 {NULL
, '0', NULL
, OPTION_HIDDEN
, NULL
, 61 },
399 {NULL
, '1', NULL
, OPTION_HIDDEN
, NULL
, 61 },
400 {NULL
, '2', NULL
, OPTION_HIDDEN
, NULL
, 61 },
401 {NULL
, '3', NULL
, OPTION_HIDDEN
, NULL
, 61 },
402 {NULL
, '4', NULL
, OPTION_HIDDEN
, NULL
, 61 },
403 {NULL
, '5', NULL
, OPTION_HIDDEN
, NULL
, 61 },
404 {NULL
, '6', NULL
, OPTION_HIDDEN
, NULL
, 61 },
405 {NULL
, '7', NULL
, OPTION_HIDDEN
, NULL
, 61 },
406 {NULL
, '8', NULL
, OPTION_HIDDEN
, NULL
, 61 },
407 {NULL
, '9', NULL
, OPTION_HIDDEN
, NULL
, 61 },
409 {"multi-volume", 'M', 0, 0,
410 N_("create/list/extract multi-volume archive"), 61 },
411 {"tape-length", 'L', N_("NUMBER"), 0,
412 N_("change tape after writing NUMBER x 1024 bytes"), 61 },
413 {"info-script", 'F', N_("NAME"), 0,
414 N_("run script at end of each tape (implies -M)"), 61 },
415 {"new-volume-script", 0, 0, OPTION_ALIAS
, NULL
, 61 },
416 {"volno-file", VOLNO_FILE_OPTION
, N_("FILE"), 0,
417 N_("use/update the volume number in FILE"), 61 },
420 N_("Device blocking:"), 70 },
422 {"blocking-factor", 'b', N_("BLOCKS"), 0,
423 N_("BLOCKS x 512 bytes per record"), 71 },
424 {"record-size", RECORD_SIZE_OPTION
, N_("NUMBER"), 0,
425 N_("NUMBER of bytes per record, multiple of 512"), 71 },
426 {"ignore-zeros", 'i', 0, 0,
427 N_("ignore zeroed blocks in archive (means EOF)"), 71 },
428 {"read-full-records", 'B', 0, 0,
429 N_("reblock as we read (for 4.2BSD pipes)"), 71 },
432 N_("Archive format selection:"), 80 },
434 {"format", 'H', N_("FORMAT"), 0,
435 N_("create archive of the given format."), 81 },
437 {NULL
, 0, NULL
, 0, N_("FORMAT is one of the following:"), 82 },
438 {" v7", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
, N_("old V7 tar format"), 83},
439 {" oldgnu", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
,
440 N_("GNU format as per tar <= 1.12"), 83},
441 {" gnu", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
,
442 N_("GNU tar 1.13.x format"), 83},
443 {" ustar", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
,
444 N_("POSIX 1003.1-1988 (ustar) format"), 83 },
445 {" pax", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
,
446 N_("POSIX 1003.1-2001 (pax) format"), 83 },
447 {" posix", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
, N_("same as pax"), 83 },
449 {"old-archive", OLD_ARCHIVE_OPTION
, 0, 0, /* FIXME */
450 N_("same as --format=v7"), 88 },
451 {"portability", 0, 0, OPTION_ALIAS
, NULL
, 88 },
452 {"posix", POSIX_OPTION
, 0, 0,
453 N_("same as --format=posix"), 88 },
454 {"pax-option", PAX_OPTION
, N_("keyword[[:]=value][,keyword[[:]=value], ...]"), 0,
455 N_("control pax keywords"), 88 },
456 {"label", 'V', N_("TEXT"), 0,
457 N_("create archive with volume name TEXT. At list/extract time, use TEXT as a globbing pattern for volume name"), 88 },
459 N_("filter the archive through bzip2"), 88 },
461 N_("filter the archive through gzip"), 88 },
462 {"gunzip", 0, 0, OPTION_ALIAS
, NULL
, 88 },
463 {"ungzip", 0, 0, OPTION_ALIAS
, NULL
, 88 },
464 {"compress", 'Z', 0, 0,
465 N_("filter the archive through compress"), 88 },
466 {"uncompress", 0, 0, OPTION_ALIAS
, NULL
, 88 },
467 {"use-compress-program", USE_COMPRESS_PROGRAM_OPTION
, N_("PROG"), 0,
468 N_("filter through PROG (must accept -d)"), 88 },
471 N_("Local file selection:"), 90 },
473 {"add-file", ARGP_KEY_ARG
, N_("FILE"), 0,
474 N_("add given FILE to the archive (useful if its name starts with a dash)"), 91},
475 {"directory", 'C', N_("DIR"), 0,
476 N_("change to directory DIR"), 91 },
477 {"files-from", 'T', N_("FILE"), 0,
478 N_("get names to extract or create from FILE"), 91 },
479 {"null", NULL_OPTION
, 0, 0,
480 N_("-T reads null-terminated names, disable -C"), 91 },
481 {"unquote", UNQUOTE_OPTION
, 0, 0,
482 N_("unquote filenames read with -T (default)"), 91 },
483 {"no-unquote", NO_UNQUOTE_OPTION
, 0, 0,
484 N_("do not unquote filenames read with -T"), 91 },
485 {"exclude", EXCLUDE_OPTION
, N_("PATTERN"), 0,
486 N_("exclude files, given as a PATTERN"), 91 },
487 {"exclude-from", 'X', N_("FILE"), 0,
488 N_("exclude patterns listed in FILE"), 91 },
489 {"exclude-caches", EXCLUDE_CACHES_OPTION
, 0, 0,
490 N_("exclude directories containing a cache tag"), 91 },
491 {"ignore-case", IGNORE_CASE_OPTION
, 0, 0,
492 N_("exclusion ignores case"), 91 },
493 {"anchored", ANCHORED_OPTION
, 0, 0,
494 N_("exclude patterns match file name start"), 91 },
495 {"no-anchored", NO_ANCHORED_OPTION
, 0, 0,
496 N_("exclude patterns match after any `/' (default)"), 91 },
497 {"no-ignore-case", NO_IGNORE_CASE_OPTION
, 0, 0,
498 N_("exclusion is case sensitive (default)"), 91 },
499 {"no-wildcards", NO_WILDCARDS_OPTION
, 0, 0,
500 N_("exclude patterns are plain strings"), 91 },
501 {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION
, 0, 0,
502 N_("exclude pattern wildcards do not match `/'"), 91 },
503 {"no-recursion", NO_RECURSION_OPTION
, 0, 0,
504 N_("avoid descending automatically in directories"), 91 },
505 {"one-file-system", ONE_FILE_SYSTEM_OPTION
, 0, 0,
506 N_("stay in local file system when creating archive"), 91 },
507 {NULL
, 'l', 0, OPTION_HIDDEN
, "", 91},
508 {"recursion", RECURSION_OPTION
, 0, 0,
509 N_("recurse into directories (default)"), 91 },
510 {"absolute-names", 'P', 0, 0,
511 N_("don't strip leading `/'s from file names"), 91 },
512 {"dereference", 'h', 0, 0,
513 N_("follow symlinks; archive and dump the files they point to"), 91 },
514 {"starting-file", 'K', N_("MEMBER-NAME"), 0,
515 N_("begin at member MEMBER-NAME in the archive"), 91 },
516 {"strip-components", STRIP_COMPONENTS_OPTION
, N_("NUMBER"), 0,
517 N_("strip NUMBER leading components from file names"), 91 },
518 {"newer", 'N', N_("DATE-OR-FILE"), 0,
519 N_("only store files newer than DATE-OR-FILE"), 91 },
520 {"newer-mtime", NEWER_MTIME_OPTION
, N_("DATE"), 0,
521 N_("compare date and time when data changed only"), 91 },
522 {"after-date", 'N', N_("DATE"), 0,
523 N_("same as -N"), 91 },
524 {"backup", BACKUP_OPTION
, N_("CONTROL"), OPTION_ARG_OPTIONAL
,
525 N_("backup before removal, choose version CONTROL"), 91 },
526 {"suffix", SUFFIX_OPTION
, N_("STRING"), 0,
527 N_("backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX)"), 91 },
528 {"wildcards", WILDCARDS_OPTION
, 0, 0,
529 N_("exclude patterns use wildcards (default)"), 91 },
530 {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION
, 0, 0,
531 N_("exclude pattern wildcards match `/' (default)"), 91 },
534 N_("Informative output:"), 100 },
536 {"verbose", 'v', 0, 0,
537 N_("verbosely list files processed"), 101 },
538 {"checkpoint", CHECKPOINT_OPTION
, 0, 0,
539 N_("display progress messages every 10th record"), 101 },
540 {"check-links", CHECK_LINKS_OPTION
, 0, 0,
541 N_("print a message if not all links are dumped"), 102 },
542 {"totals", TOTALS_OPTION
, 0, 0,
543 N_("print total bytes written while creating archive"), 102 },
544 {"utc", UTC_OPTION
, 0, 0,
545 N_("print file modification dates in UTC"), 102 },
546 {"index-file", INDEX_FILE_OPTION
, N_("FILE"), 0,
547 N_("send verbose output to FILE"), 102 },
548 {"block-number", 'R', 0, 0,
549 N_("show block number within archive with each message"), 102 },
550 {"interactive", 'w', 0, 0,
551 N_("ask for confirmation for every action"), 102 },
552 {"confirmation", 0, 0, OPTION_ALIAS
, NULL
, 102 },
553 {"show-defaults", SHOW_DEFAULTS_OPTION
, 0, 0,
554 N_("Show tar defaults"), 102 },
555 {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION
, 0, 0,
556 N_("When listing or extracting, list each directory that does not match search criteria"), 102 },
557 {"show-stored-names", SHOW_STORED_NAMES_OPTION
, 0, 0,
558 N_("When creating archive in verbose mode, list member names as stored in the archive"),
562 N_("Compatibility options:"), 110 },
565 N_("when creating, same as --old-archive. When extracting, same as --no-same-owner"), 111 },
568 N_("Other options:"), 120 },
570 {"help", '?', 0, 0, N_("Give this help list"), -1},
571 {"usage", USAGE_OPTION
, 0, 0, N_("Give a short usage message"), -1},
572 {"license", LICENSE_OPTION
, 0, 0, N_("Print license and exit"), -1},
573 {"version", VERSION_OPTION
, 0, 0, N_("Print program version"), -1},
574 /* FIXME -V (--label) conflicts with the default short option for
576 {"HANG", HANG_OPTION
, "SECS", OPTION_ARG_OPTIONAL
| OPTION_HIDDEN
,
577 N_("Hang for SECS seconds (default 3600)"), 0},
582 char const *textual_date_option
;
586 char const *backup_suffix_string
;
587 char const *version_control_string
;
592 show_default_settings (FILE *stream
)
595 "--format=%s -f%s -b%d --rmt-command=%s",
596 archive_format_string (DEFAULT_ARCHIVE_FORMAT
),
597 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
,
598 DEFAULT_RMT_COMMAND
);
600 fprintf (stream
, " --rsh-command=%s", REMOTE_SHELL
);
602 fprintf (stream
, "\n");
606 set_subcommand_option (enum subcommand subcommand
)
608 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
609 && subcommand_option
!= subcommand
)
611 _("You may not specify more than one `-Acdtrux' option")));
613 subcommand_option
= subcommand
;
617 set_use_compress_program_option (const char *string
)
619 if (use_compress_program_option
620 && strcmp (use_compress_program_option
, string
) != 0)
621 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
623 use_compress_program_option
= string
;
629 printf ("tar (%s) %s\n%s\n", PACKAGE_NAME
, PACKAGE_VERSION
,
630 "Copyright (C) 2004 Free Software Foundation, Inc.\n");
631 puts (_("Based on the work of John Gilmore and Jay Fenlason. See AUTHORS\n\
632 for complete list of authors.\n"));
633 printf (_(" GNU tar is free software; you can redistribute it and/or modify\n"
634 " it under the terms of the GNU General Public License as published by\n"
635 " the Free Software Foundation; either version 2 of the License, or\n"
636 " (at your option) any later version.\n"
638 " GNU tar is distributed in the hope that it will be useful,\n"
639 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
640 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
641 " GNU General Public License for more details.\n"
643 " You should have received a copy of the GNU General Public License\n"
644 " along with GNU tar; if not, write to the Free Software Foundation,\n"
645 " Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\n\n"));
649 static volatile int _argp_hang
;
651 enum read_file_list_state
/* Result of reading file name from the list file */
653 file_list_success
, /* OK, name read successfully */
654 file_list_end
, /* End of list file */
655 file_list_zero
/* Zero separator encountered where it should not */
658 /* Read from FP a sequence of characters up to FILENAME_TERMINATOR and put them
661 static enum read_file_list_state
662 read_name_from_file (FILE *fp
, struct obstack
*stk
)
667 for (c
= getc (fp
); c
!= EOF
&& c
!= filename_terminator
; c
= getc (fp
))
671 /* We have read a zero separator. The file possibly is zero-separated */
672 /* FATAL_ERROR((0, 0, N_("file name contains null character"))); */
673 return file_list_zero
;
675 obstack_1grow (stk
, c
);
679 obstack_1grow (stk
, 0);
681 return (counter
== 0 && c
== EOF
) ? file_list_end
: file_list_success
;
685 static bool files_from_option
; /* When set, tar will not refuse to create
687 static struct obstack argv_stk
; /* Storage for additional command line options
688 read using -T option */
690 /* Prevent recursive inclusion of the same file */
693 struct file_id_list
*next
;
698 static struct file_id_list
*file_id_list
;
701 add_file_id (const char *filename
)
703 struct file_id_list
*p
;
706 if (stat (filename
, &st
))
707 stat_fatal (filename
);
708 for (p
= file_id_list
; p
; p
= p
->next
)
709 if (p
->ino
== st
.st_ino
&& p
->dev
== st
.st_dev
)
711 FATAL_ERROR ((0, 0, _("%s: file list already read"),
712 quotearg_colon (filename
)));
714 p
= xmalloc (sizeof *p
);
715 p
->next
= file_id_list
;
722 update_argv (const char *filename
, struct argp_state
*state
)
729 bool is_stdin
= false;
730 enum read_file_list_state read_state
;
732 if (!strcmp (filename
, "-"))
735 request_stdin ("-T");
740 add_file_id (filename
);
741 if ((fp
= fopen (filename
, "r")) == NULL
)
742 open_fatal (filename
);
745 while ((read_state
= read_name_from_file (fp
, &argv_stk
)) == file_list_success
)
748 if (read_state
== file_list_zero
)
752 WARN ((0, 0, N_("%s: file name read contains nul character"),
753 quotearg_colon (filename
)));
755 /* Prepare new stack contents */
756 size
= obstack_object_size (&argv_stk
);
757 p
= obstack_finish (&argv_stk
);
758 for (; size
> 0; size
--, p
++)
760 obstack_1grow (&argv_stk
, *p
);
762 obstack_1grow (&argv_stk
, '\n');
763 obstack_1grow (&argv_stk
, 0);
766 /* Read rest of files using new filename terminator */
767 filename_terminator
= 0;
768 while (read_name_from_file (fp
, &argv_stk
) == file_list_success
)
778 start
= obstack_finish (&argv_stk
);
780 if (filename_terminator
== 0)
781 for (p
= start
; *p
; p
+= strlen (p
) + 1)
785 new_argc
= state
->argc
+ count
;
786 new_argv
= xmalloc (sizeof (state
->argv
[0]) * (new_argc
+ 1));
787 memcpy (new_argv
, state
->argv
, sizeof (state
->argv
[0]) * (state
->argc
+ 1));
788 state
->argv
= new_argv
;
789 memmove (&state
->argv
[state
->next
+ count
], &state
->argv
[state
->next
],
790 (state
->argc
- state
->next
+ 1) * sizeof (state
->argv
[0]));
792 state
->argc
= new_argc
;
794 for (i
= state
->next
, p
= start
; *p
; p
+= strlen (p
) + 1, i
++)
796 if (filename_terminator
== 0 && p
[0] == '-')
797 state
->argv
[i
++] = "--add-file";
804 parse_opt (int key
, char *arg
, struct argp_state
*state
)
806 struct tar_args
*args
= state
->input
;
811 /* File name or non-parsed option, because of ARGP_IN_ORDER */
817 set_subcommand_option (CAT_SUBCOMMAND
);
823 if (! (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
824 && u
== (blocking_factor
= u
)
825 && 0 < blocking_factor
826 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
827 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
828 _("Invalid blocking factor")));
833 /* Try to reblock input records. For reading 4.2BSD pipes. */
835 /* It would surely make sense to exchange -B and -R, but it seems
836 that -B has been used for a long while in Sun tar and most
837 BSD-derived systems. This is a consequence of the block/record
838 terminology confusion. */
840 read_full_records_option
= true;
844 set_subcommand_option (CREATE_SUBCOMMAND
);
853 set_subcommand_option (DIFF_SUBCOMMAND
);
857 if (archive_names
== allocated_archive_names
)
859 allocated_archive_names
*= 2;
861 xrealloc (archive_name_array
,
862 sizeof (const char *) * allocated_archive_names
);
864 archive_name_array
[archive_names
++] = arg
;
868 /* Since -F is only useful with -M, make it implied. Run this
869 script at the end of each tape. */
871 info_script_option
= arg
;
872 multi_volume_option
= true;
876 listed_incremental_option
= arg
;
877 after_date_option
= true;
881 /* We are making an incremental dump (FIXME: are we?); save
882 directories at the beginning of the archive, and include in each
883 directory its contents. */
885 incremental_option
= true;
889 /* Follow symbolic links. */
890 dereference_option
= true;
894 /* Ignore zero blocks (eofs). This can't be the default,
895 because Unix tar writes two blocks of zeros, then pads out
896 the record with garbage. */
898 ignore_zeros_option
= true;
903 _("Warning: the -I option is not supported;"
904 " perhaps you meant -j or -T?")));
908 set_use_compress_program_option ("bzip2");
912 /* Don't replace existing files. */
913 old_files_option
= KEEP_OLD_FILES
;
917 starting_file_option
= true;
922 /* Historically equivalent to --one-file-system. This usage is
923 incompatible with UNIX98 and POSIX specs and therefore is
924 deprecated. The semantics of -l option will be changed in
925 future versions. See TODO.
928 _("Semantics of -l option will change in the future releases.")));
930 _("Please use --one-file-system option instead.")));
932 case ONE_FILE_SYSTEM_OPTION
:
933 /* When dumping directories, don't dump files/subdirectories
934 that are on other filesystems. */
935 one_file_system_option
= true;
941 if (xstrtoumax (arg
, 0, 10, &u
, "") != LONGINT_OK
)
942 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
943 _("Invalid tape length")));
944 tape_length_option
= 1024 * (tarlong
) u
;
945 multi_volume_option
= true;
954 /* Make multivolume archive: when we can't write any more into
955 the archive, re-open it, and continue writing. */
957 multi_volume_option
= true;
961 seekable_archive
= true;
966 after_date_option
= true;
969 case NEWER_MTIME_OPTION
:
970 if (NEWER_OPTION_INITIALIZED (newer_mtime_option
))
971 USAGE_ERROR ((0, 0, _("More than one threshold date")));
973 if (FILE_SYSTEM_PREFIX_LEN (arg
) != 0
978 if (deref_stat (dereference_option
, arg
, &st
) != 0)
981 USAGE_ERROR ((0, 0, _("Date sample file not found")));
983 newer_mtime_option
= get_stat_mtime (&st
);
987 if (! get_date (&newer_mtime_option
, arg
, NULL
))
989 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
990 tartime (newer_mtime_option
, false), quote (arg
)));
991 newer_mtime_option
.tv_nsec
= 0;
994 args
->textual_date_option
= arg
;
998 #endif /* not MSDOS */
1001 args
->o_option
= true;
1005 to_stdout_option
= true;
1009 same_permissions_option
= true;
1013 absolute_names_option
= true;
1017 set_subcommand_option (APPEND_SUBCOMMAND
);
1021 /* Print block numbers for debugging bad tar archives. */
1023 /* It would surely make sense to exchange -B and -R, but it seems
1024 that -B has been used for a long while in Sun tar ans most
1025 BSD-derived systems. This is a consequence of the block/record
1026 terminology confusion. */
1028 block_number_option
= true;
1032 /* Names to extr are sorted. */
1034 same_order_option
= true;
1038 sparse_option
= true;
1042 set_subcommand_option (LIST_SUBCOMMAND
);
1046 case TEST_LABEL_OPTION
:
1047 set_subcommand_option (LIST_SUBCOMMAND
);
1048 test_label_option
= true;
1052 update_argv (arg
, state
);
1053 /* Indicate we've been given -T option. This is for backward
1054 compatibility only, so that `tar cfT archive /dev/null will
1056 files_from_option
= true;
1060 set_subcommand_option (UPDATE_SUBCOMMAND
);
1064 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1076 volume_label_option
= arg
;
1080 interactive_option
= true;
1084 verify_option
= true;
1088 set_subcommand_option (EXTRACT_SUBCOMMAND
);
1092 if (add_exclude_file (add_exclude
, excluded
, arg
,
1093 args
->exclude_options
| recursion_option
, '\n')
1097 FATAL_ERROR ((0, e
, "%s", quotearg_colon (arg
)));
1103 _("Warning: the -y option is not supported;"
1104 " perhaps you meant -j?")));
1108 set_use_compress_program_option ("gzip");
1112 set_use_compress_program_option ("compress");
1115 case ANCHORED_OPTION
:
1116 args
->exclude_options
|= EXCLUDE_ANCHORED
;
1119 case ATIME_PRESERVE_OPTION
:
1120 atime_preserve_option
= true;
1123 case CHECKPOINT_OPTION
:
1124 checkpoint_option
= true;
1128 backup_option
= true;
1130 args
->version_control_string
= arg
;
1134 set_subcommand_option (DELETE_SUBCOMMAND
);
1137 case EXCLUDE_OPTION
:
1138 add_exclude (excluded
, arg
, args
->exclude_options
| recursion_option
);
1141 case EXCLUDE_CACHES_OPTION
:
1142 exclude_caches_option
= true;
1145 case FORCE_LOCAL_OPTION
:
1146 force_local_option
= true;
1150 set_archive_format (arg
);
1153 case INDEX_FILE_OPTION
:
1154 index_file_name
= arg
;
1157 case IGNORE_CASE_OPTION
:
1158 args
->exclude_options
|= FNM_CASEFOLD
;
1161 case IGNORE_COMMAND_ERROR_OPTION
:
1162 ignore_command_error_option
= true;
1165 case IGNORE_FAILED_READ_OPTION
:
1166 ignore_failed_read_option
= true;
1169 case KEEP_NEWER_FILES_OPTION
:
1170 old_files_option
= KEEP_NEWER_FILES
;
1174 if (! (strlen (arg
) < GNAME_FIELD_SIZE
1175 && gname_to_gid (arg
, &group_option
)))
1178 if (xstrtoumax (arg
, 0, 10, &g
, "") == LONGINT_OK
1182 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1183 _("%s: Invalid group")));
1188 mode_option
= mode_compile (arg
);
1190 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
1191 initial_umask
= umask (0);
1192 umask (initial_umask
);
1195 case NO_ANCHORED_OPTION
:
1196 args
->exclude_options
&= ~ EXCLUDE_ANCHORED
;
1199 case NO_IGNORE_CASE_OPTION
:
1200 args
->exclude_options
&= ~ FNM_CASEFOLD
;
1203 case NO_IGNORE_COMMAND_ERROR_OPTION
:
1204 ignore_command_error_option
= false;
1207 case NO_OVERWRITE_DIR_OPTION
:
1208 old_files_option
= NO_OVERWRITE_DIR_OLD_FILES
;
1211 case NO_WILDCARDS_OPTION
:
1212 args
->exclude_options
&= ~ EXCLUDE_WILDCARDS
;
1215 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
1216 args
->exclude_options
|= FNM_FILE_NAME
;
1220 filename_terminator
= '\0';
1223 case NUMERIC_OWNER_OPTION
:
1224 numeric_owner_option
= true;
1227 case OCCURRENCE_OPTION
:
1229 occurrence_option
= 1;
1233 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
1234 occurrence_option
= u
;
1236 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1237 _("Invalid number")));
1241 case OVERWRITE_OPTION
:
1242 old_files_option
= OVERWRITE_OLD_FILES
;
1246 if (! (strlen (arg
) < UNAME_FIELD_SIZE
1247 && uname_to_uid (arg
, &owner_option
)))
1250 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
1254 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1255 _("Invalid owner")));
1261 xheader_set_option (arg
);
1265 set_archive_format ("posix");
1268 case PRESERVE_OPTION
:
1269 same_permissions_option
= true;
1270 same_order_option
= true;
1273 case RECORD_SIZE_OPTION
:
1276 if (! (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
1277 && u
== (size_t) u
))
1278 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1279 _("Invalid record size")));
1281 if (record_size
% BLOCKSIZE
!= 0)
1282 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1284 blocking_factor
= record_size
/ BLOCKSIZE
;
1288 case RECURSIVE_UNLINK_OPTION
:
1289 recursive_unlink_option
= true;
1292 case REMOVE_FILES_OPTION
:
1293 remove_files_option
= true;
1296 case RMT_COMMAND_OPTION
:
1300 case RSH_COMMAND_OPTION
:
1301 rsh_command_option
= arg
;
1304 case SHOW_DEFAULTS_OPTION
:
1305 show_default_settings (stdout
);
1308 case STRIP_COMPONENTS_OPTION
:
1311 if (! (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
1312 && u
== (size_t) u
))
1313 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1314 _("Invalid number of elements")));
1315 strip_name_components
= u
;
1319 case SHOW_OMITTED_DIRS_OPTION
:
1320 show_omitted_dirs_option
= true;
1323 case SHOW_STORED_NAMES_OPTION
:
1324 show_stored_names_option
= true;
1328 backup_option
= true;
1329 args
->backup_suffix_string
= arg
;
1332 case TO_COMMAND_OPTION
:
1333 if (to_command_option
)
1334 USAGE_ERROR ((0, 0, _("Only one --to-command option allowed")));
1335 to_command_option
= arg
;
1339 totals_option
= true;
1342 case USE_COMPRESS_PROGRAM_OPTION
:
1343 set_use_compress_program_option (arg
);
1346 case VOLNO_FILE_OPTION
:
1347 volno_file_option
= arg
;
1350 case WILDCARDS_OPTION
:
1351 args
->exclude_options
|= EXCLUDE_WILDCARDS
;
1354 case WILDCARDS_MATCH_SLASH_OPTION
:
1355 args
->exclude_options
&= ~ FNM_FILE_NAME
;
1358 case CHECK_LINKS_OPTION
:
1359 check_links_option
= 1;
1362 case NO_RECURSION_OPTION
:
1363 recursion_option
= 0;
1366 case NO_SAME_OWNER_OPTION
:
1367 same_owner_option
= -1;
1370 case NO_SAME_PERMISSIONS_OPTION
:
1371 same_permissions_option
= -1;
1374 case RECURSION_OPTION
:
1375 recursion_option
= FNM_LEADING_DIR
;
1378 case SAME_OWNER_OPTION
:
1379 same_owner_option
= 1;
1382 case UNQUOTE_OPTION
:
1383 unquote_option
= true;
1386 case NO_UNQUOTE_OPTION
:
1387 unquote_option
= false;
1399 #ifdef DEVICE_PREFIX
1401 int device
= key
- '0';
1403 static char buf
[sizeof DEVICE_PREFIX
+ 10];
1407 argp_error (state
, _("Malformed density argument: %s"), quote (arg
));
1409 strcpy (buf
, DEVICE_PREFIX
);
1410 cursor
= buf
+ strlen (buf
);
1412 #ifdef DENSITY_LETTER
1414 sprintf (cursor
, "%d%c", device
, arg
[0]);
1416 #else /* not DENSITY_LETTER */
1443 argp_error (state
, _("Unknown density: `%c'"), arg
[0]);
1445 sprintf (cursor
, "%d", device
);
1447 #endif /* not DENSITY_LETTER */
1449 if (archive_names
== allocated_archive_names
)
1451 allocated_archive_names
*= 2;
1452 archive_name_array
=
1453 xrealloc (archive_name_array
,
1454 sizeof (const char *) * allocated_archive_names
);
1456 archive_name_array
[archive_names
++] = xstrdup (buf
);
1460 #else /* not DEVICE_PREFIX */
1463 _("Options `-[0-7][lmh]' not supported by *this* tar"));
1465 #endif /* not DEVICE_PREFIX */
1468 state
->flags
|= ARGP_NO_EXIT
;
1469 argp_state_help (state
, state
->out_stream
,
1470 ARGP_HELP_STD_HELP
& ~ARGP_HELP_BUG_ADDR
);
1471 fprintf (state
->out_stream
, _("\n*This* tar defaults to:\n"));
1472 show_default_settings (state
->out_stream
);
1473 fprintf (state
->out_stream
, "\n");
1474 fprintf (state
->out_stream
, _("Report bugs to %s.\n"),
1475 argp_program_bug_address
);
1479 argp_state_help (state
, state
->out_stream
,
1480 ARGP_HELP_USAGE
| ARGP_HELP_EXIT_OK
);
1483 case VERSION_OPTION
:
1484 fprintf (state
->out_stream
, "%s\n", argp_program_version
);
1487 case LICENSE_OPTION
:
1492 _argp_hang
= atoi (arg
? arg
: "3600");
1493 while (_argp_hang
-- > 0)
1498 return ARGP_ERR_UNKNOWN
;
1503 static struct argp argp
= {
1516 argp_help (&argp
, stderr
, ARGP_HELP_SEE
, (char*) program_name
);
1520 /* Parse the options for tar. */
1522 static struct argp_option
*
1523 find_argp_option (struct argp_option
*options
, int letter
)
1526 !(options
->name
== NULL
1527 && options
->key
== 0
1528 && options
->arg
== 0
1529 && options
->flags
== 0
1530 && options
->doc
== NULL
); options
++)
1531 if (options
->key
== letter
)
1537 decode_options (int argc
, char **argv
)
1540 struct tar_args args
;
1542 /* Set some default option values. */
1543 args
.textual_date_option
= NULL
;
1544 args
.exclude_options
= EXCLUDE_WILDCARDS
;
1546 args
.pax_option
= 0;
1547 args
.backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
1548 args
.version_control_string
= 0;
1549 args
.input_files
= 0;
1551 subcommand_option
= UNKNOWN_SUBCOMMAND
;
1552 archive_format
= DEFAULT_FORMAT
;
1553 blocking_factor
= DEFAULT_BLOCKING
;
1554 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
1555 excluded
= new_exclude ();
1556 newer_mtime_option
.tv_sec
= TYPE_MINIMUM (time_t);
1557 newer_mtime_option
.tv_nsec
= -1;
1558 recursion_option
= FNM_LEADING_DIR
;
1559 unquote_option
= true;
1564 /* Convert old-style tar call by exploding option element and rearranging
1565 options accordingly. */
1567 if (argc
> 1 && argv
[1][0] != '-')
1569 int new_argc
; /* argc value for rearranged arguments */
1570 char **new_argv
; /* argv value for rearranged arguments */
1571 char *const *in
; /* cursor into original argv */
1572 char **out
; /* cursor into rearranged argv */
1573 const char *letter
; /* cursor into old option letters */
1574 char buffer
[3]; /* constructed option buffer */
1576 /* Initialize a constructed option. */
1581 /* Allocate a new argument array, and copy program name in it. */
1583 new_argc
= argc
- 1 + strlen (argv
[1]);
1584 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (char *));
1589 /* Copy each old letter option as a separate option, and have the
1590 corresponding argument moved next to it. */
1592 for (letter
= *in
++; *letter
; letter
++)
1594 struct argp_option
*opt
;
1596 buffer
[1] = *letter
;
1597 *out
++ = xstrdup (buffer
);
1598 opt
= find_argp_option (options
, *letter
);
1599 if (opt
&& opt
->arg
)
1601 if (in
< argv
+ argc
)
1604 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
1609 /* Copy all remaining options. */
1611 while (in
< argv
+ argc
)
1615 /* Replace the old option list by the new one. */
1621 /* Parse all options and non-options as they appear. */
1623 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
1625 if (argp_parse (&argp
, argc
, argv
, ARGP_IN_ORDER
|ARGP_NO_HELP
,
1630 /* Special handling for 'o' option:
1632 GNU tar used to say "output old format".
1633 UNIX98 tar says don't chown files after extracting (we use
1634 "--no-same-owner" for this).
1636 The old GNU tar semantics is retained when used with --create
1637 option, otherwise UNIX98 semantics is assumed */
1641 if (subcommand_option
== CREATE_SUBCOMMAND
)
1643 /* GNU Tar <= 1.13 compatibility */
1644 set_archive_format ("v7");
1648 /* UNIX98 compatibility */
1649 same_owner_option
= -1;
1653 /* Handle operands after any "--" argument. */
1654 for (; index
< argc
; index
++)
1656 name_add (argv
[index
]);
1660 /* Derive option values and check option consistency. */
1662 if (archive_format
== DEFAULT_FORMAT
)
1664 if (args
.pax_option
)
1665 archive_format
= POSIX_FORMAT
;
1667 archive_format
= DEFAULT_ARCHIVE_FORMAT
;
1670 if ((volume_label_option
&& subcommand_option
== CREATE_SUBCOMMAND
)
1671 || incremental_option
1672 || multi_volume_option
1674 assert_format (FORMAT_MASK (OLDGNU_FORMAT
)
1675 | FORMAT_MASK (GNU_FORMAT
)
1676 | FORMAT_MASK (POSIX_FORMAT
));
1678 if (occurrence_option
)
1680 if (!args
.input_files
)
1682 _("--occurrence is meaningless without a file list")));
1683 if (subcommand_option
!= DELETE_SUBCOMMAND
1684 && subcommand_option
!= DIFF_SUBCOMMAND
1685 && subcommand_option
!= EXTRACT_SUBCOMMAND
1686 && subcommand_option
!= LIST_SUBCOMMAND
)
1688 _("--occurrence cannot be used in the requested operation mode")));
1691 if (seekable_archive
&& subcommand_option
== DELETE_SUBCOMMAND
)
1693 /* The current code in delete.c is based on the assumption that
1694 skip_member() reads all data from the archive. So, we should
1695 make sure it won't use seeks. On the other hand, the same code
1696 depends on the ability to backspace a record in the archive,
1697 so setting seekable_archive to false is technically incorrect.
1698 However, it is tested only in skip_member(), so it's not a
1700 seekable_archive
= false;
1703 if (archive_names
== 0)
1705 /* If no archive file name given, try TAPE from the environment, or
1706 else, DEFAULT_ARCHIVE from the configuration process. */
1709 archive_name_array
[0] = getenv ("TAPE");
1710 if (! archive_name_array
[0])
1711 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1714 /* Allow multiple archives only with `-M'. */
1716 if (archive_names
> 1 && !multi_volume_option
)
1718 _("Multiple archive files require `-M' option")));
1720 if (listed_incremental_option
1721 && NEWER_OPTION_INITIALIZED (newer_mtime_option
))
1723 _("Cannot combine --listed-incremental with --newer")));
1725 if (volume_label_option
)
1727 if (archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1729 size_t volume_label_max_len
=
1730 (sizeof current_header
->header
.name
1731 - 1 /* for trailing '\0' */
1732 - (multi_volume_option
1733 ? (sizeof " Volume "
1734 - 1 /* for null at end of " Volume " */
1735 + INT_STRLEN_BOUND (int) /* for volume number */
1736 - 1 /* for sign, as 0 <= volno */)
1738 if (volume_label_max_len
< strlen (volume_label_option
))
1740 ngettext ("%s: Volume label is too long (limit is %lu byte)",
1741 "%s: Volume label is too long (limit is %lu bytes)",
1742 volume_label_max_len
),
1743 quotearg_colon (volume_label_option
),
1744 (unsigned long) volume_label_max_len
));
1747 Label length in PAX format is limited by the volume size. */
1752 if (multi_volume_option
)
1753 USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
1754 if (use_compress_program_option
)
1755 USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
1758 if (use_compress_program_option
)
1760 if (multi_volume_option
)
1761 USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
1762 if (subcommand_option
== UPDATE_SUBCOMMAND
1763 || subcommand_option
== APPEND_SUBCOMMAND
)
1764 USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
1765 if (subcommand_option
== CAT_SUBCOMMAND
)
1766 USAGE_ERROR ((0, 0, _("Cannot concatenate compressed archives")));
1769 /* It is no harm to use --pax-option on non-pax archives in archive
1770 reading mode. It may even be useful, since it allows to override
1771 file attributes from tar headers. Therefore I allow such usage.
1774 && archive_format
!= POSIX_FORMAT
1775 && (subcommand_option
!= EXTRACT_SUBCOMMAND
1776 || subcommand_option
!= DIFF_SUBCOMMAND
1777 || subcommand_option
!= LIST_SUBCOMMAND
))
1778 USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
1780 /* If ready to unlink hierarchies, so we are for simpler files. */
1781 if (recursive_unlink_option
)
1782 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1785 if (test_label_option
)
1787 /* --test-label is silent if the user has specified the label name to
1789 if (args
.input_files
== 0)
1792 else if (utc_option
)
1795 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1796 explicit or implied, is used correctly. */
1798 switch (subcommand_option
)
1800 case CREATE_SUBCOMMAND
:
1801 if (args
.input_files
== 0 && !files_from_option
)
1803 _("Cowardly refusing to create an empty archive")));
1806 case EXTRACT_SUBCOMMAND
:
1807 case LIST_SUBCOMMAND
:
1808 case DIFF_SUBCOMMAND
:
1809 for (archive_name_cursor
= archive_name_array
;
1810 archive_name_cursor
< archive_name_array
+ archive_names
;
1811 archive_name_cursor
++)
1812 if (!strcmp (*archive_name_cursor
, "-"))
1813 request_stdin ("-f");
1816 case CAT_SUBCOMMAND
:
1817 case UPDATE_SUBCOMMAND
:
1818 case APPEND_SUBCOMMAND
:
1819 for (archive_name_cursor
= archive_name_array
;
1820 archive_name_cursor
< archive_name_array
+ archive_names
;
1821 archive_name_cursor
++)
1822 if (!strcmp (*archive_name_cursor
, "-"))
1824 _("Options `-Aru' are incompatible with `-f -'")));
1830 archive_name_cursor
= archive_name_array
;
1832 /* Prepare for generating backup names. */
1834 if (args
.backup_suffix_string
)
1835 simple_backup_suffix
= xstrdup (args
.backup_suffix_string
);
1839 backup_type
= xget_version ("--backup", args
.version_control_string
);
1840 /* No backup is needed either if explicitely disabled or if
1841 the extracted files are not being written to disk. */
1842 if (backup_type
== no_backups
|| EXTRACT_OVER_PIPE
)
1843 backup_option
= false;
1846 if (verbose_option
&& args
.textual_date_option
)
1848 char const *treated_as
= tartime (newer_mtime_option
, true);
1849 if (strcmp (args
.textual_date_option
, treated_as
) != 0)
1850 WARN ((0, 0, _("Treating date `%s' as %s"),
1851 args
.textual_date_option
, treated_as
));
1858 /* Main routine for tar. */
1860 main (int argc
, char **argv
)
1863 program_name
= argv
[0];
1865 setlocale (LC_ALL
, "");
1866 bindtextdomain (PACKAGE
, LOCALEDIR
);
1867 textdomain (PACKAGE
);
1869 exit_status
= TAREXIT_SUCCESS
;
1870 filename_terminator
= '\n';
1871 set_quoting_style (0, escape_quoting_style
);
1873 /* Make sure we have first three descriptors available */
1876 /* Pre-allocate a few structures. */
1878 allocated_archive_names
= 10;
1879 archive_name_array
=
1880 xmalloc (sizeof (const char *) * allocated_archive_names
);
1883 obstack_init (&argv_stk
);
1886 /* System V fork+wait does not work if SIGCHLD is ignored. */
1887 signal (SIGCHLD
, SIG_DFL
);
1892 /* Decode options. */
1894 decode_options (argc
, argv
);
1897 /* Main command execution. */
1899 if (volno_file_option
)
1900 init_volume_number ();
1902 switch (subcommand_option
)
1904 case UNKNOWN_SUBCOMMAND
:
1906 _("You must specify one of the `-Acdtrux' options")));
1908 case CAT_SUBCOMMAND
:
1909 case UPDATE_SUBCOMMAND
:
1910 case APPEND_SUBCOMMAND
:
1914 case DELETE_SUBCOMMAND
:
1915 delete_archive_members ();
1918 case CREATE_SUBCOMMAND
:
1921 print_total_written ();
1924 case EXTRACT_SUBCOMMAND
:
1926 read_and (extract_archive
);
1928 /* FIXME: should extract_finish () even if an ordinary signal is
1934 case LIST_SUBCOMMAND
:
1935 read_and (list_archive
);
1938 case DIFF_SUBCOMMAND
:
1940 read_and (diff_archive
);
1944 if (check_links_option
)
1947 if (volno_file_option
)
1948 closeout_volume_number ();
1950 /* Dispose of allocated memory, and return. */
1952 free (archive_name_array
);
1955 if (stdlis
!= stderr
&& (ferror (stdlis
) || fclose (stdlis
) != 0))
1956 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1957 if (exit_status
== TAREXIT_FAILURE
)
1958 error (0, 0, _("Error exit delayed from previous errors"));
1959 if (ferror (stderr
) || fclose (stderr
) != 0)
1960 exit_status
= TAREXIT_FAILURE
;
1965 tar_stat_init (struct tar_stat_info
*st
)
1967 memset (st
, 0, sizeof (*st
));
1971 tar_stat_destroy (struct tar_stat_info
*st
)
1973 free (st
->orig_file_name
);
1974 free (st
->file_name
);
1975 free (st
->link_name
);
1978 free (st
->sparse_map
);
1980 memset (st
, 0, sizeof (*st
));
1983 /* Format mask for all available formats that support nanosecond
1984 timestamp resolution. */
1985 #define NS_PRECISION_FORMAT_MASK FORMAT_MASK (POSIX_FORMAT)
1987 /* Same as timespec_cmp, but ignore nanoseconds if current archive
1988 format does not provide sufficient resolution. */
1990 tar_timespec_cmp (struct timespec a
, struct timespec b
)
1992 if (!(FORMAT_MASK (current_format
) & NS_PRECISION_FORMAT_MASK
))
1993 a
.tv_nsec
= b
.tv_nsec
= 0;
1994 return timespec_cmp (a
, b
);