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

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