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

1.74    ! joris       1: /*     $OpenBSD: file.c,v 1.73 2005/05/20 05:13:44 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:
                     31: #include <pwd.h>
                     32: #include <errno.h>
                     33: #include <stdio.h>
                     34: #include <fcntl.h>
1.51      jfb        35: #include <libgen.h>
1.1       jfb        36: #include <dirent.h>
                     37: #include <stdlib.h>
                     38: #include <unistd.h>
                     39: #include <string.h>
                     40: #include <fnmatch.h>
                     41:
                     42: #include "cvs.h"
                     43: #include "log.h"
1.14      jfb        44: #include "file.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:
                    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.6       jfb       251:        int fd;
1.34      jfb       252:        char fp[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.22      jfb       260:        cfp->cf_mode = mode;
1.34      jfb       261:        cfp->cf_parent = parent;
1.1       jfb       262:
1.34      jfb       263:        if (type == DT_DIR) {
1.62      jfb       264:                cfp->cf_root = cvsroot_get(path);
                    265:                cfp->cf_repo = strdup(cvs_file_getpath(cfp,
1.34      jfb       266:                    fp, sizeof(fp)));
1.62      jfb       267:                if (cfp->cf_repo == NULL) {
1.34      jfb       268:                        cvs_file_free(cfp);
                    269:                        return (NULL);
                    270:                }
1.33      joris     271:
1.26      jfb       272:                if ((mkdir(path, mode) == -1) || (cvs_mkadmin(cfp, mode) < 0)) {
1.6       jfb       273:                        cvs_file_free(cfp);
                    274:                        return (NULL);
                    275:                }
1.26      jfb       276:
1.62      jfb       277:                ent = cvs_ent_open(path, O_RDWR);
                    278:                if (ent != NULL) {
                    279:                        cvs_ent_close(ent);
1.50      jfb       280:                }
1.38      deraadt   281:        } else {
1.6       jfb       282:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    283:                if (fd == -1) {
                    284:                        cvs_file_free(cfp);
1.1       jfb       285:                        return (NULL);
                    286:                }
1.6       jfb       287:                (void)close(fd);
1.1       jfb       288:        }
                    289:
1.6       jfb       290:        return (cfp);
1.3       jfb       291: }
                    292:
                    293:
                    294: /*
1.35      jfb       295:  * cvs_file_copy()
                    296:  *
                    297:  * Allocate space to create a copy of the file <orig>.  The copy inherits all
                    298:  * of the original's attributes, but does not inherit its children if the
                    299:  * original file is a directory.  Note that files copied using this mechanism
                    300:  * are linked to their parent, but the parent has no link to the file.  This
                    301:  * is so cvs_file_getpath() works.
                    302:  * Returns the copied file on success, or NULL on failure.  The returned
                    303:  * structure should be freed using cvs_file_free().
                    304:  */
                    305: CVSFILE*
                    306: cvs_file_copy(CVSFILE *orig)
                    307: {
                    308:        char path[MAXPATHLEN];
                    309:        CVSFILE *cfp;
                    310:
                    311:        cvs_file_getpath(orig, path, sizeof(path));
                    312:
                    313:        cfp = cvs_file_alloc(path, orig->cf_type);
                    314:        if (cfp == NULL)
                    315:                return (NULL);
                    316:
                    317:        cfp->cf_parent = orig->cf_parent;
                    318:        cfp->cf_mode = orig->cf_mode;
                    319:        cfp->cf_cvstat = orig->cf_cvstat;
                    320:
1.62      jfb       321:        if (orig->cf_type == DT_REG)
                    322:                cfp->cf_mtime = orig->cf_mtime;
                    323:        else if (orig->cf_type == DT_DIR) {
1.35      jfb       324:                /* XXX copy CVS directory attributes */
                    325:        }
                    326:
                    327:        return (cfp);
                    328: }
                    329:
                    330:
                    331: /*
1.3       jfb       332:  * cvs_file_get()
                    333:  *
                    334:  * Load a cvs_file structure with all the information pertaining to the file
                    335:  * <path>.
1.4       jfb       336:  * The <flags> parameter specifies various flags that alter the behaviour of
1.21      jfb       337:  * the function.  The CF_RECURSE flag causes the function to recursively load
                    338:  * subdirectories when <path> is a directory.
                    339:  * The CF_SORT flag causes the files to be sorted in alphabetical order upon
                    340:  * loading.  The special case of "." as a path specification generates
                    341:  * recursion for a single level and is equivalent to calling cvs_file_get() on
                    342:  * all files of that directory.
1.3       jfb       343:  * Returns a pointer to the cvs file structure, which must later be freed
                    344:  * with cvs_file_free().
                    345:  */
                    346:
1.14      jfb       347: CVSFILE*
1.73      joris     348: cvs_file_get(const char *path, int flags, int (*cb)(CVSFILE *, void *),
                    349:     void *arg)
1.3       jfb       350: {
1.68      joris     351:        char *files[1];
                    352:
                    353:        files[0] = path;
1.73      joris     354:        return cvs_file_getspec(files, 1, flags, cb, arg);
1.9       jfb       355: }
                    356:
                    357:
                    358: /*
                    359:  * cvs_file_getspec()
                    360:  *
                    361:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    362:  * whose size is given in <fsn>.
                    363:  * Returns a pointer to the lowest common subdirectory to all specified
                    364:  * files.
                    365:  */
                    366: CVSFILE*
1.73      joris     367: cvs_file_getspec(char **fspec, int fsn, int flags, int (*cb)(CVSFILE *, void *),
                    368:     void *arg)
1.9       jfb       369: {
1.26      jfb       370:        int i;
1.68      joris     371:        int pwd;
1.26      jfb       372:        char *sp, *np, pcopy[MAXPATHLEN];
1.68      joris     373:        CVSFILE *base, *nf;
                    374:        CVSENTRIES *entfile;
                    375:        struct cvs_ent *ent;
1.26      jfb       376:
1.68      joris     377:        entfile = cvs_ent_open(".", O_RDONLY);
                    378:        base = cvs_file_lget(".", 0, NULL, NULL);
1.26      jfb       379:        if (base == NULL)
                    380:                return (NULL);
                    381:
1.73      joris     382:        /* XXX - needed for some commands */
                    383:        if (cb != NULL) {
                    384:                if (cb(base, arg) != CVS_EX_OK) {
                    385:                        cvs_file_free(base);
                    386:                        return (NULL);
                    387:                }
                    388:        }
                    389:
1.26      jfb       390:        for (i = 0; i < fsn; i++) {
                    391:                strlcpy(pcopy, fspec[i], sizeof(pcopy));
                    392:                sp = pcopy;
1.68      joris     393:                pwd = (!strcmp(pcopy, "."));
1.26      jfb       394:
1.68      joris     395:                np = strchr(sp, '/');
                    396:                if (np != NULL)
                    397:                        *np = '\0';
                    398:
                    399:                if (pwd) {
                    400:                        nf = base;
                    401:                } else {
                    402:                        nf = cvs_file_find(base, pcopy);
1.26      jfb       403:                        if (nf == NULL) {
1.68      joris     404:                                if (entfile != NULL)
                    405:                                        ent = cvs_ent_get(entfile, pcopy);
                    406:                                else
                    407:                                        ent = NULL;
                    408:                                nf = cvs_file_lget(pcopy, 0, base, ent);
1.26      jfb       409:                                if (nf == NULL) {
                    410:                                        cvs_file_free(base);
                    411:                                        return (NULL);
                    412:                                }
                    413:
1.68      joris     414:                                if (cvs_file_attach(base, nf) < 0) {
1.59      joris     415:                                        cvs_file_free(base);
                    416:                                        return (NULL);
                    417:                                }
1.26      jfb       418:                        }
1.68      joris     419:                }
1.26      jfb       420:
1.68      joris     421:                if (nf->cf_type == DT_DIR) {
                    422:                        if (np != NULL)
                    423:                                *np++;
                    424:
1.73      joris     425:                        if (cvs_file_getdir(nf, flags, np, cb, arg) < 0) {
1.68      joris     426:                                cvs_file_free(base);
                    427:                                return (NULL);
1.26      jfb       428:                        }
1.73      joris     429:                } else {
                    430:                        if (cb != NULL) {
                    431:                                if (cb(nf, arg) != CVS_EX_OK)
                    432:                                        return (NULL);
                    433:                        }
1.68      joris     434:                }
1.26      jfb       435:        }
                    436:
                    437:        return (base);
1.3       jfb       438: }
                    439:
                    440:
                    441: /*
1.13      jfb       442:  * cvs_file_find()
                    443:  *
                    444:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    445:  * The file's pathname <path> must be relative to the base of <hier>.
                    446:  * Returns the entry on success, or NULL on failure.
                    447:  */
                    448: CVSFILE*
                    449: cvs_file_find(CVSFILE *hier, const char *path)
                    450: {
                    451:        char *pp, *sp, pbuf[MAXPATHLEN];
                    452:        CVSFILE *sf, *cf;
                    453:
                    454:        strlcpy(pbuf, path, sizeof(pbuf));
                    455:
                    456:        cf = hier;
                    457:        pp = pbuf;
                    458:        do {
                    459:                sp = strchr(pp, '/');
                    460:                if (sp != NULL)
1.24      jfb       461:                        *(sp++) = '\0';
1.13      jfb       462:
                    463:                /* special case */
                    464:                if (*pp == '.') {
                    465:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    466:                                /* request to go back to parent */
                    467:                                if (cf->cf_parent == NULL) {
                    468:                                        cvs_log(LP_NOTICE,
                    469:                                            "path %s goes back too far", path);
                    470:                                        return (NULL);
                    471:                                }
                    472:                                cf = cf->cf_parent;
                    473:                                continue;
1.38      deraadt   474:                        } else if (*(pp + 1) == '\0')
1.13      jfb       475:                                continue;
                    476:                }
                    477:
1.62      jfb       478:                SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.34      jfb       479:                        if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13      jfb       480:                                break;
                    481:                if (sf == NULL)
                    482:                        return (NULL);
                    483:
                    484:                cf = sf;
                    485:                pp = sp;
                    486:        } while (sp != NULL);
                    487:
1.24      jfb       488:        return (cf);
1.13      jfb       489: }
                    490:
                    491:
                    492: /*
1.34      jfb       493:  * cvs_file_getpath()
                    494:  *
                    495:  * Get the full path of the file <file> and store it in <buf>, which is of
                    496:  * size <len>.  For portability, it is recommended that <buf> always be
                    497:  * at least MAXPATHLEN bytes long.
                    498:  * Returns a pointer to the start of the path on success, or NULL on failure.
                    499:  */
                    500: char*
                    501: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
                    502: {
                    503:        u_int i;
                    504:        char *fp, *namevec[CVS_FILE_MAXDEPTH];
                    505:        CVSFILE *top;
                    506:
                    507:        buf[0] = '\0';
                    508:        i = CVS_FILE_MAXDEPTH;
                    509:        memset(namevec, 0, sizeof(namevec));
                    510:
                    511:        /* find the top node */
                    512:        for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
                    513:                fp = CVS_FILE_NAME(top);
                    514:
                    515:                /* skip self-references */
                    516:                if ((fp[0] == '.') && (fp[1] == '\0'))
                    517:                        continue;
                    518:                namevec[--i] = fp;
                    519:        }
                    520:
                    521:        if (i == 0)
                    522:                return (NULL);
                    523:        else if (i == CVS_FILE_MAXDEPTH) {
                    524:                strlcpy(buf, ".", len);
                    525:                return (buf);
                    526:        }
                    527:
                    528:        while (i < CVS_FILE_MAXDEPTH - 1) {
                    529:                strlcat(buf, namevec[i++], len);
                    530:                strlcat(buf, "/", len);
                    531:        }
                    532:        strlcat(buf, namevec[i], len);
                    533:
                    534:        return (buf);
                    535: }
                    536:
                    537:
                    538: /*
1.22      jfb       539:  * cvs_file_attach()
                    540:  *
                    541:  * Attach the file <file> as one of the children of parent <parent>, which
                    542:  * has to be a file of type DT_DIR.
                    543:  * Returns 0 on success, or -1 on failure.
                    544:  */
                    545: int
                    546: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
                    547: {
                    548:        if (parent->cf_type != DT_DIR)
                    549:                return (-1);
                    550:
1.62      jfb       551:        SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22      jfb       552:        file->cf_parent = parent;
                    553:
                    554:        return (0);
                    555: }
                    556:
                    557:
                    558: /*
1.68      joris     559:  * Load directory information
1.3       jfb       560:  */
1.6       jfb       561: static int
1.69      joris     562: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3       jfb       563: {
1.68      joris     564:        char fpath[MAXPATHLEN];
                    565:        char pbuf[MAXPATHLEN];
1.20      jfb       566:        struct stat st;
1.68      joris     567:        int l;
1.7       jfb       568:
1.34      jfb       569:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.62      jfb       570:        cf->cf_root = cvsroot_get(fpath);
                    571:        if (cf->cf_root == NULL)
1.48      jfb       572:                return (-1);
                    573:
1.74    ! joris     574:        if (flags & CF_MKADMIN)
        !           575:                cvs_mkadmin(cf, 0755);
1.69      joris     576:
1.74    ! joris     577:        /* if the CVS administrative directory exists, load the info */
        !           578:        l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
        !           579:        if (l == -1 || l >= (int)sizeof(pbuf)) {
        !           580:                errno = ENAMETOOLONG;
        !           581:                cvs_log(LP_ERRNO, "%s", pbuf);
        !           582:                return (-1);
        !           583:        }
1.61      xsa       584:
1.74    ! joris     585:        if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
        !           586:                if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
        !           587:                        cf->cf_repo = strdup(pbuf);
        !           588:                        if (cf->cf_repo == NULL) {
        !           589:                                cvs_log(LP_ERRNO,
        !           590:                                    "failed to dup repository string");
        !           591:                                return (-1);
1.20      jfb       592:                        }
1.68      joris     593:                }
                    594:        }
1.26      jfb       595:
1.68      joris     596:        return (0);
                    597: }
                    598:
                    599: /*
                    600:  * cvs_file_getdir()
                    601:  *
                    602:  * Get a cvs directory structure for the directory whose path is <dir>.
                    603:  * This function should not free the directory information on error, as this
                    604:  * is performed by cvs_file_free().
                    605:  */
                    606: static int
1.73      joris     607: cvs_file_getdir(CVSFILE *cf, int flags, char *path, int (*cb)(CVSFILE *, void *), void *arg)
1.68      joris     608: {
                    609:        int l;
                    610:        int check_entry;
                    611:        u_int ndirs, nfiles;
                    612:        char *cur, *np;
                    613:        char pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
                    614:        struct dirent *ent;
                    615:        CVSFILE *cfp, *moo;
                    616:        struct cvs_ent *cvsent;
                    617:        struct cvs_flist dirs;
                    618:        DIR *dirp;
                    619:        CVSENTRIES *entfile;
                    620:
                    621:        moo = NULL;
                    622:        check_entry = ndirs = nfiles = 0;
                    623:        SIMPLEQ_INIT(&dirs);
                    624:
                    625:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    626:        entfile = cvs_ent_open(fpath, O_RDONLY);
                    627:
                    628:        cf->cf_root = cvsroot_get(fpath);
                    629:        if (cf->cf_root == NULL)
                    630:                return (-1);
                    631:
                    632:        cur = np = NULL;
                    633:        if (path != NULL) {
                    634:                cur = strchr(path, '/');
                    635:                if (cur != NULL) {
                    636:                        *cur = '\0';
                    637:                        np = cur + 1;
                    638:                        if (np != NULL && *np == '\0')
                    639:                                np = NULL;
1.20      jfb       640:                }
                    641:        }
1.14      jfb       642:
1.56      jfb       643:        if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26      jfb       644:                return (0);
                    645:
1.73      joris     646:        /* callback for the directory entry */
                    647:        if (cb != NULL) {
                    648:                if (cb(cf, arg) != CVS_EX_OK)
                    649:                        return (-1);
                    650:        }
                    651:
1.68      joris     652:        dirp = opendir(fpath);
                    653:        if (dirp == NULL) {
                    654:                cvs_log(LP_ERRNO, "failed to open directory %s", fpath);
1.6       jfb       655:                return (-1);
1.3       jfb       656:        }
                    657:
1.68      joris     658:        while ((ent = readdir(dirp)) != NULL) {
                    659:                if (!strcmp(ent->d_name, ".") ||
                    660:                    !strcmp(ent->d_name, ".."))
                    661:                        continue;
1.10      jfb       662:
1.68      joris     663:                if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
                    664:                        continue;
1.11      jfb       665:
1.68      joris     666:                if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
                    667:                        continue;
1.24      jfb       668:
1.68      joris     669:                if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
                    670:                        if (entfile != NULL)
                    671:                                (void)cvs_ent_remove(entfile,
                    672:                                    ent->d_name);
                    673:                        continue;
                    674:                }
1.70      joris     675:
                    676:                if ((ent->d_type != DT_DIR) && (flags & CF_NOFILES))
                    677:                        continue;
1.56      jfb       678:
1.68      joris     679:                if (path != NULL) {
                    680:                        if (strcmp(path, ent->d_name))
1.56      jfb       681:                                continue;
1.68      joris     682:                }
1.11      jfb       683:
1.68      joris     684:                l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
                    685:                    ent->d_name);
                    686:                if (l == -1 || l >= (int)sizeof(pbuf)) {
                    687:                        errno = ENAMETOOLONG;
                    688:                        cvs_log(LP_ERRNO, "%s", pbuf);
                    689:                        closedir(dirp);
                    690:                        return (-1);
                    691:                }
1.61      xsa       692:
1.68      joris     693:                cfp = cvs_file_find(cf, ent->d_name);
                    694:                if (cfp == NULL) {
1.62      jfb       695:                        if (entfile != NULL)
                    696:                                cvsent = cvs_ent_get(entfile, ent->d_name);
1.71      joris     697:                        else
                    698:                                cvsent = NULL;
                    699:
1.62      jfb       700:                        cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
1.68      joris     701:
1.55      jfb       702:                        if (cfp == NULL) {
1.68      joris     703:                                closedir(dirp);
1.55      jfb       704:                                return (-1);
                    705:                        }
1.62      jfb       706:                        if (entfile != NULL)
                    707:                                cvs_ent_remove(entfile, cfp->cf_name);
1.55      jfb       708:
1.68      joris     709:                        if (cfp->cf_type != DT_DIR) {
1.62      jfb       710:                                SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp,
1.68      joris     711:                                    cf_list);
1.62      jfb       712:                                nfiles++;
1.11      jfb       713:                        }
1.68      joris     714:                } else {
                    715:                        cfp->cf_flags |= CVS_GDIR_IGNORE;
1.4       jfb       716:                }
1.14      jfb       717:
1.68      joris     718:                if (cfp->cf_type == DT_DIR) {
                    719:                        ndirs++;
                    720:                        SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.73      joris     721:                } else {
                    722:                        /* callback for the file */
                    723:                        if (cb != NULL) {
                    724:                                if (cb(cfp, arg) != CVS_EX_OK) {
                    725:                                        closedir(dirp);
                    726:                                        return (-1);
                    727:                                }
                    728:                        }
1.68      joris     729:                }
                    730:
                    731:                if (path != NULL) {
                    732:                        check_entry = 0;
                    733:                        break;
                    734:                }
                    735:        }
                    736:
                    737:        closedir(dirp);
                    738:
                    739:        if (entfile != NULL && check_entry) {
1.62      jfb       740:                while ((cvsent = cvs_ent_next(entfile)) != NULL) {
1.68      joris     741:                        if (path != NULL) {
                    742:                                if (strcmp(cvsent->ce_name, path))
                    743:                                        continue;
                    744:                        }
                    745:
1.61      xsa       746:                        l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44      jfb       747:                            cvsent->ce_name);
1.61      xsa       748:                        if (l == -1 || l >= (int)sizeof(pbuf)) {
                    749:                                errno = ENAMETOOLONG;
                    750:                                cvs_log(LP_ERRNO, "%s", pbuf);
                    751:                                return (-1);
                    752:                        }
                    753:
1.68      joris     754:                        cfp = cvs_file_find(cf, cvsent->ce_name);
                    755:                        if (cfp == NULL) {
                    756:                                cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
                    757:                                if (cfp == NULL)
                    758:                                        continue;
                    759:
                    760:                                if (cfp->cf_type != DT_DIR) {
                    761:                                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files),
                    762:                                            cfp, cf_list);
1.62      jfb       763:                                        nfiles++;
1.44      jfb       764:                                }
1.68      joris     765:                        } else {
                    766:                                cfp->cf_flags |= CVS_GDIR_IGNORE;
                    767:                        }
                    768:
                    769:                        if (cfp->cf_type == DT_DIR) {
                    770:                                ndirs++;
                    771:                                SIMPLEQ_INSERT_TAIL(&dirs, cfp,
                    772:                                    cf_list);
1.73      joris     773:                        } else {
                    774:                                /* callback for the file */
                    775:                                if (cb != NULL) {
                    776:                                        if (cb(cfp, arg) != CVS_EX_OK)
                    777:                                                return (-1);
                    778:                                }
1.44      jfb       779:                        }
1.68      joris     780:
                    781:                        if (path != NULL)
                    782:                                break;
1.44      jfb       783:                }
1.62      jfb       784:                cvs_ent_close(entfile);
1.40      jfb       785:        }
1.34      jfb       786:
1.10      jfb       787:        if (flags & CF_SORT) {
1.68      joris     788:                if (nfiles > 0)
                    789:                        cvs_file_sort(&(cf->cf_files), nfiles);
                    790:                if (ndirs > 0)
                    791:                        cvs_file_sort(&dirs, ndirs);
1.10      jfb       792:        }
1.26      jfb       793:
1.62      jfb       794:        while (!SIMPLEQ_EMPTY(&dirs)) {
                    795:                cfp = SIMPLEQ_FIRST(&dirs);
                    796:                SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68      joris     797:
                    798:                if (!(cfp->cf_flags & CVS_GDIR_IGNORE))
                    799:                        SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
                    800:                else
                    801:                        cfp->cf_flags &= ~CVS_GDIR_IGNORE;
                    802:
1.73      joris     803:                if (cvs_file_getdir(cfp, flags, np, cb, arg) < 0) {
1.68      joris     804:                        cvs_log(LP_ERROR, "failed to get %s",
                    805:                            CVS_FILE_NAME(cfp));
                    806:                        continue;
                    807:                }
1.26      jfb       808:        }
1.3       jfb       809:
1.6       jfb       810:        return (0);
1.3       jfb       811: }
                    812:
                    813:
                    814: /*
                    815:  * cvs_file_free()
                    816:  *
                    817:  * Free a cvs_file structure and its contents.
                    818:  */
                    819: void
1.14      jfb       820: cvs_file_free(CVSFILE *cf)
1.3       jfb       821: {
1.62      jfb       822:        CVSFILE *child;
                    823:
1.47      jfb       824:        if (cf->cf_name != NULL)
1.57      jfb       825:                cvs_strfree(cf->cf_name);
1.62      jfb       826:
                    827:        if (cf->cf_type == DT_DIR) {
                    828:                if (cf->cf_root != NULL)
                    829:                        cvsroot_free(cf->cf_root);
                    830:                if (cf->cf_repo != NULL)
                    831:                        free(cf->cf_repo);
                    832:                while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
                    833:                        child = SIMPLEQ_FIRST(&(cf->cf_files));
                    834:                        SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
                    835:                        cvs_file_free(child);
                    836:                }
1.64      joris     837:        } else {
                    838:                if (cf->cf_tag != NULL)
                    839:                        cvs_strfree(cf->cf_tag);
1.62      jfb       840:        }
1.64      joris     841:
1.3       jfb       842:        free(cf);
1.5       jfb       843: }
                    844:
                    845:
                    846: /*
                    847:  * cvs_file_examine()
                    848:  *
                    849:  * Examine the contents of the CVS file structure <cf> with the function
                    850:  * <exam>.  The function is called for all subdirectories and files of the
                    851:  * root file.
                    852:  */
                    853: int
                    854: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    855: {
                    856:        int ret;
1.14      jfb       857:        CVSFILE *fp;
1.5       jfb       858:
                    859:        if (cf->cf_type == DT_DIR) {
                    860:                ret = (*exam)(cf, arg);
1.62      jfb       861:                SIMPLEQ_FOREACH(fp, &(cf->cf_files), cf_list) {
1.5       jfb       862:                        ret = cvs_file_examine(fp, exam, arg);
1.60      joris     863:                        if (ret != 0)
1.13      jfb       864:                                break;
1.5       jfb       865:                }
1.38      deraadt   866:        } else
1.13      jfb       867:                ret = (*exam)(cf, arg);
                    868:
                    869:        return (ret);
1.3       jfb       870: }
                    871:
                    872: /*
                    873:  * cvs_file_sort()
                    874:  *
1.16      jfb       875:  * Sort a list of cvs file structures according to their filename.  The list
                    876:  * <flp> is modified according to the sorting algorithm.  The number of files
                    877:  * in the list must be given by <nfiles>.
                    878:  * Returns 0 on success, or -1 on failure.
1.3       jfb       879:  */
                    880: static int
1.16      jfb       881: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb       882: {
                    883:        int i;
                    884:        size_t nb;
1.16      jfb       885:        CVSFILE *cf, **cfvec;
                    886:
                    887:        cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
                    888:        if (cfvec == NULL) {
                    889:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                    890:                return (-1);
                    891:        }
1.3       jfb       892:
                    893:        i = 0;
1.62      jfb       894:        SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16      jfb       895:                if (i == (int)nfiles) {
1.3       jfb       896:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb       897:                        /* rebuild the list and abort sorting */
                    898:                        while (--i >= 0)
1.62      jfb       899:                                SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16      jfb       900:                        free(cfvec);
1.3       jfb       901:                        return (-1);
                    902:                }
1.16      jfb       903:                cfvec[i++] = cf;
1.3       jfb       904:
                    905:                /* now unlink it from the list,
                    906:                 * we'll put it back in order later
                    907:                 */
1.62      jfb       908:                SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3       jfb       909:        }
                    910:
                    911:        /* clear the list just in case */
1.62      jfb       912:        SIMPLEQ_INIT(flp);
1.3       jfb       913:        nb = (size_t)i;
                    914:
                    915:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    916:
                    917:        /* rebuild the list from the bottom up */
                    918:        for (i = (int)nb - 1; i >= 0; i--)
1.62      jfb       919:                SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       920:
1.16      jfb       921:        free(cfvec);
1.3       jfb       922:        return (0);
                    923: }
                    924:
                    925:
                    926: static int
                    927: cvs_file_cmp(const void *f1, const void *f2)
                    928: {
1.41      jfb       929:        const CVSFILE *cf1, *cf2;
1.62      jfb       930:        cf1 = *(CVSFILE * const *)f1;
                    931:        cf2 = *(CVSFILE * const *)f2;
1.34      jfb       932:        return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6       jfb       933: }
                    934:
                    935:
1.34      jfb       936: /*
                    937:  * cvs_file_alloc()
                    938:  *
                    939:  * Allocate a CVSFILE structure and initialize its internals.
                    940:  */
1.6       jfb       941: CVSFILE*
                    942: cvs_file_alloc(const char *path, u_int type)
                    943: {
                    944:        CVSFILE *cfp;
                    945:
1.14      jfb       946:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb       947:        if (cfp == NULL) {
                    948:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    949:                return (NULL);
                    950:        }
                    951:        memset(cfp, 0, sizeof(*cfp));
                    952:
1.62      jfb       953:        cfp->cf_type = type;
                    954:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    955:
                    956:        if (type == DT_DIR) {
                    957:                SIMPLEQ_INIT(&(cfp->cf_files));
                    958:        }
                    959:
1.57      jfb       960:        cfp->cf_name = cvs_strdup(basename(path));
1.34      jfb       961:        if (cfp->cf_name == NULL) {
1.57      jfb       962:                cvs_log(LP_ERR, "failed to copy file name");
1.58      tedu      963:                cvs_file_free(cfp);
1.6       jfb       964:                return (NULL);
                    965:        }
                    966:
                    967:        return (cfp);
1.1       jfb       968: }
1.14      jfb       969:
                    970:
                    971: /*
                    972:  * cvs_file_lget()
                    973:  *
                    974:  * Get the file and link it with the parent right away.
1.22      jfb       975:  * Returns a pointer to the created file structure on success, or NULL on
                    976:  * failure.
1.14      jfb       977:  */
                    978: static CVSFILE*
1.62      jfb       979: cvs_file_lget(const char *path, int flags, CVSFILE *parent, struct cvs_ent *ent)
1.14      jfb       980: {
1.44      jfb       981:        int ret, cwd;
                    982:        u_int type;
1.14      jfb       983:        struct stat st;
                    984:        CVSFILE *cfp;
                    985:
1.44      jfb       986:        type = DT_UNKNOWN;
                    987:        cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.62      jfb       988:
1.44      jfb       989:        ret = stat(path, &st);
                    990:        if (ret == 0)
                    991:                type = IFTODT(st.st_mode);
1.14      jfb       992:
1.44      jfb       993:        if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14      jfb       994:                return (NULL);
                    995:        cfp->cf_parent = parent;
1.54      joris     996:
                    997:        if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62      jfb       998:                cfp->cf_flags |= CVS_DIRF_BASE;
1.14      jfb       999:
1.44      jfb      1000:        if (ret == 0) {
                   1001:                cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62      jfb      1002:                if (cfp->cf_type == DT_REG)
                   1003:                        cfp->cf_mtime = st.st_mtime;
1.44      jfb      1004:
                   1005:                if (ent == NULL)
                   1006:                        cfp->cf_cvstat = (cwd == 1) ?
                   1007:                            CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14      jfb      1008:                else {
1.44      jfb      1009:                        /* always show directories as up-to-date */
                   1010:                        if (ent->ce_type == CVS_ENT_DIR)
1.14      jfb      1011:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44      jfb      1012:                        else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                   1013:                                cfp->cf_cvstat = CVS_FST_ADDED;
                   1014:                        else {
                   1015:                                /* check last modified time */
                   1016:                                if (ent->ce_mtime >= (time_t)st.st_mtime)
                   1017:                                        cfp->cf_cvstat = CVS_FST_UPTODATE;
                   1018:                                else
                   1019:                                        cfp->cf_cvstat = CVS_FST_MODIFIED;
                   1020:                        }
1.14      jfb      1021:                }
1.44      jfb      1022:        } else {
                   1023:                if (ent == NULL) {
                   1024:                        cvs_log(LP_ERR, "no Entry and no file for `%s'",
                   1025:                            CVS_FILE_NAME(cfp));
                   1026:                        cvs_file_free(cfp);
                   1027:                        return (NULL);
1.66      joris    1028:                } else {
                   1029:                        if (ent->ce_type == CVS_ENT_FILE)
                   1030:                                cfp->cf_type = DT_REG;
                   1031:                        else if (ent->ce_type == CVS_ENT_DIR)
                   1032:                                cfp->cf_type = DT_DIR;
                   1033:                        else
                   1034:                                cvs_log(LP_WARN, "unknown ce_type %d",
                   1035:                                    ent->ce_type);
1.67      joris    1036:
                   1037:                        if (ent->ce_status == CVS_ENT_REMOVED)
                   1038:                                cfp->cf_cvstat = CVS_FST_REMOVED;
                   1039:                        else
                   1040:                                cfp->cf_cvstat = CVS_FST_LOST;
1.66      joris    1041:                }
1.14      jfb      1042:        }
1.52      jfb      1043:
1.62      jfb      1044:        if (ent != NULL) {
                   1045:                /* steal the RCSNUM */
                   1046:                cfp->cf_lrev = ent->ce_rev;
1.72      jfb      1047:                ent->ce_rev = NULL;
                   1048:
                   1049:                if ((ent->ce_type == CVS_ENT_REG) &&
                   1050:                    (ent->ce_tag != NULL)) {
1.65      joris    1051:                        if ((cfp->cf_tag = cvs_strdup(ent->ce_tag)) == NULL) {
                   1052:                                cvs_file_free(cfp);
                   1053:                                return (NULL);
                   1054:                        }
1.62      jfb      1055:                }
                   1056:        }
1.14      jfb      1057:
1.69      joris    1058:        if ((cfp->cf_type == DT_DIR) && (cvs_load_dirinfo(cfp, flags) < 0)) {
1.26      jfb      1059:                cvs_file_free(cfp);
                   1060:                return (NULL);
1.14      jfb      1061:        }
1.74    ! joris    1062:
        !          1063:        if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
        !          1064:            !strcmp(cfp->cf_repo, path))
        !          1065:                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14      jfb      1066:
                   1067:        return (cfp);
1.23      jfb      1068: }
                   1069:
                   1070:
                   1071: static int
                   1072: cvs_file_cmpname(const char *name1, const char *name2)
                   1073: {
                   1074:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                   1075:            (strcasecmp(name1, name2));
1.14      jfb      1076: }