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