1 /* Various processing of names.
3 Copyright 1988, 1992, 1994, 1996-2001, 2003-2007, 2009, 2013-2016
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include <wordsplit.h>
29 static void name_add_option (int option, const char *arg);
30 static void name_add_dir (const char *name);
31 static void name_add_file (const char *name);
35 EXCLUDE_BACKUPS_OPTION = 256,
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", ARGP_KEY_ARG, 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 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 },
153 is_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++)
164 /* Either NL or NUL, as decided by the --null option. */
165 static char filename_terminator = '\n';
166 /* Treat file names read from -T input verbatim */
167 static bool verbatim_files_from_option;
170 names_parse_opt (int key, char *arg, struct argp_state *state)
180 /* Indicate we've been given -T option. This is for backward
181 compatibility only, so that `tar cfT archive /dev/null will
183 files_from_option = true;
187 if (is_file_selection_option (key))
188 name_add_option (key, arg);
190 return ARGP_ERR_UNKNOWN;
195 /* Wildcard matching settings */
198 default_wildcards, /* For exclusion == enable_wildcards,
199 for inclusion == disable_wildcards */
204 static enum wildcards wildcards = default_wildcards;
205 /* Wildcard settings (--wildcards/--no-wildcards) */
206 static int matching_flags = 0;
207 /* exclude_fnmatch options */
208 static int include_anchored = EXCLUDE_ANCHORED;
209 /* Pattern anchoring options used for file inclusion */
211 #define EXCLUDE_OPTIONS \
212 (((wildcards != disable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
216 #define INCLUDE_OPTIONS \
217 (((wildcards == enable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
222 static char const * const vcs_file_table[] = {
256 static char const * const backup_file_table[] = {
264 add_exclude_array (char const * const * fv, int opts)
268 for (i = 0; fv[i]; i++)
269 add_exclude (excluded, fv[i], opts);
273 handle_file_selection_option (int key, const char *arg)
277 case EXCLUDE_BACKUPS_OPTION:
278 add_exclude_array (backup_file_table, EXCLUDE_WILDCARDS);
282 add_exclude (excluded, arg, EXCLUDE_OPTIONS);
285 case EXCLUDE_CACHES_OPTION:
286 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_contents,
290 case EXCLUDE_CACHES_UNDER_OPTION:
291 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_under,
295 case EXCLUDE_CACHES_ALL_OPTION:
296 add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_all,
300 case EXCLUDE_IGNORE_OPTION:
301 excfile_add (arg, EXCL_NON_RECURSIVE);
304 case EXCLUDE_IGNORE_RECURSIVE_OPTION:
305 excfile_add (arg, EXCL_RECURSIVE);
308 case EXCLUDE_TAG_OPTION:
309 add_exclusion_tag (arg, exclusion_tag_contents, NULL);
312 case EXCLUDE_TAG_UNDER_OPTION:
313 add_exclusion_tag (arg, exclusion_tag_under, NULL);
316 case EXCLUDE_TAG_ALL_OPTION:
317 add_exclusion_tag (arg, exclusion_tag_all, NULL);
320 case EXCLUDE_VCS_OPTION:
321 add_exclude_array (vcs_file_table, 0);
324 case EXCLUDE_VCS_IGNORES_OPTION:
325 exclude_vcs_ignores ();
328 case RECURSION_OPTION:
329 recursion_option = FNM_LEADING_DIR;
332 case NO_RECURSION_OPTION:
333 recursion_option = 0;
337 unquote_option = true;
340 case NO_UNQUOTE_OPTION:
341 unquote_option = false;
345 filename_terminator = '\0';
346 verbatim_files_from_option = true;
350 filename_terminator = '\n';
351 verbatim_files_from_option = false;
355 if (add_exclude_file (add_exclude, excluded, arg, EXCLUDE_OPTIONS, '\n')
359 FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
363 case ANCHORED_OPTION:
364 matching_flags |= EXCLUDE_ANCHORED;
367 case NO_ANCHORED_OPTION:
368 include_anchored = 0; /* Clear the default for comman line args */
369 matching_flags &= ~ EXCLUDE_ANCHORED;
372 case IGNORE_CASE_OPTION:
373 matching_flags |= FNM_CASEFOLD;
376 case NO_IGNORE_CASE_OPTION:
377 matching_flags &= ~ FNM_CASEFOLD;
380 case WILDCARDS_OPTION:
381 wildcards = enable_wildcards;
384 case NO_WILDCARDS_OPTION:
385 wildcards = disable_wildcards;
388 case WILDCARDS_MATCH_SLASH_OPTION:
389 matching_flags &= ~ FNM_FILE_NAME;
392 case NO_WILDCARDS_MATCH_SLASH_OPTION:
393 matching_flags |= FNM_FILE_NAME;
396 case VERBATIM_FILES_FROM_OPTION:
397 verbatim_files_from_option = true;
400 case NO_VERBATIM_FILES_FROM_OPTION:
401 verbatim_files_from_option = false;
405 FATAL_ERROR ((0, 0, "unhandled positional option %d", key));
409 static struct argp names_argp = {
419 struct argp_child names_argp_children[] = {
420 { &names_argp, 0, "", 0 },
424 /* User and group names. */
426 /* Make sure you link with the proper libraries if you are running the
427 Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
428 This code should also be modified for non-UNIX systems to do something
431 static char *cached_uname;
432 static char *cached_gname;
434 static uid_t cached_uid; /* valid only if cached_uname is not empty */
435 static gid_t cached_gid; /* valid only if cached_gname is not empty */
437 /* These variables are valid only if nonempty. */
438 static char *cached_no_such_uname;
439 static char *cached_no_such_gname;
441 /* These variables are valid only if nonzero. It's not worth optimizing
442 the case for weird systems where 0 is not a valid uid or gid. */
443 static uid_t cached_no_such_uid;
444 static gid_t cached_no_such_gid;
446 /* Given UID, find the corresponding UNAME. */
448 uid_to_uname (uid_t uid, char **uname)
450 struct passwd *passwd;
452 if (uid != 0 && uid == cached_no_such_uid)
454 *uname = xstrdup ("");
458 if (!cached_uname || uid != cached_uid)
460 passwd = getpwuid (uid);
464 assign_string (&cached_uname, passwd->pw_name);
468 cached_no_such_uid = uid;
469 *uname = xstrdup ("");
473 *uname = xstrdup (cached_uname);
476 /* Given GID, find the corresponding GNAME. */
478 gid_to_gname (gid_t gid, char **gname)
482 if (gid != 0 && gid == cached_no_such_gid)
484 *gname = xstrdup ("");
488 if (!cached_gname || gid != cached_gid)
490 group = getgrgid (gid);
494 assign_string (&cached_gname, group->gr_name);
498 cached_no_such_gid = gid;
499 *gname = xstrdup ("");
503 *gname = xstrdup (cached_gname);
506 /* Given UNAME, set the corresponding UID and return 1, or else, return 0. */
508 uname_to_uid (char const *uname, uid_t *uidp)
510 struct passwd *passwd;
512 if (cached_no_such_uname
513 && strcmp (uname, cached_no_such_uname) == 0)
517 || uname[0] != cached_uname[0]
518 || strcmp (uname, cached_uname) != 0)
520 passwd = getpwnam (uname);
523 cached_uid = passwd->pw_uid;
524 assign_string (&cached_uname, passwd->pw_name);
528 assign_string (&cached_no_such_uname, uname);
536 /* Given GNAME, set the corresponding GID and return 1, or else, return 0. */
538 gname_to_gid (char const *gname, gid_t *gidp)
542 if (cached_no_such_gname
543 && strcmp (gname, cached_no_such_gname) == 0)
547 || gname[0] != cached_gname[0]
548 || strcmp (gname, cached_gname) != 0)
550 group = getgrnam (gname);
553 cached_gid = group->gr_gid;
554 assign_string (&cached_gname, gname);
558 assign_string (&cached_no_such_gname, gname);
568 make_name (const char *file_name)
570 struct name *p = xzalloc (sizeof (*p));
573 p->name = xstrdup (file_name);
574 p->length = strlen (p->name);
579 free_name (struct name *p)
590 /* Names from the command call. */
592 static struct name *namelist; /* first name in list, if any */
593 static struct name *nametail; /* end of name list */
595 /* File name arguments are processed in two stages: first a
596 name element list (see below) is filled, then the names from it
597 are moved into the namelist.
599 This awkward process is needed only to implement --same-order option,
600 which is meant to help process large archives on machines with
601 limited memory. With this option on, namelist contains at most one
602 entry, which diminishes the memory consumption.
604 However, I very much doubt if we still need this -- Sergey */
606 /* A name_list element contains entries of three types: */
610 NELT_NAME, /* File name */
611 NELT_CHDIR, /* Change directory request */
612 NELT_FILE, /* Read file names from that file */
613 NELT_NOOP, /* No operation */
614 NELT_OPTION /* Filename-selection option */
617 struct name_elt /* A name_array element. */
619 struct name_elt *next, *prev;
620 enum nelt_type type; /* Element type, see NELT_* constants above */
623 const char *name; /* File or directory name */
624 struct /* File, if type == NELT_FILE */
626 const char *name;/* File name */
627 size_t line; /* Input line number */
628 int term; /* File name terminator in the list */
629 bool verbatim; /* Verbatim handling of file names: no white-space
630 trimming, no option processing */
637 } opt; /* NELT_OPTION */
641 static struct name_elt *name_head; /* store a list of names */
642 size_t name_count; /* how many of the entries are names? */
644 static struct name_elt *
645 name_elt_alloc (void)
647 struct name_elt *elt;
649 elt = xmalloc (sizeof (*elt));
653 name_head->prev = name_head->next = NULL;
654 name_head->type = NELT_NOOP;
655 elt = xmalloc (sizeof (*elt));
658 elt->prev = name_head->prev;
660 name_head->prev->next = elt;
661 elt->next = name_head;
662 name_head->prev = elt;
667 name_list_adjust (void)
670 while (name_head->prev)
671 name_head = name_head->prev;
675 name_list_advance (void)
677 struct name_elt *elt = name_head;
678 name_head = elt->next;
680 name_head->prev = NULL;
685 /* Add to name_array the file NAME with fnmatch options MATFLAGS */
687 name_add_name (const char *name)
689 struct name_elt *ep = name_elt_alloc ();
691 ep->type = NELT_NAME;
697 name_add_option (int option, const char *arg)
699 struct name_elt *elt = name_elt_alloc ();
700 elt->type = NELT_OPTION;
701 elt->v.opt.option = option;
702 elt->v.opt.arg = arg;
705 /* Add to name_array a chdir request for the directory NAME */
707 name_add_dir (const char *name)
709 struct name_elt *ep = name_elt_alloc ();
710 ep->type = NELT_CHDIR;
715 name_add_file (const char *name)
717 struct name_elt *ep = name_elt_alloc ();
719 ep->type = NELT_FILE;
720 ep->v.file.name = name;
722 ep->v.file.fp = NULL;
725 /* Names from external name file. */
727 static char *name_buffer; /* buffer to hold the current file name */
728 static size_t name_buffer_length; /* allocated length of name_buffer */
730 /* Set up to gather file names for tar. They can either come from a
731 file or were saved from decoding arguments. */
735 name_buffer = xmalloc (NAME_FIELD_SIZE + 2);
736 name_buffer_length = NAME_FIELD_SIZE;
746 /* Prevent recursive inclusion of the same file */
749 struct file_id_list *next;
752 const char *from_file;
755 static struct file_id_list *file_id_list;
757 /* Return the name of the file from which the file names and options
761 file_list_name (void)
763 struct name_elt *elt;
765 for (elt = name_head; elt; elt = elt->next)
766 if (elt->type == NELT_FILE && elt->v.file.fp)
767 return elt->v.file.name;
768 return _("command line");
772 add_file_id (const char *filename)
774 struct file_id_list *p;
776 const char *reading_from;
778 if (stat (filename, &st))
779 stat_fatal (filename);
780 reading_from = file_list_name ();
781 for (p = file_id_list; p; p = p->next)
782 if (p->ino == st.st_ino && p->dev == st.st_dev)
784 int oldc = set_char_quoting (NULL, ':', 1);
786 _("%s: file list requested from %s already read from %s"),
787 quotearg_n (0, filename),
788 reading_from, p->from_file));
789 set_char_quoting (NULL, ':', oldc);
792 p = xmalloc (sizeof *p);
793 p->next = file_id_list;
796 p->from_file = reading_from;
801 /* Chop trailing slashes. */
803 chopslash (char *str)
805 char *p = str + strlen (str) - 1;
806 while (p > str && ISSLASH (*p))
810 enum read_file_list_state /* Result of reading file name from the list file */
812 file_list_success, /* OK, name read successfully */
813 file_list_end, /* End of list file */
814 file_list_zero, /* Zero separator encountered where it should not */
815 file_list_skip /* Empty (zero-length) entry encountered, skip it */
818 /* Read from FP a sequence of characters up to TERM and put them
821 static enum read_file_list_state
822 read_name_from_file (struct name_elt *ent)
826 FILE *fp = ent->v.file.fp;
827 int term = ent->v.file.term;
830 for (c = getc (fp); c != EOF && c != term; c = getc (fp))
832 if (counter == name_buffer_length)
833 name_buffer = x2realloc (name_buffer, &name_buffer_length);
834 name_buffer[counter++] = c;
837 /* We have read a zero separator. The file possibly is
839 return file_list_zero;
843 if (counter == 0 && c != EOF)
844 return file_list_skip;
846 if (counter == name_buffer_length)
847 name_buffer = x2realloc (name_buffer, &name_buffer_length);
848 name_buffer[counter] = 0;
849 chopslash (name_buffer);
850 return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
854 handle_option (const char *str, struct name_elt const *ent)
858 struct option_locus loc;
860 while (*str && isspace (*str))
866 if (wordsplit (str, &ws, WRDSF_DEFFLAGS|WRDSF_DOOFFS))
867 FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
868 str, wordsplit_strerror (&ws)));
869 ws.ws_wordv[0] = (char *) program_name;
870 loc.source = OPTS_FILE;
871 loc.name = ent->v.file.name;
872 loc.line = ent->v.file.line;
873 more_options (ws.ws_wordc+ws.ws_offs, ws.ws_wordv, &loc);
874 for (i = 0; i < ws.ws_wordc+ws.ws_offs; i++)
875 ws.ws_wordv[i] = NULL;
877 wordsplit_free (&ws);
882 read_next_name (struct name_elt *ent, struct name_elt *ret)
886 if (!strcmp (ent->v.file.name, "-"))
888 request_stdin ("-T");
889 ent->v.file.fp = stdin;
893 if (add_file_id (ent->v.file.name))
895 name_list_advance ();
898 if ((ent->v.file.fp = fopen (ent->v.file.name, "r")) == NULL)
899 open_fatal (ent->v.file.name);
901 ent->v.file.term = filename_terminator;
902 ent->v.file.verbatim = verbatim_files_from_option;
907 switch (read_name_from_file (ent))
913 WARNOPT (WARN_FILENAME_WITH_NULS,
914 (0, 0, N_("%s: file name read contains nul character"),
915 quotearg_colon (ent->v.file.name)));
916 ent->v.file.term = 0;
918 case file_list_success:
920 unquote_string (name_buffer);
921 if (!ent->v.file.verbatim && handle_option (name_buffer, ent) == 0)
926 ret->type = NELT_NAME;
927 ret->v.name = name_buffer;
931 if (strcmp (ent->v.file.name, "-"))
932 fclose (ent->v.file.fp);
933 ent->v.file.fp = NULL;
934 name_list_advance ();
941 copy_name (struct name_elt *ep)
947 source_len = strlen (source);
948 if (name_buffer_length < source_len)
952 name_buffer_length *= 2;
953 if (! name_buffer_length)
956 while (name_buffer_length < source_len);
959 name_buffer = xmalloc(name_buffer_length + 2);
961 strcpy (name_buffer, source);
962 chopslash (name_buffer);
966 /* Get the next NELT_NAME element from name_array. Result is in
967 static storage and can't be relied upon across two calls.
969 If CHANGE_DIRS is true, treat any entries of type NELT_CHDIR as
970 the request to change to the given directory.
973 static struct name_elt *
974 name_next_elt (int change_dirs)
976 static struct name_elt entry;
979 while ((ep = name_head) != NULL)
984 name_list_advance ();
988 if (read_next_name (ep, &entry) == 0)
995 chdir_do (chdir_arg (xstrdup (ep->v.name)));
996 name_list_advance ();
1003 unquote_string (name_buffer);
1004 entry.type = ep->type;
1005 entry.v.name = name_buffer;
1006 name_list_advance ();
1010 handle_file_selection_option (ep->v.opt.option, ep->v.opt.arg);
1011 name_list_advance ();
1020 name_next (int change_dirs)
1022 struct name_elt *nelt = name_next_elt (change_dirs);
1023 return nelt ? nelt->v.name : NULL;
1026 /* Gather names in a list for scanning. Could hash them later if we
1029 If the names are already sorted to match the archive, we just read
1030 them one by one. name_gather reads the first one, and it is called
1031 by name_match as appropriate to read the next ones. At EOF, the
1032 last name read is just left in the buffer. This option lets users
1033 of small machines extract an arbitrary number of files by doing
1034 "tar t" and editing down the list of files. */
1039 /* Buffer able to hold a single name. */
1040 static struct name *buffer = NULL;
1042 struct name_elt *ep;
1044 if (same_order_option)
1046 static int change_dir;
1048 while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
1049 change_dir = chdir_arg (xstrdup (ep->v.name));
1054 buffer = make_name (ep->v.name);
1055 buffer->change_dir = change_dir;
1057 buffer->found_count = 0;
1058 buffer->matching_flags = INCLUDE_OPTIONS;
1059 buffer->directory = NULL;
1060 buffer->parent = NULL;
1061 buffer->cmdline = true;
1063 namelist = nametail = buffer;
1065 else if (change_dir)
1066 addname (0, change_dir, false, NULL);
1070 /* Non sorted names -- read them all in. */
1075 int change_dir0 = change_dir;
1076 while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
1077 change_dir = chdir_arg (xstrdup (ep->v.name));
1080 addname (ep->v.name, change_dir, true, NULL);
1083 if (change_dir != change_dir0)
1084 addname (NULL, change_dir, false, NULL);
1091 /* Add a name to the namelist. */
1093 addname (char const *string, int change_dir, bool cmdline, struct name *parent)
1095 struct name *name = make_name (string);
1097 name->prev = nametail;
1099 name->found_count = 0;
1100 name->matching_flags = INCLUDE_OPTIONS;
1101 name->change_dir = change_dir;
1102 name->directory = NULL;
1103 name->parent = parent;
1104 name->cmdline = cmdline;
1107 nametail->next = name;
1114 /* Find a match for FILE_NAME (whose string length is LENGTH) in the name
1116 static struct name *
1117 namelist_match (char const *file_name, size_t length)
1121 for (p = namelist; p; p = p->next)
1124 && exclude_fnmatch (p->name, file_name, p->matching_flags))
1132 remname (struct name *name)
1136 if ((p = name->prev) != NULL)
1137 p->next = name->next;
1139 namelist = name->next;
1141 if ((p = name->next) != NULL)
1142 p->prev = name->prev;
1144 nametail = name->prev;
1147 /* Return true if and only if name FILE_NAME (from an archive) matches any
1148 name from the namelist. */
1150 name_match (const char *file_name)
1152 size_t length = strlen (file_name);
1156 struct name *cursor = namelist;
1161 if (cursor->name[0] == 0)
1163 chdir_do (cursor->change_dir);
1169 cursor = namelist_match (file_name, length);
1172 if (!(ISSLASH (file_name[cursor->length]) && recursion_option)
1173 || cursor->found_count == 0)
1174 cursor->found_count++; /* remember it matched */
1175 if (starting_file_option)
1181 chdir_do (cursor->change_dir);
1183 /* We got a match. */
1184 return ISFOUND (cursor);
1187 /* Filename from archive not found in namelist. If we have the whole
1188 namelist here, just return 0. Otherwise, read the next name in and
1189 compare it. If this was the last name, namelist->found_count will
1190 remain on. If not, we loop to compare the newly read name. */
1192 if (same_order_option && namelist->found_count)
1194 name_gather (); /* read one more */
1195 if (namelist->found_count)
1203 /* Returns true if all names from the namelist were processed.
1204 P is the stat_info of the most recently processed entry.
1205 The decision is postponed until the next entry is read if:
1207 1) P ended with a slash (i.e. it was a directory)
1208 2) P matches any entry from the namelist *and* represents a subdirectory
1209 or a file lying under this entry (in the terms of directory structure).
1211 This is necessary to handle contents of directories. */
1213 all_names_found (struct tar_stat_info *p)
1215 struct name const *cursor;
1218 if (!p->file_name || occurrence_option == 0 || p->had_trailing_slash)
1220 len = strlen (p->file_name);
1221 for (cursor = namelist; cursor; cursor = cursor->next)
1223 if ((cursor->name[0] && !WASFOUND (cursor))
1224 || (len >= cursor->length && ISSLASH (p->file_name[cursor->length])))
1231 regex_usage_warning (const char *name)
1233 static int warned_once = 0;
1235 /* Warn about implicit use of the wildcards in command line arguments.
1236 (Default for tar prior to 1.15.91, but changed afterwards) */
1237 if (wildcards == default_wildcards
1238 && fnmatch_pattern_has_wildcards (name, 0))
1242 _("Pattern matching characters used in file names")));
1244 _("Use --wildcards to enable pattern matching,"
1245 " or --no-wildcards to suppress this warning")));
1250 /* Print the names of things in the namelist that were not matched. */
1252 names_notfound (void)
1254 struct name const *cursor;
1256 for (cursor = namelist; cursor; cursor = cursor->next)
1257 if (!WASFOUND (cursor) && cursor->name[0])
1259 regex_usage_warning (cursor->name);
1261 (cursor->found_count == 0) ?
1262 _("%s: Not found in archive") :
1263 _("%s: Required occurrence not found in archive"),
1264 quotearg_colon (cursor->name)));
1267 /* Don't bother freeing the name list; we're about to exit. */
1271 if (same_order_option)
1275 while ((name = name_next (1)) != NULL)
1277 regex_usage_warning (name);
1278 ERROR ((0, 0, _("%s: Not found in archive"),
1279 quotearg_colon (name)));
1285 label_notfound (void)
1287 struct name const *cursor;
1292 for (cursor = namelist; cursor; cursor = cursor->next)
1293 if (WASFOUND (cursor))
1297 error (0, 0, _("Archive label mismatch"));
1298 set_exit_status (TAREXIT_DIFFERS);
1300 for (cursor = namelist; cursor; cursor = cursor->next)
1302 if (regex_usage_warning (cursor->name))
1306 /* Don't bother freeing the name list; we're about to exit. */
1310 if (same_order_option)
1314 while ((name = name_next (1)) != NULL
1315 && regex_usage_warning (name) == 0)
1320 /* Sorting name lists. */
1322 /* Sort *singly* linked LIST of names, of given LENGTH, using COMPARE
1323 to order names. Return the sorted list. Note that after calling
1324 this function, the 'prev' links in list elements are messed up.
1326 Apart from the type 'struct name' and the definition of SUCCESSOR,
1327 this is a generic list-sorting function, but it's too painful to
1328 make it both generic and portable
1331 static struct name *
1332 merge_sort_sll (struct name *list, int length,
1333 int (*compare) (struct name const*, struct name const*))
1335 struct name *first_list;
1336 struct name *second_list;
1339 struct name *result;
1340 struct name **merge_point;
1341 struct name *cursor;
1344 # define SUCCESSOR(name) ((name)->next)
1351 if ((*compare) (list, SUCCESSOR (list)) > 0)
1353 result = SUCCESSOR (list);
1354 SUCCESSOR (result) = list;
1355 SUCCESSOR (list) = 0;
1362 first_length = (length + 1) / 2;
1363 second_length = length / 2;
1364 for (cursor = list, counter = first_length - 1;
1366 cursor = SUCCESSOR (cursor), counter--)
1368 second_list = SUCCESSOR (cursor);
1369 SUCCESSOR (cursor) = 0;
1371 first_list = merge_sort_sll (first_list, first_length, compare);
1372 second_list = merge_sort_sll (second_list, second_length, compare);
1374 merge_point = &result;
1375 while (first_list && second_list)
1376 if ((*compare) (first_list, second_list) < 0)
1378 cursor = SUCCESSOR (first_list);
1379 *merge_point = first_list;
1380 merge_point = &SUCCESSOR (first_list);
1381 first_list = cursor;
1385 cursor = SUCCESSOR (second_list);
1386 *merge_point = second_list;
1387 merge_point = &SUCCESSOR (second_list);
1388 second_list = cursor;
1391 *merge_point = first_list;
1393 *merge_point = second_list;
1400 /* Sort doubly linked LIST of names, of given LENGTH, using COMPARE
1401 to order names. Return the sorted list. */
1402 static struct name *
1403 merge_sort (struct name *list, int length,
1404 int (*compare) (struct name const*, struct name const*))
1406 struct name *head, *p, *prev;
1407 head = merge_sort_sll (list, length, compare);
1408 /* Fixup prev pointers */
1409 for (prev = NULL, p = head; p; prev = p, p = p->next)
1414 /* A comparison function for sorting names. Put found names last;
1415 break ties by string comparison. */
1418 compare_names_found (struct name const *n1, struct name const *n2)
1420 int found_diff = WASFOUND (n2) - WASFOUND (n1);
1421 return found_diff ? found_diff : strcmp (n1->name, n2->name);
1424 /* Simple comparison by names. */
1426 compare_names (struct name const *n1, struct name const *n2)
1428 return strcmp (n1->name, n2->name);
1432 /* Add all the dirs under ST to the namelist NAME, descending the
1433 directory hierarchy recursively. */
1436 add_hierarchy_to_namelist (struct tar_stat_info *st, struct name *name)
1440 name->directory = scan_directory (st);
1441 buffer = directory_contents (name->directory);
1444 struct name *child_head = NULL, *child_tail = NULL;
1445 size_t name_length = name->length;
1446 size_t allocated_length = (name_length >= NAME_FIELD_SIZE
1447 ? name_length + NAME_FIELD_SIZE
1449 char *namebuf = xmalloc (allocated_length + 1);
1450 /* FIXME: + 2 above? */
1452 size_t string_length;
1453 int change_dir = name->change_dir;
1455 strcpy (namebuf, name->name);
1456 if (! ISSLASH (namebuf[name_length - 1]))
1458 namebuf[name_length++] = '/';
1459 namebuf[name_length] = '\0';
1462 for (string = buffer; *string; string += string_length + 1)
1464 string_length = strlen (string);
1468 struct tar_stat_info subdir;
1471 if (allocated_length <= name_length + string_length)
1475 allocated_length *= 2;
1476 if (! allocated_length)
1479 while (allocated_length <= name_length + string_length);
1481 namebuf = xrealloc (namebuf, allocated_length + 1);
1483 strcpy (namebuf + name_length, string + 1);
1484 np = addname (namebuf, change_dir, false, name);
1488 child_tail->sibling = np;
1491 tar_stat_init (&subdir);
1499 subfd = subfile_open (st, string + 1,
1500 open_read_flags | O_DIRECTORY);
1502 open_diag (namebuf);
1506 if (fstat (subfd, &subdir.stat) != 0)
1507 stat_diag (namebuf);
1508 else if (! (O_DIRECTORY || S_ISDIR (subdir.stat.st_mode)))
1511 open_diag (namebuf);
1515 subdir.orig_file_name = xstrdup (namebuf);
1516 add_hierarchy_to_namelist (&subdir, np);
1517 restore_parent_fd (&subdir);
1521 tar_stat_destroy (&subdir);
1526 name->child = child_head;
1530 /* Auxiliary functions for hashed table of struct name's. */
1533 name_hash (void const *entry, size_t n_buckets)
1535 struct name const *name = entry;
1536 return hash_string (name->caname, n_buckets);
1539 /* Compare two directories for equality of their names. */
1541 name_compare (void const *entry1, void const *entry2)
1543 struct name const *name1 = entry1;
1544 struct name const *name2 = entry2;
1545 return strcmp (name1->caname, name2->caname) == 0;
1549 /* Rebase 'name' member of CHILD and all its siblings to
1552 rebase_child_list (struct name *child, struct name *parent)
1554 size_t old_prefix_len = child->parent->length;
1555 size_t new_prefix_len = parent->length;
1556 char *new_prefix = parent->name;
1558 for (; child; child = child->sibling)
1560 size_t size = child->length - old_prefix_len + new_prefix_len;
1561 char *newp = xmalloc (size + 1);
1562 strcpy (newp, new_prefix);
1563 strcat (newp, child->name + old_prefix_len);
1566 child->length = size;
1568 rebase_directory (child->directory,
1569 child->parent->name, old_prefix_len,
1570 new_prefix, new_prefix_len);
1574 /* Collect all the names from argv[] (or whatever), expand them into a
1575 directory tree, and sort them. This gets only subdirectories, not
1579 collect_and_sort_names (void)
1582 struct name *next_name, *prev_name = NULL;
1584 Hash_table *nametab;
1589 addname (".", 0, false, NULL);
1591 if (listed_incremental_option)
1593 switch (chdir_count ())
1599 if (namelist->change_dir == 0)
1601 _("Using -C option inside file list is not "
1602 "allowed with --listed-incremental")));
1607 _("Only one -C option is allowed with "
1608 "--listed-incremental")));
1611 read_directory_file ();
1615 for (name = namelist; name; name = name->next, num_names++)
1617 struct tar_stat_info st;
1619 if (name->found_count || name->directory)
1621 if (name->matching_flags & EXCLUDE_WILDCARDS)
1622 /* NOTE: EXCLUDE_ANCHORED is not relevant here */
1623 /* FIXME: just skip regexps for now */
1625 chdir_do (name->change_dir);
1627 if (name->name[0] == 0)
1630 tar_stat_init (&st);
1632 if (deref_stat (name->name, &st.stat) != 0)
1634 stat_diag (name->name);
1637 if (S_ISDIR (st.stat.st_mode))
1639 int dir_fd = openat (chdir_fd, name->name,
1640 open_read_flags | O_DIRECTORY);
1642 open_diag (name->name);
1646 if (fstat (dir_fd, &st.stat) != 0)
1647 stat_diag (name->name);
1648 else if (O_DIRECTORY || S_ISDIR (st.stat.st_mode))
1650 st.orig_file_name = xstrdup (name->name);
1651 name->found_count++;
1652 add_hierarchy_to_namelist (&st, name);
1657 tar_stat_destroy (&st);
1660 namelist = merge_sort (namelist, num_names, compare_names);
1663 nametab = hash_initialize (0, 0, name_hash, name_compare, NULL);
1664 for (name = namelist; name; name = next_name)
1666 next_name = name->next;
1667 name->caname = normalize_filename (name->change_dir, name->name);
1670 struct name *p = hash_lookup (nametab, name);
1673 /* Keep the one listed in the command line */
1677 rebase_child_list (p->child, name);
1678 hash_delete (nametab, name);
1679 /* FIXME: remove_directory (p->caname); ? */
1687 rebase_child_list (name->child, p);
1688 /* FIXME: remove_directory (name->caname); ? */
1695 name->found_count = 0;
1696 if (!hash_insert (nametab, name))
1701 nametail = prev_name;
1702 hash_free (nametab);
1704 namelist = merge_sort (namelist, num_names, compare_names_found);
1706 if (listed_incremental_option)
1708 for (name = namelist; name && name->name[0] == 0; name++)
1711 append_incremental_renames (name->directory);
1715 /* This is like name_match, except that
1716 1. It returns a pointer to the name it matched, and doesn't set FOUND
1717 in structure. The caller will have to do that if it wants to.
1718 2. If the namelist is empty, it returns null, unlike name_match, which
1721 name_scan (const char *file_name)
1723 size_t length = strlen (file_name);
1727 struct name *cursor = namelist_match (file_name, length);
1731 /* Filename from archive not found in namelist. If we have the whole
1732 namelist here, just return 0. Otherwise, read the next name in and
1733 compare it. If this was the last name, namelist->found_count will
1734 remain on. If not, we loop to compare the newly read name. */
1736 if (same_order_option && namelist && namelist->found_count)
1738 name_gather (); /* read one more */
1739 if (namelist->found_count)
1747 /* This returns a name from the namelist which doesn't have ->found
1748 set. It sets ->found before returning, so successive calls will
1749 find and return all the non-found names in the namelist. */
1750 struct name *gnu_list_name;
1753 name_from_list (void)
1756 gnu_list_name = namelist;
1757 while (gnu_list_name
1758 && (gnu_list_name->found_count || gnu_list_name->name[0] == 0))
1759 gnu_list_name = gnu_list_name->next;
1762 gnu_list_name->found_count++;
1763 chdir_do (gnu_list_name->change_dir);
1764 return gnu_list_name;
1770 blank_name_list (void)
1775 for (name = namelist; name; name = name->next)
1776 name->found_count = 0;
1779 /* Yield a newly allocated file name consisting of DIR_NAME concatenated to
1780 NAME, with an intervening slash if DIR_NAME does not already end in one. */
1782 make_file_name (const char *directory_name, const char *name)
1784 size_t dirlen = strlen (directory_name);
1785 size_t namelen = strlen (name) + 1;
1786 int slash = dirlen && ! ISSLASH (directory_name[dirlen - 1]);
1787 char *buffer = xmalloc (dirlen + slash + namelen);
1788 memcpy (buffer, directory_name, dirlen);
1789 buffer[dirlen] = '/';
1790 memcpy (buffer + dirlen + slash, name, namelen);
1796 /* Return the size of the prefix of FILE_NAME that is removed after
1797 stripping NUM leading file name components. NUM must be
1801 stripped_prefix_len (char const *file_name, size_t num)
1803 char const *p = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
1804 while (ISSLASH (*p))
1808 bool slash = ISSLASH (*p);
1813 return p - file_name;
1814 while (ISSLASH (*p))
1821 /* Return nonzero if NAME contains ".." as a file name component. */
1823 contains_dot_dot (char const *name)
1825 char const *p = name + FILE_SYSTEM_PREFIX_LEN (name);
1829 if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
1832 while (! ISSLASH (*p))