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

Annotation of src/usr.bin/rcs/ci.c, Revision 1.87

1.87    ! xsa         1: /*     $OpenBSD: ci.c,v 1.86 2005/12/19 18:24:12 xsa Exp $     */
1.1       niallo      2: /*
                      3:  * Copyright (c) 2005 Niall O'Higgins <niallo@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following cinditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source cide must retain the above cipyright
                     11:  *    notice, this list of cinditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include <sys/param.h>
                     28: #include <sys/types.h>
                     29: #include <sys/stat.h>
                     30: #include <sys/wait.h>
                     31:
1.68      xsa        32: #include <ctype.h>
1.1       niallo     33: #include <pwd.h>
                     34: #include <stdio.h>
                     35: #include <stdlib.h>
1.68      xsa        36: #include <string.h>
1.1       niallo     37: #include <unistd.h>
                     38:
                     39: #include "log.h"
                     40: #include "rcs.h"
1.4       niallo     41: #include "diff.h"
1.1       niallo     42: #include "rcsprog.h"
1.9       niallo     43:
1.80      niallo     44: #define CI_OPTSTRING    "d::f::i::j::k:l::m:M::N:n:qr::s:Tt:u::Vw:x:"
1.25      niallo     45: #define DATE_NOW        -1
                     46: #define DATE_MTIME      -2
                     47:
1.58      niallo     48: #define LOG_INIT        "Initial revision"
1.63      niallo     49: #define LOG_PROMPT      "enter log message, terminated with a single '.' "    \
1.58      niallo     50:                         "or end of file:\n>> "
1.63      niallo     51: #define DESC_PROMPT     "enter description, terminated with single '.' "      \
                     52:                        "or end of file:\nNOTE: This is NOT the log message!" \
                     53:                         "\n>> "
1.58      niallo     54:
                     55: struct checkin_params {
                     56:        int flags, openflags;
                     57:        mode_t fmode;
                     58:        time_t date;
                     59:        RCSFILE *file;
                     60:        RCSNUM *frev, *newrev;
                     61:        char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.82      joris      62:        char *author;
1.80      niallo     63:        const char *symbol, *state, *description;
1.58      niallo     64: };
                     65:
1.68      xsa        66: static int      checkin_attach_symbol(struct checkin_params *pb);
                     67: static int      checkin_checklock(struct checkin_params *pb);
                     68: static char    *checkin_choose_rcsfile(const char *);
                     69: static char    *checkin_diff_file(struct checkin_params *);
                     70: static char    *checkin_getdesc(void);
                     71: static char    *checkin_getinput(const char *);
                     72: static char    *checkin_getlogmsg(RCSNUM *, RCSNUM *);
                     73: static int      checkin_init(struct checkin_params *);
                     74: static int      checkin_mtimedate(struct checkin_params *pb);
                     75: static int      checkin_update(struct checkin_params *pb);
                     76: static void     checkin_revert(struct checkin_params *pb);
1.4       niallo     77:
1.1       niallo     78: void
                     79: checkin_usage(void)
                     80: {
                     81:        fprintf(stderr,
1.67      xsa        82:            "usage: ci [-MNqTV] [-d[date]] [-f[rev]] [-i[rev]] [-j[rev]]\n"
1.74      xsa        83:            "          [-kmode] [-l[rev]] [-M[rev]] [-mmsg] [-Nsymbol]\n"
1.85      xsa        84:            "          [-nsymbol] [-r[rev]] [-sstate] [-tfile|str] [-u[rev]]\n"
1.81      niallo     85:            "          [-wusername] [-xsuffixes] file ...\n");
1.58      niallo     86: }
                     87:
1.55      niallo     88:
1.1       niallo     89:
                     90: /*
                     91:  * checkin_main()
                     92:  *
                     93:  * Handler for the `ci' program.
                     94:  * Returns 0 on success, or >0 on error.
                     95:  */
                     96: int
                     97: checkin_main(int argc, char **argv)
                     98: {
1.58      niallo     99:        int i, ch, status;
                    100:        struct checkin_params pb;
1.1       niallo    101:
1.58      niallo    102:        pb.date = DATE_NOW;
                    103:        pb.file = NULL;
1.82      joris     104:        pb.rcs_msg = pb.username = pb.author = NULL;
1.80      niallo    105:        pb.state = pb.symbol = pb.description = NULL;
1.58      niallo    106:        pb.newrev =  NULL;
                    107:        pb.fmode = pb.flags = status = 0;
1.1       niallo    108:
1.58      niallo    109:        pb.flags = INTERACTIVE;
                    110:        pb.openflags = RCS_RDWR|RCS_CREATE;
1.9       niallo    111:
1.58      niallo    112:        while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1       niallo    113:                switch (ch) {
1.20      niallo    114:                case 'd':
1.25      niallo    115:                        if (rcs_optarg == NULL)
1.58      niallo    116:                                pb.date = DATE_MTIME;
1.87    ! xsa       117:                        else if ((pb.date = cvs_date_parse(rcs_optarg)) <= 0)
        !           118:                                fatal("invalid date");
1.20      niallo    119:                        break;
1.29      niallo    120:                case 'f':
1.58      niallo    121:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    122:                        pb.flags |= FORCE;
1.29      niallo    123:                        break;
1.1       niallo    124:                case 'h':
                    125:                        (usage)();
                    126:                        exit(0);
1.58      niallo    127:                case 'i':
                    128:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    129:                        pb.openflags |= RCS_CREATE;
1.66      niallo    130:                        pb.flags |= CI_INIT;
1.58      niallo    131:                        break;
                    132:                case 'j':
                    133:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    134:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    135:                        pb.flags &= ~CI_INIT;
1.58      niallo    136:                        break;
1.30      niallo    137:                case 'l':
1.58      niallo    138:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    139:                        pb.flags |= CO_LOCK;
1.54      niallo    140:                        break;
                    141:                case 'M':
1.58      niallo    142:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    143:                        pb.flags |= CO_REVDATE;
1.30      niallo    144:                        break;
1.1       niallo    145:                case 'm':
1.58      niallo    146:                        pb.rcs_msg = rcs_optarg;
1.87    ! xsa       147:                        if (pb.rcs_msg == NULL)
        !           148:                                fatal("missing message for -m option");
1.58      niallo    149:                        pb.flags &= ~INTERACTIVE;
1.3       joris     150:                        break;
1.42      niallo    151:                case 'N':
1.84      joris     152:                        pb.symbol = xstrdup(rcs_optarg);
1.87    ! xsa       153:                        if (rcs_sym_check(pb.symbol) != 1)
        !           154:                                fatal("invalid symbol `%s'", pb.symbol);
1.58      niallo    155:                        pb.flags |= CI_SYMFORCE;
1.42      niallo    156:                        break;
1.38      niallo    157:                case 'n':
1.84      joris     158:                        pb.symbol = xstrdup(rcs_optarg);
1.87    ! xsa       159:                        if (rcs_sym_check(pb.symbol) != 1)
        !           160:                                fatal("invalid symbol `%s'", pb.symbol);
1.38      niallo    161:                        break;
1.3       joris     162:                case 'q':
                    163:                        verbose = 0;
1.1       niallo    164:                        break;
1.30      niallo    165:                case 'r':
1.58      niallo    166:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    167:                        pb.flags |= CI_DEFAULT;
1.9       niallo    168:                        break;
1.51      niallo    169:                case 's':
1.58      niallo    170:                        pb.state = rcs_optarg;
1.87    ! xsa       171:                        if (rcs_state_check(pb.state) < 0)
        !           172:                                fatal("invalid state `%s'", pb.state);
1.67      xsa       173:                        break;
                    174:                case 'T':
                    175:                        pb.flags |= PRESERVETIME;
1.51      niallo    176:                        break;
1.80      niallo    177:                case 't':
1.84      joris     178:                        pb.description = xstrdup(rcs_optarg);
1.80      niallo    179:                        break;
1.9       niallo    180:                case 'u':
1.58      niallo    181:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    182:                        pb.flags |= CO_UNLOCK;
1.9       niallo    183:                        break;
1.30      niallo    184:                case 'V':
                    185:                        printf("%s\n", rcs_version);
                    186:                        exit(0);
1.31      niallo    187:                case 'w':
1.84      joris     188:                        pb.author = xstrdup(rcs_optarg);
1.64      xsa       189:                        break;
                    190:                case 'x':
                    191:                        rcs_suffixes = rcs_optarg;
1.31      niallo    192:                        break;
1.1       niallo    193:                default:
                    194:                        (usage)();
                    195:                        exit(1);
                    196:                }
                    197:        }
                    198:
1.24      joris     199:        argc -= rcs_optind;
                    200:        argv += rcs_optind;
                    201:
1.1       niallo    202:        if (argc == 0) {
                    203:                cvs_log(LP_ERR, "no input file");
                    204:                (usage)();
                    205:                exit(1);
                    206:        }
                    207:
1.86      xsa       208:        if ((pb.username = getlogin()) == NULL)
                    209:                fatal("getlogin failed");
1.31      niallo    210:
                    211:
1.1       niallo    212:        for (i = 0; i < argc; i++) {
1.58      niallo    213:                pb.filename = argv[i];
1.1       niallo    214:
1.58      niallo    215:                /*
                    216:                 * Test for existence of ,v file. If we are expected to
                    217:                 * create one, set NEWFILE flag.
                    218:                 */
1.71      niallo    219:                if (rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath)) < 0) {
                    220:                        if (pb.openflags & RCS_CREATE)
                    221:                                pb.flags |= NEWFILE;
                    222:                        else {
                    223:                                cvs_log(LP_ERR, "No existing RCS file");
                    224:                                status = 1;
                    225:                                continue;
                    226:                        }
1.66      niallo    227:                } else {
                    228:                        if (pb.flags & CI_INIT) {
                    229:                                cvs_log(LP_ERR, "%s already exists", pb.fpath);
                    230:                                status = 1;
                    231:                                continue;
                    232:                        }
1.58      niallo    233:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    234:                }
1.63      niallo    235:                /*
                    236:                 * If we are to create a new ,v file, we must decide where it
                    237:                 * should go.
                    238:                 */
                    239:                if (pb.flags & NEWFILE) {
                    240:                        char *fpath = checkin_choose_rcsfile(pb.filename);
                    241:                        if (fpath == NULL) {
                    242:                                status = 1;
                    243:                                continue;
                    244:                        }
                    245:                        strlcpy(pb.fpath, fpath, sizeof(pb.fpath));
1.84      joris     246:                        xfree(fpath);
1.63      niallo    247:                }
                    248:
1.58      niallo    249:                pb.file = rcs_open(pb.fpath, pb.openflags, pb.fmode);
                    250:
1.87    ! xsa       251:                if (pb.file == NULL)
        !           252:                        fatal("failed to open rcsfile '%s'", pb.fpath);
1.29      niallo    253:
1.58      niallo    254:                if (verbose == 1)
                    255:                        printf("%s  <--  %s\n", pb.fpath, pb.filename);
1.61      niallo    256:
1.63      niallo    257:                if (pb.flags & NEWFILE)
                    258:                        status = checkin_init(&pb);
1.15      niallo    259:                else
1.63      niallo    260:                        status = checkin_update(&pb);
1.4       niallo    261:        }
                    262:
1.16      niallo    263:        return (status);
1.4       niallo    264: }
                    265:
1.59      niallo    266: /*
                    267:  * checkin_diff_file()
                    268:  *
1.65      xsa       269:  * Generate the diff between the working file and a revision.
1.59      niallo    270:  * Returns pointer to a char array on success, NULL on failure.
                    271:  */
1.4       niallo    272: static char *
1.58      niallo    273: checkin_diff_file(struct checkin_params *pb)
1.4       niallo    274: {
                    275:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    276:        BUF *b1, *b2, *b3;
                    277:        char rbuf[64], *deltatext;
                    278:
1.58      niallo    279:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4       niallo    280:
1.58      niallo    281:        if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    282:                cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.4       niallo    283:                return (NULL);
1.1       niallo    284:        }
                    285:
1.58      niallo    286:        if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4       niallo    287:                cvs_log(LP_ERR, "failed to load revision");
                    288:                cvs_buf_free(b1);
                    289:                return (NULL);
                    290:        }
                    291:
1.57      xsa       292:        if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4       niallo    293:                cvs_log(LP_ERR, "failed to allocated buffer for diff");
                    294:                cvs_buf_free(b1);
                    295:                cvs_buf_free(b2);
                    296:                return (NULL);
                    297:        }
                    298:
1.50      xsa       299:        strlcpy(path1, rcs_tmpdir, sizeof(path1));
                    300:        strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4       niallo    301:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    302:                cvs_log(LP_ERRNO, "could not write temporary file");
                    303:                cvs_buf_free(b1);
                    304:                cvs_buf_free(b2);
                    305:                return (NULL);
                    306:        }
                    307:        cvs_buf_free(b1);
                    308:
1.50      xsa       309:        strlcpy(path2, rcs_tmpdir, sizeof(path2));
                    310:        strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4       niallo    311:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    312:                cvs_buf_free(b2);
                    313:                (void)unlink(path1);
                    314:                return (NULL);
                    315:        }
                    316:        cvs_buf_free(b2);
                    317:
1.5       niallo    318:        diff_format = D_RCSDIFF;
1.4       niallo    319:        cvs_diffreg(path1, path2, b3);
                    320:        (void)unlink(path1);
                    321:        (void)unlink(path2);
                    322:
                    323:        cvs_buf_putc(b3, '\0');
                    324:        deltatext = (char *)cvs_buf_release(b3);
                    325:
                    326:        return (deltatext);
1.6       niallo    327: }
                    328:
                    329: /*
1.65      xsa       330:  * checkin_getlogmsg()
1.59      niallo    331:  *
1.6       niallo    332:  * Get log message from user interactively.
1.59      niallo    333:  * Returns pointer to a char array on success, NULL on failure.
1.6       niallo    334:  */
                    335: static char *
1.34      niallo    336: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6       niallo    337: {
1.58      niallo    338:        char   *rcs_msg, nrev[16], prev[16];
1.6       niallo    339:        RCSNUM *tmprev;
                    340:
1.7       niallo    341:        rcs_msg = NULL;
1.6       niallo    342:        tmprev = rcsnum_alloc();
                    343:        rcsnum_cpy(rev, tmprev, 16);
1.9       niallo    344:        rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12      niallo    345:        if (rev2 == NULL)
                    346:                rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
                    347:        else
                    348:                rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6       niallo    349:        rcsnum_free(tmprev);
                    350:
1.47      niallo    351:        if (verbose == 1)
                    352:                printf("new revision: %s; previous revision: %s\n", nrev,
                    353:                    prev);
1.32      joris     354:
1.58      niallo    355:        rcs_msg = checkin_getinput(LOG_PROMPT);
                    356:        return (rcs_msg);
                    357: }
                    358:
                    359:
                    360: /*
                    361:  * checkin_getdesc()
                    362:  *
                    363:  * Get file description interactively.
1.59      niallo    364:  * Returns pointer to a char array on success, NULL on failure.
1.58      niallo    365:  */
                    366: static char *
                    367: checkin_getdesc()
                    368: {
                    369:        char *description;
                    370:
                    371:        description = checkin_getinput(DESC_PROMPT);
                    372:        return (description);
                    373: }
                    374:
                    375: /*
                    376:  * checkin_getinput()
                    377:  *
1.59      niallo    378:  * Get some input from the user, in RCS style, prompting with message <prompt>.
                    379:  * Returns pointer to a char array on success, NULL on failure.
1.58      niallo    380:  */
                    381: static char *
                    382: checkin_getinput(const char *prompt)
                    383: {
                    384:        BUF *inputbuf;
                    385:        char *input, buf[128];
                    386:
                    387:        if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
                    388:                cvs_log(LP_ERR, "failed to allocate input buffer");
                    389:                return (NULL);
                    390:        }
                    391:
                    392:        printf(prompt);
1.6       niallo    393:        for (;;) {
                    394:                fgets(buf, (int)sizeof(buf), stdin);
1.9       niallo    395:                if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6       niallo    396:                        break;
1.58      niallo    397:                cvs_buf_append(inputbuf, buf, strlen(buf));
1.46      joris     398:                printf(">> ");
1.6       niallo    399:        }
1.32      joris     400:
1.58      niallo    401:        cvs_buf_putc(inputbuf, '\0');
                    402:        input = (char *)cvs_buf_release(inputbuf);
                    403:
                    404:        return (input);
                    405: }
                    406:
                    407: /*
1.63      niallo    408:  * checkin_update()
                    409:  *
                    410:  * Do a checkin to an existing RCS file.
                    411:  *
                    412:  * On success, return 0. On error return -1.
                    413:  */
                    414: static int
                    415: checkin_update(struct checkin_params *pb)
                    416: {
1.77      xsa       417:        char  *filec, numb1[64], numb2[64];
1.63      niallo    418:        BUF *bp;
                    419:
1.82      joris     420:        /*
                    421:         * XXX this is wrong, we need to get the revision the user
1.85      xsa       422:         * has the lock for. So we can decide if we want to create a
1.82      joris     423:         * branch or not. (if it's not current HEAD we need to branch).
                    424:         */
1.63      niallo    425:        pb->frev = pb->file->rf_head;
                    426:
1.82      joris     427:        if (checkin_checklock(pb) < 0)
                    428:                return (-1);
                    429:
                    430:        /* If revision passed on command line is less than HEAD, bail.
                    431:         * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
                    432:         * there is no lock set for the user.
                    433:         */
1.63      niallo    434:        if ((pb->newrev != NULL)
                    435:            && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77      xsa       436:                cvs_log(LP_ERR,
                    437:                    "%s: revision %s too low; must be higher than %s",
                    438:                    pb->file->rf_path,
                    439:                    rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
                    440:                    rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63      niallo    441:                rcs_close(pb->file);
                    442:                return (-1);
                    443:        }
                    444:
1.73      xsa       445:        /* Load file contents */
1.63      niallo    446:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    447:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
                    448:                return (-1);
                    449:        }
                    450:
                    451:        if (cvs_buf_putc(bp, '\0') < 0)
                    452:                return (-1);
                    453:
                    454:        filec = (char *)cvs_buf_release(bp);
                    455:
1.73      xsa       456:        /* Get RCS patch */
1.63      niallo    457:        if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
                    458:                cvs_log(LP_ERR, "failed to get diff");
                    459:                return (-1);
                    460:        }
                    461:
                    462:        /*
                    463:         * If -f is not specified and there are no differences, tell
                    464:         * the user and revert to latest version.
                    465:         */
                    466:        if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
                    467:                checkin_revert(pb);
                    468:                return (0);
                    469:        }
                    470:
1.73      xsa       471:        /* If no log message specified, get it interactively. */
1.63      niallo    472:        if (pb->flags & INTERACTIVE)
                    473:                pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
                    474:
1.82      joris     475:        if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63      niallo    476:                if (rcs_errno != RCS_ERR_NOENT)
1.82      joris     477:                        cvs_log(LP_WARN, "failed to remove lock");
                    478:                else if (!(pb->flags & CO_LOCK))
                    479:                        cvs_log(LP_WARN, "previous revision was not locked; "
                    480:                            "ignoring -l option");
1.63      niallo    481:        }
                    482:
1.73      xsa       483:        /* Current head revision gets the RCS patch as rd_text */
1.87    ! xsa       484:        if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
        !           485:                fatal("failed to set new rd_text for head rev");
1.63      niallo    486:
                    487:        /*
                    488:         * Set the date of the revision to be the last modification
                    489:         * time of the working file if -d has no argument.
                    490:         */
                    491:        if (pb->date == DATE_MTIME
                    492:            && (checkin_mtimedate(pb) < 0))
                    493:                return (-1);
                    494:
1.73      xsa       495:        /* Now add our new revision */
1.63      niallo    496:        if (rcs_rev_add(pb->file,
                    497:            (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82      joris     498:            pb->rcs_msg, pb->date, pb->author) != 0) {
1.63      niallo    499:                cvs_log(LP_ERR, "failed to add new revision");
                    500:                return (-1);
                    501:        }
                    502:
                    503:        /*
                    504:         * If we are checking in to a non-default (ie user-specified)
                    505:         * revision, set head to this revision.
                    506:         */
                    507:        if (pb->newrev != NULL)
                    508:                rcs_head_set(pb->file, pb->newrev);
                    509:        else
                    510:                pb->newrev = pb->file->rf_head;
                    511:
1.73      xsa       512:        /* New head revision has to contain entire file; */
1.87    ! xsa       513:         if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1)
        !           514:                fatal("failed to set new head revision");
1.63      niallo    515:
1.73      xsa       516:        /* Attach a symbolic name to this revision if specified. */
1.63      niallo    517:        if (pb->symbol != NULL
                    518:            && (checkin_attach_symbol(pb) < 0))
                    519:                return (-1);
                    520:
1.73      xsa       521:        /* Set the state of this revision if specified. */
1.63      niallo    522:        if (pb->state != NULL)
                    523:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    524:
1.84      joris     525:        xfree(pb->deltatext);
                    526:        xfree(filec);
1.63      niallo    527:        (void)unlink(pb->filename);
                    528:
1.73      xsa       529:        /* Do checkout if -u or -l are specified. */
1.63      niallo    530:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    531:            && !(pb->flags & CI_DEFAULT))
                    532:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.82      joris     533:                    pb->username, pb->author, NULL);
1.63      niallo    534:
                    535:        /* File will NOW be synced */
                    536:        rcs_close(pb->file);
                    537:
                    538:        if (pb->flags & INTERACTIVE) {
1.84      joris     539:                xfree(pb->rcs_msg);
1.63      niallo    540:                pb->rcs_msg = NULL;
                    541:        }
                    542:        return (0);
                    543: }
                    544:
                    545: /*
1.58      niallo    546:  * checkin_init()
1.65      xsa       547:  *
1.58      niallo    548:  * Does an initial check in, just enough to create the new ,v file
1.63      niallo    549:  * On success, return 0. On error return -1.
1.58      niallo    550:  */
1.63      niallo    551: static int
1.58      niallo    552: checkin_init(struct checkin_params *pb)
                    553: {
1.80      niallo    554:        BUF *bp, *dp;
                    555:        char *filec;
                    556:        const char *rcs_desc;
1.58      niallo    557:
1.73      xsa       558:        /* Load file contents */
1.58      niallo    559:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    560:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.63      niallo    561:                return (-1);
1.58      niallo    562:        }
                    563:
                    564:        if (cvs_buf_putc(bp, '\0') < 0)
1.63      niallo    565:                return (-1);
1.58      niallo    566:
                    567:        filec = (char *)cvs_buf_release(bp);
                    568:
1.73      xsa       569:        /* Get description from user */
1.80      niallo    570:        if (pb->description == NULL)
                    571:                rcs_desc = (const char *)checkin_getdesc();
                    572:        else {
                    573:                if (*pb->description == '-') {
                    574:                        pb->description++;
1.81      niallo    575:                        rcs_desc = (const char *)pb->description;
                    576:                } else {
1.80      niallo    577:                        dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
                    578:                        if (dp == NULL) {
                    579:                                cvs_log(LP_ERR,
                    580:                                    "failed to load description file '%s'",
                    581:                                    pb->description);
1.84      joris     582:                                xfree(filec);
1.80      niallo    583:                                return (-1);
                    584:                        }
                    585:                        if (cvs_buf_putc(dp, '\0') < 0) {
1.84      joris     586:                                xfree(filec);
1.80      niallo    587:                                return (-1);
                    588:                        }
                    589:                        rcs_desc = (const char *)cvs_buf_release(dp);
                    590:                }
                    591:        }
1.58      niallo    592:        rcs_desc_set(pb->file, rcs_desc);
                    593:
1.73      xsa       594:        /* Now add our new revision */
1.82      joris     595:        if (rcs_rev_add(pb->file, RCS_HEAD_REV, LOG_INIT, -1, pb->author) != 0) {
1.58      niallo    596:                cvs_log(LP_ERR, "failed to add new revision");
1.63      niallo    597:                return (-1);
                    598:        }
                    599:        /*
                    600:         * If we are checking in to a non-default (ie user-specified)
                    601:         * revision, set head to this revision.
                    602:         */
                    603:        if (pb->newrev != NULL)
                    604:                rcs_head_set(pb->file, pb->newrev);
                    605:        else
                    606:                pb->newrev = pb->file->rf_head;
                    607:
1.73      xsa       608:        /* New head revision has to contain entire file; */
1.66      niallo    609:        if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63      niallo    610:                cvs_log(LP_ERR, "failed to set new head revision");
                    611:                return (-1);
1.58      niallo    612:        }
1.73      xsa       613:        /* Attach a symbolic name to this revision if specified. */
1.66      niallo    614:        if (pb->symbol != NULL
                    615:            && (checkin_attach_symbol(pb) < 0))
                    616:                return (-1);
                    617:
1.73      xsa       618:        /* Set the state of this revision if specified. */
1.66      niallo    619:        if (pb->state != NULL)
                    620:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    621:
1.84      joris     622:        xfree(filec);
1.66      niallo    623:        (void)unlink(pb->filename);
                    624:
1.73      xsa       625:        /* Do checkout if -u or -l are specified. */
1.66      niallo    626:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    627:            && !(pb->flags & CI_DEFAULT))
                    628:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.82      joris     629:                    pb->username, pb->author, NULL);
1.63      niallo    630:
1.66      niallo    631:        /* File will NOW be synced */
                    632:        rcs_close(pb->file);
1.63      niallo    633:        return (0);
1.58      niallo    634: }
                    635:
1.59      niallo    636: /*
                    637:  * checkin_attach_symbol()
                    638:  *
                    639:  * Attempt to attach the specified symbol to the revision.
                    640:  * On success, return 0. On error return -1.
                    641:  */
1.58      niallo    642: static int
                    643: checkin_attach_symbol(struct checkin_params *pb)
                    644: {
                    645:        char rbuf[16];
                    646:        int ret;
                    647:        if (verbose == 1)
                    648:                printf("symbol: %s\n", pb->symbol);
                    649:        if (pb->flags & CI_SYMFORCE)
                    650:                rcs_sym_remove(pb->file, pb->symbol);
                    651:        if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
                    652:            && (rcs_errno == RCS_ERR_DUPENT)) {
                    653:                rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
                    654:                    rbuf, sizeof(rbuf));
                    655:                cvs_log(LP_ERR,
                    656:                    "symbolic name %s already bound to %s",
                    657:                    pb->symbol, rbuf);
                    658:                rcs_close(pb->file);
                    659:                return (-1);
                    660:        } else if (ret == -1) {
                    661:                cvs_log(LP_ERR, "problem adding symbol: %s",
                    662:                    pb->symbol);
                    663:                rcs_close(pb->file);
                    664:                return (-1);
                    665:        }
                    666:        return (0);
                    667: }
                    668:
1.59      niallo    669: /*
                    670:  * checkin_revert()
                    671:  *
                    672:  * If there are no differences between the working file and the latest revision
                    673:  * and the -f flag is not specified, simply revert to the latest version and
                    674:  * warn the user.
                    675:  *
                    676:  */
1.58      niallo    677: static void
                    678: checkin_revert(struct checkin_params *pb)
                    679: {
                    680:        char rbuf[16];
                    681:
                    682:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
                    683:        cvs_log(LP_WARN,
                    684:            "file is unchanged; reverting to previous revision %s",
                    685:            rbuf);
                    686:        (void)unlink(pb->filename);
                    687:        if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    688:                checkout_rev(pb->file, pb->frev, pb->filename,
1.82      joris     689:                    pb->flags, pb->username, pb->author, NULL);
                    690:        rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58      niallo    691:        rcs_close(pb->file);
                    692:        if (verbose == 1)
                    693:                printf("done\n");
                    694: }
                    695:
1.59      niallo    696: /*
                    697:  * checkin_checklock()
                    698:  *
                    699:  * Check for the existence of a lock on the file.  If there are no locks, or it
                    700:  * is not locked by the correct user, return -1.  Otherwise, return 0.
                    701:  */
1.58      niallo    702: static int
                    703: checkin_checklock(struct checkin_params *pb)
                    704: {
                    705:        struct rcs_lock *lkp;
                    706:
1.82      joris     707:        TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
                    708:                if ((!strcmp(lkp->rl_name, pb->username)) &&
                    709:                    (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
                    710:                        return (0);
1.58      niallo    711:        }
1.32      joris     712:
1.82      joris     713:        cvs_log(LP_ERR,
                    714:            "%s: no lock set by %s", pb->file->rf_path, pb->username);
                    715:        rcs_close(pb->file);
                    716:        return (-1);
1.61      niallo    717: }
                    718:
                    719: /*
1.62      niallo    720:  * checkin_mtimedate()
1.61      niallo    721:  *
                    722:  * Set the date of the revision to be the last modification
1.62      niallo    723:  * time of the working file.
1.61      niallo    724:  *
                    725:  * On success, return 0. On error return -1.
                    726:  */
                    727: static int
1.62      niallo    728: checkin_mtimedate(struct checkin_params *pb)
1.61      niallo    729: {
                    730:        struct stat sb;
                    731:        if (stat(pb->filename, &sb) != 0) {
1.72      xsa       732:                cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61      niallo    733:                rcs_close(pb->file);
                    734:                return (-1);
                    735:        }
                    736:        pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58      niallo    737:        return (0);
1.63      niallo    738: }
                    739:
                    740: /*
1.75      niallo    741:  * checkin_choose_rcsfile()
1.63      niallo    742:  *
                    743:  * Given a relative filename, decide where the corresponding ,v file
1.65      xsa       744:  * should be.
1.63      niallo    745:  *
                    746:  * Returns pointer to a char array on success, NULL on failure.
                    747:  */
                    748: static char *
                    749: checkin_choose_rcsfile(const char *filename)
                    750: {
1.76      niallo    751:        char name[MAXPATHLEN], *basepath;
                    752:        const char *ptr;
1.63      niallo    753:        size_t len;
                    754:        struct stat sb;
                    755:
1.84      joris     756:        basepath = xmalloc(MAXPATHLEN);
1.76      niallo    757:        if (strchr(filename, '/') == NULL) {
                    758:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    759:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    760:                        /* <path>/RCS/<filename>,v */
                    761:                        strlcat(basepath, filename, MAXPATHLEN);
                    762:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    763:                } else {
                    764:                        /* <path>/<filename>,v */
                    765:                        memset(basepath, '\0', MAXPATHLEN);
                    766:                        strlcpy(basepath, filename, MAXPATHLEN);
                    767:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    768:                }
                    769:        } else {
                    770:                ptr = filename;
                    771:                /* Walk backwards till we find the base directory */
                    772:                len = strlen(filename);
                    773:                ptr += len + 1;
                    774:                while (filename[len] != '/') {
                    775:                        len--;
                    776:                        ptr--;
                    777:                }
1.63      niallo    778:                /*
                    779:                 * Need two bytes extra for trailing slash and
                    780:                 * NUL-termination.
                    781:                 */
                    782:                len += 2;
1.76      niallo    783:                if (len > MAXPATHLEN) {
1.84      joris     784:                        xfree(basepath);
1.63      niallo    785:                        return (NULL);
                    786:                }
1.76      niallo    787:                strlcpy(basepath, filename, len);
                    788:                strlcpy(name, ptr, MAXPATHLEN);
1.63      niallo    789:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    790:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    791:                        /* <path>/RCS/<filename>,v */
1.76      niallo    792:                        strlcat(basepath, name, MAXPATHLEN);
1.63      niallo    793:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    794:                } else {
                    795:                        /* <path>/<filename>,v */
1.69      xsa       796:                        memset(basepath, '\0', MAXPATHLEN);
1.76      niallo    797:                        strlcpy(basepath, filename, MAXPATHLEN);
1.63      niallo    798:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    799:                }
1.76      niallo    800:        }
                    801:        return (basepath);
1.1       niallo    802: }