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

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