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

1.140   ! joris       1: /*     $OpenBSD: file.c,v 1.139 2006/05/27 06:15:50 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.134     xsa        28: #include "includes.h"
1.1       jfb        29:
                     30: #include "cvs.h"
1.83      xsa        31: #include "file.h"
1.1       jfb        32: #include "log.h"
                     33:
1.104     xsa        34: #define CVS_IGN_STATIC 0x01    /* pattern is static, no need to glob */
1.1       jfb        35:
1.104     xsa        36: #define CVS_CHAR_ISMETA(c)     ((c == '*') || (c == '?') || (c == '['))
1.1       jfb        37:
                     38: /*
                     39:  * Standard patterns to ignore.
                     40:  */
                     41: static const char *cvs_ign_std[] = {
                     42:        ".",
                     43:        "..",
                     44:        "*.o",
1.45      xsa        45:        "*.a",
1.1       jfb        46:        "*.bak",
                     47:        "*.orig",
                     48:        "*.rej",
1.45      xsa        49:        "*.old",
1.1       jfb        50:        "*.exe",
                     51:        "*.depend",
1.45      xsa        52:        "*.obj",
                     53:        "*.elc",
                     54:        "*.ln",
                     55:        "*.olb",
1.1       jfb        56:        "CVS",
                     57:        "core",
1.102     joris      58:        "cvslog*",
1.49      jfb        59:        "*.core",
1.8       jfb        60:        ".#*",
1.45      xsa        61:        "*~",
                     62:        "_$*",
                     63:        "*$",
1.1       jfb        64: };
                     65:
1.138     joris      66: struct ignore_head cvs_ign_pats;
                     67: struct ignore_head dir_ign_pats;
1.1       jfb        68:
1.138     joris      69: static int     cvs_file_cmpname(const char *, const char *);
1.1       jfb        70:
1.138     joris      71: void
1.1       jfb        72: cvs_file_init(void)
                     73: {
1.61      xsa        74:        int i, l;
1.138     joris      75:        FILE *ifp;
1.1       jfb        76:        size_t len;
1.138     joris      77:        char *path, *buf;
                     78:
                     79:        path = xmalloc(MAXPATHLEN);
                     80:        buf = xmalloc(MAXNAMLEN);
1.1       jfb        81:
                     82:        TAILQ_INIT(&cvs_ign_pats);
1.138     joris      83:        TAILQ_INIT(&dir_ign_pats);
1.4       jfb        84:
1.1       jfb        85:        /* standard patterns to ignore */
1.10      jfb        86:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.138     joris      87:                cvs_file_ignore(cvs_ign_std[i], &cvs_ign_pats);
1.1       jfb        88:
                     89:        /* read the cvsignore file in the user's home directory, if any */
1.138     joris      90:        l = snprintf(path, MAXPATHLEN, "%s/.cvsignore", cvs_homedir);
                     91:        if (l == -1 || l >= MAXPATHLEN)
                     92:                fatal("overflow in cvs_file_init");
1.61      xsa        93:
1.111     xsa        94:        ifp = fopen(path, "r");
                     95:        if (ifp == NULL) {
                     96:                if (errno != ENOENT)
                     97:                        cvs_log(LP_ERRNO,
                     98:                            "failed to open user's cvsignore file `%s'", path);
                     99:        } else {
1.138     joris     100:                while (fgets(buf, MAXNAMLEN, ifp) != NULL) {
1.111     xsa       101:                        len = strlen(buf);
                    102:                        if (len == 0)
                    103:                                continue;
1.138     joris     104:
                    105:                        if (buf[len - 1] != '\n')
1.111     xsa       106:                                cvs_log(LP_ERR, "line too long in `%s'", path);
1.138     joris     107:
1.111     xsa       108:                        buf[--len] = '\0';
1.138     joris     109:                        cvs_file_ignore(buf, &cvs_ign_pats);
1.1       jfb       110:                }
1.138     joris     111:
1.111     xsa       112:                (void)fclose(ifp);
1.1       jfb       113:        }
                    114:
1.138     joris     115:        xfree(path);
                    116:        xfree(buf);
1.1       jfb       117: }
                    118:
1.138     joris     119: void
                    120: cvs_file_ignore(const char *pat, struct ignore_head *list)
1.1       jfb       121: {
                    122:        char *cp;
1.138     joris     123:        size_t len;
1.1       jfb       124:        struct cvs_ignpat *ip;
                    125:
1.136     ray       126:        ip = xmalloc(sizeof(*ip));
1.138     joris     127:        len = strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    128:        if (len >= sizeof(ip->ip_pat))
                    129:                fatal("cvs_file_ignore: truncation of pattern '%s'", pat);
1.1       jfb       130:
                    131:        /* check if we will need globbing for that pattern */
                    132:        ip->ip_flags = CVS_IGN_STATIC;
                    133:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    134:                if (CVS_CHAR_ISMETA(*cp)) {
                    135:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    136:                        break;
                    137:                }
                    138:        }
                    139:
1.138     joris     140:        TAILQ_INSERT_TAIL(list, ip, ip_list);
1.1       jfb       141: }
                    142:
                    143: int
1.5       jfb       144: cvs_file_chkign(const char *file)
1.1       jfb       145: {
1.23      jfb       146:        int flags;
1.1       jfb       147:        struct cvs_ignpat *ip;
                    148:
1.23      jfb       149:        flags = FNM_PERIOD;
                    150:        if (cvs_nocase)
                    151:                flags |= FNM_CASEFOLD;
                    152:
1.1       jfb       153:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    154:                if (ip->ip_flags & CVS_IGN_STATIC) {
1.23      jfb       155:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1       jfb       156:                                return (1);
1.38      deraadt   157:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1       jfb       158:                        return (1);
                    159:        }
                    160:
1.138     joris     161:        TAILQ_FOREACH(ip, &dir_ign_pats, ip_list) {
                    162:                if (ip->ip_flags & CVS_IGN_STATIC) {
                    163:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
                    164:                                return (1);
                    165:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
                    166:                        return (1);
                    167:        }
                    168:
1.1       jfb       169:        return (0);
                    170: }
                    171:
1.138     joris     172: void
                    173: cvs_file_run(int argc, char **argv, struct cvs_recursion *cr)
1.1       jfb       174: {
1.138     joris     175:        int i;
                    176:        struct cvs_flisthead fl;
1.84      joris     177:
1.138     joris     178:        TAILQ_INIT(&fl);
1.85      joris     179:
1.138     joris     180:        for (i = 0; i < argc; i++)
                    181:                cvs_file_get(argv[i], &fl);
1.1       jfb       182:
1.138     joris     183:        cvs_file_walklist(&fl, cr);
                    184:        cvs_file_freelist(&fl);
1.3       jfb       185: }
                    186:
1.138     joris     187: struct cvs_filelist *
                    188: cvs_file_get(const char *name, struct cvs_flisthead *fl)
                    189: {
                    190:        const char *p;
                    191:        struct cvs_filelist *l;
1.3       jfb       192:
1.138     joris     193:        for (p = name; p[0] == '.' && p[1] == '/';)
                    194:                p += 2;
1.35      jfb       195:
1.138     joris     196:        TAILQ_FOREACH(l, fl, flist)
                    197:                if (!strcmp(l->file_path, p))
                    198:                        return (l);
1.35      jfb       199:
1.138     joris     200:        l = (struct cvs_filelist *)xmalloc(sizeof(*l));
                    201:        l->file_path = xstrdup(p);
1.35      jfb       202:
1.138     joris     203:        TAILQ_INSERT_TAIL(fl, l, flist);
                    204:        return (l);
1.35      jfb       205: }
                    206:
1.138     joris     207: struct cvs_file *
                    208: cvs_file_get_cf(const char *d, const char *f, int fd, int type)
                    209: {
                    210:        int l;
                    211:        struct cvs_file *cf;
                    212:        char *p, *rpath;
1.35      jfb       213:
1.138     joris     214:        rpath = xmalloc(MAXPATHLEN);
1.3       jfb       215:
1.138     joris     216:        l = snprintf(rpath, MAXPATHLEN, "%s/%s", d, f);
                    217:        if (l == -1 || l >= MAXPATHLEN)
                    218:                fatal("cvs_file_get_cf: overflow");
                    219:
                    220:        for (p = rpath; p[0] == '.' && p[1] == '/';)
                    221:                p += 2;
                    222:
                    223:        cf = (struct cvs_file *)xmalloc(sizeof(*cf));
                    224:        memset(cf, 0, sizeof(*cf));
                    225:
                    226:        cf->file_name = xstrdup(f);
                    227:        cf->file_wd = xstrdup(d);
                    228:        cf->file_path = xstrdup(p);
                    229:        cf->fd = fd;
                    230:        cf->repo_fd = -1;
                    231:        cf->file_type = type;
                    232:        cf->file_status = cf->file_flags = 0;
                    233:        cf->file_ent = NULL;
1.68      joris     234:
1.138     joris     235:        xfree(rpath);
                    236:        return (cf);
1.9       jfb       237: }
                    238:
1.138     joris     239: void
                    240: cvs_file_walklist(struct cvs_flisthead *fl, struct cvs_recursion *cr)
1.9       jfb       241: {
1.138     joris     242:        int len, fd, type;
                    243:        struct stat st;
                    244:        struct cvs_file *cf;
                    245:        struct cvs_filelist *l, *nxt;
                    246:        char *d, *f, *repo, *fpath;
                    247:
                    248:        fpath = xmalloc(MAXPATHLEN);
                    249:        repo = xmalloc(MAXPATHLEN);
                    250:
                    251:        for (l = TAILQ_FIRST(fl); l != NULL; l = nxt) {
                    252:                if (cvs_quit)
                    253:                        fatal("received signal %d", sig_received);
                    254:
                    255:                cvs_log(LP_TRACE, "cvs_file_walklist: element '%s'",
                    256:                    l->file_path);
                    257:
                    258:                if ((f = basename(l->file_path)) == NULL)
                    259:                        fatal("cvs_file_walklist: basename failed");
                    260:                if ((d = dirname(l->file_path)) == NULL)
                    261:                        fatal("cvs_file_walklist: dirname failed");
                    262:
                    263:                if ((fd = open(l->file_path, O_RDONLY)) != -1) {
                    264:                        if (fstat(fd, &st) == -1) {
                    265:                                cvs_log(LP_ERRNO, "%s", l->file_path);
                    266:                                (void)close(fd);
                    267:                                goto next;
                    268:                        }
1.26      jfb       269:
1.138     joris     270:                        if (S_ISDIR(st.st_mode))
                    271:                                type = CVS_DIR;
                    272:                        else if (S_ISREG(st.st_mode))
                    273:                                type = CVS_FILE;
                    274:                        else {
                    275:                                cvs_log(LP_ERR,
                    276:                                    "ignoring bad file type for %s",
                    277:                                    l->file_path);
                    278:                                (void)close(fd);
                    279:                                goto next;
                    280:                        }
                    281:                } else {
                    282:                        if (stat(d, &st) == -1) {
                    283:                                cvs_log(LP_ERRNO, "%s", d);
                    284:                                goto next;
                    285:                        }
1.87      joris     286:
1.138     joris     287:                        cvs_get_repo(d, repo, MAXPATHLEN);
                    288:                        len = snprintf(fpath, MAXPATHLEN, "%s/%s",
                    289:                            repo, f);
                    290:                        if (len == -1 || len >= MAXPATHLEN)
                    291:                                fatal("cvs_file_walklist: overflow");
                    292:
                    293:                        if ((fd = open(fpath, O_RDONLY)) == -1) {
                    294:                                strlcat(fpath, RCS_FILE_EXT, MAXPATHLEN);
                    295:                                fd = open(fpath, O_RDONLY);
                    296:                        }
1.26      jfb       297:
1.138     joris     298:                        if (fd != -1) {
                    299:                                if (fstat(fd, &st) == -1)
                    300:                                        fatal("cvs_file_walklist: %s: %s",
                    301:                                             fpath, strerror(errno));
                    302:
                    303:                                if (S_ISDIR(st.st_mode))
                    304:                                        type = CVS_DIR;
                    305:                                else if (S_ISREG(st.st_mode))
                    306:                                        type = CVS_FILE;
                    307:                                else {
                    308:                                        cvs_log(LP_ERR,
                    309:                                            "ignoring bad file type for %s",
                    310:                                            l->file_path);
                    311:                                        (void)close(fd);
                    312:                                        goto next;
                    313:                                }
                    314:
                    315:                                /* this file is not in our working copy yet */
                    316:                                (void)close(fd);
                    317:                                fd = -1;
                    318:                        }
                    319:                }
1.73      joris     320:
1.138     joris     321:                cf = cvs_file_get_cf(d, f, fd, type);
                    322:                if (cf->file_type == CVS_DIR) {
                    323:                        cvs_file_walkdir(cf, cr);
                    324:                } else {
                    325:                        if (cr->local != NULL)
                    326:                                cr->local(cf);
1.128     joris     327:                }
1.100     joris     328:
1.138     joris     329:                cvs_file_free(cf);
1.123     joris     330:
1.138     joris     331: next:
                    332:                nxt = TAILQ_NEXT(l, flist);
                    333:                TAILQ_REMOVE(fl, l, flist);
1.68      joris     334:
1.138     joris     335:                xfree(l->file_path);
                    336:                xfree(l);
1.100     joris     337:        }
                    338:
1.138     joris     339:        xfree(fpath);
                    340:        xfree(repo);
1.100     joris     341: }
                    342:
1.138     joris     343: void
                    344: cvs_file_walkdir(struct cvs_file *cf, struct cvs_recursion *cr)
1.100     joris     345: {
1.138     joris     346:        int l;
                    347:        FILE *fp;
                    348:        int nbytes;
                    349:        size_t len;
                    350:        long base;
                    351:        size_t bufsize;
                    352:        struct stat st;
                    353:        struct dirent *dp;
1.100     joris     354:        struct cvs_ent *ent;
1.138     joris     355:        struct cvs_ignpat *ip;
                    356:        struct cvs_ent_line *line;
                    357:        struct cvs_flisthead fl, dl;
                    358:        CVSENTRIES *entlist;
                    359:        char *buf, *ebuf, *cp, *repo, *fpath;
1.100     joris     360:
1.138     joris     361:        cvs_log(LP_TRACE, "cvs_file_walkdir(%s)", cf->file_path);
1.100     joris     362:
1.138     joris     363:        if (cr->enterdir != NULL)
                    364:                cr->enterdir(cf);
1.100     joris     365:
1.138     joris     366:        if (cr->local != NULL)
                    367:                cr->local(cf);
1.100     joris     368:
1.138     joris     369:        fpath = xmalloc(MAXPATHLEN);
1.100     joris     370:
                    371:        /*
1.138     joris     372:         * If we do not have a admin directory inside here, dont bother.
1.100     joris     373:         */
1.138     joris     374:        l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
                    375:            CVS_PATH_CVSDIR);
                    376:        if (l == -1 || l >= MAXPATHLEN)
                    377:                fatal("cvs_file_walkdir: overflow");
1.100     joris     378:
1.138     joris     379:        if (stat(fpath, &st) == -1) {
                    380:                xfree(fpath);
                    381:                return;
1.100     joris     382:        }
                    383:
                    384:        /*
1.138     joris     385:         * check for a local .cvsignore file
1.100     joris     386:         */
1.138     joris     387:        l = snprintf(fpath, MAXPATHLEN, "%s/.cvsignore", cf->file_path);
                    388:        if (l == -1 || l >= MAXPATHLEN)
                    389:                fatal("cvs_file_walkdir: overflow");
1.100     joris     390:
1.138     joris     391:        if ((fp = fopen(fpath, "r")) != NULL) {
                    392:                while (fgets(fpath, MAXPATHLEN, fp)) {
                    393:                        len = strlen(fpath);
                    394:                        if (fpath[len - 1] == '\n')
                    395:                                fpath[len - 1] = '\0';
1.26      jfb       396:
1.138     joris     397:                        cvs_file_ignore(fpath, &dir_ign_pats);
1.68      joris     398:                }
1.26      jfb       399:
1.138     joris     400:                (void)fclose(fp);
1.100     joris     401:        }
                    402:
1.138     joris     403:        if (fstat(cf->fd, &st) == -1)
                    404:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    405:                    strerror(errno));
1.100     joris     406:
1.138     joris     407:        bufsize = st.st_size;
                    408:        if (bufsize < st.st_blksize)
                    409:                bufsize = st.st_blksize;
1.116     joris     410:
1.138     joris     411:        buf = xmalloc(bufsize);
                    412:        TAILQ_INIT(&fl);
                    413:        TAILQ_INIT(&dl);
1.87      joris     414:
1.138     joris     415:        while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
                    416:                ebuf = buf + nbytes;
                    417:                cp = buf;
1.26      jfb       418:
1.138     joris     419:                while (cp < ebuf) {
                    420:                        dp = (struct dirent *)cp;
                    421:                        if (!strcmp(dp->d_name, ".") ||
                    422:                            !strcmp(dp->d_name, "..") ||
                    423:                            !strcmp(dp->d_name, CVS_PATH_CVSDIR) ||
                    424:                            dp->d_reclen == 0) {
                    425:                                cp += dp->d_reclen;
                    426:                                continue;
                    427:                        }
1.13      jfb       428:
1.138     joris     429:                        if (cvs_file_chkign(dp->d_name)) {
                    430:                                cp += dp->d_reclen;
1.13      jfb       431:                                continue;
1.138     joris     432:                        }
1.13      jfb       433:
1.140   ! joris     434:                        if (!(cr->flags & CR_RECURSE_DIRS) &&
        !           435:                            dp->d_type == DT_DIR) {
        !           436:                                printf("Skipping %s\n", dp->d_name);
        !           437:                                cp += dp->d_reclen;
        !           438:                                continue;
        !           439:                        }
        !           440:
1.138     joris     441:                        l = snprintf(fpath, MAXPATHLEN, "%s/%s",
                    442:                            cf->file_path, dp->d_name);
                    443:                        if (l == -1 || l >= MAXPATHLEN)
                    444:                                fatal("cvs_file_walkdir: overflow");
1.13      jfb       445:
1.138     joris     446:                        /*
                    447:                         * Anticipate the file type to sort them,
                    448:                         * note that we do not determine the final
                    449:                         * type until we actually have the fd floating
                    450:                         * around.
                    451:                         */
                    452:                        if (dp->d_type == DT_DIR)
                    453:                                cvs_file_get(fpath, &dl);
                    454:                        else if (dp->d_type == DT_REG)
                    455:                                cvs_file_get(fpath, &fl);
1.13      jfb       456:
1.138     joris     457:                        cp += dp->d_reclen;
                    458:                }
1.34      jfb       459:        }
                    460:
1.138     joris     461:        if (nbytes == -1)
                    462:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    463:                    strerror(errno));
1.22      jfb       464:
1.138     joris     465:        xfree(buf);
1.22      jfb       466:
1.138     joris     467:        while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
                    468:                TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
                    469:                xfree(ip);
1.91      joris     470:        }
1.114     xsa       471:
1.138     joris     472:        entlist = cvs_ent_open(cf->file_path);
                    473:        TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
                    474:                ent = cvs_ent_parse(line->buf);
1.61      xsa       475:
1.138     joris     476:                l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
                    477:                    ent->ce_name);
                    478:                if (l == -1 || l >= MAXPATHLEN)
                    479:                        fatal("cvs_file_walkdir: overflow");
1.100     joris     480:
1.140   ! joris     481:                if (!(cr->flags & CR_RECURSE_DIRS) &&
        !           482:                    ent->ce_type == CVS_ENT_DIR)
        !           483:                        continue;
        !           484:
1.138     joris     485:                if (ent->ce_type == CVS_ENT_DIR)
                    486:                        cvs_file_get(fpath, &dl);
                    487:                else if (ent->ce_type == CVS_ENT_FILE)
                    488:                        cvs_file_get(fpath, &fl);
1.68      joris     489:
1.138     joris     490:                cvs_ent_free(ent);
1.128     joris     491:        }
                    492:
1.138     joris     493:        cvs_ent_close(entlist, ENT_NOSYNC);
1.91      joris     494:
1.140   ! joris     495:        if (cr->flags & CR_REPO) {
        !           496:                repo = xmalloc(MAXPATHLEN);
        !           497:                cvs_get_repo(cf->file_path, repo, MAXPATHLEN);
        !           498:                cvs_repository_lock(repo);
1.68      joris     499:
1.140   ! joris     500:                cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
        !           501:                    (cr->flags & CR_RECURSE_DIRS));
        !           502:        }
1.68      joris     503:
1.138     joris     504:        cvs_file_walklist(&fl, cr);
                    505:        cvs_file_freelist(&fl);
1.68      joris     506:
1.140   ! joris     507:        if (cr->flags & CR_REPO) {
        !           508:                cvs_repository_unlock(repo);
        !           509:                xfree(repo);
        !           510:        }
1.68      joris     511:
1.138     joris     512:        cvs_file_walklist(&dl, cr);
                    513:        cvs_file_freelist(&dl);
1.34      jfb       514:
1.138     joris     515:        xfree(fpath);
1.26      jfb       516:
1.138     joris     517:        if (cr->leavedir != NULL)
                    518:                cr->leavedir(cf);
1.3       jfb       519: }
                    520:
                    521: void
1.138     joris     522: cvs_file_freelist(struct cvs_flisthead *fl)
1.3       jfb       523: {
1.138     joris     524:        struct cvs_filelist *f;
1.62      jfb       525:
1.138     joris     526:        while ((f = TAILQ_FIRST(fl)) != NULL) {
                    527:                TAILQ_REMOVE(fl, f, flist);
                    528:                xfree(f->file_path);
                    529:                xfree(f);
1.62      jfb       530:        }
1.5       jfb       531: }
                    532:
1.138     joris     533: void
                    534: cvs_file_classify(struct cvs_file *cf)
1.3       jfb       535: {
1.138     joris     536:        size_t len;
                    537:        time_t mtime;
                    538:        struct stat st;
                    539:        int rflags, l, ismodified, rcsdead;
                    540:        CVSENTRIES *entlist = NULL;
                    541:        const char *state;
                    542:        char *repo, *rcsfile, r1[16], r2[16];
                    543:
                    544:        cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
                    545:
                    546:        if (!strcmp(cf->file_path, ".")) {
                    547:                cf->file_status = FILE_UPTODATE;
                    548:                return;
                    549:        }
                    550:
                    551:        entlist = cvs_ent_open(cf->file_wd);
                    552:
                    553:        repo = xmalloc(MAXPATHLEN);
                    554:        rcsfile = xmalloc(MAXPATHLEN);
                    555:
                    556:        cvs_get_repo(cf->file_wd, repo, MAXPATHLEN);
                    557:        l = snprintf(rcsfile, MAXPATHLEN, "%s/%s",
                    558:            repo, cf->file_name);
                    559:        if (l == -1 || l >= MAXPATHLEN)
                    560:                fatal("cvs_file_classify: overflow");
                    561:
                    562:        if (cf->file_type == CVS_FILE) {
                    563:                len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
                    564:                if (len >= MAXPATHLEN)
                    565:                        fatal("cvs_file_classify: truncation");
                    566:        }
                    567:
                    568:        cf->file_rpath = xstrdup(rcsfile);
                    569:        cf->file_ent = cvs_ent_get(entlist, cf->file_name);
                    570:
                    571:        if (cf->file_type == CVS_DIR) {
                    572:                if (cf->fd == -1 && stat(rcsfile, &st) != -1)
                    573:                        cf->file_status = DIR_CREATE;
                    574:                else if (cf->file_ent != NULL)
                    575:                        cf->file_status = FILE_UPTODATE;
                    576:                xfree(repo);
                    577:                xfree(rcsfile);
                    578:                cvs_ent_close(entlist, ENT_NOSYNC);
                    579:                return;
                    580:        }
                    581:
                    582:        rflags = 0;
                    583:        cf->repo_fd = open(cf->file_rpath, O_RDONLY);
                    584:        if (cf->repo_fd != -1) {
                    585:                cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
                    586:                if (cf->file_rcs == NULL)
                    587:                        fatal("cvs_file_classify: rcs_open failed while it "
                    588:                            "shouldn't");
                    589:        } else {
                    590:                cf->file_rcs = NULL;
1.3       jfb       591:        }
                    592:
1.138     joris     593:        if (cf->file_ent != NULL)
                    594:                rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
                    595:        if (cf->file_rcs != NULL)
                    596:                rcsnum_tostr(cf->file_rcs->rf_head, r2, sizeof(r2));
1.6       jfb       597:
1.138     joris     598:        ismodified = rcsdead = 0;
                    599:        if (cf->fd != -1 && cf->file_ent != NULL) {
                    600:                if (fstat(cf->fd, &st) == -1)
                    601:                        fatal("cvs_file_classify: %s", strerror(errno));
1.6       jfb       602:
1.138     joris     603:                mtime = cvs_hack_time(st.st_mtime, 1);
                    604:                if (mtime == 0)
                    605:                        fatal("to gmt failed");
1.62      jfb       606:
1.138     joris     607:                if (mtime != cf->file_ent->ce_mtime)
                    608:                        ismodified = 1;
1.62      jfb       609:        }
                    610:
1.138     joris     611:        if (cf->file_rcs != NULL) {
                    612:                state = rcs_state_get(cf->file_rcs, cf->file_rcs->rf_head);
                    613:                if (state == NULL)
                    614:                        fatal("failed to get state for HEAD for %s",
                    615:                            cf->file_path);
1.139     joris     616:                if (!strcmp(state, RCS_STATE_DEAD))
1.138     joris     617:                        rcsdead = 1;
1.128     joris     618:        }
                    619:
1.138     joris     620:        /*
                    621:         * 10 Sin
                    622:         * 20 Goto hell
                    623:         * (I welcome you if-else hell)
                    624:         */
                    625:        if (cf->file_ent == NULL) {
                    626:                if (cf->file_rcs == NULL) {
                    627:                        if (cf->fd == -1) {
                    628:                                if (verbosity > 1)
                    629:                                        cvs_log(LP_NOTICE,
                    630:                                            "nothing known about '%s'",
                    631:                                            cf->file_path);
                    632:                        } else {
                    633:                                if (verbosity > 1)
                    634:                                        cvs_log(LP_NOTICE,
                    635:                                            "use add to create an entry for %s",
                    636:                                            cf->file_path);
                    637:                        }
1.130     joris     638:
1.138     joris     639:                        cf->file_status = FILE_UNKNOWN;
                    640:                } else if (rcsdead == 1) {
                    641:                        if (cf->fd == -1) {
                    642:                                cf->file_status = FILE_UPTODATE;
                    643:                        } else {
                    644:                                cvs_log(LP_NOTICE,
                    645:                                    "use add to create an entry for %s",
                    646:                                    cf->file_path);
                    647:                                cf->file_status = FILE_UNKNOWN;
1.44      jfb       648:                        }
1.138     joris     649:                } else {
                    650:                        cf->file_status = FILE_CHECKOUT;
1.14      jfb       651:                }
1.138     joris     652:        } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
                    653:                if (cf->fd == -1) {
                    654:                        if (verbosity > 1)
                    655:                                cvs_log(LP_NOTICE,
                    656:                                    "warning: new-born %s has dissapeared",
                    657:                                    cf->file_path);
                    658:                        cf->file_status = FILE_REMOVE_ENTRY;
                    659:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    660:                        cf->file_status = FILE_ADDED;
                    661:                } else {
                    662:                        if (verbosity > 1)
                    663:                                cvs_log(LP_NOTICE,
                    664:                                    "conflict: %s already created by others",
                    665:                                    cf->file_path);
                    666:                        cf->file_status = FILE_CONFLICT;
                    667:                }
                    668:        } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
                    669:                if (cf->fd != -1) {
                    670:                        if (verbosity > 1)
                    671:                                cvs_log(LP_NOTICE,
                    672:                                    "%s should be removed but is still there",
                    673:                                    cf->file_path);
                    674:                        cf->file_status = FILE_REMOVED;
                    675:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    676:                        cf->file_status = FILE_REMOVE_ENTRY;
1.66      joris     677:                } else {
1.138     joris     678:                        if (strcmp(r1, r2)) {
                    679:                                if (verbosity > 1)
                    680:                                        cvs_log(LP_NOTICE,
                    681:                                            "conflict: removed %s was modified"
                    682:                                            " by a second party",
                    683:                                            cf->file_path);
                    684:                                cf->file_status = FILE_CONFLICT;
                    685:                        } else {
                    686:                                cf->file_status = FILE_REMOVED;
                    687:                        }
1.66      joris     688:                }
1.138     joris     689:        } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
                    690:                if (cf->file_rcs == NULL || rcsdead == 1) {
                    691:                        if (cf->fd == -1) {
                    692:                                if (verbosity > 1)
                    693:                                        cvs_log(LP_NOTICE,
                    694:                                            "warning: %s's entry exists but"
                    695:                                            " there is no longer a file"
                    696:                                            " in the repository,"
                    697:                                            " removing entry",
                    698:                                             cf->file_path);
                    699:                                cf->file_status = FILE_REMOVE_ENTRY;
                    700:                        } else {
                    701:                                if (ismodified) {
                    702:                                        if (verbosity > 1)
                    703:                                                cvs_log(LP_NOTICE,
                    704:                                                    "conflict: %s is no longer "
                    705:                                                    "in the repository but is "
                    706:                                                    "locally modified",
                    707:                                                    cf->file_path);
                    708:                                        cf->file_status = FILE_CONFLICT;
                    709:                                } else {
                    710:                                        if (verbosity > 1)
                    711:                                                cvs_log(LP_NOTICE,
                    712:                                                    "%s is no longer in the "
                    713:                                                    "repository",
                    714:                                                    cf->file_path);
1.128     joris     715:
1.138     joris     716:                                        cf->file_status = FILE_UNLINK;
                    717:                                }
                    718:                        }
1.128     joris     719:                } else {
1.138     joris     720:                        if (cf->fd == -1) {
                    721:                                if (verbosity > 1)
                    722:                                        cvs_log(LP_NOTICE,
                    723:                                            "warning: %s was lost",
                    724:                                            cf->file_path);
                    725:                                cf->file_status = FILE_LOST;
                    726:                        } else {
                    727:                                if (ismodified == 1)
                    728:                                        cf->file_status = FILE_MODIFIED;
                    729:                                else
                    730:                                        cf->file_status = FILE_UPTODATE;
                    731:
                    732:                                if (strcmp(r1, r2)) {
                    733:                                        if (cf->file_status == FILE_MODIFIED)
                    734:                                                cf->file_status = FILE_MERGE;
                    735:                                        else
                    736:                                                cf->file_status = FILE_PATCH;
                    737:                                }
                    738:                        }
1.128     joris     739:                }
                    740:        }
                    741:
1.138     joris     742:        xfree(repo);
                    743:        xfree(rcsfile);
                    744:        cvs_ent_close(entlist, ENT_NOSYNC);
                    745: }
1.14      jfb       746:
1.138     joris     747: void
                    748: cvs_file_free(struct cvs_file *cf)
                    749: {
                    750:        xfree(cf->file_name);
                    751:        xfree(cf->file_wd);
                    752:        xfree(cf->file_path);
                    753:
                    754:        if (cf->file_rpath != NULL)
                    755:                xfree(cf->file_rpath);
                    756:        if (cf->file_ent != NULL)
                    757:                cvs_ent_free(cf->file_ent);
                    758:        if (cf->file_rcs != NULL)
                    759:                rcs_close(cf->file_rcs);
                    760:        if (cf->fd != -1)
                    761:                (void)close(cf->fd);
                    762:        if (cf->repo_fd != -1)
                    763:                (void)close(cf->repo_fd);
                    764:        xfree(cf);
1.23      jfb       765: }
                    766:
                    767: static int
                    768: cvs_file_cmpname(const char *name1, const char *name2)
                    769: {
                    770:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    771:            (strcasecmp(name1, name2));
1.14      jfb       772: }