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

1.141   ! joris       1: /*     $OpenBSD: file.c,v 1.140 2006/05/27 15:14:27 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:                                cp += dp->d_reclen;
                    437:                                continue;
                    438:                        }
                    439:
1.138     joris     440:                        l = snprintf(fpath, MAXPATHLEN, "%s/%s",
                    441:                            cf->file_path, dp->d_name);
                    442:                        if (l == -1 || l >= MAXPATHLEN)
                    443:                                fatal("cvs_file_walkdir: overflow");
1.13      jfb       444:
1.138     joris     445:                        /*
                    446:                         * Anticipate the file type to sort them,
                    447:                         * note that we do not determine the final
                    448:                         * type until we actually have the fd floating
                    449:                         * around.
                    450:                         */
                    451:                        if (dp->d_type == DT_DIR)
                    452:                                cvs_file_get(fpath, &dl);
                    453:                        else if (dp->d_type == DT_REG)
                    454:                                cvs_file_get(fpath, &fl);
1.13      jfb       455:
1.138     joris     456:                        cp += dp->d_reclen;
                    457:                }
1.34      jfb       458:        }
                    459:
1.138     joris     460:        if (nbytes == -1)
                    461:                fatal("cvs_file_walkdir: %s %s", cf->file_path,
                    462:                    strerror(errno));
1.22      jfb       463:
1.138     joris     464:        xfree(buf);
1.22      jfb       465:
1.138     joris     466:        while ((ip = TAILQ_FIRST(&dir_ign_pats)) != NULL) {
                    467:                TAILQ_REMOVE(&dir_ign_pats, ip, ip_list);
                    468:                xfree(ip);
1.91      joris     469:        }
1.114     xsa       470:
1.138     joris     471:        entlist = cvs_ent_open(cf->file_path);
                    472:        TAILQ_FOREACH(line, &(entlist->cef_ent), entries_list) {
                    473:                ent = cvs_ent_parse(line->buf);
1.61      xsa       474:
1.138     joris     475:                l = snprintf(fpath, MAXPATHLEN, "%s/%s", cf->file_path,
                    476:                    ent->ce_name);
                    477:                if (l == -1 || l >= MAXPATHLEN)
                    478:                        fatal("cvs_file_walkdir: overflow");
1.100     joris     479:
1.140     joris     480:                if (!(cr->flags & CR_RECURSE_DIRS) &&
                    481:                    ent->ce_type == CVS_ENT_DIR)
                    482:                        continue;
                    483:
1.138     joris     484:                if (ent->ce_type == CVS_ENT_DIR)
                    485:                        cvs_file_get(fpath, &dl);
                    486:                else if (ent->ce_type == CVS_ENT_FILE)
                    487:                        cvs_file_get(fpath, &fl);
1.68      joris     488:
1.138     joris     489:                cvs_ent_free(ent);
1.128     joris     490:        }
                    491:
1.138     joris     492:        cvs_ent_close(entlist, ENT_NOSYNC);
1.91      joris     493:
1.140     joris     494:        if (cr->flags & CR_REPO) {
                    495:                repo = xmalloc(MAXPATHLEN);
                    496:                cvs_get_repo(cf->file_path, repo, MAXPATHLEN);
                    497:                cvs_repository_lock(repo);
1.68      joris     498:
1.140     joris     499:                cvs_repository_getdir(repo, cf->file_path, &fl, &dl,
                    500:                    (cr->flags & CR_RECURSE_DIRS));
                    501:        }
1.68      joris     502:
1.138     joris     503:        cvs_file_walklist(&fl, cr);
                    504:        cvs_file_freelist(&fl);
1.68      joris     505:
1.140     joris     506:        if (cr->flags & CR_REPO) {
                    507:                cvs_repository_unlock(repo);
                    508:                xfree(repo);
                    509:        }
1.68      joris     510:
1.138     joris     511:        cvs_file_walklist(&dl, cr);
                    512:        cvs_file_freelist(&dl);
1.34      jfb       513:
1.138     joris     514:        xfree(fpath);
1.26      jfb       515:
1.138     joris     516:        if (cr->leavedir != NULL)
                    517:                cr->leavedir(cf);
1.3       jfb       518: }
                    519:
                    520: void
1.138     joris     521: cvs_file_freelist(struct cvs_flisthead *fl)
1.3       jfb       522: {
1.138     joris     523:        struct cvs_filelist *f;
1.62      jfb       524:
1.138     joris     525:        while ((f = TAILQ_FIRST(fl)) != NULL) {
                    526:                TAILQ_REMOVE(fl, f, flist);
                    527:                xfree(f->file_path);
                    528:                xfree(f);
1.62      jfb       529:        }
1.5       jfb       530: }
                    531:
1.138     joris     532: void
                    533: cvs_file_classify(struct cvs_file *cf)
1.3       jfb       534: {
1.138     joris     535:        size_t len;
                    536:        time_t mtime;
                    537:        struct stat st;
                    538:        int rflags, l, ismodified, rcsdead;
                    539:        CVSENTRIES *entlist = NULL;
                    540:        const char *state;
                    541:        char *repo, *rcsfile, r1[16], r2[16];
                    542:
                    543:        cvs_log(LP_TRACE, "cvs_file_classify(%s)", cf->file_path);
                    544:
                    545:        if (!strcmp(cf->file_path, ".")) {
                    546:                cf->file_status = FILE_UPTODATE;
                    547:                return;
                    548:        }
                    549:
                    550:        entlist = cvs_ent_open(cf->file_wd);
                    551:
                    552:        repo = xmalloc(MAXPATHLEN);
                    553:        rcsfile = xmalloc(MAXPATHLEN);
                    554:
                    555:        cvs_get_repo(cf->file_wd, repo, MAXPATHLEN);
                    556:        l = snprintf(rcsfile, MAXPATHLEN, "%s/%s",
                    557:            repo, cf->file_name);
                    558:        if (l == -1 || l >= MAXPATHLEN)
                    559:                fatal("cvs_file_classify: overflow");
                    560:
                    561:        if (cf->file_type == CVS_FILE) {
                    562:                len = strlcat(rcsfile, RCS_FILE_EXT, MAXPATHLEN);
                    563:                if (len >= MAXPATHLEN)
                    564:                        fatal("cvs_file_classify: truncation");
                    565:        }
                    566:
                    567:        cf->file_rpath = xstrdup(rcsfile);
                    568:        cf->file_ent = cvs_ent_get(entlist, cf->file_name);
                    569:
                    570:        if (cf->file_type == CVS_DIR) {
                    571:                if (cf->fd == -1 && stat(rcsfile, &st) != -1)
                    572:                        cf->file_status = DIR_CREATE;
                    573:                else if (cf->file_ent != NULL)
                    574:                        cf->file_status = FILE_UPTODATE;
                    575:                xfree(repo);
                    576:                xfree(rcsfile);
                    577:                cvs_ent_close(entlist, ENT_NOSYNC);
                    578:                return;
                    579:        }
                    580:
                    581:        rflags = 0;
                    582:        cf->repo_fd = open(cf->file_rpath, O_RDONLY);
                    583:        if (cf->repo_fd != -1) {
                    584:                cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, rflags);
                    585:                if (cf->file_rcs == NULL)
                    586:                        fatal("cvs_file_classify: rcs_open failed while it "
                    587:                            "shouldn't");
                    588:        } else {
                    589:                cf->file_rcs = NULL;
1.3       jfb       590:        }
                    591:
1.138     joris     592:        if (cf->file_ent != NULL)
                    593:                rcsnum_tostr(cf->file_ent->ce_rev, r1, sizeof(r1));
                    594:        if (cf->file_rcs != NULL)
                    595:                rcsnum_tostr(cf->file_rcs->rf_head, r2, sizeof(r2));
1.6       jfb       596:
1.138     joris     597:        ismodified = rcsdead = 0;
                    598:        if (cf->fd != -1 && cf->file_ent != NULL) {
                    599:                if (fstat(cf->fd, &st) == -1)
                    600:                        fatal("cvs_file_classify: %s", strerror(errno));
1.6       jfb       601:
1.138     joris     602:                mtime = cvs_hack_time(st.st_mtime, 1);
                    603:                if (mtime == 0)
                    604:                        fatal("to gmt failed");
1.62      jfb       605:
1.138     joris     606:                if (mtime != cf->file_ent->ce_mtime)
                    607:                        ismodified = 1;
1.62      jfb       608:        }
                    609:
1.138     joris     610:        if (cf->file_rcs != NULL) {
                    611:                state = rcs_state_get(cf->file_rcs, cf->file_rcs->rf_head);
                    612:                if (state == NULL)
                    613:                        fatal("failed to get state for HEAD for %s",
                    614:                            cf->file_path);
1.139     joris     615:                if (!strcmp(state, RCS_STATE_DEAD))
1.138     joris     616:                        rcsdead = 1;
1.128     joris     617:        }
                    618:
1.138     joris     619:        /*
                    620:         * 10 Sin
                    621:         * 20 Goto hell
                    622:         * (I welcome you if-else hell)
                    623:         */
                    624:        if (cf->file_ent == NULL) {
                    625:                if (cf->file_rcs == NULL) {
                    626:                        if (cf->fd == -1) {
                    627:                                if (verbosity > 1)
                    628:                                        cvs_log(LP_NOTICE,
                    629:                                            "nothing known about '%s'",
                    630:                                            cf->file_path);
                    631:                        } else {
                    632:                                if (verbosity > 1)
                    633:                                        cvs_log(LP_NOTICE,
                    634:                                            "use add to create an entry for %s",
                    635:                                            cf->file_path);
                    636:                        }
1.130     joris     637:
1.138     joris     638:                        cf->file_status = FILE_UNKNOWN;
                    639:                } else if (rcsdead == 1) {
                    640:                        if (cf->fd == -1) {
                    641:                                cf->file_status = FILE_UPTODATE;
                    642:                        } else {
                    643:                                cvs_log(LP_NOTICE,
                    644:                                    "use add to create an entry for %s",
                    645:                                    cf->file_path);
                    646:                                cf->file_status = FILE_UNKNOWN;
1.44      jfb       647:                        }
1.138     joris     648:                } else {
                    649:                        cf->file_status = FILE_CHECKOUT;
1.14      jfb       650:                }
1.138     joris     651:        } else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
                    652:                if (cf->fd == -1) {
                    653:                        if (verbosity > 1)
                    654:                                cvs_log(LP_NOTICE,
                    655:                                    "warning: new-born %s has dissapeared",
                    656:                                    cf->file_path);
                    657:                        cf->file_status = FILE_REMOVE_ENTRY;
                    658:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    659:                        cf->file_status = FILE_ADDED;
                    660:                } else {
                    661:                        if (verbosity > 1)
                    662:                                cvs_log(LP_NOTICE,
                    663:                                    "conflict: %s already created by others",
                    664:                                    cf->file_path);
                    665:                        cf->file_status = FILE_CONFLICT;
                    666:                }
                    667:        } else if (cf->file_ent->ce_status == CVS_ENT_REMOVED) {
                    668:                if (cf->fd != -1) {
                    669:                        if (verbosity > 1)
                    670:                                cvs_log(LP_NOTICE,
                    671:                                    "%s should be removed but is still there",
                    672:                                    cf->file_path);
                    673:                        cf->file_status = FILE_REMOVED;
                    674:                } else if (cf->file_rcs == NULL || rcsdead == 1) {
                    675:                        cf->file_status = FILE_REMOVE_ENTRY;
1.66      joris     676:                } else {
1.138     joris     677:                        if (strcmp(r1, r2)) {
                    678:                                if (verbosity > 1)
                    679:                                        cvs_log(LP_NOTICE,
                    680:                                            "conflict: removed %s was modified"
                    681:                                            " by a second party",
                    682:                                            cf->file_path);
                    683:                                cf->file_status = FILE_CONFLICT;
                    684:                        } else {
                    685:                                cf->file_status = FILE_REMOVED;
                    686:                        }
1.66      joris     687:                }
1.138     joris     688:        } else if (cf->file_ent->ce_status == CVS_ENT_REG) {
                    689:                if (cf->file_rcs == NULL || rcsdead == 1) {
                    690:                        if (cf->fd == -1) {
                    691:                                if (verbosity > 1)
                    692:                                        cvs_log(LP_NOTICE,
                    693:                                            "warning: %s's entry exists but"
                    694:                                            " there is no longer a file"
                    695:                                            " in the repository,"
                    696:                                            " removing entry",
                    697:                                             cf->file_path);
                    698:                                cf->file_status = FILE_REMOVE_ENTRY;
                    699:                        } else {
                    700:                                if (ismodified) {
                    701:                                        if (verbosity > 1)
                    702:                                                cvs_log(LP_NOTICE,
                    703:                                                    "conflict: %s is no longer "
                    704:                                                    "in the repository but is "
                    705:                                                    "locally modified",
                    706:                                                    cf->file_path);
                    707:                                        cf->file_status = FILE_CONFLICT;
                    708:                                } else {
                    709:                                        if (verbosity > 1)
                    710:                                                cvs_log(LP_NOTICE,
                    711:                                                    "%s is no longer in the "
                    712:                                                    "repository",
                    713:                                                    cf->file_path);
1.128     joris     714:
1.138     joris     715:                                        cf->file_status = FILE_UNLINK;
                    716:                                }
                    717:                        }
1.128     joris     718:                } else {
1.138     joris     719:                        if (cf->fd == -1) {
                    720:                                if (verbosity > 1)
                    721:                                        cvs_log(LP_NOTICE,
                    722:                                            "warning: %s was lost",
                    723:                                            cf->file_path);
                    724:                                cf->file_status = FILE_LOST;
                    725:                        } else {
                    726:                                if (ismodified == 1)
                    727:                                        cf->file_status = FILE_MODIFIED;
                    728:                                else
                    729:                                        cf->file_status = FILE_UPTODATE;
                    730:
                    731:                                if (strcmp(r1, r2)) {
                    732:                                        if (cf->file_status == FILE_MODIFIED)
                    733:                                                cf->file_status = FILE_MERGE;
                    734:                                        else
                    735:                                                cf->file_status = FILE_PATCH;
                    736:                                }
                    737:                        }
1.128     joris     738:                }
                    739:        }
                    740:
1.138     joris     741:        xfree(repo);
                    742:        xfree(rcsfile);
                    743:        cvs_ent_close(entlist, ENT_NOSYNC);
                    744: }
1.14      jfb       745:
1.138     joris     746: void
                    747: cvs_file_free(struct cvs_file *cf)
                    748: {
                    749:        xfree(cf->file_name);
                    750:        xfree(cf->file_wd);
                    751:        xfree(cf->file_path);
                    752:
                    753:        if (cf->file_rpath != NULL)
                    754:                xfree(cf->file_rpath);
                    755:        if (cf->file_ent != NULL)
                    756:                cvs_ent_free(cf->file_ent);
                    757:        if (cf->file_rcs != NULL)
                    758:                rcs_close(cf->file_rcs);
                    759:        if (cf->fd != -1)
                    760:                (void)close(cf->fd);
                    761:        if (cf->repo_fd != -1)
                    762:                (void)close(cf->repo_fd);
                    763:        xfree(cf);
1.23      jfb       764: }
                    765:
                    766: static int
                    767: cvs_file_cmpname(const char *name1, const char *name2)
                    768: {
                    769:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    770:            (strcasecmp(name1, name2));
1.14      jfb       771: }