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

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