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

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