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

1.59    ! joris       1: /*     $OpenBSD: util.c,v 1.58 2005/12/03 15:07:21 joris 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:
1.54      joris      42: #if !defined(RCSPROG)
                     43:
1.1       jfb        44: /* letter -> mode type map */
                     45: static const int cvs_modetypes[26] = {
                     46:        -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1,
                     47:        -1,  2, -1, -1, -1, -1, -1,  0, -1, -1, -1, -1, -1,
                     48: };
                     49:
                     50: /* letter -> mode map */
                     51: static const mode_t cvs_modes[3][26] = {
                     52:        {
                     53:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     54:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     55:                0,  0,       0,       S_IRUSR, 0,  0,  0,    /* n - u */
                     56:                0,  S_IWUSR, S_IXUSR, 0,       0             /* v - z */
                     57:        },
                     58:        {
                     59:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     60:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     61:                0,  0,       0,       S_IRGRP, 0,  0,  0,    /* n - u */
                     62:                0,  S_IWGRP, S_IXGRP, 0,       0             /* v - z */
                     63:        },
                     64:        {
                     65:                0,  0,       0,       0,       0,  0,  0,    /* a - g */
                     66:                0,  0,       0,       0,       0,  0,  0,    /* h - m */
                     67:                0,  0,       0,       S_IROTH, 0,  0,  0,    /* n - u */
                     68:                0,  S_IWOTH, S_IXOTH, 0,       0             /* v - z */
                     69:        }
                     70: };
                     71:
                     72:
                     73: /* octal -> string */
                     74: static const char *cvs_modestr[8] = {
                     75:        "", "x", "w", "wx", "r", "rx", "rw", "rwx"
                     76: };
                     77:
1.11      krapht     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:  * cvs_getargv()
                    286:  *
                    287:  * Parse a line contained in <line> and generate an argument vector by
                    288:  * splitting the line on spaces and tabs.  The resulting vector is stored in
                    289:  * <argv>, which can accept up to <argvlen> entries.
1.20      david     290:  * Returns the number of arguments in the vector, or -1 if an error occurred.
1.1       jfb       291:  */
                    292: int
                    293: cvs_getargv(const char *line, char **argv, int argvlen)
                    294: {
                    295:        u_int i;
                    296:        int argc, err;
                    297:        char linebuf[256], qbuf[128], *lp, *cp, *arg;
                    298:
                    299:        strlcpy(linebuf, line, sizeof(linebuf));
1.27      pat       300:        memset(argv, 0, argvlen * sizeof(char *));
1.1       jfb       301:        argc = 0;
                    302:
                    303:        /* build the argument vector */
                    304:        err = 0;
                    305:        for (lp = linebuf; lp != NULL;) {
                    306:                if (*lp == '"') {
                    307:                        /* double-quoted string */
                    308:                        lp++;
                    309:                        i = 0;
                    310:                        memset(qbuf, 0, sizeof(qbuf));
                    311:                        while (*lp != '"') {
1.16      jfb       312:                                if (*lp == '\\')
                    313:                                        lp++;
1.1       jfb       314:                                if (*lp == '\0') {
                    315:                                        cvs_log(LP_ERR, "no terminating quote");
                    316:                                        err++;
                    317:                                        break;
1.16      jfb       318:                                }
1.1       jfb       319:
1.9       jfb       320:                                qbuf[i++] = *lp++;
1.1       jfb       321:                                if (i == sizeof(qbuf)) {
                    322:                                        err++;
                    323:                                        break;
                    324:                                }
                    325:                        }
                    326:
                    327:                        arg = qbuf;
1.15      deraadt   328:                } else {
1.1       jfb       329:                        cp = strsep(&lp, " \t");
                    330:                        if (cp == NULL)
                    331:                                break;
                    332:                        else if (*cp == '\0')
                    333:                                continue;
                    334:
                    335:                        arg = cp;
                    336:                }
                    337:
1.16      jfb       338:                if (argc == argvlen) {
                    339:                        err++;
                    340:                        break;
                    341:                }
                    342:
1.59    ! joris     343:                argv[argc] = xstrdup(arg);
1.1       jfb       344:                argc++;
                    345:        }
                    346:
1.43      xsa       347:        if (err != 0) {
1.1       jfb       348:                /* ditch the argument vector */
                    349:                for (i = 0; i < (u_int)argc; i++)
1.59    ! joris     350:                        xfree(argv[i]);
1.1       jfb       351:                argc = -1;
                    352:        }
                    353:
                    354:        return (argc);
1.17      jfb       355: }
                    356:
                    357:
                    358: /*
                    359:  * cvs_makeargv()
                    360:  *
1.20      david     361:  * Allocate an argument vector large enough to accommodate for all the
1.17      jfb       362:  * arguments found in <line> and return it.
                    363:  */
1.42      xsa       364: char **
1.17      jfb       365: cvs_makeargv(const char *line, int *argc)
                    366: {
                    367:        int i, ret;
                    368:        char *argv[1024], **copy;
1.27      pat       369:        size_t size;
1.17      jfb       370:
                    371:        ret = cvs_getargv(line, argv, 1024);
                    372:        if (ret == -1)
                    373:                return (NULL);
                    374:
1.27      pat       375:        size = (ret + 1) * sizeof(char *);
1.59    ! joris     376:        copy = (char **)xmalloc(size);
1.27      pat       377:        memset(copy, 0, size);
1.17      jfb       378:
                    379:        for (i = 0; i < ret; i++)
                    380:                copy[i] = argv[i];
                    381:        copy[ret] = NULL;
                    382:
                    383:        *argc = ret;
                    384:        return (copy);
1.1       jfb       385: }
                    386:
                    387:
                    388: /*
                    389:  * cvs_freeargv()
                    390:  *
                    391:  * Free an argument vector previously generated by cvs_getargv().
                    392:  */
                    393: void
                    394: cvs_freeargv(char **argv, int argc)
                    395: {
                    396:        int i;
                    397:
                    398:        for (i = 0; i < argc; i++)
1.16      jfb       399:                if (argv[i] != NULL)
1.59    ! joris     400:                        xfree(argv[i]);
1.2       jfb       401: }
                    402:
                    403:
                    404: /*
                    405:  * cvs_mkadmin()
                    406:  *
1.5       jfb       407:  * Create the CVS administrative files within the directory <cdir>.  If the
                    408:  * files already exist, they are kept as is.
1.2       jfb       409:  * Returns 0 on success, or -1 on failure.
                    410:  */
                    411: int
1.52      xsa       412: cvs_mkadmin(const char *dpath, const char *rootpath, const char *repopath,
                    413:     char *tag, char *date, int nb)
1.2       jfb       414: {
1.37      xsa       415:        size_t l;
1.28      joris     416:        char path[MAXPATHLEN];
1.2       jfb       417:        FILE *fp;
                    418:        CVSENTRIES *ef;
1.5       jfb       419:        struct stat st;
1.13      jfb       420:
1.52      xsa       421:        cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s, %s, %s, %d)",
                    422:            dpath, rootpath, repopath, tag ? tag : "", date ? date : "", nb);
                    423:
1.37      xsa       424:        l = cvs_path_cat(dpath, CVS_PATH_CVSDIR, path, sizeof(path));
                    425:        if (l >= sizeof(path))
1.21      xsa       426:                return (-1);
                    427:
1.28      joris     428:        if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) {
1.2       jfb       429:                cvs_log(LP_ERRNO, "failed to create directory %s", path);
                    430:                return (-1);
                    431:        }
                    432:
1.5       jfb       433:        /* just create an empty Entries file */
1.13      jfb       434:        ef = cvs_ent_open(dpath, O_WRONLY);
1.48      moritz    435:        if (ef != NULL)
                    436:                cvs_ent_close(ef);
1.2       jfb       437:
1.37      xsa       438:        l = cvs_path_cat(dpath, CVS_PATH_ROOTSPEC, path, sizeof(path));
                    439:        if (l >= sizeof(path))
1.21      xsa       440:                return (-1);
                    441:
1.47      xsa       442:        if ((stat(path, &st) == -1) && (errno == ENOENT)) {
1.5       jfb       443:                fp = fopen(path, "w");
                    444:                if (fp == NULL) {
                    445:                        cvs_log(LP_ERRNO, "failed to open %s", path);
                    446:                        return (-1);
                    447:                }
1.28      joris     448:                if (rootpath != NULL)
                    449:                        fprintf(fp, "%s\n", rootpath);
1.5       jfb       450:                (void)fclose(fp);
1.2       jfb       451:        }
                    452:
1.37      xsa       453:        l = cvs_path_cat(dpath, CVS_PATH_REPOSITORY, path, sizeof(path));
                    454:        if (l >= sizeof(path))
1.21      xsa       455:                return (-1);
                    456:
1.47      xsa       457:        if ((stat(path, &st) == -1) && (errno == ENOENT)) {
1.2       jfb       458:                fp = fopen(path, "w");
                    459:                if (fp == NULL) {
                    460:                        cvs_log(LP_ERRNO, "failed to open %s", path);
                    461:                        return (-1);
                    462:                }
1.28      joris     463:                if (repopath != NULL)
                    464:                        fprintf(fp, "%s\n", repopath);
1.2       jfb       465:                (void)fclose(fp);
                    466:        }
                    467:
1.52      xsa       468:        /* create CVS/Tag file (if needed) */
1.56      joris     469:        /* XXX correct? */
                    470:        if (tag != NULL || date != NULL)
                    471:                (void)cvs_write_tagfile(tag, date, nb);
1.52      xsa       472:
1.2       jfb       473:        return (0);
1.11      krapht    474: }
                    475:
                    476:
                    477: /*
                    478:  * cvs_exec()
                    479:  */
                    480: int
                    481: cvs_exec(int argc, char **argv, int fds[3])
                    482: {
                    483:        int ret;
                    484:        pid_t pid;
                    485:
                    486:        if ((pid = fork()) == -1) {
                    487:                cvs_log(LP_ERRNO, "failed to fork");
                    488:                return (-1);
                    489:        } else if (pid == 0) {
                    490:                execvp(argv[0], argv);
1.13      jfb       491:                cvs_log(LP_ERRNO, "failed to exec %s", argv[0]);
                    492:                exit(1);
1.11      krapht    493:        }
                    494:
                    495:        if (waitpid(pid, &ret, 0) == -1)
1.13      jfb       496:                cvs_log(LP_ERRNO, "failed to waitpid");
1.11      krapht    497:
                    498:        return (ret);
1.38      xsa       499: }
                    500:
                    501: /*
                    502:  * cvs_chdir()
                    503:  *
                    504:  * Change to directory.
                    505:  * chdir() wrapper with an error message.
                    506:  * Returns 0 on success, or -1 on failure.
1.50      xsa       507:  */
1.38      xsa       508: int
                    509: cvs_chdir(const char *path)
                    510: {
                    511:        if (chdir(path) == -1) {
                    512:                cvs_log(LP_ERRNO, "cannot change to dir `%s'", path);
1.49      xsa       513:                return (-1);
                    514:        }
                    515:
                    516:        return (0);
                    517: }
                    518:
                    519: /*
                    520:  * cvs_rename()
                    521:  * Change the name of a file.
                    522:  * rename() wrapper with an error message.
                    523:  * Returns 0 on success, or -1 on failure.
                    524:  */
                    525: int
                    526: cvs_rename(const char *from, const char *to)
                    527: {
                    528:        cvs_log(LP_TRACE, "cvs_rename(%s,%s)", from, to);
                    529:
                    530:        if (cvs_noexec == 1)
                    531:                return (0);
                    532:
                    533:        if (rename(from, to) == -1) {
                    534:                cvs_log(LP_ERRNO, "cannot rename file `%s' to `%s'", from, to);
1.40      xsa       535:                return (-1);
                    536:        }
                    537:
                    538:        return (0);
                    539: }
                    540:
                    541: /*
                    542:  * cvs_unlink()
                    543:  *
                    544:  * Removes the link named by <path>.
                    545:  * unlink() wrapper with an error message.
                    546:  * Returns 0 on success, or -1 on failure.
                    547:  */
                    548: int
                    549: cvs_unlink(const char *path)
                    550: {
                    551:        cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
                    552:
                    553:        if (cvs_noexec == 1)
                    554:                return (0);
                    555:
1.44      joris     556:        if ((unlink(path) == -1) && (errno != ENOENT)) {
1.40      xsa       557:                cvs_log(LP_ERRNO, "cannot remove `%s'", path);
1.38      xsa       558:                return (-1);
                    559:        }
                    560:
                    561:        return (0);
1.1       jfb       562: }
1.24      joris     563:
                    564: /*
1.45      xsa       565:  * cvs_rmdir()
1.30      jfb       566:  *
                    567:  * Remove a directory tree from disk.
                    568:  * Returns 0 on success, or -1 on failure.
1.24      joris     569:  */
                    570: int
1.45      xsa       571: cvs_rmdir(const char *path)
1.24      joris     572: {
1.33      pat       573:        int ret = -1;
1.30      jfb       574:        size_t len;
1.24      joris     575:        DIR *dirp;
                    576:        struct dirent *ent;
                    577:        char fpath[MAXPATHLEN];
1.46      xsa       578:
                    579:        cvs_log(LP_TRACE, "cvs_rmdir(%s)", path);
                    580:
                    581:        if (cvs_noexec == 1)
                    582:                return (0);
1.24      joris     583:
                    584:        if ((dirp = opendir(path)) == NULL) {
                    585:                cvs_log(LP_ERRNO, "failed to open '%s'", path);
1.30      jfb       586:                return (-1);
1.24      joris     587:        }
                    588:
                    589:        while ((ent = readdir(dirp)) != NULL) {
                    590:                if (!strcmp(ent->d_name, ".") ||
                    591:                    !strcmp(ent->d_name, ".."))
                    592:                        continue;
                    593:
1.30      jfb       594:                len = cvs_path_cat(path, ent->d_name, fpath, sizeof(fpath));
1.33      pat       595:                if (len >= sizeof(fpath))
                    596:                        goto done;
1.24      joris     597:
                    598:                if (ent->d_type == DT_DIR) {
1.45      xsa       599:                        if (cvs_rmdir(fpath) == -1)
1.33      pat       600:                                goto done;
1.41      xsa       601:                } else if ((cvs_unlink(fpath) == -1) && (errno != ENOENT))
1.33      pat       602:                        goto done;
1.24      joris     603:        }
                    604:
                    605:
1.30      jfb       606:        if ((rmdir(path) == -1) && (errno != ENOENT)) {
1.24      joris     607:                cvs_log(LP_ERRNO, "failed to remove '%s'", path);
1.33      pat       608:                goto done;
1.30      jfb       609:        }
1.24      joris     610:
1.33      pat       611:        ret = 0;
                    612: done:
                    613:        closedir(dirp);
1.34      joris     614:        return (ret);
                    615: }
                    616:
                    617: /*
                    618:  * Create a directory, and the parent directories if needed.
                    619:  * based upon mkpath() from mkdir.c
                    620:  */
                    621: int
                    622: cvs_create_dir(const char *path, int create_adm, char *root, char *repo)
                    623: {
                    624:        size_t l;
                    625:        int len, ret;
                    626:        char *d, *s;
                    627:        struct stat sb;
                    628:        char rpath[MAXPATHLEN], entry[MAXPATHLEN];
                    629:        CVSENTRIES *entf;
                    630:        struct cvs_ent *ent;
                    631:
1.56      joris     632:        if (create_adm == 1 && (root == NULL)) {
1.34      joris     633:                cvs_log(LP_ERR, "missing stuff in cvs_create_dir");
                    634:                return (-1);
                    635:        }
                    636:
1.59    ! joris     637:        s = xstrdup(path);
1.56      joris     638:        rpath[0] = '\0';
                    639:        if (repo != NULL) {
                    640:                if (strlcpy(rpath, repo, sizeof(rpath)) >= sizeof(rpath)) {
                    641:                        errno = ENAMETOOLONG;
                    642:                        cvs_log(LP_ERRNO, "%s", rpath);
1.59    ! joris     643:                        xfree(s);
1.56      joris     644:                        return (-1);
                    645:                }
                    646:
                    647:                if (strlcat(rpath, "/", sizeof(rpath)) >= sizeof(rpath)) {
                    648:                        errno = ENAMETOOLONG;
                    649:                        cvs_log(LP_ERRNO, "%s", rpath);
1.59    ! joris     650:                        xfree(s);
1.56      joris     651:                        return (-1);
                    652:                }
1.34      joris     653:        }
                    654:
                    655:        ret = -1;
                    656:        entf = NULL;
                    657:        d = strtok(s, "/");
                    658:        while (d != NULL) {
                    659:                if (stat(d, &sb)) {
                    660:                        /* try to create the directory */
                    661:                        if ((errno != ENOENT) || (mkdir(d, 0755) &&
                    662:                            errno != EEXIST)) {
                    663:                                cvs_log(LP_ERRNO, "failed to create `%s'", d);
                    664:                                goto done;
                    665:                        }
                    666:                } else if (!S_ISDIR(sb.st_mode)) {
                    667:                        cvs_log(LP_ERR, "`%s' not a directory", d);
                    668:                        goto done;
                    669:                }
                    670:
                    671:                /*
                    672:                 * Create administrative files if requested.
                    673:                 */
1.43      xsa       674:                if (create_adm == 1) {
1.34      joris     675:                        l = strlcat(rpath, d, sizeof(rpath));
                    676:                        if (l >= sizeof(rpath))
                    677:                                goto done;
                    678:
                    679:                        l = strlcat(rpath, "/", sizeof(rpath));
                    680:                        if (l >= sizeof(rpath))
                    681:                                goto done;
                    682:
1.52      xsa       683:                        if (cvs_mkadmin(d, root, rpath, NULL, NULL, 0) < 0) {
1.34      joris     684:                                cvs_log(LP_ERR, "failed to create adm files");
                    685:                                goto done;
                    686:                        }
                    687:                }
                    688:
                    689:                /*
                    690:                 * Add it to the parent directory entry file.
                    691:                 * (if any).
                    692:                 */
                    693:                entf = cvs_ent_open(".", O_RDWR);
                    694:                if (entf != NULL && strcmp(d, ".")) {
                    695:                        len = snprintf(entry, sizeof(entry), "D/%s////", d);
                    696:                        if (len == -1 || len >= (int)sizeof(entry)) {
                    697:                                errno = ENAMETOOLONG;
                    698:                                cvs_log(LP_ERRNO, "%s", entry);
                    699:                                goto done;
                    700:                        }
                    701:
                    702:                        if ((ent = cvs_ent_parse(entry)) == NULL) {
                    703:                                cvs_log(LP_ERR, "failed to parse entry");
                    704:                                goto done;
                    705:                        }
                    706:
1.56      joris     707:                        cvs_ent_remove(entf, d, 0);
1.34      joris     708:
                    709:                        if (cvs_ent_add(entf, ent) < 0) {
                    710:                                cvs_log(LP_ERR, "failed to add entry");
                    711:                                goto done;
                    712:                        }
                    713:                }
                    714:
                    715:                if (entf != NULL) {
                    716:                        cvs_ent_close(entf);
                    717:                        entf = NULL;
                    718:                }
                    719:
                    720:                /*
                    721:                 * All went ok, switch to the newly created directory.
                    722:                 */
1.39      xsa       723:                if (cvs_chdir(d) == -1)
1.34      joris     724:                        goto done;
                    725:
                    726:                d = strtok(NULL, "/");
                    727:        }
                    728:
                    729:        ret = 0;
                    730: done:
                    731:        if (entf != NULL)
                    732:                cvs_ent_close(entf);
1.59    ! joris     733:        xfree(s);
1.33      pat       734:        return (ret);
1.24      joris     735: }
                    736:
1.30      jfb       737: /*
                    738:  * cvs_path_cat()
                    739:  *
                    740:  * Concatenate the two paths <base> and <end> and store the generated path
                    741:  * into the buffer <dst>, which can accept up to <dlen> bytes, including the
                    742:  * NUL byte.  The result is guaranteed to be NUL-terminated.
                    743:  * Returns the number of bytes necessary to store the full resulting path,
                    744:  * not including the NUL byte (a value equal to or larger than <dlen>
                    745:  * indicates truncation).
                    746:  */
1.29      jfb       747: size_t
                    748: cvs_path_cat(const char *base, const char *end, char *dst, size_t dlen)
                    749: {
                    750:        size_t len;
                    751:
                    752:        len = strlcpy(dst, base, dlen);
                    753:        if (len >= dlen - 1) {
                    754:                errno = ENAMETOOLONG;
                    755:                cvs_log(LP_ERRNO, "%s", dst);
                    756:        } else {
                    757:                dst[len] = '/';
                    758:                dst[len + 1] = '\0';
                    759:                len = strlcat(dst, end, dlen);
                    760:                if (len >= dlen) {
                    761:                        errno = ENAMETOOLONG;
                    762:                        cvs_log(LP_ERRNO, "%s", dst);
                    763:                }
                    764:        }
                    765:
                    766:        return (len);
1.35      xsa       767: }
                    768:
                    769: /*
                    770:  * cvs_rcs_getpath()
                    771:  *
                    772:  * Get the RCS path of the file <file> and store it in <buf>, which is
                    773:  * of size <len>. For portability, it is recommended that <buf> always be
                    774:  * at least MAXPATHLEN bytes long.
                    775:  * Returns a pointer to the start of the path on success, or NULL on failure.
                    776:  */
1.42      xsa       777: char *
1.35      xsa       778: cvs_rcs_getpath(CVSFILE *file, char *buf, size_t len)
                    779: {
                    780:        int l;
                    781:        char *repo;
                    782:        struct cvsroot *root;
                    783:
                    784:        root = CVS_DIR_ROOT(file);
                    785:        repo = CVS_DIR_REPO(file);
                    786:
1.36      deraadt   787:        l = snprintf(buf, len, "%s/%s/%s%s",
1.35      xsa       788:            root->cr_dir, repo, file->cf_name, RCS_FILE_EXT);
                    789:        if (l == -1 || l >= (int)len) {
                    790:                errno = ENAMETOOLONG;
                    791:                cvs_log(LP_ERRNO, "%s", buf);
                    792:                return (NULL);
                    793:        }
                    794:
                    795:        return (buf);
1.51      xsa       796: }
                    797:
                    798: /*
                    799:  * cvs_write_tagfile()
                    800:  *
                    801:  * Write the CVS/Tag file for current directory.
1.55      xsa       802:  */
1.51      xsa       803: void
                    804: cvs_write_tagfile(char *tag, char *date, int nb)
                    805: {
                    806:        FILE *fp;
                    807:        char tagpath[MAXPATHLEN];
                    808:
                    809:        if (cvs_noexec == 1)
                    810:                return;
                    811:
                    812:        if (strlcpy(tagpath, CVS_PATH_TAG, sizeof(tagpath)) >= sizeof(tagpath))
                    813:                return;
                    814:
                    815:        if ((tag != NULL) || (date != NULL)) {
                    816:                fp = fopen(tagpath, "w+");
                    817:                if (fp == NULL) {
                    818:                        if (errno != ENOENT)
                    819:                                cvs_log(LP_NOTICE,
                    820:                                    "failed to open `%s' : %s", tagpath,
                    821:                                    strerror(errno));
                    822:                        return;
                    823:                }
                    824:                if (tag != NULL) {
                    825:                        if (nb != 0)
                    826:                                fprintf(fp, "N%s\n", tag);
                    827:                        else
                    828:                                fprintf(fp, "T%s\n", tag);
                    829:                } else {
                    830:                        fprintf(fp, "D%s\n", date);
                    831:                }
                    832:                (void)fclose(fp);
                    833:        } else {
                    834:                cvs_unlink(tagpath);
                    835:                return;
                    836:        }
                    837: }
                    838:
                    839: /*
                    840:  * cvs_parse_tagfile()
                    841:  *
                    842:  * Parse the CVS/Tag file for current directory.
                    843:  *
                    844:  * If it contains a branch tag, sets <tagp>.
                    845:  * If it contains a date, sets <datep>.
                    846:  * If it contains a non-branch tag, sets <nbp>.
1.55      xsa       847:  *
1.51      xsa       848:  * Returns nothing but an error message, and sets <tagp>, <datep> to NULL
                    849:  * and <nbp> to 0.
                    850:  */
                    851: void
                    852: cvs_parse_tagfile(char **tagp, char **datep, int *nbp)
                    853: {
                    854:        FILE *fp;
                    855:        int linenum;
                    856:        size_t len;
                    857:        char linebuf[128], tagpath[MAXPATHLEN];
                    858:
                    859:        if (tagp != NULL)
                    860:                *tagp = (char *)NULL;
                    861:
                    862:        if (datep != NULL)
                    863:                *datep = (char *)NULL;
                    864:
                    865:        if (nbp != NULL)
                    866:                *nbp = 0;
                    867:
                    868:        if (strlcpy(tagpath, CVS_PATH_TAG, sizeof(tagpath)) >= sizeof(tagpath))
                    869:                return;
                    870:
                    871:        fp = fopen(tagpath, "r");
                    872:        if (fp == NULL) {
                    873:                if (errno != ENOENT)
                    874:                        cvs_log(LP_NOTICE, "failed to open `%s' : %s", tagpath,
                    875:                            strerror(errno));
                    876:                return;
                    877:        }
                    878:
                    879:        linenum = 0;
                    880:
                    881:        while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
                    882:                linenum++;
                    883:                if ((len = strlen(linebuf)) == 0)
                    884:                        continue;
                    885:                if (linebuf[len -1] != '\n') {
                    886:                        cvs_log(LP_WARN, "line too long in `%s:%d'", tagpath,
                    887:                            linenum);
                    888:                        break;
                    889:                }
                    890:                linebuf[--len] = '\0';
                    891:
1.53      reyk      892:                switch (*linebuf) {
1.51      xsa       893:                case 'T':
                    894:                        if (tagp != NULL)
1.59    ! joris     895:                                *tagp = xstrdup(linebuf);
1.51      xsa       896:                        break;
                    897:                case 'D':
                    898:                        if (datep != NULL)
1.59    ! joris     899:                                *datep = xstrdup(linebuf);
1.51      xsa       900:                        break;
                    901:                case 'N':
                    902:                        if (tagp != NULL)
1.59    ! joris     903:                                *tagp = xstrdup(linebuf);
1.51      xsa       904:                        if (nbp != NULL)
                    905:                                *nbp = 1;
                    906:                        break;
                    907:                default:
                    908:                        break;
                    909:                }
                    910:        }
                    911:        if (ferror(fp))
                    912:                cvs_log(LP_NOTICE, "failed to read line from `%s'", tagpath);
                    913:
                    914:        (void)fclose(fp);
1.54      joris     915: }
                    916:
                    917: #endif /* !RCSPROG */
                    918:
                    919: /*
                    920:  * Split the contents of a file into a list of lines.
                    921:  */
                    922: struct cvs_lines *
                    923: cvs_splitlines(const char *fcont)
                    924: {
                    925:        char *dcp;
                    926:        struct cvs_lines *lines;
                    927:        struct cvs_line *lp;
                    928:
1.59    ! joris     929:        lines = (struct cvs_lines *)xmalloc(sizeof(*lines));
1.54      joris     930:        TAILQ_INIT(&(lines->l_lines));
                    931:        lines->l_nblines = 0;
1.59    ! joris     932:        lines->l_data = xstrdup(fcont);
1.54      joris     933:
1.59    ! joris     934:        lp = (struct cvs_line *)xmalloc(sizeof(*lp));
1.54      joris     935:        lp->l_line = NULL;
                    936:        lp->l_lineno = 0;
                    937:        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    938:
                    939:        for (dcp = lines->l_data; *dcp != '\0';) {
1.59    ! joris     940:                lp = (struct cvs_line *)xmalloc(sizeof(*lp));
1.54      joris     941:                lp->l_line = dcp;
                    942:                lp->l_lineno = ++(lines->l_nblines);
                    943:                TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    944:
                    945:                dcp = strchr(dcp, '\n');
                    946:                if (dcp == NULL)
                    947:                        break;
                    948:                *(dcp++) = '\0';
                    949:        }
                    950:
                    951:        return (lines);
                    952: }
                    953:
                    954: void
                    955: cvs_freelines(struct cvs_lines *lines)
                    956: {
                    957:        struct cvs_line *lp;
                    958:
                    959:        while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
                    960:                TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.59    ! joris     961:                xfree(lp);
1.54      joris     962:        }
                    963:
1.59    ! joris     964:        xfree(lines->l_data);
        !           965:        xfree(lines);
1.54      joris     966: }
                    967:
                    968: BUF *
                    969: cvs_patchfile(const char *data, const char *patch,
                    970:     int (*p)(struct cvs_lines *, struct cvs_lines *))
                    971: {
                    972:        struct cvs_lines *dlines, *plines;
                    973:        struct cvs_line *lp;
                    974:        size_t len;
                    975:        int lineno;
                    976:        BUF *res;
                    977:
                    978:        len = strlen(data);
                    979:
                    980:        if ((dlines = cvs_splitlines(data)) == NULL)
                    981:                return (NULL);
                    982:
                    983:        if ((plines = cvs_splitlines(patch)) == NULL)
                    984:                return (NULL);
                    985:
                    986:        if (p(dlines, plines) < 0) {
                    987:                cvs_freelines(dlines);
                    988:                cvs_freelines(plines);
                    989:                return (NULL);
                    990:        }
                    991:
                    992:        if ((res = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL) {
                    993:                cvs_freelines(dlines);
                    994:                cvs_freelines(plines);
                    995:                return (NULL);
                    996:        }
                    997:
                    998:        lineno = 0;
                    999:        TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
                   1000:                if (lineno != 0)
                   1001:                        cvs_buf_fappend(res, "%s\n", lp->l_line);
                   1002:                lineno++;
                   1003:        }
                   1004:
                   1005:        cvs_freelines(dlines);
                   1006:        cvs_freelines(plines);
                   1007:        return (res);
1.58      joris    1008: }
                   1009:
                   1010: /*
                   1011:  * a hack to mimic and thus match gnu cvs behaviour.
                   1012:  */
                   1013: time_t
                   1014: cvs_hack_time(time_t oldtime, int togmt)
                   1015: {
                   1016:        int l;
                   1017:        struct tm *t;
                   1018:        char tbuf[32];
                   1019:
                   1020:        if (togmt == 1) {
                   1021:                t = gmtime(&oldtime);
                   1022:                if (t == NULL)
                   1023:                        return (0);
                   1024:
                   1025:                return (mktime(t));
                   1026:        }
                   1027:
                   1028:        t = localtime(&oldtime);
                   1029:
                   1030:        l = snprintf(tbuf, sizeof(tbuf), "%d/%d/%d GMT %d:%d:%d",
                   1031:            t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour,
                   1032:            t->tm_min, t->tm_sec);
                   1033:        if (l == -1 || l >= (int)sizeof(tbuf))
                   1034:                return (0);
                   1035:
                   1036:        return (cvs_date_parse(tbuf));
1.29      jfb      1037: }