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

1.256   ! joris       1: /*     $OpenBSD: file.c,v 1.255 2009/03/24 18:33:25 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.189     otto       28: #include <sys/types.h>
                     29: #include <sys/mman.h>
                     30: #include <sys/stat.h>
1.213     tobias     31: #include <sys/time.h>
1.1       jfb        32:
1.189     otto       33: #include <errno.h>
                     34: #include <fcntl.h>
                     35: #include <fnmatch.h>
                     36: #include <libgen.h>
                     37: #include <string.h>
                     38: #include <unistd.h>
1.164     xsa        39:
1.197     tobias     40: #include "atomicio.h"
1.1       jfb        41: #include "cvs.h"
1.198     joris      42: #include "remote.h"
1.1       jfb        43:
1.104     xsa        44: #define CVS_IGN_STATIC 0x01    /* pattern is static, no need to glob */
1.1       jfb        45:
1.104     xsa        46: #define CVS_CHAR_ISMETA(c)     ((c == '*') || (c == '?') || (c == '['))
1.1       jfb        47:
1.196     tobias     48: extern int print_stdout;
1.243     joris      49: extern int build_dirs;
1.196     tobias     50:
1.1       jfb        51: /*
                     52:  * Standard patterns to ignore.
                     53:  */
                     54: static const char *cvs_ign_std[] = {
                     55:        ".",
                     56:        "..",
                     57:        "*.o",
1.45      xsa        58:        "*.a",
1.1       jfb        59:        "*.bak",
                     60:        "*.orig",
                     61:        "*.rej",
1.45      xsa        62:        "*.old",
1.1       jfb        63:        "*.exe",
                     64:        "*.depend",
1.45      xsa        65:        "*.obj",
                     66:        "*.elc",
                     67:        "*.ln",
                     68:        "*.olb",
1.1       jfb        69:        "CVS",
                     70:        "core",
1.102     joris      71:        "cvslog*",
1.49      jfb        72:        "*.core",
1.8       jfb        73:        ".#*",
1.45      xsa        74:        "*~",
                     75:        "_$*",
                     76:        "*$",
1.1       jfb        77: };
                     78:
1.199     joris      79: char *cvs_directory_tag = NULL;
1.138     joris      80: struct ignore_head cvs_ign_pats;
                     81: struct ignore_head dir_ign_pats;
1.214     joris      82: struct ignore_head checkout_ign_pats;
1.1       jfb        83:
1.256   ! joris      84: RB_GENERATE(cvs_flisthead, cvs_filelist, flist, cvs_filelist_cmp);
        !            85:
1.138     joris      86: void
1.1       jfb        87: cvs_file_init(void)
                     88: {
1.183     xsa        89:        int i;
1.138     joris      90:        FILE *ifp;
1.176     otto       91:        char path[MAXPATHLEN], buf[MAXNAMLEN];
1.1       jfb        92:
                     93:        TAILQ_INIT(&cvs_ign_pats);
1.138     joris      94:        TAILQ_INIT(&dir_ign_pats);
1.214     joris      95:        TAILQ_INIT(&checkout_ign_pats);
1.4       jfb        96:
1.1       jfb        97:        /* standard patterns to ignore */
1.10      jfb        98:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.138     joris      99:                cvs_file_ignore(cvs_ign_std[i], &cvs_ign_pats);
1.195     tobias    100:
                    101:        if (cvs_homedir == NULL)
                    102:                return;
1.1       jfb       103:
                    104:        /* read the cvsignore file in the user's home directory, if any */
1.183     xsa       105:        (void)xsnprintf(path, MAXPATHLEN, "%s/.cvsignore", cvs_homedir);
1.61      xsa       106:
1.111     xsa       107:        ifp = fopen(path, "r");
                    108:        if (ifp == NULL) {
                    109:                if (errno != ENOENT)
                    110:                        cvs_log(LP_ERRNO,
                    111:                            "failed to open user's cvsignore file `%s'", path);
                    112:        } else {
1.138     joris     113:                while (fgets(buf, MAXNAMLEN, ifp) != NULL) {
1.203     gilles    114:                        buf[strcspn(buf, "\n")] = '\0';
                    115:                        if (buf[0] == '\0')
1.111     xsa       116:                                continue;
1.138     joris     117:
                    118:                        cvs_file_ignore(buf, &cvs_ign_pats);
1.1       jfb       119:                }
1.138     joris     120:
1.111     xsa       121:                (void)fclose(ifp);
1.1       jfb       122:        }
                    123: }
                    124:
1.138     joris     125: void
                    126: cvs_file_ignore(const char *pat, struct ignore_head *list)
1.1       jfb       127: {
                    128:        char *cp;
1.138     joris     129:        size_t len;
1.1       jfb       130:        struct cvs_ignpat *ip;
                    131:
1.136     ray       132:        ip = xmalloc(sizeof(*ip));
1.138     joris     133:        len = strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    134:        if (len >= sizeof(ip->ip_pat))
                    135:                fatal("cvs_file_ignore: truncation of pattern '%s'", pat);
1.1       jfb       136:
                    137:        /* check if we will need globbing for that pattern */
                    138:        ip->ip_flags = CVS_IGN_STATIC;
                    139:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    140:                if (CVS_CHAR_ISMETA(*cp)) {
                    141:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    142:                        break;
                    143:                }
                    144:        }
                    145:
1.138     joris     146:        TAILQ_INSERT_TAIL(list, ip, ip_list);
1.1       jfb       147: }
                    148:
                    149: int
1.5       jfb       150: cvs_file_chkign(const char *file)
1.1       jfb       151: {
1.23      jfb       152:        int flags;
1.1       jfb       153:        struct cvs_ignpat *ip;
                    154:
1.23      jfb       155:        flags = FNM_PERIOD;
                    156:        if (cvs_nocase)
                    157:                flags |= FNM_CASEFOLD;
                    158:
1.1       jfb       159:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    160:                if (ip->ip_flags & CVS_IGN_STATIC) {
1.23      jfb       161:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1       jfb       162:                                return (1);
1.38      deraadt   163:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1       jfb       164:                        return (1);
                    165:        }
                    166:
1.138     joris     167:        TAILQ_FOREACH(ip, &dir_ign_pats, ip_list) {
1.214     joris     168:                if (ip->ip_flags & CVS_IGN_STATIC) {
                    169:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
                    170:                                return (1);
                    171:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
                    172:                        return (1);
                    173:        }
                    174:
                    175:        TAILQ_FOREACH(ip, &checkout_ign_pats, ip_list) {
1.138     joris     176:                if (ip->ip_flags & CVS_IGN_STATIC) {
                    177:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
                    178:                                return (1);
                    179:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
                    180:                        return (1);
                    181:        }
                    182:
1.1       jfb       183:        return (0);
                    184: }
                    185:
1.138     joris     186: void
                    187: cvs_file_run(int argc, char **argv, struct cvs_recursion *cr)
1.1       jfb       188: {
1.138     joris     189:        int i;
                    190:        struct cvs_flisthead fl;
1.84      joris     191:
1.256   ! joris     192:        RB_INIT(&fl);
1.85      joris     193:
1.138     joris     194:        for (i = 0; i < argc; i++)
1.250     joris     195:                cvs_file_get(argv[i], FILE_USER_SUPPLIED, &fl);
1.1       jfb       196:
1.138     joris     197:        cvs_file_walklist(&fl, cr);
                    198:        cvs_file_freelist(&fl);
1.3       jfb       199: }
                    200:
1.138     joris     201: struct cvs_filelist *
1.256   ! joris     202: cvs_file_get(char *name, int flags, struct cvs_flisthead *fl)
1.138     joris     203: {
1.256   ! joris     204:        char *p;
        !           205:        struct cvs_filelist *l, find;
1.3       jfb       206:
1.138     joris     207:        for (p = name; p[0] == '.' && p[1] == '/';)
                    208:                p += 2;
1.35      jfb       209:
1.256   ! joris     210:        find.file_path = p;
        !           211:        l = RB_FIND(cvs_flisthead, fl, &find);
        !           212:        if (l != NULL)
        !           213:                return (l);
1.35      jfb       214:
1.138     joris     215:        l = (struct cvs_filelist *)xmalloc(sizeof(*l));
                    216:        l->file_path = xstrdup(p);
1.250     joris     217:        l->flags = flags;
1.35      jfb       218:
1.256   ! joris     219:        RB_INSERT(cvs_flisthead, fl, l);
1.138     joris     220:        return (l);
1.35      jfb       221: }
                    222:
1.138     joris     223: struct cvs_file *
1.246     joris     224: cvs_file_get_cf(const char *d, const char *f, const char *fpath, int fd,
1.250     joris     225:        int type, int flags)
1.138     joris     226: {
1.246     joris     227:        const char *p;
1.138     joris     228:        struct cvs_file *cf;
                    229:
1.246     joris     230:        for (p = fpath; p[0] == '.' && p[1] == '/';)
1.138     joris     231:                p += 2;
                    232:
1.207     tobias    233:        cf = (struct cvs_file *)xcalloc(1, sizeof(*cf));
1.138     joris     234:
                    235:        cf->file_name = xstrdup(f);
                    236:        cf->file_wd = xstrdup(d);
                    237:        cf->file_path = xstrdup(p);
                    238:        cf->fd = fd;
                    239:        cf->repo_fd = -1;
                    240:        cf->file_type = type;
1.250     joris     241:        cf->file_status = 0;
                    242:        cf->file_flags = flags;
1.234     joris     243:        cf->in_attic = 0;
1.138     joris     244:        cf->file_ent = NULL;
1.221     joris     245:
1.252     joris     246:        if (cf->fd != -1)
                    247:                cf->file_flags |= FILE_ON_DISK;
                    248:
1.221     joris     249:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL ||
                    250:            cvs_server_active == 1)
                    251:                cvs_validate_directory(cf->file_path);
1.68      joris     252:
1.138     joris     253:        return (cf);
1.9       jfb       254: }
                    255:
1.138     joris     256: void
                    257: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9       jfb       258: {
1.183     xsa       259:        int fd, type;
1.138     joris     260:        struct stat st;
                    261:        struct cvs_file *cf;
                    262:        struct cvs_filelist *l, *nxt;
1.176     otto      263:        char *d, *f, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.138     joris     264:
1.256   ! joris     265:        for (l = RB_MIN(cvs_flisthead, fl); l != NULL; l = nxt) {
1.138     joris     266:                if (cvs_quit)
                    267:                        fatal("received signal %d", sig_received);
                    268:
                    269:                cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
                    270:                    l->file_path);
                    271:
                    272:                if ((f = basename(l->file_path)) == NULL)
                    273:                        fatal("cvs_file_walklist: basename failed");
                    274:                if ((d = dirname(l->file_path)) == NULL)
                    275:                        fatal("cvs_file_walklist: dirname failed");
                    276:
1.146     joris     277:                type = CVS_FILE;
1.138     joris     278:                if ((fd = open(l->file_path, O_RDONLY)) != -1) {
                    279:                        if (fstat(fd, &st) == -1) {
                    280:                                cvs_log(LP_ERRNO, "%s", l->file_path);
                    281:                                (void)close(fd);
                    282:                                goto next;
                    283:                        }
1.26      jfb       284:
1.138     joris     285:                        if (S_ISDIR(st.st_mode))
                    286:                                type = CVS_DIR;
                    287:                        else if (S_ISREG(st.st_mode))
                    288:                                type = CVS_FILE;
                    289:                        else {
                    290:                                cvs_log(LP_ERR,
                    291:                                    "ignoring bad file type for %s",
                    292:                                    l->file_path);
                    293:                                (void)close(fd);
                    294:                                goto next;
                    295:                        }
1.162     joris     296:                } else if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.196     tobias    297:                        /*
                    298:                         * During checkout -p, do not use any locally
                    299:                         * available directories.
                    300:                         */
1.245     joris     301:                        if ((cmdp->cmd_flags & CVS_USE_WDIR) &&
                    302:                            (cvs_cmdop != CVS_OP_CHECKOUT || !print_stdout))
1.196     tobias    303:                                if (stat(d, &st) == -1) {
                    304:                                        cvs_log(LP_ERRNO, "%s", d);
                    305:                                        goto next;
                    306:                                }
1.87      joris     307:
1.146     joris     308:                        cvs_get_repository_path(d, repo, MAXPATHLEN);
1.183     xsa       309:                        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
1.138     joris     310:                            repo, f);
                    311:
                    312:                        if ((fd = open(fpath, O_RDONLY)) == -1) {
                    313:                                strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
                    314:                                fd = open(fpath, O_RDONLY);
                    315:                        }
1.26      jfb       316:
1.138     joris     317:                        if (fd != -1) {
                    318:                                if (fstat(fd, &st) == -1)
                    319:                                        fatal("cvs_file_walklist: %s: %s",
                    320:                                             fpath, strerror(errno));
                    321:
                    322:                                if (S_ISDIR(st.st_mode))
                    323:                                        type = CVS_DIR;
                    324:                                else if (S_ISREG(st.st_mode))
                    325:                                        type = CVS_FILE;
                    326:                                else {
                    327:                                        cvs_log(LP_ERR,
                    328:                                            "ignoring bad file type for %s",
                    329:                                            l->file_path);
                    330:                                        (void)close(fd);
                    331:                                        goto next;
                    332:                                }
1.226     deraadt   333:
1.138     joris     334:                                /* this file is not in our working copy yet */
                    335:                                (void)close(fd);
                    336:                                fd = -1;
                    337:                        }
                    338:                }
1.73      joris     339:
1.246     joris     340:                cf = cvs_file_get_cf(d, f, l->file_path,
1.250     joris     341:                    fd, type, l->flags);
1.138     joris     342:                if (cf->file_type == CVS_DIR) {
                    343:                        cvs_file_walkdir(cf, cr);
                    344:                } else {
1.250     joris     345:                        if (l->flags & FILE_USER_SUPPLIED) {
1.199     joris     346:                                cvs_parse_tagfile(cf->file_wd,
                    347:                                    &cvs_directory_tag, NULL, NULL);
                    348:
                    349:                                if (cvs_directory_tag == NULL &&
                    350:                                    cvs_specified_tag != NULL)
1.239     tobias    351:                                        cvs_directory_tag =
                    352:                                            xstrdup(cvs_specified_tag);
1.234     joris     353:
                    354:                                if (current_cvsroot->cr_method ==
                    355:                                    CVS_METHOD_LOCAL) {
                    356:                                        cvs_get_repository_path(cf->file_wd,
                    357:                                            repo, MAXPATHLEN);
                    358:                                        cvs_repository_lock(repo,
                    359:                                            (cmdp->cmd_flags & CVS_LOCK_REPO));
                    360:                                }
1.199     joris     361:                        }
                    362:
1.161     joris     363:                        if (cr->fileproc != NULL)
                    364:                                cr->fileproc(cf);
1.234     joris     365:
1.250     joris     366:                        if (l->flags & FILE_USER_SUPPLIED) {
1.239     tobias    367:                                if (cmdp->cmd_flags & CVS_LOCK_REPO)
                    368:                                        cvs_repository_unlock(repo);
                    369:                                if (cvs_directory_tag != NULL) {
                    370:                                        xfree(cvs_directory_tag);
                    371:                                        cvs_directory_tag = NULL;
                    372:                                }
                    373:                        }
1.128     joris     374:                }
1.100     joris     375:
1.138     joris     376:                cvs_file_free(cf);
1.123     joris     377:
1.138     joris     378: next:
1.256   ! joris     379:                nxt = RB_NEXT(cvs_flisthead, fl, l);
1.100     joris     380:        }
                    381: }
                    382:
1.138     joris     383: void
                    384: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100     joris     385: {
1.171     joris     386:        int l, type;
1.138     joris     387:        FILE *fp;
                    388:        int nbytes;
                    389:        long base;
                    390:        size_t bufsize;
                    391:        struct stat st;
                    392:        struct dirent *dp;
1.100     joris     393:        struct cvs_ent *ent;
1.138     joris     394:        struct cvs_ignpat *ip;
                    395:        struct cvs_ent_line *line;
                    396:        struct cvs_flisthead fl, dl;
                    397:        CVSENTRIES *entlist;
1.194     joris     398:        char *buf, *ebuf, *cp, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.100     joris     399:
1.138     joris     400:        cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100     joris     401:
1.138     joris     402:        if (cr->enterdir != NULL)
                    403:                cr->enterdir(cf);
1.100     joris     404:
1.161     joris     405:        if (cr->fileproc != NULL)
                    406:                cr->fileproc(cf);
1.100     joris     407:
1.146     joris     408:        if (cf->file_status == FILE_SKIP)
                    409:                return;
                    410:
1.100     joris     411:        /*
1.211     tobias    412:         * If this is a repository-only command, do not touch any
                    413:         * locally available directories or try to create them.
                    414:         */
                    415:        if (!(cmdp->cmd_flags & CVS_USE_WDIR)) {
1.256   ! joris     416:                RB_INIT(&fl);
        !           417:                RB_INIT(&dl);
1.211     tobias    418:                goto walkrepo;
                    419:        }
                    420:
                    421:        /*
1.253     joris     422:         * If we do not have an admin directory inside here, dont bother,
1.211     tobias    423:         * unless we are running export or import.
1.100     joris     424:         */
1.183     xsa       425:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138     joris     426:            CVS_PATH_CVSDIR);
1.100     joris     427:
1.146     joris     428:        l = stat(fpath, &st);
1.210     tobias    429:        if (cvs_cmdop != CVS_OP_EXPORT && cvs_cmdop != CVS_OP_IMPORT &&
1.151     joris     430:            (l == -1 || (l == 0 && !S_ISDIR(st.st_mode)))) {
1.138     joris     431:                return;
1.100     joris     432:        }
                    433:
1.199     joris     434:        cvs_parse_tagfile(cf->file_path, &cvs_directory_tag, NULL, NULL);
                    435:
1.100     joris     436:        /*
1.138     joris     437:         * check for a local .cvsignore file
1.100     joris     438:         */
1.183     xsa       439:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
1.100     joris     440:
1.138     joris     441:        if ((fp = fopen(fpath, "r")) != NULL) {
1.163     moritz    442:                while (fgets(fpath, MAXPATHLEN, fp) != NULL) {
1.203     gilles    443:                        fpath[strcspn(fpath, "\n")] = '\0';
                    444:                        if (fpath[0] == '\0')
1.163     moritz    445:                                continue;
1.26      jfb       446:
1.138     joris     447:                        cvs_file_ignore(fpath, &dir_ign_pats);
1.68      joris     448:                }
1.26      jfb       449:
1.138     joris     450:                (void)fclose(fp);
1.100     joris     451:        }
                    452:
1.138     joris     453:        if (fstat(cf->fd, &st) == -1)
                    454:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    455:                    strerror(errno));
1.100     joris     456:
1.228     tobias    457:        if (st.st_size > SIZE_MAX)
                    458:                fatal("cvs_file_walkdir: %s: file size too big", cf->file_name);
                    459:
1.138     joris     460:        bufsize = st.st_size;
                    461:        if (bufsize < st.st_blksize)
                    462:                bufsize = st.st_blksize;
1.116     joris     463:
1.138     joris     464:        buf = xmalloc(bufsize);
1.256   ! joris     465:        RB_INIT(&fl);
        !           466:        RB_INIT(&dl);
1.87      joris     467:
1.138     joris     468:        while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
                    469:                ebuf = buf + nbytes;
                    470:                cp = buf;
1.26      jfb       471:
1.138     joris     472:                while (cp < ebuf) {
                    473:                        dp = (struct dirent *)cp;
                    474:                        if (!strcmp(dp->d_name, ".") ||
                    475:                            !strcmp(dp->d_name, "..") ||
                    476:                            !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
1.186     joris     477:                            dp->d_fileno == 0) {
1.138     joris     478:                                cp += dp->d_reclen;
                    479:                                continue;
                    480:                        }
1.13      jfb       481:
1.209     tobias    482:                        if (cvs_file_chkign(dp->d_name) &&
                    483:                            cvs_cmdop != CVS_OP_RLOG &&
                    484:                            cvs_cmdop != CVS_OP_RTAG) {
1.138     joris     485:                                cp += dp->d_reclen;
1.13      jfb       486:                                continue;
1.138     joris     487:                        }
1.13      jfb       488:
1.203     gilles    489:                        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
1.138     joris     490:                            cf->file_path, dp->d_name);
1.13      jfb       491:
1.138     joris     492:                        /*
1.171     joris     493:                         * nfs and afs will show d_type as DT_UNKNOWN
                    494:                         * for files and/or directories so when we encounter
1.184     todd      495:                         * this we call lstat() on the path to be sure.
1.138     joris     496:                         */
1.171     joris     497:                        if (dp->d_type == DT_UNKNOWN) {
1.184     todd      498:                                if (lstat(fpath, &st) == -1)
1.171     joris     499:                                        fatal("'%s': %s", fpath,
                    500:                                            strerror(errno));
                    501:
                    502:                                switch (st.st_mode & S_IFMT) {
                    503:                                case S_IFDIR:
                    504:                                        type = CVS_DIR;
                    505:                                        break;
                    506:                                case S_IFREG:
                    507:                                        type = CVS_FILE;
                    508:                                        break;
                    509:                                default:
1.175     ray       510:                                        type = FILE_SKIP;
                    511:                                        break;
1.171     joris     512:                                }
                    513:                        } else {
                    514:                                switch (dp->d_type) {
                    515:                                case DT_DIR:
                    516:                                        type = CVS_DIR;
                    517:                                        break;
                    518:                                case DT_REG:
                    519:                                        type = CVS_FILE;
                    520:                                        break;
                    521:                                default:
1.175     ray       522:                                        type = FILE_SKIP;
                    523:                                        break;
1.171     joris     524:                                }
1.175     ray       525:                        }
                    526:
                    527:                        if (type == FILE_SKIP) {
                    528:                                if (verbosity > 1) {
                    529:                                        cvs_log(LP_NOTICE, "ignoring `%s'",
                    530:                                            dp->d_name);
                    531:                                }
                    532:                                cp += dp->d_reclen;
                    533:                                continue;
1.171     joris     534:                        }
                    535:
                    536:                        switch (type) {
                    537:                        case CVS_DIR:
1.192     niallo    538:                                if (cr->flags & CR_RECURSE_DIRS)
1.199     joris     539:                                        cvs_file_get(fpath, 0, &dl);
1.171     joris     540:                                break;
                    541:                        case CVS_FILE:
1.199     joris     542:                                cvs_file_get(fpath, 0, &fl);
1.171     joris     543:                                break;
                    544:                        default:
                    545:                                fatal("type %d unknown, shouldn't happen",
                    546:                                    type);
                    547:                        }
1.13      jfb       548:
1.138     joris     549:                        cp += dp->d_reclen;
                    550:                }
1.34      jfb       551:        }
                    552:
1.138     joris     553:        if (nbytes == -1)
                    554:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    555:                    strerror(errno));
1.22      jfb       556:
1.138     joris     557:        xfree(buf);
1.22      jfb       558:
1.138     joris     559:        while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
                    560:                TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
                    561:                xfree(ip);
1.91      joris     562:        }
1.114     xsa       563:
1.138     joris     564:        entlist = cvs_ent_open(cf->file_path);
                    565:        TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
                    566:                ent = cvs_ent_parse(line->buf);
1.61      xsa       567:
1.183     xsa       568:                (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138     joris     569:                    ent->ce_name);
1.100     joris     570:
1.140     joris     571:                if (!(cr->flags & CR_RECURSE_DIRS) &&
                    572:                    ent->ce_type == CVS_ENT_DIR)
                    573:                        continue;
1.138     joris     574:                if (ent->ce_type == CVS_ENT_DIR)
1.199     joris     575:                        cvs_file_get(fpath, 0, &dl);
1.138     joris     576:                else if (ent->ce_type == CVS_ENT_FILE)
1.199     joris     577:                        cvs_file_get(fpath, 0, &fl);
1.68      joris     578:
1.138     joris     579:                cvs_ent_free(ent);
1.128     joris     580:        }
                    581:
1.211     tobias    582: walkrepo:
1.234     joris     583:        if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.146     joris     584:                cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.234     joris     585:                cvs_repository_lock(repo, (cmdp->cmd_flags & CVS_LOCK_REPO));
                    586:        }
1.68      joris     587:
1.234     joris     588:        if (cr->flags & CR_REPO) {
1.212     tobias    589:                xsnprintf(fpath, sizeof(fpath), "%s/%s", cf->file_path,
                    590:                    CVS_PATH_STATICENTRIES);
                    591:
1.243     joris     592:                if (stat(fpath, &st) == -1 || build_dirs == 1)
1.212     tobias    593:                        cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
1.251     joris     594:                            (cr->flags & CR_RECURSE_DIRS) ?
                    595:                            REPOSITORY_DODIRS : 0);
1.140     joris     596:        }
1.68      joris     597:
1.138     joris     598:        cvs_file_walklist(&fl, cr);
                    599:        cvs_file_freelist(&fl);
1.68      joris     600:
1.234     joris     601:        if (current_cvsroot->cr_method == CVS_METHOD_LOCAL &&
                    602:            (cmdp->cmd_flags & CVS_LOCK_REPO))
1.140     joris     603:                cvs_repository_unlock(repo);
1.68      joris     604:
1.241     tobias    605:        if (cvs_directory_tag != NULL && cmdp->cmd_flags & CVS_USE_WDIR) {
                    606:                cvs_write_tagfile(cf->file_path, cvs_directory_tag, NULL);
                    607:                xfree(cvs_directory_tag);
                    608:                cvs_directory_tag = NULL;
                    609:        }
                    610:
1.138     joris     611:        cvs_file_walklist(&dl, cr);
                    612:        cvs_file_freelist(&dl);
1.34      jfb       613:
1.138     joris     614:        if (cr->leavedir != NULL)
                    615:                cr->leavedir(cf);
1.3       jfb       616: }
                    617:
                    618: void
1.138     joris     619: cvs_file_freelist(struct cvs_flisthead *fl)
1.3       jfb       620: {
1.256   ! joris     621:        struct cvs_filelist *f, *nxt;
1.62      jfb       622:
1.256   ! joris     623:        for (f = RB_MIN(cvs_flisthead, fl); f != NULL; f = nxt) {
        !           624:                nxt = RB_NEXT(cvs_flisthead, fl, f);
        !           625:                RB_REMOVE(cvs_flisthead, fl, f);
1.138     joris     626:                xfree(f->file_path);
                    627:                xfree(f);
1.62      jfb       628:        }
1.5       jfb       629: }
                    630:
1.138     joris     631: void
1.185     joris     632: cvs_file_classify(struct cvs_file *cf, const char *tag)
1.3       jfb       633: {
1.138     joris     634:        size_t len;
                    635:        struct stat st;
1.149     joris     636:        BUF *b1, *b2;
1.199     joris     637:        int server_has_file, notag;
1.183     xsa       638:        int rflags, ismodified, rcsdead;
1.138     joris     639:        CVSENTRIES *entlist = NULL;
                    640:        const char *state;
1.193     xsa       641:        char repo[MAXPATHLEN], rcsfile[MAXPATHLEN];
1.138     joris     642:
1.198     joris     643:        cvs_log(LP_TRACE, "cvs_file_classify(%s, %s)", cf->file_path,
                    644:            (tag != NULL) ? tag : "none");
1.138     joris     645:
1.232     tobias    646:        if (!strcmp(cf->file_path, ".")) {
1.138     joris     647:                cf->file_status = FILE_UPTODATE;
                    648:                return;
                    649:        }
                    650:
1.146     joris     651:        cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.183     xsa       652:        (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s",
1.138     joris     653:            repo, cf->file_name);
                    654:
1.211     tobias    655:        if (cf->file_type == CVS_FILE) {
1.138     joris     656:                len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
                    657:                if (len >= MAXPATHLEN)
                    658:                        fatal("cvs_file_classify: truncation");
                    659:        }
                    660:
                    661:        cf->file_rpath = xstrdup(rcsfile);
1.211     tobias    662:
                    663:        if (cmdp->cmd_flags & CVS_USE_WDIR) {
                    664:                entlist = cvs_ent_open(cf->file_wd);
                    665:                cf->file_ent = cvs_ent_get(entlist, cf->file_name);
                    666:        } else
                    667:                cf->file_ent = NULL;
1.138     joris     668:
1.144     joris     669:        if (cf->file_ent != NULL) {
1.252     joris     670:                if (cf->file_flags & FILE_ON_DISK &&
                    671:                    cf->file_ent->ce_type == CVS_ENT_DIR &&
1.144     joris     672:                    cf->file_type != CVS_DIR)
1.148     pedro     673:                        fatal("%s is supposed to be a directory, but it is not",
1.144     joris     674:                            cf->file_path);
1.252     joris     675:                if (cf->file_flags & FILE_ON_DISK &&
                    676:                    cf->file_ent->ce_type == CVS_ENT_FILE &&
1.144     joris     677:                    cf->file_type != CVS_FILE)
1.148     pedro     678:                        fatal("%s is supposed to be a file, but it is not",
1.144     joris     679:                            cf->file_path);
1.201     joris     680:
1.216     joris     681:                if (cf->file_ent->ce_tag != NULL && cvs_specified_tag == NULL)
1.201     joris     682:                        tag = cf->file_ent->ce_tag;
1.144     joris     683:        }
                    684:
1.138     joris     685:        if (cf->file_type == CVS_DIR) {
1.211     tobias    686:                if (!(cmdp->cmd_flags & CVS_USE_WDIR))
                    687:                        cf->file_status = FILE_UPTODATE;
                    688:                else if (cf->fd == -1 && stat(rcsfile, &st) != -1)
1.138     joris     689:                        cf->file_status = DIR_CREATE;
1.209     tobias    690:                else if (cf->file_ent != NULL || cvs_cmdop == CVS_OP_RLOG ||
                    691:                    cvs_cmdop == CVS_OP_RTAG)
1.138     joris     692:                        cf->file_status = FILE_UPTODATE;
1.144     joris     693:                else
                    694:                        cf->file_status = FILE_UNKNOWN;
                    695:
1.138     joris     696:                return;
                    697:        }
                    698:
1.147     joris     699:        rflags = RCS_READ;
                    700:        switch (cvs_cmdop) {
                    701:        case CVS_OP_COMMIT:
1.255     joris     702:        case CVS_OP_TAG:
                    703:        case CVS_OP_RTAG:
1.147     joris     704:                rflags = RCS_WRITE;
                    705:                break;
1.236     joris     706:        case CVS_OP_ADMIN:
1.156     joris     707:        case CVS_OP_IMPORT:
1.147     joris     708:        case CVS_OP_LOG:
1.192     niallo    709:        case CVS_OP_RLOG:
1.147     joris     710:                rflags |= RCS_PARSE_FULLY;
                    711:                break;
1.208     tobias    712:        default:
                    713:                break;
1.147     joris     714:        }
                    715:
1.138     joris     716:        cf->repo_fd = open(cf->file_rpath, O_RDONLY);
                    717:        if (cf->repo_fd != -1) {
                    718:                cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
                    719:                if (cf->file_rcs == NULL)
1.152     joris     720:                        fatal("cvs_file_classify: failed to parse RCS");
1.194     joris     721:        } else {
                    722:                (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s/%s%s",
                    723:                     repo, CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
                    724:
                    725:                cf->repo_fd = open(rcsfile, O_RDONLY);
                    726:                if (cf->repo_fd != -1) {
                    727:                        xfree(cf->file_rpath);
                    728:                        cf->file_rpath = xstrdup(rcsfile);
                    729:                        cf->file_rcs = rcs_open(cf->file_rpath,
                    730:                            cf->repo_fd, rflags);
                    731:                        if (cf->file_rcs == NULL)
                    732:                                fatal("cvs_file_classify: failed to parse RCS");
                    733:                        cf->in_attic = 1;
                    734:                } else {
                    735:                        cf->file_rcs = NULL;
                    736:                }
1.190     niallo    737:        }
                    738:
1.199     joris     739:        notag = 0;
1.200     joris     740:        cf->file_flags |= FILE_HAS_TAG;
1.190     niallo    741:        if (tag != NULL && cf->file_rcs != NULL) {
1.218     tobias    742:                if ((cf->file_rcsrev = rcs_translate_tag(tag, cf->file_rcs))
1.238     tobias    743:                    == NULL) {
1.218     tobias    744:                        cf->file_rcsrev = rcs_translate_tag(NULL, cf->file_rcs);
1.206     tobias    745:                        if (cf->file_rcsrev != NULL) {
                    746:                                notag = 1;
                    747:                                cf->file_flags &= ~FILE_HAS_TAG;
                    748:                        }
1.152     joris     749:                }
1.172     niallo    750:        } else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL) {
1.169     joris     751:                cf->file_rcsrev = rcsnum_alloc();
                    752:                rcsnum_cpy(cf->file_ent->ce_rev, cf->file_rcsrev, 0);
1.190     niallo    753:        } else if (cf->file_rcs != NULL) {
1.218     tobias    754:                cf->file_rcsrev = rcs_translate_tag(NULL, cf->file_rcs);
1.190     niallo    755:        } else {
1.153     joris     756:                cf->file_rcsrev = NULL;
1.190     niallo    757:        }
1.153     joris     758:
1.138     joris     759:        ismodified = rcsdead = 0;
1.252     joris     760:        if ((cf->file_flags & FILE_ON_DISK) && cf->file_ent != NULL) {
1.138     joris     761:                if (fstat(cf->fd, &st) == -1)
                    762:                        fatal("cvs_file_classify: %s", strerror(errno));
1.6       jfb       763:
1.177     joris     764:                if (st.st_mtime != cf->file_ent->ce_mtime)
1.138     joris     765:                        ismodified = 1;
1.149     joris     766:        }
                    767:
1.198     joris     768:        server_has_file = 0;
                    769:        if (cvs_server_active == 1 && cf->file_ent != NULL &&
                    770:            cf->file_ent->ce_mtime == CVS_SERVER_UPTODATE) {
                    771:                server_has_file = 1;
                    772:                ismodified = 0;
                    773:        }
1.251     joris     774:
                    775:        if ((server_has_file == 1) || (cf->fd != -1))
                    776:                cf->file_flags |= FILE_ON_DISK;
1.198     joris     777:
1.252     joris     778:        if (ismodified == 1 &&
                    779:            (cf->file_flags & FILE_ON_DISK) && cf->file_rcs != NULL &&
1.227     joris     780:            cf->file_ent != NULL && !RCSNUM_ISBRANCH(cf->file_ent->ce_rev) &&
                    781:            cf->file_ent->ce_status != CVS_ENT_ADDED) {
1.225     joris     782:                b1 = rcs_rev_getbuf(cf->file_rcs, cf->file_ent->ce_rev, 0);
1.222     tobias    783:                b2 = cvs_buf_load_fd(cf->fd);
1.149     joris     784:
                    785:                if (cvs_buf_differ(b1, b2))
                    786:                        ismodified = 1;
                    787:                else
                    788:                        ismodified = 0;
1.188     ray       789:                cvs_buf_free(b1);
                    790:                cvs_buf_free(b2);
1.62      jfb       791:        }
                    792:
1.202     joris     793:        if (cf->file_rcs != NULL && cf->file_rcsrev != NULL &&
                    794:            !RCSNUM_ISBRANCH(cf->file_rcsrev)) {
1.153     joris     795:                state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138     joris     796:                if (state == NULL)
                    797:                        fatal("failed to get state for HEAD for %s",
                    798:                            cf->file_path);
1.139     joris     799:                if (!strcmp(state, RCS_STATE_DEAD))
1.205     tobias    800:                        rcsdead = 1;
                    801:
1.248     tobias    802:                if (cvs_specified_date == -1 && cvs_directory_date == -1 &&
                    803:                    tag == NULL && cf->in_attic &&
1.205     tobias    804:                    !RCSNUM_ISBRANCHREV(cf->file_rcsrev))
1.138     joris     805:                        rcsdead = 1;
1.145     joris     806:
                    807:                cf->file_rcs->rf_dead = rcsdead;
1.128     joris     808:        }
                    809:
1.138     joris     810:        /*
                    811:         * 10 Sin
                    812:         * 20 Goto hell
                    813:         * (I welcome you if-else hell)
                    814:         */
                    815:        if (cf->file_ent == NULL) {
                    816:                if (cf->file_rcs == NULL) {
1.252     joris     817:                        if (!(cf->file_flags & FILE_ON_DISK)) {
1.178     joris     818:                                cvs_log(LP_NOTICE,
                    819:                                    "nothing known about '%s'",
                    820:                                    cf->file_path);
1.138     joris     821:                        }
1.130     joris     822:
1.138     joris     823:                        cf->file_status = FILE_UNKNOWN;
1.249     tobias    824:                } else if (rcsdead == 1 || !(cf->file_flags & FILE_HAS_TAG)) {
1.252     joris     825:                        if (!(cf->file_flags & FILE_ON_DISK)) {
1.138     joris     826:                                cf->file_status = FILE_UPTODATE;
1.179     joris     827:                        } else if (cvs_cmdop != CVS_OP_ADD) {
1.138     joris     828:                                cf->file_status = FILE_UNKNOWN;
1.44      jfb       829:                        }
1.218     tobias    830:                } else if (notag == 0 && cf->file_rcsrev != NULL) {
1.199     joris     831:                        cf->file_status = FILE_CHECKOUT;
1.138     joris     832:                } else {
1.199     joris     833:                        cf->file_status = FILE_UPTODATE;
1.14      jfb       834:                }
1.208     tobias    835:
                    836:                return;
                    837:        }
                    838:
                    839:        switch (cf->file_ent->ce_status) {
                    840:        case CVS_ENT_ADDED:
1.252     joris     841:                if (!(cf->file_flags & FILE_ON_DISK)) {
1.178     joris     842:                        if (cvs_cmdop != CVS_OP_REMOVE) {
1.138     joris     843:                                cvs_log(LP_NOTICE,
1.157     david     844:                                    "warning: new-born %s has disappeared",
1.138     joris     845:                                    cf->file_path);
1.178     joris     846:                        }
1.138     joris     847:                        cf->file_status = FILE_REMOVE_ENTRY;
1.249     tobias    848:                } else if (cf->file_rcs == NULL || rcsdead == 1 ||
                    849:                    !(cf->file_flags & FILE_HAS_TAG)) {
1.138     joris     850:                        cf->file_status = FILE_ADDED;
                    851:                } else {
1.178     joris     852:                        cvs_log(LP_NOTICE,
                    853:                            "conflict: %s already created by others",
                    854:                            cf->file_path);
1.138     joris     855:                        cf->file_status = FILE_CONFLICT;
                    856:                }
1.208     tobias    857:                break;
                    858:        case CVS_ENT_REMOVED:
1.252     joris     859:                if (cf->file_flags & FILE_ON_DISK) {
1.178     joris     860:                        cvs_log(LP_NOTICE,
                    861:                            "%s should be removed but is still there",
                    862:                            cf->file_path);
1.138     joris     863:                        cf->file_status = FILE_REMOVED;
                    864:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    865:                        cf->file_status = FILE_REMOVE_ENTRY;
1.66      joris     866:                } else {
1.238     tobias    867:                        if (rcsnum_differ(cf->file_ent->ce_rev,
                    868:                            cf->file_rcsrev) && cvs_cmdop != CVS_OP_ADD) {
1.178     joris     869:                                cvs_log(LP_NOTICE,
                    870:                                    "conflict: removed %s was modified"
                    871:                                    " by a second party",
                    872:                                    cf->file_path);
1.138     joris     873:                                cf->file_status = FILE_CONFLICT;
                    874:                        } else {
                    875:                                cf->file_status = FILE_REMOVED;
                    876:                        }
1.66      joris     877:                }
1.208     tobias    878:                break;
                    879:        case CVS_ENT_REG:
1.218     tobias    880:                if (cf->file_rcs == NULL || cf->file_rcsrev == NULL ||
1.219     tobias    881:                    rcsdead == 1 || (reset_tag == 1 && cf->in_attic == 1) ||
                    882:                    (notag == 1 && tag != NULL)) {
1.252     joris     883:                        if (!(cf->file_flags & FILE_ON_DISK)) {
1.178     joris     884:                                cvs_log(LP_NOTICE,
                    885:                                    "warning: %s's entry exists but"
1.235     joris     886:                                    " is no longer in the repository,"
1.178     joris     887:                                    " removing entry",
                    888:                                     cf->file_path);
1.138     joris     889:                                cf->file_status = FILE_REMOVE_ENTRY;
                    890:                        } else {
                    891:                                if (ismodified) {
1.178     joris     892:                                        cvs_log(LP_NOTICE,
                    893:                                            "conflict: %s is no longer "
                    894:                                            "in the repository but is "
                    895:                                            "locally modified",
                    896:                                            cf->file_path);
1.242     tobias    897:                                        if (cvs_cmdop == CVS_OP_COMMIT)
                    898:                                                cf->file_status = FILE_UNLINK;
                    899:                                        else
                    900:                                                cf->file_status = FILE_CONFLICT;
1.204     tobias    901:                                } else if (cvs_cmdop != CVS_OP_IMPORT) {
1.178     joris     902:                                        cvs_log(LP_NOTICE,
                    903:                                            "%s is no longer in the "
                    904:                                            "repository",
                    905:                                            cf->file_path);
1.128     joris     906:
1.138     joris     907:                                        cf->file_status = FILE_UNLINK;
                    908:                                }
                    909:                        }
1.218     tobias    910:                } else if (cf->file_rcsrev == NULL) {
                    911:                        cf->file_status = FILE_UNLINK;
1.237     xsa       912:                } else {
1.252     joris     913:                        if (!(cf->file_flags & FILE_ON_DISK)) {
1.178     joris     914:                                if (cvs_cmdop != CVS_OP_REMOVE) {
1.138     joris     915:                                        cvs_log(LP_NOTICE,
                    916:                                            "warning: %s was lost",
                    917:                                            cf->file_path);
1.178     joris     918:                                }
1.138     joris     919:                                cf->file_status = FILE_LOST;
                    920:                        } else {
                    921:                                if (ismodified == 1)
                    922:                                        cf->file_status = FILE_MODIFIED;
                    923:                                else
                    924:                                        cf->file_status = FILE_UPTODATE;
1.238     tobias    925:                                if (rcsnum_differ(cf->file_ent->ce_rev,
                    926:                                    cf->file_rcsrev)) {
1.138     joris     927:                                        if (cf->file_status == FILE_MODIFIED)
                    928:                                                cf->file_status = FILE_MERGE;
                    929:                                        else
                    930:                                                cf->file_status = FILE_PATCH;
1.242     tobias    931:                                } else if (cf->file_ent->ce_conflict != NULL &&
                    932:                                    cf->file_status != FILE_MODIFIED) {
                    933:                                        cf->file_status = FILE_CONFLICT;
1.138     joris     934:                                }
                    935:                        }
1.128     joris     936:                }
1.254     joris     937:                break;
                    938:        case CVS_ENT_UNKNOWN:
                    939:                if (cvs_server_active != 1)
                    940:                        fatal("server-side questionable in local mode?");
                    941:                cf->file_status = FILE_UNKNOWN;
1.208     tobias    942:                break;
                    943:        default:
                    944:                break;
1.128     joris     945:        }
1.138     joris     946: }
1.14      jfb       947:
1.138     joris     948: void
                    949: cvs_file_free(struct cvs_file *cf)
                    950: {
                    951:        xfree(cf->file_name);
                    952:        xfree(cf->file_wd);
                    953:        xfree(cf->file_path);
                    954:
1.167     joris     955:        if (cf->file_rcsrev != NULL)
                    956:                rcsnum_free(cf->file_rcsrev);
1.138     joris     957:        if (cf->file_rpath != NULL)
                    958:                xfree(cf->file_rpath);
                    959:        if (cf->file_ent != NULL)
                    960:                cvs_ent_free(cf->file_ent);
                    961:        if (cf->file_rcs != NULL)
                    962:                rcs_close(cf->file_rcs);
                    963:        if (cf->fd != -1)
                    964:                (void)close(cf->fd);
                    965:        if (cf->repo_fd != -1)
                    966:                (void)close(cf->repo_fd);
                    967:        xfree(cf);
1.23      jfb       968: }
                    969:
1.165     xsa       970: int
1.23      jfb       971: cvs_file_cmpname(const char *name1, const char *name2)
                    972: {
                    973:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    974:            (strcasecmp(name1, name2));
1.164     xsa       975: }
                    976:
                    977: int
                    978: cvs_file_cmp(const char *file1, const char *file2)
                    979: {
                    980:        struct stat stb1, stb2;
                    981:        int fd1, fd2, ret;
                    982:
                    983:        ret = 0;
                    984:
                    985:        if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
                    986:                fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
                    987:        if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
                    988:                fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
                    989:
                    990:        if (fstat(fd1, &stb1) == -1)
                    991:                fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
                    992:        if (fstat(fd2, &stb2) == -1)
                    993:                fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
                    994:
                    995:        if (stb1.st_size != stb2.st_size ||
                    996:            (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
                    997:                ret = 1;
                    998:                goto out;
                    999:        }
                   1000:
                   1001:        if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
                   1002:                if (stb1.st_rdev != stb2.st_rdev)
                   1003:                        ret = 1;
                   1004:                goto out;
                   1005:        }
                   1006:
                   1007:        if (S_ISREG(stb1.st_mode)) {
                   1008:                void *p1, *p2;
                   1009:
1.228     tobias   1010:                if (stb1.st_size > SIZE_MAX) {
1.164     xsa      1011:                        ret = 1;
                   1012:                        goto out;
1.226     deraadt  1013:                }
1.164     xsa      1014:
                   1015:                if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
                   1016:                    MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
                   1017:                        fatal("cvs_file_cmp: mmap failed");
                   1018:
                   1019:                if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
                   1020:                    MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
                   1021:                        fatal("cvs_file_cmp: mmap failed");
                   1022:
                   1023:                madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
                   1024:                madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
                   1025:
                   1026:                ret = memcmp(p1, p2, stb1.st_size);
                   1027:
                   1028:                (void)munmap(p1, stb1.st_size);
                   1029:                (void)munmap(p2, stb1.st_size);
                   1030:        }
                   1031:
                   1032: out:
                   1033:        (void)close(fd1);
                   1034:        (void)close(fd2);
1.166     xsa      1035:
                   1036:        return (ret);
                   1037: }
                   1038:
                   1039: int
                   1040: cvs_file_copy(const char *from, const char *to)
                   1041: {
                   1042:        struct stat st;
                   1043:        struct timeval tv[2];
                   1044:        time_t atime, mtime;
                   1045:        int src, dst, ret;
                   1046:
                   1047:        ret = 0;
                   1048:
                   1049:        cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
                   1050:
                   1051:        if (cvs_noexec == 1)
                   1052:                return (0);
                   1053:
                   1054:        if ((src = open(from, O_RDONLY, 0)) == -1)
                   1055:                fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
                   1056:
                   1057:        if (fstat(src, &st) == -1)
                   1058:                fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
                   1059:
                   1060:        atime = st.st_atimespec.tv_sec;
                   1061:        mtime = st.st_mtimespec.tv_sec;
                   1062:
                   1063:        if (S_ISREG(st.st_mode)) {
1.197     tobias   1064:                char *p;
1.166     xsa      1065:                int saved_errno;
                   1066:
1.228     tobias   1067:                if (st.st_size > SIZE_MAX) {
1.166     xsa      1068:                        ret = -1;
                   1069:                        goto out;
1.226     deraadt  1070:                }
1.166     xsa      1071:
                   1072:                if ((dst = open(to, O_CREAT|O_TRUNC|O_WRONLY,
                   1073:                    st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) == -1)
                   1074:                        fatal("cvs_file_copy: open `%s': %s",
                   1075:                            to, strerror(errno));
                   1076:
                   1077:                if ((p = mmap(NULL, st.st_size, PROT_READ,
                   1078:                    MAP_FILE, src, (off_t)0)) == MAP_FAILED) {
                   1079:                        saved_errno = errno;
                   1080:                        (void)unlink(to);
                   1081:                        fatal("cvs_file_copy: mmap: %s", strerror(saved_errno));
                   1082:                }
                   1083:
                   1084:                madvise(p, st.st_size, MADV_SEQUENTIAL);
                   1085:
1.197     tobias   1086:                if (atomicio(vwrite, dst, p, st.st_size) != st.st_size) {
                   1087:                        saved_errno = errno;
                   1088:                        (void)unlink(to);
                   1089:                        fatal("cvs_file_copy: `%s': %s", from,
                   1090:                            strerror(saved_errno));
1.166     xsa      1091:                }
                   1092:
                   1093:                (void)munmap(p, st.st_size);
                   1094:
                   1095:                tv[0].tv_sec = atime;
                   1096:                tv[1].tv_sec = mtime;
                   1097:
                   1098:                if (futimes(dst, tv) == -1) {
                   1099:                        saved_errno = errno;
                   1100:                        (void)unlink(to);
                   1101:                        fatal("cvs_file_copy: futimes: %s",
                   1102:                            strerror(saved_errno));
                   1103:                }
                   1104:                (void)close(dst);
                   1105:        }
                   1106: out:
                   1107:        (void)close(src);
1.164     xsa      1108:
                   1109:        return (ret);
1.256   ! joris    1110: }
        !          1111:
        !          1112: int
        !          1113: cvs_filelist_cmp(struct cvs_filelist *f1, struct cvs_filelist *f2)
        !          1114: {
        !          1115:        return (strcmp(f1->file_path, f2->file_path));
1.14      jfb      1116: }