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

1.1       jfb         1: /*     $OpenBSD$       */
                      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>
                     35: #include <dirent.h>
                     36: #include <stdlib.h>
                     37: #include <unistd.h>
                     38: #include <string.h>
                     39: #include <fnmatch.h>
                     40:
                     41: #include "cvs.h"
                     42: #include "log.h"
1.14      jfb        43: #include "file.h"
1.1       jfb        44:
                     45:
                     46: #define CVS_IGN_STATIC    0x01     /* pattern is static, no need to glob */
                     47:
                     48: #define CVS_CHAR_ISMETA(c)  ((c == '*') || (c == '?') || (c == '['))
                     49:
                     50:
                     51: /* ignore pattern */
                     52: struct cvs_ignpat {
                     53:        char  ip_pat[MAXNAMLEN];
                     54:        int   ip_flags;
                     55:        TAILQ_ENTRY (cvs_ignpat) ip_list;
                     56: };
                     57:
                     58:
                     59: /*
                     60:  * Standard patterns to ignore.
                     61:  */
                     62:
                     63: static const char *cvs_ign_std[] = {
                     64:        ".",
                     65:        "..",
                     66:        "*.o",
                     67:        "*.so",
                     68:        "*.bak",
                     69:        "*.orig",
                     70:        "*.rej",
                     71:        "*.exe",
                     72:        "*.depend",
                     73:        "CVS",
                     74:        "core",
1.8       jfb        75:        ".#*",
1.1       jfb        76: #ifdef OLD_SMELLY_CRUFT
                     77:        "RCSLOG",
                     78:        "tags",
                     79:        "TAGS",
                     80:        "RCS",
                     81:        "SCCS",
                     82:        "#*",
                     83:        ",*",
                     84: #endif
                     85: };
                     86:
                     87:
1.11      jfb        88: /*
                     89:  * Entries in the CVS/Entries file with a revision of '0' have only been
                     90:  * added.  Compare against this revision to see if this is the case
                     91:  */
1.4       jfb        92: static RCSNUM *cvs_addedrev;
                     93:
                     94:
1.1       jfb        95: TAILQ_HEAD(, cvs_ignpat)  cvs_ign_pats;
                     96:
                     97:
1.14      jfb        98: static int        cvs_file_getdir  (CVSFILE *, int);
1.6       jfb        99: static void       cvs_file_freedir (struct cvs_dir *);
1.16      jfb       100: static int        cvs_file_sort    (struct cvs_flist *, u_int);
1.6       jfb       101: static int        cvs_file_cmp     (const void *, const void *);
1.23      jfb       102: static int        cvs_file_cmpname (const char *, const char *);
1.6       jfb       103: static CVSFILE*   cvs_file_alloc   (const char *, u_int);
1.14      jfb       104: static CVSFILE*   cvs_file_lget    (const char *, int, CVSFILE *);
1.3       jfb       105:
                    106:
                    107:
1.1       jfb       108: /*
                    109:  * cvs_file_init()
                    110:  *
                    111:  */
                    112:
                    113: int
                    114: cvs_file_init(void)
                    115: {
                    116:        int i;
                    117:        size_t len;
                    118:        char path[MAXPATHLEN], buf[MAXNAMLEN];
                    119:        FILE *ifp;
                    120:        struct passwd *pwd;
                    121:
                    122:        TAILQ_INIT(&cvs_ign_pats);
                    123:
1.4       jfb       124:        cvs_addedrev = rcsnum_alloc();
                    125:        rcsnum_aton("0", NULL, cvs_addedrev);
                    126:
1.1       jfb       127:        /* standard patterns to ignore */
1.10      jfb       128:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.26    ! jfb       129:                cvs_file_ignore(cvs_ign_std[i]);
1.1       jfb       130:
                    131:        /* read the cvsignore file in the user's home directory, if any */
                    132:        pwd = getpwuid(getuid());
                    133:        if (pwd != NULL) {
                    134:                snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
                    135:                ifp = fopen(path, "r");
                    136:                if (ifp == NULL) {
                    137:                        if (errno != ENOENT)
                    138:                                cvs_log(LP_ERRNO, "failed to open `%s'", path);
                    139:                }
                    140:                else {
                    141:                        while (fgets(buf, sizeof(buf), ifp) != NULL) {
                    142:                                len = strlen(buf);
                    143:                                if (len == 0)
                    144:                                        continue;
                    145:                                if (buf[len - 1] != '\n') {
                    146:                                        cvs_log(LP_ERR, "line too long in `%s'",
                    147:                                            path);
                    148:                                }
                    149:                                buf[--len] = '\0';
                    150:                                cvs_file_ignore(buf);
                    151:                        }
                    152:                        (void)fclose(ifp);
                    153:                }
                    154:        }
                    155:
                    156:        return (0);
                    157: }
                    158:
                    159:
                    160: /*
                    161:  * cvs_file_ignore()
                    162:  *
                    163:  * Add the pattern <pat> to the list of patterns for files to ignore.
                    164:  * Returns 0 on success, or -1 on failure.
                    165:  */
                    166:
                    167: int
                    168: cvs_file_ignore(const char *pat)
                    169: {
                    170:        char *cp;
                    171:        struct cvs_ignpat *ip;
                    172:
                    173:        ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
                    174:        if (ip == NULL) {
                    175:                cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
                    176:                return (-1);
                    177:        }
                    178:
                    179:        strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    180:
                    181:        /* check if we will need globbing for that pattern */
                    182:        ip->ip_flags = CVS_IGN_STATIC;
                    183:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    184:                if (CVS_CHAR_ISMETA(*cp)) {
                    185:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    186:                        break;
                    187:                }
                    188:        }
                    189:
                    190:        TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
                    191:
                    192:        return (0);
                    193: }
                    194:
                    195:
                    196: /*
1.5       jfb       197:  * cvs_file_chkign()
1.1       jfb       198:  *
                    199:  * Returns 1 if the filename <file> is matched by one of the ignore
                    200:  * patterns, or 0 otherwise.
                    201:  */
                    202:
                    203: int
1.5       jfb       204: cvs_file_chkign(const char *file)
1.1       jfb       205: {
1.23      jfb       206:        int flags;
1.1       jfb       207:        struct cvs_ignpat *ip;
                    208:
1.23      jfb       209:        flags = FNM_PERIOD;
                    210:        if (cvs_nocase)
                    211:                flags |= FNM_CASEFOLD;
                    212:
1.1       jfb       213:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    214:                if (ip->ip_flags & CVS_IGN_STATIC) {
1.23      jfb       215:                        if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1       jfb       216:                                return (1);
                    217:                }
1.23      jfb       218:                else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1       jfb       219:                        return (1);
                    220:        }
                    221:
                    222:        return (0);
                    223: }
                    224:
                    225:
                    226: /*
1.6       jfb       227:  * cvs_file_create()
1.1       jfb       228:  *
1.6       jfb       229:  * Create a new file whose path is specified in <path> and of type <type>.
1.26    ! jfb       230:  * If the type is DT_DIR, the CVS administrative repository and files will be
        !           231:  * created.
1.25      jfb       232:  * Returns the created file on success, or NULL on failure.
1.1       jfb       233:  */
                    234:
1.6       jfb       235: CVSFILE*
                    236: cvs_file_create(const char *path, u_int type, mode_t mode)
1.1       jfb       237: {
1.6       jfb       238:        int fd;
                    239:        CVSFILE *cfp;
1.1       jfb       240:
1.6       jfb       241:        cfp = cvs_file_alloc(path, type);
                    242:        if (cfp == NULL)
                    243:                return (NULL);
1.26    ! jfb       244:
1.6       jfb       245:        cfp->cf_type = type;
1.22      jfb       246:        cfp->cf_mode = mode;
1.26    ! jfb       247:        cfp->cf_ddat->cd_root = cvsroot_get(path);
1.1       jfb       248:
1.6       jfb       249:        if (type == DT_DIR) {
1.26    ! jfb       250:                if ((mkdir(path, mode) == -1) || (cvs_mkadmin(cfp, mode) < 0)) {
1.6       jfb       251:                        cvs_file_free(cfp);
                    252:                        return (NULL);
                    253:                }
1.26    ! jfb       254:
1.1       jfb       255:        }
1.6       jfb       256:        else {
                    257:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    258:                if (fd == -1) {
                    259:                        cvs_file_free(cfp);
1.1       jfb       260:                        return (NULL);
                    261:                }
1.6       jfb       262:                (void)close(fd);
1.1       jfb       263:        }
                    264:
1.6       jfb       265:        return (cfp);
1.3       jfb       266: }
                    267:
                    268:
                    269: /*
                    270:  * cvs_file_get()
                    271:  *
                    272:  * Load a cvs_file structure with all the information pertaining to the file
                    273:  * <path>.
1.4       jfb       274:  * The <flags> parameter specifies various flags that alter the behaviour of
1.21      jfb       275:  * the function.  The CF_RECURSE flag causes the function to recursively load
                    276:  * subdirectories when <path> is a directory.
                    277:  * The CF_SORT flag causes the files to be sorted in alphabetical order upon
                    278:  * loading.  The special case of "." as a path specification generates
                    279:  * recursion for a single level and is equivalent to calling cvs_file_get() on
                    280:  * all files of that directory.
1.3       jfb       281:  * Returns a pointer to the cvs file structure, which must later be freed
                    282:  * with cvs_file_free().
                    283:  */
                    284:
1.14      jfb       285: CVSFILE*
1.3       jfb       286: cvs_file_get(const char *path, int flags)
                    287: {
1.14      jfb       288:        return cvs_file_lget(path, flags, NULL);
1.9       jfb       289: }
                    290:
                    291:
                    292: /*
                    293:  * cvs_file_getspec()
                    294:  *
                    295:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    296:  * whose size is given in <fsn>.
                    297:  * Returns a pointer to the lowest common subdirectory to all specified
                    298:  * files.
                    299:  */
                    300:
                    301: CVSFILE*
                    302: cvs_file_getspec(char **fspec, int fsn, int flags)
                    303: {
1.26    ! jfb       304:        int i;
        !           305:        char *sp, *np, pcopy[MAXPATHLEN];
        !           306:        CVSFILE *base, *cf, *nf;
        !           307:
        !           308:        base = cvs_file_get(".", 0);
        !           309:        if (base == NULL)
        !           310:                return (NULL);
        !           311:
        !           312:        for (i = 0; i < fsn; i++) {
        !           313:                strlcpy(pcopy, fspec[i], sizeof(pcopy));
        !           314:                cf = base;
        !           315:                sp = pcopy;
        !           316:
        !           317:                do {
        !           318:                        np = strchr(sp, '/');
        !           319:                        if (np != NULL)
        !           320:                                *np = '\0';
        !           321:                        nf = cvs_file_find(cf, sp);
        !           322:                        if (nf == NULL) {
        !           323:                                nf = cvs_file_lget(pcopy, 0, cf);
        !           324:                                if (nf == NULL) {
        !           325:                                        cvs_file_free(base);
        !           326:                                        return (NULL);
        !           327:                                }
        !           328:
        !           329:                                cvs_file_attach(cf, nf);
        !           330:                        }
        !           331:
        !           332:                        if (np != NULL) {
        !           333:                                *np = '/';
        !           334:                                sp = np + 1;
        !           335:                        }
        !           336:
        !           337:                        cf = nf;
        !           338:                } while (np != NULL);
        !           339:        }
        !           340:
        !           341:        return (base);
1.3       jfb       342: }
                    343:
                    344:
                    345: /*
1.13      jfb       346:  * cvs_file_find()
                    347:  *
                    348:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    349:  * The file's pathname <path> must be relative to the base of <hier>.
                    350:  * Returns the entry on success, or NULL on failure.
                    351:  */
                    352:
                    353: CVSFILE*
                    354: cvs_file_find(CVSFILE *hier, const char *path)
                    355: {
                    356:        char *pp, *sp, pbuf[MAXPATHLEN];
                    357:        CVSFILE *sf, *cf;
                    358:
                    359:        strlcpy(pbuf, path, sizeof(pbuf));
                    360:
                    361:        cf = hier;
                    362:        pp = pbuf;
                    363:        do {
                    364:                sp = strchr(pp, '/');
                    365:                if (sp != NULL)
1.24      jfb       366:                        *(sp++) = '\0';
1.13      jfb       367:
                    368:                /* special case */
                    369:                if (*pp == '.') {
                    370:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    371:                                /* request to go back to parent */
                    372:                                if (cf->cf_parent == NULL) {
                    373:                                        cvs_log(LP_NOTICE,
                    374:                                            "path %s goes back too far", path);
                    375:                                        return (NULL);
                    376:                                }
                    377:                                cf = cf->cf_parent;
                    378:                                continue;
                    379:                        }
                    380:                        else if (*(pp + 1) == '\0')
                    381:                                continue;
                    382:                }
                    383:
                    384:                TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
1.23      jfb       385:                        if (cvs_file_cmpname(pp, sf->cf_name) == 0)
1.13      jfb       386:                                break;
                    387:                if (sf == NULL)
                    388:                        return (NULL);
                    389:
                    390:                cf = sf;
                    391:                pp = sp;
                    392:        } while (sp != NULL);
                    393:
1.24      jfb       394:        return (cf);
1.13      jfb       395: }
                    396:
                    397:
                    398: /*
1.22      jfb       399:  * cvs_file_attach()
                    400:  *
                    401:  * Attach the file <file> as one of the children of parent <parent>, which
                    402:  * has to be a file of type DT_DIR.
                    403:  * Returns 0 on success, or -1 on failure.
                    404:  */
                    405:
                    406: int
                    407: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
                    408: {
1.23      jfb       409:        struct cvs_dir *dp;
                    410:        struct cvs_ent *ent;
1.22      jfb       411:
                    412:        if (parent->cf_type != DT_DIR)
                    413:                return (-1);
                    414:
1.23      jfb       415:        dp = parent->cf_ddat;
                    416:
                    417:        TAILQ_INSERT_TAIL(&(dp->cd_files), file, cf_list);
                    418:        dp->cd_nfiles++;
1.22      jfb       419:        file->cf_parent = parent;
                    420:
                    421:        return (0);
                    422: }
                    423:
                    424:
                    425: /*
1.3       jfb       426:  * cvs_file_getdir()
                    427:  *
                    428:  * Get a cvs directory structure for the directory whose path is <dir>.
                    429:  */
                    430:
1.6       jfb       431: static int
1.14      jfb       432: cvs_file_getdir(CVSFILE *cf, int flags)
1.3       jfb       433: {
1.13      jfb       434:        int ret, fd;
1.16      jfb       435:        u_int ndirs;
1.3       jfb       436:        long base;
1.13      jfb       437:        void *dp, *ep;
1.11      jfb       438:        char fbuf[2048], pbuf[MAXPATHLEN];
1.3       jfb       439:        struct dirent *ent;
1.14      jfb       440:        CVSFILE *cfp;
1.20      jfb       441:        struct stat st;
1.3       jfb       442:        struct cvs_dir *cdp;
1.10      jfb       443:        struct cvs_flist dirs;
1.3       jfb       444:
1.18      jfb       445:        ndirs = 0;
1.10      jfb       446:        TAILQ_INIT(&dirs);
1.7       jfb       447:        cdp = cf->cf_ddat;
                    448:
1.26    ! jfb       449:        if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
        !           450:                cdp->cd_root = cvsroot_get(cf->cf_path);
        !           451:                if (cdp->cd_root == NULL) {
        !           452:                        cvs_file_freedir(cdp);
        !           453:                        return (-1);
        !           454:                }
1.3       jfb       455:
1.26    ! jfb       456:                if (flags & CF_MKADMIN)
        !           457:                        cvs_mkadmin(cf, 0755);
1.14      jfb       458:
1.26    ! jfb       459:                /* if the CVS administrative directory exists, load the info */
        !           460:                snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR,
        !           461:                    cf->cf_path);
        !           462:                if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
        !           463:                        if (cvs_readrepo(cf->cf_path, pbuf,
        !           464:                            sizeof(pbuf)) == 0) {
        !           465:                                cdp->cd_repo = strdup(pbuf);
        !           466:                                if (cdp->cd_repo == NULL) {
        !           467:                                        free(cdp);
        !           468:                                        return (-1);
        !           469:                                }
1.20      jfb       470:                        }
1.26    ! jfb       471:
        !           472:                        cdp->cd_ent = cvs_ent_open(cf->cf_path, O_RDWR);
1.20      jfb       473:                }
                    474:
                    475:        }
1.14      jfb       476:
1.26    ! jfb       477:        if (!(flags & CF_RECURSE) || (cf->cf_cvstat == CVS_FST_UNKNOWN))
        !           478:                return (0);
        !           479:
1.3       jfb       480:        fd = open(cf->cf_path, O_RDONLY);
                    481:        if (fd == -1) {
                    482:                cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
                    483:                cvs_file_freedir(cdp);
1.6       jfb       484:                return (-1);
1.3       jfb       485:        }
                    486:
1.11      jfb       487:        do {
                    488:                ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    489:                if (ret == -1) {
                    490:                        cvs_log(LP_ERRNO, "failed to get directory entries");
                    491:                        (void)close(fd);
                    492:                        cvs_file_freedir(cdp);
                    493:                        return (-1);
                    494:                }
1.10      jfb       495:
1.11      jfb       496:                dp = fbuf;
                    497:                ep = fbuf + (size_t)ret;
                    498:                while (dp < ep) {
                    499:                        ent = (struct dirent *)dp;
                    500:                        dp += ent->d_reclen;
                    501:
                    502:                        if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.24      jfb       503:                                continue;
                    504:
                    505:                        if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
1.11      jfb       506:                                continue;
                    507:
                    508:                        snprintf(pbuf, sizeof(pbuf), "%s/%s",
                    509:                            cf->cf_path, ent->d_name);
1.14      jfb       510:                        cfp = cvs_file_lget(pbuf, flags, cf);
1.11      jfb       511:                        if (cfp != NULL) {
1.26    ! jfb       512:                                if (cfp->cf_type == DT_DIR) {
        !           513:                                        TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.16      jfb       514:                                        ndirs++;
                    515:                                }
                    516:                                else {
1.26    ! jfb       517:                                        TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
1.11      jfb       518:                                            cf_list);
1.16      jfb       519:                                        cdp->cd_nfiles++;
                    520:                                }
1.11      jfb       521:                        }
1.4       jfb       522:                }
1.11      jfb       523:        } while (ret > 0);
1.14      jfb       524:
1.10      jfb       525:        if (flags & CF_SORT) {
1.16      jfb       526:                cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
                    527:                cvs_file_sort(&dirs, ndirs);
1.10      jfb       528:        }
1.26    ! jfb       529:
        !           530:        while (!TAILQ_EMPTY(&dirs)) {
        !           531:                cfp = TAILQ_FIRST(&dirs);
        !           532:                TAILQ_REMOVE(&dirs, cfp, cf_list);
1.10      jfb       533:                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.26    ! jfb       534:        }
1.16      jfb       535:        cdp->cd_nfiles += ndirs;
1.3       jfb       536:
                    537:        (void)close(fd);
                    538:
1.6       jfb       539:        return (0);
1.3       jfb       540: }
                    541:
                    542:
                    543: /*
                    544:  * cvs_file_free()
                    545:  *
                    546:  * Free a cvs_file structure and its contents.
                    547:  */
                    548:
                    549: void
1.14      jfb       550: cvs_file_free(CVSFILE *cf)
1.3       jfb       551: {
                    552:        if (cf->cf_path != NULL)
                    553:                free(cf->cf_path);
                    554:        if (cf->cf_ddat != NULL)
                    555:                cvs_file_freedir(cf->cf_ddat);
                    556:        free(cf);
1.5       jfb       557: }
                    558:
                    559:
                    560: /*
                    561:  * cvs_file_examine()
                    562:  *
                    563:  * Examine the contents of the CVS file structure <cf> with the function
                    564:  * <exam>.  The function is called for all subdirectories and files of the
                    565:  * root file.
                    566:  */
                    567:
                    568: int
                    569: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    570: {
                    571:        int ret;
1.14      jfb       572:        CVSFILE *fp;
1.5       jfb       573:
                    574:        if (cf->cf_type == DT_DIR) {
                    575:                ret = (*exam)(cf, arg);
1.10      jfb       576:                TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5       jfb       577:                        ret = cvs_file_examine(fp, exam, arg);
                    578:                        if (ret == -1)
1.13      jfb       579:                                break;
1.5       jfb       580:                }
                    581:        }
                    582:        else
1.13      jfb       583:                ret = (*exam)(cf, arg);
                    584:
                    585:        return (ret);
1.3       jfb       586: }
                    587:
                    588:
                    589: /*
                    590:  * cvs_file_freedir()
                    591:  *
                    592:  * Free a cvs_dir structure and its contents.
                    593:  */
                    594:
                    595: static void
                    596: cvs_file_freedir(struct cvs_dir *cd)
                    597: {
1.14      jfb       598:        CVSFILE *cfp;
1.3       jfb       599:
                    600:        if (cd->cd_root != NULL)
                    601:                cvsroot_free(cd->cd_root);
                    602:        if (cd->cd_repo != NULL)
                    603:                free(cd->cd_repo);
                    604:
1.14      jfb       605:        if (cd->cd_ent != NULL)
                    606:                cvs_ent_close(cd->cd_ent);
                    607:
1.10      jfb       608:        while (!TAILQ_EMPTY(&(cd->cd_files))) {
                    609:                cfp = TAILQ_FIRST(&(cd->cd_files));
                    610:                TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3       jfb       611:                cvs_file_free(cfp);
                    612:        }
                    613: }
                    614:
                    615:
                    616: /*
                    617:  * cvs_file_sort()
                    618:  *
1.16      jfb       619:  * Sort a list of cvs file structures according to their filename.  The list
                    620:  * <flp> is modified according to the sorting algorithm.  The number of files
                    621:  * in the list must be given by <nfiles>.
                    622:  * Returns 0 on success, or -1 on failure.
1.3       jfb       623:  */
                    624:
                    625: static int
1.16      jfb       626: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb       627: {
                    628:        int i;
                    629:        size_t nb;
1.16      jfb       630:        CVSFILE *cf, **cfvec;
                    631:
                    632:        cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
                    633:        if (cfvec == NULL) {
                    634:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
                    635:                return (-1);
                    636:        }
1.3       jfb       637:
                    638:        i = 0;
1.10      jfb       639:        TAILQ_FOREACH(cf, flp, cf_list) {
1.16      jfb       640:                if (i == (int)nfiles) {
1.3       jfb       641:                        cvs_log(LP_WARN, "too many files to sort");
1.16      jfb       642:                        /* rebuild the list and abort sorting */
                    643:                        while (--i >= 0)
                    644:                                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
                    645:                        free(cfvec);
1.3       jfb       646:                        return (-1);
                    647:                }
1.16      jfb       648:                cfvec[i++] = cf;
1.3       jfb       649:
                    650:                /* now unlink it from the list,
                    651:                 * we'll put it back in order later
                    652:                 */
1.10      jfb       653:                TAILQ_REMOVE(flp, cf, cf_list);
1.3       jfb       654:        }
                    655:
                    656:        /* clear the list just in case */
1.10      jfb       657:        TAILQ_INIT(flp);
1.3       jfb       658:        nb = (size_t)i;
                    659:
                    660:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    661:
                    662:        /* rebuild the list from the bottom up */
                    663:        for (i = (int)nb - 1; i >= 0; i--)
1.10      jfb       664:                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       665:
1.16      jfb       666:        free(cfvec);
1.3       jfb       667:        return (0);
                    668: }
                    669:
                    670:
                    671: static int
                    672: cvs_file_cmp(const void *f1, const void *f2)
                    673: {
1.14      jfb       674:        CVSFILE *cf1, *cf2;
                    675:        cf1 = *(CVSFILE **)f1;
                    676:        cf2 = *(CVSFILE **)f2;
1.23      jfb       677:        return cvs_file_cmpname(cf1->cf_name, cf2->cf_name);
1.6       jfb       678: }
                    679:
                    680:
                    681: CVSFILE*
                    682: cvs_file_alloc(const char *path, u_int type)
                    683: {
                    684:        size_t len;
                    685:        char pbuf[MAXPATHLEN];
                    686:        CVSFILE *cfp;
                    687:        struct cvs_dir *ddat;
                    688:
1.14      jfb       689:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb       690:        if (cfp == NULL) {
                    691:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    692:                return (NULL);
                    693:        }
                    694:        memset(cfp, 0, sizeof(*cfp));
                    695:
                    696:        /* ditch trailing slashes */
                    697:        strlcpy(pbuf, path, sizeof(pbuf));
                    698:        len = strlen(pbuf);
                    699:        while (pbuf[len - 1] == '/')
                    700:                pbuf[--len] = '\0';
                    701:
                    702:        cfp->cf_path = strdup(pbuf);
                    703:        if (cfp->cf_path == NULL) {
                    704:                free(cfp);
                    705:                return (NULL);
                    706:        }
                    707:
                    708:        cfp->cf_name = strrchr(cfp->cf_path, '/');
                    709:        if (cfp->cf_name == NULL)
                    710:                cfp->cf_name = cfp->cf_path;
                    711:        else
                    712:                cfp->cf_name++;
                    713:
                    714:        cfp->cf_type = type;
                    715:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    716:
                    717:        if (type == DT_DIR) {
                    718:                ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
                    719:                if (ddat == NULL) {
                    720:                        cvs_file_free(cfp);
                    721:                        return (NULL);
                    722:                }
                    723:                memset(ddat, 0, sizeof(*ddat));
1.10      jfb       724:                TAILQ_INIT(&(ddat->cd_files));
1.6       jfb       725:                cfp->cf_ddat = ddat;
                    726:        }
                    727:        return (cfp);
1.1       jfb       728: }
1.14      jfb       729:
                    730:
                    731: /*
                    732:  * cvs_file_lget()
                    733:  *
                    734:  * Get the file and link it with the parent right away.
1.22      jfb       735:  * Returns a pointer to the created file structure on success, or NULL on
                    736:  * failure.
1.14      jfb       737:  */
                    738:
                    739: static CVSFILE*
                    740: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
                    741: {
                    742:        int cwd;
                    743:        size_t len;
                    744:        char buf[32];
                    745:        struct stat st;
                    746:        struct tm lmtm;
                    747:        CVSFILE *cfp;
                    748:        struct cvs_ent *ent;
                    749:
                    750:        ent = NULL;
                    751:
                    752:        if (strcmp(path, ".") == 0)
                    753:                cwd = 1;
                    754:        else
                    755:                cwd = 0;
                    756:
                    757:        if (stat(path, &st) == -1) {
                    758:                cvs_log(LP_ERRNO, "failed to stat %s", path);
                    759:                return (NULL);
                    760:        }
                    761:
                    762:        cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
                    763:        if (cfp == NULL) {
                    764:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    765:                return (NULL);
                    766:        }
                    767:        cfp->cf_parent = parent;
1.22      jfb       768:        cfp->cf_mode = st.st_mode & ACCESSPERMS;
                    769:        cfp->cf_mtime = st.st_mtime;
1.14      jfb       770:
                    771:        if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL)) {
1.15      jfb       772:                ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), cfp->cf_name);
1.14      jfb       773:        }
                    774:
1.26    ! jfb       775:        if (ent == NULL) {
1.14      jfb       776:                cfp->cf_cvstat = (cwd == 1) ?
                    777:                    CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.26    ! jfb       778:        }
1.14      jfb       779:        else {
                    780:                /* always show directories as up-to-date */
                    781:                if (ent->ce_type == CVS_ENT_DIR)
                    782:                        cfp->cf_cvstat = CVS_FST_UPTODATE;
                    783:                else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                    784:                        cfp->cf_cvstat = CVS_FST_ADDED;
                    785:                else {
                    786:                        /* check last modified time */
                    787:                        if ((gmtime_r((time_t *)&(st.st_mtime),
                    788:                            &lmtm) == NULL) ||
                    789:                            (asctime_r(&lmtm, buf) == NULL)) {
                    790:                                cvs_log(LP_ERR,
                    791:                                    "failed to generate file timestamp");
                    792:                                /* fake an up to date file */
                    793:                                strlcpy(buf, ent->ce_timestamp, sizeof(buf));
                    794:                        }
                    795:                        len = strlen(buf);
                    796:                        if ((len > 0) && (buf[len - 1] == '\n'))
                    797:                                buf[--len] = '\0';
                    798:
                    799:                        if (strcmp(buf, ent->ce_timestamp) == 0)
                    800:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
                    801:                        else
                    802:                                cfp->cf_cvstat = CVS_FST_MODIFIED;
                    803:                }
                    804:        }
                    805:
1.26    ! jfb       806:        if ((cfp->cf_type == DT_DIR) && (cvs_file_getdir(cfp, flags) < 0)) {
        !           807:                cvs_file_free(cfp);
        !           808:                return (NULL);
1.14      jfb       809:        }
                    810:
                    811:        return (cfp);
1.23      jfb       812: }
                    813:
                    814:
                    815: static int
                    816: cvs_file_cmpname(const char *name1, const char *name2)
                    817: {
                    818:        return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
                    819:            (strcasecmp(name1, name2));
1.14      jfb       820: }