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

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