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