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

1.91    ! joris       1: /*     $OpenBSD: file.c,v 1.90 2005/06/17 15:09: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:
                     47:
                     48: #define CVS_IGN_STATIC    0x01     /* pattern is static, no need to glob */
                     49:
                     50: #define CVS_CHAR_ISMETA(c)  ((c == '*') || (c == '?') || (c == '['))
                     51:
                     52:
                     53: /* ignore pattern */
                     54: struct cvs_ignpat {
                     55:        char  ip_pat[MAXNAMLEN];
                     56:        int   ip_flags;
                     57:        TAILQ_ENTRY (cvs_ignpat) ip_list;
                     58: };
                     59:
                     60:
                     61: /*
                     62:  * Standard patterns to ignore.
                     63:  */
                     64: static const char *cvs_ign_std[] = {
                     65:        ".",
                     66:        "..",
                     67:        "*.o",
                     68:        "*.so",
1.45      xsa        69:        "*.a",
1.1       jfb        70:        "*.bak",
                     71:        "*.orig",
                     72:        "*.rej",
1.45      xsa        73:        "*.old",
1.1       jfb        74:        "*.exe",
                     75:        "*.depend",
1.45      xsa        76:        "*.obj",
                     77:        "*.elc",
                     78:        "*.ln",
                     79:        "*.olb",
1.1       jfb        80:        "CVS",
                     81:        "core",
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.1       jfb       107: TAILQ_HEAD(, cvs_ignpat)  cvs_ign_pats;
                    108:
                    109:
1.73      joris     110: static int cvs_file_getdir(CVSFILE *, int, char *, int (*)(CVSFILE *, void *), void *);
1.69      joris     111: static int     cvs_load_dirinfo  (CVSFILE *, int);
1.62      jfb       112: static int      cvs_file_sort    (struct cvs_flist *, u_int);
                    113: static int      cvs_file_cmp     (const void *, const void *);
                    114: static int      cvs_file_cmpname (const char *, const char *);
                    115: static CVSFILE* cvs_file_alloc   (const char *, u_int);
                    116: static CVSFILE* cvs_file_lget  (const char *, int, CVSFILE *, struct cvs_ent *);
1.3       jfb       117:
                    118:
1.1       jfb       119: /*
                    120:  * cvs_file_init()
                    121:  *
                    122:  */
                    123: int
                    124: cvs_file_init(void)
                    125: {
1.61      xsa       126:        int i, l;
1.1       jfb       127:        size_t len;
                    128:        char path[MAXPATHLEN], buf[MAXNAMLEN];
                    129:        FILE *ifp;
                    130:        struct passwd *pwd;
                    131:
                    132:        TAILQ_INIT(&cvs_ign_pats);
                    133:
1.53      jfb       134:        if ((cvs_addedrev = rcsnum_parse("0")) == NULL)
1.46      jfb       135:                return (-1);
1.4       jfb       136:
1.1       jfb       137:        /* standard patterns to ignore */
1.10      jfb       138:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.26      jfb       139:                cvs_file_ignore(cvs_ign_std[i]);
1.1       jfb       140:
                    141:        /* read the cvsignore file in the user's home directory, if any */
                    142:        pwd = getpwuid(getuid());
                    143:        if (pwd != NULL) {
1.61      xsa       144:                l = snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
                    145:                if (l == -1 || l >= (int)sizeof(path)) {
                    146:                        errno = ENAMETOOLONG;
                    147:                        cvs_log(LP_ERRNO, "%s", path);
                    148:                        return (-1);
                    149:                }
                    150:
1.1       jfb       151:                ifp = fopen(path, "r");
                    152:                if (ifp == NULL) {
                    153:                        if (errno != ENOENT)
1.34      jfb       154:                                cvs_log(LP_ERRNO,
                    155:                                    "failed to open user's cvsignore", path);
1.38      deraadt   156:                } else {
1.1       jfb       157:                        while (fgets(buf, sizeof(buf), ifp) != NULL) {
                    158:                                len = strlen(buf);
                    159:                                if (len == 0)
                    160:                                        continue;
                    161:                                if (buf[len - 1] != '\n') {
                    162:                                        cvs_log(LP_ERR, "line too long in `%s'",
                    163:                                            path);
                    164:                                }
                    165:                                buf[--len] = '\0';
                    166:                                cvs_file_ignore(buf);
                    167:                        }
                    168:                        (void)fclose(ifp);
                    169:                }
                    170:        }
                    171:
                    172:        return (0);
                    173: }
                    174:
                    175:
                    176: /*
                    177:  * cvs_file_ignore()
                    178:  *
                    179:  * Add the pattern <pat> to the list of patterns for files to ignore.
                    180:  * Returns 0 on success, or -1 on failure.
                    181:  */
                    182: int
                    183: cvs_file_ignore(const char *pat)
                    184: {
                    185:        char *cp;
                    186:        struct cvs_ignpat *ip;
                    187:
                    188:        ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
                    189:        if (ip == NULL) {
                    190:                cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
                    191:                return (-1);
                    192:        }
                    193:
                    194:        strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    195:
                    196:        /* check if we will need globbing for that pattern */
                    197:        ip->ip_flags = CVS_IGN_STATIC;
                    198:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    199:                if (CVS_CHAR_ISMETA(*cp)) {
                    200:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    201:                        break;
                    202:                }
                    203:        }
                    204:
                    205:        TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
                    206:
                    207:        return (0);
                    208: }
                    209:
                    210:
                    211: /*
1.5       jfb       212:  * cvs_file_chkign()
1.1       jfb       213:  *
                    214:  * Returns 1 if the filename <file> is matched by one of the ignore
                    215:  * patterns, or 0 otherwise.
                    216:  */
                    217: int
1.5       jfb       218: cvs_file_chkign(const char *file)
1.1       jfb       219: {
1.23      jfb       220:        int flags;
1.1       jfb       221:        struct cvs_ignpat *ip;
                    222:
1.23      jfb       223:        flags = FNM_PERIOD;
                    224:        if (cvs_nocase)
                    225:                flags |= FNM_CASEFOLD;
                    226:
1.1       jfb       227:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    228:                if (ip->ip_flags & CVS_IGN_STATIC) {
1.23      jfb       229:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1       jfb       230:                                return (1);
1.38      deraadt   231:                } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1       jfb       232:                        return (1);
                    233:        }
                    234:
                    235:        return (0);
                    236: }
                    237:
                    238:
                    239: /*
1.6       jfb       240:  * cvs_file_create()
1.1       jfb       241:  *
1.6       jfb       242:  * Create a new file whose path is specified in <path> and of type <type>.
1.26      jfb       243:  * If the type is DT_DIR, the CVS administrative repository and files will be
                    244:  * created.
1.25      jfb       245:  * Returns the created file on success, or NULL on failure.
1.1       jfb       246:  */
1.6       jfb       247: CVSFILE*
1.34      jfb       248: cvs_file_create(CVSFILE *parent, const char *path, u_int type, mode_t mode)
1.1       jfb       249: {
1.85      joris     250:        int fd, l;
1.84      joris     251:        int bail;
1.85      joris     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.85      joris     260:        bail = 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:
                    267:                /*
                    268:                 * If we do not have a valid root for this, try looking at
                    269:                 * the parent its root.
                    270:                 */
                    271:                if (cfp->cf_root == NULL) {
                    272:                        if (parent != NULL && parent->cf_root != NULL) {
                    273:                                cfp->cf_root =
                    274:                                    cvsroot_parse(parent->cf_root->cr_str);
                    275:                                if (cfp->cf_root == NULL)
                    276:                                        bail = 1;
                    277:                        } else {
                    278:                                bail = 1;
                    279:                        }
                    280:                }
                    281:
                    282:                /* we tried, too bad */
                    283:                if (bail) {
                    284:                        cvs_log(LP_ERR, "failed to obtain root info for `%s'",
                    285:                            path);
                    286:                        return (NULL);
                    287:                }
                    288:
1.85      joris     289:                if (cvs_repo_base != NULL) {
                    290:                        cvs_file_getpath(cfp, fp, sizeof(fp));
                    291:                        l = snprintf(repo, sizeof(repo), "%s/%s", cvs_repo_base,
                    292:                            fp);
                    293:                } else {
                    294:                        cvs_file_getpath(cfp, repo, sizeof(repo));
                    295:                        l = 0;
                    296:                }
                    297:
                    298:                if (l == -1 || l >= (int)sizeof(repo)) {
                    299:                        errno = ENAMETOOLONG;
                    300:                        cvs_log(LP_ERRNO, "%s", repo);
                    301:                        cvs_file_free(cfp);
                    302:                        return (NULL);
                    303:                }
                    304:
                    305:                cfp->cf_repo = strdup(repo);
1.62      jfb       306:                if (cfp->cf_repo == NULL) {
1.34      jfb       307:                        cvs_file_free(cfp);
                    308:                        return (NULL);
                    309:                }
1.33      joris     310:
1.79      joris     311:                if (((mkdir(path, mode) == -1) && (errno != EEXIST)) ||
1.78      joris     312:                    (cvs_mkadmin(path, cfp->cf_root->cr_str, cfp->cf_repo) < 0)) {
1.6       jfb       313:                        cvs_file_free(cfp);
                    314:                        return (NULL);
                    315:                }
1.26      jfb       316:
1.62      jfb       317:                ent = cvs_ent_open(path, O_RDWR);
                    318:                if (ent != NULL) {
                    319:                        cvs_ent_close(ent);
1.50      jfb       320:                }
1.38      deraadt   321:        } else {
1.6       jfb       322:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    323:                if (fd == -1) {
                    324:                        cvs_file_free(cfp);
1.1       jfb       325:                        return (NULL);
                    326:                }
1.6       jfb       327:                (void)close(fd);
1.1       jfb       328:        }
                    329:
1.6       jfb       330:        return (cfp);
1.3       jfb       331: }
                    332:
                    333:
                    334: /*
1.35      jfb       335:  * cvs_file_copy()
                    336:  *
                    337:  * Allocate space to create a copy of the file <orig>.  The copy inherits all
                    338:  * of the original's attributes, but does not inherit its children if the
                    339:  * original file is a directory.  Note that files copied using this mechanism
                    340:  * are linked to their parent, but the parent has no link to the file.  This
                    341:  * is so cvs_file_getpath() works.
                    342:  * Returns the copied file on success, or NULL on failure.  The returned
                    343:  * structure should be freed using cvs_file_free().
                    344:  */
                    345: CVSFILE*
                    346: cvs_file_copy(CVSFILE *orig)
                    347: {
                    348:        char path[MAXPATHLEN];
                    349:        CVSFILE *cfp;
                    350:
                    351:        cvs_file_getpath(orig, path, sizeof(path));
                    352:
                    353:        cfp = cvs_file_alloc(path, orig->cf_type);
                    354:        if (cfp == NULL)
                    355:                return (NULL);
                    356:
                    357:        cfp->cf_parent = orig->cf_parent;
                    358:        cfp->cf_mode = orig->cf_mode;
                    359:        cfp->cf_cvstat = orig->cf_cvstat;
                    360:
1.62      jfb       361:        if (orig->cf_type == DT_REG)
                    362:                cfp->cf_mtime = orig->cf_mtime;
                    363:        else if (orig->cf_type == DT_DIR) {
1.35      jfb       364:                /* XXX copy CVS directory attributes */
                    365:        }
                    366:
                    367:        return (cfp);
                    368: }
                    369:
                    370:
                    371: /*
1.3       jfb       372:  * cvs_file_get()
                    373:  *
                    374:  * Load a cvs_file structure with all the information pertaining to the file
                    375:  * <path>.
1.4       jfb       376:  * The <flags> parameter specifies various flags that alter the behaviour of
1.21      jfb       377:  * the function.  The CF_RECURSE flag causes the function to recursively load
                    378:  * subdirectories when <path> is a directory.
                    379:  * The CF_SORT flag causes the files to be sorted in alphabetical order upon
                    380:  * loading.  The special case of "." as a path specification generates
                    381:  * recursion for a single level and is equivalent to calling cvs_file_get() on
                    382:  * all files of that directory.
1.3       jfb       383:  * Returns a pointer to the cvs file structure, which must later be freed
                    384:  * with cvs_file_free().
                    385:  */
                    386:
1.14      jfb       387: CVSFILE*
1.73      joris     388: cvs_file_get(const char *path, int flags, int (*cb)(CVSFILE *, void *),
                    389:     void *arg)
1.3       jfb       390: {
1.68      joris     391:        char *files[1];
                    392:
                    393:        files[0] = path;
1.73      joris     394:        return cvs_file_getspec(files, 1, flags, cb, arg);
1.9       jfb       395: }
                    396:
                    397:
                    398: /*
                    399:  * cvs_file_getspec()
                    400:  *
                    401:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    402:  * whose size is given in <fsn>.
                    403:  * Returns a pointer to the lowest common subdirectory to all specified
                    404:  * files.
                    405:  */
                    406: CVSFILE*
1.73      joris     407: cvs_file_getspec(char **fspec, int fsn, int flags, int (*cb)(CVSFILE *, void *),
                    408:     void *arg)
1.9       jfb       409: {
1.26      jfb       410:        int i;
1.68      joris     411:        int pwd;
1.26      jfb       412:        char *sp, *np, pcopy[MAXPATHLEN];
1.68      joris     413:        CVSFILE *base, *nf;
                    414:        CVSENTRIES *entfile;
                    415:        struct cvs_ent *ent;
1.26      jfb       416:
1.68      joris     417:        base = cvs_file_lget(".", 0, NULL, NULL);
1.26      jfb       418:        if (base == NULL)
                    419:                return (NULL);
1.85      joris     420:
1.87      joris     421:        entfile = cvs_ent_open(".", O_RDONLY);
                    422:
1.85      joris     423:        /*
                    424:         * fill in the repository base (needed to construct repo's in
                    425:         * cvs_file_create).
                    426:         */
                    427:        if (base->cf_repo != NULL) {
                    428:                cvs_repo_base = strdup(base->cf_repo);
                    429:                if (cvs_repo_base == NULL) {
                    430:                        cvs_log(LP_ERR, "failed to duplicate repository base");
                    431:                        cvs_file_free(base);
                    432:                        if (entfile)
                    433:                                cvs_ent_close(entfile);
                    434:                        return (NULL);
                    435:                }
                    436:        }
1.26      jfb       437:
1.73      joris     438:        /* XXX - needed for some commands */
                    439:        if (cb != NULL) {
                    440:                if (cb(base, arg) != CVS_EX_OK) {
                    441:                        cvs_file_free(base);
1.87      joris     442:                        if (entfile)
                    443:                                cvs_ent_close(entfile);
1.73      joris     444:                        return (NULL);
                    445:                }
                    446:        }
                    447:
1.26      jfb       448:        for (i = 0; i < fsn; i++) {
                    449:                strlcpy(pcopy, fspec[i], sizeof(pcopy));
                    450:                sp = pcopy;
1.68      joris     451:                pwd = (!strcmp(pcopy, "."));
1.26      jfb       452:
1.68      joris     453:                np = strchr(sp, '/');
                    454:                if (np != NULL)
                    455:                        *np = '\0';
                    456:
                    457:                if (pwd) {
                    458:                        nf = base;
                    459:                } else {
                    460:                        nf = cvs_file_find(base, pcopy);
1.26      jfb       461:                        if (nf == NULL) {
1.68      joris     462:                                if (entfile != NULL)
                    463:                                        ent = cvs_ent_get(entfile, pcopy);
                    464:                                else
                    465:                                        ent = NULL;
                    466:                                nf = cvs_file_lget(pcopy, 0, base, ent);
1.26      jfb       467:                                if (nf == NULL) {
                    468:                                        cvs_file_free(base);
1.87      joris     469:                                        if (entfile)
                    470:                                                cvs_ent_close(entfile);
1.26      jfb       471:                                        return (NULL);
                    472:                                }
                    473:
1.68      joris     474:                                if (cvs_file_attach(base, nf) < 0) {
1.59      joris     475:                                        cvs_file_free(base);
1.87      joris     476:                                        if (entfile)
                    477:                                                cvs_ent_close(entfile);
1.59      joris     478:                                        return (NULL);
                    479:                                }
1.26      jfb       480:                        }
1.68      joris     481:                }
1.26      jfb       482:
1.68      joris     483:                if (nf->cf_type == DT_DIR) {
                    484:                        if (np != NULL)
                    485:                                *np++;
                    486:
1.73      joris     487:                        if (cvs_file_getdir(nf, flags, np, cb, arg) < 0) {
1.68      joris     488:                                cvs_file_free(base);
1.87      joris     489:                                if (entfile)
                    490:                                        cvs_ent_close(entfile);
1.68      joris     491:                                return (NULL);
1.26      jfb       492:                        }
1.73      joris     493:                } else {
                    494:                        if (cb != NULL) {
1.75      joris     495:                                if (cb(nf, arg) != CVS_EX_OK) {
                    496:                                        cvs_file_free(base);
1.87      joris     497:                                        if (entfile)
                    498:                                                cvs_ent_close(entfile);
1.73      joris     499:                                        return (NULL);
1.75      joris     500:                                }
1.73      joris     501:                        }
1.68      joris     502:                }
1.26      jfb       503:        }
1.87      joris     504:
                    505:        if (entfile)
                    506:                cvs_ent_close(entfile);
1.26      jfb       507:
                    508:        return (base);
1.3       jfb       509: }
                    510:
                    511:
                    512: /*
1.13      jfb       513:  * cvs_file_find()
                    514:  *
                    515:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    516:  * The file's pathname <path> must be relative to the base of <hier>.
                    517:  * Returns the entry on success, or NULL on failure.
                    518:  */
                    519: CVSFILE*
                    520: cvs_file_find(CVSFILE *hier, const char *path)
                    521: {
                    522:        char *pp, *sp, pbuf[MAXPATHLEN];
                    523:        CVSFILE *sf, *cf;
                    524:
                    525:        strlcpy(pbuf, path, sizeof(pbuf));
                    526:
                    527:        cf = hier;
                    528:        pp = pbuf;
                    529:        do {
                    530:                sp = strchr(pp, '/');
                    531:                if (sp != NULL)
1.24      jfb       532:                        *(sp++) = '\0';
1.13      jfb       533:
                    534:                /* special case */
                    535:                if (*pp == '.') {
                    536:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    537:                                /* request to go back to parent */
                    538:                                if (cf->cf_parent == NULL) {
                    539:                                        cvs_log(LP_NOTICE,
                    540:                                            "path %s goes back too far", path);
                    541:                                        return (NULL);
                    542:                                }
                    543:                                cf = cf->cf_parent;
                    544:                                continue;
1.38      deraadt   545:                        } else if (*(pp + 1) == '\0')
1.13      jfb       546:                                continue;
                    547:                }
                    548:
1.62      jfb       549:                SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.34      jfb       550:                        if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13      jfb       551:                                break;
                    552:                if (sf == NULL)
                    553:                        return (NULL);
                    554:
                    555:                cf = sf;
                    556:                pp = sp;
                    557:        } while (sp != NULL);
                    558:
1.24      jfb       559:        return (cf);
1.13      jfb       560: }
                    561:
                    562:
                    563: /*
1.34      jfb       564:  * cvs_file_getpath()
                    565:  *
                    566:  * Get the full path of the file <file> and store it in <buf>, which is of
                    567:  * size <len>.  For portability, it is recommended that <buf> always be
                    568:  * at least MAXPATHLEN bytes long.
                    569:  * Returns a pointer to the start of the path on success, or NULL on failure.
                    570:  */
                    571: char*
                    572: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
                    573: {
                    574:        u_int i;
1.77      jfb       575:        const char *fp, *namevec[CVS_FILE_MAXDEPTH];
1.34      jfb       576:        CVSFILE *top;
                    577:
                    578:        buf[0] = '\0';
                    579:        i = CVS_FILE_MAXDEPTH;
                    580:        memset(namevec, 0, sizeof(namevec));
                    581:
                    582:        /* find the top node */
                    583:        for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
                    584:                fp = CVS_FILE_NAME(top);
                    585:
                    586:                /* skip self-references */
                    587:                if ((fp[0] == '.') && (fp[1] == '\0'))
                    588:                        continue;
                    589:                namevec[--i] = fp;
                    590:        }
                    591:
                    592:        if (i == 0)
                    593:                return (NULL);
                    594:        else if (i == CVS_FILE_MAXDEPTH) {
                    595:                strlcpy(buf, ".", len);
                    596:                return (buf);
                    597:        }
                    598:
                    599:        while (i < CVS_FILE_MAXDEPTH - 1) {
                    600:                strlcat(buf, namevec[i++], len);
                    601:                strlcat(buf, "/", len);
                    602:        }
                    603:        strlcat(buf, namevec[i], len);
                    604:
                    605:        return (buf);
                    606: }
                    607:
                    608:
                    609: /*
1.22      jfb       610:  * cvs_file_attach()
                    611:  *
                    612:  * Attach the file <file> as one of the children of parent <parent>, which
                    613:  * has to be a file of type DT_DIR.
                    614:  * Returns 0 on success, or -1 on failure.
                    615:  */
                    616: int
                    617: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
                    618: {
                    619:        if (parent->cf_type != DT_DIR)
                    620:                return (-1);
                    621:
1.62      jfb       622:        SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22      jfb       623:        file->cf_parent = parent;
                    624:
                    625:        return (0);
                    626: }
                    627:
                    628:
                    629: /*
1.68      joris     630:  * Load directory information
1.3       jfb       631:  */
1.6       jfb       632: static int
1.69      joris     633: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3       jfb       634: {
1.68      joris     635:        char fpath[MAXPATHLEN];
                    636:        char pbuf[MAXPATHLEN];
1.20      jfb       637:        struct stat st;
1.68      joris     638:        int l;
1.7       jfb       639:
1.34      jfb       640:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.62      jfb       641:        cf->cf_root = cvsroot_get(fpath);
1.91    ! joris     642:        if (cf->cf_root == NULL) {
        !           643:                /*
        !           644:                 * Do not fail here for an unknown directory.
        !           645:                 */
        !           646:                if (cf->cf_cvstat == CVS_FST_UNKNOWN)
        !           647:                        return (0);
1.48      jfb       648:                return (-1);
1.91    ! joris     649:        }
1.48      jfb       650:
1.74      joris     651:        if (flags & CF_MKADMIN)
1.78      joris     652:                cvs_mkadmin(fpath, cf->cf_root->cr_str, NULL);
1.69      joris     653:
1.74      joris     654:        /* if the CVS administrative directory exists, load the info */
                    655:        l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
                    656:        if (l == -1 || l >= (int)sizeof(pbuf)) {
                    657:                errno = ENAMETOOLONG;
                    658:                cvs_log(LP_ERRNO, "%s", pbuf);
                    659:                return (-1);
                    660:        }
1.61      xsa       661:
1.74      joris     662:        if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
                    663:                if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
                    664:                        cf->cf_repo = strdup(pbuf);
                    665:                        if (cf->cf_repo == NULL) {
                    666:                                cvs_log(LP_ERRNO,
                    667:                                    "failed to dup repository string");
                    668:                                return (-1);
1.20      jfb       669:                        }
1.68      joris     670:                }
                    671:        }
1.26      jfb       672:
1.68      joris     673:        return (0);
                    674: }
                    675:
                    676: /*
                    677:  * cvs_file_getdir()
                    678:  *
                    679:  * Get a cvs directory structure for the directory whose path is <dir>.
                    680:  * This function should not free the directory information on error, as this
                    681:  * is performed by cvs_file_free().
                    682:  */
                    683: static int
1.73      joris     684: cvs_file_getdir(CVSFILE *cf, int flags, char *path, int (*cb)(CVSFILE *, void *), void *arg)
1.68      joris     685: {
1.91    ! joris     686:        int l, ret;
1.68      joris     687:        int check_entry;
                    688:        u_int ndirs, nfiles;
                    689:        char *cur, *np;
                    690:        char pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
                    691:        struct dirent *ent;
1.76      joris     692:        CVSFILE *cfp;
1.68      joris     693:        struct cvs_ent *cvsent;
                    694:        struct cvs_flist dirs;
                    695:        DIR *dirp;
                    696:        CVSENTRIES *entfile;
                    697:
1.91    ! joris     698:        ret = -1;
1.81      joris     699:        check_entry = 1;
                    700:        ndirs = nfiles = 0;
1.68      joris     701:        SIMPLEQ_INIT(&dirs);
                    702:
                    703:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    704:
                    705:        cur = np = NULL;
                    706:        if (path != NULL) {
                    707:                cur = strchr(path, '/');
                    708:                if (cur != NULL) {
                    709:                        *cur = '\0';
                    710:                        np = cur + 1;
                    711:                        if (np != NULL && *np == '\0')
                    712:                                np = NULL;
1.20      jfb       713:                }
                    714:        }
1.14      jfb       715:
1.56      jfb       716:        if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26      jfb       717:                return (0);
                    718:
1.73      joris     719:        /* callback for the directory entry */
                    720:        if (cb != NULL) {
                    721:                if (cb(cf, arg) != CVS_EX_OK)
                    722:                        return (-1);
                    723:        }
                    724:
1.91    ! joris     725:        cf->cf_root = cvsroot_get(fpath);
        !           726:        if (cf->cf_root == NULL) {
        !           727:                /*
        !           728:                 * Do not fail here for an unknown directory.
        !           729:                 */
        !           730:                if (cf->cf_cvstat == CVS_FST_UNKNOWN)
        !           731:                        return (0);
        !           732:                return (-1);
        !           733:        }
        !           734:
1.68      joris     735:        dirp = opendir(fpath);
                    736:        if (dirp == NULL) {
                    737:                cvs_log(LP_ERRNO, "failed to open directory %s", fpath);
1.6       jfb       738:                return (-1);
1.3       jfb       739:        }
                    740:
1.91    ! joris     741:        entfile = cvs_ent_open(fpath, O_RDONLY);
1.68      joris     742:        while ((ent = readdir(dirp)) != NULL) {
                    743:                if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
                    744:                        continue;
1.11      jfb       745:
1.68      joris     746:                if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
                    747:                        continue;
1.24      jfb       748:
1.68      joris     749:                if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
                    750:                        if (entfile != NULL)
                    751:                                (void)cvs_ent_remove(entfile,
                    752:                                    ent->d_name);
                    753:                        continue;
                    754:                }
1.70      joris     755:
                    756:                if ((ent->d_type != DT_DIR) && (flags & CF_NOFILES))
                    757:                        continue;
1.56      jfb       758:
1.68      joris     759:                if (path != NULL) {
                    760:                        if (strcmp(path, ent->d_name))
1.56      jfb       761:                                continue;
1.68      joris     762:                }
1.11      jfb       763:
1.68      joris     764:                l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
                    765:                    ent->d_name);
                    766:                if (l == -1 || l >= (int)sizeof(pbuf)) {
                    767:                        errno = ENAMETOOLONG;
                    768:                        cvs_log(LP_ERRNO, "%s", pbuf);
                    769:                        closedir(dirp);
1.91    ! joris     770:                        goto done;
1.68      joris     771:                }
1.61      xsa       772:
1.68      joris     773:                cfp = cvs_file_find(cf, ent->d_name);
                    774:                if (cfp == NULL) {
1.62      jfb       775:                        if (entfile != NULL)
                    776:                                cvsent = cvs_ent_get(entfile, ent->d_name);
1.71      joris     777:                        else
                    778:                                cvsent = NULL;
                    779:
1.62      jfb       780:                        cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
1.68      joris     781:
1.55      jfb       782:                        if (cfp == NULL) {
1.68      joris     783:                                closedir(dirp);
1.91    ! joris     784:                                goto done;
1.55      jfb       785:                        }
1.62      jfb       786:                        if (entfile != NULL)
                    787:                                cvs_ent_remove(entfile, cfp->cf_name);
1.55      jfb       788:
1.68      joris     789:                        if (cfp->cf_type != DT_DIR) {
1.62      jfb       790:                                SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp,
1.68      joris     791:                                    cf_list);
1.62      jfb       792:                                nfiles++;
1.11      jfb       793:                        }
1.68      joris     794:                } else {
                    795:                        cfp->cf_flags |= CVS_GDIR_IGNORE;
1.4       jfb       796:                }
1.14      jfb       797:
1.68      joris     798:                if (cfp->cf_type == DT_DIR) {
                    799:                        ndirs++;
                    800:                        SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.73      joris     801:                } else {
                    802:                        /* callback for the file */
                    803:                        if (cb != NULL) {
                    804:                                if (cb(cfp, arg) != CVS_EX_OK) {
                    805:                                        closedir(dirp);
1.91    ! joris     806:                                        goto done;
1.73      joris     807:                                }
                    808:                        }
1.68      joris     809:                }
                    810:
                    811:                if (path != NULL) {
                    812:                        check_entry = 0;
                    813:                        break;
                    814:                }
                    815:        }
                    816:
                    817:        closedir(dirp);
                    818:
                    819:        if (entfile != NULL && check_entry) {
1.62      jfb       820:                while ((cvsent = cvs_ent_next(entfile)) != NULL) {
1.68      joris     821:                        if (path != NULL) {
                    822:                                if (strcmp(cvsent->ce_name, path))
                    823:                                        continue;
                    824:                        }
                    825:
1.61      xsa       826:                        l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44      jfb       827:                            cvsent->ce_name);
1.61      xsa       828:                        if (l == -1 || l >= (int)sizeof(pbuf)) {
                    829:                                errno = ENAMETOOLONG;
                    830:                                cvs_log(LP_ERRNO, "%s", pbuf);
1.91    ! joris     831:                                goto done;
1.61      xsa       832:                        }
                    833:
1.68      joris     834:                        cfp = cvs_file_find(cf, cvsent->ce_name);
                    835:                        if (cfp == NULL) {
                    836:                                cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
                    837:                                if (cfp == NULL)
                    838:                                        continue;
                    839:
                    840:                                if (cfp->cf_type != DT_DIR) {
                    841:                                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files),
                    842:                                            cfp, cf_list);
1.62      jfb       843:                                        nfiles++;
1.44      jfb       844:                                }
1.68      joris     845:                        } else {
                    846:                                cfp->cf_flags |= CVS_GDIR_IGNORE;
                    847:                        }
                    848:
                    849:                        if (cfp->cf_type == DT_DIR) {
                    850:                                ndirs++;
                    851:                                SIMPLEQ_INSERT_TAIL(&dirs, cfp,
                    852:                                    cf_list);
1.73      joris     853:                        } else {
                    854:                                /* callback for the file */
                    855:                                if (cb != NULL) {
                    856:                                        if (cb(cfp, arg) != CVS_EX_OK)
1.91    ! joris     857:                                                goto done;
1.73      joris     858:                                }
1.44      jfb       859:                        }
1.68      joris     860:
                    861:                        if (path != NULL)
                    862:                                break;
1.44      jfb       863:                }
1.40      jfb       864:        }
1.34      jfb       865:
1.10      jfb       866:        if (flags & CF_SORT) {
1.68      joris     867:                if (nfiles > 0)
                    868:                        cvs_file_sort(&(cf->cf_files), nfiles);
                    869:                if (ndirs > 0)
                    870:                        cvs_file_sort(&dirs, ndirs);
1.10      jfb       871:        }
1.26      jfb       872:
1.62      jfb       873:        while (!SIMPLEQ_EMPTY(&dirs)) {
                    874:                cfp = SIMPLEQ_FIRST(&dirs);
                    875:                SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68      joris     876:
                    877:                if (!(cfp->cf_flags & CVS_GDIR_IGNORE))
                    878:                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
                    879:                else
                    880:                        cfp->cf_flags &= ~CVS_GDIR_IGNORE;
                    881:
1.73      joris     882:                if (cvs_file_getdir(cfp, flags, np, cb, arg) < 0) {
1.82      xsa       883:                        cvs_log(LP_ERR, "failed to get %s", CVS_FILE_NAME(cfp));
1.68      joris     884:                        continue;
                    885:                }
1.26      jfb       886:        }
1.3       jfb       887:
1.91    ! joris     888:        ret = 0;
        !           889: done:
        !           890:        if (entfile != NULL)
        !           891:                cvs_ent_close(entfile);
        !           892:
        !           893:        return (ret);
1.3       jfb       894: }
                    895:
                    896:
                    897: /*
                    898:  * cvs_file_free()
                    899:  *
                    900:  * Free a cvs_file structure and its contents.
                    901:  */
                    902: void
1.14      jfb       903: cvs_file_free(CVSFILE *cf)
1.3       jfb       904: {
1.62      jfb       905:        CVSFILE *child;
                    906:
1.47      jfb       907:        if (cf->cf_name != NULL)
1.57      jfb       908:                cvs_strfree(cf->cf_name);
1.62      jfb       909:
                    910:        if (cf->cf_type == DT_DIR) {
                    911:                if (cf->cf_root != NULL)
                    912:                        cvsroot_free(cf->cf_root);
                    913:                if (cf->cf_repo != NULL)
                    914:                        free(cf->cf_repo);
                    915:                while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
                    916:                        child = SIMPLEQ_FIRST(&(cf->cf_files));
                    917:                        SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
                    918:                        cvs_file_free(child);
                    919:                }
1.64      joris     920:        } else {
                    921:                if (cf->cf_tag != NULL)
                    922:                        cvs_strfree(cf->cf_tag);
1.77      jfb       923:                if (cf->cf_opts != NULL)
                    924:                        cvs_strfree(cf->cf_opts);
1.62      jfb       925:        }
1.64      joris     926:
1.3       jfb       927:        free(cf);
1.5       jfb       928: }
                    929:
                    930:
                    931: /*
                    932:  * cvs_file_examine()
                    933:  *
                    934:  * Examine the contents of the CVS file structure <cf> with the function
                    935:  * <exam>.  The function is called for all subdirectories and files of the
                    936:  * root file.
                    937:  */
                    938: int
                    939: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    940: {
                    941:        int ret;
1.14      jfb       942:        CVSFILE *fp;
1.5       jfb       943:
                    944:        if (cf->cf_type == DT_DIR) {
                    945:                ret = (*exam)(cf, arg);
1.62      jfb       946:                SIMPLEQ_FOREACH(fp, &(cf->cf_files), cf_list) {
1.5       jfb       947:                        ret = cvs_file_examine(fp, exam, arg);
1.60      joris     948:                        if (ret != 0)
1.13      jfb       949:                                break;
1.5       jfb       950:                }
1.38      deraadt   951:        } else
1.13      jfb       952:                ret = (*exam)(cf, arg);
                    953:
                    954:        return (ret);
1.3       jfb       955: }
                    956:
                    957: /*
                    958:  * cvs_file_sort()
                    959:  *
1.16      jfb       960:  * Sort a list of cvs file structures according to their filename.  The list
                    961:  * <flp> is modified according to the sorting algorithm.  The number of files
                    962:  * in the list must be given by <nfiles>.
                    963:  * Returns 0 on success, or -1 on failure.
1.3       jfb       964:  */
                    965: static int
1.16      jfb       966: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb       967: {
                    968:        int i;
                    969:        size_t nb;
1.16      jfb       970:        CVSFILE *cf, **cfvec;
                    971:
                    972:        cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
                    973:        if (cfvec == NULL) {
                    974:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                    975:                return (-1);
                    976:        }
1.3       jfb       977:
                    978:        i = 0;
1.62      jfb       979:        SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16      jfb       980:                if (i == (int)nfiles) {
1.3       jfb       981:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb       982:                        /* rebuild the list and abort sorting */
                    983:                        while (--i >= 0)
1.62      jfb       984:                                SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16      jfb       985:                        free(cfvec);
1.3       jfb       986:                        return (-1);
                    987:                }
1.16      jfb       988:                cfvec[i++] = cf;
1.3       jfb       989:
                    990:                /* now unlink it from the list,
                    991:                 * we'll put it back in order later
                    992:                 */
1.62      jfb       993:                SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3       jfb       994:        }
                    995:
                    996:        /* clear the list just in case */
1.62      jfb       997:        SIMPLEQ_INIT(flp);
1.3       jfb       998:        nb = (size_t)i;
                    999:
                   1000:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                   1001:
                   1002:        /* rebuild the list from the bottom up */
                   1003:        for (i = (int)nb - 1; i >= 0; i--)
1.62      jfb      1004:                SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb      1005:
1.16      jfb      1006:        free(cfvec);
1.3       jfb      1007:        return (0);
                   1008: }
                   1009:
                   1010:
                   1011: static int
                   1012: cvs_file_cmp(const void *f1, const void *f2)
                   1013: {
1.41      jfb      1014:        const CVSFILE *cf1, *cf2;
1.62      jfb      1015:        cf1 = *(CVSFILE * const *)f1;
                   1016:        cf2 = *(CVSFILE * const *)f2;
1.34      jfb      1017:        return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6       jfb      1018: }
                   1019:
                   1020:
1.34      jfb      1021: /*
                   1022:  * cvs_file_alloc()
                   1023:  *
                   1024:  * Allocate a CVSFILE structure and initialize its internals.
                   1025:  */
1.6       jfb      1026: CVSFILE*
                   1027: cvs_file_alloc(const char *path, u_int type)
                   1028: {
                   1029:        CVSFILE *cfp;
                   1030:
1.14      jfb      1031:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb      1032:        if (cfp == NULL) {
                   1033:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                   1034:                return (NULL);
                   1035:        }
                   1036:        memset(cfp, 0, sizeof(*cfp));
                   1037:
1.62      jfb      1038:        cfp->cf_type = type;
                   1039:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                   1040:
                   1041:        if (type == DT_DIR) {
                   1042:                SIMPLEQ_INIT(&(cfp->cf_files));
                   1043:        }
                   1044:
1.57      jfb      1045:        cfp->cf_name = cvs_strdup(basename(path));
1.34      jfb      1046:        if (cfp->cf_name == NULL) {
1.57      jfb      1047:                cvs_log(LP_ERR, "failed to copy file name");
1.58      tedu     1048:                cvs_file_free(cfp);
1.6       jfb      1049:                return (NULL);
                   1050:        }
                   1051:
                   1052:        return (cfp);
1.1       jfb      1053: }
1.14      jfb      1054:
                   1055:
                   1056: /*
                   1057:  * cvs_file_lget()
                   1058:  *
                   1059:  * Get the file and link it with the parent right away.
1.22      jfb      1060:  * Returns a pointer to the created file structure on success, or NULL on
                   1061:  * failure.
1.14      jfb      1062:  */
                   1063: static CVSFILE*
1.62      jfb      1064: cvs_file_lget(const char *path, int flags, CVSFILE *parent, struct cvs_ent *ent)
1.14      jfb      1065: {
1.44      jfb      1066:        int ret, cwd;
                   1067:        u_int type;
1.14      jfb      1068:        struct stat st;
                   1069:        CVSFILE *cfp;
                   1070:
1.44      jfb      1071:        type = DT_UNKNOWN;
                   1072:        cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.62      jfb      1073:
1.44      jfb      1074:        ret = stat(path, &st);
                   1075:        if (ret == 0)
                   1076:                type = IFTODT(st.st_mode);
1.14      jfb      1077:
1.44      jfb      1078:        if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14      jfb      1079:                return (NULL);
                   1080:        cfp->cf_parent = parent;
1.54      joris    1081:
                   1082:        if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62      jfb      1083:                cfp->cf_flags |= CVS_DIRF_BASE;
1.14      jfb      1084:
1.44      jfb      1085:        if (ret == 0) {
                   1086:                cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62      jfb      1087:                if (cfp->cf_type == DT_REG)
                   1088:                        cfp->cf_mtime = st.st_mtime;
1.44      jfb      1089:
                   1090:                if (ent == NULL)
                   1091:                        cfp->cf_cvstat = (cwd == 1) ?
                   1092:                            CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14      jfb      1093:                else {
1.44      jfb      1094:                        /* always show directories as up-to-date */
                   1095:                        if (ent->ce_type == CVS_ENT_DIR)
1.14      jfb      1096:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44      jfb      1097:                        else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                   1098:                                cfp->cf_cvstat = CVS_FST_ADDED;
                   1099:                        else {
                   1100:                                /* check last modified time */
1.90      joris    1101:                                if (ent->ce_mtime == (time_t)st.st_mtime) {
1.44      jfb      1102:                                        cfp->cf_cvstat = CVS_FST_UPTODATE;
1.90      joris    1103:                                } else {
1.44      jfb      1104:                                        cfp->cf_cvstat = CVS_FST_MODIFIED;
1.90      joris    1105:                                }
1.44      jfb      1106:                        }
1.14      jfb      1107:                }
1.44      jfb      1108:        } else {
                   1109:                if (ent == NULL) {
                   1110:                        cvs_log(LP_ERR, "no Entry and no file for `%s'",
                   1111:                            CVS_FILE_NAME(cfp));
                   1112:                        cvs_file_free(cfp);
                   1113:                        return (NULL);
1.66      joris    1114:                } else {
                   1115:                        if (ent->ce_type == CVS_ENT_FILE)
                   1116:                                cfp->cf_type = DT_REG;
                   1117:                        else if (ent->ce_type == CVS_ENT_DIR)
                   1118:                                cfp->cf_type = DT_DIR;
                   1119:                        else
                   1120:                                cvs_log(LP_WARN, "unknown ce_type %d",
                   1121:                                    ent->ce_type);
1.67      joris    1122:
                   1123:                        if (ent->ce_status == CVS_ENT_REMOVED)
                   1124:                                cfp->cf_cvstat = CVS_FST_REMOVED;
1.90      joris    1125:                        else if (ent->ce_status == CVS_ENT_UPTODATE)
                   1126:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.67      joris    1127:                        else
                   1128:                                cfp->cf_cvstat = CVS_FST_LOST;
1.66      joris    1129:                }
1.14      jfb      1130:        }
1.52      jfb      1131:
1.62      jfb      1132:        if (ent != NULL) {
                   1133:                /* steal the RCSNUM */
                   1134:                cfp->cf_lrev = ent->ce_rev;
1.72      jfb      1135:                ent->ce_rev = NULL;
                   1136:
1.77      jfb      1137:                if (ent->ce_type == CVS_ENT_FILE) {
                   1138:                        if (ent->ce_tag[0] != '\0') {
                   1139:                                cfp->cf_tag = cvs_strdup(ent->ce_tag);
                   1140:                                if (cfp->cf_tag == NULL) {
                   1141:                                        cvs_file_free(cfp);
                   1142:                                        return (NULL);
                   1143:                                }
                   1144:                        }
                   1145:
                   1146:                        if (ent->ce_opts[0] != '\0') {
                   1147:                                cfp->cf_opts = cvs_strdup(ent->ce_opts);
                   1148:                                if (cfp->cf_opts == NULL) {
                   1149:                                        cvs_file_free(cfp);
                   1150:                                        return (NULL);
                   1151:                                }
1.65      joris    1152:                        }
1.62      jfb      1153:                }
                   1154:        }
1.14      jfb      1155:
1.69      joris    1156:        if ((cfp->cf_type == DT_DIR) && (cvs_load_dirinfo(cfp, flags) < 0)) {
1.26      jfb      1157:                cvs_file_free(cfp);
                   1158:                return (NULL);
1.14      jfb      1159:        }
1.74      joris    1160:
                   1161:        if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
                   1162:            !strcmp(cfp->cf_repo, path))
                   1163:                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14      jfb      1164:
                   1165:        return (cfp);
1.23      jfb      1166: }
                   1167:
                   1168:
                   1169: static int
                   1170: cvs_file_cmpname(const char *name1, const char *name2)
                   1171: {
                   1172:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                   1173:            (strcasecmp(name1, name2));
1.88      joris    1174: }
                   1175:
                   1176: /*
                   1177:  * remove a directory if it does not contain
                   1178:  * any files other than the CVS/ administrative files.
                   1179:  */
                   1180: int
                   1181: cvs_file_prune(char *path)
                   1182: {
                   1183:        DIR *dirp;
                   1184:        int l, pwd, empty;
                   1185:        struct dirent *dp;
                   1186:        char fpath[MAXPATHLEN];
                   1187:        CVSENTRIES *entf;
1.89      joris    1188:        CVSFILE *cfp;
1.88      joris    1189:
                   1190:        pwd = (!strcmp(path, "."));
                   1191:
                   1192:        if ((dirp = opendir(path)) == NULL) {
                   1193:                cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
                   1194:                return (-1);
                   1195:        }
                   1196:
                   1197:        empty = 0;
                   1198:        entf = cvs_ent_open(path, O_RDWR);
                   1199:
                   1200:        while ((dp = readdir(dirp)) != NULL) {
                   1201:                if (!strcmp(dp->d_name, ".") ||
                   1202:                    !strcmp(dp->d_name, "..") ||
                   1203:                    !strcmp(dp->d_name, CVS_PATH_CVSDIR))
                   1204:                        continue;
                   1205:
                   1206:                empty++;
                   1207:                if (dp->d_type == DT_DIR) {
                   1208:                        l = snprintf(fpath, sizeof(fpath), "%s%s%s",
                   1209:                            (pwd) ? "" : path, (pwd) ? "" : "/", dp->d_name);
                   1210:                        if (l == -1 || l >= (int)sizeof(fpath)) {
                   1211:                                errno = ENAMETOOLONG;
                   1212:                                cvs_log(LP_ERRNO, "%s", fpath);
                   1213:                                continue;
                   1214:                        }
1.89      joris    1215:
                   1216:                        cfp = cvs_file_find(cvs_files, fpath);
                   1217:                        if (cfp == NULL)
1.91    ! joris    1218:                                continue;
        !          1219:
        !          1220:                        /* ignore unknown directories */
        !          1221:                        if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
1.89      joris    1222:                                continue;
1.88      joris    1223:
                   1224:                        if (cvs_file_prune(fpath)) {
                   1225:                                empty--;
                   1226:                                if (entf)
                   1227:                                        cvs_ent_remove(entf, fpath);
                   1228:                        } else {
                   1229:                                empty++;
                   1230:                        }
                   1231:                }
                   1232:        }
                   1233:
                   1234:        closedir(dirp);
                   1235:        if (entf)
                   1236:                cvs_ent_close(entf);
                   1237:
                   1238:        empty = (empty == 0);
                   1239:        if (empty) {
                   1240:                if (cvs_remove_dir(path) < 0) {
                   1241:                        cvs_log(LP_ERR, "failed to prune `%s'", path);
                   1242:                        empty = 0;
                   1243:                }
                   1244:        }
                   1245:
                   1246:        return (empty);
1.14      jfb      1247: }