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

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