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