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

1.37    ! jfb         1: /*     $OpenBSD: file.c,v 1.36 2004/12/03 20:19:54 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:
                     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.6       jfb       260:        cfp = cvs_file_alloc(path, type);
                    261:        if (cfp == NULL)
                    262:                return (NULL);
1.26      jfb       263:
1.22      jfb       264:        cfp->cf_mode = mode;
1.34      jfb       265:        cfp->cf_parent = parent;
1.1       jfb       266:
1.34      jfb       267:        if (type == DT_DIR) {
                    268:                cfp->cf_ddat->cd_root = cvsroot_get(path);
                    269:                cfp->cf_ddat->cd_repo = strdup(cvs_file_getpath(cfp,
                    270:                    fp, sizeof(fp)));
                    271:                if (cfp->cf_ddat->cd_repo == NULL) {
                    272:                        cvs_file_free(cfp);
                    273:                        return (NULL);
                    274:                }
1.33      joris     275:
1.26      jfb       276:                if ((mkdir(path, mode) == -1) || (cvs_mkadmin(cfp, mode) < 0)) {
1.6       jfb       277:                        cvs_file_free(cfp);
                    278:                        return (NULL);
                    279:                }
1.26      jfb       280:
1.27      jfb       281:                cfp->cf_ddat->cd_ent = cvs_ent_open(path, O_RDWR);
1.1       jfb       282:        }
1.6       jfb       283:        else {
                    284:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    285:                if (fd == -1) {
                    286:                        cvs_file_free(cfp);
1.1       jfb       287:                        return (NULL);
                    288:                }
1.6       jfb       289:                (void)close(fd);
1.1       jfb       290:        }
                    291:
1.6       jfb       292:        return (cfp);
1.3       jfb       293: }
                    294:
                    295:
                    296: /*
1.35      jfb       297:  * cvs_file_copy()
                    298:  *
                    299:  * Allocate space to create a copy of the file <orig>.  The copy inherits all
                    300:  * of the original's attributes, but does not inherit its children if the
                    301:  * original file is a directory.  Note that files copied using this mechanism
                    302:  * are linked to their parent, but the parent has no link to the file.  This
                    303:  * is so cvs_file_getpath() works.
                    304:  * Returns the copied file on success, or NULL on failure.  The returned
                    305:  * structure should be freed using cvs_file_free().
                    306:  */
                    307:
                    308: CVSFILE*
                    309: cvs_file_copy(CVSFILE *orig)
                    310: {
                    311:        char path[MAXPATHLEN];
                    312:        CVSFILE *cfp;
                    313:
                    314:        cvs_file_getpath(orig, path, sizeof(path));
                    315:
                    316:        cfp = cvs_file_alloc(path, orig->cf_type);
                    317:        if (cfp == NULL)
                    318:                return (NULL);
                    319:
                    320:        cfp->cf_parent = orig->cf_parent;
                    321:        cfp->cf_mode = orig->cf_mode;
                    322:        cfp->cf_mtime = orig->cf_mtime;
                    323:        cfp->cf_cvstat = orig->cf_cvstat;
                    324:
                    325:        if (orig->cf_type == DT_DIR) {
                    326:                /* XXX copy CVS directory attributes */
                    327:        }
                    328:
                    329:        return (cfp);
                    330: }
                    331:
                    332:
                    333: /*
1.3       jfb       334:  * cvs_file_get()
                    335:  *
                    336:  * Load a cvs_file structure with all the information pertaining to the file
                    337:  * <path>.
1.4       jfb       338:  * The <flags> parameter specifies various flags that alter the behaviour of
1.21      jfb       339:  * the function.  The CF_RECURSE flag causes the function to recursively load
                    340:  * subdirectories when <path> is a directory.
                    341:  * The CF_SORT flag causes the files to be sorted in alphabetical order upon
                    342:  * loading.  The special case of "." as a path specification generates
                    343:  * recursion for a single level and is equivalent to calling cvs_file_get() on
                    344:  * all files of that directory.
1.3       jfb       345:  * Returns a pointer to the cvs file structure, which must later be freed
                    346:  * with cvs_file_free().
                    347:  */
                    348:
1.14      jfb       349: CVSFILE*
1.3       jfb       350: cvs_file_get(const char *path, int flags)
                    351: {
1.14      jfb       352:        return cvs_file_lget(path, flags, NULL);
1.9       jfb       353: }
                    354:
                    355:
                    356: /*
                    357:  * cvs_file_getspec()
                    358:  *
                    359:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    360:  * whose size is given in <fsn>.
                    361:  * Returns a pointer to the lowest common subdirectory to all specified
                    362:  * files.
                    363:  */
                    364:
                    365: CVSFILE*
                    366: cvs_file_getspec(char **fspec, int fsn, int flags)
                    367: {
1.26      jfb       368:        int i;
                    369:        char *sp, *np, pcopy[MAXPATHLEN];
                    370:        CVSFILE *base, *cf, *nf;
                    371:
                    372:        base = cvs_file_get(".", 0);
                    373:        if (base == NULL)
                    374:                return (NULL);
                    375:
                    376:        for (i = 0; i < fsn; i++) {
                    377:                strlcpy(pcopy, fspec[i], sizeof(pcopy));
                    378:                cf = base;
                    379:                sp = pcopy;
                    380:
                    381:                do {
                    382:                        np = strchr(sp, '/');
                    383:                        if (np != NULL)
                    384:                                *np = '\0';
                    385:                        nf = cvs_file_find(cf, sp);
                    386:                        if (nf == NULL) {
                    387:                                nf = cvs_file_lget(pcopy, 0, cf);
                    388:                                if (nf == NULL) {
                    389:                                        cvs_file_free(base);
                    390:                                        return (NULL);
                    391:                                }
                    392:
                    393:                                cvs_file_attach(cf, nf);
                    394:                        }
                    395:
                    396:                        if (np != NULL) {
                    397:                                *np = '/';
                    398:                                sp = np + 1;
                    399:                        }
                    400:
                    401:                        cf = nf;
                    402:                } while (np != NULL);
                    403:        }
                    404:
                    405:        return (base);
1.3       jfb       406: }
                    407:
                    408:
                    409: /*
1.13      jfb       410:  * cvs_file_find()
                    411:  *
                    412:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    413:  * The file's pathname <path> must be relative to the base of <hier>.
                    414:  * Returns the entry on success, or NULL on failure.
                    415:  */
                    416:
                    417: CVSFILE*
                    418: cvs_file_find(CVSFILE *hier, const char *path)
                    419: {
                    420:        char *pp, *sp, pbuf[MAXPATHLEN];
                    421:        CVSFILE *sf, *cf;
                    422:
                    423:        strlcpy(pbuf, path, sizeof(pbuf));
                    424:
                    425:        cf = hier;
                    426:        pp = pbuf;
                    427:        do {
                    428:                sp = strchr(pp, '/');
                    429:                if (sp != NULL)
1.24      jfb       430:                        *(sp++) = '\0';
1.13      jfb       431:
                    432:                /* special case */
                    433:                if (*pp == '.') {
                    434:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    435:                                /* request to go back to parent */
                    436:                                if (cf->cf_parent == NULL) {
                    437:                                        cvs_log(LP_NOTICE,
                    438:                                            "path %s goes back too far", path);
                    439:                                        return (NULL);
                    440:                                }
                    441:                                cf = cf->cf_parent;
                    442:                                continue;
                    443:                        }
                    444:                        else if (*(pp + 1) == '\0')
                    445:                                continue;
                    446:                }
                    447:
                    448:                TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
1.34      jfb       449:                        if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13      jfb       450:                                break;
                    451:                if (sf == NULL)
                    452:                        return (NULL);
                    453:
                    454:                cf = sf;
                    455:                pp = sp;
                    456:        } while (sp != NULL);
                    457:
1.24      jfb       458:        return (cf);
1.13      jfb       459: }
                    460:
                    461:
                    462: /*
1.34      jfb       463:  * cvs_file_getpath()
                    464:  *
                    465:  * Get the full path of the file <file> and store it in <buf>, which is of
                    466:  * size <len>.  For portability, it is recommended that <buf> always be
                    467:  * at least MAXPATHLEN bytes long.
                    468:  * Returns a pointer to the start of the path on success, or NULL on failure.
                    469:  */
                    470:
                    471: char*
                    472: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
                    473: {
                    474:        u_int i;
                    475:        char *fp, *namevec[CVS_FILE_MAXDEPTH];
                    476:        CVSFILE *top;
                    477:
                    478:        buf[0] = '\0';
                    479:        i = CVS_FILE_MAXDEPTH;
                    480:        memset(namevec, 0, sizeof(namevec));
                    481:
                    482:        /* find the top node */
                    483:        for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
                    484:                fp = CVS_FILE_NAME(top);
                    485:
                    486:                /* skip self-references */
                    487:                if ((fp[0] == '.') && (fp[1] == '\0'))
                    488:                        continue;
                    489:                namevec[--i] = fp;
                    490:        }
                    491:
                    492:        if (i == 0)
                    493:                return (NULL);
                    494:        else if (i == CVS_FILE_MAXDEPTH) {
                    495:                strlcpy(buf, ".", len);
                    496:                return (buf);
                    497:        }
                    498:
                    499:        while (i < CVS_FILE_MAXDEPTH - 1) {
                    500:                strlcat(buf, namevec[i++], len);
                    501:                strlcat(buf, "/", len);
                    502:        }
                    503:        strlcat(buf, namevec[i], len);
                    504:
                    505:        return (buf);
                    506: }
                    507:
                    508:
                    509: /*
1.22      jfb       510:  * cvs_file_attach()
                    511:  *
                    512:  * Attach the file <file> as one of the children of parent <parent>, which
                    513:  * has to be a file of type DT_DIR.
                    514:  * Returns 0 on success, or -1 on failure.
                    515:  */
                    516:
                    517: int
                    518: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
                    519: {
1.23      jfb       520:        struct cvs_dir *dp;
1.22      jfb       521:
                    522:        if (parent->cf_type != DT_DIR)
                    523:                return (-1);
                    524:
1.23      jfb       525:        dp = parent->cf_ddat;
                    526:
                    527:        TAILQ_INSERT_TAIL(&(dp->cd_files), file, cf_list);
                    528:        dp->cd_nfiles++;
1.22      jfb       529:        file->cf_parent = parent;
                    530:
                    531:        return (0);
                    532: }
                    533:
                    534:
                    535: /*
1.3       jfb       536:  * cvs_file_getdir()
                    537:  *
                    538:  * Get a cvs directory structure for the directory whose path is <dir>.
1.30      jfb       539:  * This function should not free the directory information on error, as this
                    540:  * is performed by cvs_file_free().
1.3       jfb       541:  */
                    542:
1.6       jfb       543: static int
1.14      jfb       544: cvs_file_getdir(CVSFILE *cf, int flags)
1.3       jfb       545: {
1.13      jfb       546:        int ret, fd;
1.16      jfb       547:        u_int ndirs;
1.3       jfb       548:        long base;
1.13      jfb       549:        void *dp, *ep;
1.34      jfb       550:        char fbuf[2048], pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
1.3       jfb       551:        struct dirent *ent;
1.14      jfb       552:        CVSFILE *cfp;
1.20      jfb       553:        struct stat st;
1.3       jfb       554:        struct cvs_dir *cdp;
1.10      jfb       555:        struct cvs_flist dirs;
1.3       jfb       556:
1.18      jfb       557:        ndirs = 0;
1.10      jfb       558:        TAILQ_INIT(&dirs);
1.7       jfb       559:        cdp = cf->cf_ddat;
                    560:
1.34      jfb       561:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    562:
1.26      jfb       563:        if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
1.34      jfb       564:                cdp->cd_root = cvsroot_get(fpath);
1.30      jfb       565:                if (cdp->cd_root == NULL)
1.26      jfb       566:                        return (-1);
1.3       jfb       567:
1.26      jfb       568:                if (flags & CF_MKADMIN)
                    569:                        cvs_mkadmin(cf, 0755);
1.14      jfb       570:
1.26      jfb       571:                /* if the CVS administrative directory exists, load the info */
1.34      jfb       572:                snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
1.26      jfb       573:                if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
1.34      jfb       574:                        if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
1.26      jfb       575:                                cdp->cd_repo = strdup(pbuf);
                    576:                                if (cdp->cd_repo == NULL) {
1.28      jfb       577:                                        cvs_log(LP_ERRNO,
                    578:                                            "failed to dup repository string");
1.26      jfb       579:                                        return (-1);
                    580:                                }
1.20      jfb       581:                        }
1.26      jfb       582:
1.37    ! jfb       583:                        cdp->cd_ent = cvs_ent_open(fpath, O_RDONLY);
1.20      jfb       584:                }
                    585:        }
1.14      jfb       586:
1.26      jfb       587:        if (!(flags & CF_RECURSE) || (cf->cf_cvstat == CVS_FST_UNKNOWN))
                    588:                return (0);
                    589:
1.34      jfb       590:        fd = open(fpath, O_RDONLY);
1.3       jfb       591:        if (fd == -1) {
1.34      jfb       592:                cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
1.6       jfb       593:                return (-1);
1.3       jfb       594:        }
                    595:
1.11      jfb       596:        do {
                    597:                ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    598:                if (ret == -1) {
                    599:                        cvs_log(LP_ERRNO, "failed to get directory entries");
                    600:                        (void)close(fd);
                    601:                        return (-1);
                    602:                }
1.10      jfb       603:
1.11      jfb       604:                dp = fbuf;
                    605:                ep = fbuf + (size_t)ret;
                    606:                while (dp < ep) {
                    607:                        ent = (struct dirent *)dp;
1.32      jfb       608:                        dp += ent->d_reclen;
1.31      jfb       609:                        if (ent->d_fileno == 0)
                    610:                                continue;
1.11      jfb       611:
                    612:                        if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.24      jfb       613:                                continue;
                    614:
                    615:                        if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
1.11      jfb       616:                                continue;
                    617:
1.34      jfb       618:                        snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
                    619:                            ent->d_name);
1.14      jfb       620:                        cfp = cvs_file_lget(pbuf, flags, cf);
1.11      jfb       621:                        if (cfp != NULL) {
1.26      jfb       622:                                if (cfp->cf_type == DT_DIR) {
                    623:                                        TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.16      jfb       624:                                        ndirs++;
                    625:                                }
                    626:                                else {
1.26      jfb       627:                                        TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
1.11      jfb       628:                                            cf_list);
1.16      jfb       629:                                        cdp->cd_nfiles++;
                    630:                                }
1.11      jfb       631:                        }
1.4       jfb       632:                }
1.11      jfb       633:        } while (ret > 0);
1.14      jfb       634:
1.34      jfb       635: #if 1
                    636:        cvs_ent_close(cdp->cd_ent);
                    637:        cdp->cd_ent = NULL;
                    638: #endif
                    639:
1.10      jfb       640:        if (flags & CF_SORT) {
1.16      jfb       641:                cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
                    642:                cvs_file_sort(&dirs, ndirs);
1.10      jfb       643:        }
1.26      jfb       644:
                    645:        while (!TAILQ_EMPTY(&dirs)) {
                    646:                cfp = TAILQ_FIRST(&dirs);
                    647:                TAILQ_REMOVE(&dirs, cfp, cf_list);
1.10      jfb       648:                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.26      jfb       649:        }
1.16      jfb       650:        cdp->cd_nfiles += ndirs;
1.3       jfb       651:
                    652:        (void)close(fd);
                    653:
1.6       jfb       654:        return (0);
1.3       jfb       655: }
                    656:
                    657:
                    658: /*
                    659:  * cvs_file_free()
                    660:  *
                    661:  * Free a cvs_file structure and its contents.
                    662:  */
                    663:
                    664: void
1.14      jfb       665: cvs_file_free(CVSFILE *cf)
1.3       jfb       666: {
                    667:        if (cf->cf_ddat != NULL)
                    668:                cvs_file_freedir(cf->cf_ddat);
                    669:        free(cf);
1.5       jfb       670: }
                    671:
                    672:
                    673: /*
                    674:  * cvs_file_examine()
                    675:  *
                    676:  * Examine the contents of the CVS file structure <cf> with the function
                    677:  * <exam>.  The function is called for all subdirectories and files of the
                    678:  * root file.
                    679:  */
                    680:
                    681: int
                    682: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    683: {
                    684:        int ret;
1.14      jfb       685:        CVSFILE *fp;
1.5       jfb       686:
                    687:        if (cf->cf_type == DT_DIR) {
                    688:                ret = (*exam)(cf, arg);
1.10      jfb       689:                TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5       jfb       690:                        ret = cvs_file_examine(fp, exam, arg);
                    691:                        if (ret == -1)
1.13      jfb       692:                                break;
1.5       jfb       693:                }
                    694:        }
                    695:        else
1.13      jfb       696:                ret = (*exam)(cf, arg);
                    697:
                    698:        return (ret);
1.3       jfb       699: }
                    700:
                    701:
                    702: /*
                    703:  * cvs_file_freedir()
                    704:  *
                    705:  * Free a cvs_dir structure and its contents.
                    706:  */
                    707:
                    708: static void
                    709: cvs_file_freedir(struct cvs_dir *cd)
                    710: {
1.14      jfb       711:        CVSFILE *cfp;
1.3       jfb       712:
                    713:        if (cd->cd_root != NULL)
                    714:                cvsroot_free(cd->cd_root);
                    715:        if (cd->cd_repo != NULL)
                    716:                free(cd->cd_repo);
                    717:
1.14      jfb       718:        if (cd->cd_ent != NULL)
                    719:                cvs_ent_close(cd->cd_ent);
                    720:
1.10      jfb       721:        while (!TAILQ_EMPTY(&(cd->cd_files))) {
                    722:                cfp = TAILQ_FIRST(&(cd->cd_files));
                    723:                TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3       jfb       724:                cvs_file_free(cfp);
                    725:        }
                    726: }
                    727:
                    728:
                    729: /*
                    730:  * cvs_file_sort()
                    731:  *
1.16      jfb       732:  * Sort a list of cvs file structures according to their filename.  The list
                    733:  * <flp> is modified according to the sorting algorithm.  The number of files
                    734:  * in the list must be given by <nfiles>.
                    735:  * Returns 0 on success, or -1 on failure.
1.3       jfb       736:  */
                    737:
                    738: static int
1.16      jfb       739: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb       740: {
                    741:        int i;
                    742:        size_t nb;
1.16      jfb       743:        CVSFILE *cf, **cfvec;
                    744:
                    745:        cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
                    746:        if (cfvec == NULL) {
                    747:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                    748:                return (-1);
                    749:        }
1.3       jfb       750:
                    751:        i = 0;
1.10      jfb       752:        TAILQ_FOREACH(cf, flp, cf_list) {
1.16      jfb       753:                if (i == (int)nfiles) {
1.3       jfb       754:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb       755:                        /* rebuild the list and abort sorting */
                    756:                        while (--i >= 0)
                    757:                                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
                    758:                        free(cfvec);
1.3       jfb       759:                        return (-1);
                    760:                }
1.16      jfb       761:                cfvec[i++] = cf;
1.3       jfb       762:
                    763:                /* now unlink it from the list,
                    764:                 * we'll put it back in order later
                    765:                 */
1.10      jfb       766:                TAILQ_REMOVE(flp, cf, cf_list);
1.3       jfb       767:        }
                    768:
                    769:        /* clear the list just in case */
1.10      jfb       770:        TAILQ_INIT(flp);
1.3       jfb       771:        nb = (size_t)i;
                    772:
                    773:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    774:
                    775:        /* rebuild the list from the bottom up */
                    776:        for (i = (int)nb - 1; i >= 0; i--)
1.10      jfb       777:                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       778:
1.16      jfb       779:        free(cfvec);
1.3       jfb       780:        return (0);
                    781: }
                    782:
                    783:
                    784: static int
                    785: cvs_file_cmp(const void *f1, const void *f2)
                    786: {
1.14      jfb       787:        CVSFILE *cf1, *cf2;
                    788:        cf1 = *(CVSFILE **)f1;
                    789:        cf2 = *(CVSFILE **)f2;
1.34      jfb       790:        return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6       jfb       791: }
                    792:
                    793:
1.34      jfb       794: /*
                    795:  * cvs_file_alloc()
                    796:  *
                    797:  * Allocate a CVSFILE structure and initialize its internals.
                    798:  */
                    799:
1.6       jfb       800: CVSFILE*
                    801: cvs_file_alloc(const char *path, u_int type)
                    802: {
                    803:        size_t len;
                    804:        char pbuf[MAXPATHLEN];
1.34      jfb       805:        const char *fnp;
1.6       jfb       806:        CVSFILE *cfp;
                    807:        struct cvs_dir *ddat;
                    808:
1.14      jfb       809:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb       810:        if (cfp == NULL) {
                    811:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    812:                return (NULL);
                    813:        }
                    814:        memset(cfp, 0, sizeof(*cfp));
                    815:
                    816:        /* ditch trailing slashes */
                    817:        strlcpy(pbuf, path, sizeof(pbuf));
                    818:        len = strlen(pbuf);
                    819:        while (pbuf[len - 1] == '/')
                    820:                pbuf[--len] = '\0';
                    821:
1.34      jfb       822:        fnp = strrchr(path, '/');
                    823:        if (fnp == NULL)
                    824:                fnp = path;
                    825:        else
                    826:                fnp++;
                    827:
                    828:        cfp->cf_name = cvs_file_getname(fnp);
                    829:        if (cfp->cf_name == NULL) {
1.35      jfb       830:                cvs_log(LP_ERR, "failed to get file name from table");
1.6       jfb       831:                return (NULL);
                    832:        }
                    833:        cfp->cf_type = type;
                    834:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    835:
                    836:        if (type == DT_DIR) {
                    837:                ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
                    838:                if (ddat == NULL) {
                    839:                        cvs_file_free(cfp);
                    840:                        return (NULL);
                    841:                }
                    842:                memset(ddat, 0, sizeof(*ddat));
1.10      jfb       843:                TAILQ_INIT(&(ddat->cd_files));
1.6       jfb       844:                cfp->cf_ddat = ddat;
                    845:        }
                    846:        return (cfp);
1.1       jfb       847: }
1.14      jfb       848:
                    849:
                    850: /*
                    851:  * cvs_file_lget()
                    852:  *
                    853:  * Get the file and link it with the parent right away.
1.22      jfb       854:  * Returns a pointer to the created file structure on success, or NULL on
                    855:  * failure.
1.14      jfb       856:  */
                    857:
                    858: static CVSFILE*
                    859: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
                    860: {
                    861:        int cwd;
                    862:        struct stat st;
                    863:        CVSFILE *cfp;
1.34      jfb       864:        struct cvs_ent *ent = NULL;
1.14      jfb       865:
                    866:        if (strcmp(path, ".") == 0)
                    867:                cwd = 1;
                    868:        else
                    869:                cwd = 0;
                    870:
                    871:        if (stat(path, &st) == -1) {
                    872:                cvs_log(LP_ERRNO, "failed to stat %s", path);
                    873:                return (NULL);
                    874:        }
                    875:
                    876:        cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
                    877:        if (cfp == NULL) {
                    878:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    879:                return (NULL);
                    880:        }
                    881:        cfp->cf_parent = parent;
1.22      jfb       882:        cfp->cf_mode = st.st_mode & ACCESSPERMS;
                    883:        cfp->cf_mtime = st.st_mtime;
1.14      jfb       884:
                    885:        if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL)) {
1.34      jfb       886:                ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), CVS_FILE_NAME(cfp));
1.14      jfb       887:        }
                    888:
1.26      jfb       889:        if (ent == NULL) {
1.14      jfb       890:                cfp->cf_cvstat = (cwd == 1) ?
                    891:                    CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.26      jfb       892:        }
1.14      jfb       893:        else {
                    894:                /* always show directories as up-to-date */
                    895:                if (ent->ce_type == CVS_ENT_DIR)
                    896:                        cfp->cf_cvstat = CVS_FST_UPTODATE;
                    897:                else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                    898:                        cfp->cf_cvstat = CVS_FST_ADDED;
                    899:                else {
                    900:                        /* check last modified time */
1.36      jfb       901:                        if (ent->ce_mtime >= (time_t)st.st_mtime)
1.14      jfb       902:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
                    903:                        else
                    904:                                cfp->cf_cvstat = CVS_FST_MODIFIED;
                    905:                }
                    906:        }
                    907:
1.26      jfb       908:        if ((cfp->cf_type == DT_DIR) && (cvs_file_getdir(cfp, flags) < 0)) {
                    909:                cvs_file_free(cfp);
                    910:                return (NULL);
1.14      jfb       911:        }
                    912:
                    913:        return (cfp);
1.23      jfb       914: }
                    915:
                    916:
                    917: static int
                    918: cvs_file_cmpname(const char *name1, const char *name2)
                    919: {
                    920:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    921:            (strcasecmp(name1, name2));
1.34      jfb       922: }
                    923:
                    924:
                    925: /*
                    926:  * cvs_file_hashname()
                    927:  *
                    928:  * Generate an 8 bit hash value from the name of a file.
                    929:  * XXX Improve my distribution!
                    930:  */
                    931:
                    932: static u_int8_t
                    933: cvs_file_hashname(const char *name)
                    934: {
                    935:        const char *np;
                    936:        u_int8_t h;
                    937:
                    938:        h = 0xb5;
                    939:        for (np = name; *np != '\0'; np++)
                    940:                h ^= (*np << 3 ^ *np >> 1);
                    941:
                    942:        return (h);
                    943: }
                    944:
                    945:
                    946: /*
                    947:  * cvs_file_getname()
                    948:  *
                    949:  * Look for the file name <name> in the filename hash table.
                    950:  * If no entry is found for that name, a new one is created and inserted into
                    951:  * the table.  The name's reference count is increased.
                    952:  */
                    953:
                    954: static struct cvs_fname*
                    955: cvs_file_getname(const char *name)
                    956: {
                    957:        u_int8_t h;
                    958:        struct cvs_fname *fnp;
                    959:
                    960:        h = cvs_file_hashname(name);
                    961:
                    962:        SLIST_FOREACH(fnp, &(cvs_fnht[h]), cf_list)
                    963:                if (strcmp(name, fnp->cf_name) == 0) {
                    964:                        fnp->cf_ref++;
                    965:                        break;
                    966:                }
                    967:
                    968:        if (fnp == NULL) {
                    969:                fnp = (struct cvs_fname *)malloc(sizeof(*fnp));
                    970:                if (fnp == NULL) {
                    971:                        cvs_log(LP_ERRNO,
                    972:                            "failed to allocate new file name entry");
                    973:                        return (NULL);
                    974:                }
                    975:
                    976:                fnp->cf_name = strdup(name);
                    977:                fnp->cf_ref = 1;
                    978:                SLIST_INSERT_HEAD(&(cvs_fnht[h]), fnp, cf_list);
                    979:        }
                    980:
                    981:        return (fnp);
1.14      jfb       982: }