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

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"
                     43:
                     44:
                     45: #define CVS_IGN_STATIC    0x01     /* pattern is static, no need to glob */
                     46:
                     47:
                     48:
                     49: #define CVS_CHAR_ISMETA(c)  ((c == '*') || (c == '?') || (c == '['))
                     50:
                     51:
                     52:
                     53: /* ignore pattern */
                     54: struct cvs_ignpat {
                     55:        char  ip_pat[MAXNAMLEN];
                     56:        int   ip_flags;
                     57:        TAILQ_ENTRY (cvs_ignpat) ip_list;
                     58: };
                     59:
                     60:
                     61: /*
                     62:  * Standard patterns to ignore.
                     63:  */
                     64:
                     65: static const char *cvs_ign_std[] = {
                     66:        ".",
                     67:        "..",
                     68:        "*.o",
                     69:        "*.so",
                     70:        "*.bak",
                     71:        "*.orig",
                     72:        "*.rej",
                     73:        "*.exe",
                     74:        "*.depend",
                     75:        "CVS",
                     76:        "core",
                     77: #ifdef OLD_SMELLY_CRUFT
                     78:        "RCSLOG",
                     79:        "tags",
                     80:        "TAGS",
                     81:        "RCS",
                     82:        "SCCS",
                     83:        "#*",
                     84:        ".#*",
                     85:        ",*",
                     86: #endif
                     87: };
                     88:
                     89:
1.4     ! jfb        90: static RCSNUM *cvs_addedrev;
        !            91:
        !            92:
1.1       jfb        93: TAILQ_HEAD(, cvs_ignpat)  cvs_ign_pats;
                     94:
                     95:
1.3       jfb        96: static struct cvs_dir*  cvs_file_getdir  (struct cvs_file *, int);
                     97: static void             cvs_file_freedir (struct cvs_dir *);
                     98: static int              cvs_file_sort    (struct cvs_flist *);
                     99: static int              cvs_file_cmp     (const void *, const void *);
                    100:
                    101:
                    102:
1.1       jfb       103: /*
                    104:  * cvs_file_init()
                    105:  *
                    106:  */
                    107:
                    108: int
                    109: cvs_file_init(void)
                    110: {
                    111:        int i;
                    112:        size_t len;
                    113:        char path[MAXPATHLEN], buf[MAXNAMLEN];
                    114:        FILE *ifp;
                    115:        struct passwd *pwd;
                    116:
                    117:        TAILQ_INIT(&cvs_ign_pats);
                    118:
1.4     ! jfb       119:        cvs_addedrev = rcsnum_alloc();
        !           120:        rcsnum_aton("0", NULL, cvs_addedrev);
        !           121:
1.1       jfb       122:        /* standard patterns to ignore */
                    123:        for (i = 0; i < sizeof(cvs_ign_std)/sizeof(char *); i++)
                    124:                cvs_file_ignore(cvs_ign_std[i]);
                    125:
                    126:        /* read the cvsignore file in the user's home directory, if any */
                    127:        pwd = getpwuid(getuid());
                    128:        if (pwd != NULL) {
                    129:                snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
                    130:                ifp = fopen(path, "r");
                    131:                if (ifp == NULL) {
                    132:                        if (errno != ENOENT)
                    133:                                cvs_log(LP_ERRNO, "failed to open `%s'", path);
                    134:                }
                    135:                else {
                    136:                        while (fgets(buf, sizeof(buf), ifp) != NULL) {
                    137:                                len = strlen(buf);
                    138:                                if (len == 0)
                    139:                                        continue;
                    140:                                if (buf[len - 1] != '\n') {
                    141:                                        cvs_log(LP_ERR, "line too long in `%s'",
                    142:                                            path);
                    143:                                }
                    144:                                buf[--len] = '\0';
                    145:                                cvs_file_ignore(buf);
                    146:                        }
                    147:                        (void)fclose(ifp);
                    148:                }
                    149:        }
                    150:
                    151:        return (0);
                    152: }
                    153:
                    154:
                    155: /*
                    156:  * cvs_file_ignore()
                    157:  *
                    158:  * Add the pattern <pat> to the list of patterns for files to ignore.
                    159:  * Returns 0 on success, or -1 on failure.
                    160:  */
                    161:
                    162: int
                    163: cvs_file_ignore(const char *pat)
                    164: {
                    165:        char *cp;
                    166:        struct cvs_ignpat *ip;
                    167:
                    168:        ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
                    169:        if (ip == NULL) {
                    170:                cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
                    171:                return (-1);
                    172:        }
                    173:
                    174:        strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
                    175:
                    176:        /* check if we will need globbing for that pattern */
                    177:        ip->ip_flags = CVS_IGN_STATIC;
                    178:        for (cp = ip->ip_pat; *cp != '\0'; cp++) {
                    179:                if (CVS_CHAR_ISMETA(*cp)) {
                    180:                        ip->ip_flags &= ~CVS_IGN_STATIC;
                    181:                        break;
                    182:                }
                    183:        }
                    184:
                    185:        TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
                    186:
                    187:        return (0);
                    188: }
                    189:
                    190:
                    191: /*
                    192:  * cvs_file_isignored()
                    193:  *
                    194:  * Returns 1 if the filename <file> is matched by one of the ignore
                    195:  * patterns, or 0 otherwise.
                    196:  */
                    197:
                    198: int
                    199: cvs_file_isignored(const char *file)
                    200: {
                    201:        struct cvs_ignpat *ip;
                    202:
                    203:        TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
                    204:                if (ip->ip_flags & CVS_IGN_STATIC) {
                    205:                        if (strcmp(file, ip->ip_pat) == 0)
                    206:                                return (1);
                    207:                }
                    208:                else if (fnmatch(ip->ip_pat, file, FNM_PERIOD) == 0)
                    209:                        return (1);
                    210:        }
                    211:
                    212:        return (0);
                    213: }
                    214:
                    215:
                    216: /*
                    217:  * cvs_file_getv()
                    218:  *
                    219:  * Get a vector of all the files found in the directory <dir> and not
                    220:  * matching any of the ignore patterns.  The number of files found is
                    221:  * returned in <nfiles>.
                    222:  * Returns a pointer to a dynamically-allocated string vector on success,
1.2       jfb       223:  * or NULL on failure.  The returned vector should be freed with
                    224:  * cvs_freeargv().
1.1       jfb       225:  */
                    226:
                    227: char**
1.3       jfb       228: cvs_file_getv(const char *dir, int *nfiles, int recurse)
1.1       jfb       229: {
                    230:        int nf, ret, fd;
                    231:        long base;
                    232:        void *dp, *ep, *tmp;
                    233:        char fbuf[1024], **fvec;
                    234:        struct dirent *ent;
                    235:
                    236:        *nfiles = 0;
                    237:        fvec = NULL;
                    238:
                    239:        fd = open(dir, O_RDONLY);
                    240:        if (fd == -1) {
                    241:                cvs_log(LP_ERRNO, "failed to open `%s'", dir);
                    242:                return (NULL);
                    243:        }
                    244:        ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    245:        if (ret == -1) {
                    246:                cvs_log(LP_ERRNO, "failed to get directory entries");
                    247:                (void)close(fd);
                    248:                return (NULL);
                    249:        }
                    250:
                    251:        dp = fbuf;
                    252:        ep = fbuf + (size_t)ret;
                    253:        while (dp < ep) {
                    254:                ent = (struct dirent *)dp;
                    255:                dp += ent->d_reclen;
                    256:
                    257:                if (cvs_file_isignored(ent->d_name))
                    258:                        continue;
                    259:
                    260:                tmp = realloc(fvec, (*nfiles + 1) * sizeof(char *));
                    261:                if (tmp == NULL) {
                    262:                        cvs_log(LP_ERRNO, "failed to reallocate file vector");
                    263:                        (void)close(fd);
                    264:                        free(fvec);
                    265:                        return (NULL);
                    266:                }
                    267:                fvec[++(*nfiles)] = strdup(ent->d_name);
                    268:
                    269:                *nfiles++;
                    270:        }
                    271:
                    272:        (void)close(fd);
                    273:
                    274:        return (fvec);
1.3       jfb       275: }
                    276:
                    277:
                    278: /*
                    279:  * cvs_file_get()
                    280:  *
                    281:  * Load a cvs_file structure with all the information pertaining to the file
                    282:  * <path>.
1.4     ! jfb       283:  * The <flags> parameter specifies various flags that alter the behaviour of
        !           284:  * the function.  The CF_STAT flag is used to keep stat information of the
        !           285:  * file in the structure after it is used (it is lost otherwise).  The
        !           286:  * CF_RECURSE flag causes the function to recursively load subdirectories
        !           287:  * when <path> is a directory.  The CF_SORT flag causes the files to be
        !           288:  * sorted in alphabetical order upon loading.
        !           289:  * The special case of "." as a path specification generates recursion for
        !           290:  * a single level and is equivalent to calling cvs_file_get() on all files
        !           291:  * of that directory.
1.3       jfb       292:  * Returns a pointer to the cvs file structure, which must later be freed
                    293:  * with cvs_file_free().
                    294:  */
                    295:
                    296: struct cvs_file*
                    297: cvs_file_get(const char *path, int flags)
                    298: {
1.4     ! jfb       299:        int cwd;
1.3       jfb       300:        size_t dlen;
                    301:        struct stat st;
                    302:        struct cvs_file *cfp;
1.4     ! jfb       303:        struct cvs_ent *ent;
1.3       jfb       304:
                    305:        printf("cvs_file_get(%s)\n", path);
                    306:
1.4     ! jfb       307:        if (strcmp(path, ".") == 0)
        !           308:                cwd = 1;
        !           309:        else
        !           310:                cwd = 0;
        !           311:
1.3       jfb       312:        if (stat(path, &st) == -1) {
1.4     ! jfb       313:                cvs_log(LP_ERRNO, "failed to stat %s", path);
1.3       jfb       314:                return (NULL);
                    315:        }
                    316:
                    317:        cfp = (struct cvs_file *)malloc(sizeof(*cfp));
                    318:        if (cfp == NULL) {
                    319:                cvs_log(LP_ERRNO, "failed to allocate CVS file data");
                    320:                return (NULL);
                    321:        }
                    322:        memset(cfp, 0, sizeof(*cfp));
                    323:
1.4     ! jfb       324:        ent = cvs_ent_getent(path);
        !           325:        if (ent == NULL)
        !           326:                cfp->cf_cvstat = (cwd == 1) ?
        !           327:                    CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
        !           328:        else {
        !           329:                if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
        !           330:                        cfp->cf_cvstat = CVS_FST_ADDED;
        !           331:                else
        !           332:                        cfp->cf_cvstat = CVS_FST_UPTODATE;
        !           333:
        !           334:                cvs_ent_free(ent);
        !           335:        }
        !           336:
1.3       jfb       337:        cfp->cf_path = strdup(path);
                    338:        if (cfp->cf_path == NULL) {
1.4     ! jfb       339:                cvs_log(LP_ERRNO, "failed to allocate file path");
1.3       jfb       340:                free(cfp);
                    341:                return (NULL);
                    342:        }
                    343:
                    344:        cfp->cf_name = strrchr(cfp->cf_path, '/');
                    345:        if (cfp->cf_name == NULL)
                    346:                cfp->cf_name = cfp->cf_path;
                    347:        else
                    348:                cfp->cf_name++;
                    349:
                    350:        /* convert from stat mode to dirent values */
                    351:        cfp->cf_type = IFTODT(st.st_mode);
1.4     ! jfb       352:        if ((cfp->cf_type == DT_DIR) && ((flags & CF_RECURSE) || cwd)) {
        !           353:                if ((flags & CF_KNOWN) && (cfp->cf_cvstat == CVS_FST_UNKNOWN))
        !           354:                        cfp->cf_ddat = NULL;
        !           355:                else {
        !           356:                        cfp->cf_ddat = cvs_file_getdir(cfp, flags);
        !           357:                        if (cfp->cf_ddat == NULL) {
        !           358:                                cvs_file_free(cfp);
        !           359:                                return (NULL);
        !           360:                        }
1.3       jfb       361:                }
                    362:        }
                    363:
                    364:        if (flags & CF_STAT) {
                    365:                cfp->cf_stat = (struct stat *)malloc(sizeof(struct stat));
                    366:                if (cfp->cf_stat == NULL) {
                    367:                        cvs_log(LP_ERRNO, "failed to allocate stat structure");
                    368:                        cvs_file_free(cfp);
                    369:                        return (NULL);
                    370:                }
                    371:
                    372:                memcpy(cfp->cf_stat, &st, sizeof(struct stat));
                    373:        }
                    374:
                    375:        return (cfp);
                    376: }
                    377:
                    378:
                    379: /*
                    380:  * cvs_file_getdir()
                    381:  *
                    382:  * Get a cvs directory structure for the directory whose path is <dir>.
                    383:  */
                    384:
                    385: static struct cvs_dir*
                    386: cvs_file_getdir(struct cvs_file *cf, int flags)
                    387: {
                    388:        int nf, ret, fd;
                    389:        long base;
                    390:        void *dp, *ep, *tmp;
                    391:        char fbuf[1024], pbuf[MAXPATHLEN];
                    392:        struct dirent *ent;
                    393:        struct cvs_file *cfp;
                    394:        struct cvs_dir *cdp;
                    395:
                    396:        cdp = (struct cvs_dir *)malloc(sizeof(*cdp));
                    397:        if (cdp == NULL) {
                    398:                cvs_log(LP_ERRNO, "failed to allocate dir");
                    399:                return (NULL);
                    400:        }
                    401:        memset(cdp, 0, sizeof(*cdp));
                    402:        LIST_INIT(&(cdp->cd_files));
                    403:
1.4     ! jfb       404:        if (cvs_readrepo(cf->cf_path, pbuf, sizeof(pbuf)) == 0) {
        !           405:                cdp->cd_repo = strdup(pbuf);
        !           406:                if (cdp->cd_repo == NULL) {
        !           407:                        free(cdp);
        !           408:                        return (NULL);
        !           409:                }
1.3       jfb       410:        }
                    411:
                    412:        cdp->cd_root = cvsroot_get(cf->cf_path);
                    413:        if (cdp->cd_root == NULL) {
                    414:                cvs_file_freedir(cdp);
                    415:                return (NULL);
                    416:        }
                    417:
                    418:        fd = open(cf->cf_path, O_RDONLY);
                    419:        if (fd == -1) {
                    420:                cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
                    421:                cvs_file_freedir(cdp);
                    422:                return (NULL);
                    423:        }
                    424:        ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
                    425:        if (ret == -1) {
                    426:                cvs_log(LP_ERRNO, "failed to get directory entries");
                    427:                (void)close(fd);
                    428:                cvs_file_freedir(cdp);
                    429:                return (NULL);
                    430:        }
                    431:
                    432:        dp = fbuf;
                    433:        ep = fbuf + (size_t)ret;
                    434:        while (dp < ep) {
                    435:                ent = (struct dirent *)dp;
                    436:                dp += ent->d_reclen;
                    437:
                    438:                if ((flags & CF_IGNORE) && cvs_file_isignored(ent->d_name))
                    439:                        continue;
                    440:
                    441:                snprintf(pbuf, sizeof(pbuf), "%s/%s", cf->cf_path, ent->d_name);
                    442:                cfp = cvs_file_get(pbuf, flags);
1.4     ! jfb       443:                if (cfp != NULL) {
        !           444:                        cfp->cf_parent = cf;
        !           445:                        LIST_INSERT_HEAD(&(cdp->cd_files), cfp, cf_list);
        !           446:                }
1.3       jfb       447:        }
                    448:
                    449:        if (flags & CF_SORT)
                    450:                cvs_file_sort(&(cdp->cd_files));
                    451:
                    452:        (void)close(fd);
                    453:
                    454:        return (cdp);
                    455: }
                    456:
                    457:
                    458: /*
                    459:  * cvs_file_free()
                    460:  *
                    461:  * Free a cvs_file structure and its contents.
                    462:  */
                    463:
                    464: void
                    465: cvs_file_free(struct cvs_file *cf)
                    466: {
                    467:        struct cvs_file *cfp;
                    468:        struct cvs_dir *cd;
                    469:
                    470:        if (cf->cf_path != NULL)
                    471:                free(cf->cf_path);
                    472:        if (cf->cf_stat != NULL)
                    473:                free(cf->cf_stat);
                    474:        if (cf->cf_ddat != NULL)
                    475:                cvs_file_freedir(cf->cf_ddat);
                    476:        free(cf);
                    477: }
                    478:
                    479:
                    480: /*
                    481:  * cvs_file_freedir()
                    482:  *
                    483:  * Free a cvs_dir structure and its contents.
                    484:  */
                    485:
                    486: static void
                    487: cvs_file_freedir(struct cvs_dir *cd)
                    488: {
                    489:        struct cvs_file *cfp;
                    490:
                    491:        if (cd->cd_root != NULL)
                    492:                cvsroot_free(cd->cd_root);
                    493:        if (cd->cd_repo != NULL)
                    494:                free(cd->cd_repo);
                    495:
                    496:        while (!LIST_EMPTY(&(cd->cd_files))) {
                    497:                cfp = LIST_FIRST(&(cd->cd_files));
                    498:                LIST_REMOVE(cfp, cf_list);
                    499:                cvs_file_free(cfp);
                    500:        }
                    501: }
                    502:
                    503:
                    504: /*
                    505:  * cvs_file_sort()
                    506:  *
                    507:  * Sort a list of cvs file structures according to their filename.
                    508:  */
                    509:
                    510: static int
                    511: cvs_file_sort(struct cvs_flist *flp)
                    512: {
                    513:        int i;
                    514:        size_t nb;
                    515:        struct cvs_file *cf, *cfvec[256];
                    516:
                    517:        i = 0;
                    518:        LIST_FOREACH(cf, flp, cf_list) {
                    519:                cfvec[i++] = cf;
                    520:                if (i == sizeof(cfvec)/sizeof(struct cvs_file *)) {
                    521:                        cvs_log(LP_WARN, "too many files to sort");
                    522:                        return (-1);
                    523:                }
                    524:
                    525:                /* now unlink it from the list,
                    526:                 * we'll put it back in order later
                    527:                 */
                    528:                LIST_REMOVE(cf, cf_list);
                    529:        }
                    530:
                    531:        /* clear the list just in case */
                    532:        LIST_INIT(flp);
                    533:        nb = (size_t)i;
                    534:
                    535:        heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
                    536:
                    537:        /* rebuild the list from the bottom up */
                    538:        for (i = (int)nb - 1; i >= 0; i--)
                    539:                LIST_INSERT_HEAD(flp, cfvec[i], cf_list);
                    540:
                    541:        return (0);
                    542: }
                    543:
                    544:
                    545: static int
                    546: cvs_file_cmp(const void *f1, const void *f2)
                    547: {
                    548:        struct cvs_file *cf1, *cf2;
                    549:        cf1 = *(struct cvs_file **)f1;
                    550:        cf2 = *(struct cvs_file **)f2;
                    551:        return strcmp(cf1->cf_name, cf2->cf_name);
1.1       jfb       552: }