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

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