xref: /freebsd-src/usr.bin/diff/diffdir.c (revision f4be3645a14d4faa5a51846054318ccf77c9cd5e)
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/cdefs.h>
24 __FBSDID("$FreeBSD$");
25 
26 #include <sys/stat.h>
27 
28 #include <dirent.h>
29 #include <err.h>
30 #include <errno.h>
31 #include <fnmatch.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <unistd.h>
37 
38 #include "diff.h"
39 
40 static int selectfile(const struct dirent *);
41 static void diffit(struct dirent *, char *, size_t, char *, size_t, int);
42 static void print_only(const char *, size_t, const char *);
43 
44 #define d_status	d_type		/* we need to store status for -l */
45 
46 /*
47  * Diff directory traversal. Will be called recursively if -r was specified.
48  */
49 void
50 diffdir(char *p1, char *p2, int flags)
51 {
52 	struct dirent *dent1, **dp1, **edp1, **dirp1 = NULL;
53 	struct dirent *dent2, **dp2, **edp2, **dirp2 = NULL;
54 	size_t dirlen1, dirlen2;
55 	char path1[PATH_MAX], path2[PATH_MAX];
56 	int pos;
57 
58 	edp1 = edp2 = NULL;
59 
60 	dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
61 	if (dirlen1 >= sizeof(path1) - 1) {
62 		warnc(ENAMETOOLONG, "%s", p1);
63 		status |= 2;
64 		return;
65 	}
66 	if (path1[dirlen1 - 1] != '/') {
67 		path1[dirlen1++] = '/';
68 		path1[dirlen1] = '\0';
69 	}
70 	dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
71 	if (dirlen2 >= sizeof(path2) - 1) {
72 		warnc(ENAMETOOLONG, "%s", p2);
73 		status |= 2;
74 		return;
75 	}
76 	if (path2[dirlen2 - 1] != '/') {
77 		path2[dirlen2++] = '/';
78 		path2[dirlen2] = '\0';
79 	}
80 
81 	/*
82 	 * Get a list of entries in each directory, skipping "excluded" files
83 	 * and sorting alphabetically.
84 	 */
85 	pos = scandir(path1, &dirp1, selectfile, alphasort);
86 	if (pos == -1) {
87 		if (errno == ENOENT && (Nflag || Pflag)) {
88 			pos = 0;
89 		} else {
90 			warn("%s", path1);
91 			goto closem;
92 		}
93 	}
94 	dp1 = dirp1;
95 	edp1 = dirp1 + pos;
96 
97 	pos = scandir(path2, &dirp2, selectfile, alphasort);
98 	if (pos == -1) {
99 		if (errno == ENOENT && Nflag) {
100 			pos = 0;
101 		} else {
102 			warn("%s", path2);
103 			goto closem;
104 		}
105 	}
106 	dp2 = dirp2;
107 	edp2 = dirp2 + pos;
108 
109 	/*
110 	 * If we were given a starting point, find it.
111 	 */
112 	if (start != NULL) {
113 		while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0)
114 			dp1++;
115 		while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0)
116 			dp2++;
117 	}
118 
119 	/*
120 	 * Iterate through the two directory lists, diffing as we go.
121 	 */
122 	while (dp1 != edp1 || dp2 != edp2) {
123 		dent1 = dp1 != edp1 ? *dp1 : NULL;
124 		dent2 = dp2 != edp2 ? *dp2 : NULL;
125 
126 		pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
127 		    ignore_file_case ? strcasecmp(dent1->d_name, dent2->d_name) :
128 		    strcmp(dent1->d_name, dent2->d_name) ;
129 		if (pos == 0) {
130 			/* file exists in both dirs, diff it */
131 			diffit(dent1, path1, dirlen1, path2, dirlen2, flags);
132 			dp1++;
133 			dp2++;
134 		} else if (pos < 0) {
135 			/* file only in first dir, only diff if -N */
136 			if (Nflag) {
137 				diffit(dent1, path1, dirlen1, path2, dirlen2,
138 				    flags);
139 			} else {
140 				print_only(path1, dirlen1, dent1->d_name);
141 				status |= 1;
142 			}
143 			dp1++;
144 		} else {
145 			/* file only in second dir, only diff if -N or -P */
146 			if (Nflag || Pflag)
147 				diffit(dent2, path1, dirlen1, path2, dirlen2,
148 				    flags);
149 			else {
150 				print_only(path2, dirlen2, dent2->d_name);
151 				status |= 1;
152 			}
153 			dp2++;
154 		}
155 	}
156 
157 closem:
158 	if (dirp1 != NULL) {
159 		for (dp1 = dirp1; dp1 < edp1; dp1++)
160 			free(*dp1);
161 		free(dirp1);
162 	}
163 	if (dirp2 != NULL) {
164 		for (dp2 = dirp2; dp2 < edp2; dp2++)
165 			free(*dp2);
166 		free(dirp2);
167 	}
168 }
169 
170 /*
171  * Do the actual diff by calling either diffreg() or diffdir().
172  */
173 static void
174 diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2,
175     int flags)
176 {
177 	flags |= D_HEADER;
178 	strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
179 	strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2);
180 
181 	if (noderef) {
182 		if (lstat(path1, &stb1) != 0) {
183 			if (!(Nflag || Pflag) || errno != ENOENT) {
184 				warn("%s", path1);
185 				return;
186 			}
187 			flags |= D_EMPTY1;
188 			memset(&stb1, 0, sizeof(stb1));
189 		}
190 
191 		if (lstat(path2, &stb2) != 0) {
192 			if (!Nflag || errno != ENOENT) {
193 				warn("%s", path2);
194 				return;
195 			}
196 			flags |= D_EMPTY2;
197 			memset(&stb2, 0, sizeof(stb2));
198 			stb2.st_mode = stb1.st_mode;
199 		}
200 		if (stb1.st_mode == 0)
201 			stb1.st_mode = stb2.st_mode;
202 		if (S_ISLNK(stb1.st_mode) || S_ISLNK(stb2.st_mode)) {
203 			if  (S_ISLNK(stb1.st_mode) && S_ISLNK(stb2.st_mode)) {
204 				char buf1[PATH_MAX];
205 				char buf2[PATH_MAX];
206 				ssize_t len1 = 0;
207 				ssize_t len2 = 0;
208 
209 				len1 = readlink(path1, buf1, sizeof(buf1));
210 				len2 = readlink(path2, buf2, sizeof(buf2));
211 
212 				if (len1 < 0 || len2 < 0) {
213 					perror("reading links");
214 					return;
215 				}
216 				buf1[len1] = '\0';
217 				buf2[len2] = '\0';
218 
219 				if (len1 != len2 || strncmp(buf1, buf2, len1) != 0) {
220 					printf("Symbolic links %s and %s differ\n",
221 						path1, path2);
222 					status |= 1;
223 				}
224 
225 				return;
226 			}
227 
228 			printf("File %s is a %s while file %s is a %s\n",
229 				path1, S_ISLNK(stb1.st_mode) ? "symbolic link" :
230 					(S_ISDIR(stb1.st_mode) ? "directory" :
231 					(S_ISREG(stb1.st_mode) ? "file" : "error")),
232 				path2, S_ISLNK(stb2.st_mode) ? "symbolic link" :
233 					(S_ISDIR(stb2.st_mode) ? "directory" :
234 					(S_ISREG(stb2.st_mode) ? "file" : "error")));
235 			status |= 1;
236 			return;
237 		}
238 	} else {
239 		if (stat(path1, &stb1) != 0) {
240 			if (!(Nflag || Pflag) || errno != ENOENT) {
241 				warn("%s", path1);
242 				return;
243 			}
244 			flags |= D_EMPTY1;
245 			memset(&stb1, 0, sizeof(stb1));
246 		}
247 
248 		if (stat(path2, &stb2) != 0) {
249 			if (!Nflag || errno != ENOENT) {
250 				warn("%s", path2);
251 				return;
252 			}
253 			flags |= D_EMPTY2;
254 			memset(&stb2, 0, sizeof(stb2));
255 			stb2.st_mode = stb1.st_mode;
256 		}
257 		if (stb1.st_mode == 0)
258 			stb1.st_mode = stb2.st_mode;
259 	}
260 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
261 		if (rflag)
262 			diffdir(path1, path2, flags);
263 		else
264 			printf("Common subdirectories: %s and %s\n",
265 			    path1, path2);
266 		return;
267 	}
268 	if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
269 		dp->d_status = D_SKIPPED1;
270 	else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
271 		dp->d_status = D_SKIPPED2;
272 	else
273 		dp->d_status = diffreg(path1, path2, flags, 0);
274 	print_status(dp->d_status, path1, path2, "");
275 }
276 
277 /*
278  * Returns 1 if the directory entry should be included in the
279  * diff, else 0.  Checks the excludes list.
280  */
281 static int
282 selectfile(const struct dirent *dp)
283 {
284 	struct excludes *excl;
285 
286 	if (dp->d_fileno == 0)
287 		return (0);
288 
289 	/* always skip "." and ".." */
290 	if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
291 	    (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
292 		return (0);
293 
294 	/* check excludes list */
295 	for (excl = excludes_list; excl != NULL; excl = excl->next)
296 		if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0)
297 			return (0);
298 
299 	return (1);
300 }
301 
302 void
303 print_only(const char *path, size_t dirlen, const char *entry)
304 {
305 	if (dirlen > 1)
306 		dirlen--;
307 	printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
308 }
309