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