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

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