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

1.39    ! xsa         1: /*     $OpenBSD: util.c,v 1.38 2005/07/23 10:49:35 xsa Exp $   */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.9       jfb         4:  * All rights reserved.
1.1       jfb         5:  *
1.9       jfb         6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.9       jfb        10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.9       jfb        13:  *    derived from this software without specific prior written permission.
1.1       jfb        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
1.9       jfb        24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
                     27: #include <sys/types.h>
                     28: #include <sys/stat.h>
1.12      krapht     29: #include <sys/wait.h>
1.1       jfb        30:
                     31: #include <errno.h>
1.2       jfb        32: #include <fcntl.h>
1.31      xsa        33: #include <md5.h>
1.1       jfb        34: #include <stdio.h>
                     35: #include <stdlib.h>
1.31      xsa        36: #include <string.h>
1.1       jfb        37: #include <unistd.h>
                     38:
                     39: #include "cvs.h"
                     40: #include "log.h"
                     41:
                     42: /* letter -> mode type map */
                     43: static const int cvs_modetypes[26] = {
                     44:        -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1,
                     45:        -1,  2, -1, -1, -1, -1, -1,  0, -1, -1, -1, -1, -1,
                     46: };
                     47:
                     48: /* letter -> mode map */
                     49: static const mode_t cvs_modes[3][26] = {
                     50:        {
                     51:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     52:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     53:                0,  0,       0,       S_IRUSR, 0,  0,  0,    /* n - u */
                     54:                0,  S_IWUSR, S_IXUSR, 0,       0             /* v - z */
                     55:        },
                     56:        {
                     57:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     58:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     59:                0,  0,       0,       S_IRGRP, 0,  0,  0,    /* n - u */
                     60:                0,  S_IWGRP, S_IXGRP, 0,       0             /* v - z */
                     61:        },
                     62:        {
                     63:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     64:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     65:                0,  0,       0,       S_IROTH, 0,  0,  0,    /* n - u */
                     66:                0,  S_IWOTH, S_IXOTH, 0,       0             /* v - z */
                     67:        }
                     68: };
                     69:
                     70:
                     71: /* octal -> string */
                     72: static const char *cvs_modestr[8] = {
                     73:        "", "x", "w", "wx", "r", "rx", "rw", "rwx"
                     74: };
                     75:
                     76:
1.11      krapht     77: pid_t cvs_exec_pid;
                     78:
1.1       jfb        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: int
                     88: cvs_readrepo(const char *dir, char *dst, size_t len)
                     89: {
1.37      xsa        90:        size_t dlen, l;
1.1       jfb        91:        FILE *fp;
                     92:        char repo_path[MAXPATHLEN];
                     93:
1.37      xsa        94:        l = cvs_path_cat(dir, "CVS/Repository", repo_path, sizeof(repo_path));
                     95:        if (l >= sizeof(repo_path))
1.21      xsa        96:                return (NULL);
                     97:
1.1       jfb        98:        fp = fopen(repo_path, "r");
1.21      xsa        99:        if (fp == NULL)
1.1       jfb       100:                return (-1);
                    101:
                    102:        if (fgets(dst, (int)len, fp) == NULL) {
                    103:                if (ferror(fp)) {
                    104:                        cvs_log(LP_ERRNO, "failed to read from `%s'",
                    105:                            repo_path);
                    106:                }
                    107:                (void)fclose(fp);
                    108:                return (-1);
                    109:        }
                    110:        dlen = strlen(dst);
                    111:        if ((dlen > 0) && (dst[dlen - 1] == '\n'))
                    112:                dst[--dlen] = '\0';
                    113:
                    114:        (void)fclose(fp);
                    115:        return (0);
1.9       jfb       116: }
                    117:
                    118:
                    119: /*
1.1       jfb       120:  * cvs_strtomode()
                    121:  *
                    122:  * Read the contents of the string <str> and generate a permission mode from
                    123:  * the contents of <str>, which is assumed to have the mode format of CVS.
                    124:  * The CVS protocol specification states that any modes or mode types that are
                    125:  * not recognized should be silently ignored.  This function does not return
                    126:  * an error in such cases, but will issue warnings.
                    127:  * Returns 0 on success, or -1 on failure.
                    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;
1.30      jfb       137:        if (strlcpy(buf, str, sizeof(buf)) >= sizeof(buf)) {
                    138:                return (-1);
                    139:        }
1.1       jfb       140:        sp = buf;
                    141:        ep = sp;
                    142:
                    143:        for (sp = buf; ep != NULL; sp = ep + 1) {
                    144:                ep = strchr(sp, ',');
                    145:                if (ep != NULL)
1.2       jfb       146:                        *ep = '\0';
1.1       jfb       147:
1.14      weingart  148:                memset(ms, 0, sizeof ms);
                    149:                if (sscanf(sp, "%c=%3s", &type, ms) != 2 &&
                    150:                        sscanf(sp, "%c=", &type) != 1) {
1.1       jfb       151:                        cvs_log(LP_WARN, "failed to scan mode string `%s'", sp);
                    152:                        continue;
                    153:                }
                    154:
                    155:                if ((type <= 'a') || (type >= 'z') ||
                    156:                    (cvs_modetypes[type - 'a'] == -1)) {
                    157:                        cvs_log(LP_WARN,
                    158:                            "invalid mode type `%c'"
                    159:                            " (`u', `g' or `o' expected), ignoring", type);
                    160:                        continue;
                    161:                }
                    162:
                    163:                /* make type contain the actual mode index */
                    164:                type = cvs_modetypes[type - 'a'];
                    165:
                    166:                for (sp = ms; *sp != '\0'; sp++) {
                    167:                        if ((*sp <= 'a') || (*sp >= 'z') ||
1.5       jfb       168:                            (cvs_modes[(int)type][*sp - 'a'] == 0)) {
1.1       jfb       169:                                cvs_log(LP_WARN,
                    170:                                    "invalid permission bit `%c'", *sp);
1.15      deraadt   171:                        } else
1.5       jfb       172:                                m |= cvs_modes[(int)type][*sp - 'a'];
1.1       jfb       173:                }
                    174:        }
                    175:
                    176:        *mode = m;
                    177:
                    178:        return (0);
                    179: }
                    180:
                    181:
                    182: /*
                    183:  * cvs_modetostr()
                    184:  *
1.30      jfb       185:  * Generate a CVS-format string to represent the permissions mask on a file
                    186:  * from the mode <mode> and store the result in <buf>, which can accept up to
                    187:  * <len> bytes (including the terminating NUL byte).  The result is guaranteed
                    188:  * to be NUL-terminated.
1.1       jfb       189:  * Returns 0 on success, or -1 on failure.
                    190:  */
                    191: int
                    192: cvs_modetostr(mode_t mode, char *buf, size_t len)
                    193: {
                    194:        size_t l;
                    195:        char tmp[16], *bp;
                    196:        mode_t um, gm, om;
                    197:
                    198:        um = (mode & S_IRWXU) >> 6;
                    199:        gm = (mode & S_IRWXG) >> 3;
                    200:        om = mode & S_IRWXO;
                    201:
                    202:        bp = buf;
                    203:        *bp = '\0';
                    204:        l = 0;
                    205:
                    206:        if (um) {
                    207:                snprintf(tmp, sizeof(tmp), "u=%s", cvs_modestr[um]);
1.9       jfb       208:                l = strlcat(buf, tmp, len);
1.1       jfb       209:        }
                    210:        if (gm) {
                    211:                if (um)
                    212:                        strlcat(buf, ",", len);
                    213:                snprintf(tmp, sizeof(tmp), "g=%s", cvs_modestr[gm]);
1.9       jfb       214:                strlcat(buf, tmp, len);
1.1       jfb       215:        }
                    216:        if (om) {
                    217:                if (um || gm)
                    218:                        strlcat(buf, ",", len);
                    219:                snprintf(tmp, sizeof(tmp), "o=%s", cvs_modestr[gm]);
1.9       jfb       220:                strlcat(buf, tmp, len);
1.1       jfb       221:        }
                    222:
                    223:        return (0);
                    224: }
                    225:
                    226: /*
                    227:  * cvs_cksum()
                    228:  *
                    229:  * Calculate the MD5 checksum of the file whose path is <file> and generate
                    230:  * a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
                    231:  * given in <len> and must be at least 33.
                    232:  * Returns 0 on success, or -1 on failure.
                    233:  */
                    234: int
                    235: cvs_cksum(const char *file, char *dst, size_t len)
                    236: {
                    237:        if (len < CVS_CKSUM_LEN) {
                    238:                cvs_log(LP_WARN, "buffer too small for checksum");
                    239:                return (-1);
                    240:        }
                    241:        if (MD5File(file, dst) == NULL) {
1.19      jfb       242:                cvs_log(LP_ERRNO, "failed to generate checksum for %s", file);
1.1       jfb       243:                return (-1);
                    244:        }
                    245:
                    246:        return (0);
                    247: }
                    248:
                    249: /*
                    250:  * cvs_splitpath()
                    251:  *
1.7       jfb       252:  * Split a path <path> into the base portion and the filename portion.
                    253:  * The path is copied in <base> and the last delimiter is replaced by a NUL
                    254:  * byte.  The <file> pointer is set to point to the first character after
                    255:  * that delimiter.
1.1       jfb       256:  * Returns 0 on success, or -1 on failure.
                    257:  */
                    258: int
1.7       jfb       259: cvs_splitpath(const char *path, char *base, size_t blen, char **file)
1.1       jfb       260: {
                    261:        size_t rlen;
1.7       jfb       262:        char *sp;
1.1       jfb       263:
1.7       jfb       264:        if ((rlen = strlcpy(base, path, blen)) >= blen)
                    265:                return (-1);
1.6       jfb       266:
1.7       jfb       267:        while ((rlen > 0) && (base[rlen - 1] == '/'))
                    268:                base[--rlen] = '\0';
                    269:
                    270:        sp = strrchr(base, '/');
1.1       jfb       271:        if (sp == NULL) {
1.7       jfb       272:                strlcpy(base, "./", blen);
                    273:                strlcat(base, path, blen);
                    274:                sp = base + 1;
1.1       jfb       275:        }
                    276:
1.7       jfb       277:        *sp = '\0';
                    278:        if (file != NULL)
                    279:                *file = sp + 1;
1.1       jfb       280:
                    281:        return (0);
                    282: }
                    283:
                    284:
                    285: /*
                    286:  * cvs_getargv()
                    287:  *
                    288:  * Parse a line contained in <line> and generate an argument vector by
                    289:  * splitting the line on spaces and tabs.  The resulting vector is stored in
                    290:  * <argv>, which can accept up to <argvlen> entries.
1.20      david     291:  * Returns the number of arguments in the vector, or -1 if an error occurred.
1.1       jfb       292:  */
                    293: int
                    294: cvs_getargv(const char *line, char **argv, int argvlen)
                    295: {
                    296:        u_int i;
                    297:        int argc, err;
                    298:        char linebuf[256], qbuf[128], *lp, *cp, *arg;
                    299:
                    300:        strlcpy(linebuf, line, sizeof(linebuf));
1.27      pat       301:        memset(argv, 0, argvlen * sizeof(char *));
1.1       jfb       302:        argc = 0;
                    303:
                    304:        /* build the argument vector */
                    305:        err = 0;
                    306:        for (lp = linebuf; lp != NULL;) {
                    307:                if (*lp == '"') {
                    308:                        /* double-quoted string */
                    309:                        lp++;
                    310:                        i = 0;
                    311:                        memset(qbuf, 0, sizeof(qbuf));
                    312:                        while (*lp != '"') {
1.16      jfb       313:                                if (*lp == '\\')
                    314:                                        lp++;
1.1       jfb       315:                                if (*lp == '\0') {
                    316:                                        cvs_log(LP_ERR, "no terminating quote");
                    317:                                        err++;
                    318:                                        break;
1.16      jfb       319:                                }
1.1       jfb       320:
1.9       jfb       321:                                qbuf[i++] = *lp++;
1.1       jfb       322:                                if (i == sizeof(qbuf)) {
                    323:                                        err++;
                    324:                                        break;
                    325:                                }
                    326:                        }
                    327:
                    328:                        arg = qbuf;
1.15      deraadt   329:                } else {
1.1       jfb       330:                        cp = strsep(&lp, " \t");
                    331:                        if (cp == NULL)
                    332:                                break;
                    333:                        else if (*cp == '\0')
                    334:                                continue;
                    335:
                    336:                        arg = cp;
                    337:                }
                    338:
1.16      jfb       339:                if (argc == argvlen) {
                    340:                        err++;
                    341:                        break;
                    342:                }
                    343:
1.1       jfb       344:                argv[argc] = strdup(arg);
                    345:                if (argv[argc] == NULL) {
                    346:                        cvs_log(LP_ERRNO, "failed to copy argument");
                    347:                        err++;
                    348:                        break;
                    349:                }
                    350:                argc++;
                    351:        }
                    352:
                    353:        if (err) {
                    354:                /* ditch the argument vector */
                    355:                for (i = 0; i < (u_int)argc; i++)
                    356:                        free(argv[i]);
                    357:                argc = -1;
                    358:        }
                    359:
                    360:        return (argc);
1.17      jfb       361: }
                    362:
                    363:
                    364: /*
                    365:  * cvs_makeargv()
                    366:  *
1.20      david     367:  * Allocate an argument vector large enough to accommodate for all the
1.17      jfb       368:  * arguments found in <line> and return it.
                    369:  */
                    370: char**
                    371: cvs_makeargv(const char *line, int *argc)
                    372: {
                    373:        int i, ret;
                    374:        char *argv[1024], **copy;
1.27      pat       375:        size_t size;
1.17      jfb       376:
                    377:        ret = cvs_getargv(line, argv, 1024);
                    378:        if (ret == -1)
                    379:                return (NULL);
                    380:
1.27      pat       381:        size = (ret + 1) * sizeof(char *);
                    382:        copy = (char **)malloc(size);
1.17      jfb       383:        if (copy == NULL) {
                    384:                cvs_log(LP_ERRNO, "failed to allocate argument vector");
1.27      pat       385:                cvs_freeargv(argv, ret);
1.17      jfb       386:                return (NULL);
                    387:        }
1.27      pat       388:        memset(copy, 0, size);
1.17      jfb       389:
                    390:        for (i = 0; i < ret; i++)
                    391:                copy[i] = argv[i];
                    392:        copy[ret] = NULL;
                    393:
                    394:        *argc = ret;
                    395:        return (copy);
1.1       jfb       396: }
                    397:
                    398:
                    399: /*
                    400:  * cvs_freeargv()
                    401:  *
                    402:  * Free an argument vector previously generated by cvs_getargv().
                    403:  */
                    404: void
                    405: cvs_freeargv(char **argv, int argc)
                    406: {
                    407:        int i;
                    408:
                    409:        for (i = 0; i < argc; i++)
1.16      jfb       410:                if (argv[i] != NULL)
                    411:                        free(argv[i]);
1.2       jfb       412: }
                    413:
                    414:
                    415: /*
                    416:  * cvs_mkadmin()
                    417:  *
1.5       jfb       418:  * Create the CVS administrative files within the directory <cdir>.  If the
                    419:  * files already exist, they are kept as is.
1.2       jfb       420:  * Returns 0 on success, or -1 on failure.
                    421:  */
                    422: int
1.28      joris     423: cvs_mkadmin(const char *dpath, const char *rootpath, const char *repopath)
1.2       jfb       424: {
1.37      xsa       425:        size_t l;
1.28      joris     426:        char path[MAXPATHLEN];
1.2       jfb       427:        FILE *fp;
                    428:        CVSENTRIES *ef;
1.5       jfb       429:        struct stat st;
1.13      jfb       430:
1.37      xsa       431:        l = cvs_path_cat(dpath, CVS_PATH_CVSDIR, path, sizeof(path));
                    432:        if (l >= sizeof(path))
1.21      xsa       433:                return (-1);
                    434:
1.28      joris     435:        if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) {
1.2       jfb       436:                cvs_log(LP_ERRNO, "failed to create directory %s", path);
                    437:                return (-1);
                    438:        }
                    439:
1.5       jfb       440:        /* just create an empty Entries file */
1.13      jfb       441:        ef = cvs_ent_open(dpath, O_WRONLY);
1.2       jfb       442:        (void)cvs_ent_close(ef);
                    443:
1.37      xsa       444:        l = cvs_path_cat(dpath, CVS_PATH_ROOTSPEC, path, sizeof(path));
                    445:        if (l >= sizeof(path))
1.21      xsa       446:                return (-1);
                    447:
1.28      joris     448:        if ((stat(path, &st) != 0) && (errno == ENOENT)) {
1.5       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.28      joris     454:                if (rootpath != NULL)
                    455:                        fprintf(fp, "%s\n", rootpath);
1.5       jfb       456:                (void)fclose(fp);
1.2       jfb       457:        }
                    458:
1.37      xsa       459:        l = cvs_path_cat(dpath, CVS_PATH_REPOSITORY, path, sizeof(path));
                    460:        if (l >= sizeof(path))
1.21      xsa       461:                return (-1);
                    462:
1.28      joris     463:        if ((stat(path, &st) != 0) && (errno == ENOENT)) {
1.2       jfb       464:                fp = fopen(path, "w");
                    465:                if (fp == NULL) {
                    466:                        cvs_log(LP_ERRNO, "failed to open %s", path);
                    467:                        return (-1);
                    468:                }
1.28      joris     469:                if (repopath != NULL)
                    470:                        fprintf(fp, "%s\n", repopath);
1.2       jfb       471:                (void)fclose(fp);
                    472:        }
                    473:
                    474:        return (0);
1.11      krapht    475: }
                    476:
                    477:
                    478: /*
                    479:  * cvs_exec()
                    480:  */
                    481: int
                    482: cvs_exec(int argc, char **argv, int fds[3])
                    483: {
                    484:        int ret;
                    485:        pid_t pid;
                    486:
                    487:        if ((pid = fork()) == -1) {
                    488:                cvs_log(LP_ERRNO, "failed to fork");
                    489:                return (-1);
                    490:        } else if (pid == 0) {
                    491:                execvp(argv[0], argv);
1.13      jfb       492:                cvs_log(LP_ERRNO, "failed to exec %s", argv[0]);
                    493:                exit(1);
1.11      krapht    494:        }
                    495:
                    496:        if (waitpid(pid, &ret, 0) == -1)
1.13      jfb       497:                cvs_log(LP_ERRNO, "failed to waitpid");
1.11      krapht    498:
                    499:        return (ret);
1.38      xsa       500: }
                    501:
                    502: /*
                    503:  * cvs_chdir()
                    504:  *
                    505:  * Change to directory.
                    506:  * chdir() wrapper with an error message.
                    507:  * Returns 0 on success, or -1 on failure.
                    508:  */
                    509: int
                    510: cvs_chdir(const char *path)
                    511: {
                    512:        if (chdir(path) == -1) {
                    513:                cvs_log(LP_ERRNO, "cannot change to dir `%s'", path);
                    514:                return (-1);
                    515:        }
                    516:
                    517:        return (0);
1.1       jfb       518: }
1.24      joris     519:
                    520: /*
1.30      jfb       521:  * cvs_remove_dir()
                    522:  *
                    523:  * Remove a directory tree from disk.
                    524:  * Returns 0 on success, or -1 on failure.
1.24      joris     525:  */
                    526: int
                    527: cvs_remove_dir(const char *path)
                    528: {
1.33      pat       529:        int ret = -1;
1.30      jfb       530:        size_t len;
1.24      joris     531:        DIR *dirp;
                    532:        struct dirent *ent;
                    533:        char fpath[MAXPATHLEN];
                    534:
                    535:        if ((dirp = opendir(path)) == NULL) {
                    536:                cvs_log(LP_ERRNO, "failed to open '%s'", path);
1.30      jfb       537:                return (-1);
1.24      joris     538:        }
                    539:
                    540:        while ((ent = readdir(dirp)) != NULL) {
                    541:                if (!strcmp(ent->d_name, ".") ||
                    542:                    !strcmp(ent->d_name, ".."))
                    543:                        continue;
                    544:
1.30      jfb       545:                len = cvs_path_cat(path, ent->d_name, fpath, sizeof(fpath));
1.33      pat       546:                if (len >= sizeof(fpath))
                    547:                        goto done;
1.24      joris     548:
                    549:                if (ent->d_type == DT_DIR) {
1.33      pat       550:                        if (cvs_remove_dir(fpath) == -1)
                    551:                                goto done;
1.30      jfb       552:                } else if ((unlink(fpath) == -1) && (errno != ENOENT)) {
                    553:                        cvs_log(LP_ERRNO, "failed to remove '%s'", fpath);
1.33      pat       554:                        goto done;
1.24      joris     555:                }
                    556:        }
                    557:
                    558:
1.30      jfb       559:        if ((rmdir(path) == -1) && (errno != ENOENT)) {
1.24      joris     560:                cvs_log(LP_ERRNO, "failed to remove '%s'", path);
1.33      pat       561:                goto done;
1.30      jfb       562:        }
1.24      joris     563:
1.33      pat       564:        ret = 0;
                    565: done:
                    566:        closedir(dirp);
1.34      joris     567:        return (ret);
                    568: }
                    569:
                    570: /*
                    571:  * Create a directory, and the parent directories if needed.
                    572:  * based upon mkpath() from mkdir.c
                    573:  */
                    574: int
                    575: cvs_create_dir(const char *path, int create_adm, char *root, char *repo)
                    576: {
                    577:        size_t l;
                    578:        int len, ret;
                    579:        char *d, *s;
                    580:        struct stat sb;
                    581:        char rpath[MAXPATHLEN], entry[MAXPATHLEN];
                    582:        CVSENTRIES *entf;
                    583:        struct cvs_ent *ent;
                    584:
                    585:        if (create_adm == 1 && (root == NULL || repo == NULL)) {
                    586:                cvs_log(LP_ERR, "missing stuff in cvs_create_dir");
                    587:                return (-1);
                    588:        }
                    589:
                    590:        if ((s = strdup(path)) == NULL)
                    591:                return (-1);
                    592:
                    593:        if (strlcpy(rpath, repo, sizeof(rpath)) >= sizeof(rpath) ||
                    594:            strlcat(rpath, "/", sizeof(rpath)) >= sizeof(rpath)) {
                    595:                errno = ENAMETOOLONG;
                    596:                cvs_log(LP_ERRNO, "%s", rpath);
                    597:                free(s);
                    598:                return (-1);
                    599:        }
                    600:
                    601:        ret = -1;
                    602:        entf = NULL;
                    603:        d = strtok(s, "/");
                    604:        while (d != NULL) {
                    605:                if (stat(d, &sb)) {
                    606:                        /* try to create the directory */
                    607:                        if ((errno != ENOENT) || (mkdir(d, 0755) &&
                    608:                            errno != EEXIST)) {
                    609:                                cvs_log(LP_ERRNO, "failed to create `%s'", d);
                    610:                                goto done;
                    611:                        }
                    612:                } else if (!S_ISDIR(sb.st_mode)) {
                    613:                        cvs_log(LP_ERR, "`%s' not a directory", d);
                    614:                        goto done;
                    615:                }
                    616:
                    617:                /*
                    618:                 * Create administrative files if requested.
                    619:                 */
                    620:                if (create_adm) {
                    621:                        l = strlcat(rpath, d, sizeof(rpath));
                    622:                        if (l >= sizeof(rpath))
                    623:                                goto done;
                    624:
                    625:                        l = strlcat(rpath, "/", sizeof(rpath));
                    626:                        if (l >= sizeof(rpath))
                    627:                                goto done;
                    628:
                    629:                        if (cvs_mkadmin(d, root, rpath) < 0) {
                    630:                                cvs_log(LP_ERR, "failed to create adm files");
                    631:                                goto done;
                    632:                        }
                    633:                }
                    634:
                    635:                /*
                    636:                 * Add it to the parent directory entry file.
                    637:                 * (if any).
                    638:                 */
                    639:                entf = cvs_ent_open(".", O_RDWR);
                    640:                if (entf != NULL && strcmp(d, ".")) {
                    641:                        len = snprintf(entry, sizeof(entry), "D/%s////", d);
                    642:                        if (len == -1 || len >= (int)sizeof(entry)) {
                    643:                                errno = ENAMETOOLONG;
                    644:                                cvs_log(LP_ERRNO, "%s", entry);
                    645:                                goto done;
                    646:                        }
                    647:
                    648:                        if ((ent = cvs_ent_parse(entry)) == NULL) {
                    649:                                cvs_log(LP_ERR, "failed to parse entry");
                    650:                                goto done;
                    651:                        }
                    652:
                    653:                        cvs_ent_remove(entf, d);
                    654:
                    655:                        if (cvs_ent_add(entf, ent) < 0) {
                    656:                                cvs_log(LP_ERR, "failed to add entry");
                    657:                                goto done;
                    658:                        }
                    659:                }
                    660:
                    661:                if (entf != NULL) {
                    662:                        cvs_ent_close(entf);
                    663:                        entf = NULL;
                    664:                }
                    665:
                    666:                /*
                    667:                 * All went ok, switch to the newly created directory.
                    668:                 */
1.39    ! xsa       669:                if (cvs_chdir(d) == -1)
1.34      joris     670:                        goto done;
                    671:
                    672:                d = strtok(NULL, "/");
                    673:        }
                    674:
                    675:        ret = 0;
                    676: done:
                    677:        if (entf != NULL)
                    678:                cvs_ent_close(entf);
                    679:        free(s);
1.33      pat       680:        return (ret);
1.24      joris     681: }
                    682:
1.30      jfb       683: /*
                    684:  * cvs_path_cat()
                    685:  *
                    686:  * Concatenate the two paths <base> and <end> and store the generated path
                    687:  * into the buffer <dst>, which can accept up to <dlen> bytes, including the
                    688:  * NUL byte.  The result is guaranteed to be NUL-terminated.
                    689:  * Returns the number of bytes necessary to store the full resulting path,
                    690:  * not including the NUL byte (a value equal to or larger than <dlen>
                    691:  * indicates truncation).
                    692:  */
1.29      jfb       693: size_t
                    694: cvs_path_cat(const char *base, const char *end, char *dst, size_t dlen)
                    695: {
                    696:        size_t len;
                    697:
                    698:        len = strlcpy(dst, base, dlen);
                    699:        if (len >= dlen - 1) {
                    700:                errno = ENAMETOOLONG;
                    701:                cvs_log(LP_ERRNO, "%s", dst);
                    702:        } else {
                    703:                dst[len] = '/';
                    704:                dst[len + 1] = '\0';
                    705:                len = strlcat(dst, end, dlen);
                    706:                if (len >= dlen) {
                    707:                        errno = ENAMETOOLONG;
                    708:                        cvs_log(LP_ERRNO, "%s", dst);
                    709:                }
                    710:        }
                    711:
                    712:        return (len);
1.35      xsa       713: }
                    714:
                    715: /*
                    716:  * cvs_rcs_getpath()
                    717:  *
                    718:  * Get the RCS path of the file <file> and store it in <buf>, which is
                    719:  * of size <len>. For portability, it is recommended that <buf> always be
                    720:  * at least MAXPATHLEN bytes long.
                    721:  * Returns a pointer to the start of the path on success, or NULL on failure.
                    722:  */
                    723: char*
                    724: cvs_rcs_getpath(CVSFILE *file, char *buf, size_t len)
                    725: {
                    726:        int l;
                    727:        char *repo;
                    728:        struct cvsroot *root;
                    729:
                    730:        root = CVS_DIR_ROOT(file);
                    731:        repo = CVS_DIR_REPO(file);
                    732:
1.36      deraadt   733:        l = snprintf(buf, len, "%s/%s/%s%s",
1.35      xsa       734:            root->cr_dir, repo, file->cf_name, RCS_FILE_EXT);
                    735:        if (l == -1 || l >= (int)len) {
                    736:                errno = ENAMETOOLONG;
                    737:                cvs_log(LP_ERRNO, "%s", buf);
                    738:                return (NULL);
                    739:        }
                    740:
                    741:        return (buf);
1.29      jfb       742: }