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

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