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

1.120   ! joris       1: /*     $OpenBSD: file.c,v 1.119 2005/09/11 14:16:48 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];
1.120   ! joris     494:        int type, callit;
1.100     joris     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:         */
1.120   ! joris     601:        callit = 1;
        !           602:        if (cb == NULL)
        !           603:                callit = 0;
        !           604:
        !           605:        if (cvs_cmdop == CVS_OP_SERVER && type != DT_DIR)
        !           606:                callit = 0;
        !           607:
        !           608:        if (root->cr_method == CVS_METHOD_LOCAL && type != DT_DIR)
        !           609:                callit = 0;
        !           610:
        !           611:        if (!(base->cf_flags & CVS_FILE_ONDISK))
        !           612:                callit = 0;
        !           613:
        !           614:        if (callit != 0) {
        !           615:                if ((cvs_error = cb(base,arg)) != CVS_EX_OK)
        !           616:                        goto fail;
        !           617:        }
1.100     joris     618:
                    619:        /*
                    620:         * If we have a normal file, pass it as well.
                    621:         */
                    622:        if (type != DT_DIR) {
                    623:                if ((cb != NULL) && ((cvs_error = cb(cf, arg)) != CVS_EX_OK))
                    624:                        goto fail;
                    625:        } else {
                    626:                /*
                    627:                 * If the directory exists, recurse through it.
                    628:                 */
                    629:                if ((base->cf_flags & CVS_FILE_ONDISK) &&
                    630:                    cvs_file_getdir(base, flags, cb, arg, freecf) < 0) {
1.108     joris     631:                        cvs_error = CVS_EX_FILE;
1.100     joris     632:                        goto fail;
1.68      joris     633:                }
1.116     joris     634:        }
                    635:
                    636:        if (entf != NULL) {
                    637:                cvs_ent_close(entf);
                    638:                entf = NULL;
1.26      jfb       639:        }
1.87      joris     640:
1.100     joris     641:        return (base);
1.26      jfb       642:
1.100     joris     643: fail:
                    644:        if (entf != NULL)
                    645:                cvs_ent_close(entf);
                    646:        if (base != NULL)
                    647:                cvs_file_free(base);
                    648:        return (NULL);
1.3       jfb       649: }
                    650:
                    651: /*
1.13      jfb       652:  * cvs_file_find()
                    653:  *
                    654:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    655:  * The file's pathname <path> must be relative to the base of <hier>.
                    656:  * Returns the entry on success, or NULL on failure.
                    657:  */
1.109     xsa       658: CVSFILE *
1.13      jfb       659: cvs_file_find(CVSFILE *hier, const char *path)
                    660: {
                    661:        char *pp, *sp, pbuf[MAXPATHLEN];
                    662:        CVSFILE *sf, *cf;
                    663:
                    664:        strlcpy(pbuf, path, sizeof(pbuf));
                    665:
                    666:        cf = hier;
                    667:        pp = pbuf;
                    668:        do {
                    669:                sp = strchr(pp, '/');
                    670:                if (sp != NULL)
1.24      jfb       671:                        *(sp++) = '\0';
1.13      jfb       672:
                    673:                /* special case */
                    674:                if (*pp == '.') {
                    675:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    676:                                /* request to go back to parent */
                    677:                                if (cf->cf_parent == NULL) {
                    678:                                        cvs_log(LP_NOTICE,
                    679:                                            "path %s goes back too far", path);
                    680:                                        return (NULL);
                    681:                                }
                    682:                                cf = cf->cf_parent;
                    683:                                continue;
1.38      deraadt   684:                        } else if (*(pp + 1) == '\0')
1.13      jfb       685:                                continue;
                    686:                }
                    687:
1.62      jfb       688:                SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.99      joris     689:                        if (cvs_file_cmpname(pp, sf->cf_name) == 0)
1.13      jfb       690:                                break;
                    691:                if (sf == NULL)
                    692:                        return (NULL);
                    693:
                    694:                cf = sf;
                    695:                pp = sp;
                    696:        } while (sp != NULL);
                    697:
1.24      jfb       698:        return (cf);
1.13      jfb       699: }
                    700:
                    701:
                    702: /*
1.34      jfb       703:  * cvs_file_getpath()
                    704:  *
                    705:  * Get the full path of the file <file> and store it in <buf>, which is of
                    706:  * size <len>.  For portability, it is recommended that <buf> always be
                    707:  * at least MAXPATHLEN bytes long.
1.100     joris     708:  * Returns a pointer to the start of the path.
1.34      jfb       709:  */
1.104     xsa       710: char *
1.34      jfb       711: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
                    712: {
1.100     joris     713:        memset(buf, '\0', len);
                    714:        if (file->cf_dir != NULL) {
                    715:                strlcat(buf, file->cf_dir, len);
1.34      jfb       716:                strlcat(buf, "/", len);
                    717:        }
                    718:
1.100     joris     719:        strlcat(buf, file->cf_name, len);
1.34      jfb       720:        return (buf);
                    721: }
                    722:
                    723: /*
1.22      jfb       724:  * cvs_file_attach()
                    725:  *
                    726:  * Attach the file <file> as one of the children of parent <parent>, which
                    727:  * has to be a file of type DT_DIR.
                    728:  * Returns 0 on success, or -1 on failure.
                    729:  */
                    730: int
                    731: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
                    732: {
                    733:        if (parent->cf_type != DT_DIR)
                    734:                return (-1);
                    735:
1.62      jfb       736:        SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22      jfb       737:        file->cf_parent = parent;
                    738:
                    739:        return (0);
                    740: }
                    741:
                    742:
                    743: /*
1.68      joris     744:  * Load directory information
1.3       jfb       745:  */
1.6       jfb       746: static int
1.69      joris     747: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3       jfb       748: {
1.68      joris     749:        char fpath[MAXPATHLEN];
                    750:        char pbuf[MAXPATHLEN];
1.20      jfb       751:        struct stat st;
1.68      joris     752:        int l;
1.7       jfb       753:
1.34      jfb       754:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.100     joris     755:
                    756:        /*
                    757:         * Try to obtain the Root for this given directory, if we cannot
                    758:         * get it, fail, unless we are dealing with a directory that is
                    759:         * unknown or not on disk.
                    760:         */
1.62      jfb       761:        cf->cf_root = cvsroot_get(fpath);
1.91      joris     762:        if (cf->cf_root == NULL) {
1.100     joris     763:                if (cf->cf_cvstat == CVS_FST_UNKNOWN ||
                    764:                    !(cf->cf_flags & CVS_FILE_ONDISK))
1.91      joris     765:                        return (0);
1.48      jfb       766:                return (-1);
1.91      joris     767:        }
1.114     xsa       768:
1.74      joris     769:        /* if the CVS administrative directory exists, load the info */
                    770:        l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
                    771:        if (l == -1 || l >= (int)sizeof(pbuf)) {
                    772:                errno = ENAMETOOLONG;
                    773:                cvs_log(LP_ERRNO, "%s", pbuf);
                    774:                return (-1);
                    775:        }
1.61      xsa       776:
1.74      joris     777:        if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
                    778:                if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
                    779:                        cf->cf_repo = strdup(pbuf);
                    780:                        if (cf->cf_repo == NULL) {
                    781:                                cvs_log(LP_ERRNO,
                    782:                                    "failed to dup repository string");
                    783:                                return (-1);
1.20      jfb       784:                        }
1.68      joris     785:                }
1.100     joris     786:        } else {
                    787:                /*
                    788:                 * Fill in the repo path ourselfs.
                    789:                 */
1.107     joris     790:                if (cvs_repo_base != NULL) {
                    791:                        l = snprintf(pbuf, sizeof(pbuf), "%s/%s",
                    792:                            cvs_repo_base, fpath);
                    793:                        if (l == -1 || l >= (int)sizeof(pbuf))
                    794:                                return (-1);
1.100     joris     795:
1.107     joris     796:                        cf->cf_repo = strdup(pbuf);
                    797:                        if (cf->cf_repo == NULL) {
                    798:                                cvs_log(LP_ERRNO, "failed to dup repo string");
                    799:                                return (-1);
                    800:                        }
                    801:                } else
                    802:                        cf->cf_repo = NULL;
1.68      joris     803:        }
1.26      jfb       804:
1.100     joris     805:        if (flags & CF_MKADMIN)
                    806:                cvs_mkadmin(fpath, cf->cf_root->cr_str, NULL);
                    807:
1.68      joris     808:        return (0);
                    809: }
                    810:
                    811: /*
                    812:  * cvs_file_getdir()
                    813:  *
                    814:  * Get a cvs directory structure for the directory whose path is <dir>.
                    815:  * This function should not free the directory information on error, as this
                    816:  * is performed by cvs_file_free().
                    817:  */
                    818: static int
1.100     joris     819: cvs_file_getdir(CVSFILE *cf, int flags, int (*cb)(CVSFILE *, void *),
                    820:     void *arg, int freecf)
1.68      joris     821: {
1.100     joris     822:        int ret;
                    823:        size_t len;
                    824:        DIR *dp;
                    825:        struct dirent *de;
                    826:        char fpath[MAXPATHLEN], pbuf[MAXPATHLEN];
                    827:        CVSENTRIES *entf;
1.76      joris     828:        CVSFILE *cfp;
1.100     joris     829:        struct cvs_ent *ent;
1.68      joris     830:        struct cvs_flist dirs;
1.100     joris     831:        int nfiles, ndirs;
1.14      jfb       832:
1.56      jfb       833:        if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26      jfb       834:                return (0);
                    835:
1.100     joris     836:        nfiles = ndirs = 0;
                    837:        SIMPLEQ_INIT(&dirs);
                    838:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.73      joris     839:
1.100     joris     840:        if ((dp = opendir(fpath)) == NULL) {
                    841:                cvs_log(LP_ERRNO, "failed to open directory '%s'", fpath);
1.91      joris     842:                return (-1);
                    843:        }
                    844:
1.100     joris     845:        ret = -1;
1.115     joris     846:        entf = cvs_ent_open(fpath, O_RDWR);
1.100     joris     847:        while ((de = readdir(dp)) != NULL) {
                    848:                if (!strcmp(de->d_name, ".") ||
                    849:                    !strcmp(de->d_name, ".."))
1.68      joris     850:                        continue;
1.11      jfb       851:
1.117     joris     852:                len = cvs_path_cat(fpath, de->d_name, pbuf, sizeof(pbuf));
                    853:                if (len >= sizeof(pbuf))
                    854:                        goto done;
                    855:
                    856:                if (entf != NULL)
                    857:                        ent = cvs_ent_get(entf, de->d_name);
                    858:                else
                    859:                        ent = NULL;
                    860:
1.100     joris     861:                /*
                    862:                 * Do some filtering on the current directory item.
                    863:                 */
                    864:                if ((flags & CF_IGNORE) && cvs_file_chkign(de->d_name))
1.68      joris     865:                        continue;
1.24      jfb       866:
1.100     joris     867:                if (!(flags & CF_RECURSE) && (de->d_type == DT_DIR)) {
1.117     joris     868:                        if (ent != NULL)
                    869:                                ent->processed = 1;
1.68      joris     870:                        continue;
                    871:                }
1.70      joris     872:
1.100     joris     873:                if ((de->d_type != DT_DIR) && (flags & CF_NOFILES))
1.70      joris     874:                        continue;
1.56      jfb       875:
1.115     joris     876:                cfp = cvs_file_lget(pbuf, flags, cf, entf, ent);
1.100     joris     877:                if (cfp == NULL) {
                    878:                        cvs_log(LP_ERR, "failed to get '%s'", pbuf);
1.91      joris     879:                        goto done;
1.68      joris     880:                }
1.61      xsa       881:
1.100     joris     882:                /*
                    883:                 * A file is linked to the parent <cf>, a directory
                    884:                 * is added to the dirs SIMPLEQ list for later use.
                    885:                 */
                    886:                if ((cfp->cf_type != DT_DIR) && !freecf) {
                    887:                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
                    888:                        nfiles++;
                    889:                } else if (cfp->cf_type == DT_DIR) {
                    890:                        SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
                    891:                        ndirs++;
                    892:                }
1.71      joris     893:
1.100     joris     894:                /*
                    895:                 * Now, for a file, pass it to the callback if it was
                    896:                 * supplied to us.
                    897:                 */
                    898:                if (cfp->cf_type != DT_DIR && cb != NULL) {
                    899:                        if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
1.91      joris     900:                                goto done;
1.4       jfb       901:                }
1.14      jfb       902:
1.100     joris     903:                /*
1.117     joris     904:                 * Mark the entry as processed.
1.100     joris     905:                 */
1.117     joris     906:                if (ent != NULL)
                    907:                        ent->processed = 1;
1.68      joris     908:
1.100     joris     909:                /*
                    910:                 * If we don't want to keep it, free it
                    911:                 */
                    912:                if ((cfp->cf_type != DT_DIR) && freecf)
                    913:                        cvs_file_free(cfp);
1.68      joris     914:        }
                    915:
1.100     joris     916:        closedir(dp);
                    917:        dp = NULL;
1.68      joris     918:
1.100     joris     919:        /*
                    920:         * Pass over all of the entries now, so we pickup any files
                    921:         * that might have been lost, or are for some reason not on disk.
                    922:         *
                    923:         * (Follows the same procedure as above ... can we merge them?)
                    924:         */
                    925:        while ((entf != NULL) && ((ent = cvs_ent_next(entf)) != NULL)) {
1.117     joris     926:                if (ent->processed == 1)
                    927:                        continue;
1.100     joris     928:                if (!(flags & CF_RECURSE) && (ent->ce_type == CVS_ENT_DIR))
                    929:                        continue;
                    930:                if ((flags & CF_NOFILES) && (ent->ce_type != CVS_ENT_DIR))
                    931:                        continue;
1.68      joris     932:
1.100     joris     933:                len = cvs_path_cat(fpath, ent->ce_name, pbuf, sizeof(pbuf));
                    934:                if (len >= sizeof(pbuf))
                    935:                        goto done;
1.61      xsa       936:
1.115     joris     937:                cfp = cvs_file_lget(pbuf, flags, cf, entf, ent);
1.100     joris     938:                if (cfp == NULL) {
                    939:                        cvs_log(LP_ERR, "failed to fetch '%s'", pbuf);
                    940:                        goto done;
                    941:                }
1.68      joris     942:
1.100     joris     943:                if ((cfp->cf_type != DT_DIR) && !freecf) {
                    944:                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
                    945:                        nfiles++;
                    946:                } else if (cfp->cf_type == DT_DIR) {
                    947:                        SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
                    948:                        ndirs++;
                    949:                }
1.68      joris     950:
1.100     joris     951:                if (cfp->cf_type != DT_DIR && cb != NULL) {
                    952:                        if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
                    953:                                goto done;
                    954:                }
1.68      joris     955:
1.100     joris     956:                if ((cfp->cf_type != DT_DIR) && freecf)
                    957:                        cvs_file_free(cfp);
1.40      jfb       958:        }
1.34      jfb       959:
1.100     joris     960:        /*
                    961:         * Sort files and dirs if requested.
                    962:         */
1.10      jfb       963:        if (flags & CF_SORT) {
1.68      joris     964:                if (nfiles > 0)
                    965:                        cvs_file_sort(&(cf->cf_files), nfiles);
                    966:                if (ndirs > 0)
                    967:                        cvs_file_sort(&dirs, ndirs);
1.10      jfb       968:        }
1.26      jfb       969:
1.100     joris     970:        /*
                    971:         * Finally, run over the directories we have encountered.
                    972:         * Before calling cvs_file_getdir() on them, we pass them
                    973:         * to the callback first.
                    974:         */
1.62      jfb       975:        while (!SIMPLEQ_EMPTY(&dirs)) {
                    976:                cfp = SIMPLEQ_FIRST(&dirs);
                    977:                SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68      joris     978:
1.100     joris     979:                if (!freecf)
1.68      joris     980:                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
                    981:
1.100     joris     982:                if (cb != NULL) {
                    983:                        if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
                    984:                                goto done;
1.68      joris     985:                }
1.100     joris     986:
                    987:                if ((cfp->cf_flags & CVS_FILE_ONDISK) &&
                    988:                    (cvs_file_getdir(cfp, flags, cb, arg, freecf) < 0))
                    989:                        goto done;
                    990:
                    991:                if (freecf)
                    992:                        cvs_file_free(cfp);
1.26      jfb       993:        }
1.3       jfb       994:
1.91      joris     995:        ret = 0;
1.100     joris     996:        cfp = NULL;
1.91      joris     997: done:
1.100     joris     998:        if ((cfp != NULL) && freecf)
                    999:                cvs_file_free(cfp);
                   1000:
                   1001:        while (!SIMPLEQ_EMPTY(&dirs)) {
                   1002:                cfp = SIMPLEQ_FIRST(&dirs);
                   1003:                SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
                   1004:
                   1005:                cvs_file_free(cfp);
                   1006:        }
                   1007:
                   1008:        if (entf != NULL)
                   1009:                cvs_ent_close(entf);
                   1010:        if (dp != NULL)
                   1011:                closedir(dp);
1.91      joris    1012:
                   1013:        return (ret);
1.3       jfb      1014: }
                   1015:
                   1016:
                   1017: /*
                   1018:  * cvs_file_free()
                   1019:  *
                   1020:  * Free a cvs_file structure and its contents.
                   1021:  */
                   1022: void
1.14      jfb      1023: cvs_file_free(CVSFILE *cf)
1.3       jfb      1024: {
1.62      jfb      1025:        CVSFILE *child;
                   1026:
1.47      jfb      1027:        if (cf->cf_name != NULL)
1.57      jfb      1028:                cvs_strfree(cf->cf_name);
1.62      jfb      1029:
1.100     joris    1030:        if (cf->cf_dir != NULL)
                   1031:                cvs_strfree(cf->cf_dir);
                   1032:
1.62      jfb      1033:        if (cf->cf_type == DT_DIR) {
                   1034:                if (cf->cf_root != NULL)
1.119     joris    1035:                        cvsroot_remove(cf->cf_root);
1.62      jfb      1036:                if (cf->cf_repo != NULL)
                   1037:                        free(cf->cf_repo);
                   1038:                while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
                   1039:                        child = SIMPLEQ_FIRST(&(cf->cf_files));
                   1040:                        SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
                   1041:                        cvs_file_free(child);
                   1042:                }
1.64      joris    1043:        } else {
                   1044:                if (cf->cf_tag != NULL)
                   1045:                        cvs_strfree(cf->cf_tag);
1.77      jfb      1046:                if (cf->cf_opts != NULL)
                   1047:                        cvs_strfree(cf->cf_opts);
1.62      jfb      1048:        }
1.64      joris    1049:
1.3       jfb      1050:        free(cf);
1.5       jfb      1051: }
                   1052:
                   1053:
                   1054: /*
                   1055:  * cvs_file_examine()
                   1056:  *
1.100     joris    1057:  * Walk through the files, calling the callback as we go.
1.5       jfb      1058:  */
                   1059: int
                   1060: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                   1061: {
                   1062:        int ret;
1.14      jfb      1063:        CVSFILE *fp;
1.5       jfb      1064:
1.100     joris    1065:        fp = NULL;
                   1066:        ret = 0;
1.13      jfb      1067:        return (ret);
1.3       jfb      1068: }
                   1069:
                   1070: /*
                   1071:  * cvs_file_sort()
                   1072:  *
1.16      jfb      1073:  * Sort a list of cvs file structures according to their filename.  The list
                   1074:  * <flp> is modified according to the sorting algorithm.  The number of files
                   1075:  * in the list must be given by <nfiles>.
                   1076:  * Returns 0 on success, or -1 on failure.
1.3       jfb      1077:  */
                   1078: static int
1.16      jfb      1079: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb      1080: {
                   1081:        int i;
                   1082:        size_t nb;
1.16      jfb      1083:        CVSFILE *cf, **cfvec;
                   1084:
1.113     xsa      1085:        cfvec = (CVSFILE **)calloc((size_t)nfiles, sizeof(CVSFILE *));
1.16      jfb      1086:        if (cfvec == NULL) {
                   1087:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                   1088:                return (-1);
                   1089:        }
1.3       jfb      1090:
                   1091:        i = 0;
1.62      jfb      1092:        SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16      jfb      1093:                if (i == (int)nfiles) {
1.3       jfb      1094:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb      1095:                        /* rebuild the list and abort sorting */
                   1096:                        while (--i >= 0)
1.62      jfb      1097:                                SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16      jfb      1098:                        free(cfvec);
1.3       jfb      1099:                        return (-1);
                   1100:                }
1.16      jfb      1101:                cfvec[i++] = cf;
1.3       jfb      1102:
                   1103:                /* now unlink it from the list,
                   1104:                 * we'll put it back in order later
                   1105:                 */
1.62      jfb      1106:                SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3       jfb      1107:        }
                   1108:
                   1109:        /* clear the list just in case */
1.62      jfb      1110:        SIMPLEQ_INIT(flp);
1.3       jfb      1111:        nb = (size_t)i;
                   1112:
                   1113:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                   1114:
                   1115:        /* rebuild the list from the bottom up */
                   1116:        for (i = (int)nb - 1; i >= 0; i--)
1.62      jfb      1117:                SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb      1118:
1.16      jfb      1119:        free(cfvec);
1.3       jfb      1120:        return (0);
                   1121: }
                   1122:
                   1123:
                   1124: static int
                   1125: cvs_file_cmp(const void *f1, const void *f2)
                   1126: {
1.41      jfb      1127:        const CVSFILE *cf1, *cf2;
1.62      jfb      1128:        cf1 = *(CVSFILE * const *)f1;
                   1129:        cf2 = *(CVSFILE * const *)f2;
1.99      joris    1130:        return cvs_file_cmpname(cf1->cf_name, cf2->cf_name);
1.6       jfb      1131: }
                   1132:
                   1133:
1.34      jfb      1134: /*
                   1135:  * cvs_file_alloc()
                   1136:  *
                   1137:  * Allocate a CVSFILE structure and initialize its internals.
                   1138:  */
1.109     xsa      1139: CVSFILE *
1.6       jfb      1140: cvs_file_alloc(const char *path, u_int type)
                   1141: {
                   1142:        CVSFILE *cfp;
1.100     joris    1143:        char *p;
1.6       jfb      1144:
1.14      jfb      1145:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb      1146:        if (cfp == NULL) {
                   1147:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                   1148:                return (NULL);
                   1149:        }
                   1150:        memset(cfp, 0, sizeof(*cfp));
                   1151:
1.62      jfb      1152:        cfp->cf_type = type;
                   1153:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                   1154:
                   1155:        if (type == DT_DIR) {
                   1156:                SIMPLEQ_INIT(&(cfp->cf_files));
                   1157:        }
                   1158:
1.57      jfb      1159:        cfp->cf_name = cvs_strdup(basename(path));
1.34      jfb      1160:        if (cfp->cf_name == NULL) {
1.57      jfb      1161:                cvs_log(LP_ERR, "failed to copy file name");
1.58      tedu     1162:                cvs_file_free(cfp);
1.6       jfb      1163:                return (NULL);
                   1164:        }
                   1165:
1.100     joris    1166:        if ((p = strrchr(path, '/')) != NULL) {
                   1167:                *p = '\0';
                   1168:                if (strcmp(path, ".")) {
                   1169:                        cfp->cf_dir = cvs_strdup(path);
                   1170:                        if (cfp->cf_dir == NULL) {
                   1171:                                cvs_log(LP_ERR,
                   1172:                                    "failed to copy directory");
                   1173:                                cvs_file_free(cfp);
                   1174:                                return (NULL);
                   1175:                        }
                   1176:                } else
                   1177:                        cfp->cf_dir = NULL;
                   1178:                *p = '/';
                   1179:        } else
                   1180:                cfp->cf_dir = NULL;
                   1181:
1.6       jfb      1182:        return (cfp);
1.1       jfb      1183: }
1.14      jfb      1184:
                   1185:
                   1186: /*
                   1187:  * cvs_file_lget()
                   1188:  *
                   1189:  * Get the file and link it with the parent right away.
1.22      jfb      1190:  * Returns a pointer to the created file structure on success, or NULL on
                   1191:  * failure.
1.14      jfb      1192:  */
1.109     xsa      1193: static CVSFILE *
1.115     joris    1194: cvs_file_lget(const char *path, int flags, CVSFILE *parent, CVSENTRIES *pent,
                   1195:     struct cvs_ent *ent)
1.14      jfb      1196: {
1.100     joris    1197:        int ret;
1.44      jfb      1198:        u_int type;
1.14      jfb      1199:        struct stat st;
                   1200:        CVSFILE *cfp;
                   1201:
1.44      jfb      1202:        type = DT_UNKNOWN;
                   1203:        ret = stat(path, &st);
                   1204:        if (ret == 0)
                   1205:                type = IFTODT(st.st_mode);
1.14      jfb      1206:
1.44      jfb      1207:        if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14      jfb      1208:                return (NULL);
                   1209:        cfp->cf_parent = parent;
1.115     joris    1210:        cfp->cf_entry = pent;
1.54      joris    1211:
                   1212:        if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62      jfb      1213:                cfp->cf_flags |= CVS_DIRF_BASE;
1.14      jfb      1214:
1.44      jfb      1215:        if (ret == 0) {
                   1216:                cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62      jfb      1217:                if (cfp->cf_type == DT_REG)
                   1218:                        cfp->cf_mtime = st.st_mtime;
1.100     joris    1219:                cfp->cf_flags |= CVS_FILE_ONDISK;
1.44      jfb      1220:
                   1221:                if (ent == NULL)
1.100     joris    1222:                        if (cfp->cf_flags & CVS_DIRF_BASE)
                   1223:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
                   1224:                        else
                   1225:                                cfp->cf_cvstat = CVS_FST_UNKNOWN;
1.14      jfb      1226:                else {
1.44      jfb      1227:                        /* always show directories as up-to-date */
                   1228:                        if (ent->ce_type == CVS_ENT_DIR)
1.14      jfb      1229:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44      jfb      1230:                        else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                   1231:                                cfp->cf_cvstat = CVS_FST_ADDED;
                   1232:                        else {
                   1233:                                /* check last modified time */
1.90      joris    1234:                                if (ent->ce_mtime == (time_t)st.st_mtime) {
1.44      jfb      1235:                                        cfp->cf_cvstat = CVS_FST_UPTODATE;
1.90      joris    1236:                                } else {
1.44      jfb      1237:                                        cfp->cf_cvstat = CVS_FST_MODIFIED;
1.90      joris    1238:                                }
1.44      jfb      1239:                        }
1.94      joris    1240:
                   1241:                        cfp->cf_etime = ent->ce_mtime;
1.14      jfb      1242:                }
1.44      jfb      1243:        } else {
                   1244:                if (ent == NULL) {
1.92      joris    1245:                        /* assume it is a file and unknown */
                   1246:                        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                   1247:                        cfp->cf_type = DT_REG;
1.66      joris    1248:                } else {
                   1249:                        if (ent->ce_type == CVS_ENT_FILE)
                   1250:                                cfp->cf_type = DT_REG;
                   1251:                        else if (ent->ce_type == CVS_ENT_DIR)
                   1252:                                cfp->cf_type = DT_DIR;
                   1253:                        else
1.93      joris    1254:                                cvs_log(LP_WARN, "unknown ce_type %d",
1.66      joris    1255:                                    ent->ce_type);
1.67      joris    1256:
                   1257:                        if (ent->ce_status == CVS_ENT_REMOVED)
                   1258:                                cfp->cf_cvstat = CVS_FST_REMOVED;
1.90      joris    1259:                        else if (ent->ce_status == CVS_ENT_UPTODATE)
                   1260:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.96      joris    1261:                        else if (ent->ce_status == CVS_ENT_ADDED)
                   1262:                                cfp->cf_cvstat = CVS_FST_ADDED;
1.67      joris    1263:                        else
                   1264:                                cfp->cf_cvstat = CVS_FST_LOST;
1.66      joris    1265:                }
1.14      jfb      1266:        }
1.52      jfb      1267:
1.62      jfb      1268:        if (ent != NULL) {
                   1269:                /* steal the RCSNUM */
                   1270:                cfp->cf_lrev = ent->ce_rev;
1.72      jfb      1271:
1.77      jfb      1272:                if (ent->ce_type == CVS_ENT_FILE) {
                   1273:                        if (ent->ce_tag[0] != '\0') {
                   1274:                                cfp->cf_tag = cvs_strdup(ent->ce_tag);
                   1275:                                if (cfp->cf_tag == NULL) {
                   1276:                                        cvs_file_free(cfp);
                   1277:                                        return (NULL);
                   1278:                                }
                   1279:                        }
                   1280:
                   1281:                        if (ent->ce_opts[0] != '\0') {
                   1282:                                cfp->cf_opts = cvs_strdup(ent->ce_opts);
                   1283:                                if (cfp->cf_opts == NULL) {
                   1284:                                        cvs_file_free(cfp);
                   1285:                                        return (NULL);
                   1286:                                }
1.65      joris    1287:                        }
1.62      jfb      1288:                }
                   1289:        }
1.14      jfb      1290:
1.115     joris    1291:        if (cfp->cf_type == DT_DIR) {
                   1292:                if (cvs_load_dirinfo(cfp, flags) < 0) {
                   1293:                        cvs_file_free(cfp);
                   1294:                        return (NULL);
                   1295:                }
1.14      jfb      1296:        }
1.74      joris    1297:
                   1298:        if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
                   1299:            !strcmp(cfp->cf_repo, path))
                   1300:                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14      jfb      1301:
1.100     joris    1302:        /*
                   1303:         * In server mode, we do a few extra checks.
                   1304:         */
                   1305:        if (cvs_cmdop == CVS_OP_SERVER) {
                   1306:                /*
                   1307:                 * If for some reason a file was added,
                   1308:                 * but does not exist anymore, start complaining.
                   1309:                 */
                   1310:                if (!(cfp->cf_flags & CVS_FILE_ONDISK) &&
                   1311:                    (cfp->cf_cvstat == CVS_FST_ADDED) &&
                   1312:                    (cfp->cf_type != DT_DIR))
                   1313:                        cvs_log(LP_WARN, "new-born %s has disappeared", path);
                   1314:
                   1315:                /*
                   1316:                 * Any other needed checks?
                   1317:                 */
                   1318:        }
                   1319:
1.14      jfb      1320:        return (cfp);
1.23      jfb      1321: }
                   1322:
                   1323:
                   1324: static int
                   1325: cvs_file_cmpname(const char *name1, const char *name2)
                   1326: {
                   1327:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                   1328:            (strcasecmp(name1, name2));
1.88      joris    1329: }
                   1330:
                   1331: /*
1.100     joris    1332:  * remove any empty directories.
1.88      joris    1333:  */
                   1334: int
                   1335: cvs_file_prune(char *path)
                   1336: {
                   1337:        DIR *dirp;
                   1338:        int l, pwd, empty;
                   1339:        struct dirent *dp;
                   1340:        char fpath[MAXPATHLEN];
                   1341:        CVSENTRIES *entf;
1.89      joris    1342:        CVSFILE *cfp;
1.88      joris    1343:
                   1344:        pwd = (!strcmp(path, "."));
                   1345:
                   1346:        if ((dirp = opendir(path)) == NULL) {
                   1347:                cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
                   1348:                return (-1);
                   1349:        }
                   1350:
                   1351:        empty = 0;
                   1352:        entf = cvs_ent_open(path, O_RDWR);
                   1353:
                   1354:        while ((dp = readdir(dirp)) != NULL) {
                   1355:                if (!strcmp(dp->d_name, ".") ||
                   1356:                    !strcmp(dp->d_name, "..") ||
                   1357:                    !strcmp(dp->d_name, CVS_PATH_CVSDIR))
                   1358:                        continue;
                   1359:
                   1360:                empty++;
                   1361:                if (dp->d_type == DT_DIR) {
                   1362:                        l = snprintf(fpath, sizeof(fpath), "%s%s%s",
                   1363:                            (pwd) ? "" : path, (pwd) ? "" : "/", dp->d_name);
                   1364:                        if (l == -1 || l >= (int)sizeof(fpath)) {
                   1365:                                errno = ENAMETOOLONG;
                   1366:                                cvs_log(LP_ERRNO, "%s", fpath);
                   1367:                                continue;
                   1368:                        }
1.89      joris    1369:
                   1370:                        cfp = cvs_file_find(cvs_files, fpath);
                   1371:                        if (cfp == NULL)
1.91      joris    1372:                                continue;
                   1373:
                   1374:                        /* ignore unknown directories */
                   1375:                        if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
1.89      joris    1376:                                continue;
1.88      joris    1377:
                   1378:                        if (cvs_file_prune(fpath)) {
                   1379:                                empty--;
                   1380:                                if (entf)
                   1381:                                        cvs_ent_remove(entf, fpath);
                   1382:                        } else {
                   1383:                                empty++;
                   1384:                        }
                   1385:                }
                   1386:        }
                   1387:
                   1388:        closedir(dirp);
                   1389:        if (entf)
                   1390:                cvs_ent_close(entf);
                   1391:
                   1392:        empty = (empty == 0);
                   1393:        if (empty) {
1.106     xsa      1394:                if (cvs_rmdir(path) < 0) {
1.88      joris    1395:                        cvs_log(LP_ERR, "failed to prune `%s'", path);
                   1396:                        empty = 0;
                   1397:                }
1.93      joris    1398:        }
1.88      joris    1399:
                   1400:        return (empty);
1.14      jfb      1401: }