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

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     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
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     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 *);
                    102: static CVSFILE*   cvs_file_alloc   (const char *, u_int);
1.14      jfb       103: static CVSFILE*   cvs_file_lget    (const char *, int, CVSFILE *);
1.3       jfb       104:
                    105:
                    106:
1.1       jfb       107: /*
                    108:  * cvs_file_init()
                    109:  *
                    110:  */
                    111:
                    112: int
                    113: cvs_file_init(void)
                    114: {
                    115:        int i;
                    116:        size_t len;
                    117:        char path[MAXPATHLEN], buf[MAXNAMLEN];
                    118:        FILE *ifp;
                    119:        struct passwd *pwd;
                    120:
                    121:        TAILQ_INIT(&cvs_ign_pats);
                    122:
1.4       jfb       123:        cvs_addedrev = rcsnum_alloc();
                    124:        rcsnum_aton("0", NULL, cvs_addedrev);
                    125:
1.1       jfb       126:        /* standard patterns to ignore */
1.10      jfb       127:        for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.1       jfb       128:                cvs_file_ignore(cvs_ign_std[i]);
                    129:
                    130:        /* read the cvsignore file in the user's home directory, if any */
                    131:        pwd = getpwuid(getuid());
                    132:        if (pwd != NULL) {
                    133:                snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
                    134:                ifp = fopen(path, "r");
                    135:                if (ifp == NULL) {
                    136:                        if (errno != ENOENT)
                    137:                                cvs_log(LP_ERRNO, "failed to open `%s'", path);
                    138:                }
                    139:                else {
                    140:                        while (fgets(buf, sizeof(buf), ifp) != NULL) {
                    141:                                len = strlen(buf);
                    142:                                if (len == 0)
                    143:                                        continue;
                    144:                                if (buf[len - 1] != '\n') {
                    145:                                        cvs_log(LP_ERR, "line too long in `%s'",
                    146:                                            path);
                    147:                                }
                    148:                                buf[--len] = '\0';
                    149:                                cvs_file_ignore(buf);
                    150:                        }
                    151:                        (void)fclose(ifp);
                    152:                }
                    153:        }
                    154:
                    155:        return (0);
                    156: }
                    157:
                    158:
                    159: /*
                    160:  * cvs_file_ignore()
                    161:  *
                    162:  * Add the pattern <pat> to the list of patterns for files to ignore.
                    163:  * Returns 0 on success, or -1 on failure.
                    164:  */
                    165:
                    166: int
                    167: cvs_file_ignore(const char *pat)
                    168: {
                    169:        char *cp;
                    170:        struct cvs_ignpat *ip;
                    171:
                    172:        ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
                    173:        if (ip == NULL) {
                    174:                cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
                    175:                return (-1);
                    176:        }
                    177:
                    178:        strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    179:
                    180:        /* check if we will need globbing for that pattern */
                    181:        ip->ip_flags = CVS_IGN_STATIC;
                    182:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    183:                if (CVS_CHAR_ISMETA(*cp)) {
                    184:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    185:                        break;
                    186:                }
                    187:        }
                    188:
                    189:        TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
                    190:
                    191:        return (0);
                    192: }
                    193:
                    194:
                    195: /*
1.5       jfb       196:  * cvs_file_chkign()
1.1       jfb       197:  *
                    198:  * Returns 1 if the filename <file> is matched by one of the ignore
                    199:  * patterns, or 0 otherwise.
                    200:  */
                    201:
                    202: int
1.5       jfb       203: cvs_file_chkign(const char *file)
1.1       jfb       204: {
                    205:        struct cvs_ignpat *ip;
                    206:
                    207:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    208:                if (ip->ip_flags & CVS_IGN_STATIC) {
                    209:                        if (strcmp(file, ip->ip_pat) == 0)
                    210:                                return (1);
                    211:                }
                    212:                else if (fnmatch(ip->ip_pat, file, FNM_PERIOD) == 0)
                    213:                        return (1);
                    214:        }
                    215:
                    216:        return (0);
                    217: }
                    218:
                    219:
                    220: /*
1.6       jfb       221:  * cvs_file_create()
1.1       jfb       222:  *
1.6       jfb       223:  * Create a new file whose path is specified in <path> and of type <type>.
1.1       jfb       224:  */
                    225:
1.6       jfb       226: CVSFILE*
                    227: cvs_file_create(const char *path, u_int type, mode_t mode)
1.1       jfb       228: {
1.6       jfb       229:        int fd;
                    230:        CVSFILE *cfp;
1.1       jfb       231:
1.6       jfb       232:        cfp = cvs_file_alloc(path, type);
                    233:        if (cfp == NULL)
                    234:                return (NULL);
                    235:        cfp->cf_type = type;
1.1       jfb       236:
1.6       jfb       237:        if (type == DT_DIR) {
                    238:                if (mkdir(path, mode) == -1) {
                    239:                        cvs_file_free(cfp);
                    240:                        return (NULL);
                    241:                }
1.1       jfb       242:        }
1.6       jfb       243:        else {
                    244:                fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
                    245:                if (fd == -1) {
                    246:                        cvs_file_free(cfp);
1.1       jfb       247:                        return (NULL);
                    248:                }
1.6       jfb       249:                (void)close(fd);
1.1       jfb       250:        }
                    251:
1.6       jfb       252:        return (cfp);
1.3       jfb       253: }
                    254:
                    255:
                    256: /*
                    257:  * cvs_file_get()
                    258:  *
                    259:  * Load a cvs_file structure with all the information pertaining to the file
                    260:  * <path>.
1.4       jfb       261:  * The <flags> parameter specifies various flags that alter the behaviour of
                    262:  * the function.  The CF_STAT flag is used to keep stat information of the
                    263:  * file in the structure after it is used (it is lost otherwise).  The
                    264:  * CF_RECURSE flag causes the function to recursively load subdirectories
                    265:  * when <path> is a directory.  The CF_SORT flag causes the files to be
                    266:  * sorted in alphabetical order upon loading.
                    267:  * The special case of "." as a path specification generates recursion for
                    268:  * a single level and is equivalent to calling cvs_file_get() on all files
                    269:  * of that directory.
1.3       jfb       270:  * Returns a pointer to the cvs file structure, which must later be freed
                    271:  * with cvs_file_free().
                    272:  */
                    273:
1.14      jfb       274: CVSFILE*
1.3       jfb       275: cvs_file_get(const char *path, int flags)
                    276: {
1.14      jfb       277:        return cvs_file_lget(path, flags, NULL);
1.9       jfb       278: }
                    279:
                    280:
                    281: /*
                    282:  * cvs_file_getspec()
                    283:  *
                    284:  * Load a specific set of files whose paths are given in the vector <fspec>,
                    285:  * whose size is given in <fsn>.
                    286:  * Returns a pointer to the lowest common subdirectory to all specified
                    287:  * files.
                    288:  */
                    289:
                    290: CVSFILE*
                    291: cvs_file_getspec(char **fspec, int fsn, int flags)
                    292: {
                    293:        int i, c;
1.15      jfb       294:        char common[MAXPATHLEN], *cp;
1.14      jfb       295:        CVSFILE *cfp;
1.9       jfb       296:
                    297:        /* first load the common subdirectory */
                    298:        cfp = cvs_file_get(common, flags);
                    299:        for (i = 0; i < fsn; i++) {
1.3       jfb       300:        }
                    301:
                    302:        return (cfp);
                    303: }
                    304:
                    305:
                    306: /*
1.13      jfb       307:  * cvs_file_find()
                    308:  *
                    309:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    310:  * The file's pathname <path> must be relative to the base of <hier>.
                    311:  * Returns the entry on success, or NULL on failure.
                    312:  */
                    313:
                    314: CVSFILE*
                    315: cvs_file_find(CVSFILE *hier, const char *path)
                    316: {
                    317:        char *pp, *sp, pbuf[MAXPATHLEN];
                    318:        CVSFILE *sf, *cf;
                    319:
                    320:        strlcpy(pbuf, path, sizeof(pbuf));
                    321:
                    322:        cf = hier;
                    323:        pp = pbuf;
                    324:        do {
                    325:                sp = strchr(pp, '/');
                    326:                if (sp != NULL)
                    327:                        *sp = '\0';
                    328:
                    329:                /* special case */
                    330:                if (*pp == '.') {
                    331:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    332:                                /* request to go back to parent */
                    333:                                if (cf->cf_parent == NULL) {
                    334:                                        cvs_log(LP_NOTICE,
                    335:                                            "path %s goes back too far", path);
                    336:                                        return (NULL);
                    337:                                }
                    338:                                cf = cf->cf_parent;
                    339:                                continue;
                    340:                        }
                    341:                        else if (*(pp + 1) == '\0')
                    342:                                continue;
                    343:                }
                    344:
                    345:                TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
                    346:                        if (strcmp(pp, sf->cf_name) == 0)
                    347:                                break;
                    348:                if (sf == NULL)
                    349:                        return (NULL);
                    350:
                    351:                cf = sf;
                    352:                pp = sp;
                    353:        } while (sp != NULL);
                    354:
                    355:        return (NULL);
                    356: }
                    357:
                    358:
                    359: /*
1.3       jfb       360:  * cvs_file_getdir()
                    361:  *
                    362:  * Get a cvs directory structure for the directory whose path is <dir>.
                    363:  */
                    364:
1.6       jfb       365: static int
1.14      jfb       366: cvs_file_getdir(CVSFILE *cf, int flags)
1.3       jfb       367: {
1.13      jfb       368:        int ret, fd;
1.16    ! jfb       369:        u_int ndirs;
1.3       jfb       370:        long base;
1.13      jfb       371:        void *dp, *ep;
1.11      jfb       372:        char fbuf[2048], pbuf[MAXPATHLEN];
1.3       jfb       373:        struct dirent *ent;
1.14      jfb       374:        CVSFILE *cfp;
1.3       jfb       375:        struct cvs_dir *cdp;
1.10      jfb       376:        struct cvs_flist dirs;
1.3       jfb       377:
1.10      jfb       378:        TAILQ_INIT(&dirs);
1.7       jfb       379:        cdp = cf->cf_ddat;
                    380:
1.4       jfb       381:        if (cvs_readrepo(cf->cf_path, pbuf, sizeof(pbuf)) == 0) {
                    382:                cdp->cd_repo = strdup(pbuf);
                    383:                if (cdp->cd_repo == NULL) {
                    384:                        free(cdp);
1.6       jfb       385:                        return (-1);
1.4       jfb       386:                }
1.3       jfb       387:        }
                    388:
                    389:        cdp->cd_root = cvsroot_get(cf->cf_path);
                    390:        if (cdp->cd_root == NULL) {
                    391:                cvs_file_freedir(cdp);
1.6       jfb       392:                return (-1);
1.3       jfb       393:        }
                    394:
1.14      jfb       395:        if (flags & CF_MKADMIN)
                    396:                cvs_mkadmin(cf, 0755);
                    397:
                    398:        cdp->cd_ent = cvs_ent_open(cf->cf_path, O_RDONLY);
                    399:
1.3       jfb       400:        fd = open(cf->cf_path, O_RDONLY);
                    401:        if (fd == -1) {
                    402:                cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
                    403:                cvs_file_freedir(cdp);
1.6       jfb       404:                return (-1);
1.3       jfb       405:        }
                    406:
1.11      jfb       407:        do {
                    408:                ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    409:                if (ret == -1) {
                    410:                        cvs_log(LP_ERRNO, "failed to get directory entries");
                    411:                        (void)close(fd);
                    412:                        cvs_file_freedir(cdp);
                    413:                        return (-1);
                    414:                }
1.10      jfb       415:
1.11      jfb       416:                dp = fbuf;
                    417:                ep = fbuf + (size_t)ret;
                    418:                while (dp < ep) {
                    419:                        ent = (struct dirent *)dp;
                    420:                        dp += ent->d_reclen;
                    421:
                    422:                        if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
                    423:                                continue;
                    424:
                    425:                        snprintf(pbuf, sizeof(pbuf), "%s/%s",
                    426:                            cf->cf_path, ent->d_name);
1.14      jfb       427:                        cfp = cvs_file_lget(pbuf, flags, cf);
1.11      jfb       428:                        if (cfp != NULL) {
1.16    ! jfb       429:                                if (cfp->cf_type == DT_DIR) {
1.11      jfb       430:                                        TAILQ_INSERT_HEAD(&dirs, cfp, cf_list);
1.16    ! jfb       431:                                        ndirs++;
        !           432:                                }
        !           433:                                else {
1.11      jfb       434:                                        TAILQ_INSERT_HEAD(&(cdp->cd_files), cfp,
                    435:                                            cf_list);
1.16    ! jfb       436:                                        cdp->cd_nfiles++;
        !           437:                                }
1.11      jfb       438:                        }
1.4       jfb       439:                }
1.11      jfb       440:        } while (ret > 0);
1.3       jfb       441:
1.14      jfb       442:        /* we can now close our Entries file */
                    443:        if (cdp->cd_ent != NULL) {
                    444:                cvs_ent_close(cdp->cd_ent);
                    445:                cdp->cd_ent = NULL;
                    446:        }
                    447:
1.10      jfb       448:        if (flags & CF_SORT) {
1.16    ! jfb       449:                cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
        !           450:                cvs_file_sort(&dirs, ndirs);
1.10      jfb       451:        }
                    452:        TAILQ_FOREACH(cfp, &dirs, cf_list)
                    453:                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.16    ! jfb       454:        cdp->cd_nfiles += ndirs;
1.3       jfb       455:
                    456:        (void)close(fd);
1.6       jfb       457:        cf->cf_ddat = cdp;
1.3       jfb       458:
1.6       jfb       459:        return (0);
1.3       jfb       460: }
                    461:
                    462:
                    463: /*
                    464:  * cvs_file_free()
                    465:  *
                    466:  * Free a cvs_file structure and its contents.
                    467:  */
                    468:
                    469: void
1.14      jfb       470: cvs_file_free(CVSFILE *cf)
1.3       jfb       471: {
                    472:        if (cf->cf_path != NULL)
                    473:                free(cf->cf_path);
                    474:        if (cf->cf_stat != NULL)
                    475:                free(cf->cf_stat);
                    476:        if (cf->cf_ddat != NULL)
                    477:                cvs_file_freedir(cf->cf_ddat);
                    478:        free(cf);
1.5       jfb       479: }
                    480:
                    481:
                    482: /*
                    483:  * cvs_file_examine()
                    484:  *
                    485:  * Examine the contents of the CVS file structure <cf> with the function
                    486:  * <exam>.  The function is called for all subdirectories and files of the
                    487:  * root file.
                    488:  */
                    489:
                    490: int
                    491: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    492: {
                    493:        int ret;
1.14      jfb       494:        CVSFILE *fp;
1.5       jfb       495:
                    496:        if (cf->cf_type == DT_DIR) {
                    497:                ret = (*exam)(cf, arg);
1.10      jfb       498:                TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5       jfb       499:                        ret = cvs_file_examine(fp, exam, arg);
                    500:                        if (ret == -1)
1.13      jfb       501:                                break;
1.5       jfb       502:                }
                    503:        }
                    504:        else
1.13      jfb       505:                ret = (*exam)(cf, arg);
                    506:
                    507:        return (ret);
1.3       jfb       508: }
                    509:
                    510:
                    511: /*
                    512:  * cvs_file_freedir()
                    513:  *
                    514:  * Free a cvs_dir structure and its contents.
                    515:  */
                    516:
                    517: static void
                    518: cvs_file_freedir(struct cvs_dir *cd)
                    519: {
1.14      jfb       520:        CVSFILE *cfp;
1.3       jfb       521:
                    522:        if (cd->cd_root != NULL)
                    523:                cvsroot_free(cd->cd_root);
                    524:        if (cd->cd_repo != NULL)
                    525:                free(cd->cd_repo);
                    526:
1.14      jfb       527:        if (cd->cd_ent != NULL)
                    528:                cvs_ent_close(cd->cd_ent);
                    529:
1.10      jfb       530:        while (!TAILQ_EMPTY(&(cd->cd_files))) {
                    531:                cfp = TAILQ_FIRST(&(cd->cd_files));
                    532:                TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3       jfb       533:                cvs_file_free(cfp);
                    534:        }
                    535: }
                    536:
                    537:
                    538: /*
                    539:  * cvs_file_sort()
                    540:  *
1.16    ! jfb       541:  * Sort a list of cvs file structures according to their filename.  The list
        !           542:  * <flp> is modified according to the sorting algorithm.  The number of files
        !           543:  * in the list must be given by <nfiles>.
        !           544:  * Returns 0 on success, or -1 on failure.
1.3       jfb       545:  */
                    546:
                    547: static int
1.16    ! jfb       548: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3       jfb       549: {
                    550:        int i;
                    551:        size_t nb;
1.16    ! jfb       552:        CVSFILE *cf, **cfvec;
        !           553:
        !           554:        cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
        !           555:        if (cfvec == NULL) {
        !           556:                cvs_log(LP_ERRNO, "failed to allocate sorting vector");
        !           557:                return (-1);
        !           558:        }
1.3       jfb       559:
                    560:        i = 0;
1.10      jfb       561:        TAILQ_FOREACH(cf, flp, cf_list) {
1.16    ! jfb       562:                if (i == (int)nfiles) {
1.3       jfb       563:                        cvs_log(LP_WARN, "too many files to sort");
1.16    ! jfb       564:                        /* rebuild the list and abort sorting */
        !           565:                        while (--i >= 0)
        !           566:                                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
        !           567:                        free(cfvec);
1.3       jfb       568:                        return (-1);
                    569:                }
1.16    ! jfb       570:                cfvec[i++] = cf;
1.3       jfb       571:
                    572:                /* now unlink it from the list,
                    573:                 * we'll put it back in order later
                    574:                 */
1.10      jfb       575:                TAILQ_REMOVE(flp, cf, cf_list);
1.3       jfb       576:        }
                    577:
                    578:        /* clear the list just in case */
1.10      jfb       579:        TAILQ_INIT(flp);
1.3       jfb       580:        nb = (size_t)i;
                    581:
                    582:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    583:
                    584:        /* rebuild the list from the bottom up */
                    585:        for (i = (int)nb - 1; i >= 0; i--)
1.10      jfb       586:                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       587:
1.16    ! jfb       588:        free(cfvec);
1.3       jfb       589:        return (0);
                    590: }
                    591:
                    592:
                    593: static int
                    594: cvs_file_cmp(const void *f1, const void *f2)
                    595: {
1.14      jfb       596:        CVSFILE *cf1, *cf2;
                    597:        cf1 = *(CVSFILE **)f1;
                    598:        cf2 = *(CVSFILE **)f2;
1.3       jfb       599:        return strcmp(cf1->cf_name, cf2->cf_name);
1.6       jfb       600: }
                    601:
                    602:
                    603: CVSFILE*
                    604: cvs_file_alloc(const char *path, u_int type)
                    605: {
                    606:        size_t len;
                    607:        char pbuf[MAXPATHLEN];
                    608:        CVSFILE *cfp;
                    609:        struct cvs_dir *ddat;
                    610:
1.14      jfb       611:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb       612:        if (cfp == NULL) {
                    613:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    614:                return (NULL);
                    615:        }
                    616:        memset(cfp, 0, sizeof(*cfp));
                    617:
                    618:        /* ditch trailing slashes */
                    619:        strlcpy(pbuf, path, sizeof(pbuf));
                    620:        len = strlen(pbuf);
                    621:        while (pbuf[len - 1] == '/')
                    622:                pbuf[--len] = '\0';
                    623:
                    624:        cfp->cf_path = strdup(pbuf);
                    625:        if (cfp->cf_path == NULL) {
                    626:                free(cfp);
                    627:                return (NULL);
                    628:        }
                    629:
                    630:        cfp->cf_name = strrchr(cfp->cf_path, '/');
                    631:        if (cfp->cf_name == NULL)
                    632:                cfp->cf_name = cfp->cf_path;
                    633:        else
                    634:                cfp->cf_name++;
                    635:
                    636:        cfp->cf_type = type;
                    637:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    638:
                    639:        if (type == DT_DIR) {
                    640:                ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
                    641:                if (ddat == NULL) {
                    642:                        cvs_file_free(cfp);
                    643:                        return (NULL);
                    644:                }
                    645:                memset(ddat, 0, sizeof(*ddat));
1.10      jfb       646:                TAILQ_INIT(&(ddat->cd_files));
1.6       jfb       647:                cfp->cf_ddat = ddat;
                    648:        }
                    649:        return (cfp);
1.1       jfb       650: }
1.14      jfb       651:
                    652:
                    653: /*
                    654:  * cvs_file_lget()
                    655:  *
                    656:  * Get the file and link it with the parent right away.
                    657:  */
                    658:
                    659: static CVSFILE*
                    660: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
                    661: {
                    662:        int cwd;
                    663:        size_t len;
                    664:        char buf[32];
                    665:        struct stat st;
                    666:        struct tm lmtm;
                    667:        CVSFILE *cfp;
                    668:        struct cvs_ent *ent;
                    669:
                    670:        ent = NULL;
                    671:
                    672:        if (strcmp(path, ".") == 0)
                    673:                cwd = 1;
                    674:        else
                    675:                cwd = 0;
                    676:
                    677:        if (stat(path, &st) == -1) {
                    678:                cvs_log(LP_ERRNO, "failed to stat %s", path);
                    679:                return (NULL);
                    680:        }
                    681:
                    682:        cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
                    683:        if (cfp == NULL) {
                    684:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    685:                return (NULL);
                    686:        }
                    687:        cfp->cf_parent = parent;
                    688:
                    689:        if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL)) {
1.15      jfb       690:                ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), cfp->cf_name);
1.14      jfb       691:        }
                    692:
                    693:        if (ent == NULL)
                    694:                cfp->cf_cvstat = (cwd == 1) ?
                    695:                    CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
                    696:        else {
                    697:                /* always show directories as up-to-date */
                    698:                if (ent->ce_type == CVS_ENT_DIR)
                    699:                        cfp->cf_cvstat = CVS_FST_UPTODATE;
                    700:                else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
                    701:                        cfp->cf_cvstat = CVS_FST_ADDED;
                    702:                else {
                    703:                        /* check last modified time */
                    704:                        if ((gmtime_r((time_t *)&(st.st_mtime),
                    705:                            &lmtm) == NULL) ||
                    706:                            (asctime_r(&lmtm, buf) == NULL)) {
                    707:                                cvs_log(LP_ERR,
                    708:                                    "failed to generate file timestamp");
                    709:                                /* fake an up to date file */
                    710:                                strlcpy(buf, ent->ce_timestamp, sizeof(buf));
                    711:                        }
                    712:                        len = strlen(buf);
                    713:                        if ((len > 0) && (buf[len - 1] == '\n'))
                    714:                                buf[--len] = '\0';
                    715:
                    716:                        if (strcmp(buf, ent->ce_timestamp) == 0)
                    717:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
                    718:                        else
                    719:                                cfp->cf_cvstat = CVS_FST_MODIFIED;
                    720:                }
                    721:        }
                    722:
                    723:        if ((cfp->cf_type == DT_DIR) && ((flags & CF_RECURSE) || cwd)) {
                    724:                if ((flags & CF_KNOWN) && (cfp->cf_cvstat == CVS_FST_UNKNOWN)) {
                    725:                        free(cfp->cf_ddat);
                    726:                        cfp->cf_ddat = NULL;
                    727:                }
                    728:                else if (cvs_file_getdir(cfp, flags) < 0) {
                    729:                        cvs_file_free(cfp);
                    730:                        return (NULL);
                    731:                }
                    732:        }
                    733:
                    734:        if (flags & CF_STAT) {
                    735:                cfp->cf_stat = (struct stat *)malloc(sizeof(struct stat));
                    736:                if (cfp->cf_stat == NULL) {
                    737:                        cvs_log(LP_ERRNO, "failed to allocate stat structure");
                    738:                        cvs_file_free(cfp);
                    739:                        return (NULL);
                    740:                }
                    741:
                    742:                memcpy(cfp->cf_stat, &st, sizeof(struct stat));
                    743:        }
                    744:
                    745:        return (cfp);
                    746: }
                    747:
                    748: