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

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