xref: /openbsd-src/usr.bin/diff/diffdir.c (revision 016bcb7b8840fd0b4ef83d0992e4c16be57add94)
1 /*	$OpenBSD: diffdir.c,v 1.24 2003/07/21 23:28:00 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 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 #ifndef lint
24 static const char rcsid[] = "$OpenBSD: diffdir.c,v 1.24 2003/07/21 23:28:00 millert Exp $";
25 #endif /* not lint */
26 
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 
30 #include <dirent.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <fnmatch.h>
35 #include <paths.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "diff.h"
42 
43 static int dircompare(const void *, const void *);
44 static int excluded(const char *);
45 static struct dirent **slurpdir(char *, char **, int);
46 static void diffit(struct dirent *, char *, size_t, char *, size_t);
47 
48 #define d_status	d_type		/* we need to store status for -l */
49 
50 /*
51  * Diff directory traveral. Will be called recursively if -r was specified.
52  */
53 void
54 diffdir(char *p1, char *p2)
55 {
56 	struct dirent **dirp1, **dirp2, **dp1, **dp2;
57 	struct dirent *dent1, *dent2;
58 	size_t dirlen1, dirlen2;
59 	char path1[MAXPATHLEN], path2[MAXPATHLEN];
60 	char *dirbuf1, *dirbuf2;
61 	int pos;
62 
63 	dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
64 	if (dirlen1 >= sizeof(path1) - 1) {
65 		warnx("%s: %s", p1, strerror(ENAMETOOLONG));
66 		status = 2;
67 		return;
68 	}
69 	if (path1[dirlen1 - 1] != '/') {
70 		path1[dirlen1++] = '/';
71 		path1[dirlen1] = '\0';
72 	}
73 	dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
74 	if (dirlen2 >= sizeof(path2) - 1) {
75 		warnx("%s: %s", p2, strerror(ENAMETOOLONG));
76 		status = 2;
77 		return;
78 	}
79 	if (path2[dirlen2 - 1] != '/') {
80 		path2[dirlen2++] = '/';
81 		path2[dirlen2] = '\0';
82 	}
83 
84 	/* get a list of the entries in each directory */
85 	dp1 = dirp1 = slurpdir(path1, &dirbuf1, Nflag + Pflag);
86 	dp2 = dirp2 = slurpdir(path2, &dirbuf2, Nflag);
87 	if (dirp1 == NULL || dirp2 == NULL)
88 		return;
89 
90 	/*
91 	 * If we were given a starting point, find it.
92 	 */
93 	if (start != NULL) {
94 		while (*dp1 != NULL && strcmp((*dp1)->d_name, start) < 0)
95 			dp1++;
96 		while (*dp2 != NULL && strcmp((*dp2)->d_name, start) < 0)
97 			dp2++;
98 	}
99 
100 	/*
101 	 * Iterate through the two directory lists, diffing as we go.
102 	 */
103 	while (*dp1 != NULL || *dp2 != NULL) {
104 		dent1 = *dp1;
105 		dent2 = *dp2;
106 
107 		pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
108 		    strcmp(dent1->d_name, dent2->d_name);
109 		if (pos == 0) {
110 			/* file exists in both dirs, diff it */
111 			diffit(dent1, path1, dirlen1, path2, dirlen2);
112 			dp1++;
113 			dp2++;
114 		} else if (pos < 0) {
115 			/* file only in first dir, only diff if -N */
116 			if (Nflag)
117 				diffit(dent1, path1, dirlen1, path2, dirlen2);
118 			else if (lflag)
119 				dent1->d_status |= D_ONLY;
120 			else
121 				printf("Only in %.*s: %s\n", (int)(dirlen1 - 1),
122 				    path1, dent1->d_name);
123 			dp1++;
124 		} else {
125 			/* file only in second dir, only diff if -N or -P */
126 			if (Nflag || Pflag)
127 				diffit(dent2, path1, dirlen1, path2, dirlen2);
128 			else if (lflag)
129 				dent2->d_status |= D_ONLY;
130 			else
131 				printf("Only in %.*s: %s\n", (int)(dirlen2 - 1),
132 				    path2, dent2->d_name);
133 			dp2++;
134 		}
135 	}
136 	if (lflag) {
137 		path1[dirlen1] = '\0';
138 		path2[dirlen2] = '\0';
139 		for (dp1 = dirp1; (dent1 = *dp1) != NULL; dp1++) {
140 			print_status(dent1->d_status, path1, path2,
141 			    dent1->d_name);
142 		}
143 		for (dp2 = dirp2; (dent2 = *dp2) != NULL; dp2++) {
144 			if (dent2->d_status == D_ONLY)
145 				print_status(dent2->d_status, path2, NULL,
146 				    dent2->d_name);
147 		}
148 	}
149 
150 	if (dirbuf1 != NULL) {
151 		free(dirp1);
152 		free(dirbuf1);
153 	}
154 	if (dirbuf2 != NULL) {
155 		free(dirp2);
156 		free(dirbuf2);
157 	}
158 }
159 
160 /*
161  * Read in a whole directory's worth of struct dirents, culling
162  * out the "excluded" ones.
163  * Returns an array of struct dirent *'s that point into the buffer
164  * returned via bufp.  Caller is responsible for free()ing both of these.
165  */
166 static struct dirent **
167 slurpdir(char *path, char **bufp, int enoentok)
168 {
169 	char *buf, *ebuf, *cp;
170 	size_t bufsize;
171 	long base;
172 	int fd, nbytes, entries;
173 	struct stat sb;
174 	struct dirent **dirlist, *dp;
175 
176 	*bufp = NULL;
177 	if ((fd = open(path, O_RDONLY, 0644)) == -1) {
178 		static struct dirent *dummy;
179 
180 		if (!enoentok || errno != ENOENT) {
181 			warn("%s", path);
182 			return (NULL);
183 		}
184 		return (&dummy);
185 	}
186 	fstat(fd, &sb);
187 
188 	bufsize = sb.st_size;
189 	if (bufsize < sb.st_blksize)
190 		bufsize = sb.st_blksize;
191 	buf = emalloc(bufsize);
192 
193 	nbytes = getdirentries(fd, buf, bufsize, &base);
194 	if (nbytes <= 0) {
195 		free(buf);
196 		warn("%s", path);
197 		return (NULL);
198 	}
199 	ebuf = buf + nbytes;
200 	close(fd);
201 
202 	/*
203 	 * We now have all the directory entries in our buffer.
204 	 * However, in order to easily sort them we need to convert
205 	 * the buffer into an array.
206 	 */
207 	for (entries = 0, cp = buf; cp < ebuf; ) {
208 		dp = (struct dirent *)cp;
209 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT)
210 			entries++;
211 		if (dp->d_reclen <= 0)
212 			break;
213 		cp += dp->d_reclen;
214 	}
215 	dirlist = emalloc(sizeof(struct dirent *) * (entries + 1));
216 	for (entries = 0, cp = buf; cp < ebuf; ) {
217 		dp = (struct dirent *)cp;
218 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
219 		    !excluded(dp->d_name)) {
220 			dp->d_status = 0;
221 			dirlist[entries++] = dp;
222 		}
223 		if (dp->d_reclen <= 0)
224 			break;
225 		cp += dp->d_reclen;
226 	}
227 	dirlist[entries] = NULL;
228 
229 	qsort(dirlist, entries, sizeof(struct dir *), dircompare);
230 
231 	*bufp = buf;
232 	return (dirlist);
233 }
234 
235 /*
236  * Compare d_name in two dirent structures; for qsort(3).
237  */
238 static int
239 dircompare(const void *vp1, const void *vp2)
240 {
241 	struct dirent *dp1 = *((struct dirent **) vp1);
242 	struct dirent *dp2 = *((struct dirent **) vp2);
243 
244 	return (strcmp(dp1->d_name, dp2->d_name));
245 }
246 
247 /*
248  * Do the actual diff by calling either diffreg() or diffdir().
249  */
250 static void
251 diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2)
252 {
253 	int flags = D_HEADER;
254 
255 	strlcpy(path1 + plen1, dp->d_name, MAXPATHLEN - plen1);
256 	if (stat(path1, &stb1) != 0) {
257 		if (!(Nflag || Pflag) || errno != ENOENT) {
258 			warn("%s", path1);
259 			return;
260 		}
261 		flags |= D_EMPTY1;
262 		memset(&stb1, 0, sizeof(stb1));
263 	}
264 
265 	strlcpy(path2 + plen2, dp->d_name, MAXPATHLEN - plen2);
266 	if (stat(path2, &stb2) != 0) {
267 		if (!Nflag || errno != ENOENT) {
268 			warn("%s", path2);
269 			return;
270 		}
271 		flags |= D_EMPTY2;
272 		memset(&stb2, 0, sizeof(stb2));
273 		stb2.st_mode = stb1.st_mode;
274 	}
275 	if (stb1.st_mode == 0)
276 		stb1.st_mode = stb2.st_mode;
277 
278 	if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
279 		if (rflag)
280 			diffdir(path1, path2);
281 		else if (lflag)
282 			dp->d_status |= D_COMMON;
283 		else
284 			printf("Common subdirectories: %s and %s\n",
285 			    path1, path2);
286 		return;
287 	}
288 	dp->d_status = diffreg(path1, path2, flags);
289 	if (!lflag)
290 		print_status(dp->d_status, path1, path2, NULL);
291 }
292 
293 /*
294  * Exclude the given directory entry?
295  */
296 static int
297 excluded(const char *entry)
298 {
299 	struct excludes *excl;
300 
301 	/* always skip "." and ".." */
302 	if (entry[0] == '.' &&
303 	    (entry[1] == '\0' || (entry[1] == '.' && entry[2] == '\0')))
304 		return (1);
305 
306 	/* check excludes list */
307 	for (excl = excludes_list; excl != NULL; excl = excl->next)
308 		if (fnmatch(excl->pattern, entry, FNM_PATHNAME) == 0)
309 			return (1);
310 
311 	return (0);
312 }
313