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

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 *);
                    100: static int        cvs_file_sort    (struct cvs_flist *);
                    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;
                    294:        char common[MAXPATHLEN];
1.14    ! jfb       295:        CVSFILE *cfp;
1.9       jfb       296:
                    297:        /* first find the common subdir */
                    298:        strlcpy(common, fspec[0], sizeof(common));
                    299:        for (i = 1; i < fsn; i++) {
                    300:                for (c = 0; ; c++) {
                    301:                        if (common[c] != fspec[i][c]) {
                    302:                                /* go back to last dir */
                    303:                                while ((c > 0) && (common[--c] != '/'))
                    304:                                        common[c] = '\0';
                    305:                                break;
                    306:                        }
                    307:                }
                    308:        }
                    309:        if (*common == '\0')
                    310:                strlcpy(common, ".", sizeof(common));
                    311:
                    312:        /* first load the common subdirectory */
                    313:        cfp = cvs_file_get(common, flags);
                    314:        for (i = 0; i < fsn; i++) {
1.3       jfb       315:        }
                    316:
                    317:        return (cfp);
                    318: }
                    319:
                    320:
                    321: /*
1.13      jfb       322:  * cvs_file_find()
                    323:  *
                    324:  * Find the pointer to a CVS file entry within the file hierarchy <hier>.
                    325:  * The file's pathname <path> must be relative to the base of <hier>.
                    326:  * Returns the entry on success, or NULL on failure.
                    327:  */
                    328:
                    329: CVSFILE*
                    330: cvs_file_find(CVSFILE *hier, const char *path)
                    331: {
                    332:        char *pp, *sp, pbuf[MAXPATHLEN];
                    333:        CVSFILE *sf, *cf;
                    334:
                    335:        strlcpy(pbuf, path, sizeof(pbuf));
                    336:
                    337:        cf = hier;
                    338:        pp = pbuf;
                    339:        do {
                    340:                sp = strchr(pp, '/');
                    341:                if (sp != NULL)
                    342:                        *sp = '\0';
                    343:
                    344:                /* special case */
                    345:                if (*pp == '.') {
                    346:                        if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
                    347:                                /* request to go back to parent */
                    348:                                if (cf->cf_parent == NULL) {
                    349:                                        cvs_log(LP_NOTICE,
                    350:                                            "path %s goes back too far", path);
                    351:                                        return (NULL);
                    352:                                }
                    353:                                cf = cf->cf_parent;
                    354:                                continue;
                    355:                        }
                    356:                        else if (*(pp + 1) == '\0')
                    357:                                continue;
                    358:                }
                    359:
                    360:                TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
                    361:                        if (strcmp(pp, sf->cf_name) == 0)
                    362:                                break;
                    363:                if (sf == NULL)
                    364:                        return (NULL);
                    365:
                    366:                cf = sf;
                    367:                pp = sp;
                    368:        } while (sp != NULL);
                    369:
                    370:        return (NULL);
                    371: }
                    372:
                    373:
                    374: /*
1.3       jfb       375:  * cvs_file_getdir()
                    376:  *
                    377:  * Get a cvs directory structure for the directory whose path is <dir>.
                    378:  */
                    379:
1.6       jfb       380: static int
1.14    ! jfb       381: cvs_file_getdir(CVSFILE *cf, int flags)
1.3       jfb       382: {
1.13      jfb       383:        int ret, fd;
1.3       jfb       384:        long base;
1.13      jfb       385:        void *dp, *ep;
1.11      jfb       386:        char fbuf[2048], pbuf[MAXPATHLEN];
1.3       jfb       387:        struct dirent *ent;
1.14    ! jfb       388:        CVSFILE *cfp;
1.3       jfb       389:        struct cvs_dir *cdp;
1.10      jfb       390:        struct cvs_flist dirs;
1.3       jfb       391:
1.10      jfb       392:        TAILQ_INIT(&dirs);
1.7       jfb       393:        cdp = cf->cf_ddat;
                    394:
1.4       jfb       395:        if (cvs_readrepo(cf->cf_path, pbuf, sizeof(pbuf)) == 0) {
                    396:                cdp->cd_repo = strdup(pbuf);
                    397:                if (cdp->cd_repo == NULL) {
                    398:                        free(cdp);
1.6       jfb       399:                        return (-1);
1.4       jfb       400:                }
1.3       jfb       401:        }
                    402:
                    403:        cdp->cd_root = cvsroot_get(cf->cf_path);
1.14    ! jfb       404:        printf("cvsroot = %s\n", cdp->cd_root->cr_str);
1.3       jfb       405:        if (cdp->cd_root == NULL) {
                    406:                cvs_file_freedir(cdp);
1.6       jfb       407:                return (-1);
1.3       jfb       408:        }
                    409:
1.14    ! jfb       410:        if (flags & CF_MKADMIN)
        !           411:                cvs_mkadmin(cf, 0755);
        !           412:
        !           413:        cdp->cd_ent = cvs_ent_open(cf->cf_path, O_RDONLY);
        !           414:
1.3       jfb       415:        fd = open(cf->cf_path, O_RDONLY);
                    416:        if (fd == -1) {
                    417:                cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
                    418:                cvs_file_freedir(cdp);
1.6       jfb       419:                return (-1);
1.3       jfb       420:        }
                    421:
1.11      jfb       422:        do {
                    423:                ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    424:                if (ret == -1) {
                    425:                        cvs_log(LP_ERRNO, "failed to get directory entries");
                    426:                        (void)close(fd);
                    427:                        cvs_file_freedir(cdp);
                    428:                        return (-1);
                    429:                }
1.10      jfb       430:
1.11      jfb       431:                dp = fbuf;
                    432:                ep = fbuf + (size_t)ret;
                    433:                while (dp < ep) {
                    434:                        ent = (struct dirent *)dp;
                    435:                        dp += ent->d_reclen;
                    436:
                    437:                        if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
                    438:                                continue;
                    439:
                    440:                        snprintf(pbuf, sizeof(pbuf), "%s/%s",
                    441:                            cf->cf_path, ent->d_name);
1.14    ! jfb       442:                        cfp = cvs_file_lget(pbuf, flags, cf);
1.11      jfb       443:                        if (cfp != NULL) {
                    444:                                cfp->cf_parent = cf;
                    445:                                if (cfp->cf_type == DT_DIR)
                    446:                                        TAILQ_INSERT_HEAD(&dirs, cfp, cf_list);
                    447:                                else
                    448:                                        TAILQ_INSERT_HEAD(&(cdp->cd_files), cfp,
                    449:                                            cf_list);
                    450:                        }
1.4       jfb       451:                }
1.11      jfb       452:        } while (ret > 0);
1.3       jfb       453:
1.14    ! jfb       454:        /* we can now close our Entries file */
        !           455:        if (cdp->cd_ent != NULL) {
        !           456:                cvs_ent_close(cdp->cd_ent);
        !           457:                cdp->cd_ent = NULL;
        !           458:        }
        !           459:
1.10      jfb       460:        if (flags & CF_SORT) {
1.3       jfb       461:                cvs_file_sort(&(cdp->cd_files));
1.10      jfb       462:                cvs_file_sort(&dirs);
                    463:        }
                    464:        TAILQ_FOREACH(cfp, &dirs, cf_list)
                    465:                TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.3       jfb       466:
                    467:        (void)close(fd);
1.6       jfb       468:        cf->cf_ddat = cdp;
1.3       jfb       469:
1.6       jfb       470:        return (0);
1.3       jfb       471: }
                    472:
                    473:
                    474: /*
                    475:  * cvs_file_free()
                    476:  *
                    477:  * Free a cvs_file structure and its contents.
                    478:  */
                    479:
                    480: void
1.14    ! jfb       481: cvs_file_free(CVSFILE *cf)
1.3       jfb       482: {
                    483:        if (cf->cf_path != NULL)
                    484:                free(cf->cf_path);
                    485:        if (cf->cf_stat != NULL)
                    486:                free(cf->cf_stat);
                    487:        if (cf->cf_ddat != NULL)
                    488:                cvs_file_freedir(cf->cf_ddat);
                    489:        free(cf);
1.5       jfb       490: }
                    491:
                    492:
                    493: /*
                    494:  * cvs_file_examine()
                    495:  *
                    496:  * Examine the contents of the CVS file structure <cf> with the function
                    497:  * <exam>.  The function is called for all subdirectories and files of the
                    498:  * root file.
                    499:  */
                    500:
                    501: int
                    502: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
                    503: {
                    504:        int ret;
1.14    ! jfb       505:        CVSFILE *fp;
1.5       jfb       506:
                    507:        if (cf->cf_type == DT_DIR) {
                    508:                ret = (*exam)(cf, arg);
1.10      jfb       509:                TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5       jfb       510:                        ret = cvs_file_examine(fp, exam, arg);
                    511:                        if (ret == -1)
1.13      jfb       512:                                break;
1.5       jfb       513:                }
                    514:        }
                    515:        else
1.13      jfb       516:                ret = (*exam)(cf, arg);
                    517:
                    518:        return (ret);
1.3       jfb       519: }
                    520:
                    521:
                    522: /*
                    523:  * cvs_file_freedir()
                    524:  *
                    525:  * Free a cvs_dir structure and its contents.
                    526:  */
                    527:
                    528: static void
                    529: cvs_file_freedir(struct cvs_dir *cd)
                    530: {
1.14    ! jfb       531:        CVSFILE *cfp;
1.3       jfb       532:
                    533:        if (cd->cd_root != NULL)
                    534:                cvsroot_free(cd->cd_root);
                    535:        if (cd->cd_repo != NULL)
                    536:                free(cd->cd_repo);
                    537:
1.14    ! jfb       538:        if (cd->cd_ent != NULL)
        !           539:                cvs_ent_close(cd->cd_ent);
        !           540:
1.10      jfb       541:        while (!TAILQ_EMPTY(&(cd->cd_files))) {
                    542:                cfp = TAILQ_FIRST(&(cd->cd_files));
                    543:                TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3       jfb       544:                cvs_file_free(cfp);
                    545:        }
                    546: }
                    547:
                    548:
                    549: /*
                    550:  * cvs_file_sort()
                    551:  *
                    552:  * Sort a list of cvs file structures according to their filename.
                    553:  */
                    554:
                    555: static int
                    556: cvs_file_sort(struct cvs_flist *flp)
                    557: {
                    558:        int i;
                    559:        size_t nb;
1.14    ! jfb       560:        CVSFILE *cf, *cfvec[256];
1.3       jfb       561:
                    562:        i = 0;
1.10      jfb       563:        TAILQ_FOREACH(cf, flp, cf_list) {
1.3       jfb       564:                cfvec[i++] = cf;
1.14    ! jfb       565:                if (i == sizeof(cfvec)/sizeof(CVSFILE *)) {
1.3       jfb       566:                        cvs_log(LP_WARN, "too many files to sort");
                    567:                        return (-1);
                    568:                }
                    569:
                    570:                /* now unlink it from the list,
                    571:                 * we'll put it back in order later
                    572:                 */
1.10      jfb       573:                TAILQ_REMOVE(flp, cf, cf_list);
1.3       jfb       574:        }
                    575:
                    576:        /* clear the list just in case */
1.10      jfb       577:        TAILQ_INIT(flp);
1.3       jfb       578:        nb = (size_t)i;
                    579:
                    580:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    581:
                    582:        /* rebuild the list from the bottom up */
                    583:        for (i = (int)nb - 1; i >= 0; i--)
1.10      jfb       584:                TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3       jfb       585:
                    586:        return (0);
                    587: }
                    588:
                    589:
                    590: static int
                    591: cvs_file_cmp(const void *f1, const void *f2)
                    592: {
1.14    ! jfb       593:        CVSFILE *cf1, *cf2;
        !           594:        cf1 = *(CVSFILE **)f1;
        !           595:        cf2 = *(CVSFILE **)f2;
1.3       jfb       596:        return strcmp(cf1->cf_name, cf2->cf_name);
1.6       jfb       597: }
                    598:
                    599:
                    600: CVSFILE*
                    601: cvs_file_alloc(const char *path, u_int type)
                    602: {
                    603:        size_t len;
                    604:        char pbuf[MAXPATHLEN];
                    605:        CVSFILE *cfp;
                    606:        struct cvs_dir *ddat;
                    607:
1.14    ! jfb       608:        cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6       jfb       609:        if (cfp == NULL) {
                    610:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    611:                return (NULL);
                    612:        }
                    613:        memset(cfp, 0, sizeof(*cfp));
                    614:
                    615:        /* ditch trailing slashes */
                    616:        strlcpy(pbuf, path, sizeof(pbuf));
                    617:        len = strlen(pbuf);
                    618:        while (pbuf[len - 1] == '/')
                    619:                pbuf[--len] = '\0';
                    620:
                    621:        cfp->cf_path = strdup(pbuf);
                    622:        if (cfp->cf_path == NULL) {
                    623:                free(cfp);
                    624:                return (NULL);
                    625:        }
                    626:
                    627:        cfp->cf_name = strrchr(cfp->cf_path, '/');
                    628:        if (cfp->cf_name == NULL)
                    629:                cfp->cf_name = cfp->cf_path;
                    630:        else
                    631:                cfp->cf_name++;
                    632:
                    633:        cfp->cf_type = type;
                    634:        cfp->cf_cvstat = CVS_FST_UNKNOWN;
                    635:
                    636:        if (type == DT_DIR) {
                    637:                ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
                    638:                if (ddat == NULL) {
                    639:                        cvs_file_free(cfp);
                    640:                        return (NULL);
                    641:                }
                    642:                memset(ddat, 0, sizeof(*ddat));
1.10      jfb       643:                TAILQ_INIT(&(ddat->cd_files));
1.6       jfb       644:                cfp->cf_ddat = ddat;
                    645:        }
                    646:        return (cfp);
1.1       jfb       647: }
1.14    ! jfb       648:
        !           649:
        !           650: /*
        !           651:  * cvs_file_lget()
        !           652:  *
        !           653:  * Get the file and link it with the parent right away.
        !           654:  */
        !           655:
        !           656: static CVSFILE*
        !           657: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
        !           658: {
        !           659:        int cwd;
        !           660:        size_t len;
        !           661:        char buf[32];
        !           662:        struct stat st;
        !           663:        struct tm lmtm;
        !           664:        CVSFILE *cfp;
        !           665:        struct cvs_ent *ent;
        !           666:
        !           667:        ent = NULL;
        !           668:
        !           669:        if (strcmp(path, ".") == 0)
        !           670:                cwd = 1;
        !           671:        else
        !           672:                cwd = 0;
        !           673:
        !           674:        if (stat(path, &st) == -1) {
        !           675:                cvs_log(LP_ERRNO, "failed to stat %s", path);
        !           676:                return (NULL);
        !           677:        }
        !           678:
        !           679:        cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
        !           680:        if (cfp == NULL) {
        !           681:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
        !           682:                return (NULL);
        !           683:        }
        !           684:        cfp->cf_parent = parent;
        !           685:
        !           686:        if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL)) {
        !           687:                ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), path);
        !           688:        }
        !           689:
        !           690:        if (ent == NULL)
        !           691:                cfp->cf_cvstat = (cwd == 1) ?
        !           692:                    CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
        !           693:        else {
        !           694:                /* always show directories as up-to-date */
        !           695:                if (ent->ce_type == CVS_ENT_DIR)
        !           696:                        cfp->cf_cvstat = CVS_FST_UPTODATE;
        !           697:                else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
        !           698:                        cfp->cf_cvstat = CVS_FST_ADDED;
        !           699:                else {
        !           700:                        /* check last modified time */
        !           701:                        if ((gmtime_r((time_t *)&(st.st_mtime),
        !           702:                            &lmtm) == NULL) ||
        !           703:                            (asctime_r(&lmtm, buf) == NULL)) {
        !           704:                                cvs_log(LP_ERR,
        !           705:                                    "failed to generate file timestamp");
        !           706:                                /* fake an up to date file */
        !           707:                                strlcpy(buf, ent->ce_timestamp, sizeof(buf));
        !           708:                        }
        !           709:                        len = strlen(buf);
        !           710:                        if ((len > 0) && (buf[len - 1] == '\n'))
        !           711:                                buf[--len] = '\0';
        !           712:
        !           713:                        if (strcmp(buf, ent->ce_timestamp) == 0)
        !           714:                                cfp->cf_cvstat = CVS_FST_UPTODATE;
        !           715:                        else
        !           716:                                cfp->cf_cvstat = CVS_FST_MODIFIED;
        !           717:                }
        !           718:
        !           719:                cvs_ent_free(ent);
        !           720:        }
        !           721:
        !           722:        if ((cfp->cf_type == DT_DIR) && ((flags & CF_RECURSE) || cwd)) {
        !           723:                if ((flags & CF_KNOWN) && (cfp->cf_cvstat == CVS_FST_UNKNOWN)) {
        !           724:                        free(cfp->cf_ddat);
        !           725:                        cfp->cf_ddat = NULL;
        !           726:                }
        !           727:                else if (cvs_file_getdir(cfp, flags) < 0) {
        !           728:                        cvs_file_free(cfp);
        !           729:                        return (NULL);
        !           730:                }
        !           731:        }
        !           732:
        !           733:        if (flags & CF_STAT) {
        !           734:                cfp->cf_stat = (struct stat *)malloc(sizeof(struct stat));
        !           735:                if (cfp->cf_stat == NULL) {
        !           736:                        cvs_log(LP_ERRNO, "failed to allocate stat structure");
        !           737:                        cvs_file_free(cfp);
        !           738:                        return (NULL);
        !           739:                }
        !           740:
        !           741:                memcpy(cfp->cf_stat, &st, sizeof(struct stat));
        !           742:        }
        !           743:
        !           744:        return (cfp);
        !           745: }
        !           746:
        !           747: