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