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

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