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

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