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

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