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

Annotation of src/usr.bin/cvs/file.c, Revision 1.178

1.178   ! joris       1: /*     $OpenBSD: file.c,v 1.177 2007/01/26 11:19:44 joris Exp $        */
1.1       jfb         2: /*
1.138     joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26      jfb         5:  * All rights reserved.
1.1       jfb         6:  *
1.26      jfb         7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
1.1       jfb        10:  *
1.26      jfb        11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        13:  * 2. The name of the author may not be used to endorse or promote products
1.26      jfb        14:  *    derived from this software without specific prior written permission.
1.1       jfb        15:  *
                     16:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     17:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     18:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     19:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     20:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     21:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     22:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     23:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     24:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.26      jfb        25:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        26:  */
                     27:
1.134     xsa        28: #include "includes.h"
1.1       jfb        29:
1.164     xsa        30: #include <sys/mman.h>
                     31:
1.1       jfb        32: #include "cvs.h"
1.83      xsa        33: #include "file.h"
1.1       jfb        34: #include "log.h"
                     35:
1.104     xsa        36: #define CVS_IGN_STATIC 0x01    /* pattern is static, no need to glob */
1.1       jfb        37:
1.104     xsa        38: #define CVS_CHAR_ISMETA(c)     ((c == '*') || (c == '?') || (c == '['))
1.1       jfb        39:
                     40: /*
                     41:  * Standard patterns to ignore.
                     42:  */
                     43: static const char *cvs_ign_std[] = {
                     44:        ".",
                     45:        "..",
                     46:        "*.o",
1.45      xsa        47:        "*.a",
1.1       jfb        48:        "*.bak",
                     49:        "*.orig",
                     50:        "*.rej",
1.45      xsa        51:        "*.old",
1.1       jfb        52:        "*.exe",
                     53:        "*.depend",
1.45      xsa        54:        "*.obj",
                     55:        "*.elc",
                     56:        "*.ln",
                     57:        "*.olb",
1.1       jfb        58:        "CVS",
                     59:        "core",
1.102     joris      60:        "cvslog*",
1.49      jfb        61:        "*.core",
1.8       jfb        62:        ".#*",
1.45      xsa        63:        "*~",
                     64:        "_$*",
                     65:        "*$",
1.1       jfb        66: };
                     67:
1.138     joris      68: struct ignore_head cvs_ign_pats;
                     69: struct ignore_head dir_ign_pats;
1.1       jfb        70:
1.138     joris      71: void
1.1       jfb        72: cvs_file_init(void)
                     73: {
1.61      xsa        74:        int i, l;
1.138     joris      75:        FILE *ifp;
1.1       jfb        76:        size_t len;
1.176     otto       77:        char path[MAXPATHLEN], buf[MAXNAMLEN];
1.1       jfb        78:
                     79:        TAILQ_INIT(&cvs_ign_pats);
1.138     joris      80:        TAILQ_INIT(&dir_ign_pats);
1.4       jfb        81:
1.1       jfb        82:        /* standard patterns to ignore */
1.10      jfb        83:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.138     joris      84:                cvs_file_ignore(cvs_ign_std[i], &cvs_ign_pats);
1.1       jfb        85:
                     86:        /* read the cvsignore file in the user's home directory, if any */
1.138     joris      87:        l = snprintf(path, MAXPATHLEN, "%s/.cvsignore", cvs_homedir);
                     88:        if (l == -1 || l >= MAXPATHLEN)
                     89:                fatal("overflow in cvs_file_init");
1.61      xsa        90:
1.111     xsa        91:        ifp = fopen(path, "r");
                     92:        if (ifp == NULL) {
                     93:                if (errno != ENOENT)
                     94:                        cvs_log(LP_ERRNO,
                     95:                            "failed to open user's cvsignore file `%s'", path);
                     96:        } else {
1.138     joris      97:                while (fgets(buf, MAXNAMLEN, ifp) != NULL) {
1.111     xsa        98:                        len = strlen(buf);
                     99:                        if (len == 0)
                    100:                                continue;
1.163     moritz    101:                        if (buf[len - 1] == '\n')
                    102:                                buf[len - 1] = '\0';
1.138     joris     103:
                    104:                        cvs_file_ignore(buf, &cvs_ign_pats);
1.1       jfb       105:                }
1.138     joris     106:
1.111     xsa       107:                (void)fclose(ifp);
1.1       jfb       108:        }
                    109: }
                    110:
1.138     joris     111: void
                    112: cvs_file_ignore(const char *pat, struct ignore_head *list)
1.1       jfb       113: {
                    114:        char *cp;
1.138     joris     115:        size_t len;
1.1       jfb       116:        struct cvs_ignpat *ip;
                    117:
1.136     ray       118:        ip = xmalloc(sizeof(*ip));
1.138     joris     119:        len = strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    120:        if (len >= sizeof(ip->ip_pat))
                    121:                fatal("cvs_file_ignore: truncation of pattern '%s'", pat);
1.1       jfb       122:
                    123:        /* check if we will need globbing for that pattern */
                    124:        ip->ip_flags = CVS_IGN_STATIC;
                    125:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    126:                if (CVS_CHAR_ISMETA(*cp)) {
                    127:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    128:                        break;
                    129:                }
                    130:        }
                    131:
1.138     joris     132:        TAILQ_INSERT_TAIL(list, ip, ip_list);
1.1       jfb       133: }
                    134:
                    135: int
1.5       jfb       136: cvs_file_chkign(const char *file)
1.1       jfb       137: {
1.23      jfb       138:        int flags;
1.1       jfb       139:        struct cvs_ignpat *ip;
                    140:
1.23      jfb       141:        flags = FNM_PERIOD;
                    142:        if (cvs_nocase)
                    143:                flags |= FNM_CASEFOLD;
                    144:
1.1       jfb       145:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    146:                if (ip->ip_flags & CVS_IGN_STATIC) {
1.23      jfb       147:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1       jfb       148:                                return (1);
1.38      deraadt   149:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1       jfb       150:                        return (1);
                    151:        }
                    152:
1.138     joris     153:        TAILQ_FOREACH(ip, &dir_ign_pats, ip_list) {
                    154:                if (ip->ip_flags & CVS_IGN_STATIC) {
                    155:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
                    156:                                return (1);
                    157:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
                    158:                        return (1);
                    159:        }
                    160:
1.1       jfb       161:        return (0);
                    162: }
                    163:
1.138     joris     164: void
                    165: cvs_file_run(int argc, char **argv, struct cvs_recursion *cr)
1.1       jfb       166: {
1.138     joris     167:        int i;
                    168:        struct cvs_flisthead fl;
1.84      joris     169:
1.138     joris     170:        TAILQ_INIT(&fl);
1.85      joris     171:
1.138     joris     172:        for (i = 0; i < argc; i++)
                    173:                cvs_file_get(argv[i], &fl);
1.1       jfb       174:
1.138     joris     175:        cvs_file_walklist(&fl, cr);
                    176:        cvs_file_freelist(&fl);
1.3       jfb       177: }
                    178:
1.138     joris     179: struct cvs_filelist *
                    180: cvs_file_get(const char *name, struct cvs_flisthead *fl)
                    181: {
                    182:        const char *p;
                    183:        struct cvs_filelist *l;
1.3       jfb       184:
1.138     joris     185:        for (p = name; p[0] == '.' && p[1] == '/';)
                    186:                p += 2;
1.35      jfb       187:
1.138     joris     188:        TAILQ_FOREACH(l, fl, flist)
                    189:                if (!strcmp(l->file_path, p))
                    190:                        return (l);
1.35      jfb       191:
1.138     joris     192:        l = (struct cvs_filelist *)xmalloc(sizeof(*l));
                    193:        l->file_path = xstrdup(p);
1.35      jfb       194:
1.138     joris     195:        TAILQ_INSERT_TAIL(fl, l, flist);
                    196:        return (l);
1.35      jfb       197: }
                    198:
1.138     joris     199: struct cvs_file *
                    200: cvs_file_get_cf(const char *d, const char *f, int fd, int type)
                    201: {
                    202:        int l;
                    203:        struct cvs_file *cf;
1.176     otto      204:        char *p, rpath[MAXPATHLEN];
1.3       jfb       205:
1.138     joris     206:        l = snprintf(rpath, MAXPATHLEN, "%s/%s", d, f);
                    207:        if (l == -1 || l >= MAXPATHLEN)
                    208:                fatal("cvs_file_get_cf: overflow");
                    209:
                    210:        for (p = rpath; p[0] == '.' && p[1] == '/';)
                    211:                p += 2;
                    212:
                    213:        cf = (struct cvs_file *)xmalloc(sizeof(*cf));
                    214:        memset(cf, 0, sizeof(*cf));
                    215:
                    216:        cf->file_name = xstrdup(f);
                    217:        cf->file_wd = xstrdup(d);
                    218:        cf->file_path = xstrdup(p);
                    219:        cf->fd = fd;
                    220:        cf->repo_fd = -1;
                    221:        cf->file_type = type;
                    222:        cf->file_status = cf->file_flags = 0;
                    223:        cf->file_ent = NULL;
1.68      joris     224:
1.138     joris     225:        return (cf);
1.9       jfb       226: }
                    227:
1.138     joris     228: void
                    229: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9       jfb       230: {
1.138     joris     231:        int len, fd, type;
                    232:        struct stat st;
                    233:        struct cvs_file *cf;
                    234:        struct cvs_filelist *l, *nxt;
1.176     otto      235:        char *d, *f, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.138     joris     236:
                    237:        for (l = TAILQ_FIRST(fl); l != NULL; l = nxt) {
                    238:                if (cvs_quit)
                    239:                        fatal("received signal %d", sig_received);
                    240:
                    241:                cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
                    242:                    l->file_path);
                    243:
                    244:                if ((f = basename(l->file_path)) == NULL)
                    245:                        fatal("cvs_file_walklist: basename failed");
                    246:                if ((d = dirname(l->file_path)) == NULL)
                    247:                        fatal("cvs_file_walklist: dirname failed");
                    248:
1.146     joris     249:                type = CVS_FILE;
1.138     joris     250:                if ((fd = open(l->file_path, O_RDONLY)) != -1) {
                    251:                        if (fstat(fd, &st) == -1) {
                    252:                                cvs_log(LP_ERRNO, "%s", l->file_path);
                    253:                                (void)close(fd);
                    254:                                goto next;
                    255:                        }
1.26      jfb       256:
1.138     joris     257:                        if (S_ISDIR(st.st_mode))
                    258:                                type = CVS_DIR;
                    259:                        else if (S_ISREG(st.st_mode))
                    260:                                type = CVS_FILE;
                    261:                        else {
                    262:                                cvs_log(LP_ERR,
                    263:                                    "ignoring bad file type for %s",
                    264:                                    l->file_path);
                    265:                                (void)close(fd);
                    266:                                goto next;
                    267:                        }
1.162     joris     268:                } else if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.138     joris     269:                        if (stat(d, &st) == -1) {
                    270:                                cvs_log(LP_ERRNO, "%s", d);
                    271:                                goto next;
                    272:                        }
1.87      joris     273:
1.146     joris     274:                        cvs_get_repository_path(d, repo, MAXPATHLEN);
1.138     joris     275:                        len = snprintf(fpath, MAXPATHLEN, "%s/%s",
                    276:                            repo, f);
                    277:                        if (len == -1 || len >= MAXPATHLEN)
                    278:                                fatal("cvs_file_walklist: overflow");
                    279:
                    280:                        if ((fd = open(fpath, O_RDONLY)) == -1) {
                    281:                                strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
                    282:                                fd = open(fpath, O_RDONLY);
                    283:                        }
1.26      jfb       284:
1.138     joris     285:                        if (fd != -1) {
                    286:                                if (fstat(fd, &st) == -1)
                    287:                                        fatal("cvs_file_walklist: %s: %s",
                    288:                                             fpath, strerror(errno));
                    289:
                    290:                                if (S_ISDIR(st.st_mode))
                    291:                                        type = CVS_DIR;
                    292:                                else if (S_ISREG(st.st_mode))
                    293:                                        type = CVS_FILE;
                    294:                                else {
                    295:                                        cvs_log(LP_ERR,
                    296:                                            "ignoring bad file type for %s",
                    297:                                            l->file_path);
                    298:                                        (void)close(fd);
                    299:                                        goto next;
                    300:                                }
                    301:
                    302:                                /* this file is not in our working copy yet */
                    303:                                (void)close(fd);
                    304:                                fd = -1;
                    305:                        }
                    306:                }
1.73      joris     307:
1.138     joris     308:                cf = cvs_file_get_cf(d, f, fd, type);
                    309:                if (cf->file_type == CVS_DIR) {
                    310:                        cvs_file_walkdir(cf, cr);
                    311:                } else {
1.161     joris     312:                        if (cr->fileproc != NULL)
                    313:                                cr->fileproc(cf);
1.128     joris     314:                }
1.100     joris     315:
1.138     joris     316:                cvs_file_free(cf);
1.123     joris     317:
1.138     joris     318: next:
                    319:                nxt = TAILQ_NEXT(l, flist);
                    320:                TAILQ_REMOVE(fl, l, flist);
1.68      joris     321:
1.138     joris     322:                xfree(l->file_path);
                    323:                xfree(l);
1.100     joris     324:        }
                    325: }
                    326:
1.138     joris     327: void
                    328: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100     joris     329: {
1.171     joris     330:        int l, type;
1.138     joris     331:        FILE *fp;
                    332:        int nbytes;
                    333:        size_t len;
                    334:        long base;
                    335:        size_t bufsize;
                    336:        struct stat st;
                    337:        struct dirent *dp;
1.100     joris     338:        struct cvs_ent *ent;
1.138     joris     339:        struct cvs_ignpat *ip;
                    340:        struct cvs_ent_line *line;
                    341:        struct cvs_flisthead fl, dl;
                    342:        CVSENTRIES *entlist;
1.176     otto      343:        char *buf, *ebuf, *cp, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.100     joris     344:
1.138     joris     345:        cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100     joris     346:
1.138     joris     347:        if (cr->enterdir != NULL)
                    348:                cr->enterdir(cf);
1.100     joris     349:
1.161     joris     350:        if (cr->fileproc != NULL)
                    351:                cr->fileproc(cf);
1.100     joris     352:
1.146     joris     353:        if (cf->file_status == FILE_SKIP)
                    354:                return;
                    355:
1.100     joris     356:        /*
1.151     joris     357:         * If we do not have a admin directory inside here, dont bother,
                    358:         * unless we are running import.
1.100     joris     359:         */
1.138     joris     360:        l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
                    361:            CVS_PATH_CVSDIR);
                    362:        if (l == -1 || l >= MAXPATHLEN)
                    363:                fatal("cvs_file_walkdir: overflow");
1.100     joris     364:
1.146     joris     365:        l = stat(fpath, &st);
1.151     joris     366:        if (cvs_cmdop != CVS_OP_IMPORT &&
                    367:            (l == -1 || (l == 0 && !S_ISDIR(st.st_mode)))) {
1.138     joris     368:                return;
1.100     joris     369:        }
                    370:
                    371:        /*
1.138     joris     372:         * check for a local .cvsignore file
1.100     joris     373:         */
1.138     joris     374:        l = snprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
                    375:        if (l == -1 || l >= MAXPATHLEN)
                    376:                fatal("cvs_file_walkdir: overflow");
1.100     joris     377:
1.138     joris     378:        if ((fp = fopen(fpath, "r")) != NULL) {
1.163     moritz    379:                while (fgets(fpath, MAXPATHLEN, fp) != NULL) {
1.138     joris     380:                        len = strlen(fpath);
1.163     moritz    381:                        if (len == 0)
                    382:                                continue;
1.138     joris     383:                        if (fpath[len - 1] == '\n')
                    384:                                fpath[len - 1] = '\0';
1.26      jfb       385:
1.138     joris     386:                        cvs_file_ignore(fpath, &dir_ign_pats);
1.68      joris     387:                }
1.26      jfb       388:
1.138     joris     389:                (void)fclose(fp);
1.100     joris     390:        }
                    391:
1.138     joris     392:        if (fstat(cf->fd, &st) == -1)
                    393:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    394:                    strerror(errno));
1.100     joris     395:
1.138     joris     396:        bufsize = st.st_size;
                    397:        if (bufsize < st.st_blksize)
                    398:                bufsize = st.st_blksize;
1.116     joris     399:
1.138     joris     400:        buf = xmalloc(bufsize);
                    401:        TAILQ_INIT(&fl);
                    402:        TAILQ_INIT(&dl);
1.87      joris     403:
1.138     joris     404:        while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
                    405:                ebuf = buf + nbytes;
                    406:                cp = buf;
1.26      jfb       407:
1.138     joris     408:                while (cp < ebuf) {
                    409:                        dp = (struct dirent *)cp;
                    410:                        if (!strcmp(dp->d_name, ".") ||
                    411:                            !strcmp(dp->d_name, "..") ||
                    412:                            !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
                    413:                            dp->d_reclen == 0) {
                    414:                                cp += dp->d_reclen;
                    415:                                continue;
                    416:                        }
1.13      jfb       417:
1.138     joris     418:                        if (cvs_file_chkign(dp->d_name)) {
                    419:                                cp += dp->d_reclen;
1.13      jfb       420:                                continue;
1.138     joris     421:                        }
1.13      jfb       422:
1.138     joris     423:                        l = snprintf(fpath, MAXPATHLEN, "%s/%s",
                    424:                            cf->file_path, dp->d_name);
                    425:                        if (l == -1 || l >= MAXPATHLEN)
                    426:                                fatal("cvs_file_walkdir: overflow");
1.13      jfb       427:
1.138     joris     428:                        /*
1.171     joris     429:                         * nfs and afs will show d_type as DT_UNKNOWN
                    430:                         * for files and/or directories so when we encounter
                    431:                         * this we call stat() on the path to be sure.
1.138     joris     432:                         */
1.171     joris     433:                        if (dp->d_type == DT_UNKNOWN) {
                    434:                                if (stat(fpath, &st) == -1)
                    435:                                        fatal("'%s': %s", fpath,
                    436:                                            strerror(errno));
                    437:
                    438:                                switch (st.st_mode & S_IFMT) {
                    439:                                case S_IFDIR:
                    440:                                        type = CVS_DIR;
                    441:                                        break;
                    442:                                case S_IFREG:
                    443:                                        type = CVS_FILE;
                    444:                                        break;
                    445:                                default:
1.175     ray       446:                                        type = FILE_SKIP;
                    447:                                        break;
1.171     joris     448:                                }
                    449:                        } else {
                    450:                                switch (dp->d_type) {
                    451:                                case DT_DIR:
                    452:                                        type = CVS_DIR;
                    453:                                        break;
                    454:                                case DT_REG:
                    455:                                        type = CVS_FILE;
                    456:                                        break;
                    457:                                default:
1.175     ray       458:                                        type = FILE_SKIP;
                    459:                                        break;
1.171     joris     460:                                }
1.175     ray       461:                        }
                    462:
                    463:                        if (type == FILE_SKIP) {
                    464:                                if (verbosity > 1) {
                    465:                                        cvs_log(LP_NOTICE, "ignoring `%s'",
                    466:                                            dp->d_name);
                    467:                                }
                    468:                                cp += dp->d_reclen;
                    469:                                continue;
1.171     joris     470:                        }
                    471:
                    472:                        if (!(cr->flags & CR_RECURSE_DIRS) &&
                    473:                            type == CVS_DIR) {
                    474:                                cp += dp->d_reclen;
                    475:                                continue;
                    476:                        }
                    477:
                    478:                        switch (type) {
                    479:                        case CVS_DIR:
1.138     joris     480:                                cvs_file_get(fpath, &dl);
1.171     joris     481:                                break;
                    482:                        case CVS_FILE:
1.138     joris     483:                                cvs_file_get(fpath, &fl);
1.171     joris     484:                                break;
                    485:                        default:
                    486:                                fatal("type %d unknown, shouldn't happen",
                    487:                                    type);
                    488:                        }
1.13      jfb       489:
1.138     joris     490:                        cp += dp->d_reclen;
                    491:                }
1.34      jfb       492:        }
                    493:
1.138     joris     494:        if (nbytes == -1)
                    495:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    496:                    strerror(errno));
1.22      jfb       497:
1.138     joris     498:        xfree(buf);
1.22      jfb       499:
1.138     joris     500:        while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
                    501:                TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
                    502:                xfree(ip);
1.91      joris     503:        }
1.114     xsa       504:
1.138     joris     505:        entlist = cvs_ent_open(cf->file_path);
                    506:        TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
                    507:                ent = cvs_ent_parse(line->buf);
1.61      xsa       508:
1.138     joris     509:                l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
                    510:                    ent->ce_name);
                    511:                if (l == -1 || l >= MAXPATHLEN)
                    512:                        fatal("cvs_file_walkdir: overflow");
1.100     joris     513:
1.140     joris     514:                if (!(cr->flags & CR_RECURSE_DIRS) &&
                    515:                    ent->ce_type == CVS_ENT_DIR)
                    516:                        continue;
                    517:
1.138     joris     518:                if (ent->ce_type == CVS_ENT_DIR)
                    519:                        cvs_file_get(fpath, &dl);
                    520:                else if (ent->ce_type == CVS_ENT_FILE)
                    521:                        cvs_file_get(fpath, &fl);
1.68      joris     522:
1.138     joris     523:                cvs_ent_free(ent);
1.128     joris     524:        }
                    525:
1.138     joris     526:        cvs_ent_close(entlist, ENT_NOSYNC);
1.91      joris     527:
1.140     joris     528:        if (cr->flags & CR_REPO) {
1.146     joris     529:                cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.140     joris     530:                cvs_repository_lock(repo);
1.68      joris     531:
1.140     joris     532:                cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
                    533:                    (cr->flags & CR_RECURSE_DIRS));
                    534:        }
1.68      joris     535:
1.138     joris     536:        cvs_file_walklist(&fl, cr);
                    537:        cvs_file_freelist(&fl);
1.68      joris     538:
1.176     otto      539:        if (cr->flags & CR_REPO)
1.140     joris     540:                cvs_repository_unlock(repo);
1.68      joris     541:
1.138     joris     542:        cvs_file_walklist(&dl, cr);
                    543:        cvs_file_freelist(&dl);
1.34      jfb       544:
1.138     joris     545:        if (cr->leavedir != NULL)
                    546:                cr->leavedir(cf);
1.3       jfb       547: }
                    548:
                    549: void
1.138     joris     550: cvs_file_freelist(struct cvs_flisthead *fl)
1.3       jfb       551: {
1.138     joris     552:        struct cvs_filelist *f;
1.62      jfb       553:
1.138     joris     554:        while ((f = TAILQ_FIRST(fl)) != NULL) {
                    555:                TAILQ_REMOVE(fl, f, flist);
                    556:                xfree(f->file_path);
                    557:                xfree(f);
1.62      jfb       558:        }
1.5       jfb       559: }
                    560:
1.138     joris     561: void
1.153     joris     562: cvs_file_classify(struct cvs_file *cf, const char *tag, int loud)
1.3       jfb       563: {
1.138     joris     564:        size_t len;
                    565:        struct stat st;
1.149     joris     566:        BUF *b1, *b2;
1.178   ! joris     567:        int rflags, l, ismodified, rcsdead;
1.138     joris     568:        CVSENTRIES *entlist = NULL;
                    569:        const char *state;
1.176     otto      570:        char repo[MAXPATHLEN], rcsfile[MAXPATHLEN], r1[16], r2[16];
1.138     joris     571:
                    572:        cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
                    573:
                    574:        if (!strcmp(cf->file_path, ".")) {
                    575:                cf->file_status = FILE_UPTODATE;
                    576:                return;
                    577:        }
                    578:
1.146     joris     579:        cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.138     joris     580:        l = snprintf(rcsfile, MAXPATHLEN, "%s/%s",
                    581:            repo, cf->file_name);
                    582:        if (l == -1 || l >= MAXPATHLEN)
                    583:                fatal("cvs_file_classify: overflow");
                    584:
                    585:        if (cf->file_type == CVS_FILE) {
                    586:                len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
                    587:                if (len >= MAXPATHLEN)
                    588:                        fatal("cvs_file_classify: truncation");
                    589:        }
                    590:
                    591:        cf->file_rpath = xstrdup(rcsfile);
1.151     joris     592:
                    593:        entlist = cvs_ent_open(cf->file_wd);
1.138     joris     594:        cf->file_ent = cvs_ent_get(entlist, cf->file_name);
                    595:
1.144     joris     596:        if (cf->file_ent != NULL) {
                    597:                if (cf->file_ent->ce_type == CVS_ENT_DIR &&
                    598:                    cf->file_type != CVS_DIR)
1.148     pedro     599:                        fatal("%s is supposed to be a directory, but it is not",
1.144     joris     600:                            cf->file_path);
                    601:                if (cf->file_ent->ce_type == CVS_ENT_FILE &&
                    602:                    cf->file_type != CVS_FILE)
1.148     pedro     603:                        fatal("%s is supposed to be a file, but it is not",
1.144     joris     604:                            cf->file_path);
                    605:        }
                    606:
1.138     joris     607:        if (cf->file_type == CVS_DIR) {
                    608:                if (cf->fd == -1 && stat(rcsfile, &st) != -1)
                    609:                        cf->file_status = DIR_CREATE;
                    610:                else if (cf->file_ent != NULL)
                    611:                        cf->file_status = FILE_UPTODATE;
1.144     joris     612:                else
                    613:                        cf->file_status = FILE_UNKNOWN;
                    614:
1.138     joris     615:                cvs_ent_close(entlist, ENT_NOSYNC);
                    616:                return;
                    617:        }
                    618:
1.147     joris     619:        rflags = RCS_READ;
                    620:        switch (cvs_cmdop) {
                    621:        case CVS_OP_COMMIT:
                    622:                rflags = RCS_WRITE;
                    623:                break;
1.156     joris     624:        case CVS_OP_IMPORT:
1.147     joris     625:        case CVS_OP_LOG:
                    626:                rflags |= RCS_PARSE_FULLY;
                    627:                break;
                    628:        }
                    629:
1.138     joris     630:        cf->repo_fd = open(cf->file_rpath, O_RDONLY);
                    631:        if (cf->repo_fd != -1) {
                    632:                cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
                    633:                if (cf->file_rcs == NULL)
1.152     joris     634:                        fatal("cvs_file_classify: failed to parse RCS");
                    635:                cf->file_rcs->rf_inattic = 0;
1.155     joris     636:        } else if (cvs_cmdop != CVS_OP_CHECKOUT) {
1.152     joris     637:                l = snprintf(rcsfile, MAXPATHLEN, "%s/%s/%s%s",
                    638:                    repo, CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
                    639:                if (l == -1 || l >= MAXPATHLEN)
                    640:                        fatal("cvs_file_classify: overflow");
                    641:
1.155     joris     642:                cf->repo_fd = open(rcsfile, O_RDONLY);
1.152     joris     643:                if (cf->repo_fd != -1) {
1.155     joris     644:                        xfree(cf->file_rpath);
                    645:                        cf->file_rpath = xstrdup(rcsfile);
1.152     joris     646:                        cf->file_rcs = rcs_open(cf->file_rpath,
                    647:                             cf->repo_fd, rflags);
                    648:                        if (cf->file_rcs == NULL)
                    649:                                fatal("cvs_file_classify: failed to parse RCS");
                    650:                        cf->file_rcs->rf_inattic = 1;
                    651:                } else {
                    652:                        cf->file_rcs = NULL;
                    653:                }
1.154     joris     654:        } else
                    655:                cf->file_rcs = NULL;
1.3       jfb       656:
1.172     niallo    657:        if (tag != NULL && cf->file_rcs != NULL) {
                    658:                if ((cf->file_rcsrev = rcs_translate_tag(tag, cf->file_rcs)) == NULL)
1.174     joris     659:                        fatal("cvs_file_classify: could not translate tag `%s'", tag);
1.172     niallo    660:        } else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL) {
1.169     joris     661:                cf->file_rcsrev = rcsnum_alloc();
                    662:                rcsnum_cpy(cf->file_ent->ce_rev, cf->file_rcsrev, 0);
                    663:        } else if (cf->file_rcs != NULL)
1.156     joris     664:                cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.153     joris     665:        else
                    666:                cf->file_rcsrev = NULL;
                    667:
1.138     joris     668:        if (cf->file_ent != NULL)
                    669:                rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
1.153     joris     670:        if (cf->file_rcsrev != NULL)
                    671:                rcsnum_tostr(cf->file_rcsrev, r2, sizeof(r2));
1.6       jfb       672:
1.138     joris     673:        ismodified = rcsdead = 0;
                    674:        if (cf->fd != -1 && cf->file_ent != NULL) {
                    675:                if (fstat(cf->fd, &st) == -1)
                    676:                        fatal("cvs_file_classify: %s", strerror(errno));
1.6       jfb       677:
1.177     joris     678:                if (st.st_mtime != cf->file_ent->ce_mtime)
1.138     joris     679:                        ismodified = 1;
1.149     joris     680:        }
                    681:
                    682:        if (ismodified == 1 && cf->fd != -1 && cf->file_rcs != NULL) {
1.170     joris     683:                b1 = rcs_rev_getbuf(cf->file_rcs, cf->file_rcsrev, 0);
1.149     joris     684:                if (b1 == NULL)
                    685:                        fatal("failed to get HEAD revision for comparison");
                    686:
1.159     joris     687:                b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT);
1.149     joris     688:                if (b2 == NULL)
                    689:                        fatal("failed to get file content for comparison");
                    690:
                    691:                /* b1 and b2 get released in cvs_buf_differ */
                    692:                if (cvs_buf_differ(b1, b2))
                    693:                        ismodified = 1;
                    694:                else
                    695:                        ismodified = 0;
1.62      jfb       696:        }
                    697:
1.173     joris     698:        if (cf->file_rcs != NULL && cf->file_rcsrev != NULL) {
1.153     joris     699:                state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138     joris     700:                if (state == NULL)
                    701:                        fatal("failed to get state for HEAD for %s",
                    702:                            cf->file_path);
1.139     joris     703:                if (!strcmp(state, RCS_STATE_DEAD))
1.138     joris     704:                        rcsdead = 1;
1.145     joris     705:
                    706:                cf->file_rcs->rf_dead = rcsdead;
1.128     joris     707:        }
                    708:
1.138     joris     709:        /*
                    710:         * 10 Sin
                    711:         * 20 Goto hell
                    712:         * (I welcome you if-else hell)
                    713:         */
                    714:        if (cf->file_ent == NULL) {
                    715:                if (cf->file_rcs == NULL) {
                    716:                        if (cf->fd == -1) {
1.178   ! joris     717:                                cvs_log(LP_NOTICE,
        !           718:                                    "nothing known about '%s'",
        !           719:                                    cf->file_path);
        !           720:                        } else if (cvs_cmdop != CVS_OP_ADD) {
        !           721:                                cvs_log(LP_NOTICE,
        !           722:                                    "use add to create an entry for %s",
        !           723:                                    cf->file_path);
1.138     joris     724:                        }
1.130     joris     725:
1.138     joris     726:                        cf->file_status = FILE_UNKNOWN;
                    727:                } else if (rcsdead == 1) {
                    728:                        if (cf->fd == -1) {
                    729:                                cf->file_status = FILE_UPTODATE;
                    730:                        } else {
1.178   ! joris     731:                                cvs_log(LP_NOTICE,
        !           732:                                    "use add to create an entry for %s",
        !           733:                                    cf->file_path);
1.138     joris     734:                                cf->file_status = FILE_UNKNOWN;
1.44      jfb       735:                        }
1.138     joris     736:                } else {
                    737:                        cf->file_status = FILE_CHECKOUT;
1.14      jfb       738:                }
1.138     joris     739:        } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
                    740:                if (cf->fd == -1) {
1.178   ! joris     741:                        if (cvs_cmdop != CVS_OP_REMOVE) {
1.138     joris     742:                                cvs_log(LP_NOTICE,
1.157     david     743:                                    "warning: new-born %s has disappeared",
1.138     joris     744:                                    cf->file_path);
1.178   ! joris     745:                        }
1.138     joris     746:                        cf->file_status = FILE_REMOVE_ENTRY;
                    747:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    748:                        cf->file_status = FILE_ADDED;
                    749:                } else {
1.178   ! joris     750:                        cvs_log(LP_NOTICE,
        !           751:                            "conflict: %s already created by others",
        !           752:                            cf->file_path);
1.138     joris     753:                        cf->file_status = FILE_CONFLICT;
                    754:                }
                    755:        } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
                    756:                if (cf->fd != -1) {
1.178   ! joris     757:                        cvs_log(LP_NOTICE,
        !           758:                            "%s should be removed but is still there",
        !           759:                            cf->file_path);
1.138     joris     760:                        cf->file_status = FILE_REMOVED;
                    761:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    762:                        cf->file_status = FILE_REMOVE_ENTRY;
1.66      joris     763:                } else {
1.138     joris     764:                        if (strcmp(r1, r2)) {
1.178   ! joris     765:                                cvs_log(LP_NOTICE,
        !           766:                                    "conflict: removed %s was modified"
        !           767:                                    " by a second party",
        !           768:                                    cf->file_path);
1.138     joris     769:                                cf->file_status = FILE_CONFLICT;
                    770:                        } else {
                    771:                                cf->file_status = FILE_REMOVED;
                    772:                        }
1.66      joris     773:                }
1.138     joris     774:        } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
                    775:                if (cf->file_rcs == NULL || rcsdead == 1) {
                    776:                        if (cf->fd == -1) {
1.178   ! joris     777:                                cvs_log(LP_NOTICE,
        !           778:                                    "warning: %s's entry exists but"
        !           779:                                    " there is no longer a file"
        !           780:                                    " in the repository,"
        !           781:                                    " removing entry",
        !           782:                                     cf->file_path);
1.138     joris     783:                                cf->file_status = FILE_REMOVE_ENTRY;
                    784:                        } else {
                    785:                                if (ismodified) {
1.178   ! joris     786:                                        cvs_log(LP_NOTICE,
        !           787:                                            "conflict: %s is no longer "
        !           788:                                            "in the repository but is "
        !           789:                                            "locally modified",
        !           790:                                            cf->file_path);
1.138     joris     791:                                        cf->file_status = FILE_CONFLICT;
                    792:                                } else {
1.178   ! joris     793:                                        cvs_log(LP_NOTICE,
        !           794:                                            "%s is no longer in the "
        !           795:                                            "repository",
        !           796:                                            cf->file_path);
1.128     joris     797:
1.138     joris     798:                                        cf->file_status = FILE_UNLINK;
                    799:                                }
                    800:                        }
1.128     joris     801:                } else {
1.138     joris     802:                        if (cf->fd == -1) {
1.178   ! joris     803:                                if (cvs_cmdop != CVS_OP_REMOVE) {
1.138     joris     804:                                        cvs_log(LP_NOTICE,
                    805:                                            "warning: %s was lost",
                    806:                                            cf->file_path);
1.178   ! joris     807:                                }
1.138     joris     808:                                cf->file_status = FILE_LOST;
                    809:                        } else {
                    810:                                if (ismodified == 1)
                    811:                                        cf->file_status = FILE_MODIFIED;
                    812:                                else
                    813:                                        cf->file_status = FILE_UPTODATE;
                    814:
                    815:                                if (strcmp(r1, r2)) {
                    816:                                        if (cf->file_status == FILE_MODIFIED)
                    817:                                                cf->file_status = FILE_MERGE;
                    818:                                        else
                    819:                                                cf->file_status = FILE_PATCH;
                    820:                                }
                    821:                        }
1.128     joris     822:                }
                    823:        }
                    824:
1.138     joris     825:        cvs_ent_close(entlist, ENT_NOSYNC);
                    826: }
1.14      jfb       827:
1.138     joris     828: void
                    829: cvs_file_free(struct cvs_file *cf)
                    830: {
                    831:        xfree(cf->file_name);
                    832:        xfree(cf->file_wd);
                    833:        xfree(cf->file_path);
                    834:
1.167     joris     835:        if (cf->file_rcsrev != NULL)
                    836:                rcsnum_free(cf->file_rcsrev);
1.138     joris     837:        if (cf->file_rpath != NULL)
                    838:                xfree(cf->file_rpath);
                    839:        if (cf->file_ent != NULL)
                    840:                cvs_ent_free(cf->file_ent);
                    841:        if (cf->file_rcs != NULL)
                    842:                rcs_close(cf->file_rcs);
                    843:        if (cf->fd != -1)
                    844:                (void)close(cf->fd);
                    845:        if (cf->repo_fd != -1)
                    846:                (void)close(cf->repo_fd);
                    847:        xfree(cf);
1.23      jfb       848: }
                    849:
1.165     xsa       850: int
1.23      jfb       851: cvs_file_cmpname(const char *name1, const char *name2)
                    852: {
                    853:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    854:            (strcasecmp(name1, name2));
1.164     xsa       855: }
                    856:
                    857: int
                    858: cvs_file_cmp(const char *file1, const char *file2)
                    859: {
                    860:        struct stat stb1, stb2;
                    861:        int fd1, fd2, ret;
                    862:
                    863:        ret = 0;
                    864:
                    865:        if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
                    866:                fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
                    867:        if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
                    868:                fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
                    869:
                    870:        if (fstat(fd1, &stb1) == -1)
                    871:                fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
                    872:        if (fstat(fd2, &stb2) == -1)
                    873:                fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
                    874:
                    875:        if (stb1.st_size != stb2.st_size ||
                    876:            (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
                    877:                ret = 1;
                    878:                goto out;
                    879:        }
                    880:
                    881:        if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
                    882:                if (stb1.st_rdev != stb2.st_rdev)
                    883:                        ret = 1;
                    884:                goto out;
                    885:        }
                    886:
                    887:        if (S_ISREG(stb1.st_mode)) {
                    888:                void *p1, *p2;
                    889:
                    890:                if (stb1.st_size > SIZE_MAX) {
                    891:                        ret = 1;
                    892:                        goto out;
                    893:                }
                    894:
                    895:                if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
                    896:                    MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
                    897:                        fatal("cvs_file_cmp: mmap failed");
                    898:
                    899:                if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
                    900:                    MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
                    901:                        fatal("cvs_file_cmp: mmap failed");
                    902:
                    903:                madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
                    904:                madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
                    905:
                    906:                ret = memcmp(p1, p2, stb1.st_size);
                    907:
                    908:                (void)munmap(p1, stb1.st_size);
                    909:                (void)munmap(p2, stb1.st_size);
                    910:        }
                    911:
                    912: out:
                    913:        (void)close(fd1);
                    914:        (void)close(fd2);
1.166     xsa       915:
                    916:        return (ret);
                    917: }
                    918:
                    919: int
                    920: cvs_file_copy(const char *from, const char *to)
                    921: {
                    922:        struct stat st;
                    923:        struct timeval tv[2];
                    924:        time_t atime, mtime;
                    925:        int src, dst, ret;
                    926:
                    927:        ret = 0;
                    928:
                    929:        cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
                    930:
                    931:        if (cvs_noexec == 1)
                    932:                return (0);
                    933:
                    934:        if ((src = open(from, O_RDONLY, 0)) == -1)
                    935:                fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
                    936:
                    937:        if (fstat(src, &st) == -1)
                    938:                fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
                    939:
                    940:        atime = st.st_atimespec.tv_sec;
                    941:        mtime = st.st_mtimespec.tv_sec;
                    942:
                    943:        if (S_ISREG(st.st_mode)) {
                    944:                size_t sz;
                    945:                ssize_t nw;
                    946:                char *p, *buf;
                    947:                int saved_errno;
                    948:
                    949:                if (st.st_size > SIZE_MAX) {
                    950:                        ret = -1;
                    951:                        goto out;
                    952:                }
                    953:
                    954:                if ((dst = open(to, O_CREAT|O_TRUNC|O_WRONLY,
                    955:                    st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) == -1)
                    956:                        fatal("cvs_file_copy: open `%s': %s",
                    957:                            to, strerror(errno));
                    958:
                    959:                if ((p = mmap(NULL, st.st_size, PROT_READ,
                    960:                    MAP_FILE, src, (off_t)0)) == MAP_FAILED) {
                    961:                        saved_errno = errno;
                    962:                        (void)unlink(to);
                    963:                        fatal("cvs_file_copy: mmap: %s", strerror(saved_errno));
                    964:                }
                    965:
                    966:                madvise(p, st.st_size, MADV_SEQUENTIAL);
                    967:
                    968:                sz = st.st_size;
                    969:                buf = p;
                    970:
                    971:                while (sz > 0) {
                    972:                        if ((nw = write(dst, p, sz)) == -1) {
                    973:                                saved_errno = errno;
                    974:                                (void)unlink(to);
                    975:                                fatal("cvs_file_copy: `%s': %s",
                    976:                                    from, strerror(saved_errno));
                    977:                        }
                    978:                        buf += nw;
                    979:                        sz -= nw;
                    980:                }
                    981:
                    982:                (void)munmap(p, st.st_size);
                    983:
                    984:                tv[0].tv_sec = atime;
                    985:                tv[1].tv_sec = mtime;
                    986:
                    987:                if (futimes(dst, tv) == -1) {
                    988:                        saved_errno = errno;
                    989:                        (void)unlink(to);
                    990:                        fatal("cvs_file_copy: futimes: %s",
                    991:                            strerror(saved_errno));
                    992:                }
                    993:                (void)close(dst);
                    994:        }
                    995: out:
                    996:        (void)close(src);
1.164     xsa       997:
                    998:        return (ret);
1.14      jfb       999: }