source: trunk/diffutils/src/dir.c@ 2749

Last change on this file since 2749 was 2556, checked in by bird, 19 years ago

diffutils 2.8.1

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