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