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