[BACK]Return to diffdir.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / diff

Annotation of src/usr.bin/diff/diffdir.c, Revision 1.33

1.33    ! ray         1: /*     $OpenBSD: diffdir.c,v 1.32 2007/06/09 05:16:21 ray Exp $        */
1.2       deraadt     2:
                      3: /*
1.19      millert     4:  * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.2       deraadt     5:  *
1.19      millert     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.
1.2       deraadt     9:  *
1.19      millert    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.
1.2       deraadt    21:  */
                     22:
1.19      millert    23: #ifndef lint
1.33    ! ray        24: static const char rcsid[] = "$OpenBSD: diffdir.c,v 1.32 2007/06/09 05:16:21 ray Exp $";
1.19      millert    25: #endif /* not lint */
                     26:
                     27: #include <sys/param.h>
                     28: #include <sys/stat.h>
1.3       tedu       29:
1.12      millert    30: #include <dirent.h>
1.19      millert    31: #include <err.h>
1.12      millert    32: #include <errno.h>
                     33: #include <fcntl.h>
1.19      millert    34: #include <fnmatch.h>
                     35: #include <paths.h>
1.8       tedu       36: #include <stdio.h>
1.3       tedu       37: #include <stdlib.h>
1.12      millert    38: #include <string.h>
1.3       tedu       39: #include <unistd.h>
1.1       deraadt    40:
                     41: #include "diff.h"
1.31      ray        42: #include "xmalloc.h"
1.3       tedu       43:
1.19      millert    44: static int dircompare(const void *, const void *);
                     45: static int excluded(const char *);
1.20      millert    46: static struct dirent **slurpdir(char *, char **, int);
1.33    ! ray        47: static void diffit(struct dirent *, char *, size_t, char *, size_t, int);
1.3       tedu       48:
1.22      millert    49: #define d_status       d_type          /* we need to store status for -l */
                     50:
1.1       deraadt    51: /*
1.26      otto       52:  * Diff directory traversal. Will be called recursively if -r was specified.
1.1       deraadt    53:  */
1.19      millert    54: void
1.33    ! ray        55: diffdir(char *p1, char *p2, int flags)
1.19      millert    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:        }
1.1       deraadt    84:
1.19      millert    85:        /* get a list of the entries in each directory */
1.20      millert    86:        dp1 = dirp1 = slurpdir(path1, &dirbuf1, Nflag + Pflag);
                     87:        dp2 = dirp2 = slurpdir(path2, &dirbuf2, Nflag);
1.19      millert    88:        if (dirp1 == NULL || dirp2 == NULL)
                     89:                return;
1.1       deraadt    90:
1.19      millert    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:        }
1.8       tedu      100:
1.19      millert   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 */
1.33    ! ray       112:                        diffit(dent1, path1, dirlen1, path2, dirlen2, flags);
1.19      millert   113:                        dp1++;
                    114:                        dp2++;
                    115:                } else if (pos < 0) {
                    116:                        /* file only in first dir, only diff if -N */
                    117:                        if (Nflag)
1.33    ! ray       118:                                diffit(dent1, path1, dirlen1, path2, dirlen2,
        !           119:                                    flags);
1.22      millert   120:                        else if (lflag)
                    121:                                dent1->d_status |= D_ONLY;
1.24      millert   122:                        else
1.25      millert   123:                                print_only(path1, dirlen1, dent1->d_name);
1.19      millert   124:                        dp1++;
1.1       deraadt   125:                } else {
1.20      millert   126:                        /* file only in second dir, only diff if -N or -P */
                    127:                        if (Nflag || Pflag)
1.33    ! ray       128:                                diffit(dent2, path1, dirlen1, path2, dirlen2,
        !           129:                                    flags);
1.22      millert   130:                        else if (lflag)
                    131:                                dent2->d_status |= D_ONLY;
1.24      millert   132:                        else
1.25      millert   133:                                print_only(path2, dirlen2, dent2->d_name);
1.19      millert   134:                        dp2++;
1.1       deraadt   135:                }
                    136:        }
1.22      millert   137:        if (lflag) {
1.23      millert   138:                path1[dirlen1] = '\0';
                    139:                path2[dirlen2] = '\0';
1.22      millert   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:        }
1.19      millert   150:
                    151:        if (dirbuf1 != NULL) {
1.31      ray       152:                xfree(dirp1);
                    153:                xfree(dirbuf1);
1.19      millert   154:        }
                    155:        if (dirbuf2 != NULL) {
1.31      ray       156:                xfree(dirp2);
                    157:                xfree(dirbuf2);
1.1       deraadt   158:        }
                    159: }
                    160:
1.19      millert   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 **
1.20      millert   168: slurpdir(char *path, char **bufp, int enoentok)
1.1       deraadt   169: {
1.19      millert   170:        char *buf, *ebuf, *cp;
1.29      otto      171:        size_t bufsize, have, need;
1.19      millert   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:
1.20      millert   181:                if (!enoentok || errno != ENOENT) {
1.19      millert   182:                        warn("%s", path);
                    183:                        return (NULL);
                    184:                }
                    185:                return (&dummy);
                    186:        }
1.29      otto      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;
1.31      ray       196:        ebuf = buf = xmalloc(bufsize);
1.1       deraadt   197:
1.28      millert   198:        do {
1.29      otto      199:                if (have < need) {
                    200:                    bufsize += need;
                    201:                    have += need;
1.31      ray       202:                    cp = xrealloc(buf, 1, bufsize);
1.28      millert   203:                    ebuf = cp + (ebuf - buf);
                    204:                    buf = cp;
                    205:                }
1.29      otto      206:                nbytes = getdirentries(fd, ebuf, have, &base);
1.28      millert   207:                if (nbytes == -1) {
1.29      otto      208:                        warn("%s", path);
1.31      ray       209:                        xfree(buf);
1.29      otto      210:                        close(fd);
1.28      millert   211:                        return (NULL);
                    212:                }
                    213:                ebuf += nbytes;
1.29      otto      214:                have -= nbytes;
                    215:        } while (nbytes != 0);
1.19      millert   216:        close(fd);
1.1       deraadt   217:
1.19      millert   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;
1.30      millert   225:                if (dp->d_fileno != 0)
1.19      millert   226:                        entries++;
                    227:                if (dp->d_reclen <= 0)
                    228:                        break;
                    229:                cp += dp->d_reclen;
                    230:        }
1.32      ray       231:        dirlist = xmalloc(sizeof(*dirlist) * (entries + 1));
1.19      millert   232:        for (entries = 0, cp = buf; cp < ebuf; ) {
                    233:                dp = (struct dirent *)cp;
1.30      millert   234:                if (dp->d_fileno != 0 && !excluded(dp->d_name)) {
1.22      millert   235:                        dp->d_status = 0;
1.19      millert   236:                        dirlist[entries++] = dp;
1.22      millert   237:                }
1.19      millert   238:                if (dp->d_reclen <= 0)
                    239:                        break;
                    240:                cp += dp->d_reclen;
                    241:        }
                    242:        dirlist[entries] = NULL;
1.1       deraadt   243:
1.29      otto      244:        qsort(dirlist, entries, sizeof(struct dirent *), dircompare);
1.1       deraadt   245:
1.19      millert   246:        *bufp = buf;
                    247:        return (dirlist);
1.1       deraadt   248: }
                    249:
1.19      millert   250: /*
                    251:  * Compare d_name in two dirent structures; for qsort(3).
                    252:  */
1.8       tedu      253: static int
1.19      millert   254: dircompare(const void *vp1, const void *vp2)
1.1       deraadt   255: {
1.19      millert   256:        struct dirent *dp1 = *((struct dirent **) vp1);
                    257:        struct dirent *dp2 = *((struct dirent **) vp2);
1.8       tedu      258:
1.19      millert   259:        return (strcmp(dp1->d_name, dp2->d_name));
1.1       deraadt   260: }
                    261:
1.19      millert   262: /*
                    263:  * Do the actual diff by calling either diffreg() or diffdir().
                    264:  */
1.3       tedu      265: static void
1.33    ! ray       266: diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2,
        !           267:     int flags)
1.1       deraadt   268: {
1.33    ! ray       269:        flags |= D_HEADER;
1.19      millert   270:        strlcpy(path1 + plen1, dp->d_name, MAXPATHLEN - plen1);
                    271:        if (stat(path1, &stb1) != 0) {
1.20      millert   272:                if (!(Nflag || Pflag) || errno != ENOENT) {
1.19      millert   273:                        warn("%s", path1);
                    274:                        return;
1.1       deraadt   275:                }
1.19      millert   276:                flags |= D_EMPTY1;
                    277:                memset(&stb1, 0, sizeof(stb1));
1.1       deraadt   278:        }
                    279:
1.19      millert   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)) {
1.20      millert   294:                if (rflag)
1.33    ! ray       295:                        diffdir(path1, path2, flags);
1.22      millert   296:                else if (lflag)
                    297:                        dp->d_status |= D_COMMON;
1.24      millert   298:                else
1.19      millert   299:                        printf("Common subdirectories: %s and %s\n",
                    300:                            path1, path2);
                    301:                return;
1.1       deraadt   302:        }
1.27      millert   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);
1.23      millert   309:        if (!lflag)
                    310:                print_status(dp->d_status, path1, path2, NULL);
1.1       deraadt   311: }
                    312:
1.19      millert   313: /*
                    314:  * Exclude the given directory entry?
                    315:  */
                    316: static int
                    317: excluded(const char *entry)
1.1       deraadt   318: {
1.19      millert   319:        struct excludes *excl;
1.15      tedu      320:
1.19      millert   321:        /* always skip "." and ".." */
                    322:        if (entry[0] == '.' &&
                    323:            (entry[1] == '\0' || (entry[1] == '.' && entry[2] == '\0')))
1.15      tedu      324:                return (1);
1.1       deraadt   325:
1.19      millert   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);
1.1       deraadt   330:
                    331:        return (0);
                    332: }