xref: /freebsd-src/contrib/diff/src/dir.c (revision 18fd37a72c3a7549d2d4f6c6ea00bdcd2bdaca01)
1*18fd37a7SXin LI /* Read, sort and compare two directories.  Used for GNU DIFF.
2*18fd37a7SXin LI 
3*18fd37a7SXin LI    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002,
4*18fd37a7SXin LI    2004 Free Software Foundation, Inc.
5*18fd37a7SXin LI 
6*18fd37a7SXin LI    This file is part of GNU DIFF.
7*18fd37a7SXin LI 
8*18fd37a7SXin LI    GNU DIFF is free software; you can redistribute it and/or modify
9*18fd37a7SXin LI    it under the terms of the GNU General Public License as published by
10*18fd37a7SXin LI    the Free Software Foundation; either version 2, or (at your option)
11*18fd37a7SXin LI    any later version.
12*18fd37a7SXin LI 
13*18fd37a7SXin LI    GNU DIFF is distributed in the hope that it will be useful,
14*18fd37a7SXin LI    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*18fd37a7SXin LI    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*18fd37a7SXin LI    GNU General Public License for more details.
17*18fd37a7SXin LI 
18*18fd37a7SXin LI    You should have received a copy of the GNU General Public License
19*18fd37a7SXin LI    along with this program; see the file COPYING.
20*18fd37a7SXin LI    If not, write to the Free Software Foundation,
21*18fd37a7SXin LI    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22*18fd37a7SXin LI 
23*18fd37a7SXin LI #include "diff.h"
24*18fd37a7SXin LI #include <error.h>
25*18fd37a7SXin LI #include <exclude.h>
26*18fd37a7SXin LI #include <setjmp.h>
27*18fd37a7SXin LI #include <strcase.h>
28*18fd37a7SXin LI #include <xalloc.h>
29*18fd37a7SXin LI 
30*18fd37a7SXin LI /* Read the directory named by DIR and store into DIRDATA a sorted vector
31*18fd37a7SXin LI    of filenames for its contents.  DIR->desc == -1 means this directory is
32*18fd37a7SXin LI    known to be nonexistent, so set DIRDATA to an empty vector.
33*18fd37a7SXin LI    Return -1 (setting errno) if error, 0 otherwise.  */
34*18fd37a7SXin LI 
35*18fd37a7SXin LI struct dirdata
36*18fd37a7SXin LI {
37*18fd37a7SXin LI   size_t nnames;	/* Number of names.  */
38*18fd37a7SXin LI   char const **names;	/* Sorted names of files in dir, followed by 0.  */
39*18fd37a7SXin LI   char *data;	/* Allocated storage for file names.  */
40*18fd37a7SXin LI };
41*18fd37a7SXin LI 
42*18fd37a7SXin LI /* Whether file names in directories should be compared with
43*18fd37a7SXin LI    locale-specific sorting.  */
44*18fd37a7SXin LI static bool locale_specific_sorting;
45*18fd37a7SXin LI 
46*18fd37a7SXin LI /* Where to go if locale-specific sorting fails.  */
47*18fd37a7SXin LI static jmp_buf failed_locale_specific_sorting;
48*18fd37a7SXin LI 
49*18fd37a7SXin LI static bool dir_loop (struct comparison const *, int);
50*18fd37a7SXin LI static int compare_names_for_qsort (void const *, void const *);
51*18fd37a7SXin LI 
52*18fd37a7SXin LI 
53*18fd37a7SXin LI /* Read a directory and get its vector of names.  */
54*18fd37a7SXin LI 
55*18fd37a7SXin LI static bool
dir_read(struct file_data const * dir,struct dirdata * dirdata)56*18fd37a7SXin LI dir_read (struct file_data const *dir, struct dirdata *dirdata)
57*18fd37a7SXin LI {
58*18fd37a7SXin LI   register struct dirent *next;
59*18fd37a7SXin LI   register size_t i;
60*18fd37a7SXin LI 
61*18fd37a7SXin LI   /* Address of block containing the files that are described.  */
62*18fd37a7SXin LI   char const **names;
63*18fd37a7SXin LI 
64*18fd37a7SXin LI   /* Number of files in directory.  */
65*18fd37a7SXin LI   size_t nnames;
66*18fd37a7SXin LI 
67*18fd37a7SXin LI   /* Allocated and used storage for file name data.  */
68*18fd37a7SXin LI   char *data;
69*18fd37a7SXin LI   size_t data_alloc, data_used;
70*18fd37a7SXin LI 
71*18fd37a7SXin LI   dirdata->names = 0;
72*18fd37a7SXin LI   dirdata->data = 0;
73*18fd37a7SXin LI   nnames = 0;
74*18fd37a7SXin LI   data = 0;
75*18fd37a7SXin LI 
76*18fd37a7SXin LI   if (dir->desc != -1)
77*18fd37a7SXin LI     {
78*18fd37a7SXin LI       /* Open the directory and check for errors.  */
79*18fd37a7SXin LI       register DIR *reading = opendir (dir->name);
80*18fd37a7SXin LI       if (!reading)
81*18fd37a7SXin LI 	return false;
82*18fd37a7SXin LI 
83*18fd37a7SXin LI       /* Initialize the table of filenames.  */
84*18fd37a7SXin LI 
85*18fd37a7SXin LI       data_alloc = 512;
86*18fd37a7SXin LI       data_used = 0;
87*18fd37a7SXin LI       dirdata->data = data = xmalloc (data_alloc);
88*18fd37a7SXin LI 
89*18fd37a7SXin LI       /* Read the directory entries, and insert the subfiles
90*18fd37a7SXin LI 	 into the `data' table.  */
91*18fd37a7SXin LI 
92*18fd37a7SXin LI       while ((errno = 0, (next = readdir (reading)) != 0))
93*18fd37a7SXin LI 	{
94*18fd37a7SXin LI 	  char *d_name = next->d_name;
95*18fd37a7SXin LI 	  size_t d_size = NAMLEN (next) + 1;
96*18fd37a7SXin LI 
97*18fd37a7SXin LI 	  /* Ignore "." and "..".  */
98*18fd37a7SXin LI 	  if (d_name[0] == '.'
99*18fd37a7SXin LI 	      && (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0)))
100*18fd37a7SXin LI 	    continue;
101*18fd37a7SXin LI 
102*18fd37a7SXin LI 	  if (excluded_filename (excluded, d_name))
103*18fd37a7SXin LI 	    continue;
104*18fd37a7SXin LI 
105*18fd37a7SXin LI 	  while (data_alloc < data_used + d_size)
106*18fd37a7SXin LI 	    {
107*18fd37a7SXin LI 	      if (PTRDIFF_MAX / 2 <= data_alloc)
108*18fd37a7SXin LI 		xalloc_die ();
109*18fd37a7SXin LI 	      dirdata->data = data = xrealloc (data, data_alloc *= 2);
110*18fd37a7SXin LI 	    }
111*18fd37a7SXin LI 
112*18fd37a7SXin LI 	  memcpy (data + data_used, d_name, d_size);
113*18fd37a7SXin LI 	  data_used += d_size;
114*18fd37a7SXin LI 	  nnames++;
115*18fd37a7SXin LI 	}
116*18fd37a7SXin LI       if (errno)
117*18fd37a7SXin LI 	{
118*18fd37a7SXin LI 	  int e = errno;
119*18fd37a7SXin LI 	  closedir (reading);
120*18fd37a7SXin LI 	  errno = e;
121*18fd37a7SXin LI 	  return false;
122*18fd37a7SXin LI 	}
123*18fd37a7SXin LI #if CLOSEDIR_VOID
124*18fd37a7SXin LI       closedir (reading);
125*18fd37a7SXin LI #else
126*18fd37a7SXin LI       if (closedir (reading) != 0)
127*18fd37a7SXin LI 	return false;
128*18fd37a7SXin LI #endif
129*18fd37a7SXin LI     }
130*18fd37a7SXin LI 
131*18fd37a7SXin LI   /* Create the `names' table from the `data' table.  */
132*18fd37a7SXin LI   if (PTRDIFF_MAX / sizeof *names - 1 <= nnames)
133*18fd37a7SXin LI     xalloc_die ();
134*18fd37a7SXin LI   dirdata->names = names = xmalloc ((nnames + 1) * sizeof *names);
135*18fd37a7SXin LI   dirdata->nnames = nnames;
136*18fd37a7SXin LI   for (i = 0;  i < nnames;  i++)
137*18fd37a7SXin LI     {
138*18fd37a7SXin LI       names[i] = data;
139*18fd37a7SXin LI       data += strlen (data) + 1;
140*18fd37a7SXin LI     }
141*18fd37a7SXin LI   names[nnames] = 0;
142*18fd37a7SXin LI   return true;
143*18fd37a7SXin LI }
144*18fd37a7SXin LI 
145*18fd37a7SXin LI /* Compare file names, returning a value compatible with strcmp.  */
146*18fd37a7SXin LI 
147*18fd37a7SXin LI static int
compare_names(char const * name1,char const * name2)148*18fd37a7SXin LI compare_names (char const *name1, char const *name2)
149*18fd37a7SXin LI {
150*18fd37a7SXin LI   if (locale_specific_sorting)
151*18fd37a7SXin LI     {
152*18fd37a7SXin LI       int r;
153*18fd37a7SXin LI       errno = 0;
154*18fd37a7SXin LI       if (ignore_file_name_case)
155*18fd37a7SXin LI 	r = strcasecoll (name1, name2);
156*18fd37a7SXin LI       else
157*18fd37a7SXin LI 	r = strcoll (name1, name2);
158*18fd37a7SXin LI       if (errno)
159*18fd37a7SXin LI 	{
160*18fd37a7SXin LI 	  error (0, errno, _("cannot compare file names `%s' and `%s'"),
161*18fd37a7SXin LI 		 name1, name2);
162*18fd37a7SXin LI 	  longjmp (failed_locale_specific_sorting, 1);
163*18fd37a7SXin LI 	}
164*18fd37a7SXin LI       return r;
165*18fd37a7SXin LI     }
166*18fd37a7SXin LI 
167*18fd37a7SXin LI   return (ignore_file_name_case
168*18fd37a7SXin LI 	  ? strcasecmp (name1, name2)
169*18fd37a7SXin LI 	  : file_name_cmp (name1, name2));
170*18fd37a7SXin LI }
171*18fd37a7SXin LI 
172*18fd37a7SXin LI /* A wrapper for compare_names suitable as an argument for qsort.  */
173*18fd37a7SXin LI 
174*18fd37a7SXin LI static int
compare_names_for_qsort(void const * file1,void const * file2)175*18fd37a7SXin LI compare_names_for_qsort (void const *file1, void const *file2)
176*18fd37a7SXin LI {
177*18fd37a7SXin LI   char const *const *f1 = file1;
178*18fd37a7SXin LI   char const *const *f2 = file2;
179*18fd37a7SXin LI   return compare_names (*f1, *f2);
180*18fd37a7SXin LI }
181*18fd37a7SXin LI 
182*18fd37a7SXin LI /* Compare the contents of two directories named in CMP.
183*18fd37a7SXin LI    This is a top-level routine; it does everything necessary for diff
184*18fd37a7SXin LI    on two directories.
185*18fd37a7SXin LI 
186*18fd37a7SXin LI    CMP->file[0].desc == -1 says directory CMP->file[0] doesn't exist,
187*18fd37a7SXin LI    but pretend it is empty.  Likewise for CMP->file[1].
188*18fd37a7SXin LI 
189*18fd37a7SXin LI    HANDLE_FILE is a caller-provided subroutine called to handle each file.
190*18fd37a7SXin LI    It gets three operands: CMP, name of file in dir 0, name of file in dir 1.
191*18fd37a7SXin LI    These names are relative to the original working directory.
192*18fd37a7SXin LI 
193*18fd37a7SXin LI    For a file that appears in only one of the dirs, one of the name-args
194*18fd37a7SXin LI    to HANDLE_FILE is zero.
195*18fd37a7SXin LI 
196*18fd37a7SXin LI    Returns the maximum of all the values returned by HANDLE_FILE,
197*18fd37a7SXin LI    or EXIT_TROUBLE if trouble is encountered in opening files.  */
198*18fd37a7SXin LI 
199*18fd37a7SXin LI int
diff_dirs(struct comparison const * cmp,int (* handle_file)(struct comparison const *,char const *,char const *))200*18fd37a7SXin LI diff_dirs (struct comparison const *cmp,
201*18fd37a7SXin LI 	   int (*handle_file) (struct comparison const *,
202*18fd37a7SXin LI 			       char const *, char const *))
203*18fd37a7SXin LI {
204*18fd37a7SXin LI   struct dirdata dirdata[2];
205*18fd37a7SXin LI   int volatile val = EXIT_SUCCESS;
206*18fd37a7SXin LI   int i;
207*18fd37a7SXin LI 
208*18fd37a7SXin LI   if ((cmp->file[0].desc == -1 || dir_loop (cmp, 0))
209*18fd37a7SXin LI       && (cmp->file[1].desc == -1 || dir_loop (cmp, 1)))
210*18fd37a7SXin LI     {
211*18fd37a7SXin LI       error (0, 0, "%s: recursive directory loop",
212*18fd37a7SXin LI 	     cmp->file[cmp->file[0].desc == -1].name);
213*18fd37a7SXin LI       return EXIT_TROUBLE;
214*18fd37a7SXin LI     }
215*18fd37a7SXin LI 
216*18fd37a7SXin LI   /* Get contents of both dirs.  */
217*18fd37a7SXin LI   for (i = 0; i < 2; i++)
218*18fd37a7SXin LI     if (! dir_read (&cmp->file[i], &dirdata[i]))
219*18fd37a7SXin LI       {
220*18fd37a7SXin LI 	perror_with_name (cmp->file[i].name);
221*18fd37a7SXin LI 	val = EXIT_TROUBLE;
222*18fd37a7SXin LI       }
223*18fd37a7SXin LI 
224*18fd37a7SXin LI   if (val == EXIT_SUCCESS)
225*18fd37a7SXin LI     {
226*18fd37a7SXin LI       char const **volatile names[2];
227*18fd37a7SXin LI       names[0] = dirdata[0].names;
228*18fd37a7SXin LI       names[1] = dirdata[1].names;
229*18fd37a7SXin LI 
230*18fd37a7SXin LI       /* Use locale-specific sorting if possible, else native byte order.  */
231*18fd37a7SXin LI       locale_specific_sorting = true;
232*18fd37a7SXin LI       if (setjmp (failed_locale_specific_sorting))
233*18fd37a7SXin LI 	locale_specific_sorting = false;
234*18fd37a7SXin LI 
235*18fd37a7SXin LI       /* Sort the directories.  */
236*18fd37a7SXin LI       for (i = 0; i < 2; i++)
237*18fd37a7SXin LI 	qsort (names[i], dirdata[i].nnames, sizeof *dirdata[i].names,
238*18fd37a7SXin LI 	       compare_names_for_qsort);
239*18fd37a7SXin LI 
240*18fd37a7SXin LI       /* If `-S name' was given, and this is the topmost level of comparison,
241*18fd37a7SXin LI 	 ignore all file names less than the specified starting name.  */
242*18fd37a7SXin LI 
243*18fd37a7SXin LI       if (starting_file && ! cmp->parent)
244*18fd37a7SXin LI 	{
245*18fd37a7SXin LI 	  while (*names[0] && compare_names (*names[0], starting_file) < 0)
246*18fd37a7SXin LI 	    names[0]++;
247*18fd37a7SXin LI 	  while (*names[1] && compare_names (*names[1], starting_file) < 0)
248*18fd37a7SXin LI 	    names[1]++;
249*18fd37a7SXin LI 	}
250*18fd37a7SXin LI 
251*18fd37a7SXin LI       /* Loop while files remain in one or both dirs.  */
252*18fd37a7SXin LI       while (*names[0] || *names[1])
253*18fd37a7SXin LI 	{
254*18fd37a7SXin LI 	  /* Compare next name in dir 0 with next name in dir 1.
255*18fd37a7SXin LI 	     At the end of a dir,
256*18fd37a7SXin LI 	     pretend the "next name" in that dir is very large.  */
257*18fd37a7SXin LI 	  int nameorder = (!*names[0] ? 1 : !*names[1] ? -1
258*18fd37a7SXin LI 			   : compare_names (*names[0], *names[1]));
259*18fd37a7SXin LI 	  int v1 = (*handle_file) (cmp,
260*18fd37a7SXin LI 				   0 < nameorder ? 0 : *names[0]++,
261*18fd37a7SXin LI 				   nameorder < 0 ? 0 : *names[1]++);
262*18fd37a7SXin LI 	  if (val < v1)
263*18fd37a7SXin LI 	    val = v1;
264*18fd37a7SXin LI 	}
265*18fd37a7SXin LI     }
266*18fd37a7SXin LI 
267*18fd37a7SXin LI   for (i = 0; i < 2; i++)
268*18fd37a7SXin LI     {
269*18fd37a7SXin LI       if (dirdata[i].names)
270*18fd37a7SXin LI 	free (dirdata[i].names);
271*18fd37a7SXin LI       if (dirdata[i].data)
272*18fd37a7SXin LI 	free (dirdata[i].data);
273*18fd37a7SXin LI     }
274*18fd37a7SXin LI 
275*18fd37a7SXin LI   return val;
276*18fd37a7SXin LI }
277*18fd37a7SXin LI 
278*18fd37a7SXin LI /* Return nonzero if CMP is looping recursively in argument I.  */
279*18fd37a7SXin LI 
280*18fd37a7SXin LI static bool
dir_loop(struct comparison const * cmp,int i)281*18fd37a7SXin LI dir_loop (struct comparison const *cmp, int i)
282*18fd37a7SXin LI {
283*18fd37a7SXin LI   struct comparison const *p = cmp;
284*18fd37a7SXin LI   while ((p = p->parent))
285*18fd37a7SXin LI     if (0 < same_file (&p->file[i].stat, &cmp->file[i].stat))
286*18fd37a7SXin LI       return true;
287*18fd37a7SXin LI   return false;
288*18fd37a7SXin LI }
289