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

1.36    ! ray         1: /*     $OpenBSD: diffdir.c,v 1.35 2009/10/27 23:59:37 deraadt 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:  */
1.19      millert    22:
                     23: #include <sys/param.h>
                     24: #include <sys/stat.h>
1.3       tedu       25:
1.12      millert    26: #include <dirent.h>
1.19      millert    27: #include <err.h>
1.12      millert    28: #include <errno.h>
                     29: #include <fcntl.h>
1.19      millert    30: #include <fnmatch.h>
                     31: #include <paths.h>
1.8       tedu       32: #include <stdio.h>
1.3       tedu       33: #include <stdlib.h>
1.12      millert    34: #include <string.h>
1.3       tedu       35: #include <unistd.h>
1.1       deraadt    36:
                     37: #include "diff.h"
1.31      ray        38: #include "xmalloc.h"
1.3       tedu       39:
1.19      millert    40: static int dircompare(const void *, const void *);
                     41: static int excluded(const char *);
1.20      millert    42: static struct dirent **slurpdir(char *, char **, int);
1.33      ray        43: static void diffit(struct dirent *, char *, size_t, char *, size_t, int);
1.3       tedu       44:
1.22      millert    45: #define d_status       d_type          /* we need to store status for -l */
                     46:
1.1       deraadt    47: /*
1.26      otto       48:  * Diff directory traversal. Will be called recursively if -r was specified.
1.1       deraadt    49:  */
1.19      millert    50: void
1.33      ray        51: diffdir(char *p1, char *p2, int flags)
1.19      millert    52: {
                     53:        struct dirent **dirp1, **dirp2, **dp1, **dp2;
                     54:        struct dirent *dent1, *dent2;
                     55:        size_t dirlen1, dirlen2;
                     56:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                     57:        char *dirbuf1, *dirbuf2;
                     58:        int pos;
                     59:
                     60:        dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
                     61:        if (dirlen1 >= sizeof(path1) - 1) {
                     62:                warnx("%s: %s", p1, strerror(ENAMETOOLONG));
                     63:                status = 2;
                     64:                return;
                     65:        }
                     66:        if (path1[dirlen1 - 1] != '/') {
                     67:                path1[dirlen1++] = '/';
                     68:                path1[dirlen1] = '\0';
                     69:        }
                     70:        dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
                     71:        if (dirlen2 >= sizeof(path2) - 1) {
                     72:                warnx("%s: %s", p2, strerror(ENAMETOOLONG));
                     73:                status = 2;
                     74:                return;
                     75:        }
                     76:        if (path2[dirlen2 - 1] != '/') {
                     77:                path2[dirlen2++] = '/';
                     78:                path2[dirlen2] = '\0';
                     79:        }
1.1       deraadt    80:
1.19      millert    81:        /* get a list of the entries in each directory */
1.20      millert    82:        dp1 = dirp1 = slurpdir(path1, &dirbuf1, Nflag + Pflag);
                     83:        dp2 = dirp2 = slurpdir(path2, &dirbuf2, Nflag);
1.19      millert    84:        if (dirp1 == NULL || dirp2 == NULL)
                     85:                return;
1.1       deraadt    86:
1.19      millert    87:        /*
                     88:         * If we were given a starting point, find it.
                     89:         */
                     90:        if (start != NULL) {
                     91:                while (*dp1 != NULL && strcmp((*dp1)->d_name, start) < 0)
                     92:                        dp1++;
                     93:                while (*dp2 != NULL && strcmp((*dp2)->d_name, start) < 0)
                     94:                        dp2++;
                     95:        }
1.8       tedu       96:
1.19      millert    97:        /*
                     98:         * Iterate through the two directory lists, diffing as we go.
                     99:         */
                    100:        while (*dp1 != NULL || *dp2 != NULL) {
                    101:                dent1 = *dp1;
                    102:                dent2 = *dp2;
                    103:
                    104:                pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
                    105:                    strcmp(dent1->d_name, dent2->d_name);
                    106:                if (pos == 0) {
                    107:                        /* file exists in both dirs, diff it */
1.33      ray       108:                        diffit(dent1, path1, dirlen1, path2, dirlen2, flags);
1.19      millert   109:                        dp1++;
                    110:                        dp2++;
                    111:                } else if (pos < 0) {
                    112:                        /* file only in first dir, only diff if -N */
                    113:                        if (Nflag)
1.33      ray       114:                                diffit(dent1, path1, dirlen1, path2, dirlen2,
                    115:                                    flags);
1.22      millert   116:                        else if (lflag)
                    117:                                dent1->d_status |= D_ONLY;
1.24      millert   118:                        else
1.25      millert   119:                                print_only(path1, dirlen1, dent1->d_name);
1.19      millert   120:                        dp1++;
1.1       deraadt   121:                } else {
1.20      millert   122:                        /* file only in second dir, only diff if -N or -P */
                    123:                        if (Nflag || Pflag)
1.33      ray       124:                                diffit(dent2, path1, dirlen1, path2, dirlen2,
                    125:                                    flags);
1.22      millert   126:                        else if (lflag)
                    127:                                dent2->d_status |= D_ONLY;
1.24      millert   128:                        else
1.25      millert   129:                                print_only(path2, dirlen2, dent2->d_name);
1.19      millert   130:                        dp2++;
1.1       deraadt   131:                }
                    132:        }
1.22      millert   133:        if (lflag) {
1.23      millert   134:                path1[dirlen1] = '\0';
                    135:                path2[dirlen2] = '\0';
1.22      millert   136:                for (dp1 = dirp1; (dent1 = *dp1) != NULL; dp1++) {
                    137:                        print_status(dent1->d_status, path1, path2,
                    138:                            dent1->d_name);
                    139:                }
                    140:                for (dp2 = dirp2; (dent2 = *dp2) != NULL; dp2++) {
                    141:                        if (dent2->d_status == D_ONLY)
                    142:                                print_status(dent2->d_status, path2, NULL,
                    143:                                    dent2->d_name);
                    144:                }
                    145:        }
1.19      millert   146:
                    147:        if (dirbuf1 != NULL) {
1.31      ray       148:                xfree(dirp1);
                    149:                xfree(dirbuf1);
1.19      millert   150:        }
                    151:        if (dirbuf2 != NULL) {
1.31      ray       152:                xfree(dirp2);
                    153:                xfree(dirbuf2);
1.1       deraadt   154:        }
                    155: }
                    156:
1.19      millert   157: /*
                    158:  * Read in a whole directory's worth of struct dirents, culling
                    159:  * out the "excluded" ones.
                    160:  * Returns an array of struct dirent *'s that point into the buffer
                    161:  * returned via bufp.  Caller is responsible for free()ing both of these.
                    162:  */
                    163: static struct dirent **
1.20      millert   164: slurpdir(char *path, char **bufp, int enoentok)
1.1       deraadt   165: {
1.19      millert   166:        char *buf, *ebuf, *cp;
1.29      otto      167:        size_t bufsize, have, need;
1.19      millert   168:        long base;
                    169:        int fd, nbytes, entries;
                    170:        struct stat sb;
                    171:        struct dirent **dirlist, *dp;
                    172:
                    173:        *bufp = NULL;
                    174:        if ((fd = open(path, O_RDONLY, 0644)) == -1) {
                    175:                static struct dirent *dummy;
                    176:
1.20      millert   177:                if (!enoentok || errno != ENOENT) {
1.19      millert   178:                        warn("%s", path);
                    179:                        return (NULL);
                    180:                }
                    181:                return (&dummy);
                    182:        }
1.29      otto      183:        if (fstat(fd, &sb) == -1) {
                    184:                warn("%s", path);
                    185:                close(fd);
                    186:                return (NULL);
                    187:        }
                    188:
                    189:        need = roundup(sb.st_blksize, sizeof(struct dirent));
                    190:        have = bufsize = roundup(MAX(sb.st_size, sb.st_blksize),
                    191:            sizeof(struct dirent)) + need;
1.31      ray       192:        ebuf = buf = xmalloc(bufsize);
1.1       deraadt   193:
1.28      millert   194:        do {
1.29      otto      195:                if (have < need) {
                    196:                    bufsize += need;
                    197:                    have += need;
1.31      ray       198:                    cp = xrealloc(buf, 1, bufsize);
1.28      millert   199:                    ebuf = cp + (ebuf - buf);
                    200:                    buf = cp;
                    201:                }
1.29      otto      202:                nbytes = getdirentries(fd, ebuf, have, &base);
1.28      millert   203:                if (nbytes == -1) {
1.29      otto      204:                        warn("%s", path);
1.31      ray       205:                        xfree(buf);
1.29      otto      206:                        close(fd);
1.28      millert   207:                        return (NULL);
                    208:                }
                    209:                ebuf += nbytes;
1.29      otto      210:                have -= nbytes;
                    211:        } while (nbytes != 0);
1.19      millert   212:        close(fd);
1.1       deraadt   213:
1.19      millert   214:        /*
                    215:         * We now have all the directory entries in our buffer.
                    216:         * However, in order to easily sort them we need to convert
                    217:         * the buffer into an array.
                    218:         */
                    219:        for (entries = 0, cp = buf; cp < ebuf; ) {
                    220:                dp = (struct dirent *)cp;
1.30      millert   221:                if (dp->d_fileno != 0)
1.19      millert   222:                        entries++;
                    223:                if (dp->d_reclen <= 0)
                    224:                        break;
                    225:                cp += dp->d_reclen;
                    226:        }
1.34      ray       227:        dirlist = xcalloc(sizeof(*dirlist), entries + 1);
1.19      millert   228:        for (entries = 0, cp = buf; cp < ebuf; ) {
                    229:                dp = (struct dirent *)cp;
1.30      millert   230:                if (dp->d_fileno != 0 && !excluded(dp->d_name)) {
1.22      millert   231:                        dp->d_status = 0;
1.19      millert   232:                        dirlist[entries++] = dp;
1.22      millert   233:                }
1.19      millert   234:                if (dp->d_reclen <= 0)
                    235:                        break;
                    236:                cp += dp->d_reclen;
                    237:        }
                    238:        dirlist[entries] = NULL;
1.1       deraadt   239:
1.29      otto      240:        qsort(dirlist, entries, sizeof(struct dirent *), dircompare);
1.1       deraadt   241:
1.19      millert   242:        *bufp = buf;
                    243:        return (dirlist);
1.1       deraadt   244: }
                    245:
1.19      millert   246: /*
                    247:  * Compare d_name in two dirent structures; for qsort(3).
                    248:  */
1.8       tedu      249: static int
1.19      millert   250: dircompare(const void *vp1, const void *vp2)
1.1       deraadt   251: {
1.19      millert   252:        struct dirent *dp1 = *((struct dirent **) vp1);
                    253:        struct dirent *dp2 = *((struct dirent **) vp2);
1.8       tedu      254:
1.19      millert   255:        return (strcmp(dp1->d_name, dp2->d_name));
1.1       deraadt   256: }
                    257:
1.19      millert   258: /*
                    259:  * Do the actual diff by calling either diffreg() or diffdir().
                    260:  */
1.3       tedu      261: static void
1.33      ray       262: diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2,
                    263:     int flags)
1.1       deraadt   264: {
1.33      ray       265:        flags |= D_HEADER;
1.19      millert   266:        strlcpy(path1 + plen1, dp->d_name, MAXPATHLEN - plen1);
                    267:        if (stat(path1, &stb1) != 0) {
1.20      millert   268:                if (!(Nflag || Pflag) || errno != ENOENT) {
1.19      millert   269:                        warn("%s", path1);
                    270:                        return;
1.1       deraadt   271:                }
1.19      millert   272:                flags |= D_EMPTY1;
                    273:                memset(&stb1, 0, sizeof(stb1));
1.1       deraadt   274:        }
                    275:
1.19      millert   276:        strlcpy(path2 + plen2, dp->d_name, MAXPATHLEN - plen2);
                    277:        if (stat(path2, &stb2) != 0) {
                    278:                if (!Nflag || errno != ENOENT) {
                    279:                        warn("%s", path2);
                    280:                        return;
                    281:                }
                    282:                flags |= D_EMPTY2;
                    283:                memset(&stb2, 0, sizeof(stb2));
                    284:                stb2.st_mode = stb1.st_mode;
                    285:        }
                    286:        if (stb1.st_mode == 0)
                    287:                stb1.st_mode = stb2.st_mode;
                    288:
                    289:        if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
1.20      millert   290:                if (rflag)
1.33      ray       291:                        diffdir(path1, path2, flags);
1.22      millert   292:                else if (lflag)
                    293:                        dp->d_status |= D_COMMON;
1.24      millert   294:                else
1.19      millert   295:                        printf("Common subdirectories: %s and %s\n",
                    296:                            path1, path2);
                    297:                return;
1.1       deraadt   298:        }
1.27      millert   299:        if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
                    300:                dp->d_status = D_SKIPPED1;
                    301:        else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
                    302:                dp->d_status = D_SKIPPED2;
                    303:        else
                    304:                dp->d_status = diffreg(path1, path2, flags);
1.23      millert   305:        if (!lflag)
1.36    ! ray       306:                print_status(dp->d_status, path1, path2, "");
1.1       deraadt   307: }
                    308:
1.19      millert   309: /*
                    310:  * Exclude the given directory entry?
                    311:  */
                    312: static int
                    313: excluded(const char *entry)
1.1       deraadt   314: {
1.19      millert   315:        struct excludes *excl;
1.15      tedu      316:
1.19      millert   317:        /* always skip "." and ".." */
                    318:        if (entry[0] == '.' &&
                    319:            (entry[1] == '\0' || (entry[1] == '.' && entry[2] == '\0')))
1.15      tedu      320:                return (1);
1.1       deraadt   321:
1.19      millert   322:        /* check excludes list */
                    323:        for (excl = excludes_list; excl != NULL; excl = excl->next)
                    324:                if (fnmatch(excl->pattern, entry, FNM_PATHNAME) == 0)
                    325:                        return (1);
1.1       deraadt   326:
                    327:        return (0);
                    328: }