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

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