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