Reduce memory consuption when handling the -T option.
[debian/tar] / src / names.c
1 /* Various processing of names.
2
3    Copyright 1988, 1992, 1994, 1996-2001, 2003-2007, 2009, 2013 Free
4    Software Foundation, Inc.
5
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
9    version.
10
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.
15
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/>.  */
18
19 #include <system.h>
20
21 #include <fnmatch.h>
22 #include <hash.h>
23 #include <quotearg.h>
24 #include <wordsplit.h>
25
26 #include "common.h"
27 \f
28 /* User and group names.  */
29
30 /* Make sure you link with the proper libraries if you are running the
31    Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
32    This code should also be modified for non-UNIX systems to do something
33    reasonable.  */
34
35 static char *cached_uname;
36 static char *cached_gname;
37
38 static uid_t cached_uid;        /* valid only if cached_uname is not empty */
39 static gid_t cached_gid;        /* valid only if cached_gname is not empty */
40
41 /* These variables are valid only if nonempty.  */
42 static char *cached_no_such_uname;
43 static char *cached_no_such_gname;
44
45 /* These variables are valid only if nonzero.  It's not worth optimizing
46    the case for weird systems where 0 is not a valid uid or gid.  */
47 static uid_t cached_no_such_uid;
48 static gid_t cached_no_such_gid;
49
50 /* Given UID, find the corresponding UNAME.  */
51 void
52 uid_to_uname (uid_t uid, char **uname)
53 {
54   struct passwd *passwd;
55
56   if (uid != 0 && uid == cached_no_such_uid)
57     {
58       *uname = xstrdup ("");
59       return;
60     }
61
62   if (!cached_uname || uid != cached_uid)
63     {
64       passwd = getpwuid (uid);
65       if (passwd)
66         {
67           cached_uid = uid;
68           assign_string (&cached_uname, passwd->pw_name);
69         }
70       else
71         {
72           cached_no_such_uid = uid;
73           *uname = xstrdup ("");
74           return;
75         }
76     }
77   *uname = xstrdup (cached_uname);
78 }
79
80 /* Given GID, find the corresponding GNAME.  */
81 void
82 gid_to_gname (gid_t gid, char **gname)
83 {
84   struct group *group;
85
86   if (gid != 0 && gid == cached_no_such_gid)
87     {
88       *gname = xstrdup ("");
89       return;
90     }
91
92   if (!cached_gname || gid != cached_gid)
93     {
94       group = getgrgid (gid);
95       if (group)
96         {
97           cached_gid = gid;
98           assign_string (&cached_gname, group->gr_name);
99         }
100       else
101         {
102           cached_no_such_gid = gid;
103           *gname = xstrdup ("");
104           return;
105         }
106     }
107   *gname = xstrdup (cached_gname);
108 }
109
110 /* Given UNAME, set the corresponding UID and return 1, or else, return 0.  */
111 int
112 uname_to_uid (char const *uname, uid_t *uidp)
113 {
114   struct passwd *passwd;
115
116   if (cached_no_such_uname
117       && strcmp (uname, cached_no_such_uname) == 0)
118     return 0;
119
120   if (!cached_uname
121       || uname[0] != cached_uname[0]
122       || strcmp (uname, cached_uname) != 0)
123     {
124       passwd = getpwnam (uname);
125       if (passwd)
126         {
127           cached_uid = passwd->pw_uid;
128           assign_string (&cached_uname, passwd->pw_name);
129         }
130       else
131         {
132           assign_string (&cached_no_such_uname, uname);
133           return 0;
134         }
135     }
136   *uidp = cached_uid;
137   return 1;
138 }
139
140 /* Given GNAME, set the corresponding GID and return 1, or else, return 0.  */
141 int
142 gname_to_gid (char const *gname, gid_t *gidp)
143 {
144   struct group *group;
145
146   if (cached_no_such_gname
147       && strcmp (gname, cached_no_such_gname) == 0)
148     return 0;
149
150   if (!cached_gname
151       || gname[0] != cached_gname[0]
152       || strcmp (gname, cached_gname) != 0)
153     {
154       group = getgrnam (gname);
155       if (group)
156         {
157           cached_gid = group->gr_gid;
158           assign_string (&cached_gname, gname);
159         }
160       else
161         {
162           assign_string (&cached_no_such_gname, gname);
163           return 0;
164         }
165     }
166   *gidp = cached_gid;
167   return 1;
168 }
169
170 \f
171 static struct name *
172 make_name (const char *file_name)
173 {
174   struct name *p = xzalloc (sizeof (*p));
175   if (!file_name)
176     file_name = "";
177   p->name = xstrdup (file_name);
178   p->length = strlen (p->name);
179   return p;
180 }
181
182 static void
183 free_name (struct name *p)
184 {
185   if (p)
186     {
187       free (p->name);
188       free (p->caname);
189       free (p);
190     }
191 }
192
193 \f
194 /* Names from the command call.  */
195
196 static struct name *namelist;   /* first name in list, if any */
197 static struct name *nametail;   /* end of name list */
198
199 /* File name arguments are processed in two stages: first a
200    name_array (see below) is filled, then the names from it
201    are moved into the namelist.
202
203    This awkward process is needed only to implement --same-order option,
204    which is meant to help process large archives on machines with
205    limited memory.  With this option on, namelist contains at most one
206    entry, which diminishes the memory consumption.
207
208    However, I very much doubt if we still need this -- Sergey */
209
210 /* A name_array element contains entries of three types: */
211
212 #define NELT_NAME  0   /* File name */
213 #define NELT_CHDIR 1   /* Change directory request */
214 #define NELT_FMASK 2   /* Change fnmatch options request */
215 #define NELT_FILE  3   /* Read file names from that file */
216                          
217 struct name_elt        /* A name_array element. */
218 {
219   char type;           /* Element type, see NELT_* constants above */
220   union
221   {
222     const char *name;  /* File or directory name */
223     int matching_flags;/* fnmatch options if type == NELT_FMASK */
224     struct
225     {
226       const char *name;/* File name */ 
227       int term;        /* File name terminator in the list */
228       FILE *fp;
229     } file;
230   } v;
231 };
232
233 static struct name_elt *name_array;  /* store an array of names */
234 static size_t allocated_entries; /* how big is the array? */
235 static size_t entries;           /* how many entries does it have? */
236 static size_t scanned;           /* how many of the entries have we scanned? */
237 size_t name_count;               /* how many of the entries are names? */
238
239 /* Check the size of name_array, reallocating it as necessary.  */
240 static void
241 check_name_alloc (void)
242 {
243   if (entries == allocated_entries)
244     {
245       if (allocated_entries == 0)
246         allocated_entries = 10; /* Set initial allocation */
247       name_array = x2nrealloc (name_array, &allocated_entries,
248                                sizeof (name_array[0]));
249     }
250 }
251
252 /* Add to name_array the file NAME with fnmatch options MATCHING_FLAGS */
253 void
254 name_add_name (const char *name, int matching_flags)
255 {
256   static int prev_flags = 0; /* FIXME: Or EXCLUDE_ANCHORED? */
257   struct name_elt *ep;
258
259   check_name_alloc ();
260   ep = &name_array[entries++];
261   if (prev_flags != matching_flags)
262     {
263       ep->type = NELT_FMASK;
264       ep->v.matching_flags = matching_flags;
265       prev_flags = matching_flags;
266       check_name_alloc ();
267       ep = &name_array[entries++];
268     }
269   ep->type = NELT_NAME;
270   ep->v.name = name;
271   name_count++;
272 }
273
274 /* Add to name_array a chdir request for the directory NAME */
275 void
276 name_add_dir (const char *name)
277 {
278   struct name_elt *ep;
279   check_name_alloc ();
280   ep = &name_array[entries++];
281   ep->type = NELT_CHDIR;
282   ep->v.name = name;
283 }
284
285 void
286 name_add_file (const char *name, int term)
287 {
288   struct name_elt *ep;
289   check_name_alloc ();
290   ep = &name_array[entries++];
291   ep->type = NELT_FILE;
292   ep->v.file.name = name;
293   ep->v.file.term = term;
294 }
295 \f
296 /* Names from external name file.  */
297
298 static char *name_buffer;       /* buffer to hold the current file name */
299 static size_t name_buffer_length; /* allocated length of name_buffer */
300
301 /* Set up to gather file names for tar.  They can either come from a
302    file or were saved from decoding arguments.  */
303 void
304 name_init (void)
305 {
306   name_buffer = xmalloc (NAME_FIELD_SIZE + 2);
307   name_buffer_length = NAME_FIELD_SIZE;
308 }
309
310 void
311 name_term (void)
312 {
313   free (name_buffer);
314   free (name_array);
315 }
316 \f
317 /* Prevent recursive inclusion of the same file */
318 struct file_id_list
319 {
320   struct file_id_list *next;
321   ino_t ino;
322   dev_t dev;
323 };
324
325 static struct file_id_list *file_id_list;
326
327 static void
328 add_file_id (const char *filename)
329 {
330   struct file_id_list *p;
331   struct stat st;
332
333   if (stat (filename, &st))
334     stat_fatal (filename);
335   for (p = file_id_list; p; p = p->next)
336     if (p->ino == st.st_ino && p->dev == st.st_dev)
337       {
338         FATAL_ERROR ((0, 0, _("%s: file list already read"),
339                       quotearg_colon (filename)));
340       }
341   p = xmalloc (sizeof *p);
342   p->next = file_id_list;
343   p->ino = st.st_ino;
344   p->dev = st.st_dev;
345   file_id_list = p;
346 }
347 \f
348 enum read_file_list_state  /* Result of reading file name from the list file */
349   {
350     file_list_success,     /* OK, name read successfully */
351     file_list_end,         /* End of list file */
352     file_list_zero,        /* Zero separator encountered where it should not */
353     file_list_skip         /* Empty (zero-length) entry encountered, skip it */
354   };
355
356 /* Read from FP a sequence of characters up to TERM and put them
357    into STK.
358  */
359 static enum read_file_list_state
360 read_name_from_file (struct name_elt *ent)
361 {
362   int c;
363   size_t counter = 0;
364   FILE *fp = ent->v.file.fp;
365   int term = ent->v.file.term;
366   size_t count;
367   
368   for (c = getc (fp); c != EOF && c != term; c = getc (fp))
369     {
370       if (count == name_buffer_length)
371         name_buffer = x2realloc (name_buffer, &name_buffer_length);
372       name_buffer[counter++] = c;
373       if (c == 0)
374         {
375           /* We have read a zero separator. The file possibly is
376              zero-separated */
377           return file_list_zero;
378         }
379     }
380
381   if (counter == 0 && c != EOF)
382     return file_list_skip;
383
384   if (count == name_buffer_length)
385     name_buffer = x2realloc (name_buffer, &name_buffer_length);
386   name_buffer[counter] = 0;
387
388   return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
389 }
390
391 static int
392 handle_option (const char *str)
393 {
394   struct wordsplit ws;
395   int i;
396   
397   while (*str && isspace (*str))
398     ;
399   if (*str != '-')
400     return 1;
401
402   ws.ws_offs = 1;
403   if (wordsplit (str, &ws, WRDSF_DEFFLAGS|WRDSF_DOOFFS))
404     FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
405                   str, wordsplit_strerror (&ws)));
406   ws.ws_wordv[0] = "tar";
407   more_options (ws.ws_wordc+ws.ws_offs, ws.ws_wordv);
408   for (i = 0; i < ws.ws_wordc+ws.ws_offs; i++)
409     ws.ws_wordv[i] = NULL;
410   
411   wordsplit_free (&ws);
412   return 0;
413 }
414
415 static int
416 read_next_name (struct name_elt *ent, struct name_elt *ret)
417 {
418   enum read_file_list_state read_state;
419
420   if (!ent->v.file.fp)
421     {
422       if (!strcmp (ent->v.file.name, "-"))
423         {
424           request_stdin ("-T");
425           ent->v.file.fp = stdin;
426         }
427       else
428         {
429           add_file_id (ent->v.file.name);
430           if ((ent->v.file.fp = fopen (ent->v.file.name, "r")) == NULL)
431             open_fatal (ent->v.file.name);
432         }
433     }
434   
435   while (1)
436     {
437       switch (read_name_from_file (ent))
438         {
439         case file_list_skip:
440           continue;
441           
442         case file_list_zero:
443           WARNOPT (WARN_FILENAME_WITH_NULS,
444                    (0, 0, N_("%s: file name read contains nul character"),
445                     quotearg_colon (ent->v.file.name)));
446           ent->v.file.term = 0;
447           /* fall through */
448         case file_list_success:
449           if (handle_option (name_buffer) == 0)
450             continue;
451           ret->type = NELT_NAME;
452           ret->v.name = name_buffer;
453           return 0;
454           
455         case file_list_end:
456           if (strcmp (ent->v.file.name, "-"))
457             fclose (ent->v.file.fp);
458           ent->v.file.fp = NULL;
459           return 1;
460         }
461     }
462 }             
463 \f
464 static void
465 copy_name (struct name_elt *ep)
466 {
467   const char *source;
468   size_t source_len;
469   char *cursor;
470
471   source = ep->v.name;
472   source_len = strlen (source);
473   if (name_buffer_length < source_len)
474     {
475       do
476         {
477           name_buffer_length *= 2;
478           if (! name_buffer_length)
479             xalloc_die ();
480         }
481       while (name_buffer_length < source_len);
482
483       free (name_buffer);
484       name_buffer = xmalloc(name_buffer_length + 2);
485     }
486   strcpy (name_buffer, source);
487
488   /* Zap trailing slashes.  */
489   cursor = name_buffer + strlen (name_buffer) - 1;
490   while (cursor > name_buffer && ISSLASH (*cursor))
491     *cursor-- = '\0';
492 }
493
494 \f
495 static int matching_flags; /* exclude_fnmatch options */
496
497 /* Get the next NELT_NAME element from name_array.  Result is in
498    static storage and can't be relied upon across two calls.
499
500    If CHANGE_DIRS is true, treat any entries of type NELT_CHDIR as
501    the request to change to the given directory.
502
503    Entries of type NELT_FMASK cause updates of the matching_flags
504    value. */
505 static struct name_elt *
506 name_next_elt (int change_dirs)
507 {
508   static struct name_elt entry;
509
510   while (scanned != entries)
511     {
512       struct name_elt *ep;
513
514       ep = &name_array[scanned];
515       if (ep->type == NELT_FMASK)
516         {
517           matching_flags = ep->v.matching_flags;
518           ++scanned;
519           continue;
520         }
521
522       switch (ep->type)
523         {
524         case NELT_FILE:
525           if (read_next_name (ep, &entry) == 0)
526             return &entry;
527           ++scanned;
528           continue;
529               
530         case NELT_CHDIR:
531           if (change_dirs)
532             {
533               ++scanned;
534               copy_name (ep);
535               if (chdir (name_buffer) < 0)
536                 chdir_fatal (name_buffer);
537               break;
538             }
539           /* fall trhough */
540         case NELT_NAME:
541           ++scanned;
542           copy_name (ep);
543           if (unquote_option)
544             unquote_string (name_buffer);
545           entry.type = ep->type;
546           entry.v.name = name_buffer;
547           return &entry;
548         }
549     }
550
551   return NULL;
552 }
553
554 const char *
555 name_next (int change_dirs)
556 {
557   struct name_elt *nelt = name_next_elt (change_dirs);
558   return nelt ? nelt->v.name : NULL;
559 }
560
561 /* Gather names in a list for scanning.  Could hash them later if we
562    really care.
563
564    If the names are already sorted to match the archive, we just read
565    them one by one.  name_gather reads the first one, and it is called
566    by name_match as appropriate to read the next ones.  At EOF, the
567    last name read is just left in the buffer.  This option lets users
568    of small machines extract an arbitrary number of files by doing
569    "tar t" and editing down the list of files.  */
570
571 void
572 name_gather (void)
573 {
574   /* Buffer able to hold a single name.  */
575   static struct name *buffer = NULL;
576
577   struct name_elt *ep;
578
579   if (same_order_option)
580     {
581       static int change_dir;
582
583       while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
584         change_dir = chdir_arg (xstrdup (ep->v.name));
585
586       if (ep)
587         {
588           free_name (buffer);
589           buffer = make_name (ep->v.name);
590           buffer->change_dir = change_dir;
591           buffer->next = 0;
592           buffer->found_count = 0;
593           buffer->matching_flags = matching_flags;
594           buffer->directory = NULL;
595           buffer->parent = NULL;
596           buffer->cmdline = true;
597
598           namelist = nametail = buffer;
599         }
600       else if (change_dir)
601         addname (0, change_dir, false, NULL);
602     }
603   else
604     {
605       /* Non sorted names -- read them all in.  */
606       int change_dir = 0;
607
608       for (;;)
609         {
610           int change_dir0 = change_dir;
611           while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
612             change_dir = chdir_arg (xstrdup (ep->v.name));
613
614           if (ep)
615             addname (ep->v.name, change_dir, true, NULL);
616           else
617             {
618               if (change_dir != change_dir0)
619                 addname (NULL, change_dir, false, NULL);
620               break;
621             }
622         }
623     }
624 }
625
626 /*  Add a name to the namelist.  */
627 struct name *
628 addname (char const *string, int change_dir, bool cmdline, struct name *parent)
629 {
630   struct name *name = make_name (string);
631
632   name->prev = nametail;
633   name->next = NULL;
634   name->found_count = 0;
635   name->matching_flags = matching_flags;
636   name->change_dir = change_dir;
637   name->directory = NULL;
638   name->parent = parent;
639   name->cmdline = cmdline;
640
641   if (nametail)
642     nametail->next = name;
643   else
644     namelist = name;
645   nametail = name;
646   return name;
647 }
648
649 /* Find a match for FILE_NAME (whose string length is LENGTH) in the name
650    list.  */
651 static struct name *
652 namelist_match (char const *file_name, size_t length)
653 {
654   struct name *p;
655
656   for (p = namelist; p; p = p->next)
657     {
658       if (p->name[0]
659           && exclude_fnmatch (p->name, file_name, p->matching_flags))
660         return p;
661     }
662
663   return NULL;
664 }
665
666 void
667 remname (struct name *name)
668 {
669   struct name *p;
670
671   if ((p = name->prev) != NULL)
672     p->next = name->next;
673   else
674     namelist = name->next;
675
676   if ((p = name->next) != NULL)
677     p->prev = name->prev;
678   else
679     nametail = name->prev;
680 }
681
682 /* Return true if and only if name FILE_NAME (from an archive) matches any
683    name from the namelist.  */
684 bool
685 name_match (const char *file_name)
686 {
687   size_t length = strlen (file_name);
688
689   while (1)
690     {
691       struct name *cursor = namelist;
692
693       if (!cursor)
694         return true;
695
696       if (cursor->name[0] == 0)
697         {
698           chdir_do (cursor->change_dir);
699           namelist = NULL;
700           nametail = NULL;
701           return true;
702         }
703
704       cursor = namelist_match (file_name, length);
705       if (cursor)
706         {
707           if (!(ISSLASH (file_name[cursor->length]) && recursion_option)
708               || cursor->found_count == 0)
709             cursor->found_count++; /* remember it matched */
710           if (starting_file_option)
711             {
712               free (namelist);
713               namelist = NULL;
714               nametail = NULL;
715             }
716           chdir_do (cursor->change_dir);
717
718           /* We got a match.  */
719           return ISFOUND (cursor);
720         }
721
722       /* Filename from archive not found in namelist.  If we have the whole
723          namelist here, just return 0.  Otherwise, read the next name in and
724          compare it.  If this was the last name, namelist->found_count will
725          remain on.  If not, we loop to compare the newly read name.  */
726
727       if (same_order_option && namelist->found_count)
728         {
729           name_gather ();       /* read one more */
730           if (namelist->found_count)
731             return false;
732         }
733       else
734         return false;
735     }
736 }
737
738 /* Returns true if all names from the namelist were processed.
739    P is the stat_info of the most recently processed entry.
740    The decision is postponed until the next entry is read if:
741
742    1) P ended with a slash (i.e. it was a directory)
743    2) P matches any entry from the namelist *and* represents a subdirectory
744    or a file lying under this entry (in the terms of directory structure).
745
746    This is necessary to handle contents of directories. */
747 bool
748 all_names_found (struct tar_stat_info *p)
749 {
750   struct name const *cursor;
751   size_t len;
752
753   if (!p->file_name || occurrence_option == 0 || p->had_trailing_slash)
754     return false;
755   len = strlen (p->file_name);
756   for (cursor = namelist; cursor; cursor = cursor->next)
757     {
758       if ((cursor->name[0] && !WASFOUND (cursor))
759           || (len >= cursor->length && ISSLASH (p->file_name[cursor->length])))
760         return false;
761     }
762   return true;
763 }
764
765 static int
766 regex_usage_warning (const char *name)
767 {
768   static int warned_once = 0;
769
770   if (warn_regex_usage && fnmatch_pattern_has_wildcards (name, 0))
771     {
772       warned_once = 1;
773       WARN ((0, 0,
774              _("Pattern matching characters used in file names")));
775       WARN ((0, 0,
776              _("Use --wildcards to enable pattern matching,"
777                " or --no-wildcards to suppress this warning")));
778     }
779   return warned_once;
780 }
781
782 /* Print the names of things in the namelist that were not matched.  */
783 void
784 names_notfound (void)
785 {
786   struct name const *cursor;
787
788   for (cursor = namelist; cursor; cursor = cursor->next)
789     if (!WASFOUND (cursor) && cursor->name[0])
790       {
791         regex_usage_warning (cursor->name);
792         ERROR ((0, 0,
793                 (cursor->found_count == 0) ?
794                      _("%s: Not found in archive") :
795                      _("%s: Required occurrence not found in archive"),
796                 quotearg_colon (cursor->name)));
797       }
798
799   /* Don't bother freeing the name list; we're about to exit.  */
800   namelist = NULL;
801   nametail = NULL;
802
803   if (same_order_option)
804     {
805       const char *name;
806
807       while ((name = name_next (1)) != NULL)
808         {
809           regex_usage_warning (name);
810           ERROR ((0, 0, _("%s: Not found in archive"),
811                   quotearg_colon (name)));
812         }
813     }
814 }
815
816 void
817 label_notfound (void)
818 {
819   struct name const *cursor;
820
821   if (!namelist)
822     return;
823
824   for (cursor = namelist; cursor; cursor = cursor->next)
825     if (WASFOUND (cursor))
826       return;
827
828   if (verbose_option)
829     error (0, 0, _("Archive label mismatch"));
830   set_exit_status (TAREXIT_DIFFERS);
831
832   for (cursor = namelist; cursor; cursor = cursor->next)
833     {
834       if (regex_usage_warning (cursor->name))
835         break;
836     }
837
838   /* Don't bother freeing the name list; we're about to exit.  */
839   namelist = NULL;
840   nametail = NULL;
841
842   if (same_order_option)
843     {
844       const char *name;
845
846       while ((name = name_next (1)) != NULL
847              && regex_usage_warning (name) == 0)
848         ;
849     }
850 }
851 \f
852 /* Sorting name lists.  */
853
854 /* Sort *singly* linked LIST of names, of given LENGTH, using COMPARE
855    to order names.  Return the sorted list.  Note that after calling
856    this function, the 'prev' links in list elements are messed up.
857
858    Apart from the type 'struct name' and the definition of SUCCESSOR,
859    this is a generic list-sorting function, but it's too painful to
860    make it both generic and portable
861    in C.  */
862
863 static struct name *
864 merge_sort_sll (struct name *list, int length,
865                 int (*compare) (struct name const*, struct name const*))
866 {
867   struct name *first_list;
868   struct name *second_list;
869   int first_length;
870   int second_length;
871   struct name *result;
872   struct name **merge_point;
873   struct name *cursor;
874   int counter;
875
876 # define SUCCESSOR(name) ((name)->next)
877
878   if (length == 1)
879     return list;
880
881   if (length == 2)
882     {
883       if ((*compare) (list, SUCCESSOR (list)) > 0)
884         {
885           result = SUCCESSOR (list);
886           SUCCESSOR (result) = list;
887           SUCCESSOR (list) = 0;
888           return result;
889         }
890       return list;
891     }
892
893   first_list = list;
894   first_length = (length + 1) / 2;
895   second_length = length / 2;
896   for (cursor = list, counter = first_length - 1;
897        counter;
898        cursor = SUCCESSOR (cursor), counter--)
899     continue;
900   second_list = SUCCESSOR (cursor);
901   SUCCESSOR (cursor) = 0;
902
903   first_list = merge_sort_sll (first_list, first_length, compare);
904   second_list = merge_sort_sll (second_list, second_length, compare);
905
906   merge_point = &result;
907   while (first_list && second_list)
908     if ((*compare) (first_list, second_list) < 0)
909       {
910         cursor = SUCCESSOR (first_list);
911         *merge_point = first_list;
912         merge_point = &SUCCESSOR (first_list);
913         first_list = cursor;
914       }
915     else
916       {
917         cursor = SUCCESSOR (second_list);
918         *merge_point = second_list;
919         merge_point = &SUCCESSOR (second_list);
920         second_list = cursor;
921       }
922   if (first_list)
923     *merge_point = first_list;
924   else
925     *merge_point = second_list;
926
927   return result;
928
929 #undef SUCCESSOR
930 }
931
932 /* Sort doubly linked LIST of names, of given LENGTH, using COMPARE
933    to order names.  Return the sorted list.  */
934 static struct name *
935 merge_sort (struct name *list, int length,
936             int (*compare) (struct name const*, struct name const*))
937 {
938   struct name *head, *p, *prev;
939   head = merge_sort_sll (list, length, compare);
940   /* Fixup prev pointers */
941   for (prev = NULL, p = head; p; prev = p, p = p->next)
942     p->prev = prev;
943   return head;
944 }
945
946 /* A comparison function for sorting names.  Put found names last;
947    break ties by string comparison.  */
948
949 static int
950 compare_names_found (struct name const *n1, struct name const *n2)
951 {
952   int found_diff = WASFOUND (n2) - WASFOUND (n1);
953   return found_diff ? found_diff : strcmp (n1->name, n2->name);
954 }
955
956 /* Simple comparison by names. */
957 static int
958 compare_names (struct name const *n1, struct name const *n2)
959 {
960   return strcmp (n1->name, n2->name);
961 }
962
963 \f
964 /* Add all the dirs under ST to the namelist NAME, descending the
965    directory hierarchy recursively.  */
966
967 static void
968 add_hierarchy_to_namelist (struct tar_stat_info *st, struct name *name)
969 {
970   const char *buffer;
971
972   name->directory = scan_directory (st);
973   buffer = directory_contents (name->directory);
974   if (buffer)
975     {
976       struct name *child_head = NULL, *child_tail = NULL;
977       size_t name_length = name->length;
978       size_t allocated_length = (name_length >= NAME_FIELD_SIZE
979                                  ? name_length + NAME_FIELD_SIZE
980                                  : NAME_FIELD_SIZE);
981       char *namebuf = xmalloc (allocated_length + 1);
982                                 /* FIXME: + 2 above?  */
983       const char *string;
984       size_t string_length;
985       int change_dir = name->change_dir;
986
987       strcpy (namebuf, name->name);
988       if (! ISSLASH (namebuf[name_length - 1]))
989         {
990           namebuf[name_length++] = '/';
991           namebuf[name_length] = '\0';
992         }
993
994       for (string = buffer; *string; string += string_length + 1)
995         {
996           string_length = strlen (string);
997           if (*string == 'D')
998             {
999               struct name *np;
1000               struct tar_stat_info subdir;
1001               int subfd;
1002
1003               if (allocated_length <= name_length + string_length)
1004                 {
1005                   do
1006                     {
1007                       allocated_length *= 2;
1008                       if (! allocated_length)
1009                         xalloc_die ();
1010                     }
1011                   while (allocated_length <= name_length + string_length);
1012
1013                   namebuf = xrealloc (namebuf, allocated_length + 1);
1014                 }
1015               strcpy (namebuf + name_length, string + 1);
1016               np = addname (namebuf, change_dir, false, name);
1017               if (!child_head)
1018                 child_head = np;
1019               else
1020                 child_tail->sibling = np;
1021               child_tail = np;
1022
1023               tar_stat_init (&subdir);
1024               subdir.parent = st;
1025               if (st->fd < 0)
1026                 {
1027                   subfd = -1;
1028                   errno = - st->fd;
1029                 }
1030               else
1031                 subfd = subfile_open (st, string + 1,
1032                                       open_read_flags | O_DIRECTORY);
1033               if (subfd < 0)
1034                 open_diag (namebuf);
1035               else
1036                 {
1037                   subdir.fd = subfd;
1038                   if (fstat (subfd, &subdir.stat) != 0)
1039                     stat_diag (namebuf);
1040                   else if (! (O_DIRECTORY || S_ISDIR (subdir.stat.st_mode)))
1041                     {
1042                       errno = ENOTDIR;
1043                       open_diag (namebuf);
1044                     }
1045                   else
1046                     {
1047                       subdir.orig_file_name = xstrdup (namebuf);
1048                       add_hierarchy_to_namelist (&subdir, np);
1049                       restore_parent_fd (&subdir);
1050                     }
1051                 }
1052
1053               tar_stat_destroy (&subdir);
1054             }
1055         }
1056
1057       free (namebuf);
1058       name->child = child_head;
1059     }
1060 }
1061 \f
1062 /* Auxiliary functions for hashed table of struct name's. */
1063
1064 static size_t
1065 name_hash (void const *entry, size_t n_buckets)
1066 {
1067   struct name const *name = entry;
1068   return hash_string (name->caname, n_buckets);
1069 }
1070
1071 /* Compare two directories for equality of their names. */
1072 static bool
1073 name_compare (void const *entry1, void const *entry2)
1074 {
1075   struct name const *name1 = entry1;
1076   struct name const *name2 = entry2;
1077   return strcmp (name1->caname, name2->caname) == 0;
1078 }
1079
1080 \f
1081 /* Rebase 'name' member of CHILD and all its siblings to
1082    the new PARENT. */
1083 static void
1084 rebase_child_list (struct name *child, struct name *parent)
1085 {
1086   size_t old_prefix_len = child->parent->length;
1087   size_t new_prefix_len = parent->length;
1088   char *new_prefix = parent->name;
1089
1090   for (; child; child = child->sibling)
1091     {
1092       size_t size = child->length - old_prefix_len + new_prefix_len;
1093       char *newp = xmalloc (size + 1);
1094       strcpy (newp, new_prefix);
1095       strcat (newp, child->name + old_prefix_len);
1096       free (child->name);
1097       child->name = newp;
1098       child->length = size;
1099
1100       rebase_directory (child->directory,
1101                         child->parent->name, old_prefix_len,
1102                         new_prefix, new_prefix_len);
1103     }
1104 }
1105
1106 /* Collect all the names from argv[] (or whatever), expand them into a
1107    directory tree, and sort them.  This gets only subdirectories, not
1108    all files.  */
1109
1110 void
1111 collect_and_sort_names (void)
1112 {
1113   struct name *name;
1114   struct name *next_name, *prev_name = NULL;
1115   int num_names;
1116   Hash_table *nametab;
1117
1118   name_gather ();
1119
1120   if (!namelist)
1121     addname (".", 0, false, NULL);
1122
1123   if (listed_incremental_option)
1124     {
1125       switch (chdir_count ())
1126         {
1127         case 0:
1128           break;
1129
1130         case 1:
1131           if (namelist->change_dir == 0)
1132             USAGE_ERROR ((0, 0,
1133                           _("Using -C option inside file list is not "
1134                             "allowed with --listed-incremental")));
1135           break;
1136
1137         default:
1138           USAGE_ERROR ((0, 0,
1139                         _("Only one -C option is allowed with "
1140                           "--listed-incremental")));
1141         }
1142
1143       read_directory_file ();
1144     }
1145
1146   num_names = 0;
1147   for (name = namelist; name; name = name->next, num_names++)
1148     {
1149       struct tar_stat_info st;
1150
1151       if (name->found_count || name->directory)
1152         continue;
1153       if (name->matching_flags & EXCLUDE_WILDCARDS)
1154         /* NOTE: EXCLUDE_ANCHORED is not relevant here */
1155         /* FIXME: just skip regexps for now */
1156         continue;
1157       chdir_do (name->change_dir);
1158
1159       if (name->name[0] == 0)
1160         continue;
1161
1162       tar_stat_init (&st);
1163
1164       if (deref_stat (name->name, &st.stat) != 0)
1165         {
1166           stat_diag (name->name);
1167           continue;
1168         }
1169       if (S_ISDIR (st.stat.st_mode))
1170         {
1171           int dir_fd = openat (chdir_fd, name->name,
1172                                open_read_flags | O_DIRECTORY);
1173           if (dir_fd < 0)
1174             open_diag (name->name);
1175           else
1176             {
1177               st.fd = dir_fd;
1178               if (fstat (dir_fd, &st.stat) != 0)
1179                 stat_diag (name->name);
1180               else if (O_DIRECTORY || S_ISDIR (st.stat.st_mode))
1181                 {
1182                   st.orig_file_name = xstrdup (name->name);
1183                   name->found_count++;
1184                   add_hierarchy_to_namelist (&st, name);
1185                 }
1186             }
1187         }
1188
1189       tar_stat_destroy (&st);
1190     }
1191
1192   namelist = merge_sort (namelist, num_names, compare_names);
1193
1194   num_names = 0;
1195   nametab = hash_initialize (0, 0,
1196                              name_hash,
1197                              name_compare, NULL);
1198   for (name = namelist; name; name = next_name)
1199     {
1200       next_name = name->next;
1201       name->caname = normalize_filename (name->name);
1202       if (prev_name)
1203         {
1204           struct name *p = hash_lookup (nametab, name);
1205           if (p)
1206             {
1207               /* Keep the one listed in the command line */
1208               if (!name->parent)
1209                 {
1210                   if (p->child)
1211                     rebase_child_list (p->child, name);
1212                   hash_delete (nametab, name);
1213                   /* FIXME: remove_directory (p->caname); ? */
1214                   remname (p);
1215                   free_name (p);
1216                   num_names--;
1217                 }
1218               else
1219                 {
1220                   if (name->child)
1221                     rebase_child_list (name->child, p);
1222                   /* FIXME: remove_directory (name->caname); ? */
1223                   remname (name);
1224                   free_name (name);
1225                   continue;
1226                 }
1227             }
1228         }
1229       name->found_count = 0;
1230       if (!hash_insert (nametab, name))
1231         xalloc_die ();
1232       prev_name = name;
1233       num_names++;
1234     }
1235   nametail = prev_name;
1236   hash_free (nametab);
1237
1238   namelist = merge_sort (namelist, num_names, compare_names_found);
1239
1240   if (listed_incremental_option)
1241     {
1242       for (name = namelist; name && name->name[0] == 0; name++)
1243         ;
1244       if (name)
1245         append_incremental_renames (name->directory);
1246     }
1247 }
1248
1249 /* This is like name_match, except that
1250     1. It returns a pointer to the name it matched, and doesn't set FOUND
1251     in structure. The caller will have to do that if it wants to.
1252     2. If the namelist is empty, it returns null, unlike name_match, which
1253     returns TRUE. */
1254 struct name *
1255 name_scan (const char *file_name)
1256 {
1257   size_t length = strlen (file_name);
1258
1259   while (1)
1260     {
1261       struct name *cursor = namelist_match (file_name, length);
1262       if (cursor)
1263         return cursor;
1264
1265       /* Filename from archive not found in namelist.  If we have the whole
1266          namelist here, just return 0.  Otherwise, read the next name in and
1267          compare it.  If this was the last name, namelist->found_count will
1268          remain on.  If not, we loop to compare the newly read name.  */
1269
1270       if (same_order_option && namelist && namelist->found_count)
1271         {
1272           name_gather ();       /* read one more */
1273           if (namelist->found_count)
1274             return 0;
1275         }
1276       else
1277         return 0;
1278     }
1279 }
1280
1281 /* This returns a name from the namelist which doesn't have ->found
1282    set.  It sets ->found before returning, so successive calls will
1283    find and return all the non-found names in the namelist.  */
1284 struct name *gnu_list_name;
1285
1286 struct name const *
1287 name_from_list (void)
1288 {
1289   if (!gnu_list_name)
1290     gnu_list_name = namelist;
1291   while (gnu_list_name
1292          && (gnu_list_name->found_count || gnu_list_name->name[0] == 0))
1293     gnu_list_name = gnu_list_name->next;
1294   if (gnu_list_name)
1295     {
1296       gnu_list_name->found_count++;
1297       chdir_do (gnu_list_name->change_dir);
1298       return gnu_list_name;
1299     }
1300   return NULL;
1301 }
1302
1303 void
1304 blank_name_list (void)
1305 {
1306   struct name *name;
1307
1308   gnu_list_name = 0;
1309   for (name = namelist; name; name = name->next)
1310     name->found_count = 0;
1311 }
1312
1313 /* Yield a newly allocated file name consisting of FILE_NAME concatenated to
1314    NAME, with an intervening slash if FILE_NAME does not already end in one. */
1315 char *
1316 new_name (const char *file_name, const char *name)
1317 {
1318   size_t file_name_len = strlen (file_name);
1319   size_t namesize = strlen (name) + 1;
1320   int slash = file_name_len && ! ISSLASH (file_name[file_name_len - 1]);
1321   char *buffer = xmalloc (file_name_len + slash + namesize);
1322   memcpy (buffer, file_name, file_name_len);
1323   buffer[file_name_len] = '/';
1324   memcpy (buffer + file_name_len + slash, name, namesize);
1325   return buffer;
1326 }
1327
1328 /* Return nonzero if file NAME is excluded.  */
1329 bool
1330 excluded_name (char const *name)
1331 {
1332   return excluded_file_name (excluded, name + FILE_SYSTEM_PREFIX_LEN (name));
1333 }
1334 \f
1335
1336 /* Return the size of the prefix of FILE_NAME that is removed after
1337    stripping NUM leading file name components.  NUM must be
1338    positive.  */
1339
1340 size_t
1341 stripped_prefix_len (char const *file_name, size_t num)
1342 {
1343   char const *p = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
1344   while (ISSLASH (*p))
1345     p++;
1346   while (*p)
1347     {
1348       bool slash = ISSLASH (*p);
1349       p++;
1350       if (slash)
1351         {
1352           if (--num == 0)
1353             return p - file_name;
1354           while (ISSLASH (*p))
1355             p++;
1356         }
1357     }
1358   return -1;
1359 }
1360 \f
1361 /* Return nonzero if NAME contains ".." as a file name component.  */
1362 bool
1363 contains_dot_dot (char const *name)
1364 {
1365   char const *p = name + FILE_SYSTEM_PREFIX_LEN (name);
1366
1367   for (;; p++)
1368     {
1369       if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
1370         return 1;
1371
1372       while (! ISSLASH (*p))
1373         {
1374           if (! *p++)
1375             return 0;
1376         }
1377     }
1378 }