xref: /freebsd/usr.bin/diff/diffdir.c (revision 0fc6c3f731a2cca3120798806c330a3081c9424b)
1 /*	$OpenBSD: diffdir.c,v 1.45 2015/10/05 20:15:00 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, 2010 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * Sponsored in part by the Defense Advanced Research Projects
19  * Agency (DARPA) and Air Force Research Laboratory, Air Force
20  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21  */
22 
23 #include <sys/stat.h>
24 #include <sys/tree.h>
25 
26 #include <dirent.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <fnmatch.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include "diff.h"
38 
39 static int selectfile(const struct dirent *);
40 static void diffit(struct dirent *, char *, size_t, struct dirent *,
41 	char *, size_t, int);
42 static void print_only(const char *, size_t, const char *);
43 
44 struct inode {
45 	dev_t dev;
46 	ino_t ino;
47 	RB_ENTRY(inode) entry;
48 };
49 
50 static int
51 inodecmp(struct inode *a, struct inode *b)
52 {
53 	return (a->dev < b->dev ? -1 : a->dev > b->dev ? 1 :
54 	    a->ino < b->ino ? -1 : a->ino > b->ino ? 1 : 0);
55 }
56 
57 RB_HEAD(inodetree, inode);
58 static struct inodetree v1 = RB_INITIALIZER(&v1);
59 static struct inodetree v2 = RB_INITIALIZER(&v2);
60 RB_GENERATE_STATIC(inodetree, inode, entry, inodecmp);
61 
62 static int
63 vscandir(struct inodetree *tree, struct inode **inop,
64     const char *path, struct dirent ***dirp,
65     int (*selectf)(const struct dirent *),
66     int (*comparf)(const struct dirent **, const struct dirent **))
67 {
68 	struct stat sb;
69 	struct inode *ino = NULL;
70 	int fd = -1, ret, serrno;
71 
72 	if ((fd = open(path, O_DIRECTORY | O_RDONLY)) < 0 ||
73 	    (ino = calloc(1, sizeof(*ino))) == NULL ||
74 	    fstat(fd, &sb) != 0)
75 		goto fail;
76 	ino->dev = sb.st_dev;
77 	ino->ino = sb.st_ino;
78 	if (RB_FIND(inodetree, tree, ino)) {
79 		free(ino);
80 		close(fd);
81 		warnx("%s: Directory loop detected", path);
82 		*dirp = NULL;
83 		return (0);
84 	}
85 	if ((ret = fdscandir(fd, dirp, selectf, comparf)) < 0)
86 		goto fail;
87 	RB_INSERT(inodetree, tree, ino);
88 	close(fd);
89 	*inop = ino;
90 	return (ret);
91 fail:
92 	serrno = errno;
93 	if (ino != NULL)
94 		free(ino);
95 	if (fd >= 0)
96 		close(fd);
97 	errno = serrno;
98 	return (-1);
99 }
100 
101 static void
102 leavedir(struct inodetree *tree, struct inode *ino)
103 {
104 	RB_REMOVE(inodetree, tree, ino);
105 	free(ino);
106 }
107 
108 /*
109  * Diff directory traversal. Will be called recursively if -r was specified.
110  */
111 void
112 diffdir(char *p1, char *p2, int flags)
113 {
114 	struct dirent *dent1, **dp1, **edp1, **dirp1 = NULL;
115 	struct dirent *dent2, **dp2, **edp2, **dirp2 = NULL;
116 	struct inode *ino1 = NULL, *ino2 = NULL;
117 	size_t dirlen1, dirlen2;
118 	char path1[PATH_MAX], path2[PATH_MAX];
119 	int pos;
120 
121 	edp1 = edp2 = NULL;
122 
123 	dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
124 	if (dirlen1 >= sizeof(path1) - 1) {
125 		warnc(ENAMETOOLONG, "%s", p1);
126 		status |= 2;
127 		return;
128 	}
129 	while (dirlen1 > 1 && path1[dirlen1 - 1] == '/')
130 		path1[--dirlen1] = '\0';
131 	dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
132 	if (dirlen2 >= sizeof(path2) - 1) {
133 		warnc(ENAMETOOLONG, "%s", p2);
134 		status |= 2;
135 		return;
136 	}
137 	while (dirlen2 > 1 && path2[dirlen2 - 1] == '/')
138 		path2[--dirlen2] = '\0';
139 
140 	/*
141 	 * Get a list of entries in each directory, skipping "excluded" files
142 	 * and sorting alphabetically.
143 	 */
144 	pos = vscandir(&v1, &ino1, path1, &dirp1, selectfile, alphasort);
145 	if (pos == -1) {
146 		if (errno == ENOENT && (Nflag || Pflag)) {
147 			pos = 0;
148 		} else {
149 			warn("%s", path1);
150 			goto closem;
151 		}
152 	}
153 	dp1 = dirp1;
154 	edp1 = dirp1 + pos;
155 
156 	pos = vscandir(&v2, &ino2, path2, &dirp2, selectfile, alphasort);
157 	if (pos == -1) {
158 		if (errno == ENOENT && Nflag) {
159 			pos = 0;
160 		} else {
161 			warn("%s", path2);
162 			goto closem;
163 		}
164 	}
165 	dp2 = dirp2;
166 	edp2 = dirp2 + pos;
167 
168 	/*
169 	 * If we were given a starting point, find it.
170 	 */
171 	if (start != NULL) {
172 		while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0)
173 			dp1++;
174 		while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0)
175 			dp2++;
176 	}
177 
178 	/*
179 	 * Append separator so children's names can be appended directly.
180 	 */
181 	if (path1[dirlen1 - 1] != '/') {
182 		path1[dirlen1++] = '/';
183 		path1[dirlen1] = '\0';
184 	}
185 	if (path2[dirlen2 - 1] != '/') {
186 		path2[dirlen2++] = '/';
187 		path2[dirlen2] = '\0';
188 	}
189 
190 	/*
191 	 * Iterate through the two directory lists, diffing as we go.
192 	 */
193 	while (dp1 != edp1 || dp2 != edp2) {
194 		dent1 = dp1 != edp1 ? *dp1 : NULL;
195 		dent2 = dp2 != edp2 ? *dp2 : NULL;
196 
197 		pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
198 		    ignore_file_case ? strcasecmp(dent1->d_name, dent2->d_name) :
199 		    strcmp(dent1->d_name, dent2->d_name) ;
200 		if (pos == 0) {
201 			/* file exists in both dirs, diff it */
202 			diffit(dent1, path1, dirlen1, dent2, path2, dirlen2, flags);
203 			dp1++;
204 			dp2++;
205 		} else if (pos < 0) {
206 			/* file only in first dir, only diff if -N */
207 			if (Nflag) {
208 				diffit(dent1, path1, dirlen1, dent2, path2,
209 					dirlen2, flags);
210 			} else {
211 				print_only(path1, dirlen1, dent1->d_name);
212 				status |= 1;
213 			}
214 			dp1++;
215 		} else {
216 			/* file only in second dir, only diff if -N or -P */
217 			if (Nflag || Pflag)
218 				diffit(dent2, path1, dirlen1, dent1, path2,
219 					dirlen2, flags);
220 			else {
221 				print_only(path2, dirlen2, dent2->d_name);
222 				status |= 1;
223 			}
224 			dp2++;
225 		}
226 	}
227 
228 closem:
229 	if (dirp1 != NULL) {
230 		if (ino1 != NULL)
231 			leavedir(&v1, ino1);
232 		for (dp1 = dirp1; dp1 < edp1; dp1++)
233 			free(*dp1);
234 		free(dirp1);
235 	}
236 	if (dirp2 != NULL) {
237 		if (ino2 != NULL)
238 			leavedir(&v2, ino2);
239 		for (dp2 = dirp2; dp2 < edp2; dp2++)
240 			free(*dp2);
241 		free(dirp2);
242 	}
243 }
244 
245 /*
246  * Do the actual diff by calling either diffreg() or diffdir().
247  */
248 static void
249 diffit(struct dirent *dp, char *path1, size_t plen1, struct dirent *dp2,
250 	char *path2, size_t plen2, int flags)
251 {
252 	int rc;
253 
254 	flags |= D_HEADER;
255 	strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
256 
257 	/*
258 	 * If we are ignoring file case, use dent2s name here if both names are
259 	 * the same apart from case.
260 	 */
261 	if (ignore_file_case && strcasecmp(dp2->d_name, dp2->d_name) == 0)
262 		strlcpy(path2 + plen2, dp2->d_name, PATH_MAX - plen2);
263 	else
264 		strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2);
265 
266 	if (noderef) {
267 		if (lstat(path1, &stb1) != 0) {
268 			if (!(Nflag || Pflag) || errno != ENOENT) {
269 				warn("%s", path1);
270 				return;
271 			}
272 			flags |= D_EMPTY1;
273 			memset(&stb1, 0, sizeof(stb1));
274 		}
275 		if (lstat(path2, &stb2) != 0) {
276 			if (!Nflag || errno != ENOENT) {
277 				warn("%s", path2);
278 				return;
279 			}
280 			flags |= D_EMPTY2;
281 			memset(&stb2, 0, sizeof(stb2));
282 			stb2.st_mode = stb1.st_mode;
283 		}
284 		if (stb1.st_mode == 0)
285 			stb1.st_mode = stb2.st_mode;
286 		if (S_ISLNK(stb1.st_mode) || S_ISLNK(stb2.st_mode)) {
287 			if  (S_ISLNK(stb1.st_mode) && S_ISLNK(stb2.st_mode)) {
288 				char buf1[PATH_MAX];
289 				char buf2[PATH_MAX];
290 				ssize_t len1 = 0;
291 				ssize_t len2 = 0;
292 
293 				len1 = readlink(path1, buf1, sizeof(buf1));
294 				len2 = readlink(path2, buf2, sizeof(buf2));
295 
296 				if (len1 < 0 || len2 < 0) {
297 					perror("reading links");
298 					return;
299 				}
300 				buf1[len1] = '\0';
301 				buf2[len2] = '\0';
302 
303 				if (len1 != len2 || strncmp(buf1, buf2, len1) != 0) {
304 					printf("Symbolic links %s and %s differ\n",
305 						path1, path2);
306 					status |= 1;
307 				}
308 
309 				return;
310 			}
311 
312 			printf("File %s is a %s while file %s is a %s\n",
313 				path1, S_ISLNK(stb1.st_mode) ? "symbolic link" :
314 					(S_ISDIR(stb1.st_mode) ? "directory" :
315 					(S_ISREG(stb1.st_mode) ? "file" : "error")),
316 				path2, S_ISLNK(stb2.st_mode) ? "symbolic link" :
317 					(S_ISDIR(stb2.st_mode) ? "directory" :
318 					(S_ISREG(stb2.st_mode) ? "file" : "error")));
319 			status |= 1;
320 			return;
321 		}
322 	} else {
323 		if (stat(path1, &stb1) != 0) {
324 			if (!(Nflag || Pflag) || errno != ENOENT) {
325 				warn("%s", path1);
326 				return;
327 			}
328 			flags |= D_EMPTY1;
329 			memset(&stb1, 0, sizeof(stb1));
330 		}
331 		if (stat(path2, &stb2) != 0) {
332 			if (!Nflag || errno != ENOENT) {
333 				warn("%s", path2);
334 				return;
335 			}
336 			flags |= D_EMPTY2;
337 			memset(&stb2, 0, sizeof(stb2));
338 			stb2.st_mode = stb1.st_mode;
339 		}
340 		if (stb1.st_mode == 0)
341 			stb1.st_mode = stb2.st_mode;
342 	}
343 	if (stb1.st_dev == stb2.st_dev && stb1.st_ino == stb2.st_ino)
344 		return;
345 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
346 		if (rflag)
347 			diffdir(path1, path2, flags);
348 		else
349 			printf("Common subdirectories: %s and %s\n",
350 			    path1, path2);
351 		return;
352 	}
353 	if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
354 		rc = D_SKIPPED1;
355 	else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
356 		rc = D_SKIPPED2;
357 	else
358 		rc = diffreg(path1, path2, flags, 0);
359 	print_status(rc, path1, path2, "");
360 }
361 
362 /*
363  * Returns 1 if the directory entry should be included in the
364  * diff, else 0.  Checks the excludes list.
365  */
366 static int
367 selectfile(const struct dirent *dp)
368 {
369 	struct excludes *excl;
370 
371 	if (dp->d_fileno == 0)
372 		return (0);
373 
374 	/* always skip "." and ".." */
375 	if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
376 	    (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
377 		return (0);
378 
379 	/* check excludes list */
380 	for (excl = excludes_list; excl != NULL; excl = excl->next)
381 		if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0)
382 			return (0);
383 
384 	return (1);
385 }
386 
387 void
388 print_only(const char *path, size_t dirlen, const char *entry)
389 {
390 	if (dirlen > 1)
391 		dirlen--;
392 	printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
393 }
394