1 /* Various processing of names.
3 Copyright 1988-2023 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
,
73 static struct argp_option names_options
[] = {
75 N_("Local file name selection:"), GRH_LOCAL
},
77 {"add-file", ADD_FILE_OPTION
, N_("FILE"), 0,
78 N_("add given FILE to the archive (useful if its name starts with a dash)"), GRID_LOCAL
},
79 {"directory", 'C', N_("DIR"), 0,
80 N_("change to directory DIR"), GRID_LOCAL
},
81 {"files-from", 'T', N_("FILE"), 0,
82 N_("get names to extract or create from FILE"), GRID_LOCAL
},
83 {"null", NULL_OPTION
, 0, 0,
84 N_("-T reads null-terminated names; implies --verbatim-files-from"),
86 {"no-null", NO_NULL_OPTION
, 0, 0,
87 N_("disable the effect of the previous --null option"), GRID_LOCAL
},
88 {"unquote", UNQUOTE_OPTION
, 0, 0,
89 N_("unquote input file or member names (default)"), GRID_LOCAL
},
90 {"no-unquote", NO_UNQUOTE_OPTION
, 0, 0,
91 N_("do not unquote input file or member names"), GRID_LOCAL
},
92 {"verbatim-files-from", VERBATIM_FILES_FROM_OPTION
, 0, 0,
93 N_("-T reads file names verbatim (no escape or option handling)"), GRID_LOCAL
},
94 {"no-verbatim-files-from", NO_VERBATIM_FILES_FROM_OPTION
, 0, 0,
95 N_("-T treats file names starting with dash as options (default)"),
97 {"exclude", EXCLUDE_OPTION
, N_("PATTERN"), 0,
98 N_("exclude files, given as a PATTERN"), GRID_LOCAL
},
99 {"exclude-from", 'X', N_("FILE"), 0,
100 N_("exclude patterns listed in FILE"), GRID_LOCAL
},
101 {"exclude-caches", EXCLUDE_CACHES_OPTION
, 0, 0,
102 N_("exclude contents of directories containing CACHEDIR.TAG, "
103 "except for the tag file itself"), GRID_LOCAL
},
104 {"exclude-caches-under", EXCLUDE_CACHES_UNDER_OPTION
, 0, 0,
105 N_("exclude everything under directories containing CACHEDIR.TAG"),
107 {"exclude-caches-all", EXCLUDE_CACHES_ALL_OPTION
, 0, 0,
108 N_("exclude directories containing CACHEDIR.TAG"), GRID_LOCAL
},
109 {"exclude-tag", EXCLUDE_TAG_OPTION
, N_("FILE"), 0,
110 N_("exclude contents of directories containing FILE, except"
111 " for FILE itself"), GRID_LOCAL
},
112 {"exclude-ignore", EXCLUDE_IGNORE_OPTION
, N_("FILE"), 0,
113 N_("read exclude patterns for each directory from FILE, if it exists"),
115 {"exclude-ignore-recursive", EXCLUDE_IGNORE_RECURSIVE_OPTION
, N_("FILE"), 0,
116 N_("read exclude patterns for each directory and its subdirectories "
117 "from FILE, if it exists"), GRID_LOCAL
},
118 {"exclude-tag-under", EXCLUDE_TAG_UNDER_OPTION
, N_("FILE"), 0,
119 N_("exclude everything under directories containing FILE"), GRID_LOCAL
},
120 {"exclude-tag-all", EXCLUDE_TAG_ALL_OPTION
, N_("FILE"), 0,
121 N_("exclude directories containing FILE"), GRID_LOCAL
},
122 {"exclude-vcs", EXCLUDE_VCS_OPTION
, NULL
, 0,
123 N_("exclude version control system directories"), GRID_LOCAL
},
124 {"exclude-vcs-ignores", EXCLUDE_VCS_IGNORES_OPTION
, NULL
, 0,
125 N_("read exclude patterns from the VCS ignore files"), GRID_LOCAL
},
126 {"exclude-backups", EXCLUDE_BACKUPS_OPTION
, NULL
, 0,
127 N_("exclude backup and lock files"), GRID_LOCAL
},
128 {"recursion", RECURSION_OPTION
, 0, 0,
129 N_("recurse into directories (default)"), GRID_LOCAL
},
130 {"no-recursion", NO_RECURSION_OPTION
, 0, 0,
131 N_("avoid descending automatically in directories"), GRID_LOCAL
},
134 N_("File name matching options (affect both exclude and include patterns):"),
136 {"anchored", ANCHORED_OPTION
, 0, 0,
137 N_("patterns match file name start"), GRID_MATCH
},
138 {"no-anchored", NO_ANCHORED_OPTION
, 0, 0,
139 N_("patterns match after any '/' (default for exclusion)"), GRID_MATCH
},
140 {"ignore-case", IGNORE_CASE_OPTION
, 0, 0,
141 N_("ignore case"), GRID_MATCH
},
142 {"no-ignore-case", NO_IGNORE_CASE_OPTION
, 0, 0,
143 N_("case sensitive matching (default)"), GRID_MATCH
},
144 {"wildcards", WILDCARDS_OPTION
, 0, 0,
145 N_("use wildcards (default for exclusion)"), GRID_MATCH
},
146 {"no-wildcards", NO_WILDCARDS_OPTION
, 0, 0,
147 N_("verbatim string matching"), GRID_MATCH
},
148 {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION
, 0, 0,
149 N_("wildcards match '/' (default for exclusion)"), GRID_MATCH
},
150 {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION
, 0, 0,
151 N_("wildcards do not match '/'"), GRID_MATCH
},
156 static struct argp_option
const *
157 file_selection_option (int key
)
159 struct argp_option
*p
;
161 for (p
= names_options
;
162 !(p
->name
== NULL
&& p
->key
== 0 && p
->doc
== NULL
); p
++)
169 file_selection_option_name (int key
)
171 struct argp_option
const *opt
= file_selection_option (key
);
172 return opt
? opt
->name
: NULL
;
176 is_file_selection_option (int key
)
178 return file_selection_option (key
) != NULL
;
181 /* Either NL or NUL, as decided by the --null option. */
182 static char filename_terminator
= '\n';
183 /* Treat file names read from -T input verbatim */
184 static bool verbatim_files_from_option
;
187 names_parse_opt (int key
, char *arg
, struct argp_state
*state
)
199 case ADD_FILE_OPTION
:
205 struct tar_args
*args
= state
->input
;
206 if (args
->loc
->source
== OPTS_FILE
)
208 error (0, 0, _("%s:%lu: unrecognized option"), args
->loc
->name
,
209 (unsigned long) args
->loc
->line
);
210 set_exit_status (TAREXIT_FAILURE
);
212 return ARGP_ERR_UNKNOWN
;
216 if (is_file_selection_option (key
))
217 name_add_option (key
, arg
);
219 return ARGP_ERR_UNKNOWN
;
225 /* Wildcard matching settings */
228 default_wildcards
, /* For exclusion == enable_wildcards,
229 for inclusion == disable_wildcards */
234 static enum wildcards wildcards
= default_wildcards
;
235 /* Wildcard settings (--wildcards/--no-wildcards) */
236 static int matching_flags
= 0;
237 /* exclude_fnmatch options */
238 static int include_anchored
= EXCLUDE_ANCHORED
;
239 /* Pattern anchoring options used for file inclusion */
241 #define EXCLUDE_OPTIONS \
242 (((wildcards != disable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
246 #define INCLUDE_OPTIONS \
247 (((wildcards == enable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
252 static char const * const vcs_file_table
[] = {
286 static char const * const backup_file_table
[] = {
294 add_exclude_array (char const * const * fv
, int opts
)
298 for (i
= 0; fv
[i
]; i
++)
299 add_exclude (excluded
, fv
[i
], opts
);
303 handle_file_selection_option (int key
, const char *arg
)
307 case EXCLUDE_BACKUPS_OPTION
:
308 add_exclude_array (backup_file_table
, EXCLUDE_WILDCARDS
);
312 add_exclude (excluded
, arg
, EXCLUDE_OPTIONS
);
315 case EXCLUDE_CACHES_OPTION
:
316 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_contents
,
320 case EXCLUDE_CACHES_UNDER_OPTION
:
321 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_under
,
325 case EXCLUDE_CACHES_ALL_OPTION
:
326 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_all
,
330 case EXCLUDE_IGNORE_OPTION
:
331 excfile_add (arg
, EXCL_NON_RECURSIVE
);
334 case EXCLUDE_IGNORE_RECURSIVE_OPTION
:
335 excfile_add (arg
, EXCL_RECURSIVE
);
338 case EXCLUDE_TAG_OPTION
:
339 add_exclusion_tag (arg
, exclusion_tag_contents
, NULL
);
342 case EXCLUDE_TAG_UNDER_OPTION
:
343 add_exclusion_tag (arg
, exclusion_tag_under
, NULL
);
346 case EXCLUDE_TAG_ALL_OPTION
:
347 add_exclusion_tag (arg
, exclusion_tag_all
, NULL
);
350 case EXCLUDE_VCS_OPTION
:
351 add_exclude_array (vcs_file_table
, FNM_LEADING_DIR
);
354 case EXCLUDE_VCS_IGNORES_OPTION
:
355 exclude_vcs_ignores ();
358 case RECURSION_OPTION
:
359 recursion_option
= FNM_LEADING_DIR
;
362 case NO_RECURSION_OPTION
:
363 recursion_option
= 0;
367 unquote_option
= true;
370 case NO_UNQUOTE_OPTION
:
371 unquote_option
= false;
375 filename_terminator
= '\0';
376 verbatim_files_from_option
= true;
380 filename_terminator
= '\n';
381 verbatim_files_from_option
= false;
385 if (add_exclude_file (add_exclude
, excluded
, arg
, EXCLUDE_OPTIONS
, '\n')
389 FATAL_ERROR ((0, e
, "%s", quotearg_colon (arg
)));
393 case ANCHORED_OPTION
:
394 matching_flags
|= EXCLUDE_ANCHORED
;
397 case NO_ANCHORED_OPTION
:
398 include_anchored
= 0; /* Clear the default for command line args */
399 matching_flags
&= ~ EXCLUDE_ANCHORED
;
402 case IGNORE_CASE_OPTION
:
403 matching_flags
|= FNM_CASEFOLD
;
406 case NO_IGNORE_CASE_OPTION
:
407 matching_flags
&= ~ FNM_CASEFOLD
;
410 case WILDCARDS_OPTION
:
411 wildcards
= enable_wildcards
;
414 case NO_WILDCARDS_OPTION
:
415 wildcards
= disable_wildcards
;
418 case WILDCARDS_MATCH_SLASH_OPTION
:
419 matching_flags
&= ~ FNM_FILE_NAME
;
422 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
423 matching_flags
|= FNM_FILE_NAME
;
426 case VERBATIM_FILES_FROM_OPTION
:
427 verbatim_files_from_option
= true;
430 case NO_VERBATIM_FILES_FROM_OPTION
:
431 verbatim_files_from_option
= false;
435 FATAL_ERROR ((0, 0, "unhandled positional option %d", key
));
439 struct argp names_argp
= {
450 /* User and group names. */
452 /* Make sure you link with the proper libraries if you are running the
453 Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
454 This code should also be modified for non-UNIX systems to do something
457 static char *cached_uname
;
458 static char *cached_gname
;
460 static uid_t cached_uid
; /* valid only if cached_uname is not empty */
461 static gid_t cached_gid
; /* valid only if cached_gname is not empty */
463 /* These variables are valid only if nonempty. */
464 static char *cached_no_such_uname
;
465 static char *cached_no_such_gname
;
467 /* These variables are valid only if nonzero. It's not worth optimizing
468 the case for weird systems where 0 is not a valid uid or gid. */
469 static uid_t cached_no_such_uid
;
470 static gid_t cached_no_such_gid
;
472 /* Given UID, find the corresponding UNAME. */
474 uid_to_uname (uid_t uid
, char **uname
)
476 struct passwd
*passwd
;
478 if (uid
!= 0 && uid
== cached_no_such_uid
)
480 *uname
= xstrdup ("");
484 if (!cached_uname
|| uid
!= cached_uid
)
486 passwd
= getpwuid (uid
);
490 assign_string (&cached_uname
, passwd
->pw_name
);
494 cached_no_such_uid
= uid
;
495 *uname
= xstrdup ("");
499 *uname
= xstrdup (cached_uname
);
502 /* Given GID, find the corresponding GNAME. */
504 gid_to_gname (gid_t gid
, char **gname
)
508 if (gid
!= 0 && gid
== cached_no_such_gid
)
510 *gname
= xstrdup ("");
514 if (!cached_gname
|| gid
!= cached_gid
)
516 group
= getgrgid (gid
);
520 assign_string (&cached_gname
, group
->gr_name
);
524 cached_no_such_gid
= gid
;
525 *gname
= xstrdup ("");
529 *gname
= xstrdup (cached_gname
);
532 /* Given UNAME, set the corresponding UID and return 1, or else, return 0. */
534 uname_to_uid (char const *uname
, uid_t
*uidp
)
536 struct passwd
*passwd
;
538 if (cached_no_such_uname
539 && strcmp (uname
, cached_no_such_uname
) == 0)
543 || uname
[0] != cached_uname
[0]
544 || strcmp (uname
, cached_uname
) != 0)
546 passwd
= getpwnam (uname
);
549 cached_uid
= passwd
->pw_uid
;
550 assign_string (&cached_uname
, passwd
->pw_name
);
554 assign_string (&cached_no_such_uname
, uname
);
562 /* Given GNAME, set the corresponding GID and return 1, or else, return 0. */
564 gname_to_gid (char const *gname
, gid_t
*gidp
)
568 if (cached_no_such_gname
569 && strcmp (gname
, cached_no_such_gname
) == 0)
573 || gname
[0] != cached_gname
[0]
574 || strcmp (gname
, cached_gname
) != 0)
576 group
= getgrnam (gname
);
579 cached_gid
= group
->gr_gid
;
580 assign_string (&cached_gname
, gname
);
584 assign_string (&cached_no_such_gname
, gname
);
594 make_name (const char *file_name
)
596 struct name
*p
= xzalloc (sizeof (*p
));
599 p
->name
= xstrdup (file_name
);
600 p
->length
= strlen (p
->name
);
605 free_name (struct name
*p
)
616 /* Names from the command call. */
618 static struct name
*namelist
; /* first name in list, if any */
619 static struct name
*nametail
; /* end of name list */
621 /* File name arguments are processed in two stages: first a
622 name element list (see below) is filled, then the names from it
623 are moved into the namelist.
625 This awkward process is needed only to implement --same-order option,
626 which is meant to help process large archives on machines with
627 limited memory. With this option on, namelist contains at most one
628 entry, which diminishes the memory consumption.
630 However, I very much doubt if we still need this -- Sergey */
632 /* A name_list element contains entries of three types: */
636 NELT_NAME
, /* File name */
637 NELT_CHDIR
, /* Change directory request */
638 NELT_FILE
, /* Read file names from that file */
639 NELT_NOOP
, /* No operation */
640 NELT_OPTION
/* Filename-selection option */
643 struct name_elt
/* A name_array element. */
645 struct name_elt
*next
, *prev
;
646 enum nelt_type type
; /* Element type, see NELT_* constants above */
649 const char *name
; /* File or directory name */
650 struct /* File, if type == NELT_FILE */
652 const char *name
;/* File name */
653 size_t line
; /* Input line number */
654 int term
; /* File name terminator in the list */
655 bool verbatim
; /* Verbatim handling of file names: no white-space
656 trimming, no option processing */
663 } opt
; /* NELT_OPTION */
667 static struct name_elt
*name_head
; /* store a list of names */
669 /* how many of the entries are file names? */
670 enum files_count filename_args
= FILES_NONE
;
672 static struct name_elt
*
673 name_elt_alloc (void)
675 struct name_elt
*elt
;
677 elt
= xmalloc (sizeof (*elt
));
681 name_head
->prev
= name_head
->next
= NULL
;
682 name_head
->type
= NELT_NOOP
;
683 elt
= xmalloc (sizeof (*elt
));
686 elt
->prev
= name_head
->prev
;
688 name_head
->prev
->next
= elt
;
689 elt
->next
= name_head
;
690 name_head
->prev
= elt
;
695 name_list_adjust (void)
698 while (name_head
->prev
)
699 name_head
= name_head
->prev
;
702 /* For error-reporting purposes, keep a doubly-linked list of unconsumed file
703 selection options. The option is deemed unconsumed unless followed by one
704 or more file/member name arguments. When archive creation is requested,
705 each file selection option encountered is pushed into the list. The list
706 is cleared upon encountering a file name argument.
708 If the list is not empty when all arguments have been processed, an error
709 message is issued reporting the options that had no effect.
711 For simplicity, only a tail pointer of the list is maintained.
714 struct name_elt
*unconsumed_option_tail
;
716 /* Push an option to the list */
718 unconsumed_option_push (struct name_elt
*elt
)
721 elt
->prev
= unconsumed_option_tail
;
722 if (unconsumed_option_tail
)
723 unconsumed_option_tail
->next
= elt
;
724 unconsumed_option_tail
= elt
;
727 /* Clear the unconsumed option list */
729 unconsumed_option_free (void)
731 while (unconsumed_option_tail
)
733 struct name_elt
*elt
= unconsumed_option_tail
;
734 unconsumed_option_tail
= unconsumed_option_tail
->prev
;
739 /* Report any options that have not been consumed */
741 unconsumed_option_report (void)
743 if (unconsumed_option_tail
)
745 struct name_elt
*elt
;
747 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.")));
749 elt
= unconsumed_option_tail
;
758 ERROR ((0, 0, _("-C %s has no effect"), quote (elt
->v
.name
)));
763 ERROR ((0, 0, _("--%s %s has no effect"),
764 file_selection_option_name (elt
->v
.opt
.option
),
765 quote (elt
->v
.opt
.arg
)));
767 ERROR ((0, 0, _("--%s has no effect"),
768 file_selection_option_name (elt
->v
.opt
.option
)));
777 unconsumed_option_free ();
782 name_list_advance (void)
784 struct name_elt
*elt
= name_head
;
785 name_head
= elt
->next
;
787 name_head
->prev
= NULL
;
788 if (elt
->type
== NELT_OPTION
|| elt
->type
== NELT_CHDIR
)
790 if (subcommand_option
== CREATE_SUBCOMMAND
791 || subcommand_option
== UPDATE_SUBCOMMAND
)
792 unconsumed_option_push (elt
);
796 if (elt
->type
!= NELT_NOOP
)
797 unconsumed_option_free ();
802 /* Add to name_array the file NAME with fnmatch options MATFLAGS */
804 name_add_name (const char *name
)
806 struct name_elt
*ep
= name_elt_alloc ();
808 ep
->type
= NELT_NAME
;
811 switch (filename_args
)
814 filename_args
= FILES_ONE
;
818 filename_args
= FILES_MANY
;
827 name_add_option (int option
, const char *arg
)
829 struct name_elt
*elt
= name_elt_alloc ();
830 elt
->type
= NELT_OPTION
;
831 elt
->v
.opt
.option
= option
;
832 elt
->v
.opt
.arg
= arg
;
835 /* Add to name_array a chdir request for the directory NAME */
837 name_add_dir (const char *name
)
839 struct name_elt
*ep
= name_elt_alloc ();
840 ep
->type
= NELT_CHDIR
;
845 name_add_file (const char *name
)
847 struct name_elt
*ep
= name_elt_alloc ();
849 ep
->type
= NELT_FILE
;
850 ep
->v
.file
.name
= name
;
852 ep
->v
.file
.fp
= NULL
;
854 /* We don't know beforehand how many files are listed.
855 Assume more than one. */
856 filename_args
= FILES_MANY
;
859 /* Names from external name file. */
861 static char *name_buffer
; /* buffer to hold the current file name */
862 static size_t name_buffer_length
; /* allocated length of name_buffer */
864 /* Set up to gather file names for tar. They can either come from a
865 file or were saved from decoding arguments. */
869 name_buffer
= xmalloc (NAME_FIELD_SIZE
+ 2);
870 name_buffer_length
= NAME_FIELD_SIZE
;
880 /* Prevent recursive inclusion of the same file */
883 struct file_id_list
*next
;
886 const char *from_file
;
889 static struct file_id_list
*file_id_list
;
891 /* Return the name of the file from which the file names and options
895 file_list_name (void)
897 struct name_elt
*elt
;
899 for (elt
= name_head
; elt
; elt
= elt
->next
)
900 if (elt
->type
== NELT_FILE
&& elt
->v
.file
.fp
)
901 return elt
->v
.file
.name
;
902 return _("command line");
906 add_file_id (const char *filename
)
908 struct file_id_list
*p
;
910 const char *reading_from
;
912 if (stat (filename
, &st
))
913 stat_fatal (filename
);
914 reading_from
= file_list_name ();
915 for (p
= file_id_list
; p
; p
= p
->next
)
916 if (p
->ino
== st
.st_ino
&& p
->dev
== st
.st_dev
)
918 int oldc
= set_char_quoting (NULL
, ':', 1);
920 _("%s: file list requested from %s already read from %s"),
921 quotearg_n (0, filename
),
922 reading_from
, p
->from_file
));
923 set_char_quoting (NULL
, ':', oldc
);
926 p
= xmalloc (sizeof *p
);
927 p
->next
= file_id_list
;
930 p
->from_file
= reading_from
;
935 /* Chop trailing slashes. */
937 chopslash (char *str
)
939 char *p
= str
+ strlen (str
) - 1;
940 while (p
> str
&& ISSLASH (*p
))
944 enum read_file_list_state
/* Result of reading file name from the list file */
946 file_list_success
, /* OK, name read successfully */
947 file_list_end
, /* End of list file */
948 file_list_zero
, /* Zero separator encountered where it should not */
949 file_list_skip
/* Empty (zero-length) entry encountered, skip it */
952 /* Read from FP a sequence of characters up to TERM and put them
955 static enum read_file_list_state
956 read_name_from_file (struct name_elt
*ent
)
960 FILE *fp
= ent
->v
.file
.fp
;
961 int term
= ent
->v
.file
.term
;
964 for (c
= getc (fp
); c
!= EOF
&& c
!= term
; c
= getc (fp
))
966 if (counter
== name_buffer_length
)
967 name_buffer
= x2realloc (name_buffer
, &name_buffer_length
);
968 name_buffer
[counter
++] = c
;
971 /* We have read a zero separator. The file possibly is
973 return file_list_zero
;
977 if (counter
== 0 && c
!= EOF
)
978 return file_list_skip
;
980 if (counter
== name_buffer_length
)
981 name_buffer
= x2realloc (name_buffer
, &name_buffer_length
);
982 name_buffer
[counter
] = 0;
983 return (counter
== 0 && c
== EOF
) ? file_list_end
: file_list_success
;
987 handle_option (const char *str
, struct name_elt
const *ent
)
991 struct option_locus loc
;
993 while (*str
&& isspace (*str
))
999 if (wordsplit (str
, &ws
, WRDSF_DEFFLAGS
|WRDSF_DOOFFS
))
1000 FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
1001 str
, wordsplit_strerror (&ws
)));
1002 ws
.ws_wordv
[0] = (char *) program_name
;
1003 loc
.source
= OPTS_FILE
;
1004 loc
.name
= ent
->v
.file
.name
;
1005 loc
.line
= ent
->v
.file
.line
;
1006 more_options (ws
.ws_wordc
+ws
.ws_offs
, ws
.ws_wordv
, &loc
);
1007 for (i
= 0; i
< ws
.ws_wordc
+ws
.ws_offs
; i
++)
1008 ws
.ws_wordv
[i
] = NULL
;
1010 wordsplit_free (&ws
);
1015 read_next_name (struct name_elt
*ent
, struct name_elt
*ret
)
1017 if (!ent
->v
.file
.fp
)
1019 if (!strcmp (ent
->v
.file
.name
, "-"))
1021 request_stdin ("-T");
1022 ent
->v
.file
.fp
= stdin
;
1026 if (add_file_id (ent
->v
.file
.name
))
1028 name_list_advance ();
1031 FILE *fp
= fopen (ent
->v
.file
.name
, "r");
1033 open_fatal (ent
->v
.file
.name
);
1034 ent
->v
.file
.fp
= fp
;
1036 ent
->v
.file
.term
= filename_terminator
;
1037 ent
->v
.file
.verbatim
= verbatim_files_from_option
;
1042 switch (read_name_from_file (ent
))
1044 case file_list_skip
:
1047 case file_list_zero
:
1048 WARNOPT (WARN_FILENAME_WITH_NULS
,
1049 (0, 0, N_("%s: file name read contains nul character"),
1050 quotearg_colon (ent
->v
.file
.name
)));
1051 ent
->v
.file
.term
= 0;
1053 case file_list_success
:
1054 if (!ent
->v
.file
.verbatim
)
1057 unquote_string (name_buffer
);
1058 if (handle_option (name_buffer
, ent
) == 0)
1060 name_list_adjust ();
1064 chopslash (name_buffer
);
1065 ret
->type
= NELT_NAME
;
1066 ret
->v
.name
= name_buffer
;
1070 if (strcmp (ent
->v
.file
.name
, "-"))
1071 fclose (ent
->v
.file
.fp
);
1072 ent
->v
.file
.fp
= NULL
;
1073 name_list_advance ();
1080 copy_name (struct name_elt
*ep
)
1085 source
= ep
->v
.name
;
1086 source_len
= strlen (source
);
1087 while (name_buffer_length
<= source_len
)
1088 name_buffer
= x2realloc(name_buffer
, &name_buffer_length
);
1089 strcpy (name_buffer
, source
);
1090 chopslash (name_buffer
);
1094 /* Get the next NELT_NAME element from name_array. Result is in
1095 static storage and can't be relied upon across two calls.
1097 If CHANGE_DIRS is true, treat any entries of type NELT_CHDIR as
1098 the request to change to the given directory.
1101 static struct name_elt
*
1102 name_next_elt (int change_dirs
)
1104 static struct name_elt entry
;
1105 struct name_elt
*ep
;
1107 while ((ep
= name_head
) != NULL
)
1112 name_list_advance ();
1116 if (read_next_name (ep
, &entry
) == 0)
1123 chdir_do (chdir_arg (xstrdup (ep
->v
.name
)));
1124 name_list_advance ();
1131 unquote_string (name_buffer
);
1132 entry
.type
= ep
->type
;
1133 entry
.v
.name
= name_buffer
;
1134 name_list_advance ();
1138 handle_file_selection_option (ep
->v
.opt
.option
, ep
->v
.opt
.arg
);
1139 name_list_advance ();
1144 unconsumed_option_report ();
1150 name_next (int change_dirs
)
1152 struct name_elt
*nelt
= name_next_elt (change_dirs
);
1153 return nelt
? nelt
->v
.name
: NULL
;
1156 /* Gather names in a list for scanning. Could hash them later if we
1159 If the names are already sorted to match the archive, we just read
1160 them one by one. name_gather reads the first one, and it is called
1161 by name_match as appropriate to read the next ones. At EOF, the
1162 last name read is just left in the buffer. This option lets users
1163 of small machines extract an arbitrary number of files by doing
1164 "tar t" and editing down the list of files. */
1169 /* Buffer able to hold a single name. */
1170 static struct name
*buffer
= NULL
;
1172 struct name_elt
*ep
;
1174 if (same_order_option
)
1176 static int change_dir
;
1178 while ((ep
= name_next_elt (0)) && ep
->type
== NELT_CHDIR
)
1179 change_dir
= chdir_arg (xstrdup (ep
->v
.name
));
1184 buffer
= make_name (ep
->v
.name
);
1185 buffer
->change_dir
= change_dir
;
1187 buffer
->found_count
= 0;
1188 buffer
->matching_flags
= INCLUDE_OPTIONS
;
1189 buffer
->directory
= NULL
;
1190 buffer
->parent
= NULL
;
1191 buffer
->cmdline
= true;
1193 namelist
= nametail
= buffer
;
1195 else if (change_dir
)
1196 addname (0, change_dir
, false, NULL
);
1200 /* Non sorted names -- read them all in. */
1205 int change_dir0
= change_dir
;
1206 while ((ep
= name_next_elt (0)) && ep
->type
== NELT_CHDIR
)
1207 change_dir
= chdir_arg (xstrdup (ep
->v
.name
));
1210 addname (ep
->v
.name
, change_dir
, true, NULL
);
1213 if (change_dir
!= change_dir0
)
1214 addname (NULL
, change_dir
, false, NULL
);
1221 /* Add a name to the namelist. */
1223 addname (char const *string
, int change_dir
, bool cmdline
, struct name
*parent
)
1225 struct name
*name
= make_name (string
);
1227 name
->prev
= nametail
;
1229 name
->found_count
= 0;
1230 name
->matching_flags
= INCLUDE_OPTIONS
;
1231 name
->change_dir
= change_dir
;
1232 name
->directory
= NULL
;
1233 name
->parent
= parent
;
1234 name
->cmdline
= cmdline
;
1237 nametail
->next
= name
;
1245 add_starting_file (char const *file_name
)
1247 struct name
*name
= make_name (file_name
);
1249 if (starting_file_option
)
1251 struct name
*head
= namelist
;
1257 name
->next
= namelist
;
1260 nametail
= namelist
;
1262 name
->found_count
= 0;
1263 name
->matching_flags
= INCLUDE_OPTIONS
;
1264 name
->change_dir
= 0;
1265 name
->directory
= NULL
;
1266 name
->parent
= NULL
;
1267 name
->cmdline
= true;
1269 starting_file_option
= true;
1272 /* Find a match for FILE_NAME (whose string length is LENGTH) in the name
1274 static struct name
*
1275 namelist_match (char const *file_name
, size_t length
)
1279 for (p
= namelist
; p
; p
= p
->next
)
1282 && exclude_fnmatch (p
->name
, file_name
, p
->matching_flags
))
1290 remname (struct name
*name
)
1294 if ((p
= name
->prev
) != NULL
)
1295 p
->next
= name
->next
;
1297 namelist
= name
->next
;
1299 if ((p
= name
->next
) != NULL
)
1300 p
->prev
= name
->prev
;
1302 nametail
= name
->prev
;
1305 /* Return true if and only if name FILE_NAME (from an archive) matches any
1306 name from the namelist. */
1308 name_match (const char *file_name
)
1310 size_t length
= strlen (file_name
);
1314 struct name
*cursor
= namelist
;
1319 if (cursor
->name
[0] == 0)
1321 chdir_do (cursor
->change_dir
);
1327 cursor
= namelist_match (file_name
, length
);
1328 if (starting_file_option
)
1330 /* If starting_file_option is set, the head of the list is the name
1331 of the member to start extraction from. Skip the match unless it
1333 if (cursor
== namelist
)
1334 starting_file_option
= false;
1340 if (!(ISSLASH (file_name
[cursor
->length
]) && recursion_option
)
1341 || cursor
->found_count
== 0)
1342 cursor
->found_count
++; /* remember it matched */
1343 chdir_do (cursor
->change_dir
);
1344 /* We got a match. */
1345 return ISFOUND (cursor
);
1348 /* Filename from archive not found in namelist. If we have the whole
1349 namelist here, just return 0. Otherwise, read the next name in and
1350 compare it. If this was the last name, namelist->found_count will
1351 remain on. If not, we loop to compare the newly read name. */
1353 if (same_order_option
&& namelist
->found_count
)
1355 name_gather (); /* read one more */
1356 if (namelist
->found_count
)
1364 /* Returns true if all names from the namelist were processed.
1365 P is the stat_info of the most recently processed entry.
1366 The decision is postponed until the next entry is read if:
1368 1) P ended with a slash (i.e. it was a directory)
1369 2) P matches any entry from the namelist *and* represents a subdirectory
1370 or a file lying under this entry (in the terms of directory structure).
1372 This is necessary to handle contents of directories. */
1374 all_names_found (struct tar_stat_info
*p
)
1376 struct name
const *cursor
;
1379 if (!p
->file_name
|| occurrence_option
== 0 || p
->had_trailing_slash
)
1381 len
= strlen (p
->file_name
);
1382 for (cursor
= namelist
; cursor
; cursor
= cursor
->next
)
1384 if ((cursor
->name
[0] && !WASFOUND (cursor
))
1385 || (len
>= cursor
->length
&& ISSLASH (p
->file_name
[cursor
->length
])))
1392 regex_usage_warning (const char *name
)
1394 static int warned_once
= 0;
1396 /* Warn about implicit use of the wildcards in command line arguments.
1397 (Default for tar prior to 1.15.91, but changed afterwards) */
1398 if (wildcards
== default_wildcards
1399 && fnmatch_pattern_has_wildcards (name
, 0))
1403 _("Pattern matching characters used in file names")));
1405 _("Use --wildcards to enable pattern matching,"
1406 " or --no-wildcards to suppress this warning")));
1411 /* Print the names of things in the namelist that were not matched. */
1413 names_notfound (void)
1415 struct name
const *cursor
;
1417 for (cursor
= namelist
; cursor
; cursor
= cursor
->next
)
1418 if (!WASFOUND (cursor
) && cursor
->name
[0])
1420 regex_usage_warning (cursor
->name
);
1422 (cursor
->found_count
== 0) ?
1423 _("%s: Not found in archive") :
1424 _("%s: Required occurrence not found in archive"),
1425 quotearg_colon (cursor
->name
)));
1428 /* Don't bother freeing the name list; we're about to exit. */
1432 if (same_order_option
)
1436 while ((name
= name_next (1)) != NULL
)
1438 regex_usage_warning (name
);
1439 ERROR ((0, 0, _("%s: Not found in archive"),
1440 quotearg_colon (name
)));
1446 label_notfound (void)
1448 struct name
const *cursor
;
1453 for (cursor
= namelist
; cursor
; cursor
= cursor
->next
)
1454 if (WASFOUND (cursor
))
1458 error (0, 0, _("Archive label mismatch"));
1459 set_exit_status (TAREXIT_DIFFERS
);
1461 for (cursor
= namelist
; cursor
; cursor
= cursor
->next
)
1463 if (regex_usage_warning (cursor
->name
))
1467 /* Don't bother freeing the name list; we're about to exit. */
1471 if (same_order_option
)
1475 while ((name
= name_next (1)) != NULL
1476 && regex_usage_warning (name
) == 0)
1481 /* Sorting name lists. */
1483 /* Sort *singly* linked LIST of names, of given LENGTH, using COMPARE
1484 to order names. Return the sorted list. Note that after calling
1485 this function, the 'prev' links in list elements are messed up.
1487 Apart from the type 'struct name' and the definition of SUCCESSOR,
1488 this is a generic list-sorting function, but it's too painful to
1489 make it both generic and portable
1492 static struct name
*
1493 merge_sort_sll (struct name
*list
, int length
,
1494 int (*compare
) (struct name
const*, struct name
const*))
1496 struct name
*first_list
;
1497 struct name
*second_list
;
1500 struct name
*result
;
1501 struct name
**merge_point
;
1502 struct name
*cursor
;
1505 # define SUCCESSOR(name) ((name)->next)
1512 if ((*compare
) (list
, SUCCESSOR (list
)) > 0)
1514 result
= SUCCESSOR (list
);
1515 SUCCESSOR (result
) = list
;
1516 SUCCESSOR (list
) = 0;
1523 first_length
= (length
+ 1) / 2;
1524 second_length
= length
/ 2;
1525 for (cursor
= list
, counter
= first_length
- 1;
1527 cursor
= SUCCESSOR (cursor
), counter
--)
1529 second_list
= SUCCESSOR (cursor
);
1530 SUCCESSOR (cursor
) = 0;
1532 first_list
= merge_sort_sll (first_list
, first_length
, compare
);
1533 second_list
= merge_sort_sll (second_list
, second_length
, compare
);
1535 merge_point
= &result
;
1536 while (first_list
&& second_list
)
1537 if ((*compare
) (first_list
, second_list
) < 0)
1539 cursor
= SUCCESSOR (first_list
);
1540 *merge_point
= first_list
;
1541 merge_point
= &SUCCESSOR (first_list
);
1542 first_list
= cursor
;
1546 cursor
= SUCCESSOR (second_list
);
1547 *merge_point
= second_list
;
1548 merge_point
= &SUCCESSOR (second_list
);
1549 second_list
= cursor
;
1552 *merge_point
= first_list
;
1554 *merge_point
= second_list
;
1561 /* Sort doubly linked LIST of names, of given LENGTH, using COMPARE
1562 to order names. Return the sorted list. */
1563 static struct name
*
1564 merge_sort (struct name
*list
, int length
,
1565 int (*compare
) (struct name
const*, struct name
const*))
1567 struct name
*head
, *p
, *prev
;
1568 head
= merge_sort_sll (list
, length
, compare
);
1569 /* Fixup prev pointers */
1570 for (prev
= NULL
, p
= head
; p
; prev
= p
, p
= p
->next
)
1575 /* A comparison function for sorting names. Put found names last;
1576 break ties by string comparison. */
1579 compare_names_found (struct name
const *n1
, struct name
const *n2
)
1581 int found_diff
= WASFOUND (n2
) - WASFOUND (n1
);
1582 return found_diff
? found_diff
: strcmp (n1
->name
, n2
->name
);
1585 /* Simple comparison by names. */
1587 compare_names (struct name
const *n1
, struct name
const *n2
)
1589 return strcmp (n1
->name
, n2
->name
);
1593 /* Add all the dirs under ST to the namelist NAME, descending the
1594 directory hierarchy recursively. */
1597 add_hierarchy_to_namelist (struct tar_stat_info
*st
, struct name
*name
)
1601 name
->directory
= scan_directory (st
);
1602 buffer
= directory_contents (name
->directory
);
1605 struct name
*child_head
= NULL
, *child_tail
= NULL
;
1606 size_t name_length
= name
->length
;
1607 size_t allocated_length
= (name_length
>= NAME_FIELD_SIZE
1608 ? name_length
+ NAME_FIELD_SIZE
1609 : NAME_FIELD_SIZE
) + 2;
1610 char *namebuf
= xmalloc (allocated_length
);
1612 size_t string_length
;
1613 int change_dir
= name
->change_dir
;
1615 strcpy (namebuf
, name
->name
);
1616 if (! ISSLASH (namebuf
[name_length
- 1]))
1618 namebuf
[name_length
++] = '/';
1619 namebuf
[name_length
] = '\0';
1622 for (string
= buffer
; *string
; string
+= string_length
+ 1)
1624 string_length
= strlen (string
);
1628 struct tar_stat_info subdir
;
1631 /* need to have at least string_length bytes above the
1632 name_length, this includes the trailing null character */
1633 while (allocated_length
< name_length
+ string_length
)
1634 namebuf
= x2realloc (namebuf
, &allocated_length
);
1635 strcpy (namebuf
+ name_length
, string
+ 1);
1636 np
= addname (namebuf
, change_dir
, false, name
);
1640 child_tail
->sibling
= np
;
1643 tar_stat_init (&subdir
);
1651 subfd
= subfile_open (st
, string
+ 1,
1652 open_read_flags
| O_DIRECTORY
);
1654 open_diag (namebuf
);
1658 if (fstat (subfd
, &subdir
.stat
) != 0)
1659 stat_diag (namebuf
);
1660 else if (! (O_DIRECTORY
|| S_ISDIR (subdir
.stat
.st_mode
)))
1663 open_diag (namebuf
);
1667 subdir
.orig_file_name
= xstrdup (namebuf
);
1668 add_hierarchy_to_namelist (&subdir
, np
);
1669 restore_parent_fd (&subdir
);
1673 tar_stat_destroy (&subdir
);
1678 name
->child
= child_head
;
1682 /* Auxiliary functions for hashed table of struct name's. */
1685 name_hash (void const *entry
, size_t n_buckets
)
1687 struct name
const *name
= entry
;
1688 return hash_string (name
->caname
, n_buckets
);
1691 /* Compare two directories for equality of their names. */
1693 name_compare (void const *entry1
, void const *entry2
)
1695 struct name
const *name1
= entry1
;
1696 struct name
const *name2
= entry2
;
1697 return strcmp (name1
->caname
, name2
->caname
) == 0;
1701 /* Rebase 'name' member of CHILD and all its siblings to
1704 rebase_child_list (struct name
*child
, struct name
*parent
)
1706 size_t old_prefix_len
= child
->parent
->length
;
1707 size_t new_prefix_len
= parent
->length
;
1708 char *new_prefix
= parent
->name
;
1710 for (; child
; child
= child
->sibling
)
1712 size_t size
= child
->length
- old_prefix_len
+ new_prefix_len
;
1713 char *newp
= xmalloc (size
+ 1);
1714 strcpy (newp
, new_prefix
);
1715 strcat (newp
, child
->name
+ old_prefix_len
);
1718 child
->length
= size
;
1720 rebase_directory (child
->directory
,
1721 child
->parent
->name
, old_prefix_len
,
1722 new_prefix
, new_prefix_len
);
1726 /* Collect all the names from argv[] (or whatever), expand them into a
1727 directory tree, and sort them. This gets only subdirectories, not
1731 collect_and_sort_names (void)
1734 struct name
*next_name
, *prev_name
= NULL
;
1736 Hash_table
*nametab
;
1741 addname (".", 0, false, NULL
);
1743 if (listed_incremental_option
)
1745 switch (chdir_count ())
1751 if (namelist
->change_dir
== 0)
1753 _("Using -C option inside file list is not "
1754 "allowed with --listed-incremental")));
1759 _("Only one -C option is allowed with "
1760 "--listed-incremental")));
1763 read_directory_file ();
1767 for (name
= namelist
; name
; name
= name
->next
, num_names
++)
1769 struct tar_stat_info st
;
1771 if (name
->found_count
|| name
->directory
)
1773 if (name
->matching_flags
& EXCLUDE_WILDCARDS
)
1774 /* NOTE: EXCLUDE_ANCHORED is not relevant here */
1775 /* FIXME: just skip regexps for now */
1777 chdir_do (name
->change_dir
);
1779 if (name
->name
[0] == 0)
1782 tar_stat_init (&st
);
1784 if (deref_stat (name
->name
, &st
.stat
) != 0)
1786 stat_diag (name
->name
);
1789 if (S_ISDIR (st
.stat
.st_mode
))
1791 int dir_fd
= openat (chdir_fd
, name
->name
,
1792 open_read_flags
| O_DIRECTORY
);
1794 open_diag (name
->name
);
1798 if (fstat (dir_fd
, &st
.stat
) != 0)
1799 stat_diag (name
->name
);
1800 else if (O_DIRECTORY
|| S_ISDIR (st
.stat
.st_mode
))
1802 st
.orig_file_name
= xstrdup (name
->name
);
1803 name
->found_count
++;
1804 add_hierarchy_to_namelist (&st
, name
);
1809 open_diag (name
->name
);
1814 tar_stat_destroy (&st
);
1817 namelist
= merge_sort (namelist
, num_names
, compare_names
);
1820 nametab
= hash_initialize (0, 0, name_hash
, name_compare
, NULL
);
1821 for (name
= namelist
; name
; name
= next_name
)
1823 next_name
= name
->next
;
1824 name
->caname
= normalize_filename (name
->change_dir
, name
->name
);
1827 struct name
*p
= hash_lookup (nametab
, name
);
1830 /* Keep the one listed in the command line */
1834 rebase_child_list (p
->child
, name
);
1835 hash_remove (nametab
, name
);
1836 /* FIXME: remove_directory (p->caname); ? */
1844 rebase_child_list (name
->child
, p
);
1845 /* FIXME: remove_directory (name->caname); ? */
1852 name
->found_count
= 0;
1853 if (!hash_insert (nametab
, name
))
1858 nametail
= prev_name
;
1859 hash_free (nametab
);
1861 namelist
= merge_sort (namelist
, num_names
, compare_names_found
);
1863 if (listed_incremental_option
)
1865 for (name
= namelist
; name
&& name
->name
[0] == 0; name
= name
->next
)
1868 append_incremental_renames (name
->directory
);
1872 /* This is like name_match, except that
1873 1. It returns a pointer to the name it matched, and doesn't set FOUND
1874 in structure. The caller will have to do that if it wants to.
1875 2. If the namelist is empty, it returns null, unlike name_match, which
1878 name_scan (const char *file_name
)
1880 size_t length
= strlen (file_name
);
1884 struct name
*cursor
= namelist_match (file_name
, length
);
1888 /* Filename from archive not found in namelist. If we have the whole
1889 namelist here, just return 0. Otherwise, read the next name in and
1890 compare it. If this was the last name, namelist->found_count will
1891 remain on. If not, we loop to compare the newly read name. */
1893 if (same_order_option
&& namelist
&& namelist
->found_count
)
1895 name_gather (); /* read one more */
1896 if (namelist
->found_count
)
1904 /* This returns a name from the namelist which doesn't have ->found
1905 set. It sets ->found before returning, so successive calls will
1906 find and return all the non-found names in the namelist. */
1907 struct name
*gnu_list_name
;
1910 name_from_list (void)
1913 gnu_list_name
= namelist
;
1914 while (gnu_list_name
1915 && (gnu_list_name
->found_count
|| gnu_list_name
->name
[0] == 0))
1916 gnu_list_name
= gnu_list_name
->next
;
1919 gnu_list_name
->found_count
++;
1920 chdir_do (gnu_list_name
->change_dir
);
1921 return gnu_list_name
;
1927 blank_name_list (void)
1932 for (name
= namelist
; name
; name
= name
->next
)
1933 name
->found_count
= 0;
1936 /* Yield a newly allocated file name consisting of DIR_NAME concatenated to
1937 NAME, with an intervening slash if DIR_NAME does not already end in one. */
1939 make_file_name (const char *directory_name
, const char *name
)
1941 size_t dirlen
= strlen (directory_name
);
1942 size_t namelen
= strlen (name
) + 1;
1943 int slash
= dirlen
&& ! ISSLASH (directory_name
[dirlen
- 1]);
1944 char *buffer
= xmalloc (dirlen
+ slash
+ namelen
);
1945 memcpy (buffer
, directory_name
, dirlen
);
1946 buffer
[dirlen
] = '/';
1947 memcpy (buffer
+ dirlen
+ slash
, name
, namelen
);
1953 /* Return the size of the prefix of FILE_NAME that is removed after
1954 stripping NUM leading file name components. NUM must be
1958 stripped_prefix_len (char const *file_name
, size_t num
)
1960 char const *p
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
1961 while (ISSLASH (*p
))
1965 bool slash
= ISSLASH (*p
);
1970 return p
- file_name
;
1971 while (ISSLASH (*p
))
1978 /* Return nonzero if NAME contains ".." as a file name component. */
1980 contains_dot_dot (char const *name
)
1982 char const *p
= name
+ FILE_SYSTEM_PREFIX_LEN (name
);
1986 if (p
[0] == '.' && p
[1] == '.' && (ISSLASH (p
[2]) || !p
[2]))
1989 while (! ISSLASH (*p
))