Annotation of src/usr.bin/diff/diffdir.c, Revision 1.43
1.43 ! deraadt 1: /* $OpenBSD: diffdir.c,v 1.42 2014/05/20 01:25:23 guenther Exp $ */
1.2 deraadt 2:
3: /*
1.39 millert 4: * Copyright (c) 2003, 2010 Todd C. Miller <Todd.Miller@courtesan.com>
1.2 deraadt 5: *
1.19 millert 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.
1.2 deraadt 9: *
1.19 millert 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.
1.2 deraadt 21: */
1.19 millert 22:
23: #include <sys/stat.h>
1.3 tedu 24:
1.12 millert 25: #include <dirent.h>
1.19 millert 26: #include <err.h>
1.12 millert 27: #include <errno.h>
28: #include <fcntl.h>
1.19 millert 29: #include <fnmatch.h>
30: #include <paths.h>
1.8 tedu 31: #include <stdio.h>
1.3 tedu 32: #include <stdlib.h>
1.12 millert 33: #include <string.h>
1.3 tedu 34: #include <unistd.h>
1.43 ! deraadt 35: #include <limits.h>
1.1 deraadt 36:
37: #include "diff.h"
1.31 ray 38: #include "xmalloc.h"
1.3 tedu 39:
1.41 guenther 40: static int selectfile(const struct dirent *);
1.33 ray 41: static void diffit(struct dirent *, char *, size_t, char *, size_t, int);
1.3 tedu 42:
1.22 millert 43: #define d_status d_type /* we need to store status for -l */
44:
1.1 deraadt 45: /*
1.26 otto 46: * Diff directory traversal. Will be called recursively if -r was specified.
1.1 deraadt 47: */
1.19 millert 48: void
1.33 ray 49: diffdir(char *p1, char *p2, int flags)
1.19 millert 50: {
1.40 millert 51: struct dirent *dent1, **dp1, **edp1, **dirp1 = NULL;
52: struct dirent *dent2, **dp2, **edp2, **dirp2 = NULL;
1.19 millert 53: size_t dirlen1, dirlen2;
1.43 ! deraadt 54: char path1[PATH_MAX], path2[PATH_MAX];
1.19 millert 55: int pos;
56:
57: dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
58: if (dirlen1 >= sizeof(path1) - 1) {
1.42 guenther 59: warnc(ENAMETOOLONG, "%s", p1);
1.19 millert 60: status = 2;
61: return;
62: }
63: if (path1[dirlen1 - 1] != '/') {
64: path1[dirlen1++] = '/';
65: path1[dirlen1] = '\0';
66: }
67: dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
68: if (dirlen2 >= sizeof(path2) - 1) {
1.42 guenther 69: warnc(ENAMETOOLONG, "%s", p2);
1.19 millert 70: status = 2;
71: return;
72: }
73: if (path2[dirlen2 - 1] != '/') {
74: path2[dirlen2++] = '/';
75: path2[dirlen2] = '\0';
76: }
1.1 deraadt 77:
1.40 millert 78: /*
79: * Get a list of entries in each directory, skipping "excluded" files
80: * and sorting alphabetically.
81: */
82: pos = scandir(path1, &dirp1, selectfile, alphasort);
83: if (pos == -1) {
84: if (errno == ENOENT && (Nflag || Pflag)) {
85: pos = 0;
86: } else {
87: warn("%s", path1);
88: goto closem;
89: }
90: }
91: dp1 = dirp1;
92: edp1 = dirp1 + pos;
93:
94: pos = scandir(path2, &dirp2, selectfile, alphasort);
95: if (pos == -1) {
96: if (errno == ENOENT && Nflag) {
97: pos = 0;
98: } else {
99: warn("%s", path2);
100: goto closem;
101: }
102: }
103: dp2 = dirp2;
104: edp2 = dirp2 + pos;
1.1 deraadt 105:
1.19 millert 106: /*
107: * If we were given a starting point, find it.
108: */
109: if (start != NULL) {
1.40 millert 110: while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0)
1.19 millert 111: dp1++;
1.40 millert 112: while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0)
1.19 millert 113: dp2++;
114: }
1.8 tedu 115:
1.19 millert 116: /*
117: * Iterate through the two directory lists, diffing as we go.
118: */
1.40 millert 119: while (dp1 != edp1 || dp2 != edp2) {
120: dent1 = dp1 != edp1 ? *dp1 : NULL;
121: dent2 = dp2 != edp2 ? *dp2 : NULL;
1.19 millert 122:
123: pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
124: strcmp(dent1->d_name, dent2->d_name);
125: if (pos == 0) {
126: /* file exists in both dirs, diff it */
1.33 ray 127: diffit(dent1, path1, dirlen1, path2, dirlen2, flags);
1.19 millert 128: dp1++;
129: dp2++;
130: } else if (pos < 0) {
131: /* file only in first dir, only diff if -N */
132: if (Nflag)
1.33 ray 133: diffit(dent1, path1, dirlen1, path2, dirlen2,
134: flags);
1.22 millert 135: else if (lflag)
136: dent1->d_status |= D_ONLY;
1.24 millert 137: else
1.25 millert 138: print_only(path1, dirlen1, dent1->d_name);
1.19 millert 139: dp1++;
1.1 deraadt 140: } else {
1.20 millert 141: /* file only in second dir, only diff if -N or -P */
142: if (Nflag || Pflag)
1.33 ray 143: diffit(dent2, path1, dirlen1, path2, dirlen2,
144: flags);
1.22 millert 145: else if (lflag)
146: dent2->d_status |= D_ONLY;
1.24 millert 147: else
1.25 millert 148: print_only(path2, dirlen2, dent2->d_name);
1.19 millert 149: dp2++;
1.1 deraadt 150: }
151: }
1.22 millert 152: if (lflag) {
1.23 millert 153: path1[dirlen1] = '\0';
154: path2[dirlen2] = '\0';
1.22 millert 155: for (dp1 = dirp1; (dent1 = *dp1) != NULL; dp1++) {
156: print_status(dent1->d_status, path1, path2,
157: dent1->d_name);
158: }
159: for (dp2 = dirp2; (dent2 = *dp2) != NULL; dp2++) {
160: if (dent2->d_status == D_ONLY)
161: print_status(dent2->d_status, path2, NULL,
162: dent2->d_name);
163: }
164: }
1.19 millert 165:
1.37 ray 166: closem:
1.39 millert 167: if (dirp1 != NULL) {
1.40 millert 168: for (dp1 = dirp1; dp1 < edp1; dp1++)
169: xfree(*dp1);
1.31 ray 170: xfree(dirp1);
1.19 millert 171: }
1.39 millert 172: if (dirp2 != NULL) {
1.40 millert 173: for (dp2 = dirp2; dp2 < edp2; dp2++)
174: xfree(*dp2);
1.31 ray 175: xfree(dirp2);
1.1 deraadt 176: }
177: }
178:
1.19 millert 179: /*
180: * Do the actual diff by calling either diffreg() or diffdir().
181: */
1.3 tedu 182: static void
1.33 ray 183: diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2,
184: int flags)
1.1 deraadt 185: {
1.33 ray 186: flags |= D_HEADER;
1.43 ! deraadt 187: strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
1.19 millert 188: if (stat(path1, &stb1) != 0) {
1.20 millert 189: if (!(Nflag || Pflag) || errno != ENOENT) {
1.19 millert 190: warn("%s", path1);
191: return;
1.1 deraadt 192: }
1.19 millert 193: flags |= D_EMPTY1;
194: memset(&stb1, 0, sizeof(stb1));
1.1 deraadt 195: }
196:
1.43 ! deraadt 197: strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2);
1.19 millert 198: if (stat(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:
210: if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1.20 millert 211: if (rflag)
1.33 ray 212: diffdir(path1, path2, flags);
1.22 millert 213: else if (lflag)
214: dp->d_status |= D_COMMON;
1.24 millert 215: else
1.19 millert 216: printf("Common subdirectories: %s and %s\n",
217: path1, path2);
218: return;
1.1 deraadt 219: }
1.27 millert 220: if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
221: dp->d_status = D_SKIPPED1;
222: else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
223: dp->d_status = D_SKIPPED2;
224: else
225: dp->d_status = diffreg(path1, path2, flags);
1.23 millert 226: if (!lflag)
1.36 ray 227: print_status(dp->d_status, path1, path2, "");
1.1 deraadt 228: }
229:
1.19 millert 230: /*
1.39 millert 231: * Returns 1 if the directory entry should be included in the
232: * diff, else 0. Checks the excludes list.
1.19 millert 233: */
234: static int
1.41 guenther 235: selectfile(const struct dirent *dp)
1.1 deraadt 236: {
1.19 millert 237: struct excludes *excl;
1.15 tedu 238:
1.39 millert 239: if (dp->d_fileno == 0)
240: return (0);
241:
1.19 millert 242: /* always skip "." and ".." */
1.39 millert 243: if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
244: (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
245: return (0);
1.1 deraadt 246:
1.19 millert 247: /* check excludes list */
248: for (excl = excludes_list; excl != NULL; excl = excl->next)
1.39 millert 249: if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0)
250: return (0);
1.1 deraadt 251:
1.39 millert 252: return (1);
1.1 deraadt 253: }