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>
46 /* Local declarations. */
48 #ifndef DEFAULT_ARCHIVE_FORMAT
49 # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
52 #ifndef DEFAULT_ARCHIVE
53 # define DEFAULT_ARCHIVE "tar.out"
56 #ifndef DEFAULT_BLOCKING
57 # define DEFAULT_BLOCKING 20
63 /* Name of option using stdin. */
64 static const char *stdin_used_by
;
66 /* Doesn't return if stdin already requested. */
68 request_stdin (const char *option
)
71 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
72 stdin_used_by
, option
));
74 stdin_used_by
= option
;
77 /* Returns true if and only if the user typed 'y' or 'Y'. */
79 confirm (const char *message_action
, const char *message_name
)
81 static FILE *confirm_file
;
82 static int confirm_file_EOF
;
86 if (archive
== 0 || stdin_used_by
)
88 confirm_file
= fopen (TTY_NAME
, "r");
90 open_fatal (TTY_NAME
);
99 fprintf (stdlis
, "%s %s?", message_action
, quote (message_name
));
103 int reply
= confirm_file_EOF
? EOF
: getc (confirm_file
);
106 for (character
= reply
;
108 character
= getc (confirm_file
))
109 if (character
== EOF
)
111 confirm_file_EOF
= 1;
112 fputc ('\n', stdlis
);
116 return reply
== 'y' || reply
== 'Y';
120 static struct fmttab
{
122 enum archive_format fmt
;
125 { "oldgnu", OLDGNU_FORMAT
},
126 { "ustar", USTAR_FORMAT
},
127 { "posix", POSIX_FORMAT
},
128 #if 0 /* not fully supported yet */
129 { "star", STAR_FORMAT
},
131 { "gnu", GNU_FORMAT
},
132 { "pax", POSIX_FORMAT
}, /* An alias for posix */
137 set_archive_format (char const *name
)
139 struct fmttab
const *p
;
141 for (p
= fmttab
; strcmp (p
->name
, name
) != 0; )
143 USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
144 quotearg_colon (name
)));
146 archive_format
= p
->fmt
;
150 archive_format_string (enum archive_format fmt
)
152 struct fmttab
const *p
;
154 for (p
= fmttab
; p
->name
; p
++)
160 #define FORMAT_MASK(n) (1<<(n))
163 assert_format(unsigned fmt_mask
)
165 if ((FORMAT_MASK(archive_format
) & fmt_mask
) == 0)
167 _("GNU features wanted on incompatible archive format")));
174 /* For long options that unconditionally set a single flag, we have getopt
175 do it. For the others, we share the code for the equivalent short
176 named option, the name of which is stored in the otherwise-unused `val'
177 field of the `struct option'; for long options that have no equivalent
178 short option, we use non-characters as pseudo short options,
179 starting at CHAR_MAX + 1 and going upwards. */
183 ANCHORED_OPTION
= CHAR_MAX
+ 1,
184 ATIME_PRESERVE_OPTION
,
190 EXCLUDE_CACHES_OPTION
,
195 IGNORE_COMMAND_ERROR_OPTION
,
196 IGNORE_FAILED_READ_OPTION
,
198 KEEP_NEWER_FILES_OPTION
,
203 NO_IGNORE_CASE_OPTION
,
204 NO_IGNORE_COMMAND_ERROR_OPTION
,
205 NO_OVERWRITE_DIR_OPTION
,
207 NO_SAME_OWNER_OPTION
,
208 NO_SAME_PERMISSIONS_OPTION
,
211 NO_WILDCARDS_MATCH_SLASH_OPTION
,
213 NUMERIC_OWNER_OPTION
,
216 ONE_FILE_SYSTEM_OPTION
,
224 RECURSIVE_UNLINK_OPTION
,
229 SHOW_DEFAULTS_OPTION
,
230 SHOW_OMITTED_DIRS_OPTION
,
231 STRIP_COMPONENTS_OPTION
,
237 USE_COMPRESS_PROGRAM_OPTION
,
242 WILDCARDS_MATCH_SLASH_OPTION
245 const char *argp_program_version
= "tar (" PACKAGE_NAME
") " VERSION
;
246 const char *argp_program_bug_address
= "<" PACKAGE_BUGREPORT
">";
247 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\
250 tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
251 tar -tvf archive.tar # List all files in archive.tar verbosely.\n\
252 tar -xf archive.tar # Extract all files from archive.tar.\n\
253 \vThe backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
254 The version control may be set with --backup or VERSION_CONTROL, values are:\n\n\
255 none, off never make backups\n\
256 t, numbered make numbered backups\n\
257 nil, existing numbered if numbered backups exist, simple otherwise\n\
258 never, simple always make simple backups\n");
263 Available option letters are DEIJQY and aeqy. Consider the following
266 [For Solaris tar compatibility]
267 e exit immediately with a nonzero exit status if unexpected errors occur
268 E use extended headers (--format=posix)
269 [q alias for --occurrence=1 =/= this would better be used for quiet?]
270 [I same as T =/= will harm star compatibility]
272 y per-file gzip compression
273 Y per-block gzip compression */
275 static struct argp_option options
[] = {
277 N_("Main operation mode:"), 0},
280 N_("list the contents of an archive"), 10 },
281 {"extract", 'x', 0, 0,
282 N_("extract files from an archive"), 10 },
283 {"get", 0, 0, OPTION_ALIAS
, NULL
, 0 },
284 {"create", 'c', 0, 0,
285 N_("create a new archive"), 10 },
287 N_("find differences between archive and file system"), 10 },
288 {"compare", 0, 0, OPTION_ALIAS
, NULL
, 10},
289 {"append", 'r', 0, 0,
290 N_("append files to the end of an archive"), 10 },
291 {"update", 'u', 0, 0,
292 N_("only append files newer than copy in archive"), 10 },
293 {"catenate", 'A', 0, 0,
294 N_("append tar files to an archive"), 10 },
295 {"concatenate", 0, 0, OPTION_ALIAS
, NULL
, 10},
296 {"delete", DELETE_OPTION
, 0, 0,
297 N_("delete from the archive (not on mag tapes!)"), 10 },
300 N_("Operation modifiers:"), 20},
302 {"sparse", 'S', 0, 0,
303 N_("handle sparse files efficiently"), 21 },
304 {"incremental", 'G', 0, 0,
305 N_("handle old GNU-format incremental backup"), 21 },
306 {"listed-incremental", 'g', N_("FILE"), 0,
307 N_("handle new GNU-format incremental backup"), 21 },
308 {"ignore-failed-read", IGNORE_FAILED_READ_OPTION
, 0, 0,
309 N_("do not exit with nonzero on unreadable files"), 21 },
310 {"occurrence", OCCURRENCE_OPTION
, N_("NUMBER"), OPTION_ARG_OPTIONAL
,
311 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 },
312 {"seek", 'n', NULL
, 0,
313 N_("archive is seekable"), 21 },
316 N_("Overwrite control:"), 30},
318 {"verify", 'W', 0, 0,
319 N_("attempt to verify the archive after writing it"), 31 },
320 {"remove-files", REMOVE_FILES_OPTION
, 0, 0,
321 N_("remove files after adding them to the archive"), 31 },
322 {"keep-old-files", 'k', 0, 0,
323 N_("don't replace existing files when extracting"), 31 },
324 {"keep-newer-files", KEEP_NEWER_FILES_OPTION
, 0, 0,
325 N_("don't replace existing files that are newer than their archive copies"), 31 },
326 {"overwrite", OVERWRITE_OPTION
, 0, 0,
327 N_("overwrite existing files when extracting"), 31 },
328 {"unlink-first", 'U', 0, 0,
329 N_("remove each file prior to extracting over it"), 31 },
330 {"recursive-unlink", RECURSIVE_UNLINK_OPTION
, 0, 0,
331 N_("empty hierarchies prior to extracting directory"), 31 },
332 {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION
, 0, 0,
333 N_("preserve metadata of existing directories"), 31 },
336 N_("Select output stream:"), 40},
338 {"to-stdout", 'O', 0, 0,
339 N_("extract files to standard output"), 41 },
340 {"to-command", TO_COMMAND_OPTION
, N_("COMMAND"), 0,
341 N_("pipe extracted files to another program"), 41 },
342 {"ignore-command-error", IGNORE_COMMAND_ERROR_OPTION
, 0, 0,
343 N_("ignore exit codes of children"), 41 },
344 {"no-ignore-command-error", NO_IGNORE_COMMAND_ERROR_OPTION
, 0, 0,
345 N_("treat non-zero exit codes of children as error"), 41 },
348 N_("Handling of file attributes:"), 50 },
350 {"owner", OWNER_OPTION
, N_("NAME"), 0,
351 N_("force NAME as owner for added files"), 51 },
352 {"group", GROUP_OPTION
, N_("NAME"), 0,
353 N_("force NAME as group for added files"), 51 },
354 {"mode", MODE_OPTION
, N_("CHANGES"), 0,
355 N_("force (symbolic) mode CHANGES for added files"), 51 },
356 {"atime-preserve", ATIME_PRESERVE_OPTION
, 0, 0,
357 N_("don't change access times on dumped files"), 51 },
359 N_("don't extract file modified time"), 51 },
360 {"same-owner", SAME_OWNER_OPTION
, 0, 0,
361 N_("try extracting files with the same ownership"), 51 },
362 {"no-same-owner", NO_SAME_OWNER_OPTION
, 0, 0,
363 N_("extract files as yourself"), 51 },
364 {"numeric-owner", NUMERIC_OWNER_OPTION
, 0, 0,
365 N_("always use numbers for user/group names"), 51 },
366 {"preserve-permissions", 'p', 0, 0,
367 N_("extract information about file permissions (default for superuser)"),
369 {"same-permissions", 0, 0, OPTION_ALIAS
, NULL
, 51 },
370 {"no-same-permissions", NO_SAME_PERMISSIONS_OPTION
, 0, 0,
371 N_("apply the user's umask when extracting permissions from the archive (default for ordinary users)"), 51 },
372 {"preserve-order", 's', 0, 0,
373 N_("sort names to extract to match archive"), 51 },
374 {"same-order", 0, 0, OPTION_ALIAS
, NULL
, 51 },
375 {"preserve", PRESERVE_OPTION
, 0, 0,
376 N_("same as both -p and -s"), 51 },
379 N_("Device selection and switching:"), 60 },
381 {"file", 'f', N_("ARCHIVE"), 0,
382 N_("use archive file or device ARCHIVE"), 61 },
383 {"force-local", FORCE_LOCAL_OPTION
, 0, 0,
384 N_("archive file is local even if it has a colon"), 61 },
385 {"rmt-command", RMT_COMMAND_OPTION
, N_("COMMAND"), 0,
386 N_("use given rmt COMMAND instead of rmt"), 61 },
387 {"rsh-command", RSH_COMMAND_OPTION
, N_("COMMAND"), 0,
388 N_("use remote COMMAND instead of rsh"), 61 },
390 {"-[0-7][lmh]", 0, NULL
, OPTION_DOC
, /* It is OK, since `name' will never be
392 N_("specify drive and density"), 61 },
394 {NULL
, '0', NULL
, OPTION_HIDDEN
, NULL
, 61 },
395 {NULL
, '1', NULL
, OPTION_HIDDEN
, NULL
, 61 },
396 {NULL
, '2', NULL
, OPTION_HIDDEN
, NULL
, 61 },
397 {NULL
, '3', NULL
, OPTION_HIDDEN
, NULL
, 61 },
398 {NULL
, '4', NULL
, OPTION_HIDDEN
, NULL
, 61 },
399 {NULL
, '5', NULL
, OPTION_HIDDEN
, NULL
, 61 },
400 {NULL
, '6', NULL
, OPTION_HIDDEN
, NULL
, 61 },
401 {NULL
, '7', NULL
, OPTION_HIDDEN
, NULL
, 61 },
402 {NULL
, '8', NULL
, OPTION_HIDDEN
, NULL
, 61 },
403 {NULL
, '9', NULL
, OPTION_HIDDEN
, NULL
, 61 },
405 {"multi-volume", 'M', 0, 0,
406 N_("create/list/extract multi-volume archive"), 61 },
407 {"tape-length", 'L', N_("NUMBER"), 0,
408 N_("change tape after writing NUMBER x 1024 bytes"), 61 },
409 {"info-script", 'F', N_("NAME"), 0,
410 N_("run script at end of each tape (implies -M)"), 61 },
411 {"new-volume-script", 0, 0, OPTION_ALIAS
, NULL
, 61 },
412 {"volno-file", VOLNO_FILE_OPTION
, N_("FILE"), 0,
413 N_("use/update the volume number in FILE"), 61 },
416 N_("Device blocking:"), 70 },
418 {"blocking-factor", 'b', N_("BLOCKS"), 0,
419 N_("BLOCKS x 512 bytes per record"), 71 },
420 {"record-size", RECORD_SIZE_OPTION
, N_("NUMBER"), 0,
421 N_("NUMBER of bytes per record, multiple of 512"), 71 },
422 {"ignore-zeros", 'i', 0, 0,
423 N_("ignore zeroed blocks in archive (means EOF)"), 71 },
424 {"read-full-records", 'B', 0, 0,
425 N_("reblock as we read (for 4.2BSD pipes)"), 71 },
428 N_("Archive format selection:"), 80 },
430 {"format", 'H', N_("FORMAT"), 0,
431 N_("create archive of the given format."), 81 },
433 {NULL
, 0, NULL
, 0, N_("FORMAT is one of the following:"), 82 },
434 {" v7", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
, N_("old V7 tar format"), 83},
435 {" oldgnu", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
,
436 N_("GNU format as per tar <= 1.12"), 83},
437 {" gnu", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
,
438 N_("GNU tar 1.13.x format"), 83},
439 {" ustar", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
,
440 N_("POSIX 1003.1-1988 (ustar) format"), 83 },
441 {" pax", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
,
442 N_("POSIX 1003.1-2001 (pax) format"), 83 },
443 {" posix", 0, NULL
, OPTION_DOC
|OPTION_NO_TRANS
, N_("same as pax"), 83 },
445 {"old-archive", OLD_ARCHIVE_OPTION
, 0, 0, /* FIXME */
446 N_("same as --format=v7"), 88 },
447 {"portability", 0, 0, OPTION_ALIAS
, NULL
, 88 },
448 {"posix", POSIX_OPTION
, 0, 0,
449 N_("same as --format=posix"), 88 },
450 {"pax-option", PAX_OPTION
, N_("keyword[[:]=value][,keyword[[:]=value], ...]"), 0,
451 N_("control pax keywords"), 88 },
452 {"label", 'V', N_("TEXT"), 0,
453 N_("create archive with volume name TEXT. At list/extract time, use TEXT as a globbing pattern for volume name"), 88 },
455 N_("filter the archive through bzip2"), 88 },
457 N_("filter the archive through gzip"), 88 },
458 {"gunzip", 0, 0, OPTION_ALIAS
, NULL
, 88 },
459 {"ungzip", 0, 0, OPTION_ALIAS
, NULL
, 88 },
460 {"compress", 'Z', 0, 0,
461 N_("filter the archive through compress"), 88 },
462 {"uncompress", 0, 0, OPTION_ALIAS
, NULL
, 88 },
463 {"use-compress-program", USE_COMPRESS_PROGRAM_OPTION
, N_("PROG"), 0,
464 N_("filter through PROG (must accept -d)"), 88 },
467 N_("Local file selection:"), 90 },
469 {"add-file", ARGP_KEY_ARG
, N_("FILE"), 0,
470 N_("add given FILE to the archive (useful if its name starts with a dash)"), 91},
471 {"directory", 'C', N_("DIR"), 0,
472 N_("change to directory DIR"), 91 },
473 {"files-from", 'T', N_("FILE"), 0,
474 N_("get names to extract or create from FILE"), 91 },
475 {"null", NULL_OPTION
, 0, 0,
476 N_("-T reads null-terminated names, disable -C"), 91 },
477 {"unquote", UNQUOTE_OPTION
, 0, 0,
478 N_("unquote filenames read with -T (default)"), 91 },
479 {"no-unquote", NO_UNQUOTE_OPTION
, 0, 0,
480 N_("do not unquote filenames read with -T"), 91 },
481 {"exclude", EXCLUDE_OPTION
, N_("PATTERN"), 0,
482 N_("exclude files, given as a PATTERN"), 91 },
483 {"exclude-from", 'X', N_("FILE"), 0,
484 N_("exclude patterns listed in FILE"), 91 },
485 {"exclude-caches", EXCLUDE_CACHES_OPTION
, 0, 0,
486 N_("exclude directories containing a cache tag"), 91 },
487 {"ignore-case", IGNORE_CASE_OPTION
, 0, 0,
488 N_("exclusion ignores case"), 91 },
489 {"anchored", ANCHORED_OPTION
, 0, 0,
490 N_("exclude patterns match file name start"), 91 },
491 {"no-anchored", NO_ANCHORED_OPTION
, 0, 0,
492 N_("exclude patterns match after any `/' (default)"), 91 },
493 {"no-ignore-case", NO_IGNORE_CASE_OPTION
, 0, 0,
494 N_("exclusion is case sensitive (default)"), 91 },
495 {"no-wildcards", NO_WILDCARDS_OPTION
, 0, 0,
496 N_("exclude patterns are plain strings"), 91 },
497 {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION
, 0, 0,
498 N_("exclude pattern wildcards do not match `/'"), 91 },
499 {"no-recursion", NO_RECURSION_OPTION
, 0, 0,
500 N_("avoid descending automatically in directories"), 91 },
501 {"one-file-system", ONE_FILE_SYSTEM_OPTION
, 0, 0,
502 N_("stay in local file system when creating archive"), 91 },
503 {NULL
, 'l', 0, OPTION_HIDDEN
, "", 91},
504 {"recursion", RECURSION_OPTION
, 0, 0,
505 N_("recurse into directories (default)"), 91 },
506 {"absolute-names", 'P', 0, 0,
507 N_("don't strip leading `/'s from file names"), 91 },
508 {"dereference", 'h', 0, 0,
509 N_("follow symlinks; archive and dump the files they point to"), 91 },
510 {"starting-file", 'K', N_("MEMBER-NAME"), 0,
511 N_("begin at member MEMBER-NAME in the archive"), 91 },
512 {"strip-components", STRIP_COMPONENTS_OPTION
, N_("NUMBER"), 0,
513 N_("strip NUMBER leading components from file names"), 91 },
514 {"newer", 'N', N_("DATE-OR-FILE"), 0,
515 N_("only store files newer than DATE-OR-FILE"), 91 },
516 {"newer-mtime", NEWER_MTIME_OPTION
, N_("DATE"), 0,
517 N_("compare date and time when data changed only"), 91 },
518 {"after-date", 'N', N_("DATE"), 0,
519 N_("same as -N"), 91 },
520 {"backup", BACKUP_OPTION
, N_("CONTROL"), OPTION_ARG_OPTIONAL
,
521 N_("backup before removal, choose version CONTROL"), 91 },
522 {"suffix", SUFFIX_OPTION
, N_("STRING"), 0,
523 N_("backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX)"), 91 },
524 {"wildcards", WILDCARDS_OPTION
, 0, 0,
525 N_("exclude patterns use wildcards (default)"), 91 },
526 {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION
, 0, 0,
527 N_("exclude pattern wildcards match `/' (default)"), 91 },
530 N_("Informative output:"), 100 },
532 {"verbose", 'v', 0, 0,
533 N_("verbosely list files processed"), 101 },
534 {"checkpoint", CHECKPOINT_OPTION
, 0, 0,
535 N_("display progress messages every 10th record"), 101 },
536 {"check-links", CHECK_LINKS_OPTION
, 0, 0,
537 N_("print a message if not all links are dumped"), 102 },
538 {"totals", TOTALS_OPTION
, 0, 0,
539 N_("print total bytes written while creating archive"), 102 },
540 {"utc", UTC_OPTION
, 0, 0,
541 N_("print file modification dates in UTC"), 102 },
542 {"index-file", INDEX_FILE_OPTION
, N_("FILE"), 0,
543 N_("send verbose output to FILE"), 102 },
544 {"block-number", 'R', 0, 0,
545 N_("show block number within archive with each message"), 102 },
546 {"interactive", 'w', 0, 0,
547 N_("ask for confirmation for every action"), 102 },
548 {"confirmation", 0, 0, OPTION_ALIAS
, NULL
, 102 },
549 {"show-defaults", SHOW_DEFAULTS_OPTION
, 0, 0,
550 N_("Show tar defaults"), 102 },
551 {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION
, 0, 0,
552 N_("When listing or extracting, list each directory that does not match search criteria"), 102 },
555 N_("Compatibility options:"), 110 },
558 N_("when creating, same as --old-archive. When extracting, same as --no-same-owner"), 111 },
561 N_("Other options:"), 120 },
563 {"help", '?', 0, 0, N_("Give this help list"), -1},
564 {"usage", USAGE_OPTION
, 0, 0, N_("Give a short usage message"), -1},
565 {"license", LICENSE_OPTION
, 0, 0, N_("Print license and exit"), -1},
566 {"version", VERSION_OPTION
, 0, 0, N_("Print program version"), -1},
567 /* FIXME -V (--label) conflicts with the default short option for
569 {"HANG", HANG_OPTION
, "SECS", OPTION_ARG_OPTIONAL
| OPTION_HIDDEN
,
570 N_("Hang for SECS seconds (default 3600)"), 0},
575 char const *textual_date_option
;
579 char const *backup_suffix_string
;
580 char const *version_control_string
;
585 show_default_settings (FILE *stream
)
588 "--format=%s -f%s -b%d --rmt-command=%s",
589 archive_format_string (DEFAULT_ARCHIVE_FORMAT
),
590 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
,
591 DEFAULT_RMT_COMMAND
);
593 fprintf (stream
, " --rsh-command=%s", REMOTE_SHELL
);
595 fprintf (stream
, "\n");
599 set_subcommand_option (enum subcommand subcommand
)
601 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
602 && subcommand_option
!= subcommand
)
604 _("You may not specify more than one `-Acdtrux' option")));
606 subcommand_option
= subcommand
;
610 set_use_compress_program_option (const char *string
)
612 if (use_compress_program_option
613 && strcmp (use_compress_program_option
, string
) != 0)
614 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
616 use_compress_program_option
= string
;
622 printf ("tar (%s) %s\n%s\n", PACKAGE_NAME
, PACKAGE_VERSION
,
623 "Copyright (C) 2004 Free Software Foundation, Inc.\n");
624 puts (_("Based on the work of John Gilmore and Jay Fenlason. See AUTHORS\n\
625 for complete list of authors.\n"));
626 printf (_(" GNU tar is free software; you can redistribute it and/or modify\n"
627 " it under the terms of the GNU General Public License as published by\n"
628 " the Free Software Foundation; either version 2 of the License, or\n"
629 " (at your option) any later version.\n"
631 " GNU tar is distributed in the hope that it will be useful,\n"
632 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
633 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
634 " GNU General Public License for more details.\n"
636 " You should have received a copy of the GNU General Public License\n"
637 " along with GNU tar; if not, write to the Free Software Foundation,\n"
638 " Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\n\n"));
642 static volatile int _argp_hang
;
644 enum read_file_list_state
/* Result of reading file name from the list file */
646 file_list_success
, /* OK, name read successfully */
647 file_list_end
, /* End of list file */
648 file_list_zero
/* Zero separator encountered where it should not */
651 /* Read from FP a sequence of characters up to FILENAME_TERMINATOR and put them
654 static enum read_file_list_state
655 read_name_from_file (FILE *fp
, struct obstack
*stk
)
660 for (c
= getc (fp
); c
!= EOF
&& c
!= filename_terminator
; c
= getc (fp
))
664 /* We have read a zero separator. The file possibly is zero-separated */
665 /* FATAL_ERROR((0, 0, N_("file name contains null character"))); */
666 return file_list_zero
;
668 obstack_1grow (stk
, c
);
672 obstack_1grow (stk
, 0);
674 return (counter
== 0 && c
== EOF
) ? file_list_end
: file_list_success
;
678 static bool files_from_option
; /* When set, tar will not refuse to create
680 static struct obstack argv_stk
; /* Storage for additional command line options
681 read using -T option */
683 /* Prevent recursive inclusion of the same file */
686 struct file_id_list
*next
;
691 static struct file_id_list
*file_id_list
;
694 add_file_id (const char *filename
)
696 struct file_id_list
*p
;
699 if (stat (filename
, &st
))
700 stat_fatal (filename
);
701 for (p
= file_id_list
; p
; p
= p
->next
)
702 if (p
->ino
== st
.st_ino
&& p
->dev
== st
.st_dev
)
704 FATAL_ERROR ((0, 0, _("%s: file list already read"),
705 quotearg_colon (filename
)));
707 p
= xmalloc (sizeof *p
);
708 p
->next
= file_id_list
;
715 update_argv (const char *filename
, struct argp_state
*state
)
722 bool is_stdin
= false;
723 enum read_file_list_state read_state
;
725 if (!strcmp (filename
, "-"))
728 request_stdin ("-T");
733 add_file_id (filename
);
734 if ((fp
= fopen (filename
, "r")) == NULL
)
735 open_fatal (filename
);
738 while ((read_state
= read_name_from_file (fp
, &argv_stk
)) == file_list_success
)
741 if (read_state
== file_list_zero
)
745 WARN ((0, 0, N_("%s: file name read contains nul character"),
746 quotearg_colon (filename
)));
748 /* Prepare new stack contents */
749 size
= obstack_object_size (&argv_stk
);
750 p
= obstack_finish (&argv_stk
);
751 for (; size
> 0; size
--, p
++)
753 obstack_1grow (&argv_stk
, *p
);
755 obstack_1grow (&argv_stk
, '\n');
756 obstack_1grow (&argv_stk
, 0);
759 /* Read rest of files using new filename terminator */
760 filename_terminator
= 0;
761 while (read_name_from_file (fp
, &argv_stk
) == file_list_success
)
771 start
= obstack_finish (&argv_stk
);
773 if (filename_terminator
== 0)
774 for (p
= start
; *p
; p
+= strlen (p
) + 1)
778 new_argc
= state
->argc
+ count
;
779 new_argv
= xmalloc (sizeof (state
->argv
[0]) * (new_argc
+ 1));
780 memcpy (new_argv
, state
->argv
, sizeof (state
->argv
[0]) * (state
->argc
+ 1));
781 state
->argv
= new_argv
;
782 memmove (&state
->argv
[state
->next
+ count
], &state
->argv
[state
->next
],
783 (state
->argc
- state
->next
+ 1) * sizeof (state
->argv
[0]));
785 state
->argc
= new_argc
;
787 for (i
= state
->next
, p
= start
; *p
; p
+= strlen (p
) + 1, i
++)
789 if (filename_terminator
== 0 && p
[0] == '-')
790 state
->argv
[i
++] = "--add-file";
797 parse_opt (int key
, char *arg
, struct argp_state
*state
)
799 struct tar_args
*args
= state
->input
;
804 /* File name or non-parsed option, because of ARGP_IN_ORDER */
810 set_subcommand_option (CAT_SUBCOMMAND
);
816 if (! (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
817 && u
== (blocking_factor
= u
)
818 && 0 < blocking_factor
819 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
820 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
821 _("Invalid blocking factor")));
826 /* Try to reblock input records. For reading 4.2BSD pipes. */
828 /* It would surely make sense to exchange -B and -R, but it seems
829 that -B has been used for a long while in Sun tar and most
830 BSD-derived systems. This is a consequence of the block/record
831 terminology confusion. */
833 read_full_records_option
= true;
837 set_subcommand_option (CREATE_SUBCOMMAND
);
846 set_subcommand_option (DIFF_SUBCOMMAND
);
850 if (archive_names
== allocated_archive_names
)
852 allocated_archive_names
*= 2;
854 xrealloc (archive_name_array
,
855 sizeof (const char *) * allocated_archive_names
);
857 archive_name_array
[archive_names
++] = arg
;
861 /* Since -F is only useful with -M, make it implied. Run this
862 script at the end of each tape. */
864 info_script_option
= arg
;
865 multi_volume_option
= true;
869 listed_incremental_option
= arg
;
870 after_date_option
= true;
874 /* We are making an incremental dump (FIXME: are we?); save
875 directories at the beginning of the archive, and include in each
876 directory its contents. */
878 incremental_option
= true;
882 /* Follow symbolic links. */
883 dereference_option
= true;
887 /* Ignore zero blocks (eofs). This can't be the default,
888 because Unix tar writes two blocks of zeros, then pads out
889 the record with garbage. */
891 ignore_zeros_option
= true;
896 _("Warning: the -I option is not supported;"
897 " perhaps you meant -j or -T?")));
901 set_use_compress_program_option ("bzip2");
905 /* Don't replace existing files. */
906 old_files_option
= KEEP_OLD_FILES
;
910 starting_file_option
= true;
915 /* Historically equivalent to --one-file-system. This usage is
916 incompatible with UNIX98 and POSIX specs and therefore is
917 deprecated. The semantics of -l option will be changed in
918 future versions. See TODO.
921 _("Semantics of -l option will change in the future releases.")));
923 _("Please use --one-file-system option instead.")));
925 case ONE_FILE_SYSTEM_OPTION
:
926 /* When dumping directories, don't dump files/subdirectories
927 that are on other filesystems. */
928 one_file_system_option
= true;
934 if (xstrtoumax (arg
, 0, 10, &u
, "") != LONGINT_OK
)
935 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
936 _("Invalid tape length")));
937 tape_length_option
= 1024 * (tarlong
) u
;
938 multi_volume_option
= true;
947 /* Make multivolume archive: when we can't write any more into
948 the archive, re-open it, and continue writing. */
950 multi_volume_option
= true;
954 seekable_archive
= true;
959 after_date_option
= true;
962 case NEWER_MTIME_OPTION
:
963 if (NEWER_OPTION_INITIALIZED (newer_mtime_option
))
964 USAGE_ERROR ((0, 0, _("More than one threshold date")));
966 if (FILE_SYSTEM_PREFIX_LEN (arg
) != 0
971 if (deref_stat (dereference_option
, arg
, &st
) != 0)
974 USAGE_ERROR ((0, 0, _("Date sample file not found")));
976 newer_mtime_option
= get_stat_mtime (&st
);
980 if (! get_date (&newer_mtime_option
, arg
, NULL
))
982 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
983 tartime (newer_mtime_option
, false), quote (arg
)));
984 newer_mtime_option
.tv_nsec
= 0;
987 args
->textual_date_option
= arg
;
991 #endif /* not MSDOS */
994 args
->o_option
= true;
998 to_stdout_option
= true;
1002 same_permissions_option
= true;
1006 absolute_names_option
= true;
1010 set_subcommand_option (APPEND_SUBCOMMAND
);
1014 /* Print block numbers for debugging bad tar archives. */
1016 /* It would surely make sense to exchange -B and -R, but it seems
1017 that -B has been used for a long while in Sun tar ans most
1018 BSD-derived systems. This is a consequence of the block/record
1019 terminology confusion. */
1021 block_number_option
= true;
1025 /* Names to extr are sorted. */
1027 same_order_option
= true;
1031 sparse_option
= true;
1035 set_subcommand_option (LIST_SUBCOMMAND
);
1040 update_argv (arg
, state
);
1041 /* Indicate we've been given -T option. This is for backward
1042 compatibility only, so that `tar cfT archive /dev/null will
1044 files_from_option
= true;
1048 set_subcommand_option (UPDATE_SUBCOMMAND
);
1052 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1064 volume_label_option
= arg
;
1068 interactive_option
= true;
1072 verify_option
= true;
1076 set_subcommand_option (EXTRACT_SUBCOMMAND
);
1080 if (add_exclude_file (add_exclude
, excluded
, arg
,
1081 args
->exclude_options
| recursion_option
, '\n')
1085 FATAL_ERROR ((0, e
, "%s", quotearg_colon (arg
)));
1091 _("Warning: the -y option is not supported;"
1092 " perhaps you meant -j?")));
1096 set_use_compress_program_option ("gzip");
1100 set_use_compress_program_option ("compress");
1103 case ANCHORED_OPTION
:
1104 args
->exclude_options
|= EXCLUDE_ANCHORED
;
1107 case ATIME_PRESERVE_OPTION
:
1108 atime_preserve_option
= true;
1111 case CHECKPOINT_OPTION
:
1112 checkpoint_option
= true;
1116 backup_option
= true;
1118 args
->version_control_string
= arg
;
1122 set_subcommand_option (DELETE_SUBCOMMAND
);
1125 case EXCLUDE_OPTION
:
1126 add_exclude (excluded
, arg
, args
->exclude_options
| recursion_option
);
1129 case EXCLUDE_CACHES_OPTION
:
1130 exclude_caches_option
= true;
1133 case FORCE_LOCAL_OPTION
:
1134 force_local_option
= true;
1138 set_archive_format (arg
);
1141 case INDEX_FILE_OPTION
:
1142 index_file_name
= arg
;
1145 case IGNORE_CASE_OPTION
:
1146 args
->exclude_options
|= FNM_CASEFOLD
;
1149 case IGNORE_COMMAND_ERROR_OPTION
:
1150 ignore_command_error_option
= true;
1153 case IGNORE_FAILED_READ_OPTION
:
1154 ignore_failed_read_option
= true;
1157 case KEEP_NEWER_FILES_OPTION
:
1158 old_files_option
= KEEP_NEWER_FILES
;
1162 if (! (strlen (arg
) < GNAME_FIELD_SIZE
1163 && gname_to_gid (arg
, &group_option
)))
1166 if (xstrtoumax (arg
, 0, 10, &g
, "") == LONGINT_OK
1170 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1171 _("%s: Invalid group")));
1176 mode_option
= mode_compile (arg
);
1178 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
1179 initial_umask
= umask (0);
1180 umask (initial_umask
);
1183 case NO_ANCHORED_OPTION
:
1184 args
->exclude_options
&= ~ EXCLUDE_ANCHORED
;
1187 case NO_IGNORE_CASE_OPTION
:
1188 args
->exclude_options
&= ~ FNM_CASEFOLD
;
1191 case NO_IGNORE_COMMAND_ERROR_OPTION
:
1192 ignore_command_error_option
= false;
1195 case NO_OVERWRITE_DIR_OPTION
:
1196 old_files_option
= NO_OVERWRITE_DIR_OLD_FILES
;
1199 case NO_WILDCARDS_OPTION
:
1200 args
->exclude_options
&= ~ EXCLUDE_WILDCARDS
;
1203 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
1204 args
->exclude_options
|= FNM_FILE_NAME
;
1208 filename_terminator
= '\0';
1211 case NUMERIC_OWNER_OPTION
:
1212 numeric_owner_option
= true;
1215 case OCCURRENCE_OPTION
:
1217 occurrence_option
= 1;
1221 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
1222 occurrence_option
= u
;
1224 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1225 _("Invalid number")));
1229 case OVERWRITE_OPTION
:
1230 old_files_option
= OVERWRITE_OLD_FILES
;
1234 if (! (strlen (arg
) < UNAME_FIELD_SIZE
1235 && uname_to_uid (arg
, &owner_option
)))
1238 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
1242 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1243 _("Invalid owner")));
1249 xheader_set_option (arg
);
1253 set_archive_format ("posix");
1256 case PRESERVE_OPTION
:
1257 same_permissions_option
= true;
1258 same_order_option
= true;
1261 case RECORD_SIZE_OPTION
:
1264 if (! (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
1265 && u
== (size_t) u
))
1266 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1267 _("Invalid record size")));
1269 if (record_size
% BLOCKSIZE
!= 0)
1270 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1272 blocking_factor
= record_size
/ BLOCKSIZE
;
1276 case RECURSIVE_UNLINK_OPTION
:
1277 recursive_unlink_option
= true;
1280 case REMOVE_FILES_OPTION
:
1281 remove_files_option
= true;
1284 case RMT_COMMAND_OPTION
:
1288 case RSH_COMMAND_OPTION
:
1289 rsh_command_option
= arg
;
1292 case SHOW_DEFAULTS_OPTION
:
1293 show_default_settings (stdout
);
1296 case STRIP_COMPONENTS_OPTION
:
1299 if (! (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
1300 && u
== (size_t) u
))
1301 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg
),
1302 _("Invalid number of elements")));
1303 strip_name_components
= u
;
1307 case SHOW_OMITTED_DIRS_OPTION
:
1308 show_omitted_dirs_option
= true;
1312 backup_option
= true;
1313 args
->backup_suffix_string
= arg
;
1316 case TO_COMMAND_OPTION
:
1317 if (to_command_option
)
1318 USAGE_ERROR ((0, 0, _("Only one --to-command option allowed")));
1319 to_command_option
= arg
;
1323 totals_option
= true;
1326 case USE_COMPRESS_PROGRAM_OPTION
:
1327 set_use_compress_program_option (arg
);
1330 case VOLNO_FILE_OPTION
:
1331 volno_file_option
= arg
;
1334 case WILDCARDS_OPTION
:
1335 args
->exclude_options
|= EXCLUDE_WILDCARDS
;
1338 case WILDCARDS_MATCH_SLASH_OPTION
:
1339 args
->exclude_options
&= ~ FNM_FILE_NAME
;
1342 case CHECK_LINKS_OPTION
:
1343 check_links_option
= 1;
1346 case NO_RECURSION_OPTION
:
1347 recursion_option
= 0;
1350 case NO_SAME_OWNER_OPTION
:
1351 same_owner_option
= -1;
1354 case NO_SAME_PERMISSIONS_OPTION
:
1355 same_permissions_option
= -1;
1358 case RECURSION_OPTION
:
1359 recursion_option
= FNM_LEADING_DIR
;
1362 case SAME_OWNER_OPTION
:
1363 same_owner_option
= 1;
1366 case UNQUOTE_OPTION
:
1367 unquote_option
= true;
1370 case NO_UNQUOTE_OPTION
:
1371 unquote_option
= false;
1383 #ifdef DEVICE_PREFIX
1385 int device
= key
- '0';
1387 static char buf
[sizeof DEVICE_PREFIX
+ 10];
1391 argp_error (state
, _("Malformed density argument: %s"), quote (arg
));
1393 strcpy (buf
, DEVICE_PREFIX
);
1394 cursor
= buf
+ strlen (buf
);
1396 #ifdef DENSITY_LETTER
1398 sprintf (cursor
, "%d%c", device
, arg
[0]);
1400 #else /* not DENSITY_LETTER */
1427 argp_error (state
, _("Unknown density: `%c'"), arg
[0]);
1429 sprintf (cursor
, "%d", device
);
1431 #endif /* not DENSITY_LETTER */
1433 if (archive_names
== allocated_archive_names
)
1435 allocated_archive_names
*= 2;
1436 archive_name_array
=
1437 xrealloc (archive_name_array
,
1438 sizeof (const char *) * allocated_archive_names
);
1440 archive_name_array
[archive_names
++] = xstrdup (buf
);
1444 #else /* not DEVICE_PREFIX */
1447 _("Options `-[0-7][lmh]' not supported by *this* tar"));
1449 #endif /* not DEVICE_PREFIX */
1452 state
->flags
|= ARGP_NO_EXIT
;
1453 argp_state_help (state
, state
->out_stream
,
1454 ARGP_HELP_STD_HELP
& ~ARGP_HELP_BUG_ADDR
);
1455 fprintf (state
->out_stream
, _("\n*This* tar defaults to:\n"));
1456 show_default_settings (state
->out_stream
);
1457 fprintf (state
->out_stream
, "\n");
1458 fprintf (state
->out_stream
, _("Report bugs to %s.\n"),
1459 argp_program_bug_address
);
1463 argp_state_help (state
, state
->out_stream
,
1464 ARGP_HELP_USAGE
| ARGP_HELP_EXIT_OK
);
1467 case VERSION_OPTION
:
1468 fprintf (state
->out_stream
, "%s\n", argp_program_version
);
1471 case LICENSE_OPTION
:
1476 _argp_hang
= atoi (arg
? arg
: "3600");
1477 while (_argp_hang
-- > 0)
1482 return ARGP_ERR_UNKNOWN
;
1487 static struct argp argp
= {
1500 argp_help (&argp
, stderr
, ARGP_HELP_SEE
, (char*) program_name
);
1504 /* Parse the options for tar. */
1506 static struct argp_option
*
1507 find_argp_option (struct argp_option
*options
, int letter
)
1510 !(options
->name
== NULL
1511 && options
->key
== 0
1512 && options
->arg
== 0
1513 && options
->flags
== 0
1514 && options
->doc
== NULL
); options
++)
1515 if (options
->key
== letter
)
1521 decode_options (int argc
, char **argv
)
1524 struct tar_args args
;
1526 /* Set some default option values. */
1527 args
.textual_date_option
= NULL
;
1528 args
.exclude_options
= EXCLUDE_WILDCARDS
;
1530 args
.pax_option
= 0;
1531 args
.backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
1532 args
.version_control_string
= 0;
1533 args
.input_files
= 0;
1535 subcommand_option
= UNKNOWN_SUBCOMMAND
;
1536 archive_format
= DEFAULT_FORMAT
;
1537 blocking_factor
= DEFAULT_BLOCKING
;
1538 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
1539 excluded
= new_exclude ();
1540 newer_mtime_option
.tv_sec
= TYPE_MINIMUM (time_t);
1541 newer_mtime_option
.tv_nsec
= -1;
1542 recursion_option
= FNM_LEADING_DIR
;
1543 unquote_option
= true;
1548 /* Convert old-style tar call by exploding option element and rearranging
1549 options accordingly. */
1551 if (argc
> 1 && argv
[1][0] != '-')
1553 int new_argc
; /* argc value for rearranged arguments */
1554 char **new_argv
; /* argv value for rearranged arguments */
1555 char *const *in
; /* cursor into original argv */
1556 char **out
; /* cursor into rearranged argv */
1557 const char *letter
; /* cursor into old option letters */
1558 char buffer
[3]; /* constructed option buffer */
1560 /* Initialize a constructed option. */
1565 /* Allocate a new argument array, and copy program name in it. */
1567 new_argc
= argc
- 1 + strlen (argv
[1]);
1568 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (char *));
1573 /* Copy each old letter option as a separate option, and have the
1574 corresponding argument moved next to it. */
1576 for (letter
= *in
++; *letter
; letter
++)
1578 struct argp_option
*opt
;
1580 buffer
[1] = *letter
;
1581 *out
++ = xstrdup (buffer
);
1582 opt
= find_argp_option (options
, *letter
);
1583 if (opt
&& opt
->arg
)
1585 if (in
< argv
+ argc
)
1588 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
1593 /* Copy all remaining options. */
1595 while (in
< argv
+ argc
)
1599 /* Replace the old option list by the new one. */
1605 /* Parse all options and non-options as they appear. */
1607 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
1609 if (argp_parse (&argp
, argc
, argv
, ARGP_IN_ORDER
|ARGP_NO_HELP
,
1614 /* Special handling for 'o' option:
1616 GNU tar used to say "output old format".
1617 UNIX98 tar says don't chown files after extracting (we use
1618 "--no-same-owner" for this).
1620 The old GNU tar semantics is retained when used with --create
1621 option, otherwise UNIX98 semantics is assumed */
1625 if (subcommand_option
== CREATE_SUBCOMMAND
)
1627 /* GNU Tar <= 1.13 compatibility */
1628 set_archive_format ("v7");
1632 /* UNIX98 compatibility */
1633 same_owner_option
= -1;
1637 /* Handle operands after any "--" argument. */
1638 for (; index
< argc
; index
++)
1640 name_add (argv
[index
]);
1644 /* Derive option values and check option consistency. */
1646 if (archive_format
== DEFAULT_FORMAT
)
1648 if (args
.pax_option
)
1649 archive_format
= POSIX_FORMAT
;
1651 archive_format
= DEFAULT_ARCHIVE_FORMAT
;
1654 if (volume_label_option
&& subcommand_option
== CREATE_SUBCOMMAND
)
1655 assert_format (FORMAT_MASK (OLDGNU_FORMAT
)
1656 | FORMAT_MASK (GNU_FORMAT
));
1659 if (incremental_option
|| multi_volume_option
)
1660 assert_format (FORMAT_MASK (OLDGNU_FORMAT
) | FORMAT_MASK (GNU_FORMAT
));
1663 assert_format (FORMAT_MASK (OLDGNU_FORMAT
)
1664 | FORMAT_MASK (GNU_FORMAT
)
1665 | FORMAT_MASK (POSIX_FORMAT
));
1667 if (occurrence_option
)
1669 if (!args
.input_files
)
1671 _("--occurrence is meaningless without a file list")));
1672 if (subcommand_option
!= DELETE_SUBCOMMAND
1673 && subcommand_option
!= DIFF_SUBCOMMAND
1674 && subcommand_option
!= EXTRACT_SUBCOMMAND
1675 && subcommand_option
!= LIST_SUBCOMMAND
)
1677 _("--occurrence cannot be used in the requested operation mode")));
1680 if (seekable_archive
&& subcommand_option
== DELETE_SUBCOMMAND
)
1682 /* The current code in delete.c is based on the assumption that
1683 skip_member() reads all data from the archive. So, we should
1684 make sure it won't use seeks. On the other hand, the same code
1685 depends on the ability to backspace a record in the archive,
1686 so setting seekable_archive to false is technically incorrect.
1687 However, it is tested only in skip_member(), so it's not a
1689 seekable_archive
= false;
1692 if (archive_names
== 0)
1694 /* If no archive file name given, try TAPE from the environment, or
1695 else, DEFAULT_ARCHIVE from the configuration process. */
1698 archive_name_array
[0] = getenv ("TAPE");
1699 if (! archive_name_array
[0])
1700 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1703 /* Allow multiple archives only with `-M'. */
1705 if (archive_names
> 1 && !multi_volume_option
)
1707 _("Multiple archive files require `-M' option")));
1709 if (listed_incremental_option
1710 && NEWER_OPTION_INITIALIZED (newer_mtime_option
))
1712 _("Cannot combine --listed-incremental with --newer")));
1714 if (volume_label_option
)
1716 size_t volume_label_max_len
=
1717 (sizeof current_header
->header
.name
1718 - 1 /* for trailing '\0' */
1719 - (multi_volume_option
1720 ? (sizeof " Volume "
1721 - 1 /* for null at end of " Volume " */
1722 + INT_STRLEN_BOUND (int) /* for volume number */
1723 - 1 /* for sign, as 0 <= volno */)
1725 if (volume_label_max_len
< strlen (volume_label_option
))
1727 ngettext ("%s: Volume label is too long (limit is %lu byte)",
1728 "%s: Volume label is too long (limit is %lu bytes)",
1729 volume_label_max_len
),
1730 quotearg_colon (volume_label_option
),
1731 (unsigned long) volume_label_max_len
));
1736 if (multi_volume_option
)
1737 USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
1738 if (use_compress_program_option
)
1739 USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
1742 if (use_compress_program_option
)
1744 if (multi_volume_option
)
1745 USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
1746 if (subcommand_option
== UPDATE_SUBCOMMAND
)
1747 USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
1750 /* It is no harm to use --pax-option on non-pax archives in archive
1751 reading mode. It may even be useful, since it allows to override
1752 file attributes from tar headers. Therefore I allow such usage.
1755 && archive_format
!= POSIX_FORMAT
1756 && (subcommand_option
!= EXTRACT_SUBCOMMAND
1757 || subcommand_option
!= DIFF_SUBCOMMAND
1758 || subcommand_option
!= LIST_SUBCOMMAND
))
1759 USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
1761 /* If ready to unlink hierarchies, so we are for simpler files. */
1762 if (recursive_unlink_option
)
1763 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1768 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1769 explicit or implied, is used correctly. */
1771 switch (subcommand_option
)
1773 case CREATE_SUBCOMMAND
:
1774 if (args
.input_files
== 0 && !files_from_option
)
1776 _("Cowardly refusing to create an empty archive")));
1779 case EXTRACT_SUBCOMMAND
:
1780 case LIST_SUBCOMMAND
:
1781 case DIFF_SUBCOMMAND
:
1782 for (archive_name_cursor
= archive_name_array
;
1783 archive_name_cursor
< archive_name_array
+ archive_names
;
1784 archive_name_cursor
++)
1785 if (!strcmp (*archive_name_cursor
, "-"))
1786 request_stdin ("-f");
1789 case CAT_SUBCOMMAND
:
1790 case UPDATE_SUBCOMMAND
:
1791 case APPEND_SUBCOMMAND
:
1792 for (archive_name_cursor
= archive_name_array
;
1793 archive_name_cursor
< archive_name_array
+ archive_names
;
1794 archive_name_cursor
++)
1795 if (!strcmp (*archive_name_cursor
, "-"))
1797 _("Options `-Aru' are incompatible with `-f -'")));
1803 archive_name_cursor
= archive_name_array
;
1805 /* Prepare for generating backup names. */
1807 if (args
.backup_suffix_string
)
1808 simple_backup_suffix
= xstrdup (args
.backup_suffix_string
);
1812 backup_type
= xget_version ("--backup", args
.version_control_string
);
1813 /* No backup is needed either if explicitely disabled or if
1814 the extracted files are not being written to disk. */
1815 if (backup_type
== no_backups
|| EXTRACT_OVER_PIPE
)
1816 backup_option
= false;
1819 if (verbose_option
&& args
.textual_date_option
)
1821 char const *treated_as
= tartime (newer_mtime_option
, true);
1822 if (strcmp (args
.textual_date_option
, treated_as
) != 0)
1823 WARN ((0, 0, _("Treating date `%s' as %s"),
1824 args
.textual_date_option
, treated_as
));
1831 /* Main routine for tar. */
1833 main (int argc
, char **argv
)
1836 program_name
= argv
[0];
1838 setlocale (LC_ALL
, "");
1839 bindtextdomain (PACKAGE
, LOCALEDIR
);
1840 textdomain (PACKAGE
);
1842 exit_status
= TAREXIT_SUCCESS
;
1843 filename_terminator
= '\n';
1844 set_quoting_style (0, escape_quoting_style
);
1846 /* Pre-allocate a few structures. */
1848 allocated_archive_names
= 10;
1849 archive_name_array
=
1850 xmalloc (sizeof (const char *) * allocated_archive_names
);
1853 obstack_init (&argv_stk
);
1856 /* System V fork+wait does not work if SIGCHLD is ignored. */
1857 signal (SIGCHLD
, SIG_DFL
);
1862 /* Decode options. */
1864 decode_options (argc
, argv
);
1867 /* Main command execution. */
1869 if (volno_file_option
)
1870 init_volume_number ();
1872 switch (subcommand_option
)
1874 case UNKNOWN_SUBCOMMAND
:
1876 _("You must specify one of the `-Acdtrux' options")));
1878 case CAT_SUBCOMMAND
:
1879 case UPDATE_SUBCOMMAND
:
1880 case APPEND_SUBCOMMAND
:
1884 case DELETE_SUBCOMMAND
:
1885 delete_archive_members ();
1888 case CREATE_SUBCOMMAND
:
1891 print_total_written ();
1894 case EXTRACT_SUBCOMMAND
:
1896 read_and (extract_archive
);
1898 /* FIXME: should extract_finish () even if an ordinary signal is
1904 case LIST_SUBCOMMAND
:
1905 read_and (list_archive
);
1908 case DIFF_SUBCOMMAND
:
1910 read_and (diff_archive
);
1914 if (check_links_option
)
1917 if (volno_file_option
)
1918 closeout_volume_number ();
1920 /* Dispose of allocated memory, and return. */
1922 free (archive_name_array
);
1925 if (stdlis
!= stderr
&& (ferror (stdlis
) || fclose (stdlis
) != 0))
1926 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1927 if (exit_status
== TAREXIT_FAILURE
)
1928 error (0, 0, _("Error exit delayed from previous errors"));
1929 if (ferror (stderr
) || fclose (stderr
) != 0)
1930 exit_status
= TAREXIT_FAILURE
;
1935 tar_stat_init (struct tar_stat_info
*st
)
1937 memset (st
, 0, sizeof (*st
));
1941 tar_stat_destroy (struct tar_stat_info
*st
)
1943 free (st
->orig_file_name
);
1944 free (st
->file_name
);
1945 free (st
->link_name
);
1948 free (st
->sparse_map
);
1949 memset (st
, 0, sizeof (*st
));