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

1.200   ! joris       1: /*     $OpenBSD: file.c,v 1.199 2007/09/22 16:01:22 joris Exp $        */
1.1       jfb         2: /*
1.138     joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26      jfb         5:  * All rights reserved.
1.1       jfb         6:  *
1.26      jfb         7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
1.1       jfb        10:  *
1.26      jfb        11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        13:  * 2. The name of the author may not be used to endorse or promote products
1.26      jfb        14:  *    derived from this software without specific prior written permission.
1.1       jfb        15:  *
                     16:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     17:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     18:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     19:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     20:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     21:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     22:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     23:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     24:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.26      jfb        25:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        26:  */
                     27:
1.189     otto       28: #include <sys/types.h>
                     29: #include <sys/mman.h>
                     30: #include <sys/stat.h>
1.1       jfb        31:
1.189     otto       32: #include <errno.h>
                     33: #include <fcntl.h>
                     34: #include <fnmatch.h>
                     35: #include <libgen.h>
                     36: #include <string.h>
                     37: #include <unistd.h>
1.164     xsa        38:
1.197     tobias     39: #include "atomicio.h"
1.1       jfb        40: #include "cvs.h"
1.198     joris      41: #include "remote.h"
1.1       jfb        42:
1.104     xsa        43: #define CVS_IGN_STATIC 0x01    /* pattern is static, no need to glob */
1.1       jfb        44:
1.104     xsa        45: #define CVS_CHAR_ISMETA(c)     ((c == '*') || (c == '?') || (c == '['))
1.1       jfb        46:
1.196     tobias     47: extern int print_stdout;
                     48:
1.1       jfb        49: /*
                     50:  * Standard patterns to ignore.
                     51:  */
                     52: static const char *cvs_ign_std[] = {
                     53:        ".",
                     54:        "..",
                     55:        "*.o",
1.45      xsa        56:        "*.a",
1.1       jfb        57:        "*.bak",
                     58:        "*.orig",
                     59:        "*.rej",
1.45      xsa        60:        "*.old",
1.1       jfb        61:        "*.exe",
                     62:        "*.depend",
1.45      xsa        63:        "*.obj",
                     64:        "*.elc",
                     65:        "*.ln",
                     66:        "*.olb",
1.1       jfb        67:        "CVS",
                     68:        "core",
1.102     joris      69:        "cvslog*",
1.49      jfb        70:        "*.core",
1.8       jfb        71:        ".#*",
1.45      xsa        72:        "*~",
                     73:        "_$*",
                     74:        "*$",
1.1       jfb        75: };
                     76:
1.199     joris      77: char *cvs_directory_tag = NULL;
1.138     joris      78: struct ignore_head cvs_ign_pats;
                     79: struct ignore_head dir_ign_pats;
1.1       jfb        80:
1.138     joris      81: void
1.1       jfb        82: cvs_file_init(void)
                     83: {
1.183     xsa        84:        int i;
1.138     joris      85:        FILE *ifp;
1.1       jfb        86:        size_t len;
1.176     otto       87:        char path[MAXPATHLEN], buf[MAXNAMLEN];
1.1       jfb        88:
                     89:        TAILQ_INIT(&cvs_ign_pats);
1.138     joris      90:        TAILQ_INIT(&dir_ign_pats);
1.4       jfb        91:
1.1       jfb        92:        /* standard patterns to ignore */
1.10      jfb        93:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.138     joris      94:                cvs_file_ignore(cvs_ign_std[i], &cvs_ign_pats);
1.195     tobias     95:
                     96:        if (cvs_homedir == NULL)
                     97:                return;
1.1       jfb        98:
                     99:        /* read the cvsignore file in the user's home directory, if any */
1.183     xsa       100:        (void)xsnprintf(path, MAXPATHLEN, "%s/.cvsignore", cvs_homedir);
1.61      xsa       101:
1.111     xsa       102:        ifp = fopen(path, "r");
                    103:        if (ifp == NULL) {
                    104:                if (errno != ENOENT)
                    105:                        cvs_log(LP_ERRNO,
                    106:                            "failed to open user's cvsignore file `%s'", path);
                    107:        } else {
1.138     joris     108:                while (fgets(buf, MAXNAMLEN, ifp) != NULL) {
1.111     xsa       109:                        len = strlen(buf);
                    110:                        if (len == 0)
                    111:                                continue;
1.163     moritz    112:                        if (buf[len - 1] == '\n')
                    113:                                buf[len - 1] = '\0';
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) {
                    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:
1.1       jfb       172:        return (0);
                    173: }
                    174:
1.138     joris     175: void
                    176: cvs_file_run(int argc, char **argv, struct cvs_recursion *cr)
1.1       jfb       177: {
1.138     joris     178:        int i;
                    179:        struct cvs_flisthead fl;
1.84      joris     180:
1.138     joris     181:        TAILQ_INIT(&fl);
1.85      joris     182:
1.138     joris     183:        for (i = 0; i < argc; i++)
1.199     joris     184:                cvs_file_get(argv[i], 1, &fl);
1.1       jfb       185:
1.138     joris     186:        cvs_file_walklist(&fl, cr);
                    187:        cvs_file_freelist(&fl);
1.3       jfb       188: }
                    189:
1.138     joris     190: struct cvs_filelist *
1.199     joris     191: cvs_file_get(const char *name, int check_dir_tag, struct cvs_flisthead *fl)
1.138     joris     192: {
                    193:        const char *p;
                    194:        struct cvs_filelist *l;
1.3       jfb       195:
1.138     joris     196:        for (p = name; p[0] == '.' && p[1] == '/';)
                    197:                p += 2;
1.35      jfb       198:
1.138     joris     199:        TAILQ_FOREACH(l, fl, flist)
                    200:                if (!strcmp(l->file_path, p))
                    201:                        return (l);
1.35      jfb       202:
1.138     joris     203:        l = (struct cvs_filelist *)xmalloc(sizeof(*l));
                    204:        l->file_path = xstrdup(p);
1.199     joris     205:        l->check_dir_tag = check_dir_tag;
1.35      jfb       206:
1.138     joris     207:        TAILQ_INSERT_TAIL(fl, l, flist);
                    208:        return (l);
1.35      jfb       209: }
                    210:
1.138     joris     211: struct cvs_file *
                    212: cvs_file_get_cf(const char *d, const char *f, int fd, int type)
                    213: {
                    214:        struct cvs_file *cf;
1.176     otto      215:        char *p, rpath[MAXPATHLEN];
1.3       jfb       216:
1.183     xsa       217:        (void)xsnprintf(rpath, MAXPATHLEN, "%s/%s", d, f);
1.138     joris     218:
                    219:        for (p = rpath; p[0] == '.' && p[1] == '/';)
                    220:                p += 2;
                    221:
                    222:        cf = (struct cvs_file *)xmalloc(sizeof(*cf));
                    223:        memset(cf, 0, sizeof(*cf));
                    224:
                    225:        cf->file_name = xstrdup(f);
                    226:        cf->file_wd = xstrdup(d);
                    227:        cf->file_path = xstrdup(p);
                    228:        cf->fd = fd;
                    229:        cf->repo_fd = -1;
                    230:        cf->file_type = type;
                    231:        cf->file_status = cf->file_flags = 0;
                    232:        cf->file_ent = NULL;
1.68      joris     233:
1.138     joris     234:        return (cf);
1.9       jfb       235: }
                    236:
1.138     joris     237: void
                    238: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9       jfb       239: {
1.183     xsa       240:        int fd, type;
1.138     joris     241:        struct stat st;
                    242:        struct cvs_file *cf;
                    243:        struct cvs_filelist *l, *nxt;
1.176     otto      244:        char *d, *f, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.138     joris     245:
                    246:        for (l = TAILQ_FIRST(fl); l != NULL; l = nxt) {
                    247:                if (cvs_quit)
                    248:                        fatal("received signal %d", sig_received);
                    249:
                    250:                cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
                    251:                    l->file_path);
                    252:
                    253:                if ((f = basename(l->file_path)) == NULL)
                    254:                        fatal("cvs_file_walklist: basename failed");
                    255:                if ((d = dirname(l->file_path)) == NULL)
                    256:                        fatal("cvs_file_walklist: dirname failed");
                    257:
1.146     joris     258:                type = CVS_FILE;
1.138     joris     259:                if ((fd = open(l->file_path, O_RDONLY)) != -1) {
                    260:                        if (fstat(fd, &st) == -1) {
                    261:                                cvs_log(LP_ERRNO, "%s", l->file_path);
                    262:                                (void)close(fd);
                    263:                                goto next;
                    264:                        }
1.26      jfb       265:
1.138     joris     266:                        if (S_ISDIR(st.st_mode))
                    267:                                type = CVS_DIR;
                    268:                        else if (S_ISREG(st.st_mode))
                    269:                                type = CVS_FILE;
                    270:                        else {
                    271:                                cvs_log(LP_ERR,
                    272:                                    "ignoring bad file type for %s",
                    273:                                    l->file_path);
                    274:                                (void)close(fd);
                    275:                                goto next;
                    276:                        }
1.162     joris     277:                } else if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.196     tobias    278:                        /*
                    279:                         * During checkout -p, do not use any locally
                    280:                         * available directories.
                    281:                         */
                    282:                        if (cvs_cmdop != CVS_OP_CHECKOUT || !print_stdout)
                    283:                                if (stat(d, &st) == -1) {
                    284:                                        cvs_log(LP_ERRNO, "%s", d);
                    285:                                        goto next;
                    286:                                }
1.87      joris     287:
1.146     joris     288:                        cvs_get_repository_path(d, repo, MAXPATHLEN);
1.183     xsa       289:                        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
1.138     joris     290:                            repo, f);
                    291:
                    292:                        if ((fd = open(fpath, O_RDONLY)) == -1) {
                    293:                                strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
                    294:                                fd = open(fpath, O_RDONLY);
                    295:                        }
1.26      jfb       296:
1.138     joris     297:                        if (fd != -1) {
                    298:                                if (fstat(fd, &st) == -1)
                    299:                                        fatal("cvs_file_walklist: %s: %s",
                    300:                                             fpath, strerror(errno));
                    301:
                    302:                                if (S_ISDIR(st.st_mode))
                    303:                                        type = CVS_DIR;
                    304:                                else if (S_ISREG(st.st_mode))
                    305:                                        type = CVS_FILE;
                    306:                                else {
                    307:                                        cvs_log(LP_ERR,
                    308:                                            "ignoring bad file type for %s",
                    309:                                            l->file_path);
                    310:                                        (void)close(fd);
                    311:                                        goto next;
                    312:                                }
                    313:
                    314:                                /* this file is not in our working copy yet */
                    315:                                (void)close(fd);
                    316:                                fd = -1;
                    317:                        }
                    318:                }
1.73      joris     319:
1.138     joris     320:                cf = cvs_file_get_cf(d, f, fd, type);
                    321:                if (cf->file_type == CVS_DIR) {
                    322:                        cvs_file_walkdir(cf, cr);
                    323:                } else {
1.199     joris     324:                        if (l->check_dir_tag) {
                    325:                                cvs_parse_tagfile(cf->file_wd,
                    326:                                    &cvs_directory_tag, NULL, NULL);
                    327:
                    328:                                if (cvs_directory_tag == NULL &&
                    329:                                    cvs_specified_tag != NULL)
                    330:                                        cvs_directory_tag = cvs_specified_tag;
                    331:                        }
                    332:
1.161     joris     333:                        if (cr->fileproc != NULL)
                    334:                                cr->fileproc(cf);
1.128     joris     335:                }
1.100     joris     336:
1.138     joris     337:                cvs_file_free(cf);
1.123     joris     338:
1.138     joris     339: next:
                    340:                nxt = TAILQ_NEXT(l, flist);
                    341:                TAILQ_REMOVE(fl, l, flist);
1.68      joris     342:
1.138     joris     343:                xfree(l->file_path);
                    344:                xfree(l);
1.100     joris     345:        }
                    346: }
                    347:
1.138     joris     348: void
                    349: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100     joris     350: {
1.171     joris     351:        int l, type;
1.138     joris     352:        FILE *fp;
                    353:        int nbytes;
                    354:        size_t len;
                    355:        long base;
                    356:        size_t bufsize;
                    357:        struct stat st;
                    358:        struct dirent *dp;
1.100     joris     359:        struct cvs_ent *ent;
1.138     joris     360:        struct cvs_ignpat *ip;
                    361:        struct cvs_ent_line *line;
                    362:        struct cvs_flisthead fl, dl;
                    363:        CVSENTRIES *entlist;
1.194     joris     364:        char *buf, *ebuf, *cp, repo[MAXPATHLEN], fpath[MAXPATHLEN];
1.100     joris     365:
1.138     joris     366:        cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100     joris     367:
1.138     joris     368:        if (cr->enterdir != NULL)
                    369:                cr->enterdir(cf);
1.100     joris     370:
1.161     joris     371:        if (cr->fileproc != NULL)
                    372:                cr->fileproc(cf);
1.100     joris     373:
1.146     joris     374:        if (cf->file_status == FILE_SKIP)
                    375:                return;
                    376:
1.100     joris     377:        /*
1.151     joris     378:         * If we do not have a admin directory inside here, dont bother,
                    379:         * unless we are running import.
1.100     joris     380:         */
1.183     xsa       381:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138     joris     382:            CVS_PATH_CVSDIR);
1.100     joris     383:
1.146     joris     384:        l = stat(fpath, &st);
1.192     niallo    385:        if (cvs_cmdop != CVS_OP_IMPORT && cvs_cmdop != CVS_OP_RLOG &&
1.151     joris     386:            (l == -1 || (l == 0 && !S_ISDIR(st.st_mode)))) {
1.138     joris     387:                return;
1.100     joris     388:        }
                    389:
1.199     joris     390:        cvs_parse_tagfile(cf->file_path, &cvs_directory_tag, NULL, NULL);
                    391:
1.100     joris     392:        /*
1.138     joris     393:         * check for a local .cvsignore file
1.100     joris     394:         */
1.183     xsa       395:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
1.100     joris     396:
1.138     joris     397:        if ((fp = fopen(fpath, "r")) != NULL) {
1.163     moritz    398:                while (fgets(fpath, MAXPATHLEN, fp) != NULL) {
1.138     joris     399:                        len = strlen(fpath);
1.163     moritz    400:                        if (len == 0)
                    401:                                continue;
1.138     joris     402:                        if (fpath[len - 1] == '\n')
                    403:                                fpath[len - 1] = '\0';
1.26      jfb       404:
1.138     joris     405:                        cvs_file_ignore(fpath, &dir_ign_pats);
1.68      joris     406:                }
1.26      jfb       407:
1.138     joris     408:                (void)fclose(fp);
1.100     joris     409:        }
                    410:
1.138     joris     411:        if (fstat(cf->fd, &st) == -1)
                    412:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    413:                    strerror(errno));
1.100     joris     414:
1.138     joris     415:        bufsize = st.st_size;
                    416:        if (bufsize < st.st_blksize)
                    417:                bufsize = st.st_blksize;
1.116     joris     418:
1.138     joris     419:        buf = xmalloc(bufsize);
                    420:        TAILQ_INIT(&fl);
                    421:        TAILQ_INIT(&dl);
1.87      joris     422:
1.138     joris     423:        while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
                    424:                ebuf = buf + nbytes;
                    425:                cp = buf;
1.26      jfb       426:
1.138     joris     427:                while (cp < ebuf) {
                    428:                        dp = (struct dirent *)cp;
                    429:                        if (!strcmp(dp->d_name, ".") ||
                    430:                            !strcmp(dp->d_name, "..") ||
                    431:                            !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
1.186     joris     432:                            dp->d_fileno == 0) {
1.138     joris     433:                                cp += dp->d_reclen;
                    434:                                continue;
                    435:                        }
1.13      jfb       436:
1.138     joris     437:                        if (cvs_file_chkign(dp->d_name)) {
                    438:                                cp += dp->d_reclen;
1.13      jfb       439:                                continue;
1.138     joris     440:                        }
1.13      jfb       441:
1.192     niallo    442:                        len = xsnprintf(fpath, MAXPATHLEN, "%s/%s",
1.138     joris     443:                            cf->file_path, dp->d_name);
1.13      jfb       444:
1.138     joris     445:                        /*
1.171     joris     446:                         * nfs and afs will show d_type as DT_UNKNOWN
                    447:                         * for files and/or directories so when we encounter
1.184     todd      448:                         * this we call lstat() on the path to be sure.
1.138     joris     449:                         */
1.171     joris     450:                        if (dp->d_type == DT_UNKNOWN) {
1.184     todd      451:                                if (lstat(fpath, &st) == -1)
1.171     joris     452:                                        fatal("'%s': %s", fpath,
                    453:                                            strerror(errno));
                    454:
                    455:                                switch (st.st_mode & S_IFMT) {
                    456:                                case S_IFDIR:
                    457:                                        type = CVS_DIR;
                    458:                                        break;
                    459:                                case S_IFREG:
                    460:                                        type = CVS_FILE;
                    461:                                        break;
                    462:                                default:
1.175     ray       463:                                        type = FILE_SKIP;
                    464:                                        break;
1.171     joris     465:                                }
                    466:                        } else {
                    467:                                switch (dp->d_type) {
                    468:                                case DT_DIR:
                    469:                                        type = CVS_DIR;
                    470:                                        break;
                    471:                                case DT_REG:
                    472:                                        type = CVS_FILE;
                    473:                                        break;
                    474:                                default:
1.175     ray       475:                                        type = FILE_SKIP;
                    476:                                        break;
1.171     joris     477:                                }
1.175     ray       478:                        }
                    479:
                    480:                        if (type == FILE_SKIP) {
                    481:                                if (verbosity > 1) {
                    482:                                        cvs_log(LP_NOTICE, "ignoring `%s'",
                    483:                                            dp->d_name);
                    484:                                }
                    485:                                cp += dp->d_reclen;
                    486:                                continue;
1.171     joris     487:                        }
                    488:
                    489:                        switch (type) {
                    490:                        case CVS_DIR:
1.192     niallo    491:                                if (cr->flags & CR_RECURSE_DIRS)
1.199     joris     492:                                        cvs_file_get(fpath, 0, &dl);
1.171     joris     493:                                break;
                    494:                        case CVS_FILE:
1.199     joris     495:                                cvs_file_get(fpath, 0, &fl);
1.171     joris     496:                                break;
                    497:                        default:
                    498:                                fatal("type %d unknown, shouldn't happen",
                    499:                                    type);
                    500:                        }
1.13      jfb       501:
1.138     joris     502:                        cp += dp->d_reclen;
                    503:                }
1.34      jfb       504:        }
                    505:
1.138     joris     506:        if (nbytes == -1)
                    507:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    508:                    strerror(errno));
1.22      jfb       509:
1.138     joris     510:        xfree(buf);
1.22      jfb       511:
1.138     joris     512:        while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
                    513:                TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
                    514:                xfree(ip);
1.91      joris     515:        }
1.114     xsa       516:
1.138     joris     517:        entlist = cvs_ent_open(cf->file_path);
                    518:        TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
                    519:                ent = cvs_ent_parse(line->buf);
1.61      xsa       520:
1.183     xsa       521:                (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
1.138     joris     522:                    ent->ce_name);
1.100     joris     523:
1.140     joris     524:                if (!(cr->flags & CR_RECURSE_DIRS) &&
                    525:                    ent->ce_type == CVS_ENT_DIR)
                    526:                        continue;
1.138     joris     527:                if (ent->ce_type == CVS_ENT_DIR)
1.199     joris     528:                        cvs_file_get(fpath, 0, &dl);
1.138     joris     529:                else if (ent->ce_type == CVS_ENT_FILE)
1.199     joris     530:                        cvs_file_get(fpath, 0, &fl);
1.68      joris     531:
1.138     joris     532:                cvs_ent_free(ent);
1.128     joris     533:        }
                    534:
1.138     joris     535:        cvs_ent_close(entlist, ENT_NOSYNC);
1.91      joris     536:
1.140     joris     537:        if (cr->flags & CR_REPO) {
1.146     joris     538:                cvs_get_repository_path(cf->file_path, repo, MAXPATHLEN);
1.140     joris     539:                cvs_repository_lock(repo);
1.68      joris     540:
1.140     joris     541:                cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
                    542:                    (cr->flags & CR_RECURSE_DIRS));
                    543:        }
1.68      joris     544:
1.138     joris     545:        cvs_file_walklist(&fl, cr);
                    546:        cvs_file_freelist(&fl);
1.68      joris     547:
1.176     otto      548:        if (cr->flags & CR_REPO)
1.140     joris     549:                cvs_repository_unlock(repo);
1.68      joris     550:
1.138     joris     551:        cvs_file_walklist(&dl, cr);
                    552:        cvs_file_freelist(&dl);
1.34      jfb       553:
1.138     joris     554:        if (cr->leavedir != NULL)
                    555:                cr->leavedir(cf);
1.199     joris     556:
                    557:        if (cvs_directory_tag != NULL) {
                    558:                cvs_write_tagfile(cf->file_path, cvs_directory_tag, NULL, 0);
                    559:                xfree(cvs_directory_tag);
                    560:                cvs_directory_tag = NULL;
                    561:        }
1.3       jfb       562: }
                    563:
                    564: void
1.138     joris     565: cvs_file_freelist(struct cvs_flisthead *fl)
1.3       jfb       566: {
1.138     joris     567:        struct cvs_filelist *f;
1.62      jfb       568:
1.138     joris     569:        while ((f = TAILQ_FIRST(fl)) != NULL) {
                    570:                TAILQ_REMOVE(fl, f, flist);
                    571:                xfree(f->file_path);
                    572:                xfree(f);
1.62      jfb       573:        }
1.5       jfb       574: }
                    575:
1.138     joris     576: void
1.185     joris     577: cvs_file_classify(struct cvs_file *cf, const char *tag)
1.3       jfb       578: {
1.138     joris     579:        size_t len;
                    580:        struct stat st;
1.149     joris     581:        BUF *b1, *b2;
1.199     joris     582:        int server_has_file, notag;
1.183     xsa       583:        int rflags, ismodified, rcsdead;
1.138     joris     584:        CVSENTRIES *entlist = NULL;
                    585:        const char *state;
1.193     xsa       586:        char repo[MAXPATHLEN], rcsfile[MAXPATHLEN];
1.194     joris     587:        char r1[CVS_REV_BUFSZ], r2[CVS_REV_BUFSZ];
1.138     joris     588:
1.198     joris     589:        cvs_log(LP_TRACE, "cvs_file_classify(%s, %s)", cf->file_path,
                    590:            (tag != NULL) ? tag : "none");
1.138     joris     591:
                    592:        if (!strcmp(cf->file_path, ".")) {
                    593:                cf->file_status = FILE_UPTODATE;
                    594:                return;
                    595:        }
                    596:
1.146     joris     597:        cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.183     xsa       598:        (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s",
1.138     joris     599:            repo, cf->file_name);
                    600:
                    601:        if (cf->file_type == CVS_FILE) {
                    602:                len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
                    603:                if (len >= MAXPATHLEN)
                    604:                        fatal("cvs_file_classify: truncation");
                    605:        }
                    606:
                    607:        cf->file_rpath = xstrdup(rcsfile);
1.151     joris     608:        entlist = cvs_ent_open(cf->file_wd);
1.138     joris     609:        cf->file_ent = cvs_ent_get(entlist, cf->file_name);
                    610:
1.144     joris     611:        if (cf->file_ent != NULL) {
                    612:                if (cf->file_ent->ce_type == CVS_ENT_DIR &&
                    613:                    cf->file_type != CVS_DIR)
1.148     pedro     614:                        fatal("%s is supposed to be a directory, but it is not",
1.144     joris     615:                            cf->file_path);
                    616:                if (cf->file_ent->ce_type == CVS_ENT_FILE &&
                    617:                    cf->file_type != CVS_FILE)
1.148     pedro     618:                        fatal("%s is supposed to be a file, but it is not",
1.144     joris     619:                            cf->file_path);
                    620:        }
                    621:
1.138     joris     622:        if (cf->file_type == CVS_DIR) {
                    623:                if (cf->fd == -1 && stat(rcsfile, &st) != -1)
                    624:                        cf->file_status = DIR_CREATE;
1.192     niallo    625:                else if (cf->file_ent != NULL || cvs_cmdop == CVS_OP_RLOG)
1.138     joris     626:                        cf->file_status = FILE_UPTODATE;
1.144     joris     627:                else
                    628:                        cf->file_status = FILE_UNKNOWN;
                    629:
1.138     joris     630:                cvs_ent_close(entlist, ENT_NOSYNC);
                    631:                return;
                    632:        }
                    633:
1.147     joris     634:        rflags = RCS_READ;
                    635:        switch (cvs_cmdop) {
                    636:        case CVS_OP_COMMIT:
                    637:                rflags = RCS_WRITE;
                    638:                break;
1.156     joris     639:        case CVS_OP_IMPORT:
1.147     joris     640:        case CVS_OP_LOG:
1.192     niallo    641:        case CVS_OP_RLOG:
1.147     joris     642:                rflags |= RCS_PARSE_FULLY;
                    643:                break;
                    644:        }
                    645:
1.138     joris     646:        cf->repo_fd = open(cf->file_rpath, O_RDONLY);
                    647:        if (cf->repo_fd != -1) {
                    648:                cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
                    649:                if (cf->file_rcs == NULL)
1.152     joris     650:                        fatal("cvs_file_classify: failed to parse RCS");
1.194     joris     651:        } else {
                    652:                (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s/%s%s",
                    653:                     repo, CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
                    654:
                    655:                cf->repo_fd = open(rcsfile, O_RDONLY);
                    656:                if (cf->repo_fd != -1) {
                    657:                        xfree(cf->file_rpath);
                    658:                        cf->file_rpath = xstrdup(rcsfile);
                    659:                        cf->file_rcs = rcs_open(cf->file_rpath,
                    660:                            cf->repo_fd, rflags);
                    661:                        if (cf->file_rcs == NULL)
                    662:                                fatal("cvs_file_classify: failed to parse RCS");
                    663:                        cf->in_attic = 1;
                    664:                } else {
                    665:                        cf->file_rcs = NULL;
                    666:                }
1.190     niallo    667:        }
                    668:
1.199     joris     669:        notag = 0;
1.200   ! joris     670:        cf->file_flags |= FILE_HAS_TAG;
1.190     niallo    671:        if (tag != NULL && cf->file_rcs != NULL) {
1.199     joris     672:                if ((cf->file_rcsrev = rcs_translate_tag(tag,
                    673:                    cf->file_rcs)) != NULL) {
                    674:                        rcsnum_tostr(cf->file_rcsrev, r1, sizeof(r1));
                    675:                } else {
                    676:                        notag = 1;
                    677:                        cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.200   ! joris     678:                        cf->file_flags &= ~FILE_HAS_TAG;
1.152     joris     679:                }
1.172     niallo    680:        } else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL) {
1.169     joris     681:                cf->file_rcsrev = rcsnum_alloc();
                    682:                rcsnum_cpy(cf->file_ent->ce_rev, cf->file_rcsrev, 0);
1.190     niallo    683:        } else if (cf->file_rcs != NULL) {
1.156     joris     684:                cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.190     niallo    685:        } else {
1.153     joris     686:                cf->file_rcsrev = NULL;
1.190     niallo    687:        }
1.153     joris     688:
1.138     joris     689:        if (cf->file_ent != NULL)
                    690:                rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
1.190     niallo    691:        if (cf->file_rcsrev != NULL) {
1.153     joris     692:                rcsnum_tostr(cf->file_rcsrev, r2, sizeof(r2));
1.190     niallo    693:        }
1.6       jfb       694:
1.138     joris     695:        ismodified = rcsdead = 0;
                    696:        if (cf->fd != -1 && cf->file_ent != NULL) {
                    697:                if (fstat(cf->fd, &st) == -1)
                    698:                        fatal("cvs_file_classify: %s", strerror(errno));
1.6       jfb       699:
1.177     joris     700:                if (st.st_mtime != cf->file_ent->ce_mtime)
1.138     joris     701:                        ismodified = 1;
1.149     joris     702:        }
                    703:
1.198     joris     704:        server_has_file = 0;
                    705:        if (cvs_server_active == 1 && cf->file_ent != NULL &&
                    706:            cf->file_ent->ce_mtime == CVS_SERVER_UPTODATE) {
                    707:                server_has_file = 1;
                    708:                ismodified = 0;
                    709:        }
                    710:
1.149     joris     711:        if (ismodified == 1 && cf->fd != -1 && cf->file_rcs != NULL) {
1.170     joris     712:                b1 = rcs_rev_getbuf(cf->file_rcs, cf->file_rcsrev, 0);
1.149     joris     713:                if (b1 == NULL)
                    714:                        fatal("failed to get HEAD revision for comparison");
                    715:
1.159     joris     716:                b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT);
1.149     joris     717:                if (b2 == NULL)
                    718:                        fatal("failed to get file content for comparison");
                    719:
                    720:                if (cvs_buf_differ(b1, b2))
                    721:                        ismodified = 1;
                    722:                else
                    723:                        ismodified = 0;
1.188     ray       724:                cvs_buf_free(b1);
                    725:                cvs_buf_free(b2);
1.62      jfb       726:        }
                    727:
1.173     joris     728:        if (cf->file_rcs != NULL && cf->file_rcsrev != NULL) {
1.153     joris     729:                state = rcs_state_get(cf->file_rcs, cf->file_rcsrev);
1.138     joris     730:                if (state == NULL)
                    731:                        fatal("failed to get state for HEAD for %s",
                    732:                            cf->file_path);
1.139     joris     733:                if (!strcmp(state, RCS_STATE_DEAD))
1.138     joris     734:                        rcsdead = 1;
1.145     joris     735:
                    736:                cf->file_rcs->rf_dead = rcsdead;
1.128     joris     737:        }
                    738:
1.138     joris     739:        /*
                    740:         * 10 Sin
                    741:         * 20 Goto hell
                    742:         * (I welcome you if-else hell)
                    743:         */
                    744:        if (cf->file_ent == NULL) {
                    745:                if (cf->file_rcs == NULL) {
                    746:                        if (cf->fd == -1) {
1.178     joris     747:                                cvs_log(LP_NOTICE,
                    748:                                    "nothing known about '%s'",
                    749:                                    cf->file_path);
1.138     joris     750:                        }
1.130     joris     751:
1.138     joris     752:                        cf->file_status = FILE_UNKNOWN;
                    753:                } else if (rcsdead == 1) {
                    754:                        if (cf->fd == -1) {
                    755:                                cf->file_status = FILE_UPTODATE;
1.179     joris     756:                        } else if (cvs_cmdop != CVS_OP_ADD) {
1.138     joris     757:                                cf->file_status = FILE_UNKNOWN;
1.44      jfb       758:                        }
1.199     joris     759:                } else if (notag == 0) {
                    760:                        cf->file_status = FILE_CHECKOUT;
1.138     joris     761:                } else {
1.199     joris     762:                        cf->file_status = FILE_UPTODATE;
1.14      jfb       763:                }
1.138     joris     764:        } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
                    765:                if (cf->fd == -1) {
1.178     joris     766:                        if (cvs_cmdop != CVS_OP_REMOVE) {
1.138     joris     767:                                cvs_log(LP_NOTICE,
1.157     david     768:                                    "warning: new-born %s has disappeared",
1.138     joris     769:                                    cf->file_path);
1.178     joris     770:                        }
1.138     joris     771:                        cf->file_status = FILE_REMOVE_ENTRY;
                    772:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    773:                        cf->file_status = FILE_ADDED;
                    774:                } else {
1.178     joris     775:                        cvs_log(LP_NOTICE,
                    776:                            "conflict: %s already created by others",
                    777:                            cf->file_path);
1.138     joris     778:                        cf->file_status = FILE_CONFLICT;
                    779:                }
                    780:        } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
                    781:                if (cf->fd != -1) {
1.178     joris     782:                        cvs_log(LP_NOTICE,
                    783:                            "%s should be removed but is still there",
                    784:                            cf->file_path);
1.138     joris     785:                        cf->file_status = FILE_REMOVED;
                    786:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    787:                        cf->file_status = FILE_REMOVE_ENTRY;
1.66      joris     788:                } else {
1.138     joris     789:                        if (strcmp(r1, r2)) {
1.178     joris     790:                                cvs_log(LP_NOTICE,
                    791:                                    "conflict: removed %s was modified"
                    792:                                    " by a second party",
                    793:                                    cf->file_path);
1.138     joris     794:                                cf->file_status = FILE_CONFLICT;
                    795:                        } else {
                    796:                                cf->file_status = FILE_REMOVED;
                    797:                        }
1.66      joris     798:                }
1.138     joris     799:        } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
1.194     joris     800:                if (cf->file_rcs == NULL || rcsdead == 1 ||
1.199     joris     801:                    (reset_stickies == 1 && cf->in_attic == 1) ||
                    802:                    (notag == 1 && tag != NULL)) {
1.198     joris     803:                        if (cf->fd == -1 && server_has_file == 0) {
1.178     joris     804:                                cvs_log(LP_NOTICE,
                    805:                                    "warning: %s's entry exists but"
                    806:                                    " there is no longer a file"
                    807:                                    " in the repository,"
                    808:                                    " removing entry",
                    809:                                     cf->file_path);
1.138     joris     810:                                cf->file_status = FILE_REMOVE_ENTRY;
                    811:                        } else {
                    812:                                if (ismodified) {
1.178     joris     813:                                        cvs_log(LP_NOTICE,
                    814:                                            "conflict: %s is no longer "
                    815:                                            "in the repository but is "
                    816:                                            "locally modified",
                    817:                                            cf->file_path);
1.138     joris     818:                                        cf->file_status = FILE_CONFLICT;
                    819:                                } else {
1.178     joris     820:                                        cvs_log(LP_NOTICE,
                    821:                                            "%s is no longer in the "
                    822:                                            "repository",
                    823:                                            cf->file_path);
1.128     joris     824:
1.138     joris     825:                                        cf->file_status = FILE_UNLINK;
                    826:                                }
                    827:                        }
1.128     joris     828:                } else {
1.198     joris     829:                        if (cf->fd == -1 && server_has_file == 0) {
1.178     joris     830:                                if (cvs_cmdop != CVS_OP_REMOVE) {
1.138     joris     831:                                        cvs_log(LP_NOTICE,
                    832:                                            "warning: %s was lost",
                    833:                                            cf->file_path);
1.178     joris     834:                                }
1.138     joris     835:                                cf->file_status = FILE_LOST;
                    836:                        } else {
                    837:                                if (ismodified == 1)
                    838:                                        cf->file_status = FILE_MODIFIED;
                    839:                                else
                    840:                                        cf->file_status = FILE_UPTODATE;
                    841:
                    842:                                if (strcmp(r1, r2)) {
                    843:                                        if (cf->file_status == FILE_MODIFIED)
                    844:                                                cf->file_status = FILE_MERGE;
                    845:                                        else
                    846:                                                cf->file_status = FILE_PATCH;
                    847:                                }
                    848:                        }
1.128     joris     849:                }
                    850:        }
                    851:
1.138     joris     852:        cvs_ent_close(entlist, ENT_NOSYNC);
                    853: }
1.14      jfb       854:
1.138     joris     855: void
                    856: cvs_file_free(struct cvs_file *cf)
                    857: {
                    858:        xfree(cf->file_name);
                    859:        xfree(cf->file_wd);
                    860:        xfree(cf->file_path);
                    861:
1.167     joris     862:        if (cf->file_rcsrev != NULL)
                    863:                rcsnum_free(cf->file_rcsrev);
1.138     joris     864:        if (cf->file_rpath != NULL)
                    865:                xfree(cf->file_rpath);
                    866:        if (cf->file_ent != NULL)
                    867:                cvs_ent_free(cf->file_ent);
                    868:        if (cf->file_rcs != NULL)
                    869:                rcs_close(cf->file_rcs);
                    870:        if (cf->fd != -1)
                    871:                (void)close(cf->fd);
                    872:        if (cf->repo_fd != -1)
                    873:                (void)close(cf->repo_fd);
                    874:        xfree(cf);
1.23      jfb       875: }
                    876:
1.165     xsa       877: int
1.23      jfb       878: cvs_file_cmpname(const char *name1, const char *name2)
                    879: {
                    880:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    881:            (strcasecmp(name1, name2));
1.164     xsa       882: }
                    883:
                    884: int
                    885: cvs_file_cmp(const char *file1, const char *file2)
                    886: {
                    887:        struct stat stb1, stb2;
                    888:        int fd1, fd2, ret;
                    889:
                    890:        ret = 0;
                    891:
                    892:        if ((fd1 = open(file1, O_RDONLY|O_NOFOLLOW, 0)) == -1)
                    893:                fatal("cvs_file_cmp: open: `%s': %s", file1, strerror(errno));
                    894:        if ((fd2 = open(file2, O_RDONLY|O_NOFOLLOW, 0)) == -1)
                    895:                fatal("cvs_file_cmp: open: `%s': %s", file2, strerror(errno));
                    896:
                    897:        if (fstat(fd1, &stb1) == -1)
                    898:                fatal("cvs_file_cmp: `%s': %s", file1, strerror(errno));
                    899:        if (fstat(fd2, &stb2) == -1)
                    900:                fatal("cvs_file_cmp: `%s': %s", file2, strerror(errno));
                    901:
                    902:        if (stb1.st_size != stb2.st_size ||
                    903:            (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT)) {
                    904:                ret = 1;
                    905:                goto out;
                    906:        }
                    907:
                    908:        if (S_ISBLK(stb1.st_mode) || S_ISCHR(stb1.st_mode)) {
                    909:                if (stb1.st_rdev != stb2.st_rdev)
                    910:                        ret = 1;
                    911:                goto out;
                    912:        }
                    913:
                    914:        if (S_ISREG(stb1.st_mode)) {
                    915:                void *p1, *p2;
                    916:
1.187     otto      917:                if (stb1.st_size > (off_t)SIZE_MAX) {
1.164     xsa       918:                        ret = 1;
                    919:                        goto out;
                    920:                }
                    921:
                    922:                if ((p1 = mmap(NULL, stb1.st_size, PROT_READ,
                    923:                    MAP_FILE, fd1, (off_t)0)) == MAP_FAILED)
                    924:                        fatal("cvs_file_cmp: mmap failed");
                    925:
                    926:                if ((p2 = mmap(NULL, stb1.st_size, PROT_READ,
                    927:                    MAP_FILE, fd2, (off_t)0)) == MAP_FAILED)
                    928:                        fatal("cvs_file_cmp: mmap failed");
                    929:
                    930:                madvise(p1, stb1.st_size, MADV_SEQUENTIAL);
                    931:                madvise(p2, stb1.st_size, MADV_SEQUENTIAL);
                    932:
                    933:                ret = memcmp(p1, p2, stb1.st_size);
                    934:
                    935:                (void)munmap(p1, stb1.st_size);
                    936:                (void)munmap(p2, stb1.st_size);
                    937:        }
                    938:
                    939: out:
                    940:        (void)close(fd1);
                    941:        (void)close(fd2);
1.166     xsa       942:
                    943:        return (ret);
                    944: }
                    945:
                    946: int
                    947: cvs_file_copy(const char *from, const char *to)
                    948: {
                    949:        struct stat st;
                    950:        struct timeval tv[2];
                    951:        time_t atime, mtime;
                    952:        int src, dst, ret;
                    953:
                    954:        ret = 0;
                    955:
                    956:        cvs_log(LP_TRACE, "cvs_file_copy(%s,%s)", from, to);
                    957:
                    958:        if (cvs_noexec == 1)
                    959:                return (0);
                    960:
                    961:        if ((src = open(from, O_RDONLY, 0)) == -1)
                    962:                fatal("cvs_file_copy: open: `%s': %s", from, strerror(errno));
                    963:
                    964:        if (fstat(src, &st) == -1)
                    965:                fatal("cvs_file_copy: `%s': %s", from, strerror(errno));
                    966:
                    967:        atime = st.st_atimespec.tv_sec;
                    968:        mtime = st.st_mtimespec.tv_sec;
                    969:
                    970:        if (S_ISREG(st.st_mode)) {
1.197     tobias    971:                char *p;
1.166     xsa       972:                int saved_errno;
                    973:
1.187     otto      974:                if (st.st_size > (off_t)SIZE_MAX) {
1.166     xsa       975:                        ret = -1;
                    976:                        goto out;
                    977:                }
                    978:
                    979:                if ((dst = open(to, O_CREAT|O_TRUNC|O_WRONLY,
                    980:                    st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))) == -1)
                    981:                        fatal("cvs_file_copy: open `%s': %s",
                    982:                            to, strerror(errno));
                    983:
                    984:                if ((p = mmap(NULL, st.st_size, PROT_READ,
                    985:                    MAP_FILE, src, (off_t)0)) == MAP_FAILED) {
                    986:                        saved_errno = errno;
                    987:                        (void)unlink(to);
                    988:                        fatal("cvs_file_copy: mmap: %s", strerror(saved_errno));
                    989:                }
                    990:
                    991:                madvise(p, st.st_size, MADV_SEQUENTIAL);
                    992:
1.197     tobias    993:                if (atomicio(vwrite, dst, p, st.st_size) != st.st_size) {
                    994:                        saved_errno = errno;
                    995:                        (void)unlink(to);
                    996:                        fatal("cvs_file_copy: `%s': %s", from,
                    997:                            strerror(saved_errno));
1.166     xsa       998:                }
                    999:
                   1000:                (void)munmap(p, st.st_size);
                   1001:
                   1002:                tv[0].tv_sec = atime;
                   1003:                tv[1].tv_sec = mtime;
                   1004:
                   1005:                if (futimes(dst, tv) == -1) {
                   1006:                        saved_errno = errno;
                   1007:                        (void)unlink(to);
                   1008:                        fatal("cvs_file_copy: futimes: %s",
                   1009:                            strerror(saved_errno));
                   1010:                }
                   1011:                (void)close(dst);
                   1012:        }
                   1013: out:
                   1014:        (void)close(src);
1.164     xsa      1015:
                   1016:        return (ret);
1.14      jfb      1017: }