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

1.133   ! joris       1: /*     $OpenBSD: file.c,v 1.132 2005/12/10 20:27:45 joris 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:
1.83      xsa        31: #include <dirent.h>
1.1       jfb        32: #include <errno.h>
                     33: #include <fcntl.h>
1.83      xsa        34: #include <fnmatch.h>
1.51      jfb        35: #include <libgen.h>
1.83      xsa        36: #include <pwd.h>
                     37: #include <stdio.h>
1.1       jfb        38: #include <stdlib.h>
1.83      xsa        39: #include <string.h>
1.1       jfb        40: #include <unistd.h>
                     41:
                     42: #include "cvs.h"
1.83      xsa        43: #include "file.h"
1.1       jfb        44: #include "log.h"
                     45:
1.104     xsa        46: #define CVS_IGN_STATIC 0x01    /* pattern is static, no need to glob */
1.1       jfb        47:
1.104     xsa        48: #define CVS_CHAR_ISMETA(c)     ((c == '*') || (c == '?') || (c == '['))
1.1       jfb        49:
                     50:
                     51: /* ignore pattern */
                     52: struct cvs_ignpat {
1.104     xsa        53:        char                            ip_pat[MAXNAMLEN];
                     54:        int                             ip_flags;
1.124     reyk       55:        TAILQ_ENTRY(cvs_ignpat)         ip_list;
1.1       jfb        56: };
                     57:
                     58:
                     59: /*
                     60:  * Standard patterns to ignore.
                     61:  */
                     62: static const char *cvs_ign_std[] = {
                     63:        ".",
                     64:        "..",
                     65:        "*.o",
                     66:        "*.so",
1.45      xsa        67:        "*.a",
1.1       jfb        68:        "*.bak",
                     69:        "*.orig",
                     70:        "*.rej",
1.45      xsa        71:        "*.old",
1.1       jfb        72:        "*.exe",
                     73:        "*.depend",
1.45      xsa        74:        "*.obj",
                     75:        "*.elc",
                     76:        "*.ln",
                     77:        "*.olb",
1.1       jfb        78:        "CVS",
                     79:        "core",
1.102     joris      80:        "cvslog*",
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: /*
                    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.104     xsa       106: TAILQ_HEAD(, cvs_ignpat)       cvs_ign_pats;
1.1       jfb       107:
1.100     joris     108: static int cvs_file_getdir(CVSFILE *, int, int (*)(CVSFILE *, void *),
                    109:     void *, int);
1.1       jfb       110:
1.104     xsa       111: static int      cvs_load_dirinfo(CVSFILE *, int);
                    112: static int      cvs_file_sort(struct cvs_flist *, u_int);
                    113: static int      cvs_file_cmp(const void *, const void *);
                    114: static int      cvs_file_cmpname(const char *, const char *);
                    115: static CVSFILE *cvs_file_alloc(const char *, u_int);
1.115     joris     116: static CVSFILE *cvs_file_lget(const char *, int, CVSFILE *, CVSENTRIES *,
                    117:                    struct cvs_ent *);
1.3       jfb       118:
                    119:
1.1       jfb       120: /*
                    121:  * cvs_file_init()
                    122:  *
                    123:  */
                    124: int
                    125: cvs_file_init(void)
                    126: {
1.61      xsa       127:        int i, l;
1.1       jfb       128:        size_t len;
                    129:        char path[MAXPATHLEN], buf[MAXNAMLEN];
                    130:        FILE *ifp;
                    131:
                    132:        TAILQ_INIT(&cvs_ign_pats);
                    133:
1.53      jfb       134:        if ((cvs_addedrev = rcsnum_parse("0")) == NULL)
1.46      jfb       135:                return (-1);
1.4       jfb       136:
1.1       jfb       137:        /* standard patterns to ignore */
1.10      jfb       138:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.26      jfb       139:                cvs_file_ignore(cvs_ign_std[i]);
1.1       jfb       140:
                    141:        /* read the cvsignore file in the user's home directory, if any */
1.111     xsa       142:        l = snprintf(path, sizeof(path), "%s/.cvsignore", cvs_homedir);
                    143:        if (l == -1 || l >= (int)sizeof(path)) {
                    144:                errno = ENAMETOOLONG;
                    145:                cvs_log(LP_ERRNO, "%s", path);
                    146:                return (-1);
                    147:        }
1.61      xsa       148:
1.111     xsa       149:        ifp = fopen(path, "r");
                    150:        if (ifp == NULL) {
                    151:                if (errno != ENOENT)
                    152:                        cvs_log(LP_ERRNO,
                    153:                            "failed to open user's cvsignore file `%s'", path);
                    154:        } else {
1.112     xsa       155:                while (fgets(buf, (int)sizeof(buf), ifp) != NULL) {
1.111     xsa       156:                        len = strlen(buf);
                    157:                        if (len == 0)
                    158:                                continue;
                    159:                        if (buf[len - 1] != '\n') {
                    160:                                cvs_log(LP_ERR, "line too long in `%s'", path);
1.1       jfb       161:                        }
1.111     xsa       162:                        buf[--len] = '\0';
                    163:                        cvs_file_ignore(buf);
1.1       jfb       164:                }
1.111     xsa       165:                (void)fclose(ifp);
1.1       jfb       166:        }
                    167:
                    168:        return (0);
                    169: }
                    170:
                    171:
                    172: /*
                    173:  * cvs_file_ignore()
                    174:  *
                    175:  * Add the pattern <pat> to the list of patterns for files to ignore.
                    176:  * Returns 0 on success, or -1 on failure.
                    177:  */
                    178: int
                    179: cvs_file_ignore(const char *pat)
                    180: {
                    181:        char *cp;
                    182:        struct cvs_ignpat *ip;
                    183:
1.132     joris     184:        ip = (struct cvs_ignpat *)xmalloc(sizeof(*ip));
1.1       jfb       185:        strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    186:
                    187:        /* check if we will need globbing for that pattern */
                    188:        ip->ip_flags = CVS_IGN_STATIC;
                    189:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    190:                if (CVS_CHAR_ISMETA(*cp)) {
                    191:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    192:                        break;
                    193:                }
                    194:        }
                    195:
                    196:        TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
                    197:
                    198:        return (0);
                    199: }
                    200:
                    201:
                    202: /*
1.5       jfb       203:  * cvs_file_chkign()
1.1       jfb       204:  *
                    205:  * Returns 1 if the filename <file> is matched by one of the ignore
                    206:  * patterns, or 0 otherwise.
                    207:  */
                    208: int
1.5       jfb       209: cvs_file_chkign(const char *file)
1.1       jfb       210: {
1.23      jfb       211:        int flags;
1.1       jfb       212:        struct cvs_ignpat *ip;
                    213:
1.23      jfb       214:        flags = FNM_PERIOD;
                    215:        if (cvs_nocase)
                    216:                flags |= FNM_CASEFOLD;
                    217:
1.1       jfb       218:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    219:                if (ip->ip_flags & CVS_IGN_STATIC) {
1.23      jfb       220:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1       jfb       221:                                return (1);
1.38      deraadt   222:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1       jfb       223:                        return (1);
                    224:        }
                    225:
                    226:        return (0);
                    227: }
                    228:
                    229:
                    230: /*
1.6       jfb       231:  * cvs_file_create()
1.1       jfb       232:  *
1.6       jfb       233:  * Create a new file whose path is specified in <path> and of type <type>.
1.26      jfb       234:  * If the type is DT_DIR, the CVS administrative repository and files will be
                    235:  * created.
1.25      jfb       236:  * Returns the created file on success, or NULL on failure.
1.1       jfb       237:  */
1.109     xsa       238: CVSFILE *
1.34      jfb       239: cvs_file_create(CVSFILE *parent, const char *path, u_int type, mode_t mode)
1.1       jfb       240: {
1.85      joris     241:        int fd, l;
                    242:        char fp[MAXPATHLEN], repo[MAXPATHLEN];
1.6       jfb       243:        CVSFILE *cfp;
1.1       jfb       244:
1.6       jfb       245:        cfp = cvs_file_alloc(path, type);
                    246:        if (cfp == NULL)
                    247:                return (NULL);
1.26      jfb       248:
1.100     joris     249:        l = 0;
1.22      jfb       250:        cfp->cf_mode = mode;
1.34      jfb       251:        cfp->cf_parent = parent;
1.1       jfb       252:
1.34      jfb       253:        if (type == DT_DIR) {
1.62      jfb       254:                cfp->cf_root = cvsroot_get(path);
1.84      joris     255:                if (cfp->cf_root == NULL) {
1.100     joris     256:                        cvs_file_free(cfp);
1.84      joris     257:                        return (NULL);
                    258:                }
                    259:
1.85      joris     260:                if (cvs_repo_base != NULL) {
                    261:                        cvs_file_getpath(cfp, fp, sizeof(fp));
                    262:                        l = snprintf(repo, sizeof(repo), "%s/%s", cvs_repo_base,
                    263:                            fp);
                    264:                } else {
                    265:                        cvs_file_getpath(cfp, repo, sizeof(repo));
                    266:                        l = 0;
                    267:                }
                    268:
                    269:                if (l == -1 || l >= (int)sizeof(repo)) {
                    270:                        errno = ENAMETOOLONG;
                    271:                        cvs_log(LP_ERRNO, "%s", repo);
                    272:                        cvs_file_free(cfp);
                    273:                        return (NULL);
                    274:                }
                    275:
1.132     joris     276:                cfp->cf_repo = xstrdup(repo);
1.79      joris     277:                if (((mkdir(path, mode) == -1) && (errno != EEXIST)) ||
1.122     xsa       278:                    (cvs_mkadmin(path, cfp->cf_root->cr_str, cfp->cf_repo,
1.124     reyk      279:                    NULL, NULL, 0) < 0)) {
1.6       jfb       280:                        cvs_file_free(cfp);
                    281:                        return (NULL);
1.50      jfb       282:                }
1.38      deraadt   283:        } else {
1.6       jfb       284:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    285:                if (fd == -1) {
                    286:                        cvs_file_free(cfp);
1.1       jfb       287:                        return (NULL);
                    288:                }
1.6       jfb       289:                (void)close(fd);
1.1       jfb       290:        }
                    291:
1.6       jfb       292:        return (cfp);
1.3       jfb       293: }
                    294:
                    295:
                    296: /*
1.35      jfb       297:  * cvs_file_copy()
                    298:  *
                    299:  * Allocate space to create a copy of the file <orig>.  The copy inherits all
                    300:  * of the original's attributes, but does not inherit its children if the
                    301:  * original file is a directory.  Note that files copied using this mechanism
                    302:  * are linked to their parent, but the parent has no link to the file.  This
                    303:  * is so cvs_file_getpath() works.
                    304:  * Returns the copied file on success, or NULL on failure.  The returned
                    305:  * structure should be freed using cvs_file_free().
                    306:  */
1.109     xsa       307: CVSFILE *
1.35      jfb       308: cvs_file_copy(CVSFILE *orig)
                    309: {
                    310:        char path[MAXPATHLEN];
                    311:        CVSFILE *cfp;
                    312:
                    313:        cvs_file_getpath(orig, path, sizeof(path));
                    314:
                    315:        cfp = cvs_file_alloc(path, orig->cf_type);
                    316:        if (cfp == NULL)
                    317:                return (NULL);
                    318:
                    319:        cfp->cf_parent = orig->cf_parent;
                    320:        cfp->cf_mode = orig->cf_mode;
                    321:        cfp->cf_cvstat = orig->cf_cvstat;
                    322:
1.94      joris     323:        if (orig->cf_type == DT_REG) {
                    324:                cfp->cf_etime = orig->cf_etime;
1.62      jfb       325:                cfp->cf_mtime = orig->cf_mtime;
1.94      joris     326:        } else if (orig->cf_type == DT_DIR) {
1.35      jfb       327:                /* XXX copy CVS directory attributes */
                    328:        }
                    329:
                    330:        return (cfp);
                    331: }
                    332:
                    333:
                    334: /*
1.3       jfb       335:  * cvs_file_get()
                    336:  *
                    337:  * Load a cvs_file structure with all the information pertaining to the file
                    338:  * <path>.
1.4       jfb       339:  * The <flags> parameter specifies various flags that alter the behaviour of
1.21      jfb       340:  * the function.  The CF_RECURSE flag causes the function to recursively load
                    341:  * subdirectories when <path> is a directory.
                    342:  * The CF_SORT flag causes the files to be sorted in alphabetical order upon
                    343:  * loading.  The special case of "." as a path specification generates
                    344:  * recursion for a single level and is equivalent to calling cvs_file_get() on
                    345:  * all files of that directory.
1.3       jfb       346:  * Returns a pointer to the cvs file structure, which must later be freed
                    347:  * with cvs_file_free().
                    348:  */
                    349:
1.100     joris     350: int
1.73      joris     351: cvs_file_get(const char *path, int flags, int (*cb)(CVSFILE *, void *),
1.100     joris     352:     void *arg, struct cvs_flist *list)
1.3       jfb       353: {
1.68      joris     354:        char *files[1];
                    355:
1.105     joris     356:        *(const char **)files = path;
1.100     joris     357:        return cvs_file_getspec(files, 1, flags, cb, arg, list);
1.9       jfb       358: }
                    359:
                    360:
                    361: /*
                    362:  * cvs_file_getspec()
                    363:  *
1.100     joris     364:  * Obtain the info about the supplied files or directories.
1.9       jfb       365:  */
1.100     joris     366: int
1.73      joris     367: cvs_file_getspec(char **fspec, int fsn, int flags, int (*cb)(CVSFILE *, void *),
1.100     joris     368:     void *arg, struct cvs_flist *list)
1.9       jfb       369: {
1.100     joris     370:        int i, freecf;
                    371:        char pcopy[MAXPATHLEN];
                    372:        CVSFILE *cf;
                    373:        extern char *cvs_rootstr;
                    374:
                    375:        freecf = (list == NULL);
                    376:        cvs_error = CVS_EX_DATA;
                    377:
                    378:        /* init the list */
                    379:        if (list != NULL)
                    380:                SIMPLEQ_INIT(list);
1.26      jfb       381:
1.100     joris     382:        /*
                    383:         * Fetch the needed information about ".", so we can setup a few
                    384:         * things to get ourselfs going.
                    385:         */
1.115     joris     386:        cf = cvs_file_lget(".", 0, NULL, NULL, NULL);
1.100     joris     387:        if (cf == NULL) {
1.125     joris     388:                cvs_log(LP_ERR, "failed to obtain '.' information");
1.100     joris     389:                return (-1);
                    390:        }
1.87      joris     391:
1.85      joris     392:        /*
1.100     joris     393:         * save the base repository path so we can use it to create
                    394:         * the correct full repopath later on.
1.85      joris     395:         */
1.100     joris     396:        if (cf->cf_repo != NULL) {
                    397:                if (cvs_repo_base != NULL)
1.132     joris     398:                        xfree(cvs_repo_base);
                    399:                cvs_repo_base = xstrdup(cf->cf_repo);
1.85      joris     400:        }
1.26      jfb       401:
1.95      joris     402:        /*
1.100     joris     403:         * This will go away when we have support for multiple Roots.
1.95      joris     404:         */
1.100     joris     405:        if (cvs_rootstr == NULL && cf->cf_root != NULL) {
1.132     joris     406:                cvs_rootstr = xstrdup(cf->cf_root->cr_str);
1.73      joris     407:        }
                    408:
1.100     joris     409:        cvs_error = CVS_EX_OK;
                    410:
                    411:        /*
                    412:         * Since some commands don't require any files to operate
                    413:         * we can stop right here for those.
                    414:         */
1.128     joris     415:        if (cf->cf_root != NULL) {
                    416:                if (cf->cf_root->cr_method != CVS_METHOD_LOCAL &&
                    417:                    cvs_cmdop == CVS_OP_CHECKOUT) {
                    418:                        cvs_file_free(cf);
                    419:                        return (0);
                    420:                }
                    421:        }
                    422:
                    423:        cvs_file_free(cf);
                    424:
                    425:        if (cvs_cmdop == CVS_OP_VERSION)
1.100     joris     426:                return (0);
                    427:
1.26      jfb       428:        for (i = 0; i < fsn; i++) {
                    429:                strlcpy(pcopy, fspec[i], sizeof(pcopy));
1.123     joris     430:
                    431:                /*
                    432:                 * get rid of any trailing slashes.
                    433:                 */
                    434:                STRIP_SLASH(pcopy);
1.26      jfb       435:
1.100     joris     436:                /*
                    437:                 * Load the information.
                    438:                 */
                    439:                cf = cvs_file_loadinfo(pcopy, flags, cb, arg, freecf);
1.108     joris     440:                if (cf == NULL) {
                    441:                        if (cvs_error != CVS_EX_OK)
1.126     joris     442:                                return (-1);
1.100     joris     443:                        continue;
1.108     joris     444:                }
1.68      joris     445:
1.100     joris     446:                /*
                    447:                 * If extra actions are needed, do them now.
                    448:                 */
                    449:                if (cf->cf_type == DT_DIR) {
                    450:                        /* do possible extra actions .. */
1.68      joris     451:                } else {
1.100     joris     452:                        /* do possible extra actions .. */
                    453:                }
                    454:
                    455:                /*
                    456:                 * Attach it to a list if requested, otherwise
                    457:                 * just free it again.
                    458:                 */
                    459:                if (list != NULL)
                    460:                        SIMPLEQ_INSERT_TAIL(list, cf, cf_list);
                    461:                else
                    462:                        cvs_file_free(cf);
                    463:        }
                    464:
                    465:        return (0);
                    466: }
                    467:
                    468: /*
                    469:  * Load the neccesary information about file or directory <path>.
                    470:  * Returns a pointer to the loaded information on success, or NULL
                    471:  * on failure.
                    472:  *
                    473:  * If cb is not NULL, the requested path will be passed to that callback
                    474:  * with <arg> as an argument.
                    475:  *
                    476:  * the <freecf> argument is passed to cvs_file_getdir, if this is 1
                    477:  * CVSFILE * structs will be free'd once we are done with them.
                    478:  */
1.114     xsa       479: CVSFILE *
1.100     joris     480: cvs_file_loadinfo(char *path, int flags, int (*cb)(CVSFILE *, void *),
                    481:     void *arg, int freecf)
                    482: {
                    483:        CVSFILE *cf, *base;
                    484:        CVSENTRIES *entf;
                    485:        struct cvs_ent *ent;
                    486:        char *p;
                    487:        char parent[MAXPATHLEN], item[MAXPATHLEN];
1.120     joris     488:        int type, callit;
1.100     joris     489:        struct stat st;
                    490:        struct cvsroot *root;
                    491:
                    492:        type = 0;
                    493:        base = cf = NULL;
                    494:        entf = NULL;
                    495:        ent = NULL;
                    496:
                    497:        /*
                    498:         * We first have to find out what type of item we are
                    499:         * dealing with. A file or a directory.
                    500:         *
                    501:         * We can do this by stat(2)'ing the item, but since it
                    502:         * might be gone we also check the Entries file in the
                    503:         * parent directory.
                    504:         */
                    505:
                    506:        /* get parent directory */
                    507:        if ((p = strrchr(path, '/')) != NULL) {
                    508:                *p++ = '\0';
                    509:                strlcpy(parent, path, sizeof(parent));
                    510:                strlcpy(item, p, sizeof(item));
                    511:                *--p = '/';
                    512:        } else {
                    513:                strlcpy(parent, ".", sizeof(parent));
                    514:                strlcpy(item, path, sizeof(item));
                    515:        }
                    516:
                    517:        /*
                    518:         * There might not be an Entries file, so do not fail if there
                    519:         * is none available to get the info from.
                    520:         */
1.115     joris     521:        entf = cvs_ent_open(parent, O_RDWR);
1.100     joris     522:
                    523:        /*
                    524:         * Load the Entry if we successfully opened the Entries file.
                    525:         */
                    526:        if (entf != NULL)
                    527:                ent = cvs_ent_get(entf, item);
                    528:
                    529:        /*
                    530:         * No Entry available? fall back to stat(2)'ing the item, if
1.114     xsa       531:         * that fails, assume a normal file.
1.100     joris     532:         */
                    533:        if (ent == NULL) {
1.103     joris     534:                if (stat(path, &st) == -1)
1.100     joris     535:                        type = DT_REG;
1.103     joris     536:                else
1.100     joris     537:                        type = IFTODT(st.st_mode);
                    538:        } else {
                    539:                if (ent->ce_type == CVS_ENT_DIR)
                    540:                        type = DT_DIR;
                    541:                else
                    542:                        type = DT_REG;
                    543:        }
                    544:
                    545:        /*
                    546:         * Get the base, which is <parent> for a normal file or
                    547:         * <path> for a directory.
                    548:         */
                    549:        if (type == DT_DIR)
1.115     joris     550:                base = cvs_file_lget(path, flags, NULL, entf, ent);
1.100     joris     551:        else
1.115     joris     552:                base = cvs_file_lget(parent, flags, NULL, entf, NULL);
1.100     joris     553:
                    554:        if (base == NULL) {
                    555:                cvs_log(LP_ERR, "failed to obtain directory info for '%s'",
                    556:                    parent);
1.108     joris     557:                cvs_error = CVS_EX_FILE;
1.100     joris     558:                goto fail;
                    559:        }
                    560:
                    561:        /*
                    562:         * Sanity.
                    563:         */
                    564:        if (base->cf_type != DT_DIR) {
                    565:                cvs_log(LP_ERR, "base directory isn't a directory at all");
                    566:                goto fail;
                    567:        }
                    568:
                    569:        root = CVS_DIR_ROOT(base);
                    570:        if (root == NULL) {
1.108     joris     571:                cvs_error = CVS_EX_BADROOT;
1.100     joris     572:                goto fail;
                    573:        }
1.26      jfb       574:
1.100     joris     575:        /*
                    576:         * If we have a normal file, get the info and link it
                    577:         * to the base.
                    578:         */
                    579:        if (type != DT_DIR) {
1.115     joris     580:                cf = cvs_file_lget(path, flags, base, entf, ent);
1.100     joris     581:                if (cf == NULL) {
1.108     joris     582:                        cvs_error = CVS_EX_DATA;
1.100     joris     583:                        goto fail;
1.68      joris     584:                }
1.26      jfb       585:
1.100     joris     586:                cvs_file_attach(base, cf);
                    587:        }
                    588:
                    589:        /*
                    590:         * Always pass the base directory, unless:
                    591:         * - we are running in server or local mode and the path is not "."
                    592:         * - the directory does not exist on disk.
                    593:         * - the callback is NULL.
1.122     xsa       594:        */
1.120     joris     595:        callit = 1;
                    596:        if (cb == NULL)
                    597:                callit = 0;
                    598:
1.121     joris     599:        if ((cvs_cmdop == CVS_OP_SERVER) && (type != DT_DIR))
1.120     joris     600:                callit = 0;
                    601:
1.121     joris     602:        if ((root->cr_method == CVS_METHOD_LOCAL) && (type != DT_DIR))
1.120     joris     603:                callit = 0;
                    604:
                    605:        if (!(base->cf_flags & CVS_FILE_ONDISK))
                    606:                callit = 0;
                    607:
                    608:        if (callit != 0) {
                    609:                if ((cvs_error = cb(base,arg)) != CVS_EX_OK)
                    610:                        goto fail;
                    611:        }
1.100     joris     612:
                    613:        /*
                    614:         * If we have a normal file, pass it as well.
                    615:         */
                    616:        if (type != DT_DIR) {
                    617:                if ((cb != NULL) && ((cvs_error = cb(cf, arg)) != CVS_EX_OK))
                    618:                        goto fail;
                    619:        } else {
                    620:                /*
                    621:                 * If the directory exists, recurse through it.
                    622:                 */
                    623:                if ((base->cf_flags & CVS_FILE_ONDISK) &&
                    624:                    cvs_file_getdir(base, flags, cb, arg, freecf) < 0) {
1.108     joris     625:                        cvs_error = CVS_EX_FILE;
1.100     joris     626:                        goto fail;
1.68      joris     627:                }
1.116     joris     628:        }
                    629:
                    630:        if (entf != NULL) {
                    631:                cvs_ent_close(entf);
                    632:                entf = NULL;
1.26      jfb       633:        }
1.87      joris     634:
1.100     joris     635:        return (base);
1.26      jfb       636:
1.100     joris     637: fail:
                    638:        if (entf != NULL)
                    639:                cvs_ent_close(entf);
                    640:        if (base != NULL)
                    641:                cvs_file_free(base);
                    642:        return (NULL);
1.3       jfb       643: }
                    644:
                    645: /*
1.13      jfb       646:  * cvs_file_find()
                    647:  *
                    648:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    649:  * The file's pathname <path> must be relative to the base of <hier>.
                    650:  * Returns the entry on success, or NULL on failure.
                    651:  */
1.109     xsa       652: CVSFILE *
1.13      jfb       653: cvs_file_find(CVSFILE *hier, const char *path)
                    654: {
                    655:        char *pp, *sp, pbuf[MAXPATHLEN];
                    656:        CVSFILE *sf, *cf;
                    657:
                    658:        strlcpy(pbuf, path, sizeof(pbuf));
                    659:
                    660:        cf = hier;
                    661:        pp = pbuf;
                    662:        do {
                    663:                sp = strchr(pp, '/');
                    664:                if (sp != NULL)
1.24      jfb       665:                        *(sp++) = '\0';
1.13      jfb       666:
                    667:                /* special case */
                    668:                if (*pp == '.') {
                    669:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    670:                                /* request to go back to parent */
                    671:                                if (cf->cf_parent == NULL) {
                    672:                                        cvs_log(LP_NOTICE,
                    673:                                            "path %s goes back too far", path);
                    674:                                        return (NULL);
                    675:                                }
                    676:                                cf = cf->cf_parent;
                    677:                                continue;
1.38      deraadt   678:                        } else if (*(pp + 1) == '\0')
1.13      jfb       679:                                continue;
                    680:                }
                    681:
1.62      jfb       682:                SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.99      joris     683:                        if (cvs_file_cmpname(pp, sf->cf_name) == 0)
1.13      jfb       684:                                break;
                    685:                if (sf == NULL)
                    686:                        return (NULL);
                    687:
                    688:                cf = sf;
                    689:                pp = sp;
                    690:        } while (sp != NULL);
                    691:
1.24      jfb       692:        return (cf);
1.13      jfb       693: }
                    694:
                    695:
                    696: /*
1.34      jfb       697:  * cvs_file_getpath()
                    698:  *
                    699:  * Get the full path of the file <file> and store it in <buf>, which is of
                    700:  * size <len>.  For portability, it is recommended that <buf> always be
                    701:  * at least MAXPATHLEN bytes long.
1.100     joris     702:  * Returns a pointer to the start of the path.
1.34      jfb       703:  */
1.104     xsa       704: char *
1.34      jfb       705: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
                    706: {
1.100     joris     707:        memset(buf, '\0', len);
                    708:        if (file->cf_dir != NULL) {
                    709:                strlcat(buf, file->cf_dir, len);
1.34      jfb       710:                strlcat(buf, "/", len);
                    711:        }
                    712:
1.100     joris     713:        strlcat(buf, file->cf_name, len);
1.34      jfb       714:        return (buf);
                    715: }
                    716:
                    717: /*
1.22      jfb       718:  * cvs_file_attach()
                    719:  *
                    720:  * Attach the file <file> as one of the children of parent <parent>, which
                    721:  * has to be a file of type DT_DIR.
                    722:  * Returns 0 on success, or -1 on failure.
                    723:  */
                    724: int
                    725: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
                    726: {
                    727:        if (parent->cf_type != DT_DIR)
                    728:                return (-1);
                    729:
1.62      jfb       730:        SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22      jfb       731:        file->cf_parent = parent;
                    732:
                    733:        return (0);
                    734: }
                    735:
                    736:
                    737: /*
1.68      joris     738:  * Load directory information
1.3       jfb       739:  */
1.6       jfb       740: static int
1.69      joris     741: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3       jfb       742: {
1.68      joris     743:        char fpath[MAXPATHLEN];
                    744:        char pbuf[MAXPATHLEN];
1.20      jfb       745:        struct stat st;
1.68      joris     746:        int l;
1.7       jfb       747:
1.34      jfb       748:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.100     joris     749:
                    750:        /*
                    751:         * Try to obtain the Root for this given directory, if we cannot
                    752:         * get it, fail, unless we are dealing with a directory that is
                    753:         * unknown or not on disk.
                    754:         */
1.62      jfb       755:        cf->cf_root = cvsroot_get(fpath);
1.91      joris     756:        if (cf->cf_root == NULL) {
1.100     joris     757:                if (cf->cf_cvstat == CVS_FST_UNKNOWN ||
                    758:                    !(cf->cf_flags & CVS_FILE_ONDISK))
1.91      joris     759:                        return (0);
1.48      jfb       760:                return (-1);
1.91      joris     761:        }
1.114     xsa       762:
1.74      joris     763:        /* if the CVS administrative directory exists, load the info */
                    764:        l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
                    765:        if (l == -1 || l >= (int)sizeof(pbuf)) {
                    766:                errno = ENAMETOOLONG;
                    767:                cvs_log(LP_ERRNO, "%s", pbuf);
                    768:                return (-1);
                    769:        }
1.61      xsa       770:
1.74      joris     771:        if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
1.132     joris     772:                if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0)
                    773:                        cf->cf_repo = xstrdup(pbuf);
1.100     joris     774:        } else {
                    775:                /*
                    776:                 * Fill in the repo path ourselfs.
                    777:                 */
1.107     joris     778:                if (cvs_repo_base != NULL) {
                    779:                        l = snprintf(pbuf, sizeof(pbuf), "%s/%s",
                    780:                            cvs_repo_base, fpath);
                    781:                        if (l == -1 || l >= (int)sizeof(pbuf))
                    782:                                return (-1);
1.100     joris     783:
1.132     joris     784:                        cf->cf_repo = xstrdup(pbuf);
1.107     joris     785:                } else
                    786:                        cf->cf_repo = NULL;
1.68      joris     787:        }
1.100     joris     788:
1.68      joris     789:        return (0);
                    790: }
                    791:
                    792: /*
                    793:  * cvs_file_getdir()
                    794:  *
                    795:  * Get a cvs directory structure for the directory whose path is <dir>.
                    796:  * This function should not free the directory information on error, as this
                    797:  * is performed by cvs_file_free().
                    798:  */
                    799: static int
1.100     joris     800: cvs_file_getdir(CVSFILE *cf, int flags, int (*cb)(CVSFILE *, void *),
                    801:     void *arg, int freecf)
1.68      joris     802: {
1.100     joris     803:        int ret;
                    804:        size_t len;
                    805:        DIR *dp;
                    806:        struct dirent *de;
                    807:        char fpath[MAXPATHLEN], pbuf[MAXPATHLEN];
                    808:        CVSENTRIES *entf;
1.76      joris     809:        CVSFILE *cfp;
1.100     joris     810:        struct cvs_ent *ent;
1.68      joris     811:        struct cvs_flist dirs;
1.100     joris     812:        int nfiles, ndirs;
1.14      jfb       813:
1.56      jfb       814:        if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26      jfb       815:                return (0);
                    816:
1.128     joris     817:        /*
                    818:         * if we are working with a repository, fiddle with
                    819:         * the pathname again.
                    820:         */
                    821:        if (flags & CF_REPO) {
                    822:                ret = snprintf(fpath, sizeof(fpath), "%s%s%s",
                    823:                    cf->cf_root->cr_dir,
                    824:                    (cf->cf_dir != NULL) ? "/" : "",
                    825:                    (cf->cf_dir != NULL) ? cf->cf_dir : "");
                    826:                if (ret == -1 || ret >= (int)sizeof(fpath))
                    827:                        return (-1);
1.133   ! joris     828:
        !           829:                if (cf->cf_dir != NULL)
        !           830:                        xfree(cf->cf_dir);
1.132     joris     831:                cf->cf_dir = xstrdup(fpath);
1.128     joris     832:        }
                    833:
1.100     joris     834:        nfiles = ndirs = 0;
                    835:        SIMPLEQ_INIT(&dirs);
                    836:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.73      joris     837:
1.100     joris     838:        if ((dp = opendir(fpath)) == NULL) {
                    839:                cvs_log(LP_ERRNO, "failed to open directory '%s'", fpath);
1.91      joris     840:                return (-1);
                    841:        }
                    842:
1.100     joris     843:        ret = -1;
1.115     joris     844:        entf = cvs_ent_open(fpath, O_RDWR);
1.100     joris     845:        while ((de = readdir(dp)) != NULL) {
                    846:                if (!strcmp(de->d_name, ".") ||
                    847:                    !strcmp(de->d_name, ".."))
1.68      joris     848:                        continue;
1.11      jfb       849:
1.117     joris     850:                len = cvs_path_cat(fpath, de->d_name, pbuf, sizeof(pbuf));
                    851:                if (len >= sizeof(pbuf))
                    852:                        goto done;
                    853:
                    854:                if (entf != NULL)
                    855:                        ent = cvs_ent_get(entf, de->d_name);
                    856:                else
                    857:                        ent = NULL;
                    858:
1.100     joris     859:                /*
                    860:                 * Do some filtering on the current directory item.
                    861:                 */
                    862:                if ((flags & CF_IGNORE) && cvs_file_chkign(de->d_name))
1.68      joris     863:                        continue;
1.24      jfb       864:
1.100     joris     865:                if (!(flags & CF_RECURSE) && (de->d_type == DT_DIR)) {
1.117     joris     866:                        if (ent != NULL)
                    867:                                ent->processed = 1;
1.68      joris     868:                        continue;
                    869:                }
1.70      joris     870:
1.100     joris     871:                if ((de->d_type != DT_DIR) && (flags & CF_NOFILES))
1.70      joris     872:                        continue;
1.56      jfb       873:
1.115     joris     874:                cfp = cvs_file_lget(pbuf, flags, cf, entf, ent);
1.100     joris     875:                if (cfp == NULL) {
                    876:                        cvs_log(LP_ERR, "failed to get '%s'", pbuf);
1.91      joris     877:                        goto done;
1.68      joris     878:                }
1.61      xsa       879:
1.100     joris     880:                /*
                    881:                 * A file is linked to the parent <cf>, a directory
                    882:                 * is added to the dirs SIMPLEQ list for later use.
                    883:                 */
                    884:                if ((cfp->cf_type != DT_DIR) && !freecf) {
                    885:                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
                    886:                        nfiles++;
                    887:                } else if (cfp->cf_type == DT_DIR) {
                    888:                        SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
                    889:                        ndirs++;
                    890:                }
1.71      joris     891:
1.100     joris     892:                /*
                    893:                 * Now, for a file, pass it to the callback if it was
                    894:                 * supplied to us.
                    895:                 */
                    896:                if (cfp->cf_type != DT_DIR && cb != NULL) {
                    897:                        if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
1.91      joris     898:                                goto done;
1.4       jfb       899:                }
1.14      jfb       900:
1.100     joris     901:                /*
1.117     joris     902:                 * Mark the entry as processed.
1.100     joris     903:                 */
1.117     joris     904:                if (ent != NULL)
                    905:                        ent->processed = 1;
1.68      joris     906:
1.100     joris     907:                /*
                    908:                 * If we don't want to keep it, free it
                    909:                 */
                    910:                if ((cfp->cf_type != DT_DIR) && freecf)
                    911:                        cvs_file_free(cfp);
1.68      joris     912:        }
                    913:
1.100     joris     914:        closedir(dp);
                    915:        dp = NULL;
1.68      joris     916:
1.100     joris     917:        /*
                    918:         * Pass over all of the entries now, so we pickup any files
                    919:         * that might have been lost, or are for some reason not on disk.
                    920:         *
                    921:         * (Follows the same procedure as above ... can we merge them?)
                    922:         */
                    923:        while ((entf != NULL) && ((ent = cvs_ent_next(entf)) != NULL)) {
1.117     joris     924:                if (ent->processed == 1)
                    925:                        continue;
1.100     joris     926:                if (!(flags & CF_RECURSE) && (ent->ce_type == CVS_ENT_DIR))
                    927:                        continue;
                    928:                if ((flags & CF_NOFILES) && (ent->ce_type != CVS_ENT_DIR))
                    929:                        continue;
1.68      joris     930:
1.100     joris     931:                len = cvs_path_cat(fpath, ent->ce_name, pbuf, sizeof(pbuf));
                    932:                if (len >= sizeof(pbuf))
                    933:                        goto done;
1.61      xsa       934:
1.115     joris     935:                cfp = cvs_file_lget(pbuf, flags, cf, entf, ent);
1.100     joris     936:                if (cfp == NULL) {
                    937:                        cvs_log(LP_ERR, "failed to fetch '%s'", pbuf);
                    938:                        goto done;
                    939:                }
1.68      joris     940:
1.100     joris     941:                if ((cfp->cf_type != DT_DIR) && !freecf) {
                    942:                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
                    943:                        nfiles++;
                    944:                } else if (cfp->cf_type == DT_DIR) {
                    945:                        SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
                    946:                        ndirs++;
                    947:                }
1.68      joris     948:
1.100     joris     949:                if (cfp->cf_type != DT_DIR && cb != NULL) {
                    950:                        if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
                    951:                                goto done;
                    952:                }
1.68      joris     953:
1.100     joris     954:                if ((cfp->cf_type != DT_DIR) && freecf)
                    955:                        cvs_file_free(cfp);
1.40      jfb       956:        }
1.34      jfb       957:
1.100     joris     958:        /*
                    959:         * Sort files and dirs if requested.
                    960:         */
1.10      jfb       961:        if (flags & CF_SORT) {
1.68      joris     962:                if (nfiles > 0)
                    963:                        cvs_file_sort(&(cf->cf_files), nfiles);
                    964:                if (ndirs > 0)
                    965:                        cvs_file_sort(&dirs, ndirs);
1.10      jfb       966:        }
1.26      jfb       967:
1.100     joris     968:        /*
                    969:         * Finally, run over the directories we have encountered.
                    970:         * Before calling cvs_file_getdir() on them, we pass them
                    971:         * to the callback first.
                    972:         */
1.62      jfb       973:        while (!SIMPLEQ_EMPTY(&dirs)) {
                    974:                cfp = SIMPLEQ_FIRST(&dirs);
                    975:                SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68      joris     976:
1.100     joris     977:                if (!freecf)
1.68      joris     978:                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
                    979:
1.100     joris     980:                if (cb != NULL) {
                    981:                        if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
                    982:                                goto done;
1.68      joris     983:                }
1.100     joris     984:
                    985:                if ((cfp->cf_flags & CVS_FILE_ONDISK) &&
                    986:                    (cvs_file_getdir(cfp, flags, cb, arg, freecf) < 0))
                    987:                        goto done;
                    988:
                    989:                if (freecf)
                    990:                        cvs_file_free(cfp);
1.26      jfb       991:        }
1.3       jfb       992:
1.91      joris     993:        ret = 0;
1.100     joris     994:        cfp = NULL;
1.91      joris     995: done:
1.100     joris     996:        if ((cfp != NULL) && freecf)
                    997:                cvs_file_free(cfp);
                    998:
                    999:        while (!SIMPLEQ_EMPTY(&dirs)) {
                   1000:                cfp = SIMPLEQ_FIRST(&dirs);
                   1001:                SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
                   1002:
                   1003:                cvs_file_free(cfp);
                   1004:        }
                   1005:
                   1006:        if (entf != NULL)
                   1007:                cvs_ent_close(entf);
                   1008:        if (dp != NULL)
                   1009:                closedir(dp);
1.91      joris    1010:
                   1011:        return (ret);
1.3       jfb      1012: }
                   1013:
                   1014:
                   1015: /*
                   1016:  * cvs_file_free()
                   1017:  *
                   1018:  * Free a cvs_file structure and its contents.
                   1019:  */
                   1020: void
1.14      jfb      1021: cvs_file_free(CVSFILE *cf)
1.3       jfb      1022: {
1.62      jfb      1023:        CVSFILE *child;
                   1024:
1.47      jfb      1025:        if (cf->cf_name != NULL)
1.132     joris    1026:                xfree(cf->cf_name);
1.62      jfb      1027:
1.100     joris    1028:        if (cf->cf_dir != NULL)
1.132     joris    1029:                xfree(cf->cf_dir);
1.100     joris    1030:
1.62      jfb      1031:        if (cf->cf_type == DT_DIR) {
                   1032:                if (cf->cf_root != NULL)
1.119     joris    1033:                        cvsroot_remove(cf->cf_root);
1.62      jfb      1034:                if (cf->cf_repo != NULL)
1.132     joris    1035:                        xfree(cf->cf_repo);
1.62      jfb      1036:                while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
                   1037:                        child = SIMPLEQ_FIRST(&(cf->cf_files));
                   1038:                        SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
                   1039:                        cvs_file_free(child);
                   1040:                }
1.64      joris    1041:        } else {
                   1042:                if (cf->cf_tag != NULL)
1.132     joris    1043:                        xfree(cf->cf_tag);
1.77      jfb      1044:                if (cf->cf_opts != NULL)
1.132     joris    1045:                        xfree(cf->cf_opts);
1.62      jfb      1046:        }
1.64      joris    1047:
1.132     joris    1048:        xfree(cf);
1.5       jfb      1049: }
                   1050:
1.3       jfb      1051:
                   1052: /*
                   1053:  * cvs_file_sort()
                   1054:  *
1.16      jfb      1055:  * Sort a list of cvs file structures according to their filename.  The list
                   1056:  * <flp> is modified according to the sorting algorithm.  The number of files
                   1057:  * in the list must be given by <nfiles>.
                   1058:  * Returns 0 on success, or -1 on failure.
1.3       jfb      1059:  */
                   1060: static int
1.16      jfb      1061: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb      1062: {
                   1063:        int i;
                   1064:        size_t nb;
1.16      jfb      1065:        CVSFILE *cf, **cfvec;
                   1066:
1.113     xsa      1067:        cfvec = (CVSFILE **)calloc((size_t)nfiles, sizeof(CVSFILE *));
1.16      jfb      1068:        if (cfvec == NULL) {
                   1069:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                   1070:                return (-1);
                   1071:        }
1.3       jfb      1072:
                   1073:        i = 0;
1.62      jfb      1074:        SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16      jfb      1075:                if (i == (int)nfiles) {
1.3       jfb      1076:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb      1077:                        /* rebuild the list and abort sorting */
                   1078:                        while (--i >= 0)
1.62      jfb      1079:                                SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.132     joris    1080:                        xfree(cfvec);
1.3       jfb      1081:                        return (-1);
                   1082:                }
1.16      jfb      1083:                cfvec[i++] = cf;
1.3       jfb      1084:
                   1085:                /* now unlink it from the list,
                   1086:                 * we'll put it back in order later
                   1087:                 */
1.62      jfb      1088:                SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3       jfb      1089:        }
                   1090:
                   1091:        /* clear the list just in case */
1.62      jfb      1092:        SIMPLEQ_INIT(flp);
1.3       jfb      1093:        nb = (size_t)i;
                   1094:
                   1095:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                   1096:
                   1097:        /* rebuild the list from the bottom up */
                   1098:        for (i = (int)nb - 1; i >= 0; i--)
1.62      jfb      1099:                SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb      1100:
1.132     joris    1101:        xfree(cfvec);
1.3       jfb      1102:        return (0);
                   1103: }
                   1104:
                   1105:
                   1106: static int
                   1107: cvs_file_cmp(const void *f1, const void *f2)
                   1108: {
1.41      jfb      1109:        const CVSFILE *cf1, *cf2;
1.62      jfb      1110:        cf1 = *(CVSFILE * const *)f1;
                   1111:        cf2 = *(CVSFILE * const *)f2;
1.99      joris    1112:        return cvs_file_cmpname(cf1->cf_name, cf2->cf_name);
1.6       jfb      1113: }
                   1114:
                   1115:
1.34      jfb      1116: /*
                   1117:  * cvs_file_alloc()
                   1118:  *
                   1119:  * Allocate a CVSFILE structure and initialize its internals.
                   1120:  */
1.109     xsa      1121: CVSFILE *
1.6       jfb      1122: cvs_file_alloc(const char *path, u_int type)
                   1123: {
                   1124:        CVSFILE *cfp;
1.100     joris    1125:        char *p;
1.6       jfb      1126:
1.132     joris    1127:        cfp = (CVSFILE *)xmalloc(sizeof(*cfp));
1.6       jfb      1128:        memset(cfp, 0, sizeof(*cfp));
                   1129:
1.62      jfb      1130:        cfp->cf_type = type;
                   1131:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                   1132:
                   1133:        if (type == DT_DIR) {
                   1134:                SIMPLEQ_INIT(&(cfp->cf_files));
                   1135:        }
                   1136:
1.132     joris    1137:        cfp->cf_name = xstrdup(basename(path));
1.100     joris    1138:        if ((p = strrchr(path, '/')) != NULL) {
                   1139:                *p = '\0';
1.132     joris    1140:                if (strcmp(path, "."))
                   1141:                        cfp->cf_dir = xstrdup(path);
                   1142:                else
1.100     joris    1143:                        cfp->cf_dir = NULL;
                   1144:                *p = '/';
                   1145:        } else
                   1146:                cfp->cf_dir = NULL;
                   1147:
1.6       jfb      1148:        return (cfp);
1.1       jfb      1149: }
1.14      jfb      1150:
                   1151:
                   1152: /*
                   1153:  * cvs_file_lget()
                   1154:  *
                   1155:  * Get the file and link it with the parent right away.
1.22      jfb      1156:  * Returns a pointer to the created file structure on success, or NULL on
                   1157:  * failure.
1.14      jfb      1158:  */
1.109     xsa      1159: static CVSFILE *
1.115     joris    1160: cvs_file_lget(const char *path, int flags, CVSFILE *parent, CVSENTRIES *pent,
                   1161:     struct cvs_ent *ent)
1.14      jfb      1162: {
1.128     joris    1163:        char *c;
1.100     joris    1164:        int ret;
1.44      jfb      1165:        u_int type;
1.14      jfb      1166:        struct stat st;
                   1167:        CVSFILE *cfp;
1.128     joris    1168:        struct cvsroot *root;
1.14      jfb      1169:
1.44      jfb      1170:        type = DT_UNKNOWN;
                   1171:        ret = stat(path, &st);
                   1172:        if (ret == 0)
                   1173:                type = IFTODT(st.st_mode);
1.14      jfb      1174:
1.128     joris    1175:        if ((flags & CF_REPO) && (type != DT_DIR)) {
                   1176:                if ((c = strrchr(path, ',')) == NULL)
                   1177:                        return (NULL);
                   1178:                *c = '\0';
                   1179:        }
                   1180:
1.44      jfb      1181:        if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14      jfb      1182:                return (NULL);
                   1183:        cfp->cf_parent = parent;
1.115     joris    1184:        cfp->cf_entry = pent;
1.54      joris    1185:
                   1186:        if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62      jfb      1187:                cfp->cf_flags |= CVS_DIRF_BASE;
1.14      jfb      1188:
1.44      jfb      1189:        if (ret == 0) {
                   1190:                cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62      jfb      1191:                if (cfp->cf_type == DT_REG)
                   1192:                        cfp->cf_mtime = st.st_mtime;
1.100     joris    1193:                cfp->cf_flags |= CVS_FILE_ONDISK;
1.44      jfb      1194:
                   1195:                if (ent == NULL)
1.100     joris    1196:                        if (cfp->cf_flags & CVS_DIRF_BASE)
                   1197:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
                   1198:                        else
                   1199:                                cfp->cf_cvstat = CVS_FST_UNKNOWN;
1.14      jfb      1200:                else {
1.44      jfb      1201:                        /* always show directories as up-to-date */
                   1202:                        if (ent->ce_type == CVS_ENT_DIR)
1.14      jfb      1203:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44      jfb      1204:                        else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                   1205:                                cfp->cf_cvstat = CVS_FST_ADDED;
                   1206:                        else {
1.130     joris    1207:                                /*
                   1208:                                 * correct st.st_mtime first
                   1209:                                 */
                   1210:                                if ((st.st_mtime =
                   1211:                                    cvs_hack_time(st.st_mtime, 1)) == 0) {
                   1212:                                        cvs_file_free(cfp);
                   1213:                                        return (NULL);
                   1214:                                }
                   1215:
1.44      jfb      1216:                                /* check last modified time */
1.90      joris    1217:                                if (ent->ce_mtime == (time_t)st.st_mtime) {
1.44      jfb      1218:                                        cfp->cf_cvstat = CVS_FST_UPTODATE;
1.90      joris    1219:                                } else {
1.44      jfb      1220:                                        cfp->cf_cvstat = CVS_FST_MODIFIED;
1.90      joris    1221:                                }
1.44      jfb      1222:                        }
1.94      joris    1223:
                   1224:                        cfp->cf_etime = ent->ce_mtime;
1.14      jfb      1225:                }
1.44      jfb      1226:        } else {
                   1227:                if (ent == NULL) {
1.92      joris    1228:                        /* assume it is a file and unknown */
                   1229:                        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                   1230:                        cfp->cf_type = DT_REG;
1.66      joris    1231:                } else {
                   1232:                        if (ent->ce_type == CVS_ENT_FILE)
                   1233:                                cfp->cf_type = DT_REG;
                   1234:                        else if (ent->ce_type == CVS_ENT_DIR)
                   1235:                                cfp->cf_type = DT_DIR;
                   1236:                        else
1.93      joris    1237:                                cvs_log(LP_WARN, "unknown ce_type %d",
1.66      joris    1238:                                    ent->ce_type);
1.67      joris    1239:
                   1240:                        if (ent->ce_status == CVS_ENT_REMOVED)
                   1241:                                cfp->cf_cvstat = CVS_FST_REMOVED;
1.90      joris    1242:                        else if (ent->ce_status == CVS_ENT_UPTODATE)
                   1243:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.96      joris    1244:                        else if (ent->ce_status == CVS_ENT_ADDED)
                   1245:                                cfp->cf_cvstat = CVS_FST_ADDED;
1.67      joris    1246:                        else
                   1247:                                cfp->cf_cvstat = CVS_FST_LOST;
1.66      joris    1248:                }
1.128     joris    1249:
                   1250:                /* XXX assume 0644 ? */
                   1251:                cfp->cf_mode = 0644;
1.14      jfb      1252:        }
1.52      jfb      1253:
1.62      jfb      1254:        if (ent != NULL) {
                   1255:                /* steal the RCSNUM */
                   1256:                cfp->cf_lrev = ent->ce_rev;
1.72      jfb      1257:
1.77      jfb      1258:                if (ent->ce_type == CVS_ENT_FILE) {
1.132     joris    1259:                        if (ent->ce_tag[0] != '\0')
                   1260:                                cfp->cf_tag = xstrdup(ent->ce_tag);
1.77      jfb      1261:
1.132     joris    1262:                        if (ent->ce_opts[0] != '\0')
                   1263:                                cfp->cf_opts = xstrdup(ent->ce_opts);
1.62      jfb      1264:                }
                   1265:        }
1.14      jfb      1266:
1.115     joris    1267:        if (cfp->cf_type == DT_DIR) {
                   1268:                if (cvs_load_dirinfo(cfp, flags) < 0) {
                   1269:                        cvs_file_free(cfp);
                   1270:                        return (NULL);
                   1271:                }
1.14      jfb      1272:        }
1.74      joris    1273:
1.128     joris    1274:        if (flags & CF_REPO) {
                   1275:                root = CVS_DIR_ROOT(cfp);
                   1276:
                   1277:                cfp->cf_mode = 0644;
                   1278:                cfp->cf_cvstat = CVS_FST_LOST;
                   1279:
1.132     joris    1280:                c = xstrdup(cfp->cf_dir);
                   1281:                xfree(cfp->cf_dir);
1.128     joris    1282:
                   1283:                if (strcmp(c, root->cr_dir)) {
                   1284:                        c += strlen(root->cr_dir) + 1;
1.132     joris    1285:                        cfp->cf_dir = xstrdup(c);
1.128     joris    1286:                        c -= strlen(root->cr_dir) + 1;
                   1287:                } else {
                   1288:                        cfp->cf_dir = NULL;
                   1289:                }
                   1290:
1.132     joris    1291:                xfree(c);
1.128     joris    1292:        }
                   1293:
1.74      joris    1294:        if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
                   1295:            !strcmp(cfp->cf_repo, path))
                   1296:                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14      jfb      1297:
1.100     joris    1298:        /*
                   1299:         * In server mode, we do a few extra checks.
                   1300:         */
                   1301:        if (cvs_cmdop == CVS_OP_SERVER) {
                   1302:                /*
                   1303:                 * If for some reason a file was added,
                   1304:                 * but does not exist anymore, start complaining.
                   1305:                 */
                   1306:                if (!(cfp->cf_flags & CVS_FILE_ONDISK) &&
1.124     reyk     1307:                    (cfp->cf_cvstat == CVS_FST_ADDED) &&
1.100     joris    1308:                    (cfp->cf_type != DT_DIR))
                   1309:                        cvs_log(LP_WARN, "new-born %s has disappeared", path);
                   1310:
                   1311:                /*
                   1312:                 * Any other needed checks?
                   1313:                 */
                   1314:        }
                   1315:
1.14      jfb      1316:        return (cfp);
1.23      jfb      1317: }
                   1318:
                   1319:
                   1320: static int
                   1321: cvs_file_cmpname(const char *name1, const char *name2)
                   1322: {
                   1323:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                   1324:            (strcasecmp(name1, name2));
1.88      joris    1325: }
                   1326:
                   1327: /*
1.100     joris    1328:  * remove any empty directories.
1.88      joris    1329:  */
                   1330: int
                   1331: cvs_file_prune(char *path)
                   1332: {
                   1333:        DIR *dirp;
                   1334:        int l, pwd, empty;
                   1335:        struct dirent *dp;
                   1336:        char fpath[MAXPATHLEN];
                   1337:        CVSENTRIES *entf;
1.89      joris    1338:        CVSFILE *cfp;
1.88      joris    1339:
                   1340:        pwd = (!strcmp(path, "."));
                   1341:
                   1342:        if ((dirp = opendir(path)) == NULL) {
                   1343:                cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
                   1344:                return (-1);
                   1345:        }
                   1346:
                   1347:        empty = 0;
                   1348:        entf = cvs_ent_open(path, O_RDWR);
                   1349:
                   1350:        while ((dp = readdir(dirp)) != NULL) {
                   1351:                if (!strcmp(dp->d_name, ".") ||
                   1352:                    !strcmp(dp->d_name, "..") ||
                   1353:                    !strcmp(dp->d_name, CVS_PATH_CVSDIR))
                   1354:                        continue;
                   1355:
                   1356:                empty++;
                   1357:                if (dp->d_type == DT_DIR) {
                   1358:                        l = snprintf(fpath, sizeof(fpath), "%s%s%s",
                   1359:                            (pwd) ? "" : path, (pwd) ? "" : "/", dp->d_name);
                   1360:                        if (l == -1 || l >= (int)sizeof(fpath)) {
                   1361:                                errno = ENAMETOOLONG;
                   1362:                                cvs_log(LP_ERRNO, "%s", fpath);
                   1363:                                continue;
                   1364:                        }
1.89      joris    1365:
                   1366:                        cfp = cvs_file_find(cvs_files, fpath);
                   1367:                        if (cfp == NULL)
1.91      joris    1368:                                continue;
                   1369:
                   1370:                        /* ignore unknown directories */
                   1371:                        if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
1.89      joris    1372:                                continue;
1.88      joris    1373:
                   1374:                        if (cvs_file_prune(fpath)) {
                   1375:                                empty--;
                   1376:                                if (entf)
1.128     joris    1377:                                        cvs_ent_remove(entf, fpath, 0);
1.88      joris    1378:                        } else {
                   1379:                                empty++;
                   1380:                        }
                   1381:                }
                   1382:        }
                   1383:
                   1384:        closedir(dirp);
                   1385:        if (entf)
                   1386:                cvs_ent_close(entf);
                   1387:
                   1388:        empty = (empty == 0);
                   1389:        if (empty) {
1.106     xsa      1390:                if (cvs_rmdir(path) < 0) {
1.88      joris    1391:                        cvs_log(LP_ERR, "failed to prune `%s'", path);
                   1392:                        empty = 0;
                   1393:                }
1.93      joris    1394:        }
1.88      joris    1395:
                   1396:        return (empty);
1.14      jfb      1397: }