1 /* $OpenBSD: diffdir.c,v 1.39 2010/11/08 15:49:13 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/param.h> 24 #include <sys/stat.h> 25 26 #include <dirent.h> 27 #include <err.h> 28 #include <errno.h> 29 #include <fcntl.h> 30 #include <fnmatch.h> 31 #include <paths.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <unistd.h> 36 37 #include "diff.h" 38 #include "xmalloc.h" 39 40 static int selectfile(struct dirent *); 41 static struct dirent **slurpdir(char *, int); 42 static void diffit(struct dirent *, char *, size_t, char *, size_t, int); 43 44 #define d_status d_type /* we need to store status for -l */ 45 46 /* 47 * Diff directory traversal. Will be called recursively if -r was specified. 48 */ 49 void 50 diffdir(char *p1, char *p2, int flags) 51 { 52 struct dirent *dent1, **dp1, **dirp1 = NULL; 53 struct dirent *dent2, **dp2, **dirp2 = NULL; 54 size_t dirlen1, dirlen2; 55 char path1[MAXPATHLEN], path2[MAXPATHLEN]; 56 int pos; 57 58 dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1)); 59 if (dirlen1 >= sizeof(path1) - 1) { 60 warnx("%s: %s", p1, strerror(ENAMETOOLONG)); 61 status = 2; 62 return; 63 } 64 if (path1[dirlen1 - 1] != '/') { 65 path1[dirlen1++] = '/'; 66 path1[dirlen1] = '\0'; 67 } 68 dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2)); 69 if (dirlen2 >= sizeof(path2) - 1) { 70 warnx("%s: %s", p2, strerror(ENAMETOOLONG)); 71 status = 2; 72 return; 73 } 74 if (path2[dirlen2 - 1] != '/') { 75 path2[dirlen2++] = '/'; 76 path2[dirlen2] = '\0'; 77 } 78 79 /* get a list of the entries in each directory */ 80 dp1 = dirp1 = slurpdir(path1, Nflag + Pflag); 81 dp2 = dirp2 = slurpdir(path2, Nflag); 82 if (dirp1 == NULL || dirp2 == NULL) 83 goto closem; 84 85 /* 86 * If we were given a starting point, find it. 87 */ 88 if (start != NULL) { 89 while (*dp1 != NULL && strcmp((*dp1)->d_name, start) < 0) 90 dp1++; 91 while (*dp2 != NULL && strcmp((*dp2)->d_name, start) < 0) 92 dp2++; 93 } 94 95 /* 96 * Iterate through the two directory lists, diffing as we go. 97 */ 98 while (*dp1 != NULL || *dp2 != NULL) { 99 dent1 = *dp1; 100 dent2 = *dp2; 101 102 pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 : 103 strcmp(dent1->d_name, dent2->d_name); 104 if (pos == 0) { 105 /* file exists in both dirs, diff it */ 106 diffit(dent1, path1, dirlen1, path2, dirlen2, flags); 107 dp1++; 108 dp2++; 109 } else if (pos < 0) { 110 /* file only in first dir, only diff if -N */ 111 if (Nflag) 112 diffit(dent1, path1, dirlen1, path2, dirlen2, 113 flags); 114 else if (lflag) 115 dent1->d_status |= D_ONLY; 116 else 117 print_only(path1, dirlen1, dent1->d_name); 118 dp1++; 119 } else { 120 /* file only in second dir, only diff if -N or -P */ 121 if (Nflag || Pflag) 122 diffit(dent2, path1, dirlen1, path2, dirlen2, 123 flags); 124 else if (lflag) 125 dent2->d_status |= D_ONLY; 126 else 127 print_only(path2, dirlen2, dent2->d_name); 128 dp2++; 129 } 130 } 131 if (lflag) { 132 path1[dirlen1] = '\0'; 133 path2[dirlen2] = '\0'; 134 for (dp1 = dirp1; (dent1 = *dp1) != NULL; dp1++) { 135 print_status(dent1->d_status, path1, path2, 136 dent1->d_name); 137 } 138 for (dp2 = dirp2; (dent2 = *dp2) != NULL; dp2++) { 139 if (dent2->d_status == D_ONLY) 140 print_status(dent2->d_status, path2, NULL, 141 dent2->d_name); 142 } 143 } 144 145 closem: 146 if (dirp1 != NULL) { 147 for (dp1 = dirp1; (dent1 = *dp1) != NULL; dp1++) 148 xfree(dent1); 149 xfree(dirp1); 150 } 151 if (dirp2 != NULL) { 152 for (dp2 = dirp2; (dent2 = *dp2) != NULL; dp2++) 153 xfree(dent2); 154 xfree(dirp2); 155 } 156 } 157 158 /* 159 * Read in a whole directory, culling out the "excluded" files. 160 * Returns an array of struct dirent *'s in alphabetic order. 161 * Caller is responsible for free()ing each array element and the array itself. 162 */ 163 static struct dirent ** 164 slurpdir(char *dirname, int enoentok) 165 { 166 struct dirent **namelist = NULL; 167 int rval; 168 169 rval = scandir(dirname, &namelist, selectfile, alphasort); 170 if (rval == -1) { 171 if (enoentok && errno == ENOENT) { 172 namelist = xmalloc(sizeof(struct dirent *)); 173 namelist[0] = NULL; 174 } else { 175 warn("%s", dirname); 176 } 177 } 178 179 return (namelist); 180 } 181 182 /* 183 * Do the actual diff by calling either diffreg() or diffdir(). 184 */ 185 static void 186 diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2, 187 int flags) 188 { 189 flags |= D_HEADER; 190 strlcpy(path1 + plen1, dp->d_name, MAXPATHLEN - plen1); 191 if (stat(path1, &stb1) != 0) { 192 if (!(Nflag || Pflag) || errno != ENOENT) { 193 warn("%s", path1); 194 return; 195 } 196 flags |= D_EMPTY1; 197 memset(&stb1, 0, sizeof(stb1)); 198 } 199 200 strlcpy(path2 + plen2, dp->d_name, MAXPATHLEN - plen2); 201 if (stat(path2, &stb2) != 0) { 202 if (!Nflag || errno != ENOENT) { 203 warn("%s", path2); 204 return; 205 } 206 flags |= D_EMPTY2; 207 memset(&stb2, 0, sizeof(stb2)); 208 stb2.st_mode = stb1.st_mode; 209 } 210 if (stb1.st_mode == 0) 211 stb1.st_mode = stb2.st_mode; 212 213 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 214 if (rflag) 215 diffdir(path1, path2, flags); 216 else if (lflag) 217 dp->d_status |= D_COMMON; 218 else 219 printf("Common subdirectories: %s and %s\n", 220 path1, path2); 221 return; 222 } 223 if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode)) 224 dp->d_status = D_SKIPPED1; 225 else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode)) 226 dp->d_status = D_SKIPPED2; 227 else 228 dp->d_status = diffreg(path1, path2, flags); 229 if (!lflag) 230 print_status(dp->d_status, path1, path2, ""); 231 } 232 233 /* 234 * Returns 1 if the directory entry should be included in the 235 * diff, else 0. Checks the excludes list. 236 */ 237 static int 238 selectfile(struct dirent *dp) 239 { 240 struct excludes *excl; 241 242 if (dp->d_fileno == 0) 243 return (0); 244 245 /* always skip "." and ".." */ 246 if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || 247 (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) 248 return (0); 249 250 /* check excludes list */ 251 for (excl = excludes_list; excl != NULL; excl = excl->next) 252 if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0) 253 return (0); 254 255 return (1); 256 } 257