1 *c9733229SMatthew Dillon /* $OpenBSD: diffdir.c,v 1.47 2019/01/25 00:19:26 millert Exp $ */
2 *c9733229SMatthew Dillon
3 *c9733229SMatthew Dillon /*
4 *c9733229SMatthew Dillon * Copyright (c) 2003, 2010 Todd C. Miller <millert@openbsd.org>
5 *c9733229SMatthew Dillon *
6 *c9733229SMatthew Dillon * Permission to use, copy, modify, and distribute this software for any
7 *c9733229SMatthew Dillon * purpose with or without fee is hereby granted, provided that the above
8 *c9733229SMatthew Dillon * copyright notice and this permission notice appear in all copies.
9 *c9733229SMatthew Dillon *
10 *c9733229SMatthew Dillon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 *c9733229SMatthew Dillon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 *c9733229SMatthew Dillon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 *c9733229SMatthew Dillon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 *c9733229SMatthew Dillon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 *c9733229SMatthew Dillon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 *c9733229SMatthew Dillon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *c9733229SMatthew Dillon *
18 *c9733229SMatthew Dillon * Sponsored in part by the Defense Advanced Research Projects
19 *c9733229SMatthew Dillon * Agency (DARPA) and Air Force Research Laboratory, Air Force
20 *c9733229SMatthew Dillon * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21 *c9733229SMatthew Dillon */
22 *c9733229SMatthew Dillon
23 *c9733229SMatthew Dillon #include <sys/stat.h>
24 *c9733229SMatthew Dillon
25 *c9733229SMatthew Dillon #include <dirent.h>
26 *c9733229SMatthew Dillon #include <err.h>
27 *c9733229SMatthew Dillon #include <errno.h>
28 *c9733229SMatthew Dillon #include <fcntl.h>
29 *c9733229SMatthew Dillon #include <fnmatch.h>
30 *c9733229SMatthew Dillon #include <paths.h>
31 *c9733229SMatthew Dillon #include <stdio.h>
32 *c9733229SMatthew Dillon #include <stdlib.h>
33 *c9733229SMatthew Dillon #include <string.h>
34 *c9733229SMatthew Dillon #include <unistd.h>
35 *c9733229SMatthew Dillon #include <limits.h>
36 *c9733229SMatthew Dillon
37 *c9733229SMatthew Dillon #include "diff.h"
38 *c9733229SMatthew Dillon #include "xmalloc.h"
39 *c9733229SMatthew Dillon
40 *c9733229SMatthew Dillon static int selectfile(const struct dirent *);
41 *c9733229SMatthew Dillon static void diffit(struct dirent *, char *, size_t, char *, size_t, int);
42 *c9733229SMatthew Dillon
43 *c9733229SMatthew Dillon #define d_status d_type /* we need to store status for -l */
44 *c9733229SMatthew Dillon
45 *c9733229SMatthew Dillon /*
46 *c9733229SMatthew Dillon * Diff directory traversal. Will be called recursively if -r was specified.
47 *c9733229SMatthew Dillon */
48 *c9733229SMatthew Dillon void
diffdir(char * p1,char * p2,int flags)49 *c9733229SMatthew Dillon diffdir(char *p1, char *p2, int flags)
50 *c9733229SMatthew Dillon {
51 *c9733229SMatthew Dillon struct dirent *dent1, **dp1, **edp1 = NULL, **dirp1 = NULL;
52 *c9733229SMatthew Dillon struct dirent *dent2, **dp2, **edp2 = NULL, **dirp2 = NULL;
53 *c9733229SMatthew Dillon size_t dirlen1, dirlen2;
54 *c9733229SMatthew Dillon char path1[PATH_MAX], path2[PATH_MAX];
55 *c9733229SMatthew Dillon int pos;
56 *c9733229SMatthew Dillon
57 *c9733229SMatthew Dillon dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
58 *c9733229SMatthew Dillon if (dirlen1 >= sizeof(path1) - 1) {
59 *c9733229SMatthew Dillon warnc(ENAMETOOLONG, "%s", p1);
60 *c9733229SMatthew Dillon status |= 2;
61 *c9733229SMatthew Dillon return;
62 *c9733229SMatthew Dillon }
63 *c9733229SMatthew Dillon if (path1[dirlen1 - 1] != '/') {
64 *c9733229SMatthew Dillon path1[dirlen1++] = '/';
65 *c9733229SMatthew Dillon path1[dirlen1] = '\0';
66 *c9733229SMatthew Dillon }
67 *c9733229SMatthew Dillon dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
68 *c9733229SMatthew Dillon if (dirlen2 >= sizeof(path2) - 1) {
69 *c9733229SMatthew Dillon warnc(ENAMETOOLONG, "%s", p2);
70 *c9733229SMatthew Dillon status |= 2;
71 *c9733229SMatthew Dillon return;
72 *c9733229SMatthew Dillon }
73 *c9733229SMatthew Dillon if (path2[dirlen2 - 1] != '/') {
74 *c9733229SMatthew Dillon path2[dirlen2++] = '/';
75 *c9733229SMatthew Dillon path2[dirlen2] = '\0';
76 *c9733229SMatthew Dillon }
77 *c9733229SMatthew Dillon
78 *c9733229SMatthew Dillon /*
79 *c9733229SMatthew Dillon * Get a list of entries in each directory, skipping "excluded" files
80 *c9733229SMatthew Dillon * and sorting alphabetically.
81 *c9733229SMatthew Dillon */
82 *c9733229SMatthew Dillon pos = scandir(path1, &dirp1, selectfile, alphasort);
83 *c9733229SMatthew Dillon if (pos == -1) {
84 *c9733229SMatthew Dillon if (errno == ENOENT && (Nflag || Pflag)) {
85 *c9733229SMatthew Dillon pos = 0;
86 *c9733229SMatthew Dillon } else {
87 *c9733229SMatthew Dillon warn("%s", path1);
88 *c9733229SMatthew Dillon goto closem;
89 *c9733229SMatthew Dillon }
90 *c9733229SMatthew Dillon }
91 *c9733229SMatthew Dillon dp1 = dirp1;
92 *c9733229SMatthew Dillon edp1 = dirp1 + pos;
93 *c9733229SMatthew Dillon
94 *c9733229SMatthew Dillon pos = scandir(path2, &dirp2, selectfile, alphasort);
95 *c9733229SMatthew Dillon if (pos == -1) {
96 *c9733229SMatthew Dillon if (errno == ENOENT && Nflag) {
97 *c9733229SMatthew Dillon pos = 0;
98 *c9733229SMatthew Dillon } else {
99 *c9733229SMatthew Dillon warn("%s", path2);
100 *c9733229SMatthew Dillon goto closem;
101 *c9733229SMatthew Dillon }
102 *c9733229SMatthew Dillon }
103 *c9733229SMatthew Dillon dp2 = dirp2;
104 *c9733229SMatthew Dillon edp2 = dirp2 + pos;
105 *c9733229SMatthew Dillon
106 *c9733229SMatthew Dillon /*
107 *c9733229SMatthew Dillon * If we were given a starting point, find it.
108 *c9733229SMatthew Dillon */
109 *c9733229SMatthew Dillon if (start != NULL) {
110 *c9733229SMatthew Dillon while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0)
111 *c9733229SMatthew Dillon dp1++;
112 *c9733229SMatthew Dillon while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0)
113 *c9733229SMatthew Dillon dp2++;
114 *c9733229SMatthew Dillon }
115 *c9733229SMatthew Dillon
116 *c9733229SMatthew Dillon /*
117 *c9733229SMatthew Dillon * Iterate through the two directory lists, diffing as we go.
118 *c9733229SMatthew Dillon */
119 *c9733229SMatthew Dillon while (dp1 != edp1 || dp2 != edp2) {
120 *c9733229SMatthew Dillon dent1 = dp1 != edp1 ? *dp1 : NULL;
121 *c9733229SMatthew Dillon dent2 = dp2 != edp2 ? *dp2 : NULL;
122 *c9733229SMatthew Dillon
123 *c9733229SMatthew Dillon pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
124 *c9733229SMatthew Dillon strcmp(dent1->d_name, dent2->d_name);
125 *c9733229SMatthew Dillon if (pos == 0) {
126 *c9733229SMatthew Dillon /* file exists in both dirs, diff it */
127 *c9733229SMatthew Dillon diffit(dent1, path1, dirlen1, path2, dirlen2, flags);
128 *c9733229SMatthew Dillon dp1++;
129 *c9733229SMatthew Dillon dp2++;
130 *c9733229SMatthew Dillon } else if (pos < 0) {
131 *c9733229SMatthew Dillon /* file only in first dir, only diff if -N */
132 *c9733229SMatthew Dillon if (Nflag) {
133 *c9733229SMatthew Dillon diffit(dent1, path1, dirlen1, path2, dirlen2,
134 *c9733229SMatthew Dillon flags);
135 *c9733229SMatthew Dillon } else {
136 *c9733229SMatthew Dillon print_only(path1, dirlen1, dent1->d_name);
137 *c9733229SMatthew Dillon status |= 1;
138 *c9733229SMatthew Dillon }
139 *c9733229SMatthew Dillon dp1++;
140 *c9733229SMatthew Dillon } else {
141 *c9733229SMatthew Dillon /* file only in second dir, only diff if -N or -P */
142 *c9733229SMatthew Dillon if (Nflag || Pflag) {
143 *c9733229SMatthew Dillon diffit(dent2, path1, dirlen1, path2, dirlen2,
144 *c9733229SMatthew Dillon flags);
145 *c9733229SMatthew Dillon } else {
146 *c9733229SMatthew Dillon print_only(path2, dirlen2, dent2->d_name);
147 *c9733229SMatthew Dillon status |= 1;
148 *c9733229SMatthew Dillon }
149 *c9733229SMatthew Dillon dp2++;
150 *c9733229SMatthew Dillon }
151 *c9733229SMatthew Dillon }
152 *c9733229SMatthew Dillon
153 *c9733229SMatthew Dillon closem:
154 *c9733229SMatthew Dillon if (dirp1 != NULL) {
155 *c9733229SMatthew Dillon for (dp1 = dirp1; dp1 < edp1; dp1++)
156 *c9733229SMatthew Dillon free(*dp1);
157 *c9733229SMatthew Dillon free(dirp1);
158 *c9733229SMatthew Dillon }
159 *c9733229SMatthew Dillon if (dirp2 != NULL) {
160 *c9733229SMatthew Dillon for (dp2 = dirp2; dp2 < edp2; dp2++)
161 *c9733229SMatthew Dillon free(*dp2);
162 *c9733229SMatthew Dillon free(dirp2);
163 *c9733229SMatthew Dillon }
164 *c9733229SMatthew Dillon }
165 *c9733229SMatthew Dillon
166 *c9733229SMatthew Dillon /*
167 *c9733229SMatthew Dillon * Do the actual diff by calling either diffreg() or diffdir().
168 *c9733229SMatthew Dillon */
169 *c9733229SMatthew Dillon static void
diffit(struct dirent * dp,char * path1,size_t plen1,char * path2,size_t plen2,int flags)170 *c9733229SMatthew Dillon diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2,
171 *c9733229SMatthew Dillon int flags)
172 *c9733229SMatthew Dillon {
173 *c9733229SMatthew Dillon flags |= D_HEADER;
174 *c9733229SMatthew Dillon strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
175 *c9733229SMatthew Dillon if (stat(path1, &stb1) != 0) {
176 *c9733229SMatthew Dillon if (!(Nflag || Pflag) || errno != ENOENT) {
177 *c9733229SMatthew Dillon warn("%s", path1);
178 *c9733229SMatthew Dillon return;
179 *c9733229SMatthew Dillon }
180 *c9733229SMatthew Dillon flags |= D_EMPTY1;
181 *c9733229SMatthew Dillon memset(&stb1, 0, sizeof(stb1));
182 *c9733229SMatthew Dillon }
183 *c9733229SMatthew Dillon
184 *c9733229SMatthew Dillon strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2);
185 *c9733229SMatthew Dillon if (stat(path2, &stb2) != 0) {
186 *c9733229SMatthew Dillon if (!Nflag || errno != ENOENT) {
187 *c9733229SMatthew Dillon warn("%s", path2);
188 *c9733229SMatthew Dillon return;
189 *c9733229SMatthew Dillon }
190 *c9733229SMatthew Dillon flags |= D_EMPTY2;
191 *c9733229SMatthew Dillon memset(&stb2, 0, sizeof(stb2));
192 *c9733229SMatthew Dillon stb2.st_mode = stb1.st_mode;
193 *c9733229SMatthew Dillon }
194 *c9733229SMatthew Dillon if (stb1.st_mode == 0)
195 *c9733229SMatthew Dillon stb1.st_mode = stb2.st_mode;
196 *c9733229SMatthew Dillon
197 *c9733229SMatthew Dillon if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
198 *c9733229SMatthew Dillon if (rflag)
199 *c9733229SMatthew Dillon diffdir(path1, path2, flags);
200 *c9733229SMatthew Dillon else
201 *c9733229SMatthew Dillon printf("Common subdirectories: %s and %s\n",
202 *c9733229SMatthew Dillon path1, path2);
203 *c9733229SMatthew Dillon return;
204 *c9733229SMatthew Dillon }
205 *c9733229SMatthew Dillon if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
206 *c9733229SMatthew Dillon dp->d_status = D_SKIPPED1;
207 *c9733229SMatthew Dillon else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
208 *c9733229SMatthew Dillon dp->d_status = D_SKIPPED2;
209 *c9733229SMatthew Dillon else
210 *c9733229SMatthew Dillon dp->d_status = diffreg(path1, path2, flags);
211 *c9733229SMatthew Dillon print_status(dp->d_status, path1, path2, "");
212 *c9733229SMatthew Dillon }
213 *c9733229SMatthew Dillon
214 *c9733229SMatthew Dillon /*
215 *c9733229SMatthew Dillon * Returns 1 if the directory entry should be included in the
216 *c9733229SMatthew Dillon * diff, else 0. Checks the excludes list.
217 *c9733229SMatthew Dillon */
218 *c9733229SMatthew Dillon static int
selectfile(const struct dirent * dp)219 *c9733229SMatthew Dillon selectfile(const struct dirent *dp)
220 *c9733229SMatthew Dillon {
221 *c9733229SMatthew Dillon struct excludes *excl;
222 *c9733229SMatthew Dillon
223 *c9733229SMatthew Dillon if (dp->d_fileno == 0)
224 *c9733229SMatthew Dillon return (0);
225 *c9733229SMatthew Dillon
226 *c9733229SMatthew Dillon /* always skip "." and ".." */
227 *c9733229SMatthew Dillon if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
228 *c9733229SMatthew Dillon (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
229 *c9733229SMatthew Dillon return (0);
230 *c9733229SMatthew Dillon
231 *c9733229SMatthew Dillon /* check excludes list */
232 *c9733229SMatthew Dillon for (excl = excludes_list; excl != NULL; excl = excl->next)
233 *c9733229SMatthew Dillon if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0)
234 *c9733229SMatthew Dillon return (0);
235 *c9733229SMatthew Dillon
236 *c9733229SMatthew Dillon return (1);
237 *c9733229SMatthew Dillon }
238