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

1.107   ! otto        1: /*     $OpenBSD: util.c,v 1.106 2007/02/19 11:40:00 otto Exp $ */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.71      xsa         4:  * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
                      5:  * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.9       jfb         6:  * All rights reserved.
1.1       jfb         7:  *
1.9       jfb         8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
1.1       jfb        11:  *
1.9       jfb        12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        14:  * 2. The name of the author may not be used to endorse or promote products
1.9       jfb        15:  *    derived from this software without specific prior written permission.
1.1       jfb        16:  *
                     17:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     18:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     19:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     20:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     21:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     22:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     23:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     24:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     25:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.9       jfb        26:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        27:  */
                     28:
1.107   ! otto       29: #include <sys/stat.h>
        !            30: #include <sys/wait.h>
        !            31:
        !            32: #include <errno.h>
        !            33: #include <md5.h>
        !            34: #include <stdlib.h>
        !            35: #include <string.h>
        !            36: #include <unistd.h>
1.1       jfb        37:
                     38: #include "cvs.h"
                     39:
                     40: /* letter -> mode type map */
                     41: static const int cvs_modetypes[26] = {
                     42:        -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1,
                     43:        -1,  2, -1, -1, -1, -1, -1,  0, -1, -1, -1, -1, -1,
                     44: };
                     45:
                     46: /* letter -> mode map */
                     47: static const mode_t cvs_modes[3][26] = {
                     48:        {
                     49:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     50:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     51:                0,  0,       0,       S_IRUSR, 0,  0,  0,    /* n - u */
                     52:                0,  S_IWUSR, S_IXUSR, 0,       0             /* v - z */
                     53:        },
                     54:        {
                     55:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     56:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     57:                0,  0,       0,       S_IRGRP, 0,  0,  0,    /* n - u */
                     58:                0,  S_IWGRP, S_IXGRP, 0,       0             /* v - z */
                     59:        },
                     60:        {
                     61:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     62:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     63:                0,  0,       0,       S_IROTH, 0,  0,  0,    /* n - u */
                     64:                0,  S_IWOTH, S_IXOTH, 0,       0             /* v - z */
                     65:        }
                     66: };
                     67:
                     68:
                     69: /* octal -> string */
                     70: static const char *cvs_modestr[8] = {
                     71:        "", "x", "w", "wx", "r", "rx", "rw", "rwx"
                     72: };
                     73:
1.9       jfb        74: /*
1.1       jfb        75:  * cvs_strtomode()
                     76:  *
                     77:  * Read the contents of the string <str> and generate a permission mode from
                     78:  * the contents of <str>, which is assumed to have the mode format of CVS.
                     79:  * The CVS protocol specification states that any modes or mode types that are
                     80:  * not recognized should be silently ignored.  This function does not return
                     81:  * an error in such cases, but will issue warnings.
                     82:  */
1.64      joris      83: void
1.1       jfb        84: cvs_strtomode(const char *str, mode_t *mode)
                     85: {
1.2       jfb        86:        char type;
1.64      joris      87:        size_t l;
1.1       jfb        88:        mode_t m;
                     89:        char buf[32], ms[4], *sp, *ep;
                     90:
                     91:        m = 0;
1.64      joris      92:        l = strlcpy(buf, str, sizeof(buf));
                     93:        if (l >= sizeof(buf))
                     94:                fatal("cvs_strtomode: string truncation");
                     95:
1.1       jfb        96:        sp = buf;
                     97:        ep = sp;
                     98:
                     99:        for (sp = buf; ep != NULL; sp = ep + 1) {
                    100:                ep = strchr(sp, ',');
                    101:                if (ep != NULL)
1.2       jfb       102:                        *ep = '\0';
1.1       jfb       103:
1.14      weingart  104:                memset(ms, 0, sizeof ms);
                    105:                if (sscanf(sp, "%c=%3s", &type, ms) != 2 &&
                    106:                        sscanf(sp, "%c=", &type) != 1) {
1.89      joris     107:                        fatal("failed to scan mode string `%s'", sp);
1.1       jfb       108:                }
                    109:
1.78      deraadt   110:                if (type <= 'a' || type >= 'z' ||
                    111:                    cvs_modetypes[type - 'a'] == -1) {
1.79      joris     112:                        cvs_log(LP_ERR,
1.1       jfb       113:                            "invalid mode type `%c'"
                    114:                            " (`u', `g' or `o' expected), ignoring", type);
                    115:                        continue;
                    116:                }
                    117:
                    118:                /* make type contain the actual mode index */
                    119:                type = cvs_modetypes[type - 'a'];
                    120:
                    121:                for (sp = ms; *sp != '\0'; sp++) {
1.78      deraadt   122:                        if (*sp <= 'a' || *sp >= 'z' ||
                    123:                            cvs_modes[(int)type][*sp - 'a'] == 0) {
1.89      joris     124:                                fatal("invalid permission bit `%c'", *sp);
1.15      deraadt   125:                        } else
1.5       jfb       126:                                m |= cvs_modes[(int)type][*sp - 'a'];
1.1       jfb       127:                }
                    128:        }
                    129:
                    130:        *mode = m;
                    131: }
                    132:
                    133: /*
                    134:  * cvs_modetostr()
                    135:  *
1.30      jfb       136:  * Generate a CVS-format string to represent the permissions mask on a file
                    137:  * from the mode <mode> and store the result in <buf>, which can accept up to
                    138:  * <len> bytes (including the terminating NUL byte).  The result is guaranteed
                    139:  * to be NUL-terminated.
1.1       jfb       140:  */
1.65      joris     141: void
1.1       jfb       142: cvs_modetostr(mode_t mode, char *buf, size_t len)
                    143: {
                    144:        char tmp[16], *bp;
                    145:        mode_t um, gm, om;
                    146:
                    147:        um = (mode & S_IRWXU) >> 6;
                    148:        gm = (mode & S_IRWXG) >> 3;
                    149:        om = mode & S_IRWXO;
                    150:
                    151:        bp = buf;
                    152:        *bp = '\0';
                    153:
                    154:        if (um) {
1.68      xsa       155:                if (strlcpy(tmp, "u=", sizeof(tmp)) >= sizeof(tmp) ||
                    156:                    strlcat(tmp, cvs_modestr[um], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     157:                        fatal("cvs_modetostr: overflow for user mode");
                    158:
1.68      xsa       159:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     160:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       161:        }
1.65      joris     162:
1.1       jfb       163:        if (gm) {
1.65      joris     164:                if (um) {
1.68      xsa       165:                        if (strlcat(buf, ",", len) >= len)
1.65      joris     166:                                fatal("cvs_modetostr: string truncation");
                    167:                }
                    168:
1.68      xsa       169:                if (strlcpy(tmp, "g=", sizeof(tmp)) >= sizeof(tmp) ||
                    170:                    strlcat(tmp, cvs_modestr[gm], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     171:                        fatal("cvs_modetostr: overflow for group mode");
                    172:
1.68      xsa       173:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     174:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       175:        }
1.65      joris     176:
1.1       jfb       177:        if (om) {
1.65      joris     178:                if (um || gm) {
1.68      xsa       179:                        if (strlcat(buf, ",", len) >= len)
1.65      joris     180:                                fatal("cvs_modetostr: string truncation");
                    181:                }
                    182:
1.68      xsa       183:                if (strlcpy(tmp, "o=", sizeof(tmp)) >= sizeof(tmp) ||
                    184:                    strlcat(tmp, cvs_modestr[gm], sizeof(tmp)) >= sizeof(tmp))
1.65      joris     185:                        fatal("cvs_modetostr: overflow for others mode");
                    186:
1.68      xsa       187:                if (strlcat(buf, tmp, len) >= len)
1.65      joris     188:                        fatal("cvs_modetostr: string truncation");
1.1       jfb       189:        }
                    190: }
                    191:
                    192: /*
                    193:  * cvs_cksum()
                    194:  *
                    195:  * Calculate the MD5 checksum of the file whose path is <file> and generate
                    196:  * a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
                    197:  * given in <len> and must be at least 33.
                    198:  * Returns 0 on success, or -1 on failure.
                    199:  */
                    200: int
                    201: cvs_cksum(const char *file, char *dst, size_t len)
                    202: {
                    203:        if (len < CVS_CKSUM_LEN) {
1.79      joris     204:                cvs_log(LP_ERR, "buffer too small for checksum");
1.1       jfb       205:                return (-1);
                    206:        }
                    207:        if (MD5File(file, dst) == NULL) {
1.79      joris     208:                cvs_log(LP_ERR, "failed to generate checksum for %s", file);
1.1       jfb       209:                return (-1);
                    210:        }
                    211:
                    212:        return (0);
                    213: }
                    214:
                    215: /*
                    216:  * cvs_splitpath()
                    217:  *
1.7       jfb       218:  * Split a path <path> into the base portion and the filename portion.
                    219:  * The path is copied in <base> and the last delimiter is replaced by a NUL
                    220:  * byte.  The <file> pointer is set to point to the first character after
                    221:  * that delimiter.
1.1       jfb       222:  * Returns 0 on success, or -1 on failure.
                    223:  */
1.65      joris     224: void
1.7       jfb       225: cvs_splitpath(const char *path, char *base, size_t blen, char **file)
1.1       jfb       226: {
                    227:        size_t rlen;
1.7       jfb       228:        char *sp;
1.1       jfb       229:
1.7       jfb       230:        if ((rlen = strlcpy(base, path, blen)) >= blen)
1.60      xsa       231:                fatal("cvs_splitpath: path truncation");
1.6       jfb       232:
1.78      deraadt   233:        while (rlen > 0 && base[rlen - 1] == '/')
1.7       jfb       234:                base[--rlen] = '\0';
                    235:
                    236:        sp = strrchr(base, '/');
1.1       jfb       237:        if (sp == NULL) {
1.65      joris     238:                rlen = strlcpy(base, "./", blen);
                    239:                if (rlen >= blen)
                    240:                        fatal("cvs_splitpath: path truncation");
                    241:
                    242:                rlen = strlcat(base, path, blen);
                    243:                if (rlen >= blen)
                    244:                        fatal("cvs_splitpath: path truncation");
                    245:
1.7       jfb       246:                sp = base + 1;
1.1       jfb       247:        }
                    248:
1.7       jfb       249:        *sp = '\0';
                    250:        if (file != NULL)
                    251:                *file = sp + 1;
1.1       jfb       252: }
                    253:
                    254: /*
                    255:  * cvs_getargv()
                    256:  *
                    257:  * Parse a line contained in <line> and generate an argument vector by
                    258:  * splitting the line on spaces and tabs.  The resulting vector is stored in
                    259:  * <argv>, which can accept up to <argvlen> entries.
1.20      david     260:  * Returns the number of arguments in the vector, or -1 if an error occurred.
1.1       jfb       261:  */
                    262: int
                    263: cvs_getargv(const char *line, char **argv, int argvlen)
                    264: {
1.65      joris     265:        size_t l;
1.1       jfb       266:        u_int i;
1.67      xsa       267:        int argc, error;
1.1       jfb       268:        char linebuf[256], qbuf[128], *lp, *cp, *arg;
                    269:
1.65      joris     270:        l = strlcpy(linebuf, line, sizeof(linebuf));
                    271:        if (l >= sizeof(linebuf))
                    272:                fatal("cvs_getargv: string truncation");
                    273:
1.27      pat       274:        memset(argv, 0, argvlen * sizeof(char *));
1.1       jfb       275:        argc = 0;
                    276:
                    277:        /* build the argument vector */
1.67      xsa       278:        error = 0;
1.1       jfb       279:        for (lp = linebuf; lp != NULL;) {
                    280:                if (*lp == '"') {
                    281:                        /* double-quoted string */
                    282:                        lp++;
                    283:                        i = 0;
                    284:                        memset(qbuf, 0, sizeof(qbuf));
                    285:                        while (*lp != '"') {
1.16      jfb       286:                                if (*lp == '\\')
                    287:                                        lp++;
1.1       jfb       288:                                if (*lp == '\0') {
                    289:                                        cvs_log(LP_ERR, "no terminating quote");
1.67      xsa       290:                                        error++;
1.1       jfb       291:                                        break;
1.16      jfb       292:                                }
1.1       jfb       293:
1.9       jfb       294:                                qbuf[i++] = *lp++;
1.1       jfb       295:                                if (i == sizeof(qbuf)) {
1.67      xsa       296:                                        error++;
1.1       jfb       297:                                        break;
                    298:                                }
                    299:                        }
                    300:
                    301:                        arg = qbuf;
1.15      deraadt   302:                } else {
1.1       jfb       303:                        cp = strsep(&lp, " \t");
                    304:                        if (cp == NULL)
                    305:                                break;
                    306:                        else if (*cp == '\0')
                    307:                                continue;
                    308:
                    309:                        arg = cp;
                    310:                }
                    311:
1.16      jfb       312:                if (argc == argvlen) {
1.67      xsa       313:                        error++;
1.16      jfb       314:                        break;
                    315:                }
                    316:
1.59      joris     317:                argv[argc] = xstrdup(arg);
1.1       jfb       318:                argc++;
                    319:        }
                    320:
1.67      xsa       321:        if (error != 0) {
1.1       jfb       322:                /* ditch the argument vector */
                    323:                for (i = 0; i < (u_int)argc; i++)
1.59      joris     324:                        xfree(argv[i]);
1.1       jfb       325:                argc = -1;
                    326:        }
                    327:
                    328:        return (argc);
1.17      jfb       329: }
                    330:
                    331: /*
                    332:  * cvs_makeargv()
                    333:  *
1.20      david     334:  * Allocate an argument vector large enough to accommodate for all the
1.17      jfb       335:  * arguments found in <line> and return it.
                    336:  */
1.42      xsa       337: char **
1.17      jfb       338: cvs_makeargv(const char *line, int *argc)
                    339: {
                    340:        int i, ret;
                    341:        char *argv[1024], **copy;
                    342:
                    343:        ret = cvs_getargv(line, argv, 1024);
                    344:        if (ret == -1)
                    345:                return (NULL);
                    346:
1.77      ray       347:        copy = xcalloc(ret + 1, sizeof(char *));
1.17      jfb       348:
                    349:        for (i = 0; i < ret; i++)
                    350:                copy[i] = argv[i];
                    351:        copy[ret] = NULL;
                    352:
                    353:        *argc = ret;
                    354:        return (copy);
1.1       jfb       355: }
                    356:
                    357: /*
                    358:  * cvs_freeargv()
                    359:  *
                    360:  * Free an argument vector previously generated by cvs_getargv().
                    361:  */
                    362: void
                    363: cvs_freeargv(char **argv, int argc)
                    364: {
                    365:        int i;
                    366:
                    367:        for (i = 0; i < argc; i++)
1.16      jfb       368:                if (argv[i] != NULL)
1.59      joris     369:                        xfree(argv[i]);
1.2       jfb       370: }
                    371:
1.11      krapht    372: /*
                    373:  * cvs_exec()
                    374:  */
                    375: int
1.96      joris     376: cvs_exec(int argc, char **argv)
1.11      krapht    377: {
                    378:        int ret;
                    379:        pid_t pid;
                    380:
                    381:        if ((pid = fork()) == -1) {
1.79      joris     382:                cvs_log(LP_ERR, "failed to fork");
1.11      krapht    383:                return (-1);
                    384:        } else if (pid == 0) {
                    385:                execvp(argv[0], argv);
1.79      joris     386:                cvs_log(LP_ERR, "failed to exec %s", argv[0]);
1.13      jfb       387:                exit(1);
1.11      krapht    388:        }
                    389:
                    390:        if (waitpid(pid, &ret, 0) == -1)
1.79      joris     391:                cvs_log(LP_ERR, "failed to waitpid");
1.11      krapht    392:
                    393:        return (ret);
1.38      xsa       394: }
                    395:
                    396: /*
                    397:  * cvs_chdir()
                    398:  *
1.63      xsa       399:  * Change to directory <path>.
                    400:  * If <rm> is equal to `1', <path> is removed if chdir() fails so we
                    401:  * do not have temporary directories leftovers.
1.60      xsa       402:  * Returns 0 on success.
1.50      xsa       403:  */
1.38      xsa       404: int
1.63      xsa       405: cvs_chdir(const char *path, int rm)
1.38      xsa       406: {
1.63      xsa       407:        if (chdir(path) == -1) {
                    408:                if (rm == 1)
                    409:                        cvs_unlink(path);
1.60      xsa       410:                fatal("cvs_chdir: `%s': %s", path, strerror(errno));
1.63      xsa       411:        }
1.49      xsa       412:
                    413:        return (0);
                    414: }
                    415:
                    416: /*
                    417:  * cvs_rename()
                    418:  * Change the name of a file.
                    419:  * rename() wrapper with an error message.
1.60      xsa       420:  * Returns 0 on success.
1.49      xsa       421:  */
                    422: int
                    423: cvs_rename(const char *from, const char *to)
                    424: {
1.90      joris     425:        if (cvs_server_active == 0)
                    426:                cvs_log(LP_TRACE, "cvs_rename(%s,%s)", from, to);
1.49      xsa       427:
                    428:        if (cvs_noexec == 1)
                    429:                return (0);
                    430:
1.60      xsa       431:        if (rename(from, to) == -1)
                    432:                fatal("cvs_rename: `%s'->`%s': %s", from, to, strerror(errno));
1.40      xsa       433:
                    434:        return (0);
                    435: }
                    436:
                    437: /*
                    438:  * cvs_unlink()
                    439:  *
                    440:  * Removes the link named by <path>.
                    441:  * unlink() wrapper with an error message.
                    442:  * Returns 0 on success, or -1 on failure.
                    443:  */
                    444: int
                    445: cvs_unlink(const char *path)
                    446: {
1.90      joris     447:        if (cvs_server_active == 0)
                    448:                cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
1.40      xsa       449:
                    450:        if (cvs_noexec == 1)
                    451:                return (0);
                    452:
1.78      deraadt   453:        if (unlink(path) == -1 && errno != ENOENT) {
1.94      xsa       454:                cvs_log(LP_ERRNO, "%s", path);
1.38      xsa       455:                return (-1);
                    456:        }
                    457:
                    458:        return (0);
1.1       jfb       459: }
1.24      joris     460:
                    461: /*
1.45      xsa       462:  * cvs_rmdir()
1.30      jfb       463:  *
                    464:  * Remove a directory tree from disk.
                    465:  * Returns 0 on success, or -1 on failure.
1.24      joris     466:  */
                    467: int
1.45      xsa       468: cvs_rmdir(const char *path)
1.24      joris     469: {
1.99      todd      470:        int type, ret = -1;
1.24      joris     471:        DIR *dirp;
                    472:        struct dirent *ent;
1.99      todd      473:        struct stat st;
1.24      joris     474:        char fpath[MAXPATHLEN];
1.46      xsa       475:
1.90      joris     476:        if (cvs_server_active == 0)
                    477:                cvs_log(LP_TRACE, "cvs_rmdir(%s)", path);
1.46      xsa       478:
                    479:        if (cvs_noexec == 1)
                    480:                return (0);
1.24      joris     481:
                    482:        if ((dirp = opendir(path)) == NULL) {
1.79      joris     483:                cvs_log(LP_ERR, "failed to open '%s'", path);
1.30      jfb       484:                return (-1);
1.24      joris     485:        }
                    486:
                    487:        while ((ent = readdir(dirp)) != NULL) {
                    488:                if (!strcmp(ent->d_name, ".") ||
                    489:                    !strcmp(ent->d_name, ".."))
                    490:                        continue;
                    491:
1.105     xsa       492:                (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
                    493:                    path, ent->d_name);
1.24      joris     494:
1.99      todd      495:                if (ent->d_type == DT_UNKNOWN) {
1.104     todd      496:                        if (lstat(fpath, &st) == -1)
1.99      todd      497:                                fatal("'%s': %s", fpath, strerror(errno));
                    498:
                    499:                        switch (st.st_mode & S_IFMT) {
                    500:                        case S_IFDIR:
                    501:                                type = CVS_DIR;
                    502:                                break;
                    503:                        case S_IFREG:
                    504:                                type = CVS_FILE;
                    505:                                break;
                    506:                        default:
                    507:                                fatal("'%s': Unknown file type in copy",
                    508:                                    fpath);
                    509:                        }
                    510:                } else {
                    511:                        switch (ent->d_type) {
                    512:                        case DT_DIR:
                    513:                                type = CVS_DIR;
                    514:                                break;
                    515:                        case DT_REG:
                    516:                                type = CVS_FILE;
                    517:                                break;
                    518:                        default:
                    519:                                fatal("'%s': Unknown file type in copy",
                    520:                                    fpath);
                    521:                        }
                    522:                }
                    523:                switch (type) {
                    524:                case CVS_DIR:
1.45      xsa       525:                        if (cvs_rmdir(fpath) == -1)
1.33      pat       526:                                goto done;
1.99      todd      527:                        break;
                    528:                case CVS_FILE:
                    529:                        if (cvs_unlink(fpath) == -1 && errno != ENOENT)
                    530:                                goto done;
                    531:                        break;
                    532:                default:
                    533:                        fatal("type %d unknown, shouldn't happen", type);
                    534:                }
1.24      joris     535:        }
                    536:
                    537:
1.78      deraadt   538:        if (rmdir(path) == -1 && errno != ENOENT) {
1.93      xsa       539:                cvs_log(LP_ERRNO, "%s", path);
1.33      pat       540:                goto done;
1.30      jfb       541:        }
1.24      joris     542:
1.33      pat       543:        ret = 0;
                    544: done:
                    545:        closedir(dirp);
1.34      joris     546:        return (ret);
                    547: }
                    548:
1.51      xsa       549: void
1.81      joris     550: cvs_get_repository_path(const char *dir, char *dst, size_t len)
                    551: {
                    552:        char buf[MAXPATHLEN];
                    553:
                    554:        cvs_get_repository_name(dir, buf, sizeof(buf));
1.105     xsa       555:        (void)xsnprintf(dst, len, "%s/%s", current_cvsroot->cr_dir, buf);
1.81      joris     556: }
                    557:
                    558: void
                    559: cvs_get_repository_name(const char *dir, char *dst, size_t len)
1.51      xsa       560: {
                    561:        FILE *fp;
1.81      joris     562:        char *s, fpath[MAXPATHLEN];
1.79      joris     563:
1.105     xsa       564:        (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
                    565:            dir, CVS_PATH_REPOSITORY);
1.79      joris     566:
                    567:        if ((fp = fopen(fpath, "r")) != NULL) {
1.101     thib      568:                if ((fgets(dst, len, fp)) == NULL)
                    569:                        fatal("cvs_get_repository_name: bad repository file");
1.51      xsa       570:
1.81      joris     571:                if ((s = strchr(dst, '\n')) != NULL)
1.79      joris     572:                        *s = '\0';
1.51      xsa       573:
                    574:                (void)fclose(fp);
1.81      joris     575:        } else {
1.84      joris     576:                dst[0] = '\0';
                    577:
                    578:                if (cvs_cmdop == CVS_OP_IMPORT) {
                    579:                        if (strlcpy(dst, import_repository, len) >= len)
1.85      joris     580:                                fatal("cvs_get_repository_name: truncation");
1.84      joris     581:                        if (strlcat(dst, "/", len) >= len)
1.85      joris     582:                                fatal("cvs_get_repository_name: truncation");
                    583:
                    584:                        if (strcmp(dir, ".")) {
                    585:                                if (strlcat(dst, dir, len) >= len) {
                    586:                                        fatal("cvs_get_repository_name: "
                    587:                                            "truncation");
                    588:                                }
                    589:                        }
                    590:                } else {
1.98      joris     591:                        if (cvs_cmdop != CVS_OP_CHECKOUT) {
                    592:                                if (strlcat(dst, dir, len) >= len)
                    593:                                        fatal("cvs_get_repository_name: "
                    594:                                            "truncation");
                    595:                        }
1.84      joris     596:                }
1.51      xsa       597:        }
                    598: }
                    599:
                    600: void
1.87      xsa       601: cvs_mkadmin(const char *path, const char *root, const char *repo,
                    602:     char *tag, char *date, int nb)
1.51      xsa       603: {
                    604:        FILE *fp;
1.79      joris     605:        struct stat st;
                    606:        char buf[MAXPATHLEN];
1.51      xsa       607:
1.90      joris     608:        if (cvs_server_active == 0)
                    609:                cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s, %s, %s, %d)",
                    610:                    path, root, repo, (tag != NULL) ? tag : "",
                    611:                    (date != NULL) ? date : "", nb);
1.51      xsa       612:
1.105     xsa       613:        (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_CVSDIR);
1.51      xsa       614:
1.79      joris     615:        if (stat(buf, &st) != -1)
                    616:                return;
1.51      xsa       617:
1.79      joris     618:        if (mkdir(buf, 0755) == -1 && errno != EEXIST)
                    619:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
                    620:
1.105     xsa       621:        (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_ROOTSPEC);
1.79      joris     622:
                    623:        if ((fp = fopen(buf, "w")) == NULL)
                    624:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
                    625:
                    626:        fprintf(fp, "%s\n", root);
                    627:        (void)fclose(fp);
1.51      xsa       628:
1.105     xsa       629:        (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_REPOSITORY);
1.51      xsa       630:
1.79      joris     631:        if ((fp = fopen(buf, "w")) == NULL)
                    632:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51      xsa       633:
1.79      joris     634:        fprintf(fp, "%s\n", repo);
                    635:        (void)fclose(fp);
1.51      xsa       636:
1.105     xsa       637:        (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_ENTRIES);
1.51      xsa       638:
1.79      joris     639:        if ((fp = fopen(buf, "w")) == NULL)
                    640:                fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51      xsa       641:        (void)fclose(fp);
1.87      xsa       642:
                    643:        cvs_write_tagfile(path, tag, date, nb);
1.54      joris     644: }
                    645:
1.79      joris     646: void
                    647: cvs_mkpath(const char *path)
1.72      niallo    648: {
1.79      joris     649:        FILE *fp;
                    650:        size_t len;
                    651:        char *sp, *dp, *dir, rpath[MAXPATHLEN], repo[MAXPATHLEN];
                    652:
                    653:        dir = xstrdup(path);
                    654:
                    655:        STRIP_SLASH(dir);
1.90      joris     656:
                    657:        if (cvs_server_active == 0)
                    658:                cvs_log(LP_TRACE, "cvs_mkpath(%s)", dir);
1.72      niallo    659:
1.79      joris     660:        repo[0] = '\0';
                    661:        rpath[0] = '\0';
1.72      niallo    662:
1.79      joris     663:        if (cvs_cmdop == CVS_OP_UPDATE) {
                    664:                if ((fp = fopen(CVS_PATH_REPOSITORY, "r")) != NULL) {
1.91      thib      665:                        if ((fgets(repo, sizeof(repo), fp)) == NULL)
                    666:                                fatal("cvs_mkpath: bad repository file");
                    667:                        if ((len = strlen(repo)) && repo[len - 1] == '\n')
                    668:                                repo[len - 1] = '\0';
1.79      joris     669:                        (void)fclose(fp);
                    670:                }
1.72      niallo    671:        }
                    672:
1.79      joris     673:        for (sp = dir; sp != NULL; sp = dp) {
                    674:                dp = strchr(sp, '/');
                    675:                if (dp != NULL)
                    676:                        *(dp++) = '\0';
                    677:
                    678:                if (repo[0] != '\0') {
                    679:                        len = strlcat(repo, "/", sizeof(repo));
                    680:                        if (len >= (int)sizeof(repo))
                    681:                                fatal("cvs_mkpath: overflow");
                    682:                }
                    683:
                    684:                len = strlcat(repo, sp, sizeof(repo));
                    685:                if (len >= (int)sizeof(repo))
                    686:                        fatal("cvs_mkpath: overflow");
                    687:
                    688:                if (rpath[0] != '\0') {
                    689:                        len = strlcat(rpath, "/", sizeof(rpath));
                    690:                        if (len >= (int)sizeof(rpath))
                    691:                                fatal("cvs_mkpath: overflow");
                    692:                }
                    693:
                    694:                len = strlcat(rpath, sp, sizeof(rpath));
                    695:                if (len >= (int)sizeof(rpath))
                    696:                        fatal("cvs_mkpath: overflow");
                    697:
                    698:                if (mkdir(rpath, 0755) == -1 && errno != EEXIST)
                    699:                        fatal("cvs_mkpath: %s: %s", rpath, strerror(errno));
1.72      niallo    700:
1.98      joris     701:                cvs_mkadmin(rpath, current_cvsroot->cr_str, repo,
1.87      xsa       702:                    NULL, NULL, 0);
1.79      joris     703:        }
1.72      niallo    704:
1.79      joris     705:        xfree(dir);
1.72      niallo    706: }
1.54      joris     707:
                    708: /*
                    709:  * Split the contents of a file into a list of lines.
                    710:  */
                    711: struct cvs_lines *
1.106     otto      712: cvs_splitlines(u_char *data, size_t len)
1.54      joris     713: {
1.97      niallo    714:        u_char *p, *c;
                    715:        size_t i, tlen;
1.54      joris     716:        struct cvs_lines *lines;
                    717:        struct cvs_line *lp;
                    718:
1.77      ray       719:        lines = xmalloc(sizeof(*lines));
1.97      niallo    720:        memset(lines, 0, sizeof(*lines));
1.54      joris     721:        TAILQ_INIT(&(lines->l_lines));
                    722:
1.77      ray       723:        lp = xmalloc(sizeof(*lp));
1.97      niallo    724:        memset(lp, 0, sizeof(*lp));
1.54      joris     725:        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    726:
1.97      niallo    727:        p = c = data;
                    728:        for (i = 0; i < len; i++) {
                    729:                if (*p == '\n' || (i == len - 1)) {
                    730:                        tlen = p - c + 1;
                    731:                        lp = xmalloc(sizeof(*lp));
1.102     niallo    732:                        memset(lp, 0, sizeof(*lp));
1.97      niallo    733:                        lp->l_line = c;
                    734:                        lp->l_len = tlen;
                    735:                        lp->l_lineno = ++(lines->l_nblines);
                    736:                        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    737:                        c = p + 1;
                    738:                }
                    739:                p++;
1.54      joris     740:        }
                    741:
                    742:        return (lines);
                    743: }
                    744:
                    745: void
                    746: cvs_freelines(struct cvs_lines *lines)
                    747: {
                    748:        struct cvs_line *lp;
                    749:
                    750:        while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
                    751:                TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.102     niallo    752:                if (lp->l_needsfree == 1)
                    753:                        xfree(lp->l_line);
1.59      joris     754:                xfree(lp);
1.54      joris     755:        }
                    756:
1.59      joris     757:        xfree(lines);
1.54      joris     758: }
                    759:
                    760: BUF *
1.106     otto      761: cvs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
1.54      joris     762:     int (*p)(struct cvs_lines *, struct cvs_lines *))
                    763: {
                    764:        struct cvs_lines *dlines, *plines;
                    765:        struct cvs_line *lp;
                    766:        BUF *res;
                    767:
1.97      niallo    768:        if ((dlines = cvs_splitlines(data, dlen)) == NULL)
1.54      joris     769:                return (NULL);
                    770:
1.97      niallo    771:        if ((plines = cvs_splitlines(patch, plen)) == NULL)
1.54      joris     772:                return (NULL);
                    773:
                    774:        if (p(dlines, plines) < 0) {
                    775:                cvs_freelines(dlines);
                    776:                cvs_freelines(plines);
                    777:                return (NULL);
                    778:        }
                    779:
1.97      niallo    780:        res = cvs_buf_alloc(1024, BUF_AUTOEXT);
1.54      joris     781:        TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
1.97      niallo    782:                if (lp->l_line == NULL)
                    783:                        continue;
                    784:                cvs_buf_append(res, lp->l_line, lp->l_len);
1.54      joris     785:        }
                    786:
                    787:        cvs_freelines(dlines);
                    788:        cvs_freelines(plines);
                    789:        return (res);
1.70      niallo    790: }
                    791:
1.71      xsa       792: /*
                    793:  * cvs_strsplit()
                    794:  *
                    795:  * Split a string <str> of <sep>-separated values and allocate
                    796:  * an argument vector for the values found.
                    797:  */
1.73      pat       798: struct cvs_argvector *
1.71      xsa       799: cvs_strsplit(char *str, const char *sep)
                    800: {
1.73      pat       801:        struct cvs_argvector *av;
1.76      ray       802:        size_t i = 0;
1.73      pat       803:        char **nargv;
1.71      xsa       804:        char *cp, *p;
                    805:
                    806:        cp = xstrdup(str);
1.77      ray       807:        av = xmalloc(sizeof(*av));
1.73      pat       808:        av->str = cp;
1.77      ray       809:        av->argv = xcalloc(i + 1, sizeof(*(av->argv)));
1.71      xsa       810:
                    811:        while ((p = strsep(&cp, sep)) != NULL) {
1.73      pat       812:                av->argv[i++] = p;
1.77      ray       813:                nargv = xrealloc(av->argv,
                    814:                    i + 1, sizeof(*(av->argv)));
1.73      pat       815:                av->argv = nargv;
1.71      xsa       816:        }
1.73      pat       817:        av->argv[i] = NULL;
                    818:
                    819:        return (av);
                    820: }
1.71      xsa       821:
1.73      pat       822: /*
                    823:  * cvs_argv_destroy()
                    824:  *
                    825:  * Free an argument vector previously allocated by cvs_strsplit().
                    826:  */
                    827: void
                    828: cvs_argv_destroy(struct cvs_argvector *av)
                    829: {
                    830:        xfree(av->str);
                    831:        xfree(av->argv);
                    832:        xfree(av);
1.82      joris     833: }
                    834:
                    835: u_int
                    836: cvs_revision_select(RCSFILE *file, char *range)
                    837: {
                    838:        int i;
                    839:        u_int nrev;
                    840:        char *lstr, *rstr;
                    841:        struct rcs_delta *rdp;
                    842:        struct cvs_argvector *revargv, *revrange;
                    843:        RCSNUM *lnum, *rnum;
                    844:
                    845:        nrev = 0;
                    846:        lnum = rnum = NULL;
                    847:
                    848:        revargv = cvs_strsplit(range, ",");
                    849:        for (i = 0; revargv->argv[i] != NULL; i++) {
                    850:                revrange = cvs_strsplit(revargv->argv[i], ":");
                    851:                if (revrange->argv[0] == NULL)
                    852:                        fatal("invalid revision range: %s", revargv->argv[i]);
                    853:                else if (revrange->argv[1] == NULL)
                    854:                        lstr = rstr = revrange->argv[0];
                    855:                else {
                    856:                        if (revrange->argv[2] != NULL)
                    857:                                fatal("invalid revision range: %s",
                    858:                                    revargv->argv[i]);
                    859:
                    860:                        lstr = revrange->argv[0];
                    861:                        rstr = revrange->argv[1];
                    862:
                    863:                        if (strcmp(lstr, "") == 0)
                    864:                                lstr = NULL;
                    865:                        if (strcmp(rstr, "") == 0)
                    866:                                rstr = NULL;
                    867:                }
                    868:
                    869:                if (lstr == NULL)
                    870:                        lstr = RCS_HEAD_INIT;
                    871:
1.100     niallo    872:                if ((lnum = rcs_translate_tag(lstr, file)) == NULL)
                    873:                        fatal("cvs_revision_select: could not translate tag `%s'", lstr);
1.82      joris     874:
                    875:                if (rstr != NULL) {
1.100     niallo    876:                        if ((rnum = rcs_translate_tag(rstr, file)) == NULL)
                    877:                                fatal("cvs_revision_select: could not translate tag `%s'", rstr);
1.82      joris     878:                } else {
                    879:                        rnum = rcsnum_alloc();
                    880:                        rcsnum_cpy(file->rf_head, rnum, 0);
                    881:                }
                    882:
                    883:                cvs_argv_destroy(revrange);
                    884:
                    885:                TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
                    886:                        if (rcsnum_cmp(rdp->rd_num, lnum, 0) <= 0 &&
                    887:                            rcsnum_cmp(rdp->rd_num, rnum, 0) >= 0 &&
                    888:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    889:                                rdp->rd_flags |= RCS_RD_SELECT;
                    890:                                nrev++;
                    891:                        }
                    892:                }
                    893:        }
                    894:
                    895:        cvs_argv_destroy(revargv);
                    896:
                    897:        if (lnum != NULL)
                    898:                rcsnum_free(lnum);
                    899:        if (rnum != NULL)
                    900:                rcsnum_free(rnum);
                    901:
                    902:        return (nrev);
1.95      xsa       903: }
                    904:
                    905: int
                    906: cvs_yesno(void)
                    907: {
                    908:        int c, ret;
                    909:
                    910:        ret = 0;
                    911:
                    912:        fflush(stderr);
                    913:        fflush(stdout);
                    914:
                    915:        if ((c = getchar()) != 'y' && c != 'Y')
                    916:                ret = -1;
                    917:        else
                    918:                while (c != EOF && c != '\n')
                    919:                        c = getchar();
                    920:
                    921:        return (ret);
1.71      xsa       922: }