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

1.61    ! xsa         1: /*     $OpenBSD: file.c,v 1.60 2005/04/13 20:11:21 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.57      jfb       110: static int       cvs_file_getdir   (CVSFILE *, int);
                    111: static void      cvs_file_freedir  (struct cvs_dir *);
                    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 *);
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.1       jfb       254:
1.6       jfb       255:        cfp = cvs_file_alloc(path, type);
                    256:        if (cfp == NULL)
                    257:                return (NULL);
1.26      jfb       258:
1.22      jfb       259:        cfp->cf_mode = mode;
1.34      jfb       260:        cfp->cf_parent = parent;
1.1       jfb       261:
1.34      jfb       262:        if (type == DT_DIR) {
                    263:                cfp->cf_ddat->cd_root = cvsroot_get(path);
                    264:                cfp->cf_ddat->cd_repo = strdup(cvs_file_getpath(cfp,
                    265:                    fp, sizeof(fp)));
                    266:                if (cfp->cf_ddat->cd_repo == NULL) {
                    267:                        cvs_file_free(cfp);
                    268:                        return (NULL);
                    269:                }
1.33      joris     270:
1.26      jfb       271:                if ((mkdir(path, mode) == -1) || (cvs_mkadmin(cfp, mode) < 0)) {
1.6       jfb       272:                        cvs_file_free(cfp);
                    273:                        return (NULL);
                    274:                }
1.26      jfb       275:
1.27      jfb       276:                cfp->cf_ddat->cd_ent = cvs_ent_open(path, O_RDWR);
1.50      jfb       277:                if (cfp->cf_ddat->cd_ent != NULL) {
                    278:                        cvs_ent_close(cfp->cf_ddat->cd_ent);
                    279:                        cfp->cf_ddat->cd_ent = NULL;
                    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_mtime = orig->cf_mtime;
                    320:        cfp->cf_cvstat = orig->cf_cvstat;
                    321:
                    322:        if (orig->cf_type == DT_DIR) {
                    323:                /* XXX copy CVS directory attributes */
                    324:        }
                    325:
                    326:        return (cfp);
                    327: }
                    328:
                    329:
                    330: /*
1.3       jfb       331:  * cvs_file_get()
                    332:  *
                    333:  * Load a cvs_file structure with all the information pertaining to the file
                    334:  * <path>.
1.4       jfb       335:  * The <flags> parameter specifies various flags that alter the behaviour of
1.21      jfb       336:  * the function.  The CF_RECURSE flag causes the function to recursively load
                    337:  * subdirectories when <path> is a directory.
                    338:  * The CF_SORT flag causes the files to be sorted in alphabetical order upon
                    339:  * loading.  The special case of "." as a path specification generates
                    340:  * recursion for a single level and is equivalent to calling cvs_file_get() on
                    341:  * all files of that directory.
1.3       jfb       342:  * Returns a pointer to the cvs file structure, which must later be freed
                    343:  * with cvs_file_free().
                    344:  */
                    345:
1.14      jfb       346: CVSFILE*
1.3       jfb       347: cvs_file_get(const char *path, int flags)
                    348: {
1.14      jfb       349:        return cvs_file_lget(path, flags, NULL);
1.9       jfb       350: }
                    351:
                    352:
                    353: /*
                    354:  * cvs_file_getspec()
                    355:  *
                    356:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    357:  * whose size is given in <fsn>.
                    358:  * Returns a pointer to the lowest common subdirectory to all specified
                    359:  * files.
                    360:  */
                    361: CVSFILE*
                    362: cvs_file_getspec(char **fspec, int fsn, int flags)
                    363: {
1.26      jfb       364:        int i;
                    365:        char *sp, *np, pcopy[MAXPATHLEN];
                    366:        CVSFILE *base, *cf, *nf;
                    367:
                    368:        base = cvs_file_get(".", 0);
                    369:        if (base == NULL)
                    370:                return (NULL);
                    371:
                    372:        for (i = 0; i < fsn; i++) {
                    373:                strlcpy(pcopy, fspec[i], sizeof(pcopy));
                    374:                cf = base;
                    375:                sp = pcopy;
                    376:
                    377:                do {
                    378:                        np = strchr(sp, '/');
                    379:                        if (np != NULL)
                    380:                                *np = '\0';
                    381:                        nf = cvs_file_find(cf, sp);
                    382:                        if (nf == NULL) {
                    383:                                nf = cvs_file_lget(pcopy, 0, cf);
                    384:                                if (nf == NULL) {
                    385:                                        cvs_file_free(base);
                    386:                                        return (NULL);
                    387:                                }
                    388:
1.59      joris     389:                                if (cvs_file_attach(cf, nf) < 0) {
                    390:                                        cvs_file_free(base);
                    391:                                        return (NULL);
                    392:                                }
1.26      jfb       393:                        }
                    394:
                    395:                        if (np != NULL) {
                    396:                                *np = '/';
                    397:                                sp = np + 1;
                    398:                        }
                    399:
                    400:                        cf = nf;
                    401:                } while (np != NULL);
                    402:        }
                    403:
                    404:        return (base);
1.3       jfb       405: }
                    406:
                    407:
                    408: /*
1.13      jfb       409:  * cvs_file_find()
                    410:  *
                    411:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    412:  * The file's pathname <path> must be relative to the base of <hier>.
                    413:  * Returns the entry on success, or NULL on failure.
                    414:  */
                    415: CVSFILE*
                    416: cvs_file_find(CVSFILE *hier, const char *path)
                    417: {
                    418:        char *pp, *sp, pbuf[MAXPATHLEN];
                    419:        CVSFILE *sf, *cf;
                    420:
                    421:        strlcpy(pbuf, path, sizeof(pbuf));
                    422:
                    423:        cf = hier;
                    424:        pp = pbuf;
                    425:        do {
                    426:                sp = strchr(pp, '/');
                    427:                if (sp != NULL)
1.24      jfb       428:                        *(sp++) = '\0';
1.13      jfb       429:
                    430:                /* special case */
                    431:                if (*pp == '.') {
                    432:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    433:                                /* request to go back to parent */
                    434:                                if (cf->cf_parent == NULL) {
                    435:                                        cvs_log(LP_NOTICE,
                    436:                                            "path %s goes back too far", path);
                    437:                                        return (NULL);
                    438:                                }
                    439:                                cf = cf->cf_parent;
                    440:                                continue;
1.38      deraadt   441:                        } else if (*(pp + 1) == '\0')
1.13      jfb       442:                                continue;
                    443:                }
                    444:
                    445:                TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
1.34      jfb       446:                        if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13      jfb       447:                                break;
                    448:                if (sf == NULL)
                    449:                        return (NULL);
                    450:
                    451:                cf = sf;
                    452:                pp = sp;
                    453:        } while (sp != NULL);
                    454:
1.24      jfb       455:        return (cf);
1.13      jfb       456: }
                    457:
                    458:
                    459: /*
1.34      jfb       460:  * cvs_file_getpath()
                    461:  *
                    462:  * Get the full path of the file <file> and store it in <buf>, which is of
                    463:  * size <len>.  For portability, it is recommended that <buf> always be
                    464:  * at least MAXPATHLEN bytes long.
                    465:  * Returns a pointer to the start of the path on success, or NULL on failure.
                    466:  */
                    467: char*
                    468: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
                    469: {
                    470:        u_int i;
                    471:        char *fp, *namevec[CVS_FILE_MAXDEPTH];
                    472:        CVSFILE *top;
                    473:
                    474:        buf[0] = '\0';
                    475:        i = CVS_FILE_MAXDEPTH;
                    476:        memset(namevec, 0, sizeof(namevec));
                    477:
                    478:        /* find the top node */
                    479:        for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
                    480:                fp = CVS_FILE_NAME(top);
                    481:
                    482:                /* skip self-references */
                    483:                if ((fp[0] == '.') && (fp[1] == '\0'))
                    484:                        continue;
                    485:                namevec[--i] = fp;
                    486:        }
                    487:
                    488:        if (i == 0)
                    489:                return (NULL);
                    490:        else if (i == CVS_FILE_MAXDEPTH) {
                    491:                strlcpy(buf, ".", len);
                    492:                return (buf);
                    493:        }
                    494:
                    495:        while (i < CVS_FILE_MAXDEPTH - 1) {
                    496:                strlcat(buf, namevec[i++], len);
                    497:                strlcat(buf, "/", len);
                    498:        }
                    499:        strlcat(buf, namevec[i], len);
                    500:
                    501:        return (buf);
                    502: }
                    503:
                    504:
                    505: /*
1.22      jfb       506:  * cvs_file_attach()
                    507:  *
                    508:  * Attach the file <file> as one of the children of parent <parent>, which
                    509:  * has to be a file of type DT_DIR.
                    510:  * Returns 0 on success, or -1 on failure.
                    511:  */
                    512: int
                    513: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
                    514: {
1.23      jfb       515:        struct cvs_dir *dp;
1.22      jfb       516:
                    517:        if (parent->cf_type != DT_DIR)
                    518:                return (-1);
                    519:
1.23      jfb       520:        dp = parent->cf_ddat;
                    521:
                    522:        TAILQ_INSERT_TAIL(&(dp->cd_files), file, cf_list);
                    523:        dp->cd_nfiles++;
1.22      jfb       524:        file->cf_parent = parent;
                    525:
                    526:        return (0);
                    527: }
                    528:
                    529:
                    530: /*
1.3       jfb       531:  * cvs_file_getdir()
                    532:  *
                    533:  * Get a cvs directory structure for the directory whose path is <dir>.
1.30      jfb       534:  * This function should not free the directory information on error, as this
                    535:  * is performed by cvs_file_free().
1.3       jfb       536:  */
1.6       jfb       537: static int
1.14      jfb       538: cvs_file_getdir(CVSFILE *cf, int flags)
1.3       jfb       539: {
1.61    ! xsa       540:        int ret, fd, l;
1.16      jfb       541:        u_int ndirs;
1.3       jfb       542:        long base;
1.43      jfb       543:        u_char *dp, *ep;
1.34      jfb       544:        char fbuf[2048], pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
1.3       jfb       545:        struct dirent *ent;
1.14      jfb       546:        CVSFILE *cfp;
1.20      jfb       547:        struct stat st;
1.3       jfb       548:        struct cvs_dir *cdp;
1.44      jfb       549:        struct cvs_ent *cvsent;
1.10      jfb       550:        struct cvs_flist dirs;
1.3       jfb       551:
1.18      jfb       552:        ndirs = 0;
1.10      jfb       553:        TAILQ_INIT(&dirs);
1.7       jfb       554:        cdp = cf->cf_ddat;
                    555:
1.34      jfb       556:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    557:
1.48      jfb       558:        cdp->cd_root = cvsroot_get(fpath);
                    559:        if (cdp->cd_root == NULL)
                    560:                return (-1);
                    561:
1.26      jfb       562:        if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
                    563:                if (flags & CF_MKADMIN)
                    564:                        cvs_mkadmin(cf, 0755);
1.14      jfb       565:
1.26      jfb       566:                /* if the CVS administrative directory exists, load the info */
1.61    ! xsa       567:                l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
        !           568:                if (l == -1 || l >= (int)sizeof(pbuf)) {
        !           569:                        errno = ENAMETOOLONG;
        !           570:                        cvs_log(LP_ERRNO, "%s", pbuf);
        !           571:                        return (-1);
        !           572:                }
        !           573:
1.26      jfb       574:                if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
1.34      jfb       575:                        if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
1.26      jfb       576:                                cdp->cd_repo = strdup(pbuf);
                    577:                                if (cdp->cd_repo == NULL) {
1.28      jfb       578:                                        cvs_log(LP_ERRNO,
                    579:                                            "failed to dup repository string");
1.26      jfb       580:                                        return (-1);
                    581:                                }
1.20      jfb       582:                        }
1.26      jfb       583:
1.37      jfb       584:                        cdp->cd_ent = cvs_ent_open(fpath, O_RDONLY);
1.20      jfb       585:                }
                    586:        }
1.14      jfb       587:
1.56      jfb       588:        if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26      jfb       589:                return (0);
                    590:
1.34      jfb       591:        fd = open(fpath, O_RDONLY);
1.3       jfb       592:        if (fd == -1) {
1.34      jfb       593:                cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
1.6       jfb       594:                return (-1);
1.3       jfb       595:        }
                    596:
1.44      jfb       597:        /* To load all files, we first get the entries for the directory and
                    598:         * load the information for each of those entries.  The handle to
                    599:         * the Entries file kept in the directory data is only temporary and
                    600:         * the files should remove their entry when they use it.  After all
                    601:         * files in the directory have been processed, the Entries handle
                    602:         * should only be left with those entries for which no real file
                    603:         * exists.  We then build file structures for those files too, as
                    604:         * we will likely receive fresh copies from the server as part of the
                    605:         * response.
                    606:         */
1.11      jfb       607:        do {
                    608:                ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    609:                if (ret == -1) {
                    610:                        cvs_log(LP_ERRNO, "failed to get directory entries");
                    611:                        (void)close(fd);
                    612:                        return (-1);
                    613:                }
1.10      jfb       614:
1.11      jfb       615:                dp = fbuf;
                    616:                ep = fbuf + (size_t)ret;
                    617:                while (dp < ep) {
                    618:                        ent = (struct dirent *)dp;
1.32      jfb       619:                        dp += ent->d_reclen;
1.31      jfb       620:                        if (ent->d_fileno == 0)
                    621:                                continue;
1.11      jfb       622:
                    623:                        if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.24      jfb       624:                                continue;
                    625:
                    626:                        if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
1.11      jfb       627:                                continue;
1.56      jfb       628:
                    629:                        if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
                    630:                                if (cdp->cd_ent != NULL)
                    631:                                        (void)cvs_ent_remove(cdp->cd_ent,
                    632:                                            ent->d_name);
                    633:                                continue;
                    634:                        }
1.11      jfb       635:
1.61    ! xsa       636:                        l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.34      jfb       637:                            ent->d_name);
1.61    ! xsa       638:                        if (l == -1 || l >= (int)sizeof(pbuf)) {
        !           639:                                errno = ENAMETOOLONG;
        !           640:                                cvs_log(LP_ERRNO, "%s", pbuf);
        !           641:
        !           642:                                (void)close(fd);
        !           643:                                return (-1);
        !           644:                        }
        !           645:
1.14      jfb       646:                        cfp = cvs_file_lget(pbuf, flags, cf);
1.55      jfb       647:                        if (cfp == NULL) {
                    648:                                (void)close(fd);
                    649:                                return (-1);
                    650:                        }
                    651:
                    652:                        if (cfp->cf_type == DT_DIR) {
                    653:                                TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
                    654:                                ndirs++;
                    655:                        } else {
                    656:                                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
                    657:                                    cf_list);
                    658:                                cdp->cd_nfiles++;
1.11      jfb       659:                        }
1.4       jfb       660:                }
1.11      jfb       661:        } while (ret > 0);
1.14      jfb       662:
1.40      jfb       663:        if (cdp->cd_ent != NULL) {
1.44      jfb       664:                /* now create file structure for files which have an
                    665:                 * entry in the Entries file but no file on disk
                    666:                 */
                    667:                while ((cvsent = cvs_ent_next(cdp->cd_ent)) != NULL) {
1.61    ! xsa       668:                        l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44      jfb       669:                            cvsent->ce_name);
1.61    ! xsa       670:                        if (l == -1 || l >= (int)sizeof(pbuf)) {
        !           671:                                errno = ENAMETOOLONG;
        !           672:                                cvs_log(LP_ERRNO, "%s", pbuf);
        !           673:
        !           674:                                (void)close(fd);
        !           675:                                return (-1);
        !           676:                        }
        !           677:
1.44      jfb       678:                        cfp = cvs_file_lget(pbuf, flags, cf);
                    679:                        if (cfp != NULL) {
                    680:                                if (cfp->cf_type == DT_DIR) {
                    681:                                        TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
                    682:                                        ndirs++;
                    683:                                } else {
                    684:                                        TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
                    685:                                            cf_list);
                    686:                                        cdp->cd_nfiles++;
                    687:                                }
                    688:                        }
                    689:                }
1.40      jfb       690:                cvs_ent_close(cdp->cd_ent);
                    691:                cdp->cd_ent = NULL;
                    692:        }
1.34      jfb       693:
1.10      jfb       694:        if (flags & CF_SORT) {
1.16      jfb       695:                cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
                    696:                cvs_file_sort(&dirs, ndirs);
1.10      jfb       697:        }
1.26      jfb       698:
                    699:        while (!TAILQ_EMPTY(&dirs)) {
                    700:                cfp = TAILQ_FIRST(&dirs);
                    701:                TAILQ_REMOVE(&dirs, cfp, cf_list);
1.10      jfb       702:                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.26      jfb       703:        }
1.16      jfb       704:        cdp->cd_nfiles += ndirs;
1.3       jfb       705:
                    706:        (void)close(fd);
                    707:
1.6       jfb       708:        return (0);
1.3       jfb       709: }
                    710:
                    711:
                    712: /*
                    713:  * cvs_file_free()
                    714:  *
                    715:  * Free a cvs_file structure and its contents.
                    716:  */
                    717: void
1.14      jfb       718: cvs_file_free(CVSFILE *cf)
1.3       jfb       719: {
                    720:        if (cf->cf_ddat != NULL)
                    721:                cvs_file_freedir(cf->cf_ddat);
1.47      jfb       722:        if (cf->cf_name != NULL)
1.57      jfb       723:                cvs_strfree(cf->cf_name);
1.3       jfb       724:        free(cf);
1.5       jfb       725: }
                    726:
                    727:
                    728: /*
                    729:  * cvs_file_examine()
                    730:  *
                    731:  * Examine the contents of the CVS file structure <cf> with the function
                    732:  * <exam>.  The function is called for all subdirectories and files of the
                    733:  * root file.
                    734:  */
                    735: int
                    736: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    737: {
                    738:        int ret;
1.14      jfb       739:        CVSFILE *fp;
1.5       jfb       740:
                    741:        if (cf->cf_type == DT_DIR) {
                    742:                ret = (*exam)(cf, arg);
1.10      jfb       743:                TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5       jfb       744:                        ret = cvs_file_examine(fp, exam, arg);
1.60      joris     745:                        if (ret != 0)
1.13      jfb       746:                                break;
1.5       jfb       747:                }
1.38      deraadt   748:        } else
1.13      jfb       749:                ret = (*exam)(cf, arg);
                    750:
                    751:        return (ret);
1.3       jfb       752: }
                    753:
                    754:
                    755: /*
                    756:  * cvs_file_freedir()
                    757:  *
                    758:  * Free a cvs_dir structure and its contents.
                    759:  */
                    760: static void
                    761: cvs_file_freedir(struct cvs_dir *cd)
                    762: {
1.14      jfb       763:        CVSFILE *cfp;
1.3       jfb       764:
                    765:        if (cd->cd_root != NULL)
                    766:                cvsroot_free(cd->cd_root);
                    767:        if (cd->cd_repo != NULL)
                    768:                free(cd->cd_repo);
                    769:
1.14      jfb       770:        if (cd->cd_ent != NULL)
                    771:                cvs_ent_close(cd->cd_ent);
                    772:
1.10      jfb       773:        while (!TAILQ_EMPTY(&(cd->cd_files))) {
                    774:                cfp = TAILQ_FIRST(&(cd->cd_files));
                    775:                TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3       jfb       776:                cvs_file_free(cfp);
                    777:        }
                    778: }
                    779:
                    780:
                    781: /*
                    782:  * cvs_file_sort()
                    783:  *
1.16      jfb       784:  * Sort a list of cvs file structures according to their filename.  The list
                    785:  * <flp> is modified according to the sorting algorithm.  The number of files
                    786:  * in the list must be given by <nfiles>.
                    787:  * Returns 0 on success, or -1 on failure.
1.3       jfb       788:  */
                    789: static int
1.16      jfb       790: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb       791: {
                    792:        int i;
                    793:        size_t nb;
1.16      jfb       794:        CVSFILE *cf, **cfvec;
                    795:
                    796:        cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
                    797:        if (cfvec == NULL) {
                    798:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                    799:                return (-1);
                    800:        }
1.3       jfb       801:
                    802:        i = 0;
1.10      jfb       803:        TAILQ_FOREACH(cf, flp, cf_list) {
1.16      jfb       804:                if (i == (int)nfiles) {
1.3       jfb       805:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb       806:                        /* rebuild the list and abort sorting */
                    807:                        while (--i >= 0)
                    808:                                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
                    809:                        free(cfvec);
1.3       jfb       810:                        return (-1);
                    811:                }
1.16      jfb       812:                cfvec[i++] = cf;
1.3       jfb       813:
                    814:                /* now unlink it from the list,
                    815:                 * we'll put it back in order later
                    816:                 */
1.10      jfb       817:                TAILQ_REMOVE(flp, cf, cf_list);
1.3       jfb       818:        }
                    819:
                    820:        /* clear the list just in case */
1.10      jfb       821:        TAILQ_INIT(flp);
1.3       jfb       822:        nb = (size_t)i;
                    823:
                    824:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    825:
                    826:        /* rebuild the list from the bottom up */
                    827:        for (i = (int)nb - 1; i >= 0; i--)
1.10      jfb       828:                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       829:
1.16      jfb       830:        free(cfvec);
1.3       jfb       831:        return (0);
                    832: }
                    833:
                    834:
                    835: static int
                    836: cvs_file_cmp(const void *f1, const void *f2)
                    837: {
1.41      jfb       838:        const CVSFILE *cf1, *cf2;
                    839:        cf1 = *(const CVSFILE **)f1;
                    840:        cf2 = *(const CVSFILE **)f2;
1.34      jfb       841:        return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6       jfb       842: }
                    843:
                    844:
1.34      jfb       845: /*
                    846:  * cvs_file_alloc()
                    847:  *
                    848:  * Allocate a CVSFILE structure and initialize its internals.
                    849:  */
1.6       jfb       850: CVSFILE*
                    851: cvs_file_alloc(const char *path, u_int type)
                    852: {
                    853:        CVSFILE *cfp;
                    854:        struct cvs_dir *ddat;
                    855:
1.14      jfb       856:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb       857:        if (cfp == NULL) {
                    858:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    859:                return (NULL);
                    860:        }
                    861:        memset(cfp, 0, sizeof(*cfp));
                    862:
1.57      jfb       863:        cfp->cf_name = cvs_strdup(basename(path));
1.34      jfb       864:        if (cfp->cf_name == NULL) {
1.57      jfb       865:                cvs_log(LP_ERR, "failed to copy file name");
1.58      tedu      866:                cvs_file_free(cfp);
1.6       jfb       867:                return (NULL);
                    868:        }
                    869:        cfp->cf_type = type;
                    870:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    871:
                    872:        if (type == DT_DIR) {
                    873:                ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
                    874:                if (ddat == NULL) {
1.41      jfb       875:                        cvs_log(LP_ERRNO, "failed to allocate directory data");
1.6       jfb       876:                        cvs_file_free(cfp);
                    877:                        return (NULL);
                    878:                }
                    879:                memset(ddat, 0, sizeof(*ddat));
1.10      jfb       880:                TAILQ_INIT(&(ddat->cd_files));
1.6       jfb       881:                cfp->cf_ddat = ddat;
                    882:        }
                    883:        return (cfp);
1.1       jfb       884: }
1.14      jfb       885:
                    886:
                    887: /*
                    888:  * cvs_file_lget()
                    889:  *
                    890:  * Get the file and link it with the parent right away.
1.22      jfb       891:  * Returns a pointer to the created file structure on success, or NULL on
                    892:  * failure.
1.14      jfb       893:  */
                    894: static CVSFILE*
                    895: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
                    896: {
1.44      jfb       897:        int ret, cwd;
                    898:        u_int type;
1.14      jfb       899:        struct stat st;
                    900:        CVSFILE *cfp;
1.34      jfb       901:        struct cvs_ent *ent = NULL;
1.14      jfb       902:
1.44      jfb       903:        type = DT_UNKNOWN;
                    904:        cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.14      jfb       905:
1.44      jfb       906:        ret = stat(path, &st);
                    907:        if (ret == 0)
                    908:                type = IFTODT(st.st_mode);
1.14      jfb       909:
1.44      jfb       910:        if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14      jfb       911:                return (NULL);
                    912:        cfp->cf_parent = parent;
1.54      joris     913:
                    914:        if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
                    915:                cfp->cf_ddat->cd_flags |= CVS_DIRF_BASE;
1.14      jfb       916:
1.44      jfb       917:        if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL))
1.34      jfb       918:                ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), CVS_FILE_NAME(cfp));
1.14      jfb       919:
1.44      jfb       920:        if (ret == 0) {
                    921:                cfp->cf_mode = st.st_mode & ACCESSPERMS;
                    922:                cfp->cf_mtime = st.st_mtime;
                    923:
                    924:                if (ent == NULL)
                    925:                        cfp->cf_cvstat = (cwd == 1) ?
                    926:                            CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14      jfb       927:                else {
1.44      jfb       928:                        /* always show directories as up-to-date */
                    929:                        if (ent->ce_type == CVS_ENT_DIR)
1.14      jfb       930:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44      jfb       931:                        else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                    932:                                cfp->cf_cvstat = CVS_FST_ADDED;
                    933:                        else {
                    934:                                /* check last modified time */
                    935:                                if (ent->ce_mtime >= (time_t)st.st_mtime)
                    936:                                        cfp->cf_cvstat = CVS_FST_UPTODATE;
                    937:                                else
                    938:                                        cfp->cf_cvstat = CVS_FST_MODIFIED;
                    939:                        }
1.14      jfb       940:                }
1.44      jfb       941:        } else {
                    942:                if (ent == NULL) {
                    943:                        cvs_log(LP_ERR, "no Entry and no file for `%s'",
                    944:                            CVS_FILE_NAME(cfp));
                    945:                        cvs_file_free(cfp);
                    946:                        return (NULL);
                    947:                } else
                    948:                        cfp->cf_cvstat = CVS_FST_LOST;
1.14      jfb       949:        }
1.52      jfb       950:
                    951:        if (ent != NULL)
                    952:                cvs_ent_remove(CVS_DIR_ENTRIES(parent), CVS_FILE_NAME(cfp));
1.14      jfb       953:
1.26      jfb       954:        if ((cfp->cf_type == DT_DIR) && (cvs_file_getdir(cfp, flags) < 0)) {
                    955:                cvs_file_free(cfp);
                    956:                return (NULL);
1.14      jfb       957:        }
                    958:
                    959:        return (cfp);
1.23      jfb       960: }
                    961:
                    962:
                    963: static int
                    964: cvs_file_cmpname(const char *name1, const char *name2)
                    965: {
                    966:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    967:            (strcasecmp(name1, name2));
1.14      jfb       968: }