[BACK]Return to util.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/util.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/stat.h>
                     29:
                     30: #include <md5.h>
                     31: #include <errno.h>
1.2       jfb        32: #include <fcntl.h>
1.1       jfb        33: #include <stdio.h>
                     34: #include <stdlib.h>
                     35: #include <unistd.h>
                     36: #include <string.h>
                     37:
                     38: #include "cvs.h"
                     39: #include "log.h"
1.5       jfb        40: #include "file.h"
1.1       jfb        41:
                     42:
                     43: /* letter -> mode type map */
                     44: static const int cvs_modetypes[26] = {
                     45:        -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1,
                     46:        -1,  2, -1, -1, -1, -1, -1,  0, -1, -1, -1, -1, -1,
                     47: };
                     48:
                     49: /* letter -> mode map */
                     50: static const mode_t cvs_modes[3][26] = {
                     51:        {
                     52:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     53:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     54:                0,  0,       0,       S_IRUSR, 0,  0,  0,    /* n - u */
                     55:                0,  S_IWUSR, S_IXUSR, 0,       0             /* v - z */
                     56:        },
                     57:        {
                     58:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     59:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     60:                0,  0,       0,       S_IRGRP, 0,  0,  0,    /* n - u */
                     61:                0,  S_IWGRP, S_IXGRP, 0,       0             /* v - z */
                     62:        },
                     63:        {
                     64:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     65:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     66:                0,  0,       0,       S_IROTH, 0,  0,  0,    /* n - u */
                     67:                0,  S_IWOTH, S_IXOTH, 0,       0             /* v - z */
                     68:        }
                     69: };
                     70:
                     71:
                     72: /* octal -> string */
                     73: static const char *cvs_modestr[8] = {
                     74:        "", "x", "w", "wx", "r", "rx", "rw", "rwx"
                     75: };
                     76:
                     77:
                     78:
                     79:
                     80: /*
                     81:  * cvs_readrepo()
                     82:  *
                     83:  * Read the path stored in the `Repository' CVS file for a given directory
                     84:  * <dir>, and store that path into the buffer pointed to by <dst>, whose size
                     85:  * is <len>.
                     86:  */
                     87:
                     88: int
                     89: cvs_readrepo(const char *dir, char *dst, size_t len)
                     90: {
                     91:        size_t dlen;
                     92:        FILE *fp;
                     93:        char repo_path[MAXPATHLEN];
                     94:
                     95:        snprintf(repo_path, sizeof(repo_path), "%s/CVS/Repository", dir);
                     96:        fp = fopen(repo_path, "r");
                     97:        if (fp == NULL) {
                     98:                return (-1);
                     99:        }
                    100:
                    101:        if (fgets(dst, (int)len, fp) == NULL) {
                    102:                if (ferror(fp)) {
                    103:                        cvs_log(LP_ERRNO, "failed to read from `%s'",
                    104:                            repo_path);
                    105:                }
                    106:                (void)fclose(fp);
                    107:                return (-1);
                    108:        }
                    109:        dlen = strlen(dst);
                    110:        if ((dlen > 0) && (dst[dlen - 1] == '\n'))
                    111:                dst[--dlen] = '\0';
                    112:
                    113:        (void)fclose(fp);
                    114:        return (0);
                    115: }
                    116:
                    117:
                    118: /*
                    119:  * cvs_strtomode()
                    120:  *
                    121:  * Read the contents of the string <str> and generate a permission mode from
                    122:  * the contents of <str>, which is assumed to have the mode format of CVS.
                    123:  * The CVS protocol specification states that any modes or mode types that are
                    124:  * not recognized should be silently ignored.  This function does not return
                    125:  * an error in such cases, but will issue warnings.
                    126:  * Returns 0 on success, or -1 on failure.
                    127:  */
                    128:
                    129: int
                    130: cvs_strtomode(const char *str, mode_t *mode)
                    131: {
1.2       jfb       132:        char type;
1.1       jfb       133:        mode_t m;
                    134:        char buf[32], ms[4], *sp, *ep;
                    135:
                    136:        m = 0;
                    137:        strlcpy(buf, str, sizeof(buf));
                    138:        sp = buf;
                    139:        ep = sp;
                    140:
                    141:        for (sp = buf; ep != NULL; sp = ep + 1) {
                    142:                ep = strchr(sp, ',');
                    143:                if (ep != NULL)
1.2       jfb       144:                        *ep = '\0';
1.1       jfb       145:
1.2       jfb       146:                if (sscanf(sp, "%c=%3s", &type, ms) != 2) {
1.1       jfb       147:                        cvs_log(LP_WARN, "failed to scan mode string `%s'", sp);
                    148:                        continue;
                    149:                }
                    150:
                    151:                if ((type <= 'a') || (type >= 'z') ||
                    152:                    (cvs_modetypes[type - 'a'] == -1)) {
                    153:                        cvs_log(LP_WARN,
                    154:                            "invalid mode type `%c'"
                    155:                            " (`u', `g' or `o' expected), ignoring", type);
                    156:                        continue;
                    157:                }
                    158:
                    159:                /* make type contain the actual mode index */
                    160:                type = cvs_modetypes[type - 'a'];
                    161:
                    162:                for (sp = ms; *sp != '\0'; sp++) {
                    163:                        if ((*sp <= 'a') || (*sp >= 'z') ||
1.5       jfb       164:                            (cvs_modes[(int)type][*sp - 'a'] == 0)) {
1.1       jfb       165:                                cvs_log(LP_WARN,
                    166:                                    "invalid permission bit `%c'", *sp);
                    167:                        }
                    168:                        else
1.5       jfb       169:                                m |= cvs_modes[(int)type][*sp - 'a'];
1.1       jfb       170:                }
                    171:        }
                    172:
                    173:        *mode = m;
                    174:
                    175:        return (0);
                    176: }
                    177:
                    178:
                    179: /*
                    180:  * cvs_modetostr()
                    181:  *
                    182:  * Returns 0 on success, or -1 on failure.
                    183:  */
                    184:
                    185: int
                    186: cvs_modetostr(mode_t mode, char *buf, size_t len)
                    187: {
                    188:        size_t l;
                    189:        char tmp[16], *bp;
                    190:        mode_t um, gm, om;
                    191:
                    192:        um = (mode & S_IRWXU) >> 6;
                    193:        gm = (mode & S_IRWXG) >> 3;
                    194:        om = mode & S_IRWXO;
                    195:
                    196:        bp = buf;
                    197:        *bp = '\0';
                    198:        l = 0;
                    199:
                    200:        if (um) {
                    201:                snprintf(tmp, sizeof(tmp), "u=%s", cvs_modestr[um]);
                    202:                l = strlcat(buf, tmp, len);
                    203:        }
                    204:        if (gm) {
                    205:                if (um)
                    206:                        strlcat(buf, ",", len);
                    207:                snprintf(tmp, sizeof(tmp), "g=%s", cvs_modestr[gm]);
                    208:                strlcat(buf, tmp, len);
                    209:        }
                    210:        if (om) {
                    211:                if (um || gm)
                    212:                        strlcat(buf, ",", len);
                    213:                snprintf(tmp, sizeof(tmp), "o=%s", cvs_modestr[gm]);
                    214:                strlcat(buf, tmp, len);
                    215:        }
                    216:
                    217:        return (0);
                    218: }
                    219:
                    220:
                    221: /*
                    222:  * cvs_cksum()
                    223:  *
                    224:  * Calculate the MD5 checksum of the file whose path is <file> and generate
                    225:  * a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
                    226:  * given in <len> and must be at least 33.
                    227:  * Returns 0 on success, or -1 on failure.
                    228:  */
                    229:
                    230: int
                    231: cvs_cksum(const char *file, char *dst, size_t len)
                    232: {
                    233:        if (len < CVS_CKSUM_LEN) {
                    234:                cvs_log(LP_WARN, "buffer too small for checksum");
                    235:                return (-1);
                    236:        }
                    237:        if (MD5File(file, dst) == NULL) {
                    238:                cvs_log(LP_ERRNO, "failed to generate file checksum");
                    239:                return (-1);
                    240:        }
                    241:
                    242:        return (0);
                    243: }
                    244:
                    245:
                    246: /*
                    247:  * cvs_splitpath()
                    248:  *
                    249:  * Split a path <path> into the directory portion and the filename portion
                    250:  * and copy them in <dir> and <file>, whose lengths are <dlen> and <flen>,
                    251:  * unless they are NULL.
                    252:  * Returns 0 on success, or -1 on failure.
                    253:  */
                    254:
                    255: int
                    256: cvs_splitpath(const char *path, char *dir, size_t dlen, char *file, size_t flen)
                    257: {
                    258:        size_t rlen;
                    259:        const char *sp;
                    260:        struct stat st;
                    261:
1.6     ! jfb       262:        rlen = strlen(path);
        !           263:        while ((rlen > 0) && (path[rlen - 1] == '/'))
        !           264:                path[--rlen] = '\0';
        !           265:
1.1       jfb       266:        sp = strrchr(path, '/');
                    267:        if (sp == NULL) {
                    268:                if (stat(path, &st) == -1)
                    269:                        return (-1);
                    270:
                    271:                if (S_ISDIR(st.st_mode)) {
                    272:                        if (dir != NULL)
                    273:                                strlcpy(dir, path, dlen);
                    274:                        if (file != NULL)
                    275:                                file[0] = '\0';
                    276:                }
                    277:                else {
                    278:                        if (file != NULL)
                    279:                                strlcpy(file, path, flen);
                    280:                        if (dir != NULL)
                    281:                                strlcpy(dir, ".", dlen);
                    282:                }
                    283:        }
                    284:        else {
1.4       jfb       285:                rlen = MIN(dlen - 1, (size_t)(sp - path));
1.1       jfb       286:                if (dir != NULL) {
                    287:                        strncpy(dir, path, rlen);
                    288:                        dir[rlen] = '\0';
                    289:                }
                    290:
                    291:                sp++;
                    292:                if (file != NULL)
                    293:                        strlcpy(file, sp, flen);
                    294:        }
                    295:
                    296:        return (0);
                    297: }
                    298:
                    299:
                    300: /*
                    301:  * cvs_getargv()
                    302:  *
                    303:  * Parse a line contained in <line> and generate an argument vector by
                    304:  * splitting the line on spaces and tabs.  The resulting vector is stored in
                    305:  * <argv>, which can accept up to <argvlen> entries.
                    306:  * Returns the number of arguments in the vector, or -1 if an error occured.
                    307:  */
                    308:
                    309: int
                    310: cvs_getargv(const char *line, char **argv, int argvlen)
                    311: {
                    312:        u_int i;
                    313:        int argc, err;
                    314:        char linebuf[256], qbuf[128], *lp, *cp, *arg;
                    315:
                    316:        strlcpy(linebuf, line, sizeof(linebuf));
                    317:        memset(argv, 0, sizeof(argv));
                    318:        argc = 0;
                    319:
                    320:        /* build the argument vector */
                    321:        err = 0;
                    322:        for (lp = linebuf; lp != NULL;) {
                    323:                if (*lp == '"') {
                    324:                        /* double-quoted string */
                    325:                        lp++;
                    326:                        i = 0;
                    327:                        memset(qbuf, 0, sizeof(qbuf));
                    328:                        while (*lp != '"') {
                    329:                                if (*lp == '\0') {
                    330:                                        cvs_log(LP_ERR, "no terminating quote");
                    331:                                        err++;
                    332:                                        break;
                    333:                                }
                    334:                                else if (*lp == '\\')
                    335:                                        lp++;
                    336:
                    337:                                qbuf[i++] = *lp++;
                    338:                                if (i == sizeof(qbuf)) {
                    339:                                        err++;
                    340:                                        break;
                    341:                                }
                    342:                        }
                    343:
                    344:                        arg = qbuf;
                    345:                }
                    346:                else {
                    347:                        cp = strsep(&lp, " \t");
                    348:                        if (cp == NULL)
                    349:                                break;
                    350:                        else if (*cp == '\0')
                    351:                                continue;
                    352:
                    353:                        arg = cp;
                    354:                }
                    355:
                    356:                argv[argc] = strdup(arg);
                    357:                if (argv[argc] == NULL) {
                    358:                        cvs_log(LP_ERRNO, "failed to copy argument");
                    359:                        err++;
                    360:                        break;
                    361:                }
                    362:                argc++;
                    363:        }
                    364:
                    365:        if (err) {
                    366:                /* ditch the argument vector */
                    367:                for (i = 0; i < (u_int)argc; i++)
                    368:                        free(argv[i]);
                    369:                argc = -1;
                    370:        }
                    371:
                    372:        return (argc);
                    373: }
                    374:
                    375:
                    376: /*
                    377:  * cvs_freeargv()
                    378:  *
                    379:  * Free an argument vector previously generated by cvs_getargv().
                    380:  */
                    381:
                    382: void
                    383: cvs_freeargv(char **argv, int argc)
                    384: {
                    385:        int i;
                    386:
                    387:        for (i = 0; i < argc; i++)
                    388:                free(argv[i]);
1.2       jfb       389: }
                    390:
                    391:
                    392: /*
                    393:  * cvs_mkadmin()
                    394:  *
1.5       jfb       395:  * Create the CVS administrative files within the directory <cdir>.  If the
                    396:  * files already exist, they are kept as is.
1.2       jfb       397:  * Returns 0 on success, or -1 on failure.
                    398:  */
                    399:
                    400: int
                    401: cvs_mkadmin(struct cvs_file *cdir, mode_t mode)
                    402: {
                    403:        char path[MAXPATHLEN];
                    404:        FILE *fp;
                    405:        CVSENTRIES *ef;
1.5       jfb       406:        struct stat st;
1.2       jfb       407:        struct cvsroot *root;
                    408:
                    409:        snprintf(path, sizeof(path), "%s/" CVS_PATH_CVSDIR, cdir->cf_path);
1.5       jfb       410:        if ((mkdir(path, mode) == -1) && (errno != EEXIST)) {
1.2       jfb       411:                cvs_log(LP_ERRNO, "failed to create directory %s", path);
                    412:                return (-1);
                    413:        }
                    414:
1.5       jfb       415:        /* just create an empty Entries file */
1.2       jfb       416:        ef = cvs_ent_open(cdir->cf_path, O_WRONLY);
                    417:        (void)cvs_ent_close(ef);
                    418:
1.5       jfb       419:        root = cdir->cf_ddat->cd_root;
1.2       jfb       420:        snprintf(path, sizeof(path), "%s/" CVS_PATH_ROOTSPEC, cdir->cf_path);
1.5       jfb       421:        if ((stat(path, &st) != 0) && (errno == ENOENT) && (root != NULL)) {
                    422:                fp = fopen(path, "w");
                    423:                if (fp == NULL) {
                    424:                        cvs_log(LP_ERRNO, "failed to open %s", path);
                    425:                        return (-1);
                    426:                }
                    427:                if (root->cr_user != NULL) {
                    428:                        fprintf(fp, "%s", root->cr_user);
                    429:                        if (root->cr_pass != NULL)
                    430:                                fprintf(fp, ":%s", root->cr_pass);
                    431:                        if (root->cr_host != NULL)
                    432:                                putc('@', fp);
                    433:                }
                    434:
                    435:                if (root->cr_host != NULL) {
                    436:                        fprintf(fp, "%s", root->cr_host);
                    437:                        if (root->cr_dir != NULL)
                    438:                                putc(':', fp);
                    439:                }
                    440:                if (root->cr_dir)
                    441:                        fprintf(fp, "%s", root->cr_dir);
                    442:                putc('\n', fp);
                    443:                (void)fclose(fp);
1.2       jfb       444:        }
                    445:
1.5       jfb       446:        snprintf(path, sizeof(path), "%s/" CVS_PATH_REPOSITORY, cdir->cf_path);
                    447:        if ((stat(path, &st) != 0) && (errno == ENOENT) &&
                    448:            (cdir->cf_ddat->cd_repo != NULL)) {
1.2       jfb       449:                fp = fopen(path, "w");
                    450:                if (fp == NULL) {
                    451:                        cvs_log(LP_ERRNO, "failed to open %s", path);
                    452:                        return (-1);
                    453:                }
1.3       jfb       454:                fprintf(fp, "%s\n", cdir->cf_ddat->cd_repo);
1.2       jfb       455:                (void)fclose(fp);
                    456:        }
                    457:
                    458:        return (0);
1.1       jfb       459: }