xref: /freebsd/usr.bin/diff/diffdir.c (revision b4c3e9b5b09c829b4135aff738bd2893ed052377)
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, const char *path, struct dirent ***dirp,
64     int (*selectf)(const struct dirent *),
65     int (*comparf)(const struct dirent **, const struct dirent **))
66 {
67 	struct stat sb;
68 	struct inode *ino = NULL;
69 	int fd = -1, ret, serrno;
70 
71 	if ((fd = open(path, O_DIRECTORY | O_RDONLY)) < 0 ||
72 	    (ino = calloc(1, sizeof(*ino))) == NULL ||
73 	    fstat(fd, &sb) != 0)
74 		goto fail;
75 	ino->dev = sb.st_dev;
76 	ino->ino = sb.st_ino;
77 	if (RB_FIND(inodetree, tree, ino)) {
78 		free(ino);
79 		close(fd);
80 		warnx("%s: Directory loop detected", path);
81 		*dirp = NULL;
82 		return (0);
83 	}
84 	if ((ret = fdscandir(fd, dirp, selectf, comparf)) < 0)
85 		goto fail;
86 	RB_INSERT(inodetree, tree, ino);
87 	close(fd);
88 	return (ret);
89 fail:
90 	serrno = errno;
91 	if (ino != NULL)
92 		free(ino);
93 	if (fd >= 0)
94 		close(fd);
95 	errno = serrno;
96 	return (-1);
97 }
98 
99 /*
100  * Diff directory traversal. Will be called recursively if -r was specified.
101  */
102 void
103 diffdir(char *p1, char *p2, int flags)
104 {
105 	struct dirent *dent1, **dp1, **edp1, **dirp1 = NULL;
106 	struct dirent *dent2, **dp2, **edp2, **dirp2 = NULL;
107 	size_t dirlen1, dirlen2;
108 	char path1[PATH_MAX], path2[PATH_MAX];
109 	int pos;
110 
111 	edp1 = edp2 = NULL;
112 
113 	dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
114 	if (dirlen1 >= sizeof(path1) - 1) {
115 		warnc(ENAMETOOLONG, "%s", p1);
116 		status |= 2;
117 		return;
118 	}
119 	while (dirlen1 > 1 && path1[dirlen1 - 1] == '/')
120 		path1[--dirlen1] = '\0';
121 	dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
122 	if (dirlen2 >= sizeof(path2) - 1) {
123 		warnc(ENAMETOOLONG, "%s", p2);
124 		status |= 2;
125 		return;
126 	}
127 	while (dirlen2 > 1 && path2[dirlen2 - 1] == '/')
128 		path2[--dirlen2] = '\0';
129 
130 	/*
131 	 * Get a list of entries in each directory, skipping "excluded" files
132 	 * and sorting alphabetically.
133 	 */
134 	pos = vscandir(&v1, path1, &dirp1, selectfile, alphasort);
135 	if (pos == -1) {
136 		if (errno == ENOENT && (Nflag || Pflag)) {
137 			pos = 0;
138 		} else {
139 			warn("%s", path1);
140 			goto closem;
141 		}
142 	}
143 	dp1 = dirp1;
144 	edp1 = dirp1 + pos;
145 
146 	pos = vscandir(&v2, path2, &dirp2, selectfile, alphasort);
147 	if (pos == -1) {
148 		if (errno == ENOENT && Nflag) {
149 			pos = 0;
150 		} else {
151 			warn("%s", path2);
152 			goto closem;
153 		}
154 	}
155 	dp2 = dirp2;
156 	edp2 = dirp2 + pos;
157 
158 	/*
159 	 * If we were given a starting point, find it.
160 	 */
161 	if (start != NULL) {
162 		while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0)
163 			dp1++;
164 		while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0)
165 			dp2++;
166 	}
167 
168 	/*
169 	 * Append separator so children's names can be appended directly.
170 	 */
171 	if (path1[dirlen1 - 1] != '/') {
172 		path1[dirlen1++] = '/';
173 		path1[dirlen1] = '\0';
174 	}
175 	if (path2[dirlen2 - 1] != '/') {
176 		path2[dirlen2++] = '/';
177 		path2[dirlen2] = '\0';
178 	}
179 
180 	/*
181 	 * Iterate through the two directory lists, diffing as we go.
182 	 */
183 	while (dp1 != edp1 || dp2 != edp2) {
184 		dent1 = dp1 != edp1 ? *dp1 : NULL;
185 		dent2 = dp2 != edp2 ? *dp2 : NULL;
186 
187 		pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
188 		    ignore_file_case ? strcasecmp(dent1->d_name, dent2->d_name) :
189 		    strcmp(dent1->d_name, dent2->d_name) ;
190 		if (pos == 0) {
191 			/* file exists in both dirs, diff it */
192 			diffit(dent1, path1, dirlen1, dent2, path2, dirlen2, flags);
193 			dp1++;
194 			dp2++;
195 		} else if (pos < 0) {
196 			/* file only in first dir, only diff if -N */
197 			if (Nflag) {
198 				diffit(dent1, path1, dirlen1, dent2, path2,
199 					dirlen2, flags);
200 			} else {
201 				print_only(path1, dirlen1, dent1->d_name);
202 				status |= 1;
203 			}
204 			dp1++;
205 		} else {
206 			/* file only in second dir, only diff if -N or -P */
207 			if (Nflag || Pflag)
208 				diffit(dent2, path1, dirlen1, dent1, path2,
209 					dirlen2, flags);
210 			else {
211 				print_only(path2, dirlen2, dent2->d_name);
212 				status |= 1;
213 			}
214 			dp2++;
215 		}
216 	}
217 
218 closem:
219 	if (dirp1 != NULL) {
220 		for (dp1 = dirp1; dp1 < edp1; dp1++)
221 			free(*dp1);
222 		free(dirp1);
223 	}
224 	if (dirp2 != NULL) {
225 		for (dp2 = dirp2; dp2 < edp2; dp2++)
226 			free(*dp2);
227 		free(dirp2);
228 	}
229 }
230 
231 /*
232  * Do the actual diff by calling either diffreg() or diffdir().
233  */
234 static void
235 diffit(struct dirent *dp, char *path1, size_t plen1, struct dirent *dp2,
236 	char *path2, size_t plen2, int flags)
237 {
238 	int rc;
239 
240 	flags |= D_HEADER;
241 	strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
242 
243 	/*
244 	 * If we are ignoring file case, use dent2s name here if both names are
245 	 * the same apart from case.
246 	 */
247 	if (ignore_file_case && strcasecmp(dp2->d_name, dp2->d_name) == 0)
248 		strlcpy(path2 + plen2, dp2->d_name, PATH_MAX - plen2);
249 	else
250 		strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2);
251 
252 	if (noderef) {
253 		if (lstat(path1, &stb1) != 0) {
254 			if (!(Nflag || Pflag) || errno != ENOENT) {
255 				warn("%s", path1);
256 				return;
257 			}
258 			flags |= D_EMPTY1;
259 			memset(&stb1, 0, sizeof(stb1));
260 		}
261 		if (lstat(path2, &stb2) != 0) {
262 			if (!Nflag || errno != ENOENT) {
263 				warn("%s", path2);
264 				return;
265 			}
266 			flags |= D_EMPTY2;
267 			memset(&stb2, 0, sizeof(stb2));
268 			stb2.st_mode = stb1.st_mode;
269 		}
270 		if (stb1.st_mode == 0)
271 			stb1.st_mode = stb2.st_mode;
272 		if (S_ISLNK(stb1.st_mode) || S_ISLNK(stb2.st_mode)) {
273 			if  (S_ISLNK(stb1.st_mode) && S_ISLNK(stb2.st_mode)) {
274 				char buf1[PATH_MAX];
275 				char buf2[PATH_MAX];
276 				ssize_t len1 = 0;
277 				ssize_t len2 = 0;
278 
279 				len1 = readlink(path1, buf1, sizeof(buf1));
280 				len2 = readlink(path2, buf2, sizeof(buf2));
281 
282 				if (len1 < 0 || len2 < 0) {
283 					perror("reading links");
284 					return;
285 				}
286 				buf1[len1] = '\0';
287 				buf2[len2] = '\0';
288 
289 				if (len1 != len2 || strncmp(buf1, buf2, len1) != 0) {
290 					printf("Symbolic links %s and %s differ\n",
291 						path1, path2);
292 					status |= 1;
293 				}
294 
295 				return;
296 			}
297 
298 			printf("File %s is a %s while file %s is a %s\n",
299 				path1, S_ISLNK(stb1.st_mode) ? "symbolic link" :
300 					(S_ISDIR(stb1.st_mode) ? "directory" :
301 					(S_ISREG(stb1.st_mode) ? "file" : "error")),
302 				path2, S_ISLNK(stb2.st_mode) ? "symbolic link" :
303 					(S_ISDIR(stb2.st_mode) ? "directory" :
304 					(S_ISREG(stb2.st_mode) ? "file" : "error")));
305 			status |= 1;
306 			return;
307 		}
308 	} else {
309 		if (stat(path1, &stb1) != 0) {
310 			if (!(Nflag || Pflag) || errno != ENOENT) {
311 				warn("%s", path1);
312 				return;
313 			}
314 			flags |= D_EMPTY1;
315 			memset(&stb1, 0, sizeof(stb1));
316 		}
317 		if (stat(path2, &stb2) != 0) {
318 			if (!Nflag || errno != ENOENT) {
319 				warn("%s", path2);
320 				return;
321 			}
322 			flags |= D_EMPTY2;
323 			memset(&stb2, 0, sizeof(stb2));
324 			stb2.st_mode = stb1.st_mode;
325 		}
326 		if (stb1.st_mode == 0)
327 			stb1.st_mode = stb2.st_mode;
328 	}
329 	if (stb1.st_dev == stb2.st_dev && stb1.st_ino == stb2.st_ino)
330 		return;
331 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
332 		if (rflag)
333 			diffdir(path1, path2, flags);
334 		else
335 			printf("Common subdirectories: %s and %s\n",
336 			    path1, path2);
337 		return;
338 	}
339 	if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
340 		rc = D_SKIPPED1;
341 	else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
342 		rc = D_SKIPPED2;
343 	else
344 		rc = diffreg(path1, path2, flags, 0);
345 	print_status(rc, path1, path2, "");
346 }
347 
348 /*
349  * Returns 1 if the directory entry should be included in the
350  * diff, else 0.  Checks the excludes list.
351  */
352 static int
353 selectfile(const struct dirent *dp)
354 {
355 	struct excludes *excl;
356 
357 	if (dp->d_fileno == 0)
358 		return (0);
359 
360 	/* always skip "." and ".." */
361 	if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
362 	    (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
363 		return (0);
364 
365 	/* check excludes list */
366 	for (excl = excludes_list; excl != NULL; excl = excl->next)
367 		if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0)
368 			return (0);
369 
370 	return (1);
371 }
372 
373 void
374 print_only(const char *path, size_t dirlen, const char *entry)
375 {
376 	if (dirlen > 1)
377 		dirlen--;
378 	printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
379 }
380