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

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26      jfb         4:  * All rights reserved.
1.1       jfb         5:  *
1.26      jfb         6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.26      jfb        10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.26      jfb        13:  *    derived from this software without specific prior written permission.
1.1       jfb        14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.26      jfb        24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
                     27: #include <sys/types.h>
                     28: #include <sys/queue.h>
                     29: #include <sys/stat.h>
                     30:
                     31: #include <pwd.h>
                     32: #include <errno.h>
                     33: #include <stdio.h>
                     34: #include <fcntl.h>
                     35: #include <dirent.h>
                     36: #include <stdlib.h>
                     37: #include <unistd.h>
                     38: #include <string.h>
                     39: #include <fnmatch.h>
                     40:
                     41: #include "cvs.h"
                     42: #include "log.h"
1.14      jfb        43: #include "file.h"
1.1       jfb        44:
                     45:
                     46: #define CVS_IGN_STATIC    0x01     /* pattern is static, no need to glob */
                     47:
                     48: #define CVS_CHAR_ISMETA(c)  ((c == '*') || (c == '?') || (c == '['))
                     49:
                     50:
                     51: /* ignore pattern */
                     52: struct cvs_ignpat {
                     53:        char  ip_pat[MAXNAMLEN];
                     54:        int   ip_flags;
                     55:        TAILQ_ENTRY (cvs_ignpat) ip_list;
                     56: };
                     57:
                     58:
                     59: /*
                     60:  * Standard patterns to ignore.
                     61:  */
                     62:
                     63: static const char *cvs_ign_std[] = {
                     64:        ".",
                     65:        "..",
                     66:        "*.o",
                     67:        "*.so",
                     68:        "*.bak",
                     69:        "*.orig",
                     70:        "*.rej",
                     71:        "*.exe",
                     72:        "*.depend",
                     73:        "CVS",
                     74:        "core",
1.8       jfb        75:        ".#*",
1.1       jfb        76: #ifdef OLD_SMELLY_CRUFT
                     77:        "RCSLOG",
                     78:        "tags",
                     79:        "TAGS",
                     80:        "RCS",
                     81:        "SCCS",
                     82:        "#*",
                     83:        ",*",
                     84: #endif
                     85: };
                     86:
                     87:
1.11      jfb        88: /*
1.34    ! jfb        89:  * Filename hash table used to avoid duplication of name strings when working
        !            90:  * on large source trees with common parts.
        !            91:  */
        !            92:
        !            93: SLIST_HEAD(cvs_fhb, cvs_fname);
        !            94:
        !            95: static struct cvs_fhb cvs_fnht[CVS_FILE_NBUCKETS];
        !            96:
        !            97:
        !            98:
        !            99: /*
1.11      jfb       100:  * Entries in the CVS/Entries file with a revision of '0' have only been
                    101:  * added.  Compare against this revision to see if this is the case
                    102:  */
1.4       jfb       103: static RCSNUM *cvs_addedrev;
                    104:
                    105:
1.1       jfb       106: TAILQ_HEAD(, cvs_ignpat)  cvs_ign_pats;
                    107:
                    108:
1.34    ! jfb       109: static int               cvs_file_getdir   (CVSFILE *, int);
        !           110: static void              cvs_file_freedir  (struct cvs_dir *);
        !           111: static int               cvs_file_sort     (struct cvs_flist *, u_int);
        !           112: static int               cvs_file_cmp      (const void *, const void *);
        !           113: static int               cvs_file_cmpname  (const char *, const char *);
        !           114: static u_int8_t          cvs_file_hashname (const char *);
        !           115: static struct cvs_fname* cvs_file_getname  (const char *);
        !           116: static CVSFILE*          cvs_file_alloc    (const char *, u_int);
        !           117: static CVSFILE*          cvs_file_lget     (const char *, int, CVSFILE *);
1.3       jfb       118:
                    119:
                    120:
1.1       jfb       121: /*
                    122:  * cvs_file_init()
                    123:  *
                    124:  */
                    125:
                    126: int
                    127: cvs_file_init(void)
                    128: {
                    129:        int i;
                    130:        size_t len;
                    131:        char path[MAXPATHLEN], buf[MAXNAMLEN];
                    132:        FILE *ifp;
                    133:        struct passwd *pwd;
                    134:
1.34    ! jfb       135:        /* initialize the filename hash table */
        !           136:        for (i = 0; i < CVS_FILE_NBUCKETS; i++)
        !           137:                SLIST_INIT(&(cvs_fnht[i]));
        !           138:
1.1       jfb       139:        TAILQ_INIT(&cvs_ign_pats);
                    140:
1.4       jfb       141:        cvs_addedrev = rcsnum_alloc();
                    142:        rcsnum_aton("0", NULL, cvs_addedrev);
                    143:
1.1       jfb       144:        /* standard patterns to ignore */
1.10      jfb       145:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.26      jfb       146:                cvs_file_ignore(cvs_ign_std[i]);
1.1       jfb       147:
                    148:        /* read the cvsignore file in the user's home directory, if any */
                    149:        pwd = getpwuid(getuid());
                    150:        if (pwd != NULL) {
                    151:                snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
                    152:                ifp = fopen(path, "r");
                    153:                if (ifp == NULL) {
                    154:                        if (errno != ENOENT)
1.34    ! jfb       155:                                cvs_log(LP_ERRNO,
        !           156:                                    "failed to open user's cvsignore", path);
1.1       jfb       157:                }
                    158:                else {
                    159:                        while (fgets(buf, sizeof(buf), ifp) != NULL) {
                    160:                                len = strlen(buf);
                    161:                                if (len == 0)
                    162:                                        continue;
                    163:                                if (buf[len - 1] != '\n') {
                    164:                                        cvs_log(LP_ERR, "line too long in `%s'",
                    165:                                            path);
                    166:                                }
                    167:                                buf[--len] = '\0';
                    168:                                cvs_file_ignore(buf);
                    169:                        }
                    170:                        (void)fclose(ifp);
                    171:                }
                    172:        }
                    173:
                    174:        return (0);
                    175: }
                    176:
                    177:
                    178: /*
                    179:  * cvs_file_ignore()
                    180:  *
                    181:  * Add the pattern <pat> to the list of patterns for files to ignore.
                    182:  * Returns 0 on success, or -1 on failure.
                    183:  */
                    184:
                    185: int
                    186: cvs_file_ignore(const char *pat)
                    187: {
                    188:        char *cp;
                    189:        struct cvs_ignpat *ip;
                    190:
                    191:        ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
                    192:        if (ip == NULL) {
                    193:                cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
                    194:                return (-1);
                    195:        }
                    196:
                    197:        strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    198:
                    199:        /* check if we will need globbing for that pattern */
                    200:        ip->ip_flags = CVS_IGN_STATIC;
                    201:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    202:                if (CVS_CHAR_ISMETA(*cp)) {
                    203:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    204:                        break;
                    205:                }
                    206:        }
                    207:
                    208:        TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
                    209:
                    210:        return (0);
                    211: }
                    212:
                    213:
                    214: /*
1.5       jfb       215:  * cvs_file_chkign()
1.1       jfb       216:  *
                    217:  * Returns 1 if the filename <file> is matched by one of the ignore
                    218:  * patterns, or 0 otherwise.
                    219:  */
                    220:
                    221: int
1.5       jfb       222: cvs_file_chkign(const char *file)
1.1       jfb       223: {
1.23      jfb       224:        int flags;
1.1       jfb       225:        struct cvs_ignpat *ip;
                    226:
1.23      jfb       227:        flags = FNM_PERIOD;
                    228:        if (cvs_nocase)
                    229:                flags |= FNM_CASEFOLD;
                    230:
1.1       jfb       231:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    232:                if (ip->ip_flags & CVS_IGN_STATIC) {
1.23      jfb       233:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1       jfb       234:                                return (1);
                    235:                }
1.23      jfb       236:                else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1       jfb       237:                        return (1);
                    238:        }
                    239:
                    240:        return (0);
                    241: }
                    242:
                    243:
                    244: /*
1.6       jfb       245:  * cvs_file_create()
1.1       jfb       246:  *
1.6       jfb       247:  * Create a new file whose path is specified in <path> and of type <type>.
1.26      jfb       248:  * If the type is DT_DIR, the CVS administrative repository and files will be
                    249:  * created.
1.25      jfb       250:  * Returns the created file on success, or NULL on failure.
1.1       jfb       251:  */
                    252:
1.6       jfb       253: CVSFILE*
1.34    ! jfb       254: cvs_file_create(CVSFILE *parent, const char *path, u_int type, mode_t mode)
1.1       jfb       255: {
1.6       jfb       256:        int fd;
1.34    ! jfb       257:        char fp[MAXPATHLEN];
1.6       jfb       258:        CVSFILE *cfp;
1.1       jfb       259:
1.34    ! jfb       260:        printf("cvs_file_create(%s)\n", path);
1.6       jfb       261:        cfp = cvs_file_alloc(path, type);
                    262:        if (cfp == NULL)
                    263:                return (NULL);
1.26      jfb       264:
1.6       jfb       265:        cfp->cf_type = type;
1.22      jfb       266:        cfp->cf_mode = mode;
1.34    ! jfb       267:        cfp->cf_parent = parent;
1.1       jfb       268:
1.34    ! jfb       269:        if (type == DT_DIR) {
        !           270:                cfp->cf_ddat->cd_root = cvsroot_get(path);
        !           271:                cfp->cf_ddat->cd_repo = strdup(cvs_file_getpath(cfp,
        !           272:                    fp, sizeof(fp)));
        !           273:                if (cfp->cf_ddat->cd_repo == NULL) {
        !           274:                        cvs_file_free(cfp);
        !           275:                        return (NULL);
        !           276:                }
1.33      joris     277:
1.26      jfb       278:                if ((mkdir(path, mode) == -1) || (cvs_mkadmin(cfp, mode) < 0)) {
1.6       jfb       279:                        cvs_file_free(cfp);
                    280:                        return (NULL);
                    281:                }
1.26      jfb       282:
1.27      jfb       283:                cfp->cf_ddat->cd_ent = cvs_ent_open(path, O_RDWR);
1.1       jfb       284:        }
1.6       jfb       285:        else {
                    286:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    287:                if (fd == -1) {
                    288:                        cvs_file_free(cfp);
1.1       jfb       289:                        return (NULL);
                    290:                }
1.6       jfb       291:                (void)close(fd);
1.1       jfb       292:        }
                    293:
1.6       jfb       294:        return (cfp);
1.3       jfb       295: }
                    296:
                    297:
                    298: /*
                    299:  * cvs_file_get()
                    300:  *
                    301:  * Load a cvs_file structure with all the information pertaining to the file
                    302:  * <path>.
1.4       jfb       303:  * The <flags> parameter specifies various flags that alter the behaviour of
1.21      jfb       304:  * the function.  The CF_RECURSE flag causes the function to recursively load
                    305:  * subdirectories when <path> is a directory.
                    306:  * The CF_SORT flag causes the files to be sorted in alphabetical order upon
                    307:  * loading.  The special case of "." as a path specification generates
                    308:  * recursion for a single level and is equivalent to calling cvs_file_get() on
                    309:  * all files of that directory.
1.3       jfb       310:  * Returns a pointer to the cvs file structure, which must later be freed
                    311:  * with cvs_file_free().
                    312:  */
                    313:
1.14      jfb       314: CVSFILE*
1.3       jfb       315: cvs_file_get(const char *path, int flags)
                    316: {
1.14      jfb       317:        return cvs_file_lget(path, flags, NULL);
1.9       jfb       318: }
                    319:
                    320:
                    321: /*
                    322:  * cvs_file_getspec()
                    323:  *
                    324:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    325:  * whose size is given in <fsn>.
                    326:  * Returns a pointer to the lowest common subdirectory to all specified
                    327:  * files.
                    328:  */
                    329:
                    330: CVSFILE*
                    331: cvs_file_getspec(char **fspec, int fsn, int flags)
                    332: {
1.26      jfb       333:        int i;
                    334:        char *sp, *np, pcopy[MAXPATHLEN];
                    335:        CVSFILE *base, *cf, *nf;
                    336:
                    337:        base = cvs_file_get(".", 0);
                    338:        if (base == NULL)
                    339:                return (NULL);
                    340:
                    341:        for (i = 0; i < fsn; i++) {
                    342:                strlcpy(pcopy, fspec[i], sizeof(pcopy));
                    343:                cf = base;
                    344:                sp = pcopy;
                    345:
                    346:                do {
                    347:                        np = strchr(sp, '/');
                    348:                        if (np != NULL)
                    349:                                *np = '\0';
                    350:                        nf = cvs_file_find(cf, sp);
                    351:                        if (nf == NULL) {
                    352:                                nf = cvs_file_lget(pcopy, 0, cf);
                    353:                                if (nf == NULL) {
                    354:                                        cvs_file_free(base);
                    355:                                        return (NULL);
                    356:                                }
                    357:
                    358:                                cvs_file_attach(cf, nf);
                    359:                        }
                    360:
                    361:                        if (np != NULL) {
                    362:                                *np = '/';
                    363:                                sp = np + 1;
                    364:                        }
                    365:
                    366:                        cf = nf;
                    367:                } while (np != NULL);
                    368:        }
                    369:
                    370:        return (base);
1.3       jfb       371: }
                    372:
                    373:
                    374: /*
1.13      jfb       375:  * cvs_file_find()
                    376:  *
                    377:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    378:  * The file's pathname <path> must be relative to the base of <hier>.
                    379:  * Returns the entry on success, or NULL on failure.
                    380:  */
                    381:
                    382: CVSFILE*
                    383: cvs_file_find(CVSFILE *hier, const char *path)
                    384: {
                    385:        char *pp, *sp, pbuf[MAXPATHLEN];
                    386:        CVSFILE *sf, *cf;
                    387:
                    388:        strlcpy(pbuf, path, sizeof(pbuf));
                    389:
                    390:        cf = hier;
                    391:        pp = pbuf;
                    392:        do {
                    393:                sp = strchr(pp, '/');
                    394:                if (sp != NULL)
1.24      jfb       395:                        *(sp++) = '\0';
1.13      jfb       396:
                    397:                /* special case */
                    398:                if (*pp == '.') {
                    399:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    400:                                /* request to go back to parent */
                    401:                                if (cf->cf_parent == NULL) {
                    402:                                        cvs_log(LP_NOTICE,
                    403:                                            "path %s goes back too far", path);
                    404:                                        return (NULL);
                    405:                                }
                    406:                                cf = cf->cf_parent;
                    407:                                continue;
                    408:                        }
                    409:                        else if (*(pp + 1) == '\0')
                    410:                                continue;
                    411:                }
                    412:
                    413:                TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
1.34    ! jfb       414:                        if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13      jfb       415:                                break;
                    416:                if (sf == NULL)
                    417:                        return (NULL);
                    418:
                    419:                cf = sf;
                    420:                pp = sp;
                    421:        } while (sp != NULL);
                    422:
1.24      jfb       423:        return (cf);
1.13      jfb       424: }
                    425:
                    426:
                    427: /*
1.34    ! jfb       428:  * cvs_file_getpath()
        !           429:  *
        !           430:  * Get the full path of the file <file> and store it in <buf>, which is of
        !           431:  * size <len>.  For portability, it is recommended that <buf> always be
        !           432:  * at least MAXPATHLEN bytes long.
        !           433:  * Returns a pointer to the start of the path on success, or NULL on failure.
        !           434:  */
        !           435:
        !           436: char*
        !           437: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
        !           438: {
        !           439:        u_int i;
        !           440:        char *fp, *namevec[CVS_FILE_MAXDEPTH];
        !           441:        CVSFILE *top;
        !           442:
        !           443:        buf[0] = '\0';
        !           444:        i = CVS_FILE_MAXDEPTH;
        !           445:        memset(namevec, 0, sizeof(namevec));
        !           446:
        !           447:        /* find the top node */
        !           448:        for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
        !           449:                fp = CVS_FILE_NAME(top);
        !           450:
        !           451:                /* skip self-references */
        !           452:                if ((fp[0] == '.') && (fp[1] == '\0'))
        !           453:                        continue;
        !           454:                namevec[--i] = fp;
        !           455:        }
        !           456:
        !           457:        if (i == 0)
        !           458:                return (NULL);
        !           459:        else if (i == CVS_FILE_MAXDEPTH) {
        !           460:                strlcpy(buf, ".", len);
        !           461:                return (buf);
        !           462:        }
        !           463:
        !           464:        while (i < CVS_FILE_MAXDEPTH - 1) {
        !           465:                strlcat(buf, namevec[i++], len);
        !           466:                strlcat(buf, "/", len);
        !           467:        }
        !           468:        strlcat(buf, namevec[i], len);
        !           469:
        !           470:        return (buf);
        !           471: }
        !           472:
        !           473:
        !           474: /*
1.22      jfb       475:  * cvs_file_attach()
                    476:  *
                    477:  * Attach the file <file> as one of the children of parent <parent>, which
                    478:  * has to be a file of type DT_DIR.
                    479:  * Returns 0 on success, or -1 on failure.
                    480:  */
                    481:
                    482: int
                    483: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
                    484: {
1.23      jfb       485:        struct cvs_dir *dp;
1.22      jfb       486:
                    487:        if (parent->cf_type != DT_DIR)
                    488:                return (-1);
                    489:
1.23      jfb       490:        dp = parent->cf_ddat;
                    491:
                    492:        TAILQ_INSERT_TAIL(&(dp->cd_files), file, cf_list);
                    493:        dp->cd_nfiles++;
1.22      jfb       494:        file->cf_parent = parent;
                    495:
                    496:        return (0);
                    497: }
                    498:
                    499:
                    500: /*
1.3       jfb       501:  * cvs_file_getdir()
                    502:  *
                    503:  * Get a cvs directory structure for the directory whose path is <dir>.
1.30      jfb       504:  * This function should not free the directory information on error, as this
                    505:  * is performed by cvs_file_free().
1.3       jfb       506:  */
                    507:
1.6       jfb       508: static int
1.14      jfb       509: cvs_file_getdir(CVSFILE *cf, int flags)
1.3       jfb       510: {
1.13      jfb       511:        int ret, fd;
1.16      jfb       512:        u_int ndirs;
1.3       jfb       513:        long base;
1.13      jfb       514:        void *dp, *ep;
1.34    ! jfb       515:        char fbuf[2048], pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
1.3       jfb       516:        struct dirent *ent;
1.14      jfb       517:        CVSFILE *cfp;
1.20      jfb       518:        struct stat st;
1.3       jfb       519:        struct cvs_dir *cdp;
1.10      jfb       520:        struct cvs_flist dirs;
1.3       jfb       521:
1.18      jfb       522:        ndirs = 0;
1.10      jfb       523:        TAILQ_INIT(&dirs);
1.7       jfb       524:        cdp = cf->cf_ddat;
                    525:
1.34    ! jfb       526:        cvs_file_getpath(cf, fpath, sizeof(fpath));
        !           527:
1.26      jfb       528:        if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
1.34    ! jfb       529:                cdp->cd_root = cvsroot_get(fpath);
1.30      jfb       530:                if (cdp->cd_root == NULL)
1.26      jfb       531:                        return (-1);
1.3       jfb       532:
1.26      jfb       533:                if (flags & CF_MKADMIN)
                    534:                        cvs_mkadmin(cf, 0755);
1.14      jfb       535:
1.26      jfb       536:                /* if the CVS administrative directory exists, load the info */
1.34    ! jfb       537:                snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
1.26      jfb       538:                if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
1.34    ! jfb       539:                        if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
1.26      jfb       540:                                cdp->cd_repo = strdup(pbuf);
                    541:                                if (cdp->cd_repo == NULL) {
1.28      jfb       542:                                        cvs_log(LP_ERRNO,
                    543:                                            "failed to dup repository string");
1.26      jfb       544:                                        return (-1);
                    545:                                }
1.20      jfb       546:                        }
1.26      jfb       547:
1.34    ! jfb       548:                        cdp->cd_ent = cvs_ent_open(fpath, O_RDWR);
1.20      jfb       549:                }
                    550:        }
1.14      jfb       551:
1.26      jfb       552:        if (!(flags & CF_RECURSE) || (cf->cf_cvstat == CVS_FST_UNKNOWN))
                    553:                return (0);
                    554:
1.34    ! jfb       555:        fd = open(fpath, O_RDONLY);
1.3       jfb       556:        if (fd == -1) {
1.34    ! jfb       557:                cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
1.6       jfb       558:                return (-1);
1.3       jfb       559:        }
                    560:
1.11      jfb       561:        do {
                    562:                ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    563:                if (ret == -1) {
                    564:                        cvs_log(LP_ERRNO, "failed to get directory entries");
                    565:                        (void)close(fd);
                    566:                        return (-1);
                    567:                }
1.10      jfb       568:
1.11      jfb       569:                dp = fbuf;
                    570:                ep = fbuf + (size_t)ret;
                    571:                while (dp < ep) {
                    572:                        ent = (struct dirent *)dp;
1.32      jfb       573:                        dp += ent->d_reclen;
1.31      jfb       574:                        if (ent->d_fileno == 0)
                    575:                                continue;
1.11      jfb       576:
                    577:                        if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.24      jfb       578:                                continue;
                    579:
                    580:                        if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
1.11      jfb       581:                                continue;
                    582:
1.34    ! jfb       583:                        snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
        !           584:                            ent->d_name);
1.14      jfb       585:                        cfp = cvs_file_lget(pbuf, flags, cf);
1.11      jfb       586:                        if (cfp != NULL) {
1.26      jfb       587:                                if (cfp->cf_type == DT_DIR) {
                    588:                                        TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.16      jfb       589:                                        ndirs++;
                    590:                                }
                    591:                                else {
1.26      jfb       592:                                        TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
1.11      jfb       593:                                            cf_list);
1.16      jfb       594:                                        cdp->cd_nfiles++;
                    595:                                }
1.11      jfb       596:                        }
1.4       jfb       597:                }
1.11      jfb       598:        } while (ret > 0);
1.14      jfb       599:
1.34    ! jfb       600: #if 1
        !           601:        cvs_ent_close(cdp->cd_ent);
        !           602:        cdp->cd_ent = NULL;
        !           603: #endif
        !           604:
1.10      jfb       605:        if (flags & CF_SORT) {
1.16      jfb       606:                cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
                    607:                cvs_file_sort(&dirs, ndirs);
1.10      jfb       608:        }
1.26      jfb       609:
                    610:        while (!TAILQ_EMPTY(&dirs)) {
                    611:                cfp = TAILQ_FIRST(&dirs);
                    612:                TAILQ_REMOVE(&dirs, cfp, cf_list);
1.10      jfb       613:                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.26      jfb       614:        }
1.16      jfb       615:        cdp->cd_nfiles += ndirs;
1.3       jfb       616:
                    617:        (void)close(fd);
                    618:
1.6       jfb       619:        return (0);
1.3       jfb       620: }
                    621:
                    622:
                    623: /*
                    624:  * cvs_file_free()
                    625:  *
                    626:  * Free a cvs_file structure and its contents.
                    627:  */
                    628:
                    629: void
1.14      jfb       630: cvs_file_free(CVSFILE *cf)
1.3       jfb       631: {
                    632:        if (cf->cf_ddat != NULL)
                    633:                cvs_file_freedir(cf->cf_ddat);
                    634:        free(cf);
1.5       jfb       635: }
                    636:
                    637:
                    638: /*
                    639:  * cvs_file_examine()
                    640:  *
                    641:  * Examine the contents of the CVS file structure <cf> with the function
                    642:  * <exam>.  The function is called for all subdirectories and files of the
                    643:  * root file.
                    644:  */
                    645:
                    646: int
                    647: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    648: {
                    649:        int ret;
1.14      jfb       650:        CVSFILE *fp;
1.5       jfb       651:
                    652:        if (cf->cf_type == DT_DIR) {
                    653:                ret = (*exam)(cf, arg);
1.10      jfb       654:                TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5       jfb       655:                        ret = cvs_file_examine(fp, exam, arg);
                    656:                        if (ret == -1)
1.13      jfb       657:                                break;
1.5       jfb       658:                }
                    659:        }
                    660:        else
1.13      jfb       661:                ret = (*exam)(cf, arg);
                    662:
                    663:        return (ret);
1.3       jfb       664: }
                    665:
                    666:
                    667: /*
                    668:  * cvs_file_freedir()
                    669:  *
                    670:  * Free a cvs_dir structure and its contents.
                    671:  */
                    672:
                    673: static void
                    674: cvs_file_freedir(struct cvs_dir *cd)
                    675: {
1.14      jfb       676:        CVSFILE *cfp;
1.3       jfb       677:
                    678:        if (cd->cd_root != NULL)
                    679:                cvsroot_free(cd->cd_root);
                    680:        if (cd->cd_repo != NULL)
                    681:                free(cd->cd_repo);
                    682:
1.14      jfb       683:        if (cd->cd_ent != NULL)
                    684:                cvs_ent_close(cd->cd_ent);
                    685:
1.10      jfb       686:        while (!TAILQ_EMPTY(&(cd->cd_files))) {
                    687:                cfp = TAILQ_FIRST(&(cd->cd_files));
                    688:                TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3       jfb       689:                cvs_file_free(cfp);
                    690:        }
                    691: }
                    692:
                    693:
                    694: /*
                    695:  * cvs_file_sort()
                    696:  *
1.16      jfb       697:  * Sort a list of cvs file structures according to their filename.  The list
                    698:  * <flp> is modified according to the sorting algorithm.  The number of files
                    699:  * in the list must be given by <nfiles>.
                    700:  * Returns 0 on success, or -1 on failure.
1.3       jfb       701:  */
                    702:
                    703: static int
1.16      jfb       704: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb       705: {
                    706:        int i;
                    707:        size_t nb;
1.16      jfb       708:        CVSFILE *cf, **cfvec;
                    709:
                    710:        cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
                    711:        if (cfvec == NULL) {
                    712:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                    713:                return (-1);
                    714:        }
1.3       jfb       715:
                    716:        i = 0;
1.10      jfb       717:        TAILQ_FOREACH(cf, flp, cf_list) {
1.16      jfb       718:                if (i == (int)nfiles) {
1.3       jfb       719:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb       720:                        /* rebuild the list and abort sorting */
                    721:                        while (--i >= 0)
                    722:                                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
                    723:                        free(cfvec);
1.3       jfb       724:                        return (-1);
                    725:                }
1.16      jfb       726:                cfvec[i++] = cf;
1.3       jfb       727:
                    728:                /* now unlink it from the list,
                    729:                 * we'll put it back in order later
                    730:                 */
1.10      jfb       731:                TAILQ_REMOVE(flp, cf, cf_list);
1.3       jfb       732:        }
                    733:
                    734:        /* clear the list just in case */
1.10      jfb       735:        TAILQ_INIT(flp);
1.3       jfb       736:        nb = (size_t)i;
                    737:
                    738:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    739:
                    740:        /* rebuild the list from the bottom up */
                    741:        for (i = (int)nb - 1; i >= 0; i--)
1.10      jfb       742:                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       743:
1.16      jfb       744:        free(cfvec);
1.3       jfb       745:        return (0);
                    746: }
                    747:
                    748:
                    749: static int
                    750: cvs_file_cmp(const void *f1, const void *f2)
                    751: {
1.14      jfb       752:        CVSFILE *cf1, *cf2;
                    753:        cf1 = *(CVSFILE **)f1;
                    754:        cf2 = *(CVSFILE **)f2;
1.34    ! jfb       755:        return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6       jfb       756: }
                    757:
                    758:
1.34    ! jfb       759: /*
        !           760:  * cvs_file_alloc()
        !           761:  *
        !           762:  * Allocate a CVSFILE structure and initialize its internals.
        !           763:  */
        !           764:
1.6       jfb       765: CVSFILE*
                    766: cvs_file_alloc(const char *path, u_int type)
                    767: {
                    768:        size_t len;
                    769:        char pbuf[MAXPATHLEN];
1.34    ! jfb       770:        const char *fnp;
1.6       jfb       771:        CVSFILE *cfp;
                    772:        struct cvs_dir *ddat;
                    773:
1.14      jfb       774:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb       775:        if (cfp == NULL) {
                    776:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    777:                return (NULL);
                    778:        }
                    779:        memset(cfp, 0, sizeof(*cfp));
                    780:
                    781:        /* ditch trailing slashes */
                    782:        strlcpy(pbuf, path, sizeof(pbuf));
                    783:        len = strlen(pbuf);
                    784:        while (pbuf[len - 1] == '/')
                    785:                pbuf[--len] = '\0';
                    786:
1.34    ! jfb       787:        fnp = strrchr(path, '/');
        !           788:        if (fnp == NULL)
        !           789:                fnp = path;
        !           790:        else
        !           791:                fnp++;
        !           792:
        !           793:        cfp->cf_name = cvs_file_getname(fnp);
        !           794:        if (cfp->cf_name == NULL) {
        !           795:                cvs_log(LP_ERR, "WTF!?");
1.6       jfb       796:                return (NULL);
                    797:        }
                    798:        cfp->cf_type = type;
                    799:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    800:
                    801:        if (type == DT_DIR) {
                    802:                ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
                    803:                if (ddat == NULL) {
                    804:                        cvs_file_free(cfp);
                    805:                        return (NULL);
                    806:                }
                    807:                memset(ddat, 0, sizeof(*ddat));
1.10      jfb       808:                TAILQ_INIT(&(ddat->cd_files));
1.6       jfb       809:                cfp->cf_ddat = ddat;
                    810:        }
                    811:        return (cfp);
1.1       jfb       812: }
1.14      jfb       813:
                    814:
                    815: /*
                    816:  * cvs_file_lget()
                    817:  *
                    818:  * Get the file and link it with the parent right away.
1.22      jfb       819:  * Returns a pointer to the created file structure on success, or NULL on
                    820:  * failure.
1.14      jfb       821:  */
                    822:
                    823: static CVSFILE*
                    824: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
                    825: {
                    826:        int cwd;
                    827:        struct stat st;
                    828:        CVSFILE *cfp;
1.34    ! jfb       829:        struct cvs_ent *ent = NULL;
1.14      jfb       830:
                    831:        if (strcmp(path, ".") == 0)
                    832:                cwd = 1;
                    833:        else
                    834:                cwd = 0;
                    835:
                    836:        if (stat(path, &st) == -1) {
                    837:                cvs_log(LP_ERRNO, "failed to stat %s", path);
                    838:                return (NULL);
                    839:        }
                    840:
                    841:        cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
                    842:        if (cfp == NULL) {
                    843:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    844:                return (NULL);
                    845:        }
                    846:        cfp->cf_parent = parent;
1.22      jfb       847:        cfp->cf_mode = st.st_mode & ACCESSPERMS;
                    848:        cfp->cf_mtime = st.st_mtime;
1.14      jfb       849:
                    850:        if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL)) {
1.34    ! jfb       851:                ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), CVS_FILE_NAME(cfp));
1.14      jfb       852:        }
                    853:
1.26      jfb       854:        if (ent == NULL) {
1.14      jfb       855:                cfp->cf_cvstat = (cwd == 1) ?
                    856:                    CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.26      jfb       857:        }
1.14      jfb       858:        else {
                    859:                /* always show directories as up-to-date */
                    860:                if (ent->ce_type == CVS_ENT_DIR)
                    861:                        cfp->cf_cvstat = CVS_FST_UPTODATE;
                    862:                else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                    863:                        cfp->cf_cvstat = CVS_FST_ADDED;
                    864:                else {
                    865:                        /* check last modified time */
1.28      jfb       866:                        if (ent->ce_mtime == st.st_mtime)
1.14      jfb       867:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
                    868:                        else
                    869:                                cfp->cf_cvstat = CVS_FST_MODIFIED;
                    870:                }
                    871:        }
                    872:
1.26      jfb       873:        if ((cfp->cf_type == DT_DIR) && (cvs_file_getdir(cfp, flags) < 0)) {
                    874:                cvs_file_free(cfp);
                    875:                return (NULL);
1.14      jfb       876:        }
                    877:
                    878:        return (cfp);
1.23      jfb       879: }
                    880:
                    881:
                    882: static int
                    883: cvs_file_cmpname(const char *name1, const char *name2)
                    884: {
                    885:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    886:            (strcasecmp(name1, name2));
1.34    ! jfb       887: }
        !           888:
        !           889:
        !           890: /*
        !           891:  * cvs_file_hashname()
        !           892:  *
        !           893:  * Generate an 8 bit hash value from the name of a file.
        !           894:  * XXX Improve my distribution!
        !           895:  */
        !           896:
        !           897: static u_int8_t
        !           898: cvs_file_hashname(const char *name)
        !           899: {
        !           900:        const char *np;
        !           901:        u_int8_t h;
        !           902:
        !           903:        h = 0xb5;
        !           904:        for (np = name; *np != '\0'; np++)
        !           905:                h ^= (*np << 3 ^ *np >> 1);
        !           906:
        !           907:        return (h);
        !           908: }
        !           909:
        !           910:
        !           911: /*
        !           912:  * cvs_file_getname()
        !           913:  *
        !           914:  * Look for the file name <name> in the filename hash table.
        !           915:  * If no entry is found for that name, a new one is created and inserted into
        !           916:  * the table.  The name's reference count is increased.
        !           917:  */
        !           918:
        !           919: static struct cvs_fname*
        !           920: cvs_file_getname(const char *name)
        !           921: {
        !           922:        u_int8_t h;
        !           923:        struct cvs_fname *fnp;
        !           924:
        !           925:        h = cvs_file_hashname(name);
        !           926:
        !           927:        SLIST_FOREACH(fnp, &(cvs_fnht[h]), cf_list)
        !           928:                if (strcmp(name, fnp->cf_name) == 0) {
        !           929:                        fnp->cf_ref++;
        !           930:                        break;
        !           931:                }
        !           932:
        !           933:        if (fnp == NULL) {
        !           934:                fnp = (struct cvs_fname *)malloc(sizeof(*fnp));
        !           935:                if (fnp == NULL) {
        !           936:                        cvs_log(LP_ERRNO,
        !           937:                            "failed to allocate new file name entry");
        !           938:                        return (NULL);
        !           939:                }
        !           940:
        !           941:                fnp->cf_name = strdup(name);
        !           942:                fnp->cf_ref = 1;
        !           943:                SLIST_INSERT_HEAD(&(cvs_fnht[h]), fnp, cf_list);
        !           944:        }
        !           945:
        !           946:        return (fnp);
1.14      jfb       947: }