1 /* Various processing of names.
3 Copyright 1988-2020 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <wordsplit.h>
28 static void name_add_option (int option
, const char *arg
);
29 static void name_add_dir (const char *name
);
30 static void name_add_file (const char *name
);
34 ADD_FILE_OPTION
= 256,
35 EXCLUDE_BACKUPS_OPTION
,
36 EXCLUDE_CACHES_OPTION
,
37 EXCLUDE_CACHES_UNDER_OPTION
,
38 EXCLUDE_CACHES_ALL_OPTION
,
40 EXCLUDE_IGNORE_OPTION
,
41 EXCLUDE_IGNORE_RECURSIVE_OPTION
,
43 EXCLUDE_TAG_UNDER_OPTION
,
44 EXCLUDE_TAG_ALL_OPTION
,
46 EXCLUDE_VCS_IGNORES_OPTION
,
48 NO_IGNORE_CASE_OPTION
,
55 NO_VERBATIM_FILES_FROM_OPTION
,
56 NO_WILDCARDS_MATCH_SLASH_OPTION
,
60 VERBATIM_FILES_FROM_OPTION
,
61 WILDCARDS_MATCH_SLASH_OPTION
,
65 static struct argp_option names_options
[] = {
68 N_("Local file name selection:"), GRID
},
70 {"add-file", ADD_FILE_OPTION
, N_("FILE"), 0,
71 N_("add given FILE to the archive (useful if its name starts with a dash)"), GRID
+1 },
72 {"directory", 'C', N_("DIR"), 0,
73 N_("change to directory DIR"), GRID
+1 },
74 {"files-from", 'T', N_("FILE"), 0,
75 N_("get names to extract or create from FILE"), GRID
+1 },
76 {"null", NULL_OPTION
, 0, 0,
77 N_("-T reads null-terminated names; implies --verbatim-files-from"),
79 {"no-null", NO_NULL_OPTION
, 0, 0,
80 N_("disable the effect of the previous --null option"), GRID
+1 },
81 {"unquote", UNQUOTE_OPTION
, 0, 0,
82 N_("unquote input file or member names (default)"), GRID
+1 },
83 {"no-unquote", NO_UNQUOTE_OPTION
, 0, 0,
84 N_("do not unquote input file or member names"), GRID
+1 },
85 {"verbatim-files-from", VERBATIM_FILES_FROM_OPTION
, 0, 0,
86 N_("-T reads file names verbatim (no escape or option handling)"), GRID
+1 },
87 {"no-verbatim-files-from", NO_VERBATIM_FILES_FROM_OPTION
, 0, 0,
88 N_("-T treats file names starting with dash as options (default)"),
90 {"exclude", EXCLUDE_OPTION
, N_("PATTERN"), 0,
91 N_("exclude files, given as a PATTERN"), GRID
+1 },
92 {"exclude-from", 'X', N_("FILE"), 0,
93 N_("exclude patterns listed in FILE"), GRID
+1 },
94 {"exclude-caches", EXCLUDE_CACHES_OPTION
, 0, 0,
95 N_("exclude contents of directories containing CACHEDIR.TAG, "
96 "except for the tag file itself"), GRID
+1 },
97 {"exclude-caches-under", EXCLUDE_CACHES_UNDER_OPTION
, 0, 0,
98 N_("exclude everything under directories containing CACHEDIR.TAG"),
100 {"exclude-caches-all", EXCLUDE_CACHES_ALL_OPTION
, 0, 0,
101 N_("exclude directories containing CACHEDIR.TAG"), GRID
+1 },
102 {"exclude-tag", EXCLUDE_TAG_OPTION
, N_("FILE"), 0,
103 N_("exclude contents of directories containing FILE, except"
104 " for FILE itself"), GRID
+1 },
105 {"exclude-ignore", EXCLUDE_IGNORE_OPTION
, N_("FILE"), 0,
106 N_("read exclude patterns for each directory from FILE, if it exists"),
108 {"exclude-ignore-recursive", EXCLUDE_IGNORE_RECURSIVE_OPTION
, N_("FILE"), 0,
109 N_("read exclude patterns for each directory and its subdirectories "
110 "from FILE, if it exists"), GRID
+1 },
111 {"exclude-tag-under", EXCLUDE_TAG_UNDER_OPTION
, N_("FILE"), 0,
112 N_("exclude everything under directories containing FILE"), GRID
+1 },
113 {"exclude-tag-all", EXCLUDE_TAG_ALL_OPTION
, N_("FILE"), 0,
114 N_("exclude directories containing FILE"), GRID
+1 },
115 {"exclude-vcs", EXCLUDE_VCS_OPTION
, NULL
, 0,
116 N_("exclude version control system directories"), GRID
+1 },
117 {"exclude-vcs-ignores", EXCLUDE_VCS_IGNORES_OPTION
, NULL
, 0,
118 N_("read exclude patterns from the VCS ignore files"), GRID
+1 },
119 {"exclude-backups", EXCLUDE_BACKUPS_OPTION
, NULL
, 0,
120 N_("exclude backup and lock files"), GRID
+1 },
121 {"recursion", RECURSION_OPTION
, 0, 0,
122 N_("recurse into directories (default)"), GRID
+1 },
123 {"no-recursion", NO_RECURSION_OPTION
, 0, 0,
124 N_("avoid descending automatically in directories"), GRID
+1 },
129 N_("File name matching options (affect both exclude and include patterns):"),
131 {"anchored", ANCHORED_OPTION
, 0, 0,
132 N_("patterns match file name start"), GRID
+1 },
133 {"no-anchored", NO_ANCHORED_OPTION
, 0, 0,
134 N_("patterns match after any '/' (default for exclusion)"), GRID
+1 },
135 {"ignore-case", IGNORE_CASE_OPTION
, 0, 0,
136 N_("ignore case"), GRID
+1 },
137 {"no-ignore-case", NO_IGNORE_CASE_OPTION
, 0, 0,
138 N_("case sensitive matching (default)"), GRID
+1 },
139 {"wildcards", WILDCARDS_OPTION
, 0, 0,
140 N_("use wildcards (default for exclusion)"), GRID
+1 },
141 {"no-wildcards", NO_WILDCARDS_OPTION
, 0, 0,
142 N_("verbatim string matching"), GRID
+1 },
143 {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION
, 0, 0,
144 N_("wildcards match '/' (default for exclusion)"), GRID
+1 },
145 {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION
, 0, 0,
146 N_("wildcards do not match '/'"), GRID
+1 },
152 static struct argp_option
const *
153 file_selection_option (int key
)
155 struct argp_option
*p
;
157 for (p
= names_options
;
158 !(p
->name
== NULL
&& p
->key
== 0 && p
->doc
== NULL
); p
++)
165 file_selection_option_name (int key
)
167 struct argp_option
const *opt
= file_selection_option (key
);
168 return opt
? opt
->name
: NULL
;
172 is_file_selection_option (int key
)
174 return file_selection_option (key
) != NULL
;
177 /* Either NL or NUL, as decided by the --null option. */
178 static char filename_terminator
= '\n';
179 /* Treat file names read from -T input verbatim */
180 static bool verbatim_files_from_option
;
183 names_parse_opt (int key
, char *arg
, struct argp_state
*state
)
195 case ADD_FILE_OPTION
:
200 if (is_file_selection_option (key
))
201 name_add_option (key
, arg
);
203 return ARGP_ERR_UNKNOWN
;
208 /* Wildcard matching settings */
211 default_wildcards
, /* For exclusion == enable_wildcards,
212 for inclusion == disable_wildcards */
217 static enum wildcards wildcards
= default_wildcards
;
218 /* Wildcard settings (--wildcards/--no-wildcards) */
219 static int matching_flags
= 0;
220 /* exclude_fnmatch options */
221 static int include_anchored
= EXCLUDE_ANCHORED
;
222 /* Pattern anchoring options used for file inclusion */
224 #define EXCLUDE_OPTIONS \
225 (((wildcards != disable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
229 #define INCLUDE_OPTIONS \
230 (((wildcards == enable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
235 static char const * const vcs_file_table
[] = {
269 static char const * const backup_file_table
[] = {
277 add_exclude_array (char const * const * fv
, int opts
)
281 for (i
= 0; fv
[i
]; i
++)
282 add_exclude (excluded
, fv
[i
], opts
);
286 handle_file_selection_option (int key
, const char *arg
)
290 case EXCLUDE_BACKUPS_OPTION
:
291 add_exclude_array (backup_file_table
, EXCLUDE_WILDCARDS
);
295 add_exclude (excluded
, arg
, EXCLUDE_OPTIONS
);
298 case EXCLUDE_CACHES_OPTION
:
299 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_contents
,
303 case EXCLUDE_CACHES_UNDER_OPTION
:
304 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_under
,
308 case EXCLUDE_CACHES_ALL_OPTION
:
309 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_all
,
313 case EXCLUDE_IGNORE_OPTION
:
314 excfile_add (arg
, EXCL_NON_RECURSIVE
);
317 case EXCLUDE_IGNORE_RECURSIVE_OPTION
:
318 excfile_add (arg
, EXCL_RECURSIVE
);
321 case EXCLUDE_TAG_OPTION
:
322 add_exclusion_tag (arg
, exclusion_tag_contents
, NULL
);
325 case EXCLUDE_TAG_UNDER_OPTION
:
326 add_exclusion_tag (arg
, exclusion_tag_under
, NULL
);
329 case EXCLUDE_TAG_ALL_OPTION
:
330 add_exclusion_tag (arg
, exclusion_tag_all
, NULL
);
333 case EXCLUDE_VCS_OPTION
:
334 add_exclude_array (vcs_file_table
, 0);
337 case EXCLUDE_VCS_IGNORES_OPTION
:
338 exclude_vcs_ignores ();
341 case RECURSION_OPTION
:
342 recursion_option
= FNM_LEADING_DIR
;
345 case NO_RECURSION_OPTION
:
346 recursion_option
= 0;
350 unquote_option
= true;
353 case NO_UNQUOTE_OPTION
:
354 unquote_option
= false;
358 filename_terminator
= '\0';
359 verbatim_files_from_option
= true;
363 filename_terminator
= '\n';
364 verbatim_files_from_option
= false;
368 if (add_exclude_file (add_exclude
, excluded
, arg
, EXCLUDE_OPTIONS
, '\n')
372 FATAL_ERROR ((0, e
, "%s", quotearg_colon (arg
)));
376 case ANCHORED_OPTION
:
377 matching_flags
|= EXCLUDE_ANCHORED
;
380 case NO_ANCHORED_OPTION
:
381 include_anchored
= 0; /* Clear the default for comman line args */
382 matching_flags
&= ~ EXCLUDE_ANCHORED
;
385 case IGNORE_CASE_OPTION
:
386 matching_flags
|= FNM_CASEFOLD
;
389 case NO_IGNORE_CASE_OPTION
:
390 matching_flags
&= ~ FNM_CASEFOLD
;
393 case WILDCARDS_OPTION
:
394 wildcards
= enable_wildcards
;
397 case NO_WILDCARDS_OPTION
:
398 wildcards
= disable_wildcards
;
401 case WILDCARDS_MATCH_SLASH_OPTION
:
402 matching_flags
&= ~ FNM_FILE_NAME
;
405 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
406 matching_flags
|= FNM_FILE_NAME
;
409 case VERBATIM_FILES_FROM_OPTION
:
410 verbatim_files_from_option
= true;
413 case NO_VERBATIM_FILES_FROM_OPTION
:
414 verbatim_files_from_option
= false;
418 FATAL_ERROR ((0, 0, "unhandled positional option %d", key
));
422 static struct argp names_argp
= {
432 struct argp_child names_argp_children
[] = {
433 { &names_argp
, 0, "", 0 },
437 /* User and group names. */
439 /* Make sure you link with the proper libraries if you are running the
440 Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
441 This code should also be modified for non-UNIX systems to do something
444 static char *cached_uname
;
445 static char *cached_gname
;
447 static uid_t cached_uid
; /* valid only if cached_uname is not empty */
448 static gid_t cached_gid
; /* valid only if cached_gname is not empty */
450 /* These variables are valid only if nonempty. */
451 static char *cached_no_such_uname
;
452 static char *cached_no_such_gname
;
454 /* These variables are valid only if nonzero. It's not worth optimizing
455 the case for weird systems where 0 is not a valid uid or gid. */
456 static uid_t cached_no_such_uid
;
457 static gid_t cached_no_such_gid
;
459 /* Given UID, find the corresponding UNAME. */
461 uid_to_uname (uid_t uid
, char **uname
)
463 struct passwd
*passwd
;
465 if (uid
!= 0 && uid
== cached_no_such_uid
)
467 *uname
= xstrdup ("");
471 if (!cached_uname
|| uid
!= cached_uid
)
473 passwd
= getpwuid (uid
);
477 assign_string (&cached_uname
, passwd
->pw_name
);
481 cached_no_such_uid
= uid
;
482 *uname
= xstrdup ("");
486 *uname
= xstrdup (cached_uname
);
489 /* Given GID, find the corresponding GNAME. */
491 gid_to_gname (gid_t gid
, char **gname
)
495 if (gid
!= 0 && gid
== cached_no_such_gid
)
497 *gname
= xstrdup ("");
501 if (!cached_gname
|| gid
!= cached_gid
)
503 group
= getgrgid (gid
);
507 assign_string (&cached_gname
, group
->gr_name
);
511 cached_no_such_gid
= gid
;
512 *gname
= xstrdup ("");
516 *gname
= xstrdup (cached_gname
);
519 /* Given UNAME, set the corresponding UID and return 1, or else, return 0. */
521 uname_to_uid (char const *uname
, uid_t
*uidp
)
523 struct passwd
*passwd
;
525 if (cached_no_such_uname
526 && strcmp (uname
, cached_no_such_uname
) == 0)
530 || uname
[0] != cached_uname
[0]
531 || strcmp (uname
, cached_uname
) != 0)
533 passwd
= getpwnam (uname
);
536 cached_uid
= passwd
->pw_uid
;
537 assign_string (&cached_uname
, passwd
->pw_name
);
541 assign_string (&cached_no_such_uname
, uname
);
549 /* Given GNAME, set the corresponding GID and return 1, or else, return 0. */
551 gname_to_gid (char const *gname
, gid_t
*gidp
)
555 if (cached_no_such_gname
556 && strcmp (gname
, cached_no_such_gname
) == 0)
560 || gname
[0] != cached_gname
[0]
561 || strcmp (gname
, cached_gname
) != 0)
563 group
= getgrnam (gname
);
566 cached_gid
= group
->gr_gid
;
567 assign_string (&cached_gname
, gname
);
571 assign_string (&cached_no_such_gname
, gname
);
581 make_name (const char *file_name
)
583 struct name
*p
= xzalloc (sizeof (*p
));
586 p
->name
= xstrdup (file_name
);
587 p
->length
= strlen (p
->name
);
592 free_name (struct name
*p
)
603 /* Names from the command call. */
605 static struct name
*namelist
; /* first name in list, if any */
606 static struct name
*nametail
; /* end of name list */
608 /* File name arguments are processed in two stages: first a
609 name element list (see below) is filled, then the names from it
610 are moved into the namelist.
612 This awkward process is needed only to implement --same-order option,
613 which is meant to help process large archives on machines with
614 limited memory. With this option on, namelist contains at most one
615 entry, which diminishes the memory consumption.
617 However, I very much doubt if we still need this -- Sergey */
619 /* A name_list element contains entries of three types: */
623 NELT_NAME
, /* File name */
624 NELT_CHDIR
, /* Change directory request */
625 NELT_FILE
, /* Read file names from that file */
626 NELT_NOOP
, /* No operation */
627 NELT_OPTION
/* Filename-selection option */
630 struct name_elt
/* A name_array element. */
632 struct name_elt
*next
, *prev
;
633 enum nelt_type type
; /* Element type, see NELT_* constants above */
636 const char *name
; /* File or directory name */
637 struct /* File, if type == NELT_FILE */
639 const char *name
;/* File name */
640 size_t line
; /* Input line number */
641 int term
; /* File name terminator in the list */
642 bool verbatim
; /* Verbatim handling of file names: no white-space
643 trimming, no option processing */
650 } opt
; /* NELT_OPTION */
654 static struct name_elt
*name_head
; /* store a list of names */
656 /* how many of the entries are file names? */
657 enum files_count filename_args
= FILES_NONE
;
659 static struct name_elt
*
660 name_elt_alloc (void)
662 struct name_elt
*elt
;
664 elt
= xmalloc (sizeof (*elt
));
668 name_head
->prev
= name_head
->next
= NULL
;
669 name_head
->type
= NELT_NOOP
;
670 elt
= xmalloc (sizeof (*elt
));
673 elt
->prev
= name_head
->prev
;
675 name_head
->prev
->next
= elt
;
676 elt
->next
= name_head
;
677 name_head
->prev
= elt
;
682 name_list_adjust (void)
685 while (name_head
->prev
)
686 name_head
= name_head
->prev
;
689 /* For error-reporting purposes, keep a doubly-linked list of unconsumed file
690 selection options. The option is deemed unconsumed unless followed by one
691 or more file/member name arguments. When archive creation is requested,
692 each file selection option encountered is pushed into the list. The list
693 is cleared upon encountering a file name argument.
695 If the list is not empty when all arguments have been processed, an error
696 message is issued reporting the options that had no effect.
698 For simplicity, only a tail pointer of the list is maintained.
701 struct name_elt
*unconsumed_option_tail
;
703 /* Push an option to the list */
705 unconsumed_option_push (struct name_elt
*elt
)
708 elt
->prev
= unconsumed_option_tail
;
709 if (unconsumed_option_tail
)
710 unconsumed_option_tail
->next
= elt
;
711 unconsumed_option_tail
= elt
;
714 /* Clear the unconsumed option list */
716 unconsumed_option_free (void)
718 while (unconsumed_option_tail
)
720 struct name_elt
*elt
= unconsumed_option_tail
;
721 unconsumed_option_tail
= unconsumed_option_tail
->prev
;
726 /* Report any options that have not been consumed */
728 unconsumed_option_report (void)
730 if (unconsumed_option_tail
)
732 struct name_elt
*elt
;
734 ERROR ((0, 0, _("The following options were used after any non-optional arguments in archive create or update mode. These options are positional and affect only arguments that follow them. Please, rearrange them properly.")));
736 elt
= unconsumed_option_tail
;
745 ERROR ((0, 0, _("-C %s has no effect"), quote (elt
->v
.name
)));
750 ERROR ((0, 0, _("--%s %s has no effect"),
751 file_selection_option_name (elt
->v
.opt
.option
),
752 quote (elt
->v
.opt
.arg
)));
754 ERROR ((0, 0, _("--%s has no effect"),
755 file_selection_option_name (elt
->v
.opt
.option
)));
764 unconsumed_option_free ();
769 name_list_advance (void)
771 struct name_elt
*elt
= name_head
;
772 name_head
= elt
->next
;
774 name_head
->prev
= NULL
;
775 if (elt
->type
== NELT_OPTION
|| elt
->type
== NELT_CHDIR
)
777 if (subcommand_option
== CREATE_SUBCOMMAND
778 || subcommand_option
== UPDATE_SUBCOMMAND
)
779 unconsumed_option_push (elt
);
783 if (elt
->type
!= NELT_NOOP
)
784 unconsumed_option_free ();
789 /* Add to name_array the file NAME with fnmatch options MATFLAGS */
791 name_add_name (const char *name
)
793 struct name_elt
*ep
= name_elt_alloc ();
795 ep
->type
= NELT_NAME
;
798 switch (filename_args
)
801 filename_args
= FILES_ONE
;
805 filename_args
= FILES_MANY
;
814 name_add_option (int option
, const char *arg
)
816 struct name_elt
*elt
= name_elt_alloc ();
817 elt
->type
= NELT_OPTION
;
818 elt
->v
.opt
.option
= option
;
819 elt
->v
.opt
.arg
= arg
;
822 /* Add to name_array a chdir request for the directory NAME */
824 name_add_dir (const char *name
)
826 struct name_elt
*ep
= name_elt_alloc ();
827 ep
->type
= NELT_CHDIR
;
832 name_add_file (const char *name
)
834 struct name_elt
*ep
= name_elt_alloc ();
836 ep
->type
= NELT_FILE
;
837 ep
->v
.file
.name
= name
;
839 ep
->v
.file
.fp
= NULL
;
841 /* We don't know beforehand how many files are listed.
842 Assume more than one. */
843 filename_args
= FILES_MANY
;
846 /* Names from external name file. */
848 static char *name_buffer
; /* buffer to hold the current file name */
849 static size_t name_buffer_length
; /* allocated length of name_buffer */
851 /* Set up to gather file names for tar. They can either come from a
852 file or were saved from decoding arguments. */
856 name_buffer
= xmalloc (NAME_FIELD_SIZE
+ 2);
857 name_buffer_length
= NAME_FIELD_SIZE
;
867 /* Prevent recursive inclusion of the same file */
870 struct file_id_list
*next
;
873 const char *from_file
;
876 static struct file_id_list
*file_id_list
;
878 /* Return the name of the file from which the file names and options
882 file_list_name (void)
884 struct name_elt
*elt
;
886 for (elt
= name_head
; elt
; elt
= elt
->next
)
887 if (elt
->type
== NELT_FILE
&& elt
->v
.file
.fp
)
888 return elt
->v
.file
.name
;
889 return _("command line");
893 add_file_id (const char *filename
)
895 struct file_id_list
*p
;
897 const char *reading_from
;
899 if (stat (filename
, &st
))
900 stat_fatal (filename
);
901 reading_from
= file_list_name ();
902 for (p
= file_id_list
; p
; p
= p
->next
)
903 if (p
->ino
== st
.st_ino
&& p
->dev
== st
.st_dev
)
905 int oldc
= set_char_quoting (NULL
, ':', 1);
907 _("%s: file list requested from %s already read from %s"),
908 quotearg_n (0, filename
),
909 reading_from
, p
->from_file
));
910 set_char_quoting (NULL
, ':', oldc
);
913 p
= xmalloc (sizeof *p
);
914 p
->next
= file_id_list
;
917 p
->from_file
= reading_from
;
922 /* Chop trailing slashes. */
924 chopslash (char *str
)
926 char *p
= str
+ strlen (str
) - 1;
927 while (p
> str
&& ISSLASH (*p
))
931 enum read_file_list_state
/* Result of reading file name from the list file */
933 file_list_success
, /* OK, name read successfully */
934 file_list_end
, /* End of list file */
935 file_list_zero
, /* Zero separator encountered where it should not */
936 file_list_skip
/* Empty (zero-length) entry encountered, skip it */
939 /* Read from FP a sequence of characters up to TERM and put them
942 static enum read_file_list_state
943 read_name_from_file (struct name_elt
*ent
)
947 FILE *fp
= ent
->v
.file
.fp
;
948 int term
= ent
->v
.file
.term
;
951 for (c
= getc (fp
); c
!= EOF
&& c
!= term
; c
= getc (fp
))
953 if (counter
== name_buffer_length
)
954 name_buffer
= x2realloc (name_buffer
, &name_buffer_length
);
955 name_buffer
[counter
++] = c
;
958 /* We have read a zero separator. The file possibly is
960 return file_list_zero
;
964 if (counter
== 0 && c
!= EOF
)
965 return file_list_skip
;
967 if (counter
== name_buffer_length
)
968 name_buffer
= x2realloc (name_buffer
, &name_buffer_length
);
969 name_buffer
[counter
] = 0;
970 chopslash (name_buffer
);
971 return (counter
== 0 && c
== EOF
) ? file_list_end
: file_list_success
;
975 handle_option (const char *str
, struct name_elt
const *ent
)
979 struct option_locus loc
;
981 while (*str
&& isspace (*str
))
987 if (wordsplit (str
, &ws
, WRDSF_DEFFLAGS
|WRDSF_DOOFFS
))
988 FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
989 str
, wordsplit_strerror (&ws
)));
990 ws
.ws_wordv
[0] = (char *) program_name
;
991 loc
.source
= OPTS_FILE
;
992 loc
.name
= ent
->v
.file
.name
;
993 loc
.line
= ent
->v
.file
.line
;
994 more_options (ws
.ws_wordc
+ws
.ws_offs
, ws
.ws_wordv
, &loc
);
995 for (i
= 0; i
< ws
.ws_wordc
+ws
.ws_offs
; i
++)
996 ws
.ws_wordv
[i
] = NULL
;
998 wordsplit_free (&ws
);
1003 read_next_name (struct name_elt
*ent
, struct name_elt
*ret
)
1005 if (!ent
->v
.file
.fp
)
1007 if (!strcmp (ent
->v
.file
.name
, "-"))
1009 request_stdin ("-T");
1010 ent
->v
.file
.fp
= stdin
;
1014 if (add_file_id (ent
->v
.file
.name
))
1016 name_list_advance ();
1019 if ((ent
->v
.file
.fp
= fopen (ent
->v
.file
.name
, "r")) == NULL
)
1020 open_fatal (ent
->v
.file
.name
);
1022 ent
->v
.file
.term
= filename_terminator
;
1023 ent
->v
.file
.verbatim
= verbatim_files_from_option
;
1028 switch (read_name_from_file (ent
))
1030 case file_list_skip
:
1033 case file_list_zero
:
1034 WARNOPT (WARN_FILENAME_WITH_NULS
,
1035 (0, 0, N_("%s: file name read contains nul character"),
1036 quotearg_colon (ent
->v
.file
.name
)));
1037 ent
->v
.file
.term
= 0;
1039 case file_list_success
:
1040 if (!ent
->v
.file
.verbatim
)
1043 unquote_string (name_buffer
);
1044 if (handle_option (name_buffer
, ent
) == 0)
1046 name_list_adjust ();
1050 ret
->type
= NELT_NAME
;
1051 ret
->v
.name
= name_buffer
;
1055 if (strcmp (ent
->v
.file
.name
, "-"))
1056 fclose (ent
->v
.file
.fp
);
1057 ent
->v
.file
.fp
= NULL
;
1058 name_list_advance ();
1065 copy_name (struct name_elt
*ep
)
1070 source
= ep
->v
.name
;
1071 source_len
= strlen (source
);
1072 while (name_buffer_length
<= source_len
)
1073 name_buffer
= x2realloc(name_buffer
, &name_buffer_length
);
1074 strcpy (name_buffer
, source
);
1075 chopslash (name_buffer
);
1079 /* Get the next NELT_NAME element from name_array. Result is in
1080 static storage and can't be relied upon across two calls.
1082 If CHANGE_DIRS is true, treat any entries of type NELT_CHDIR as
1083 the request to change to the given directory.
1086 static struct name_elt
*
1087 name_next_elt (int change_dirs
)
1089 static struct name_elt entry
;
1090 struct name_elt
*ep
;
1092 while ((ep
= name_head
) != NULL
)
1097 name_list_advance ();
1101 if (read_next_name (ep
, &entry
) == 0)
1108 chdir_do (chdir_arg (xstrdup (ep
->v
.name
)));
1109 name_list_advance ();
1116 unquote_string (name_buffer
);
1117 entry
.type
= ep
->type
;
1118 entry
.v
.name
= name_buffer
;
1119 name_list_advance ();
1123 handle_file_selection_option (ep
->v
.opt
.option
, ep
->v
.opt
.arg
);
1124 name_list_advance ();
1129 unconsumed_option_report ();
1135 name_next (int change_dirs
)
1137 struct name_elt
*nelt
= name_next_elt (change_dirs
);
1138 return nelt
? nelt
->v
.name
: NULL
;
1141 /* Gather names in a list for scanning. Could hash them later if we
1144 If the names are already sorted to match the archive, we just read
1145 them one by one. name_gather reads the first one, and it is called
1146 by name_match as appropriate to read the next ones. At EOF, the
1147 last name read is just left in the buffer. This option lets users
1148 of small machines extract an arbitrary number of files by doing
1149 "tar t" and editing down the list of files. */
1154 /* Buffer able to hold a single name. */
1155 static struct name
*buffer
= NULL
;
1157 struct name_elt
*ep
;
1159 if (same_order_option
)
1161 static int change_dir
;
1163 while ((ep
= name_next_elt (0)) && ep
->type
== NELT_CHDIR
)
1164 change_dir
= chdir_arg (xstrdup (ep
->v
.name
));
1169 buffer
= make_name (ep
->v
.name
);
1170 buffer
->change_dir
= change_dir
;
1172 buffer
->found_count
= 0;
1173 buffer
->matching_flags
= INCLUDE_OPTIONS
;
1174 buffer
->directory
= NULL
;
1175 buffer
->parent
= NULL
;
1176 buffer
->cmdline
= true;
1178 namelist
= nametail
= buffer
;
1180 else if (change_dir
)
1181 addname (0, change_dir
, false, NULL
);
1185 /* Non sorted names -- read them all in. */
1190 int change_dir0
= change_dir
;
1191 while ((ep
= name_next_elt (0)) && ep
->type
== NELT_CHDIR
)
1192 change_dir
= chdir_arg (xstrdup (ep
->v
.name
));
1195 addname (ep
->v
.name
, change_dir
, true, NULL
);
1198 if (change_dir
!= change_dir0
)
1199 addname (NULL
, change_dir
, false, NULL
);
1206 /* Add a name to the namelist. */
1208 addname (char const *string
, int change_dir
, bool cmdline
, struct name
*parent
)
1210 struct name
*name
= make_name (string
);
1212 name
->prev
= nametail
;
1214 name
->found_count
= 0;
1215 name
->matching_flags
= INCLUDE_OPTIONS
;
1216 name
->change_dir
= change_dir
;
1217 name
->directory
= NULL
;
1218 name
->parent
= parent
;
1219 name
->cmdline
= cmdline
;
1222 nametail
->next
= name
;
1230 add_starting_file (char const *file_name
)
1232 struct name
*name
= make_name (file_name
);
1234 if (starting_file_option
)
1236 struct name
*head
= namelist
;
1242 name
->next
= namelist
;
1245 nametail
= namelist
;
1247 name
->found_count
= 0;
1248 name
->matching_flags
= INCLUDE_OPTIONS
;
1249 name
->change_dir
= 0;
1250 name
->directory
= NULL
;
1251 name
->parent
= NULL
;
1252 name
->cmdline
= true;
1254 starting_file_option
= true;
1257 /* Find a match for FILE_NAME (whose string length is LENGTH) in the name
1259 static struct name
*
1260 namelist_match (char const *file_name
, size_t length
)
1264 for (p
= namelist
; p
; p
= p
->next
)
1267 && exclude_fnmatch (p
->name
, file_name
, p
->matching_flags
))
1275 remname (struct name
*name
)
1279 if ((p
= name
->prev
) != NULL
)
1280 p
->next
= name
->next
;
1282 namelist
= name
->next
;
1284 if ((p
= name
->next
) != NULL
)
1285 p
->prev
= name
->prev
;
1287 nametail
= name
->prev
;
1290 /* Return true if and only if name FILE_NAME (from an archive) matches any
1291 name from the namelist. */
1293 name_match (const char *file_name
)
1295 size_t length
= strlen (file_name
);
1299 struct name
*cursor
= namelist
;
1304 if (cursor
->name
[0] == 0)
1306 chdir_do (cursor
->change_dir
);
1312 cursor
= namelist_match (file_name
, length
);
1313 if (starting_file_option
)
1315 /* If starting_file_option is set, the head of the list is the name
1316 of the member to start extraction from. Skip the match unless it
1318 if (cursor
== namelist
)
1319 starting_file_option
= false;
1325 if (!(ISSLASH (file_name
[cursor
->length
]) && recursion_option
)
1326 || cursor
->found_count
== 0)
1327 cursor
->found_count
++; /* remember it matched */
1328 chdir_do (cursor
->change_dir
);
1329 /* We got a match. */
1330 return ISFOUND (cursor
);
1333 /* Filename from archive not found in namelist. If we have the whole
1334 namelist here, just return 0. Otherwise, read the next name in and
1335 compare it. If this was the last name, namelist->found_count will
1336 remain on. If not, we loop to compare the newly read name. */
1338 if (same_order_option
&& namelist
->found_count
)
1340 name_gather (); /* read one more */
1341 if (namelist
->found_count
)
1349 /* Returns true if all names from the namelist were processed.
1350 P is the stat_info of the most recently processed entry.
1351 The decision is postponed until the next entry is read if:
1353 1) P ended with a slash (i.e. it was a directory)
1354 2) P matches any entry from the namelist *and* represents a subdirectory
1355 or a file lying under this entry (in the terms of directory structure).
1357 This is necessary to handle contents of directories. */
1359 all_names_found (struct tar_stat_info
*p
)
1361 struct name
const *cursor
;
1364 if (!p
->file_name
|| occurrence_option
== 0 || p
->had_trailing_slash
)
1366 len
= strlen (p
->file_name
);
1367 for (cursor
= namelist
; cursor
; cursor
= cursor
->next
)
1369 if ((cursor
->name
[0] && !WASFOUND (cursor
))
1370 || (len
>= cursor
->length
&& ISSLASH (p
->file_name
[cursor
->length
])))
1377 regex_usage_warning (const char *name
)
1379 static int warned_once
= 0;
1381 /* Warn about implicit use of the wildcards in command line arguments.
1382 (Default for tar prior to 1.15.91, but changed afterwards) */
1383 if (wildcards
== default_wildcards
1384 && fnmatch_pattern_has_wildcards (name
, 0))
1388 _("Pattern matching characters used in file names")));
1390 _("Use --wildcards to enable pattern matching,"
1391 " or --no-wildcards to suppress this warning")));
1396 /* Print the names of things in the namelist that were not matched. */
1398 names_notfound (void)
1400 struct name
const *cursor
;
1402 for (cursor
= namelist
; cursor
; cursor
= cursor
->next
)
1403 if (!WASFOUND (cursor
) && cursor
->name
[0])
1405 regex_usage_warning (cursor
->name
);
1407 (cursor
->found_count
== 0) ?
1408 _("%s: Not found in archive") :
1409 _("%s: Required occurrence not found in archive"),
1410 quotearg_colon (cursor
->name
)));
1413 /* Don't bother freeing the name list; we're about to exit. */
1417 if (same_order_option
)
1421 while ((name
= name_next (1)) != NULL
)
1423 regex_usage_warning (name
);
1424 ERROR ((0, 0, _("%s: Not found in archive"),
1425 quotearg_colon (name
)));
1431 label_notfound (void)
1433 struct name
const *cursor
;
1438 for (cursor
= namelist
; cursor
; cursor
= cursor
->next
)
1439 if (WASFOUND (cursor
))
1443 error (0, 0, _("Archive label mismatch"));
1444 set_exit_status (TAREXIT_DIFFERS
);
1446 for (cursor
= namelist
; cursor
; cursor
= cursor
->next
)
1448 if (regex_usage_warning (cursor
->name
))
1452 /* Don't bother freeing the name list; we're about to exit. */
1456 if (same_order_option
)
1460 while ((name
= name_next (1)) != NULL
1461 && regex_usage_warning (name
) == 0)
1466 /* Sorting name lists. */
1468 /* Sort *singly* linked LIST of names, of given LENGTH, using COMPARE
1469 to order names. Return the sorted list. Note that after calling
1470 this function, the 'prev' links in list elements are messed up.
1472 Apart from the type 'struct name' and the definition of SUCCESSOR,
1473 this is a generic list-sorting function, but it's too painful to
1474 make it both generic and portable
1477 static struct name
*
1478 merge_sort_sll (struct name
*list
, int length
,
1479 int (*compare
) (struct name
const*, struct name
const*))
1481 struct name
*first_list
;
1482 struct name
*second_list
;
1485 struct name
*result
;
1486 struct name
**merge_point
;
1487 struct name
*cursor
;
1490 # define SUCCESSOR(name) ((name)->next)
1497 if ((*compare
) (list
, SUCCESSOR (list
)) > 0)
1499 result
= SUCCESSOR (list
);
1500 SUCCESSOR (result
) = list
;
1501 SUCCESSOR (list
) = 0;
1508 first_length
= (length
+ 1) / 2;
1509 second_length
= length
/ 2;
1510 for (cursor
= list
, counter
= first_length
- 1;
1512 cursor
= SUCCESSOR (cursor
), counter
--)
1514 second_list
= SUCCESSOR (cursor
);
1515 SUCCESSOR (cursor
) = 0;
1517 first_list
= merge_sort_sll (first_list
, first_length
, compare
);
1518 second_list
= merge_sort_sll (second_list
, second_length
, compare
);
1520 merge_point
= &result
;
1521 while (first_list
&& second_list
)
1522 if ((*compare
) (first_list
, second_list
) < 0)
1524 cursor
= SUCCESSOR (first_list
);
1525 *merge_point
= first_list
;
1526 merge_point
= &SUCCESSOR (first_list
);
1527 first_list
= cursor
;
1531 cursor
= SUCCESSOR (second_list
);
1532 *merge_point
= second_list
;
1533 merge_point
= &SUCCESSOR (second_list
);
1534 second_list
= cursor
;
1537 *merge_point
= first_list
;
1539 *merge_point
= second_list
;
1546 /* Sort doubly linked LIST of names, of given LENGTH, using COMPARE
1547 to order names. Return the sorted list. */
1548 static struct name
*
1549 merge_sort (struct name
*list
, int length
,
1550 int (*compare
) (struct name
const*, struct name
const*))
1552 struct name
*head
, *p
, *prev
;
1553 head
= merge_sort_sll (list
, length
, compare
);
1554 /* Fixup prev pointers */
1555 for (prev
= NULL
, p
= head
; p
; prev
= p
, p
= p
->next
)
1560 /* A comparison function for sorting names. Put found names last;
1561 break ties by string comparison. */
1564 compare_names_found (struct name
const *n1
, struct name
const *n2
)
1566 int found_diff
= WASFOUND (n2
) - WASFOUND (n1
);
1567 return found_diff
? found_diff
: strcmp (n1
->name
, n2
->name
);
1570 /* Simple comparison by names. */
1572 compare_names (struct name
const *n1
, struct name
const *n2
)
1574 return strcmp (n1
->name
, n2
->name
);
1578 /* Add all the dirs under ST to the namelist NAME, descending the
1579 directory hierarchy recursively. */
1582 add_hierarchy_to_namelist (struct tar_stat_info
*st
, struct name
*name
)
1586 name
->directory
= scan_directory (st
);
1587 buffer
= directory_contents (name
->directory
);
1590 struct name
*child_head
= NULL
, *child_tail
= NULL
;
1591 size_t name_length
= name
->length
;
1592 size_t allocated_length
= (name_length
>= NAME_FIELD_SIZE
1593 ? name_length
+ NAME_FIELD_SIZE
1594 : NAME_FIELD_SIZE
) + 2;
1595 char *namebuf
= xmalloc (allocated_length
);
1597 size_t string_length
;
1598 int change_dir
= name
->change_dir
;
1600 strcpy (namebuf
, name
->name
);
1601 if (! ISSLASH (namebuf
[name_length
- 1]))
1603 namebuf
[name_length
++] = '/';
1604 namebuf
[name_length
] = '\0';
1607 for (string
= buffer
; *string
; string
+= string_length
+ 1)
1609 string_length
= strlen (string
);
1613 struct tar_stat_info subdir
;
1616 /* need to have at least string_length bytes above the
1617 name_length, this includes the trailing null character */
1618 while (allocated_length
< name_length
+ string_length
)
1619 namebuf
= x2realloc (namebuf
, &allocated_length
);
1620 strcpy (namebuf
+ name_length
, string
+ 1);
1621 np
= addname (namebuf
, change_dir
, false, name
);
1625 child_tail
->sibling
= np
;
1628 tar_stat_init (&subdir
);
1636 subfd
= subfile_open (st
, string
+ 1,
1637 open_read_flags
| O_DIRECTORY
);
1639 open_diag (namebuf
);
1643 if (fstat (subfd
, &subdir
.stat
) != 0)
1644 stat_diag (namebuf
);
1645 else if (! (O_DIRECTORY
|| S_ISDIR (subdir
.stat
.st_mode
)))
1648 open_diag (namebuf
);
1652 subdir
.orig_file_name
= xstrdup (namebuf
);
1653 add_hierarchy_to_namelist (&subdir
, np
);
1654 restore_parent_fd (&subdir
);
1658 tar_stat_destroy (&subdir
);
1663 name
->child
= child_head
;
1667 /* Auxiliary functions for hashed table of struct name's. */
1670 name_hash (void const *entry
, size_t n_buckets
)
1672 struct name
const *name
= entry
;
1673 return hash_string (name
->caname
, n_buckets
);
1676 /* Compare two directories for equality of their names. */
1678 name_compare (void const *entry1
, void const *entry2
)
1680 struct name
const *name1
= entry1
;
1681 struct name
const *name2
= entry2
;
1682 return strcmp (name1
->caname
, name2
->caname
) == 0;
1686 /* Rebase 'name' member of CHILD and all its siblings to
1689 rebase_child_list (struct name
*child
, struct name
*parent
)
1691 size_t old_prefix_len
= child
->parent
->length
;
1692 size_t new_prefix_len
= parent
->length
;
1693 char *new_prefix
= parent
->name
;
1695 for (; child
; child
= child
->sibling
)
1697 size_t size
= child
->length
- old_prefix_len
+ new_prefix_len
;
1698 char *newp
= xmalloc (size
+ 1);
1699 strcpy (newp
, new_prefix
);
1700 strcat (newp
, child
->name
+ old_prefix_len
);
1703 child
->length
= size
;
1705 rebase_directory (child
->directory
,
1706 child
->parent
->name
, old_prefix_len
,
1707 new_prefix
, new_prefix_len
);
1711 /* Collect all the names from argv[] (or whatever), expand them into a
1712 directory tree, and sort them. This gets only subdirectories, not
1716 collect_and_sort_names (void)
1719 struct name
*next_name
, *prev_name
= NULL
;
1721 Hash_table
*nametab
;
1726 addname (".", 0, false, NULL
);
1728 if (listed_incremental_option
)
1730 switch (chdir_count ())
1736 if (namelist
->change_dir
== 0)
1738 _("Using -C option inside file list is not "
1739 "allowed with --listed-incremental")));
1744 _("Only one -C option is allowed with "
1745 "--listed-incremental")));
1748 read_directory_file ();
1752 for (name
= namelist
; name
; name
= name
->next
, num_names
++)
1754 struct tar_stat_info st
;
1756 if (name
->found_count
|| name
->directory
)
1758 if (name
->matching_flags
& EXCLUDE_WILDCARDS
)
1759 /* NOTE: EXCLUDE_ANCHORED is not relevant here */
1760 /* FIXME: just skip regexps for now */
1762 chdir_do (name
->change_dir
);
1764 if (name
->name
[0] == 0)
1767 tar_stat_init (&st
);
1769 if (deref_stat (name
->name
, &st
.stat
) != 0)
1771 stat_diag (name
->name
);
1774 if (S_ISDIR (st
.stat
.st_mode
))
1776 int dir_fd
= openat (chdir_fd
, name
->name
,
1777 open_read_flags
| O_DIRECTORY
);
1779 open_diag (name
->name
);
1783 if (fstat (dir_fd
, &st
.stat
) != 0)
1784 stat_diag (name
->name
);
1785 else if (O_DIRECTORY
|| S_ISDIR (st
.stat
.st_mode
))
1787 st
.orig_file_name
= xstrdup (name
->name
);
1788 name
->found_count
++;
1789 add_hierarchy_to_namelist (&st
, name
);
1794 open_diag (name
->name
);
1799 tar_stat_destroy (&st
);
1802 namelist
= merge_sort (namelist
, num_names
, compare_names
);
1805 nametab
= hash_initialize (0, 0, name_hash
, name_compare
, NULL
);
1806 for (name
= namelist
; name
; name
= next_name
)
1808 next_name
= name
->next
;
1809 name
->caname
= normalize_filename (name
->change_dir
, name
->name
);
1812 struct name
*p
= hash_lookup (nametab
, name
);
1815 /* Keep the one listed in the command line */
1819 rebase_child_list (p
->child
, name
);
1820 hash_delete (nametab
, name
);
1821 /* FIXME: remove_directory (p->caname); ? */
1829 rebase_child_list (name
->child
, p
);
1830 /* FIXME: remove_directory (name->caname); ? */
1837 name
->found_count
= 0;
1838 if (!hash_insert (nametab
, name
))
1843 nametail
= prev_name
;
1844 hash_free (nametab
);
1846 namelist
= merge_sort (namelist
, num_names
, compare_names_found
);
1848 if (listed_incremental_option
)
1850 for (name
= namelist
; name
&& name
->name
[0] == 0; name
= name
->next
)
1853 append_incremental_renames (name
->directory
);
1857 /* This is like name_match, except that
1858 1. It returns a pointer to the name it matched, and doesn't set FOUND
1859 in structure. The caller will have to do that if it wants to.
1860 2. If the namelist is empty, it returns null, unlike name_match, which
1863 name_scan (const char *file_name
)
1865 size_t length
= strlen (file_name
);
1869 struct name
*cursor
= namelist_match (file_name
, length
);
1873 /* Filename from archive not found in namelist. If we have the whole
1874 namelist here, just return 0. Otherwise, read the next name in and
1875 compare it. If this was the last name, namelist->found_count will
1876 remain on. If not, we loop to compare the newly read name. */
1878 if (same_order_option
&& namelist
&& namelist
->found_count
)
1880 name_gather (); /* read one more */
1881 if (namelist
->found_count
)
1889 /* This returns a name from the namelist which doesn't have ->found
1890 set. It sets ->found before returning, so successive calls will
1891 find and return all the non-found names in the namelist. */
1892 struct name
*gnu_list_name
;
1895 name_from_list (void)
1898 gnu_list_name
= namelist
;
1899 while (gnu_list_name
1900 && (gnu_list_name
->found_count
|| gnu_list_name
->name
[0] == 0))
1901 gnu_list_name
= gnu_list_name
->next
;
1904 gnu_list_name
->found_count
++;
1905 chdir_do (gnu_list_name
->change_dir
);
1906 return gnu_list_name
;
1912 blank_name_list (void)
1917 for (name
= namelist
; name
; name
= name
->next
)
1918 name
->found_count
= 0;
1921 /* Yield a newly allocated file name consisting of DIR_NAME concatenated to
1922 NAME, with an intervening slash if DIR_NAME does not already end in one. */
1924 make_file_name (const char *directory_name
, const char *name
)
1926 size_t dirlen
= strlen (directory_name
);
1927 size_t namelen
= strlen (name
) + 1;
1928 int slash
= dirlen
&& ! ISSLASH (directory_name
[dirlen
- 1]);
1929 char *buffer
= xmalloc (dirlen
+ slash
+ namelen
);
1930 memcpy (buffer
, directory_name
, dirlen
);
1931 buffer
[dirlen
] = '/';
1932 memcpy (buffer
+ dirlen
+ slash
, name
, namelen
);
1938 /* Return the size of the prefix of FILE_NAME that is removed after
1939 stripping NUM leading file name components. NUM must be
1943 stripped_prefix_len (char const *file_name
, size_t num
)
1945 char const *p
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
1946 while (ISSLASH (*p
))
1950 bool slash
= ISSLASH (*p
);
1955 return p
- file_name
;
1956 while (ISSLASH (*p
))
1963 /* Return nonzero if NAME contains ".." as a file name component. */
1965 contains_dot_dot (char const *name
)
1967 char const *p
= name
+ FILE_SYSTEM_PREFIX_LEN (name
);
1971 if (p
[0] == '.' && p
[1] == '.' && (ISSLASH (p
[2]) || !p
[2]))
1974 while (! ISSLASH (*p
))