]> Dogcows Code - chaz/tar/blob - src/tar.c
Handle new options --to-command,--ignore-command-error
[chaz/tar] / src / tar.c
1 /* A tar (tape archiver) program.
2
3 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
4 2001, 2003, 2004 Free Software Foundation, Inc.
5
6 Written by John Gilmore, starting 1985-08-25.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include <system.h>
23
24 #include <fnmatch.h>
25 #include <argp.h>
26
27 #include <signal.h>
28 #if ! defined SIGCHLD && defined SIGCLD
29 # define SIGCHLD SIGCLD
30 #endif
31
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. */
36 #define GLOBAL
37 #include "common.h"
38
39 #include <getdate.h>
40 #include <localedir.h>
41 #include <rmt.h>
42 #include <prepargs.h>
43 #include <quotearg.h>
44 #include <xstrtol.h>
45
46 /* Local declarations. */
47
48 #ifndef DEFAULT_ARCHIVE_FORMAT
49 # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
50 #endif
51
52 #ifndef DEFAULT_ARCHIVE
53 # define DEFAULT_ARCHIVE "tar.out"
54 #endif
55
56 #ifndef DEFAULT_BLOCKING
57 # define DEFAULT_BLOCKING 20
58 #endif
59
60 \f
61 /* Miscellaneous. */
62
63 /* Name of option using stdin. */
64 static const char *stdin_used_by;
65
66 /* Doesn't return if stdin already requested. */
67 void
68 request_stdin (const char *option)
69 {
70 if (stdin_used_by)
71 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
72 stdin_used_by, option));
73
74 stdin_used_by = option;
75 }
76
77 /* Returns true if and only if the user typed 'y' or 'Y'. */
78 int
79 confirm (const char *message_action, const char *message_name)
80 {
81 static FILE *confirm_file;
82 static int confirm_file_EOF;
83
84 if (!confirm_file)
85 {
86 if (archive == 0 || stdin_used_by)
87 {
88 confirm_file = fopen (TTY_NAME, "r");
89 if (! confirm_file)
90 open_fatal (TTY_NAME);
91 }
92 else
93 {
94 request_stdin ("-w");
95 confirm_file = stdin;
96 }
97 }
98
99 fprintf (stdlis, "%s %s?", message_action, quote (message_name));
100 fflush (stdlis);
101
102 {
103 int reply = confirm_file_EOF ? EOF : getc (confirm_file);
104 int character;
105
106 for (character = reply;
107 character != '\n';
108 character = getc (confirm_file))
109 if (character == EOF)
110 {
111 confirm_file_EOF = 1;
112 fputc ('\n', stdlis);
113 fflush (stdlis);
114 break;
115 }
116 return reply == 'y' || reply == 'Y';
117 }
118 }
119
120 static struct fmttab {
121 char const *name;
122 enum archive_format fmt;
123 } const fmttab[] = {
124 { "v7", V7_FORMAT },
125 { "oldgnu", OLDGNU_FORMAT },
126 { "ustar", USTAR_FORMAT },
127 { "posix", POSIX_FORMAT },
128 #if 0 /* not fully supported yet */
129 { "star", STAR_FORMAT },
130 #endif
131 { "gnu", GNU_FORMAT },
132 { "pax", POSIX_FORMAT }, /* An alias for posix */
133 { NULL, 0 }
134 };
135
136 static void
137 set_archive_format (char const *name)
138 {
139 struct fmttab const *p;
140
141 for (p = fmttab; strcmp (p->name, name) != 0; )
142 if (! (++p)->name)
143 USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
144 quotearg_colon (name)));
145
146 archive_format = p->fmt;
147 }
148
149 static const char *
150 archive_format_string (enum archive_format fmt)
151 {
152 struct fmttab const *p;
153
154 for (p = fmttab; p->name; p++)
155 if (p->fmt == fmt)
156 return p->name;
157 return "unknown?";
158 }
159
160 #define FORMAT_MASK(n) (1<<(n))
161
162 static void
163 assert_format(unsigned fmt_mask)
164 {
165 if ((FORMAT_MASK(archive_format) & fmt_mask) == 0)
166 USAGE_ERROR ((0, 0,
167 _("GNU features wanted on incompatible archive format")));
168 }
169
170
171 \f
172 /* Options. */
173
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. */
180
181 enum
182 {
183 ANCHORED_OPTION = CHAR_MAX + 1,
184 ATIME_PRESERVE_OPTION,
185 BACKUP_OPTION,
186 CHECKPOINT_OPTION,
187 CHECK_LINKS_OPTION,
188 DELETE_OPTION,
189 EXCLUDE_OPTION,
190 EXCLUDE_CACHES_OPTION,
191 FORCE_LOCAL_OPTION,
192 GROUP_OPTION,
193 HANG_OPTION,
194 IGNORE_CASE_OPTION,
195 IGNORE_COMMAND_ERROR_OPTION,
196 IGNORE_FAILED_READ_OPTION,
197 INDEX_FILE_OPTION,
198 KEEP_NEWER_FILES_OPTION,
199 LICENSE_OPTION,
200 MODE_OPTION,
201 NEWER_MTIME_OPTION,
202 NO_ANCHORED_OPTION,
203 NO_IGNORE_CASE_OPTION,
204 NO_IGNORE_COMMAND_ERROR_OPTION,
205 NO_OVERWRITE_DIR_OPTION,
206 NO_RECURSION_OPTION,
207 NO_SAME_OWNER_OPTION,
208 NO_SAME_PERMISSIONS_OPTION,
209 NO_UNQUOTE_OPTION,
210 NO_WILDCARDS_OPTION,
211 NO_WILDCARDS_MATCH_SLASH_OPTION,
212 NULL_OPTION,
213 NUMERIC_OWNER_OPTION,
214 OCCURRENCE_OPTION,
215 OLD_ARCHIVE_OPTION,
216 ONE_FILE_SYSTEM_OPTION,
217 OVERWRITE_OPTION,
218 OWNER_OPTION,
219 PAX_OPTION,
220 POSIX_OPTION,
221 PRESERVE_OPTION,
222 RECORD_SIZE_OPTION,
223 RECURSION_OPTION,
224 RECURSIVE_UNLINK_OPTION,
225 REMOVE_FILES_OPTION,
226 RMT_COMMAND_OPTION,
227 RSH_COMMAND_OPTION,
228 SAME_OWNER_OPTION,
229 SHOW_DEFAULTS_OPTION,
230 SHOW_OMITTED_DIRS_OPTION,
231 STRIP_COMPONENTS_OPTION,
232 SUFFIX_OPTION,
233 TO_COMMAND_OPTION,
234 TOTALS_OPTION,
235 UNQUOTE_OPTION,
236 USAGE_OPTION,
237 USE_COMPRESS_PROGRAM_OPTION,
238 UTC_OPTION,
239 VERSION_OPTION,
240 VOLNO_FILE_OPTION,
241 WILDCARDS_OPTION,
242 WILDCARDS_MATCH_SLASH_OPTION
243 };
244
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\
248 \n\
249 Examples:\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 t, numbered make numbered backups\n\
256 nil, existing numbered if numbered backups exist, simple otherwise\n\
257 never, simple always make simple backups\n");
258
259
260 /* NOTE:
261
262 Available option letters are DEIJQY and aeqy. Consider the following
263 assignments:
264
265 [For Solaris tar compatibility]
266 e exit immediately with a nonzero exit status if unexpected errors occur
267 E use extended headers (--format=posix)
268 [q alias for --occurrence=1 =/= this would better be used for quiet?]
269 [I same as T =/= will harm star compatibility]
270
271 y per-file gzip compression
272 Y per-block gzip compression */
273
274 static struct argp_option options[] = {
275 {NULL, 0, NULL, 0,
276 N_("Main operation mode:"), 0},
277
278 {"list", 't', 0, 0,
279 N_("list the contents of an archive"), 10 },
280 {"extract", 'x', 0, 0,
281 N_("extract files from an archive"), 10 },
282 {"get", 0, 0, OPTION_ALIAS, NULL, 0 },
283 {"create", 'c', 0, 0,
284 N_("create a new archive"), 10 },
285 {"diff", 'd', 0, 0,
286 N_("find differences between archive and file system"), 10 },
287 {"compare", 0, 0, OPTION_ALIAS, NULL, 10},
288 {"append", 'r', 0, 0,
289 N_("append files to the end of an archive"), 10 },
290 {"update", 'u', 0, 0,
291 N_("only append files newer than copy in archive"), 10 },
292 {"catenate", 'A', 0, 0,
293 N_("append tar files to an archive"), 10 },
294 {"concatenate", 0, 0, OPTION_ALIAS, NULL, 10},
295 {"delete", DELETE_OPTION, 0, 0,
296 N_("delete from the archive (not on mag tapes!)"), 10 },
297
298 {NULL, 0, NULL, 0,
299 N_("Operation modifiers:"), 20},
300
301 {"verify", 'W', 0, 0,
302 N_("attempt to verify the archive after writing it"), 21 },
303 {"remove-files", REMOVE_FILES_OPTION, 0, 0,
304 N_("remove files after adding them to the archive"), 21 },
305 {"keep-old-files", 'k', 0, 0,
306 N_("don't replace existing files when extracting"), 21 },
307 {"keep-newer-files", KEEP_NEWER_FILES_OPTION, 0, 0,
308 N_("don't replace existing files that are newer than their archive copies"), 21 },
309 {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION, 0, 0,
310 N_("preserve metadata of existing directories"), 21 },
311 {"overwrite", OVERWRITE_OPTION, 0, 0,
312 N_("overwrite existing files when extracting"), 21 },
313 {"unlink-first", 'U', 0, 0,
314 N_("remove each file prior to extracting over it"), 21 },
315 {"recursive-unlink", RECURSIVE_UNLINK_OPTION, 0, 0,
316 N_("empty hierarchies prior to extracting directory"), 21 },
317 {"sparse", 'S', 0, 0,
318 N_("handle sparse files efficiently"), 21 },
319 {"to-stdout", 'O', 0, 0,
320 N_("extract files to standard output"), 21 },
321 {"to-command", TO_COMMAND_OPTION, N_("COMMAND"), 0,
322 N_("pipe extracted files to another program"), 21 },
323 {"ignore-command-error", IGNORE_COMMAND_ERROR_OPTION, 0, 0,
324 N_("ignore exit codes of children"), 21 },
325 {"no-ignore-command-error", NO_IGNORE_COMMAND_ERROR_OPTION, 0, 0,
326 N_("treat non-zero exit codes of children as error"), 21 },
327 {"incremental", 'G', 0, 0,
328 N_("handle old GNU-format incremental backup"), 21 },
329 {"listed-incremental", 'g', N_("FILE"), 0,
330 N_("handle new GNU-format incremental backup"), 21 },
331 {"ignore-failed-read", IGNORE_FAILED_READ_OPTION, 0, 0,
332 N_("do not exit with nonzero on unreadable files"), 21 },
333 {"occurrence", OCCURRENCE_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,
334 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 },
335 {"seek", 'n', NULL, 0,
336 N_("Archive is seekable"), 21 },
337
338 {NULL, 0, NULL, 0,
339 N_("Handling of file attributes:"), 30 },
340
341 {"owner", OWNER_OPTION, N_("NAME"), 0,
342 N_("force NAME as owner for added files"), 31 },
343 {"group", GROUP_OPTION, N_("NAME"), 0,
344 N_("force NAME as group for added files"), 31 },
345 {"mode", MODE_OPTION, N_("CHANGES"), 0,
346 N_("force (symbolic) mode CHANGES for added files"), 31 },
347 {"atime-preserve", ATIME_PRESERVE_OPTION, 0, 0,
348 N_("don't change access times on dumped files"), 31 },
349 {"touch", 'm', 0, 0,
350 N_("don't extract file modified time"), 31 },
351 {"same-owner", SAME_OWNER_OPTION, 0, 0,
352 N_("try extracting files with the same ownership"), 31 },
353 {"no-same-owner", NO_SAME_OWNER_OPTION, 0, 0,
354 N_("extract files as yourself"), 31 },
355 {"numeric-owner", NUMERIC_OWNER_OPTION, 0, 0,
356 N_("always use numbers for user/group names"), 31 },
357 {"preserve-permissions", 'p', 0, 0,
358 N_("extract information about file permissions (default for superuser)"), 31 },
359 {"same-permissions", 0, 0, OPTION_ALIAS, NULL, 31 },
360 {"no-same-permissions", NO_SAME_PERMISSIONS_OPTION, 0, 0,
361 N_("apply the user's umask when extracting permissions from the archive (default for ordinary users)"), 31 },
362 {"preserve-order", 's', 0, 0,
363 N_("sort names to extract to match archive"), 31 },
364 {"same-order", 0, 0, OPTION_ALIAS, NULL, 31 },
365 {"preserve", PRESERVE_OPTION, 0, 0,
366 N_("same as both -p and -s"), 31 },
367
368 {NULL, 0, NULL, 0,
369 N_("Device selection and switching:"), 40 },
370
371 {"file", 'f', N_("ARCHIVE"), 0,
372 N_("use archive file or device ARCHIVE"), 41 },
373 {"force-local", FORCE_LOCAL_OPTION, 0, 0,
374 N_("archive file is local even if has a colon"), 41 },
375 {"rmt-command", RMT_COMMAND_OPTION, N_("COMMAND"), 0,
376 N_("use given rmt COMMAND instead of rmt"), 41 },
377 {"rsh-command", RSH_COMMAND_OPTION, N_("COMMAND"), 0,
378 N_("use remote COMMAND instead of rsh"), 41 },
379 #ifdef DEVICE_PREFIX
380 {"-[0-7][lmh]", 0, NULL, OPTION_DOC, /* It is OK, since `name' will never be
381 translated */
382 N_("specify drive and density"), 41 },
383 #endif
384 {NULL, '0', NULL, OPTION_HIDDEN, NULL, 41 },
385 {NULL, '1', NULL, OPTION_HIDDEN, NULL, 41 },
386 {NULL, '2', NULL, OPTION_HIDDEN, NULL, 41 },
387 {NULL, '3', NULL, OPTION_HIDDEN, NULL, 41 },
388 {NULL, '4', NULL, OPTION_HIDDEN, NULL, 41 },
389 {NULL, '5', NULL, OPTION_HIDDEN, NULL, 41 },
390 {NULL, '6', NULL, OPTION_HIDDEN, NULL, 41 },
391 {NULL, '7', NULL, OPTION_HIDDEN, NULL, 41 },
392 {NULL, '8', NULL, OPTION_HIDDEN, NULL, 41 },
393 {NULL, '9', NULL, OPTION_HIDDEN, NULL, 41 },
394
395 {"multi-volume", 'M', 0, 0,
396 N_("create/list/extract multi-volume archive"), 41 },
397 {"tape-length", 'L', N_("NUMBER"), 0,
398 N_("change tape after writing NUMBER x 1024 bytes"), 41 },
399 {"info-script", 'F', N_("NAME"), 0,
400 N_("run script at end of each tape (implies -M)"), 41 },
401 {"new-volume-script", 0, 0, OPTION_ALIAS, NULL, 41 },
402 {"volno-file", VOLNO_FILE_OPTION, N_("FILE"), 0,
403 N_("use/update the volume number in FILE"), 41 },
404
405 {NULL, 0, NULL, 0,
406 N_("Device blocking:"), 50 },
407
408 {"blocking-factor", 'b', N_("BLOCKS"), 0,
409 N_("BLOCKS x 512 bytes per record"), 51 },
410 {"record-size", RECORD_SIZE_OPTION, N_("NUMBER"), 0,
411 N_("SIZE bytes per record, multiple of 512"), 51 },
412 {"ignore-zeros", 'i', 0, 0,
413 N_("ignore zeroed blocks in archive (means EOF)"), 51 },
414 {"read-full-records", 'B', 0, 0,
415 N_("reblock as we read (for 4.2BSD pipes)"), 51 },
416
417 {NULL, 0, NULL, 0,
418 N_("Archive format selection:"), 60 },
419
420 {"format", 'H', N_("FORMAT"), 0,
421 N_("create archive of the given format."), 61 },
422
423 {NULL, 0, NULL, 0, N_("FORMAT is one of the following:"), 62 },
424 {" v7", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("old V7 tar format"), 63},
425 {" oldgnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
426 N_("GNU format as per tar <= 1.12"), 63},
427 {" gnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
428 N_("GNU tar 1.13.x format"), 63},
429 {" ustar", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
430 N_("POSIX 1003.1-1988 (ustar) format"), 63 },
431 {" pax", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
432 N_("POSIX 1003.1-2001 (pax) format"), 63 },
433 {" posix", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("Same as pax"), 63 },
434
435 {"old-archive", OLD_ARCHIVE_OPTION, 0, 0, /* FIXME */
436 N_("same as --format=v7"), 68 },
437 {"portability", 0, 0, OPTION_ALIAS, NULL, 68 },
438 {"posix", POSIX_OPTION, 0, 0,
439 N_("same as --format=posix"), 68 },
440 {"pax-option", PAX_OPTION, N_("keyword[[:]=value][,keyword[[:]=value], ...]"), 0,
441 N_("control pax keywords"), 68 },
442 {"label", 'V', N_("TEXT"), 0,
443 N_("create archive with volume name NAME. At list/extract time, use TEXT as a globbing pattern"), 68 },
444 {"bzip2", 'j', 0, 0,
445 N_("filter the archive through bzip2"), 68 },
446 {"gzip", 'z', 0, 0,
447 N_("filter the archive through gzip"), 68 },
448 {"gunzip", 0, 0, OPTION_ALIAS, NULL, 68 },
449 {"ungzip", 0, 0, OPTION_ALIAS, NULL, 68 },
450 {"compress", 'Z', 0, 0,
451 N_("filter the archive through compress"), 68 },
452 {"uncompress", 0, 0, OPTION_ALIAS, NULL, 68 },
453 {"use-compress-program", USE_COMPRESS_PROGRAM_OPTION, N_("PROG"), 0,
454 N_("filter through PROG (must accept -d)"), 68 },
455
456 {NULL, 0, NULL, 0,
457 N_("Local file selection:"), 70 },
458
459 {"add-file", ARGP_KEY_ARG, N_("FILE"), 0,
460 N_("add given file to the archive (useful if FILE name starts with a dash)"), 71},
461 {"directory", 'C', N_("DIR"), 0,
462 N_("change to directory DIR"), 71 },
463 {"files-from", 'T', N_("FILE-OF-NAMES"), 0,
464 N_("get names to extract or create from file NAME"), 71 },
465 {"null", NULL_OPTION, 0, 0,
466 N_("-T reads null-terminated names, disable -C"), 71 },
467 {"unquote", UNQUOTE_OPTION, 0, 0,
468 N_("Unquote filenames read with -T (default)"), 71 },
469 {"no-unquote", NO_UNQUOTE_OPTION, 0, 0,
470 N_("Do not unquote filenames read with -T"), 71 },
471 {"exclude", EXCLUDE_OPTION, N_("PATTERN"), 0,
472 N_("exclude files, given as a PATTERN"), 71 },
473 {"exclude-from", 'X', N_("FILE"), 0,
474 N_("exclude patterns listed in FILE"), 71 },
475 {"exclude-caches", EXCLUDE_CACHES_OPTION, 0, 0,
476 N_("exclude directories containing a cache tag"), 71 },
477 {"ignore-case", IGNORE_CASE_OPTION, 0, 0,
478 N_("exclusion ignores case"), 71 },
479 {"anchored", ANCHORED_OPTION, 0, 0,
480 N_("exclude patterns match file name start"), 71 },
481 {"no-anchored", NO_ANCHORED_OPTION, 0, 0,
482 N_("exclude patterns match after any / (default)"), 71 },
483 {"no-ignore-case", NO_IGNORE_CASE_OPTION, 0, 0,
484 N_("exclusion is case sensitive (default)"), 71 },
485 {"no-wildcards", NO_WILDCARDS_OPTION, 0, 0,
486 N_("exclude patterns are plain strings"), 71 },
487 {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
488 N_("exclude pattern wildcards do not match '/'"), 71 },
489 {"no-recursion", NO_RECURSION_OPTION, 0, 0,
490 N_("avoid descending automatically in directories"), 71 },
491 {"one-file-system", ONE_FILE_SYSTEM_OPTION, 0, 0,
492 N_("stay in local file system when creating archive"), 71 },
493 {NULL, 'l', 0, OPTION_HIDDEN, "", 71},
494 {"recursion", RECURSION_OPTION, 0, 0,
495 N_("recurse into directories (default)"), 71 },
496 {"absolute-names", 'P', 0, 0,
497 N_("don't strip leading `/'s from file names"), 71 },
498 {"dereference", 'h', 0, 0,
499 N_("dump instead the files symlinks point to"), 71 },
500 {"starting-file", 'K', N_("MEMBER-NAME"), 0,
501 N_("begin at member MEMBER-NAME in the archive"), 71 },
502 {"strip-components", STRIP_COMPONENTS_OPTION, N_("NUMBER"), 0,
503 N_("strip NUMBER leading components from file names"), 71 },
504 {"newer", 'N', N_("DATE-OR-FILE"), 0,
505 N_("only store files newer than DATE-OR-FILE"), 71 },
506 {"newer-mtime", NEWER_MTIME_OPTION, N_("DATE"), 0,
507 N_("compare date and time when data changed only"), 71 },
508 {"after-date", 'N', N_("DATE"), 0,
509 N_("same as -N"), 71 },
510 {"backup", BACKUP_OPTION, N_("CONTROL"), OPTION_ARG_OPTIONAL,
511 N_("backup before removal, choose version CONTROL"), 71 },
512 {"suffix", SUFFIX_OPTION, N_("STRING"), 0,
513 N_("backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX"), 71 },
514 {"wildcards", WILDCARDS_OPTION, 0, 0,
515 N_("exclude patterns use wildcards (default)"), 71 },
516 {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
517 N_("exclude pattern wildcards match '/' (default)"), 71 },
518
519 {NULL, 0, NULL, 0,
520 N_("Informative output:"), 80 },
521
522 {"verbose", 'v', 0, 0,
523 N_("verbosely list files processed"), 81 },
524 {"checkpoint", CHECKPOINT_OPTION, 0, 0,
525 N_("display progress messages every 10th record"), 81 },
526 {"check-links", CHECK_LINKS_OPTION, 0, 0,
527 N_("print a message if not all links are dumped"), 82 },
528 {"totals", TOTALS_OPTION, 0, 0,
529 N_("print total bytes written while creating archive"), 82 },
530 {"utc", UTC_OPTION, 0, 0,
531 N_("print file modification dates in UTC"), 82 },
532 {"index-file", INDEX_FILE_OPTION, N_("FILE"), 0,
533 N_("send verbose output to FILE"), 82 },
534 {"block-number", 'R', 0, 0,
535 N_("show block number within archive with each message"), 82 },
536 {"interactive", 'w', 0, 0,
537 N_("ask for confirmation for every action"), 82 },
538 {"confirmation", 0, 0, OPTION_ALIAS, NULL, 82 },
539 {"show-defaults", SHOW_DEFAULTS_OPTION, 0, 0,
540 N_("Show tar defaults"), 82 },
541 {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION, 0, 0,
542 N_("When listing or extracting, list each directory that does not match search criteria"), 82 },
543
544 {NULL, 0, NULL, 0,
545 N_("Compatibility options:"), 90 },
546
547 {NULL, 'o', 0, 0,
548 N_("when creating, same as --old-archive. When extracting, same as --no-same-owner"), 91 },
549
550 {NULL, 0, NULL, 0,
551 N_("Other options:"), 100 },
552
553 {"help", '?', 0, 0, N_("Give this help list"), -1},
554 {"usage", USAGE_OPTION, 0, 0, N_("Give a short usage message"), -1},
555 {"license", LICENSE_OPTION, 0, 0, N_("Print license and exit"), -1},
556 {"version", VERSION_OPTION, 0, 0, N_("Print program version"), -1},
557 /* FIXME -V (--label) conflicts with the default short option for
558 --version */
559 {"HANG", HANG_OPTION, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
560 N_("Hang for SECS seconds (default 3600)"), 0},
561 {0, 0, 0, 0, 0, 0}
562 };
563
564 struct tar_args {
565 char const *textual_date_option;
566 int exclude_options;
567 bool o_option;
568 int pax_option;
569 char const *backup_suffix_string;
570 char const *version_control_string;
571 int input_files;
572 };
573
574 static void
575 show_default_settings (FILE *stream)
576 {
577 fprintf (stream,
578 "--format=%s -f%s -b%d --rmt-command=%s",
579 archive_format_string (DEFAULT_ARCHIVE_FORMAT),
580 DEFAULT_ARCHIVE, DEFAULT_BLOCKING,
581 DEFAULT_RMT_COMMAND);
582 #ifdef REMOTE_SHELL
583 fprintf (stream, " --rsh-command=%s", REMOTE_SHELL);
584 #endif
585 fprintf (stream, "\n");
586 }
587
588 static void
589 set_subcommand_option (enum subcommand subcommand)
590 {
591 if (subcommand_option != UNKNOWN_SUBCOMMAND
592 && subcommand_option != subcommand)
593 USAGE_ERROR ((0, 0,
594 _("You may not specify more than one `-Acdtrux' option")));
595
596 subcommand_option = subcommand;
597 }
598
599 static void
600 set_use_compress_program_option (const char *string)
601 {
602 if (use_compress_program_option
603 && strcmp (use_compress_program_option, string) != 0)
604 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
605
606 use_compress_program_option = string;
607 }
608
609 void
610 license ()
611 {
612 printf ("tar (%s) %s\n%s\n", PACKAGE_NAME, PACKAGE_VERSION,
613 "Copyright (C) 2004 Free Software Foundation, Inc.\n");
614 puts (_("Based on the work of John Gilmore and Jay Fenlason. See AUTHORS\n\
615 for complete list of authors.\n"));
616 printf (_(" GNU tar is free software; you can redistribute it and/or modify\n"
617 " it under the terms of the GNU General Public License as published by\n"
618 " the Free Software Foundation; either version 2 of the License, or\n"
619 " (at your option) any later version.\n"
620 "\n"
621 " GNU tar is distributed in the hope that it will be useful,\n"
622 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
623 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
624 " GNU General Public License for more details.\n"
625 "\n"
626 " You should have received a copy of the GNU General Public License\n"
627 " along with GNU tar; if not, write to the Free Software\n"
628 " Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n\n"));
629 exit (0);
630 }
631
632 static volatile int _argp_hang;
633
634 static bool
635 read_name_from_file (FILE *fp, struct obstack *stk)
636 {
637 int c;
638 size_t counter = 0;
639
640 for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
641 {
642 if (c == 0)
643 FATAL_ERROR((0, 0, N_("file name contains null character")));
644 obstack_1grow (stk, c);
645 counter++;
646 }
647
648 obstack_1grow (stk, 0);
649
650 return !(counter == 0 && c == EOF);
651 }
652
653 \f
654 static bool files_from_option; /* When set, tar will not refuse to create
655 empty archives */
656 static struct obstack argv_stk; /* Storage for additional command line options
657 read using -T option */
658
659 /* Prevent recursive inclusion of the same file */
660 struct file_id_list
661 {
662 struct file_id_list *next;
663 ino_t ino;
664 dev_t dev;
665 };
666
667 static struct file_id_list *file_id_list;
668
669 static void
670 add_file_id (const char *filename)
671 {
672 struct file_id_list *p;
673 struct stat st;
674
675 if (stat (filename, &st))
676 stat_fatal (filename);
677 for (p = file_id_list; p; p = p->next)
678 if (p->ino == st.st_ino && p->dev == st.st_dev)
679 {
680 FATAL_ERROR ((0, 0, _("%s: file list already read"),
681 quotearg_colon (filename)));
682 }
683 p = xmalloc (sizeof *p);
684 p->next = file_id_list;
685 p->ino = st.st_ino;
686 p->dev = st.st_dev;
687 file_id_list = p;
688 }
689
690 static void
691 update_argv (const char *filename, struct argp_state *state)
692 {
693 FILE *fp;
694 size_t count = 0, i;
695 char *start, *p;
696 char **new_argv;
697 size_t new_argc;
698 bool is_stdin = false;
699
700 if (!strcmp (filename, "-"))
701 {
702 is_stdin = true;
703 request_stdin ("-T");
704 fp = stdin;
705 }
706 else
707 {
708 add_file_id (filename);
709 if ((fp = fopen (filename, "r")) == NULL)
710 open_fatal (filename);
711 }
712
713 while (read_name_from_file (fp, &argv_stk))
714 count++;
715
716 if (!is_stdin)
717 fclose (fp);
718
719 if (count == 0)
720 return;
721
722 start = obstack_finish (&argv_stk);
723
724 if (filename_terminator == 0)
725 for (p = start; *p; p += strlen (p) + 1)
726 if (p[0] == '-')
727 count++;
728
729 new_argc = state->argc + count;
730 new_argv = xmalloc (sizeof (state->argv[0]) * (new_argc + 1));
731 memcpy (new_argv, state->argv, sizeof (state->argv[0]) * (state->argc + 1));
732 state->argv = new_argv;
733 memmove (&state->argv[state->next + count], &state->argv[state->next],
734 (state->argc - state->next + 1) * sizeof (state->argv[0]));
735
736 state->argc = new_argc;
737
738 for (i = state->next, p = start; *p; p += strlen (p) + 1, i++)
739 {
740 if (filename_terminator == 0 && p[0] == '-')
741 state->argv[i++] = "--add-file";
742 state->argv[i] = p;
743 }
744 }
745
746 \f
747 static error_t
748 parse_opt (int key, char *arg, struct argp_state *state)
749 {
750 struct tar_args *args = state->input;
751
752 switch (key)
753 {
754 case ARGP_KEY_ARG:
755 /* File name or non-parsed option, because of ARGP_IN_ORDER */
756 name_add (arg);
757 args->input_files++;
758 break;
759
760 case 'A':
761 set_subcommand_option (CAT_SUBCOMMAND);
762 break;
763
764 case 'b':
765 {
766 uintmax_t u;
767 if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
768 && u == (blocking_factor = u)
769 && 0 < blocking_factor
770 && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
771 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
772 _("Invalid blocking factor")));
773 }
774 break;
775
776 case 'B':
777 /* Try to reblock input records. For reading 4.2BSD pipes. */
778
779 /* It would surely make sense to exchange -B and -R, but it seems
780 that -B has been used for a long while in Sun tar and most
781 BSD-derived systems. This is a consequence of the block/record
782 terminology confusion. */
783
784 read_full_records_option = true;
785 break;
786
787 case 'c':
788 set_subcommand_option (CREATE_SUBCOMMAND);
789 break;
790
791 case 'C':
792 name_add ("-C");
793 name_add (arg);
794 break;
795
796 case 'd':
797 set_subcommand_option (DIFF_SUBCOMMAND);
798 break;
799
800 case 'f':
801 if (archive_names == allocated_archive_names)
802 {
803 allocated_archive_names *= 2;
804 archive_name_array =
805 xrealloc (archive_name_array,
806 sizeof (const char *) * allocated_archive_names);
807 }
808 archive_name_array[archive_names++] = arg;
809 break;
810
811 case 'F':
812 /* Since -F is only useful with -M, make it implied. Run this
813 script at the end of each tape. */
814
815 info_script_option = arg;
816 multi_volume_option = true;
817 break;
818
819 case 'g':
820 listed_incremental_option = arg;
821 after_date_option = true;
822 /* Fall through. */
823
824 case 'G':
825 /* We are making an incremental dump (FIXME: are we?); save
826 directories at the beginning of the archive, and include in each
827 directory its contents. */
828
829 incremental_option = true;
830 break;
831
832 case 'h':
833 /* Follow symbolic links. */
834 dereference_option = true;
835 break;
836
837 case 'i':
838 /* Ignore zero blocks (eofs). This can't be the default,
839 because Unix tar writes two blocks of zeros, then pads out
840 the record with garbage. */
841
842 ignore_zeros_option = true;
843 break;
844
845 case 'I':
846 USAGE_ERROR ((0, 0,
847 _("Warning: the -I option is not supported;"
848 " perhaps you meant -j or -T?")));
849 break;
850
851 case 'j':
852 set_use_compress_program_option ("bzip2");
853 break;
854
855 case 'k':
856 /* Don't replace existing files. */
857 old_files_option = KEEP_OLD_FILES;
858 break;
859
860 case 'K':
861 starting_file_option = true;
862 addname (arg, 0);
863 break;
864
865 case 'l':
866 /* Historically equivalent to --one-file-system. This usage is
867 incompatible with UNIX98 and POSIX specs and therefore is
868 deprecated. The semantics of -l option will be changed in
869 future versions. See TODO.
870 */
871 WARN ((0, 0,
872 _("Semantics of -l option will change in the future releases.")));
873 WARN ((0, 0,
874 _("Please use --one-file-system option instead.")));
875 /* FALL THROUGH */
876 case ONE_FILE_SYSTEM_OPTION:
877 /* When dumping directories, don't dump files/subdirectories
878 that are on other filesystems. */
879 one_file_system_option = true;
880 break;
881
882 case 'L':
883 {
884 uintmax_t u;
885 if (xstrtoumax (arg, 0, 10, &u, "") != LONGINT_OK)
886 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
887 _("Invalid tape length")));
888 tape_length_option = 1024 * (tarlong) u;
889 multi_volume_option = true;
890 }
891 break;
892
893 case 'm':
894 touch_option = true;
895 break;
896
897 case 'M':
898 /* Make multivolume archive: when we can't write any more into
899 the archive, re-open it, and continue writing. */
900
901 multi_volume_option = true;
902 break;
903
904 case 'n':
905 seekable_archive = true;
906 break;
907
908 #if !MSDOS
909 case 'N':
910 after_date_option = true;
911 /* Fall through. */
912
913 case NEWER_MTIME_OPTION:
914 if (NEWER_OPTION_INITIALIZED (newer_mtime_option))
915 USAGE_ERROR ((0, 0, _("More than one threshold date")));
916
917 if (FILE_SYSTEM_PREFIX_LEN (arg) != 0
918 || ISSLASH (*arg)
919 || *arg == '.')
920 {
921 struct stat st;
922 if (deref_stat (dereference_option, arg, &st) != 0)
923 {
924 stat_error (arg);
925 USAGE_ERROR ((0, 0, _("Date file not found")));
926 }
927 newer_mtime_option.tv_sec = st.st_mtime;
928 newer_mtime_option.tv_nsec = TIMESPEC_NS (st.st_mtim);
929 }
930 else
931 {
932 if (! get_date (&newer_mtime_option, arg, NULL))
933 {
934 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
935 tartime (newer_mtime_option.tv_sec), quote (arg)));
936 newer_mtime_option.tv_nsec = 0;
937 }
938 else
939 args->textual_date_option = arg;
940 }
941
942 break;
943 #endif /* not MSDOS */
944
945 case 'o':
946 args->o_option = true;
947 break;
948
949 case 'O':
950 to_stdout_option = true;
951 break;
952
953 case 'p':
954 same_permissions_option = true;
955 break;
956
957 case 'P':
958 absolute_names_option = true;
959 break;
960
961 case 'r':
962 set_subcommand_option (APPEND_SUBCOMMAND);
963 break;
964
965 case 'R':
966 /* Print block numbers for debugging bad tar archives. */
967
968 /* It would surely make sense to exchange -B and -R, but it seems
969 that -B has been used for a long while in Sun tar ans most
970 BSD-derived systems. This is a consequence of the block/record
971 terminology confusion. */
972
973 block_number_option = true;
974 break;
975
976 case 's':
977 /* Names to extr are sorted. */
978
979 same_order_option = true;
980 break;
981
982 case 'S':
983 sparse_option = true;
984 break;
985
986 case 't':
987 set_subcommand_option (LIST_SUBCOMMAND);
988 verbose_option++;
989 break;
990
991 case 'T':
992 update_argv (arg, state);
993 /* Indicate we've been given -T option. This is for backward
994 compatibility only, so that `tar cfT archive /dev/null will
995 succeed */
996 files_from_option = true;
997 break;
998
999 case 'u':
1000 set_subcommand_option (UPDATE_SUBCOMMAND);
1001 break;
1002
1003 case 'U':
1004 old_files_option = UNLINK_FIRST_OLD_FILES;
1005 break;
1006
1007 case UTC_OPTION:
1008 utc_option = true;
1009 break;
1010
1011 case 'v':
1012 verbose_option++;
1013 break;
1014
1015 case 'V':
1016 volume_label_option = arg;
1017 break;
1018
1019 case 'w':
1020 interactive_option = true;
1021 break;
1022
1023 case 'W':
1024 verify_option = true;
1025 break;
1026
1027 case 'x':
1028 set_subcommand_option (EXTRACT_SUBCOMMAND);
1029 break;
1030
1031 case 'X':
1032 if (add_exclude_file (add_exclude, excluded, arg,
1033 args->exclude_options | recursion_option, '\n')
1034 != 0)
1035 {
1036 int e = errno;
1037 FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
1038 }
1039 break;
1040
1041 case 'y':
1042 USAGE_ERROR ((0, 0,
1043 _("Warning: the -y option is not supported;"
1044 " perhaps you meant -j?")));
1045 break;
1046
1047 case 'z':
1048 set_use_compress_program_option ("gzip");
1049 break;
1050
1051 case 'Z':
1052 set_use_compress_program_option ("compress");
1053 break;
1054
1055 case ANCHORED_OPTION:
1056 args->exclude_options |= EXCLUDE_ANCHORED;
1057 break;
1058
1059 case ATIME_PRESERVE_OPTION:
1060 atime_preserve_option = true;
1061 break;
1062
1063 case CHECKPOINT_OPTION:
1064 checkpoint_option = true;
1065 break;
1066
1067 case BACKUP_OPTION:
1068 backup_option = true;
1069 if (arg)
1070 args->version_control_string = arg;
1071 break;
1072
1073 case DELETE_OPTION:
1074 set_subcommand_option (DELETE_SUBCOMMAND);
1075 break;
1076
1077 case EXCLUDE_OPTION:
1078 add_exclude (excluded, arg, args->exclude_options | recursion_option);
1079 break;
1080
1081 case EXCLUDE_CACHES_OPTION:
1082 exclude_caches_option = true;
1083 break;
1084
1085 case FORCE_LOCAL_OPTION:
1086 force_local_option = true;
1087 break;
1088
1089 case 'H':
1090 set_archive_format (arg);
1091 break;
1092
1093 case INDEX_FILE_OPTION:
1094 index_file_name = arg;
1095 break;
1096
1097 case IGNORE_CASE_OPTION:
1098 args->exclude_options |= FNM_CASEFOLD;
1099 break;
1100
1101 case IGNORE_COMMAND_ERROR_OPTION:
1102 ignore_command_error_option = true;
1103 break;
1104
1105 case IGNORE_FAILED_READ_OPTION:
1106 ignore_failed_read_option = true;
1107 break;
1108
1109 case KEEP_NEWER_FILES_OPTION:
1110 old_files_option = KEEP_NEWER_FILES;
1111 break;
1112
1113 case GROUP_OPTION:
1114 if (! (strlen (arg) < GNAME_FIELD_SIZE
1115 && gname_to_gid (arg, &group_option)))
1116 {
1117 uintmax_t g;
1118 if (xstrtoumax (arg, 0, 10, &g, "") == LONGINT_OK
1119 && g == (gid_t) g)
1120 group_option = g;
1121 else
1122 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1123 _("%s: Invalid group")));
1124 }
1125 break;
1126
1127 case MODE_OPTION:
1128 mode_option
1129 = mode_compile (arg,
1130 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
1131 if (mode_option == MODE_INVALID)
1132 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
1133 if (mode_option == MODE_MEMORY_EXHAUSTED)
1134 xalloc_die ();
1135 break;
1136
1137 case NO_ANCHORED_OPTION:
1138 args->exclude_options &= ~ EXCLUDE_ANCHORED;
1139 break;
1140
1141 case NO_IGNORE_CASE_OPTION:
1142 args->exclude_options &= ~ FNM_CASEFOLD;
1143 break;
1144
1145 case NO_IGNORE_COMMAND_ERROR_OPTION:
1146 ignore_command_error_option = false;
1147 break;
1148
1149 case NO_OVERWRITE_DIR_OPTION:
1150 old_files_option = NO_OVERWRITE_DIR_OLD_FILES;
1151 break;
1152
1153 case NO_WILDCARDS_OPTION:
1154 args->exclude_options &= ~ EXCLUDE_WILDCARDS;
1155 break;
1156
1157 case NO_WILDCARDS_MATCH_SLASH_OPTION:
1158 args->exclude_options |= FNM_FILE_NAME;
1159 break;
1160
1161 case NULL_OPTION:
1162 filename_terminator = '\0';
1163 break;
1164
1165 case NUMERIC_OWNER_OPTION:
1166 numeric_owner_option = true;
1167 break;
1168
1169 case OCCURRENCE_OPTION:
1170 if (!arg)
1171 occurrence_option = 1;
1172 else
1173 {
1174 uintmax_t u;
1175 if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK)
1176 occurrence_option = u;
1177 else
1178 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1179 _("Invalid number")));
1180 }
1181 break;
1182
1183 case OVERWRITE_OPTION:
1184 old_files_option = OVERWRITE_OLD_FILES;
1185 break;
1186
1187 case OWNER_OPTION:
1188 if (! (strlen (arg) < UNAME_FIELD_SIZE
1189 && uname_to_uid (arg, &owner_option)))
1190 {
1191 uintmax_t u;
1192 if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
1193 && u == (uid_t) u)
1194 owner_option = u;
1195 else
1196 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1197 _("Invalid owner")));
1198 }
1199 break;
1200
1201 case PAX_OPTION:
1202 args->pax_option++;
1203 xheader_set_option (arg);
1204 break;
1205
1206 case POSIX_OPTION:
1207 set_archive_format ("posix");
1208 break;
1209
1210 case PRESERVE_OPTION:
1211 same_permissions_option = true;
1212 same_order_option = true;
1213 break;
1214
1215 case RECORD_SIZE_OPTION:
1216 {
1217 uintmax_t u;
1218 if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
1219 && u == (size_t) u))
1220 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1221 _("Invalid record size")));
1222 record_size = u;
1223 if (record_size % BLOCKSIZE != 0)
1224 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1225 BLOCKSIZE));
1226 blocking_factor = record_size / BLOCKSIZE;
1227 }
1228 break;
1229
1230 case RECURSIVE_UNLINK_OPTION:
1231 recursive_unlink_option = true;
1232 break;
1233
1234 case REMOVE_FILES_OPTION:
1235 remove_files_option = true;
1236 break;
1237
1238 case RMT_COMMAND_OPTION:
1239 rmt_command = arg;
1240 break;
1241
1242 case RSH_COMMAND_OPTION:
1243 rsh_command_option = arg;
1244 break;
1245
1246 case SHOW_DEFAULTS_OPTION:
1247 show_default_settings (stdout);
1248 exit(0);
1249
1250 case STRIP_COMPONENTS_OPTION:
1251 {
1252 uintmax_t u;
1253 if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
1254 && u == (size_t) u))
1255 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
1256 _("Invalid number of elements")));
1257 strip_name_components = u;
1258 }
1259 break;
1260
1261 case SHOW_OMITTED_DIRS_OPTION:
1262 show_omitted_dirs_option = true;
1263 break;
1264
1265 case SUFFIX_OPTION:
1266 backup_option = true;
1267 args->backup_suffix_string = arg;
1268 break;
1269
1270 case TO_COMMAND_OPTION:
1271 if (to_command_option)
1272 USAGE_ERROR ((0, 0, _("Only one --to-command option allowed")));
1273 to_command_option = arg;
1274 break;
1275
1276 case TOTALS_OPTION:
1277 totals_option = true;
1278 break;
1279
1280 case USE_COMPRESS_PROGRAM_OPTION:
1281 set_use_compress_program_option (arg);
1282 break;
1283
1284 case VOLNO_FILE_OPTION:
1285 volno_file_option = arg;
1286 break;
1287
1288 case WILDCARDS_OPTION:
1289 args->exclude_options |= EXCLUDE_WILDCARDS;
1290 break;
1291
1292 case WILDCARDS_MATCH_SLASH_OPTION:
1293 args->exclude_options &= ~ FNM_FILE_NAME;
1294 break;
1295
1296 case CHECK_LINKS_OPTION:
1297 check_links_option = 1;
1298 break;
1299
1300 case NO_RECURSION_OPTION:
1301 recursion_option = 0;
1302 break;
1303
1304 case NO_SAME_OWNER_OPTION:
1305 same_owner_option = -1;
1306 break;
1307
1308 case NO_SAME_PERMISSIONS_OPTION:
1309 same_permissions_option = -1;
1310 break;
1311
1312 case RECURSION_OPTION:
1313 recursion_option = FNM_LEADING_DIR;
1314 break;
1315
1316 case SAME_OWNER_OPTION:
1317 same_owner_option = 1;
1318 break;
1319
1320 case UNQUOTE_OPTION:
1321 unquote_option = true;
1322 break;
1323
1324 case NO_UNQUOTE_OPTION:
1325 unquote_option = false;
1326 break;
1327
1328 case '0':
1329 case '1':
1330 case '2':
1331 case '3':
1332 case '4':
1333 case '5':
1334 case '6':
1335 case '7':
1336
1337 #ifdef DEVICE_PREFIX
1338 {
1339 int device = key - '0';
1340 int density;
1341 static char buf[sizeof DEVICE_PREFIX + 10];
1342 char *cursor;
1343
1344 if (arg[1])
1345 argp_error (state, _("Malformed density argument: '%s'"), arg);
1346
1347 strcpy (buf, DEVICE_PREFIX);
1348 cursor = buf + strlen (buf);
1349
1350 #ifdef DENSITY_LETTER
1351
1352 sprintf (cursor, "%d%c", device, arg[0]);
1353
1354 #else /* not DENSITY_LETTER */
1355
1356 switch (arg[0])
1357 {
1358 case 'l':
1359 #ifdef LOW_NUM
1360 device += LOW_NUM;
1361 #endif
1362 break;
1363
1364 case 'm':
1365 #ifdef MID_NUM
1366 device += MID_NUM;
1367 #else
1368 device += 8;
1369 #endif
1370 break;
1371
1372 case 'h':
1373 #ifdef HGH_NUM
1374 device += HGH_NUM;
1375 #else
1376 device += 16;
1377 #endif
1378 break;
1379
1380 default:
1381 argp_error (state, _("Unknown density: '%c'"), arg[0]);
1382 }
1383 sprintf (cursor, "%d", device);
1384
1385 #endif /* not DENSITY_LETTER */
1386
1387 if (archive_names == allocated_archive_names)
1388 {
1389 allocated_archive_names *= 2;
1390 archive_name_array =
1391 xrealloc (archive_name_array,
1392 sizeof (const char *) * allocated_archive_names);
1393 }
1394 archive_name_array[archive_names++] = strdup (buf);
1395 }
1396 break;
1397
1398 #else /* not DEVICE_PREFIX */
1399
1400 argp_error (state,
1401 _("Options `-[0-7][lmh]' not supported by *this* tar"));
1402
1403 #endif /* not DEVICE_PREFIX */
1404
1405 case '?':
1406 state->flags |= ARGP_NO_EXIT;
1407 argp_state_help (state, state->out_stream,
1408 ARGP_HELP_STD_HELP & ~ARGP_HELP_BUG_ADDR);
1409 fprintf (state->out_stream, _("\n*This* tar defaults to:\n"));
1410 show_default_settings (state->out_stream);
1411 fprintf (state->out_stream, "\n");
1412 fprintf (state->out_stream, _("Report bugs to %s.\n"),
1413 argp_program_bug_address);
1414 exit (0);
1415
1416 case USAGE_OPTION:
1417 argp_state_help (state, state->out_stream,
1418 ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
1419 break;
1420
1421 case VERSION_OPTION:
1422 fprintf (state->out_stream, "%s\n", argp_program_version);
1423 exit (0);
1424
1425 case LICENSE_OPTION:
1426 license ();
1427 break;
1428
1429 case HANG_OPTION:
1430 _argp_hang = atoi (arg ? arg : "3600");
1431 while (_argp_hang-- > 0)
1432 sleep (1);
1433 break;
1434
1435 default:
1436 return ARGP_ERR_UNKNOWN;
1437 }
1438 return 0;
1439 }
1440
1441 static struct argp argp = {
1442 options,
1443 parse_opt,
1444 N_("[FILE]..."),
1445 doc,
1446 NULL,
1447 NULL,
1448 NULL
1449 };
1450
1451 void
1452 usage (int status)
1453 {
1454 argp_help (&argp, stderr, ARGP_HELP_SEE, (char*) program_name);
1455 exit (status);
1456 }
1457
1458 /* Parse the options for tar. */
1459
1460 static struct argp_option *
1461 find_argp_option (struct argp_option *options, int letter)
1462 {
1463 for (;
1464 !(options->name == NULL
1465 && options->key == 0
1466 && options->arg == 0
1467 && options->flags == 0
1468 && options->doc == NULL); options++)
1469 if (options->key == letter)
1470 return options;
1471 return NULL;
1472 }
1473
1474 static void
1475 decode_options (int argc, char **argv)
1476 {
1477 int index;
1478 struct tar_args args;
1479
1480 /* Set some default option values. */
1481 args.textual_date_option = NULL;
1482 args.exclude_options = EXCLUDE_WILDCARDS;
1483 args.o_option = 0;
1484 args.pax_option = 0;
1485 args.backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
1486 args.version_control_string = 0;
1487 args.input_files = 0;
1488
1489 subcommand_option = UNKNOWN_SUBCOMMAND;
1490 archive_format = DEFAULT_FORMAT;
1491 blocking_factor = DEFAULT_BLOCKING;
1492 record_size = DEFAULT_BLOCKING * BLOCKSIZE;
1493 excluded = new_exclude ();
1494 newer_mtime_option.tv_sec = TYPE_MINIMUM (time_t);
1495 newer_mtime_option.tv_nsec = -1;
1496 recursion_option = FNM_LEADING_DIR;
1497 unquote_option = true;
1498
1499 owner_option = -1;
1500 group_option = -1;
1501
1502 /* Convert old-style tar call by exploding option element and rearranging
1503 options accordingly. */
1504
1505 if (argc > 1 && argv[1][0] != '-')
1506 {
1507 int new_argc; /* argc value for rearranged arguments */
1508 char **new_argv; /* argv value for rearranged arguments */
1509 char *const *in; /* cursor into original argv */
1510 char **out; /* cursor into rearranged argv */
1511 const char *letter; /* cursor into old option letters */
1512 char buffer[3]; /* constructed option buffer */
1513
1514 /* Initialize a constructed option. */
1515
1516 buffer[0] = '-';
1517 buffer[2] = '\0';
1518
1519 /* Allocate a new argument array, and copy program name in it. */
1520
1521 new_argc = argc - 1 + strlen (argv[1]);
1522 new_argv = xmalloc ((new_argc + 1) * sizeof (char *));
1523 in = argv;
1524 out = new_argv;
1525 *out++ = *in++;
1526
1527 /* Copy each old letter option as a separate option, and have the
1528 corresponding argument moved next to it. */
1529
1530 for (letter = *in++; *letter; letter++)
1531 {
1532 struct argp_option *opt;
1533
1534 buffer[1] = *letter;
1535 *out++ = xstrdup (buffer);
1536 opt = find_argp_option (options, *letter);
1537 if (opt && opt->arg)
1538 {
1539 if (in < argv + argc)
1540 *out++ = *in++;
1541 else
1542 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
1543 *letter));
1544 }
1545 }
1546
1547 /* Copy all remaining options. */
1548
1549 while (in < argv + argc)
1550 *out++ = *in++;
1551 *out = 0;
1552
1553 /* Replace the old option list by the new one. */
1554
1555 argc = new_argc;
1556 argv = new_argv;
1557 }
1558
1559 /* Parse all options and non-options as they appear. */
1560
1561 prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
1562
1563 if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_HELP,
1564 &index, &args))
1565 exit (1);
1566
1567
1568 /* Special handling for 'o' option:
1569
1570 GNU tar used to say "output old format".
1571 UNIX98 tar says don't chown files after extracting (we use
1572 "--no-same-owner" for this).
1573
1574 The old GNU tar semantics is retained when used with --create
1575 option, otherwise UNIX98 semantics is assumed */
1576
1577 if (args.o_option)
1578 {
1579 if (subcommand_option == CREATE_SUBCOMMAND)
1580 {
1581 /* GNU Tar <= 1.13 compatibility */
1582 set_archive_format ("v7");
1583 }
1584 else
1585 {
1586 /* UNIX98 compatibility */
1587 same_owner_option = -1;
1588 }
1589 }
1590
1591 /* Handle operands after any "--" argument. */
1592 for (; index < argc; index++)
1593 {
1594 name_add (argv[index]);
1595 args.input_files++;
1596 }
1597
1598 /* Derive option values and check option consistency. */
1599
1600 if (archive_format == DEFAULT_FORMAT)
1601 {
1602 if (args.pax_option)
1603 archive_format = POSIX_FORMAT;
1604 else
1605 archive_format = DEFAULT_ARCHIVE_FORMAT;
1606 }
1607
1608 if (volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
1609 assert_format (FORMAT_MASK (OLDGNU_FORMAT)
1610 | FORMAT_MASK (GNU_FORMAT));
1611
1612
1613 if (incremental_option || multi_volume_option)
1614 assert_format (FORMAT_MASK (OLDGNU_FORMAT) | FORMAT_MASK (GNU_FORMAT));
1615
1616 if (sparse_option)
1617 assert_format (FORMAT_MASK (OLDGNU_FORMAT)
1618 | FORMAT_MASK (GNU_FORMAT)
1619 | FORMAT_MASK (POSIX_FORMAT));
1620
1621 if (occurrence_option)
1622 {
1623 if (!args.input_files)
1624 USAGE_ERROR ((0, 0,
1625 _("--occurrence is meaningless without a file list")));
1626 if (subcommand_option != DELETE_SUBCOMMAND
1627 && subcommand_option != DIFF_SUBCOMMAND
1628 && subcommand_option != EXTRACT_SUBCOMMAND
1629 && subcommand_option != LIST_SUBCOMMAND)
1630 USAGE_ERROR ((0, 0,
1631 _("--occurrence cannot be used in the requested operation mode")));
1632 }
1633
1634 if (seekable_archive && subcommand_option == DELETE_SUBCOMMAND)
1635 {
1636 /* The current code in delete.c is based on the assumption that
1637 skip_member() reads all data from the archive. So, we should
1638 make sure it won't use seeks. On the other hand, the same code
1639 depends on the ability to backspace a record in the archive,
1640 so setting seekable_archive to false is technically incorrect.
1641 However, it is tested only in skip_member(), so it's not a
1642 problem. */
1643 seekable_archive = false;
1644 }
1645
1646 if (archive_names == 0)
1647 {
1648 /* If no archive file name given, try TAPE from the environment, or
1649 else, DEFAULT_ARCHIVE from the configuration process. */
1650
1651 archive_names = 1;
1652 archive_name_array[0] = getenv ("TAPE");
1653 if (! archive_name_array[0])
1654 archive_name_array[0] = DEFAULT_ARCHIVE;
1655 }
1656
1657 /* Allow multiple archives only with `-M'. */
1658
1659 if (archive_names > 1 && !multi_volume_option)
1660 USAGE_ERROR ((0, 0,
1661 _("Multiple archive files require `-M' option")));
1662
1663 if (listed_incremental_option
1664 && NEWER_OPTION_INITIALIZED (newer_mtime_option))
1665 USAGE_ERROR ((0, 0,
1666 _("Cannot combine --listed-incremental with --newer")));
1667
1668 if (volume_label_option)
1669 {
1670 size_t volume_label_max_len =
1671 (sizeof current_header->header.name
1672 - 1 /* for trailing '\0' */
1673 - (multi_volume_option
1674 ? (sizeof " Volume "
1675 - 1 /* for null at end of " Volume " */
1676 + INT_STRLEN_BOUND (int) /* for volume number */
1677 - 1 /* for sign, as 0 <= volno */)
1678 : 0));
1679 if (volume_label_max_len < strlen (volume_label_option))
1680 USAGE_ERROR ((0, 0,
1681 ngettext ("%s: Volume label is too long (limit is %lu byte)",
1682 "%s: Volume label is too long (limit is %lu bytes)",
1683 volume_label_max_len),
1684 quotearg_colon (volume_label_option),
1685 (unsigned long) volume_label_max_len));
1686 }
1687
1688 if (verify_option)
1689 {
1690 if (multi_volume_option)
1691 USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
1692 if (use_compress_program_option)
1693 USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
1694 }
1695
1696 if (use_compress_program_option)
1697 {
1698 if (multi_volume_option)
1699 USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
1700 if (subcommand_option == UPDATE_SUBCOMMAND)
1701 USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
1702 }
1703
1704 /* It is no harm to use --pax-option on non-pax archives in archive
1705 reading mode. It may even be useful, since it allows to override
1706 file attributes from tar headers. Therefore I allow such usage.
1707 --gray */
1708 if (args.pax_option
1709 && archive_format != POSIX_FORMAT
1710 && (subcommand_option != EXTRACT_SUBCOMMAND
1711 || subcommand_option != DIFF_SUBCOMMAND
1712 || subcommand_option != LIST_SUBCOMMAND))
1713 USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
1714
1715 /* If ready to unlink hierarchies, so we are for simpler files. */
1716 if (recursive_unlink_option)
1717 old_files_option = UNLINK_FIRST_OLD_FILES;
1718
1719 if (utc_option)
1720 verbose_option = 2;
1721
1722 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1723 explicit or implied, is used correctly. */
1724
1725 switch (subcommand_option)
1726 {
1727 case CREATE_SUBCOMMAND:
1728 if (args.input_files == 0 && !files_from_option)
1729 USAGE_ERROR ((0, 0,
1730 _("Cowardly refusing to create an empty archive")));
1731 break;
1732
1733 case EXTRACT_SUBCOMMAND:
1734 case LIST_SUBCOMMAND:
1735 case DIFF_SUBCOMMAND:
1736 for (archive_name_cursor = archive_name_array;
1737 archive_name_cursor < archive_name_array + archive_names;
1738 archive_name_cursor++)
1739 if (!strcmp (*archive_name_cursor, "-"))
1740 request_stdin ("-f");
1741 break;
1742
1743 case CAT_SUBCOMMAND:
1744 case UPDATE_SUBCOMMAND:
1745 case APPEND_SUBCOMMAND:
1746 for (archive_name_cursor = archive_name_array;
1747 archive_name_cursor < archive_name_array + archive_names;
1748 archive_name_cursor++)
1749 if (!strcmp (*archive_name_cursor, "-"))
1750 USAGE_ERROR ((0, 0,
1751 _("Options `-Aru' are incompatible with `-f -'")));
1752
1753 default:
1754 break;
1755 }
1756
1757 archive_name_cursor = archive_name_array;
1758
1759 /* Prepare for generating backup names. */
1760
1761 if (args.backup_suffix_string)
1762 simple_backup_suffix = xstrdup (args.backup_suffix_string);
1763
1764 if (backup_option)
1765 backup_type = xget_version ("--backup", args.version_control_string);
1766
1767 if (verbose_option && args.textual_date_option)
1768 {
1769 /* FIXME: tartime should support nanoseconds, too, so that this
1770 comparison doesn't complain about lost nanoseconds. */
1771 char const *treated_as = tartime (newer_mtime_option.tv_sec);
1772 if (strcmp (args.textual_date_option, treated_as) != 0)
1773 WARN ((0, 0,
1774 ngettext ("Treating date `%s' as %s + %ld nanosecond",
1775 "Treating date `%s' as %s + %ld nanoseconds",
1776 newer_mtime_option.tv_nsec),
1777 args.textual_date_option, treated_as,
1778 newer_mtime_option.tv_nsec));
1779 }
1780 }
1781
1782 \f
1783 /* Tar proper. */
1784
1785 /* Main routine for tar. */
1786 int
1787 main (int argc, char **argv)
1788 {
1789 set_start_time ();
1790 program_name = argv[0];
1791
1792 setlocale (LC_ALL, "");
1793 bindtextdomain (PACKAGE, LOCALEDIR);
1794 textdomain (PACKAGE);
1795
1796 exit_status = TAREXIT_SUCCESS;
1797 filename_terminator = '\n';
1798 set_quoting_style (0, escape_quoting_style);
1799
1800 /* Pre-allocate a few structures. */
1801
1802 allocated_archive_names = 10;
1803 archive_name_array =
1804 xmalloc (sizeof (const char *) * allocated_archive_names);
1805 archive_names = 0;
1806
1807 obstack_init (&argv_stk);
1808
1809 #ifdef SIGCHLD
1810 /* System V fork+wait does not work if SIGCHLD is ignored. */
1811 signal (SIGCHLD, SIG_DFL);
1812 #endif
1813
1814 init_names ();
1815
1816 /* Decode options. */
1817
1818 decode_options (argc, argv);
1819 name_init ();
1820
1821 /* Main command execution. */
1822
1823 if (volno_file_option)
1824 init_volume_number ();
1825
1826 switch (subcommand_option)
1827 {
1828 case UNKNOWN_SUBCOMMAND:
1829 USAGE_ERROR ((0, 0,
1830 _("You must specify one of the `-Acdtrux' options")));
1831
1832 case CAT_SUBCOMMAND:
1833 case UPDATE_SUBCOMMAND:
1834 case APPEND_SUBCOMMAND:
1835 update_archive ();
1836 break;
1837
1838 case DELETE_SUBCOMMAND:
1839 delete_archive_members ();
1840 break;
1841
1842 case CREATE_SUBCOMMAND:
1843 create_archive ();
1844 if (totals_option)
1845 print_total_written ();
1846 break;
1847
1848 case EXTRACT_SUBCOMMAND:
1849 extr_init ();
1850 read_and (extract_archive);
1851
1852 /* FIXME: should extract_finish () even if an ordinary signal is
1853 received. */
1854 extract_finish ();
1855
1856 break;
1857
1858 case LIST_SUBCOMMAND:
1859 read_and (list_archive);
1860 break;
1861
1862 case DIFF_SUBCOMMAND:
1863 diff_init ();
1864 read_and (diff_archive);
1865 break;
1866 }
1867
1868 if (check_links_option)
1869 check_links ();
1870
1871 if (volno_file_option)
1872 closeout_volume_number ();
1873
1874 /* Dispose of allocated memory, and return. */
1875
1876 free (archive_name_array);
1877 name_term ();
1878
1879 if (stdlis != stderr && (ferror (stdlis) || fclose (stdlis) != 0))
1880 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1881 if (exit_status == TAREXIT_FAILURE)
1882 error (0, 0, _("Error exit delayed from previous errors"));
1883 if (ferror (stderr) || fclose (stderr) != 0)
1884 exit_status = TAREXIT_FAILURE;
1885 return exit_status;
1886 }
1887
1888 void
1889 tar_stat_init (struct tar_stat_info *st)
1890 {
1891 memset (st, 0, sizeof (*st));
1892 }
1893
1894 void
1895 tar_stat_destroy (struct tar_stat_info *st)
1896 {
1897 free (st->orig_file_name);
1898 free (st->file_name);
1899 free (st->link_name);
1900 free (st->uname);
1901 free (st->gname);
1902 free (st->sparse_map);
1903 memset (st, 0, sizeof (*st));
1904 }
This page took 0.130485 seconds and 5 git commands to generate.