[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.31

1.31    ! ray         1: /*     $OpenBSD: diffdir.c,v 1.30 2005/06/15 18:44:01 millert 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.31    ! ray        24: static const char rcsid[] = "$OpenBSD: diffdir.c,v 1.30 2005/06/15 18:44:01 millert 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.19      millert    47: static void diffit(struct dirent *, char *, size_t, char *, size_t);
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
                     55: diffdir(char *p1, char *p2)
                     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 */
                    112:                        diffit(dent1, path1, dirlen1, path2, dirlen2);
                    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);
1.22      millert   119:                        else if (lflag)
                    120:                                dent1->d_status |= D_ONLY;
1.24      millert   121:                        else
1.25      millert   122:                                print_only(path1, dirlen1, dent1->d_name);
1.19      millert   123:                        dp1++;
1.1       deraadt   124:                } else {
1.20      millert   125:                        /* file only in second dir, only diff if -N or -P */
                    126:                        if (Nflag || Pflag)
1.19      millert   127:                                diffit(dent2, path1, dirlen1, path2, dirlen2);
1.22      millert   128:                        else if (lflag)
                    129:                                dent2->d_status |= D_ONLY;
1.24      millert   130:                        else
1.25      millert   131:                                print_only(path2, dirlen2, dent2->d_name);
1.19      millert   132:                        dp2++;
1.1       deraadt   133:                }
                    134:        }
1.22      millert   135:        if (lflag) {
1.23      millert   136:                path1[dirlen1] = '\0';
                    137:                path2[dirlen2] = '\0';
1.22      millert   138:                for (dp1 = dirp1; (dent1 = *dp1) != NULL; dp1++) {
                    139:                        print_status(dent1->d_status, path1, path2,
                    140:                            dent1->d_name);
                    141:                }
                    142:                for (dp2 = dirp2; (dent2 = *dp2) != NULL; dp2++) {
                    143:                        if (dent2->d_status == D_ONLY)
                    144:                                print_status(dent2->d_status, path2, NULL,
                    145:                                    dent2->d_name);
                    146:                }
                    147:        }
1.19      millert   148:
                    149:        if (dirbuf1 != NULL) {
1.31    ! ray       150:                xfree(dirp1);
        !           151:                xfree(dirbuf1);
1.19      millert   152:        }
                    153:        if (dirbuf2 != NULL) {
1.31    ! ray       154:                xfree(dirp2);
        !           155:                xfree(dirbuf2);
1.1       deraadt   156:        }
                    157: }
                    158:
1.19      millert   159: /*
                    160:  * Read in a whole directory's worth of struct dirents, culling
                    161:  * out the "excluded" ones.
                    162:  * Returns an array of struct dirent *'s that point into the buffer
                    163:  * returned via bufp.  Caller is responsible for free()ing both of these.
                    164:  */
                    165: static struct dirent **
1.20      millert   166: slurpdir(char *path, char **bufp, int enoentok)
1.1       deraadt   167: {
1.19      millert   168:        char *buf, *ebuf, *cp;
1.29      otto      169:        size_t bufsize, have, need;
1.19      millert   170:        long base;
                    171:        int fd, nbytes, entries;
                    172:        struct stat sb;
                    173:        struct dirent **dirlist, *dp;
                    174:
                    175:        *bufp = NULL;
                    176:        if ((fd = open(path, O_RDONLY, 0644)) == -1) {
                    177:                static struct dirent *dummy;
                    178:
1.20      millert   179:                if (!enoentok || errno != ENOENT) {
1.19      millert   180:                        warn("%s", path);
                    181:                        return (NULL);
                    182:                }
                    183:                return (&dummy);
                    184:        }
1.29      otto      185:        if (fstat(fd, &sb) == -1) {
                    186:                warn("%s", path);
                    187:                close(fd);
                    188:                return (NULL);
                    189:        }
                    190:
                    191:        need = roundup(sb.st_blksize, sizeof(struct dirent));
                    192:        have = bufsize = roundup(MAX(sb.st_size, sb.st_blksize),
                    193:            sizeof(struct dirent)) + need;
1.31    ! ray       194:        ebuf = buf = xmalloc(bufsize);
1.1       deraadt   195:
1.28      millert   196:        do {
1.29      otto      197:                if (have < need) {
                    198:                    bufsize += need;
                    199:                    have += need;
1.31    ! ray       200:                    cp = xrealloc(buf, 1, bufsize);
1.28      millert   201:                    ebuf = cp + (ebuf - buf);
                    202:                    buf = cp;
                    203:                }
1.29      otto      204:                nbytes = getdirentries(fd, ebuf, have, &base);
1.28      millert   205:                if (nbytes == -1) {
1.29      otto      206:                        warn("%s", path);
1.31    ! ray       207:                        xfree(buf);
1.29      otto      208:                        close(fd);
1.28      millert   209:                        return (NULL);
                    210:                }
                    211:                ebuf += nbytes;
1.29      otto      212:                have -= nbytes;
                    213:        } while (nbytes != 0);
1.19      millert   214:        close(fd);
1.1       deraadt   215:
1.19      millert   216:        /*
                    217:         * We now have all the directory entries in our buffer.
                    218:         * However, in order to easily sort them we need to convert
                    219:         * the buffer into an array.
                    220:         */
                    221:        for (entries = 0, cp = buf; cp < ebuf; ) {
                    222:                dp = (struct dirent *)cp;
1.30      millert   223:                if (dp->d_fileno != 0)
1.19      millert   224:                        entries++;
                    225:                if (dp->d_reclen <= 0)
                    226:                        break;
                    227:                cp += dp->d_reclen;
                    228:        }
1.31    ! ray       229:        dirlist = xmalloc(sizeof(struct dirent *) * (entries + 1));
1.19      millert   230:        for (entries = 0, cp = buf; cp < ebuf; ) {
                    231:                dp = (struct dirent *)cp;
1.30      millert   232:                if (dp->d_fileno != 0 && !excluded(dp->d_name)) {
1.22      millert   233:                        dp->d_status = 0;
1.19      millert   234:                        dirlist[entries++] = dp;
1.22      millert   235:                }
1.19      millert   236:                if (dp->d_reclen <= 0)
                    237:                        break;
                    238:                cp += dp->d_reclen;
                    239:        }
                    240:        dirlist[entries] = NULL;
1.1       deraadt   241:
1.29      otto      242:        qsort(dirlist, entries, sizeof(struct dirent *), dircompare);
1.1       deraadt   243:
1.19      millert   244:        *bufp = buf;
                    245:        return (dirlist);
1.1       deraadt   246: }
                    247:
1.19      millert   248: /*
                    249:  * Compare d_name in two dirent structures; for qsort(3).
                    250:  */
1.8       tedu      251: static int
1.19      millert   252: dircompare(const void *vp1, const void *vp2)
1.1       deraadt   253: {
1.19      millert   254:        struct dirent *dp1 = *((struct dirent **) vp1);
                    255:        struct dirent *dp2 = *((struct dirent **) vp2);
1.8       tedu      256:
1.19      millert   257:        return (strcmp(dp1->d_name, dp2->d_name));
1.1       deraadt   258: }
                    259:
1.19      millert   260: /*
                    261:  * Do the actual diff by calling either diffreg() or diffdir().
                    262:  */
1.3       tedu      263: static void
1.19      millert   264: diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2)
1.1       deraadt   265: {
1.19      millert   266:        int flags = D_HEADER;
                    267:
                    268:        strlcpy(path1 + plen1, dp->d_name, MAXPATHLEN - plen1);
                    269:        if (stat(path1, &stb1) != 0) {
1.20      millert   270:                if (!(Nflag || Pflag) || errno != ENOENT) {
1.19      millert   271:                        warn("%s", path1);
                    272:                        return;
1.1       deraadt   273:                }
1.19      millert   274:                flags |= D_EMPTY1;
                    275:                memset(&stb1, 0, sizeof(stb1));
1.1       deraadt   276:        }
                    277:
1.19      millert   278:        strlcpy(path2 + plen2, dp->d_name, MAXPATHLEN - plen2);
                    279:        if (stat(path2, &stb2) != 0) {
                    280:                if (!Nflag || errno != ENOENT) {
                    281:                        warn("%s", path2);
                    282:                        return;
                    283:                }
                    284:                flags |= D_EMPTY2;
                    285:                memset(&stb2, 0, sizeof(stb2));
                    286:                stb2.st_mode = stb1.st_mode;
                    287:        }
                    288:        if (stb1.st_mode == 0)
                    289:                stb1.st_mode = stb2.st_mode;
                    290:
                    291:        if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1.20      millert   292:                if (rflag)
                    293:                        diffdir(path1, path2);
1.22      millert   294:                else if (lflag)
                    295:                        dp->d_status |= D_COMMON;
1.24      millert   296:                else
1.19      millert   297:                        printf("Common subdirectories: %s and %s\n",
                    298:                            path1, path2);
                    299:                return;
1.1       deraadt   300:        }
1.27      millert   301:        if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
                    302:                dp->d_status = D_SKIPPED1;
                    303:        else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
                    304:                dp->d_status = D_SKIPPED2;
                    305:        else
                    306:                dp->d_status = diffreg(path1, path2, flags);
1.23      millert   307:        if (!lflag)
                    308:                print_status(dp->d_status, path1, path2, NULL);
1.1       deraadt   309: }
                    310:
1.19      millert   311: /*
                    312:  * Exclude the given directory entry?
                    313:  */
                    314: static int
                    315: excluded(const char *entry)
1.1       deraadt   316: {
1.19      millert   317:        struct excludes *excl;
1.15      tedu      318:
1.19      millert   319:        /* always skip "." and ".." */
                    320:        if (entry[0] == '.' &&
                    321:            (entry[1] == '\0' || (entry[1] == '.' && entry[2] == '\0')))
1.15      tedu      322:                return (1);
1.1       deraadt   323:
1.19      millert   324:        /* check excludes list */
                    325:        for (excl = excludes_list; excl != NULL; excl = excl->next)
                    326:                if (fnmatch(excl->pattern, entry, FNM_PATHNAME) == 0)
                    327:                        return (1);
1.1       deraadt   328:
                    329:        return (0);
                    330: }