[BACK]Return to catman.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/catman.c, Revision 1.6

1.6     ! kristaps    1: /*     $Id: catman.c,v 1.5 2011/12/12 02:00:49 schwarze Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
                     21: #include <sys/param.h>
                     22: #include <sys/stat.h>
                     23: #include <sys/wait.h>
                     24:
                     25: #include <assert.h>
                     26: #include <errno.h>
                     27: #include <fcntl.h>
                     28: #include <getopt.h>
                     29: #include <stdio.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <unistd.h>
                     33:
                     34: #ifdef __linux__
                     35: # include <db_185.h>
                     36: #else
                     37: # include <db.h>
                     38: #endif
                     39:
                     40: #include "manpath.h"
                     41:
                     42: #define        xstrlcpy(_dst, _src, _sz) \
                     43:        do if (strlcpy((_dst), (_src), (_sz)) >= (_sz)) { \
                     44:                fprintf(stderr, "%s: Path too long", (_dst)); \
                     45:                exit(EXIT_FAILURE); \
                     46:        } while (/* CONSTCOND */0)
                     47:
                     48: #define        xstrlcat(_dst, _src, _sz) \
                     49:        do if (strlcat((_dst), (_src), (_sz)) >= (_sz)) { \
                     50:                fprintf(stderr, "%s: Path too long", (_dst)); \
                     51:                exit(EXIT_FAILURE); \
                     52:        } while (/* CONSTCOND */0)
                     53:
1.6     ! kristaps   54: static int              indexhtml(char *, size_t, char *, size_t);
1.4       kristaps   55: static int              manup(const struct manpaths *, char *);
1.1       kristaps   56: static int              mkpath(char *, mode_t, mode_t);
1.6     ! kristaps   57: static int              treecpy(char *, char *);
        !            58: static int              update(char *, char *);
1.1       kristaps   59: static void             usage(void);
                     60:
                     61: static const char      *progname;
                     62: static int              verbose;
                     63: static int              force;
                     64:
                     65: int
                     66: main(int argc, char *argv[])
                     67: {
                     68:        int              ch;
                     69:        char            *aux, *base;
                     70:        struct manpaths  dirs;
1.4       kristaps   71:        char             buf[MAXPATHLEN];
1.1       kristaps   72:        extern char     *optarg;
                     73:        extern int       optind;
                     74:
                     75:        progname = strrchr(argv[0], '/');
                     76:        if (progname == NULL)
                     77:                progname = argv[0];
                     78:        else
                     79:                ++progname;
                     80:
                     81:        aux = base = NULL;
1.4       kristaps   82:        xstrlcpy(buf, "/var/www/cache/man.cgi", MAXPATHLEN);
1.1       kristaps   83:
                     84:        while (-1 != (ch = getopt(argc, argv, "fm:M:o:v")))
                     85:                switch (ch) {
                     86:                case ('f'):
                     87:                        force = 1;
                     88:                        break;
                     89:                case ('m'):
                     90:                        aux = optarg;
                     91:                        break;
                     92:                case ('M'):
                     93:                        base = optarg;
                     94:                        break;
                     95:                case ('o'):
1.4       kristaps   96:                        xstrlcpy(buf, optarg, MAXPATHLEN);
1.1       kristaps   97:                        break;
                     98:                case ('v'):
                     99:                        verbose++;
                    100:                        break;
                    101:                default:
                    102:                        usage();
                    103:                        return(EXIT_FAILURE);
                    104:                }
                    105:
                    106:        argc -= optind;
                    107:        argv += optind;
                    108:
                    109:        if (argc > 0) {
                    110:                usage();
                    111:                return(EXIT_FAILURE);
                    112:        }
                    113:
                    114:        memset(&dirs, 0, sizeof(struct manpaths));
1.5       schwarze  115:        manpath_parse(&dirs, NULL, base, aux);
1.4       kristaps  116:        ch = manup(&dirs, buf);
1.1       kristaps  117:        manpath_free(&dirs);
                    118:        return(ch ? EXIT_SUCCESS : EXIT_FAILURE);
                    119: }
                    120:
                    121: static void
                    122: usage(void)
                    123: {
                    124:
                    125:        fprintf(stderr, "usage: %s "
                    126:                        "[-fv] "
                    127:                        "[-o path] "
                    128:                        "[-m manpath] "
                    129:                        "[-M manpath]\n",
                    130:                        progname);
                    131: }
                    132:
                    133: /*
                    134:  * If "src" file doesn't exist (errors out), return -1.  Otherwise,
                    135:  * return 1 if "src" is newer (which also happens "dst" doesn't exist)
                    136:  * and 0 otherwise.
                    137:  */
                    138: static int
                    139: isnewer(const char *dst, const char *src)
                    140: {
                    141:        struct stat      s1, s2;
                    142:
                    143:        if (-1 == stat(src, &s1))
                    144:                return(-1);
                    145:        if (force)
                    146:                return(1);
                    147:
                    148:        return(-1 == stat(dst, &s2) ? 1 : s1.st_mtime > s2.st_mtime);
                    149: }
                    150:
                    151: /*
                    152:  * Copy the contents of one file into another.
                    153:  * Returns 0 on failure, 1 on success.
                    154:  */
                    155: static int
                    156: filecpy(const char *dst, const char *src)
                    157: {
                    158:        char             buf[BUFSIZ];
                    159:        int              sfd, dfd, rc;
                    160:        ssize_t          rsz, wsz;
                    161:
                    162:        sfd = dfd = -1;
                    163:        rc = 0;
                    164:
                    165:        if (-1 == (dfd = open(dst, O_CREAT|O_TRUNC|O_WRONLY, 0644))) {
                    166:                perror(dst);
                    167:                goto out;
                    168:        } else if (-1 == (sfd = open(src, O_RDONLY, 0))) {
                    169:                perror(src);
                    170:                goto out;
                    171:        }
                    172:
                    173:        while ((rsz = read(sfd, buf, BUFSIZ)) > 0)
                    174:                if (-1 == (wsz = write(dfd, buf, (size_t)rsz))) {
                    175:                        perror(dst);
                    176:                        goto out;
                    177:                } else if (wsz < rsz) {
                    178:                        fprintf(stderr, "%s: Short write\n", dst);
                    179:                        goto out;
                    180:                }
                    181:
                    182:        if (rsz < 0)
                    183:                perror(src);
                    184:        else
                    185:                rc = 1;
                    186: out:
                    187:        if (-1 != sfd)
                    188:                close(sfd);
                    189:        if (-1 != dfd)
                    190:                close(dfd);
                    191:
                    192:        return(rc);
                    193: }
                    194:
                    195: /*
                    196:  * Pass over the recno database and re-create HTML pages if they're
                    197:  * found to be out of date.
                    198:  * Returns -1 on fatal error, 1 on success.
                    199:  */
                    200: static int
1.6     ! kristaps  201: indexhtml(char *src, size_t ssz, char *dst, size_t dsz)
1.1       kristaps  202: {
1.3       kristaps  203:        DB              *idx;
1.1       kristaps  204:        DBT              key, val;
                    205:        int              c, rc;
                    206:        unsigned int     fl;
1.3       kristaps  207:        const char      *f, *cp;
1.1       kristaps  208:        char            *d;
                    209:        char             fname[MAXPATHLEN];
                    210:        pid_t            pid;
                    211:
                    212:        pid = -1;
                    213:
                    214:        xstrlcpy(fname, dst, MAXPATHLEN);
                    215:        xstrlcat(fname, "/mandoc.index", MAXPATHLEN);
                    216:
1.3       kristaps  217:        idx = dbopen(fname, O_RDONLY, 0, DB_RECNO, NULL);
                    218:        if (NULL == idx) {
1.1       kristaps  219:                perror(fname);
                    220:                return(-1);
                    221:        }
                    222:
                    223:        fl = R_FIRST;
1.3       kristaps  224:        while (0 == (c = (*idx->seq)(idx, &key, &val, fl))) {
1.1       kristaps  225:                fl = R_NEXT;
1.3       kristaps  226:                cp = (const char *)val.data;
                    227:                if (0 == val.size)
                    228:                        continue;
                    229:                if (NULL == (f = memchr(cp, '\0', val.size)))
                    230:                        break;
                    231:                if (++f - cp >= (int)val.size)
                    232:                        break;
                    233:                if (NULL == memchr(f, '\0', val.size - (f - cp)))
                    234:                        break;
1.1       kristaps  235:
1.6     ! kristaps  236:                src[(int)ssz] = dst[(int)dsz] = '\0';
        !           237:
        !           238:                xstrlcat(dst, "/", MAXPATHLEN);
        !           239:                xstrlcat(dst, f, MAXPATHLEN);
1.1       kristaps  240:
1.6     ! kristaps  241:                xstrlcat(src, "/", MAXPATHLEN);
        !           242:                xstrlcat(src, f, MAXPATHLEN);
1.1       kristaps  243:
1.6     ! kristaps  244:                if (-1 == (rc = isnewer(dst, src))) {
1.3       kristaps  245:                        fprintf(stderr, "%s: File missing\n", f);
1.1       kristaps  246:                        break;
                    247:                } else if (0 == rc)
                    248:                        continue;
                    249:
1.6     ! kristaps  250:                d = strrchr(dst, '/');
1.1       kristaps  251:                assert(NULL != d);
                    252:                *d = '\0';
                    253:
1.6     ! kristaps  254:                if (-1 == mkpath(dst, 0755, 0755)) {
        !           255:                        perror(dst);
1.1       kristaps  256:                        break;
                    257:                }
                    258:
                    259:                *d = '/';
1.2       kristaps  260:
1.6     ! kristaps  261:                if ( ! filecpy(dst, src))
1.3       kristaps  262:                        break;
1.1       kristaps  263:                if (verbose)
1.6     ! kristaps  264:                        printf("%s\n", dst);
1.1       kristaps  265:        }
                    266:
1.3       kristaps  267:        (*idx->close)(idx);
1.1       kristaps  268:
                    269:        if (c < 0)
                    270:                perror(fname);
1.3       kristaps  271:        else if (0 == c)
                    272:                fprintf(stderr, "%s: Corrupt index\n", fname);
1.1       kristaps  273:
                    274:        return(1 == c ? 1 : -1);
                    275: }
                    276:
                    277: /*
                    278:  * Copy both recno and btree databases into the destination.
                    279:  * Call in to begin recreating HTML files.
                    280:  * Return -1 on fatal error and 1 if the update went well.
                    281:  */
                    282: static int
1.6     ! kristaps  283: update(char *dst, char *src)
1.1       kristaps  284: {
                    285:        size_t           dsz, ssz;
                    286:
                    287:        dsz = strlen(dst);
                    288:        ssz = strlen(src);
                    289:
                    290:        xstrlcat(src, "/mandoc.db", MAXPATHLEN);
                    291:        xstrlcat(dst, "/mandoc.db", MAXPATHLEN);
                    292:
                    293:        if ( ! filecpy(dst, src))
                    294:                return(-1);
                    295:        if (verbose)
                    296:                printf("%s\n", dst);
                    297:
                    298:        dst[(int)dsz] = src[(int)ssz] = '\0';
                    299:
                    300:        xstrlcat(src, "/mandoc.index", MAXPATHLEN);
                    301:        xstrlcat(dst, "/mandoc.index", MAXPATHLEN);
                    302:
                    303:        if ( ! filecpy(dst, src))
                    304:                return(-1);
                    305:        if (verbose)
                    306:                printf("%s\n", dst);
                    307:
1.6     ! kristaps  308:        dst[(int)dsz] = src[(int)ssz] = '\0';
1.1       kristaps  309:
1.6     ! kristaps  310:        return(indexhtml(src, ssz, dst, dsz));
1.1       kristaps  311: }
                    312:
                    313: /*
                    314:  * See if btree or recno databases in the destination are out of date
                    315:  * with respect to a single manpath component.
                    316:  * Return -1 on fatal error, 0 if the source is no longer valid (and
                    317:  * shouldn't be listed), and 1 if the update went well.
                    318:  */
                    319: static int
1.6     ! kristaps  320: treecpy(char *dst, char *src)
1.1       kristaps  321: {
                    322:        size_t           dsz, ssz;
                    323:        int              rc;
                    324:
                    325:        dsz = strlen(dst);
                    326:        ssz = strlen(src);
                    327:
                    328:        xstrlcat(src, "/mandoc.index", MAXPATHLEN);
                    329:        xstrlcat(dst, "/mandoc.index", MAXPATHLEN);
                    330:
                    331:        if (-1 == (rc = isnewer(dst, src)))
                    332:                return(0);
                    333:
                    334:        dst[(int)dsz] = src[(int)ssz] = '\0';
                    335:
                    336:        if (1 == rc)
1.6     ! kristaps  337:                return(update(dst, src));
1.1       kristaps  338:
                    339:        xstrlcat(src, "/mandoc.db", MAXPATHLEN);
                    340:        xstrlcat(dst, "/mandoc.db", MAXPATHLEN);
                    341:
                    342:        if (-1 == (rc = isnewer(dst, src)))
                    343:                return(0);
                    344:        else if (rc == 0)
                    345:                return(1);
                    346:
                    347:        dst[(int)dsz] = src[(int)ssz] = '\0';
                    348:
1.6     ! kristaps  349:        return(update(dst, src));
1.1       kristaps  350: }
                    351:
                    352: /*
                    353:  * Update the destination's file-tree with respect to changes in the
                    354:  * source manpath components.
                    355:  * "Change" is defined by an updated index or btree database.
                    356:  * Returns 1 on success, 0 on failure.
                    357:  */
                    358: static int
1.4       kristaps  359: manup(const struct manpaths *dirs, char *base)
1.1       kristaps  360: {
                    361:        char             dst[MAXPATHLEN],
                    362:                         src[MAXPATHLEN];
                    363:        const char      *path;
                    364:        int              i, c;
                    365:        size_t           sz;
                    366:        FILE            *f;
                    367:
1.4       kristaps  368:        /* Create the path and file for the catman.conf file. */
                    369:
                    370:        sz = strlen(base);
                    371:        xstrlcpy(dst, base, MAXPATHLEN);
1.1       kristaps  372:        xstrlcat(dst, "/etc", MAXPATHLEN);
                    373:        if (-1 == mkpath(dst, 0755, 0755)) {
                    374:                perror(dst);
                    375:                return(0);
                    376:        }
                    377:
1.3       kristaps  378:        xstrlcat(dst, "/catman.conf", MAXPATHLEN);
1.1       kristaps  379:        if (NULL == (f = fopen(dst, "w"))) {
                    380:                perror(dst);
                    381:                return(0);
1.4       kristaps  382:        } else if (verbose)
                    383:                printf("%s\n", dst);
1.1       kristaps  384:
                    385:        for (i = 0; i < dirs->sz; i++) {
                    386:                path = dirs->paths[i];
1.6     ! kristaps  387:                dst[(int)sz] = '\0';
1.1       kristaps  388:                xstrlcat(dst, path, MAXPATHLEN);
                    389:                if (-1 == mkpath(dst, 0755, 0755)) {
                    390:                        perror(dst);
                    391:                        break;
                    392:                }
                    393:
                    394:                xstrlcpy(src, path, MAXPATHLEN);
1.6     ! kristaps  395:                if (-1 == (c = treecpy(dst, src)))
1.1       kristaps  396:                        break;
                    397:                else if (0 == c)
                    398:                        continue;
                    399:
                    400:                /*
                    401:                 * We want to use a relative path here because manpath.h
                    402:                 * will realpath() when invoked with man.cgi, and we'll
                    403:                 * make sure to chdir() into the cache directory before.
                    404:                 *
                    405:                 * This allows the cache directory to be in an arbitrary
                    406:                 * place, working in both chroot() and non-chroot()
                    407:                 * "safe" modes.
                    408:                 */
                    409:                assert('/' == path[0]);
                    410:                fprintf(f, "_whatdb %s/whatis.db\n", path + 1);
                    411:        }
                    412:
                    413:        fclose(f);
                    414:        return(i == dirs->sz);
                    415: }
                    416:
                    417: /*
                    418:  * Copyright (c) 1983, 1992, 1993
                    419:  *     The Regents of the University of California.  All rights reserved.
                    420:  *
                    421:  * Redistribution and use in source and binary forms, with or without
                    422:  * modification, are permitted provided that the following conditions
                    423:  * are met:
                    424:  * 1. Redistributions of source code must retain the above copyright
                    425:  *    notice, this list of conditions and the following disclaimer.
                    426:  * 2. Redistributions in binary form must reproduce the above copyright
                    427:  *    notice, this list of conditions and the following disclaimer in the
                    428:  *    documentation and/or other materials provided with the distribution.
                    429:  * 3. Neither the name of the University nor the names of its contributors
                    430:  *    may be used to endorse or promote products derived from this software
                    431:  *    without specific prior written permission.
                    432:  *
                    433:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                    434:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                    435:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                    436:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                    437:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                    438:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                    439:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                    440:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                    441:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                    442:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                    443:  * SUCH DAMAGE.
                    444:  */
                    445: static int
                    446: mkpath(char *path, mode_t mode, mode_t dir_mode)
                    447: {
                    448:        struct stat sb;
                    449:        char *slash;
                    450:        int done, exists;
                    451:
                    452:        slash = path;
                    453:
                    454:        for (;;) {
                    455:                /* LINTED */
                    456:                slash += strspn(slash, "/");
                    457:                /* LINTED */
                    458:                slash += strcspn(slash, "/");
                    459:
                    460:                done = (*slash == '\0');
                    461:                *slash = '\0';
                    462:
                    463:                /* skip existing path components */
                    464:                exists = !stat(path, &sb);
                    465:                if (!done && exists && S_ISDIR(sb.st_mode)) {
                    466:                        *slash = '/';
                    467:                        continue;
                    468:                }
                    469:
                    470:                if (mkdir(path, done ? mode : dir_mode) == 0) {
                    471:                        if (mode > 0777 && chmod(path, mode) < 0)
                    472:                                return (-1);
                    473:                } else {
                    474:                        if (!exists) {
                    475:                                /* Not there */
                    476:                                return (-1);
                    477:                        }
                    478:                        if (!S_ISDIR(sb.st_mode)) {
                    479:                                /* Is there, but isn't a directory */
                    480:                                errno = ENOTDIR;
                    481:                                return (-1);
                    482:                        }
                    483:                }
                    484:
                    485:                if (done)
                    486:                        break;
                    487:
                    488:                *slash = '/';
                    489:        }
                    490:
                    491:        return (0);
                    492: }

CVSweb