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

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