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

1.85    ! xsa         1: /*     $OpenBSD: ci.c,v 1.84 2005/12/10 20:27:46 joris 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;
                    117:                        else if ((pb.date = cvs_date_parse(rcs_optarg)) <= 0) {
1.20      niallo    118:                                cvs_log(LP_ERR, "invalide date");
                    119:                                exit(1);
                    120:                        }
                    121:                        break;
1.29      niallo    122:                case 'f':
1.58      niallo    123:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    124:                        pb.flags |= FORCE;
1.29      niallo    125:                        break;
1.1       niallo    126:                case 'h':
                    127:                        (usage)();
                    128:                        exit(0);
1.58      niallo    129:                case 'i':
                    130:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    131:                        pb.openflags |= RCS_CREATE;
1.66      niallo    132:                        pb.flags |= CI_INIT;
1.58      niallo    133:                        break;
                    134:                case 'j':
                    135:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    136:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    137:                        pb.flags &= ~CI_INIT;
1.58      niallo    138:                        break;
1.30      niallo    139:                case 'l':
1.58      niallo    140:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    141:                        pb.flags |= CO_LOCK;
1.54      niallo    142:                        break;
                    143:                case 'M':
1.58      niallo    144:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    145:                        pb.flags |= CO_REVDATE;
1.30      niallo    146:                        break;
1.1       niallo    147:                case 'm':
1.58      niallo    148:                        pb.rcs_msg = rcs_optarg;
                    149:                        if (pb.rcs_msg == NULL) {
                    150:                                cvs_log(LP_ERR,
                    151:                                    "missing message for -m option");
                    152:                                exit(1);
                    153:                        }
                    154:                        pb.flags &= ~INTERACTIVE;
1.3       joris     155:                        break;
1.42      niallo    156:                case 'N':
1.84      joris     157:                        pb.symbol = xstrdup(rcs_optarg);
1.58      niallo    158:                        if (rcs_sym_check(pb.symbol) != 1) {
                    159:                                cvs_log(LP_ERR, "invalid symbol `%s'",
                    160:                                    pb.symbol);
1.42      niallo    161:                                exit(1);
                    162:                        }
1.58      niallo    163:                        pb.flags |= CI_SYMFORCE;
1.42      niallo    164:                        break;
1.38      niallo    165:                case 'n':
1.84      joris     166:                        pb.symbol = xstrdup(rcs_optarg);
1.58      niallo    167:                        if (rcs_sym_check(pb.symbol) != 1) {
                    168:                                cvs_log(LP_ERR, "invalid symbol `%s'",
                    169:                                    pb.symbol);
1.38      niallo    170:                                exit(1);
                    171:                        }
                    172:                        break;
1.3       joris     173:                case 'q':
                    174:                        verbose = 0;
1.1       niallo    175:                        break;
1.30      niallo    176:                case 'r':
1.58      niallo    177:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    178:                        pb.flags |= CI_DEFAULT;
1.9       niallo    179:                        break;
1.51      niallo    180:                case 's':
1.58      niallo    181:                        pb.state = rcs_optarg;
                    182:                        if (rcs_state_check(pb.state) < 0) {
                    183:                                cvs_log(LP_ERR, "invalid state `%s'",
                    184:                                    pb.state);
1.51      niallo    185:                                exit(1);
                    186:                        }
1.67      xsa       187:                        break;
                    188:                case 'T':
                    189:                        pb.flags |= PRESERVETIME;
1.51      niallo    190:                        break;
1.80      niallo    191:                case 't':
1.84      joris     192:                        pb.description = xstrdup(rcs_optarg);
1.80      niallo    193:                        break;
1.9       niallo    194:                case 'u':
1.58      niallo    195:                        rcs_set_rev(rcs_optarg, &pb.newrev);
                    196:                        pb.flags |= CO_UNLOCK;
1.9       niallo    197:                        break;
1.30      niallo    198:                case 'V':
                    199:                        printf("%s\n", rcs_version);
                    200:                        exit(0);
1.31      niallo    201:                case 'w':
1.84      joris     202:                        pb.author = xstrdup(rcs_optarg);
1.64      xsa       203:                        break;
                    204:                case 'x':
                    205:                        rcs_suffixes = rcs_optarg;
1.31      niallo    206:                        break;
1.1       niallo    207:                default:
                    208:                        (usage)();
                    209:                        exit(1);
                    210:                }
                    211:        }
                    212:
1.24      joris     213:        argc -= rcs_optind;
                    214:        argv += rcs_optind;
                    215:
1.1       niallo    216:        if (argc == 0) {
                    217:                cvs_log(LP_ERR, "no input file");
                    218:                (usage)();
                    219:                exit(1);
                    220:        }
                    221:
1.82      joris     222:        if ((pb.username = getlogin()) == NULL) {
1.31      niallo    223:                cvs_log(LP_ERRNO, "failed to get username");
                    224:                exit(1);
                    225:        }
                    226:
                    227:
1.1       niallo    228:        for (i = 0; i < argc; i++) {
1.58      niallo    229:                pb.filename = argv[i];
1.1       niallo    230:
1.58      niallo    231:                /*
                    232:                 * Test for existence of ,v file. If we are expected to
                    233:                 * create one, set NEWFILE flag.
                    234:                 */
1.71      niallo    235:                if (rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath)) < 0) {
                    236:                        if (pb.openflags & RCS_CREATE)
                    237:                                pb.flags |= NEWFILE;
                    238:                        else {
                    239:                                cvs_log(LP_ERR, "No existing RCS file");
                    240:                                status = 1;
                    241:                                continue;
                    242:                        }
1.66      niallo    243:                } else {
                    244:                        if (pb.flags & CI_INIT) {
                    245:                                cvs_log(LP_ERR, "%s already exists", pb.fpath);
                    246:                                status = 1;
                    247:                                continue;
                    248:                        }
1.58      niallo    249:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    250:                }
1.63      niallo    251:                /*
                    252:                 * If we are to create a new ,v file, we must decide where it
                    253:                 * should go.
                    254:                 */
                    255:                if (pb.flags & NEWFILE) {
                    256:                        char *fpath = checkin_choose_rcsfile(pb.filename);
                    257:                        if (fpath == NULL) {
                    258:                                status = 1;
                    259:                                continue;
                    260:                        }
                    261:                        strlcpy(pb.fpath, fpath, sizeof(pb.fpath));
1.84      joris     262:                        xfree(fpath);
1.63      niallo    263:                }
                    264:
1.58      niallo    265:                pb.file = rcs_open(pb.fpath, pb.openflags, pb.fmode);
                    266:
                    267:                if (pb.file == NULL) {
1.60      niallo    268:                        cvs_log(LP_ERR, "failed to open rcsfile '%s'",
                    269:                            pb.fpath);
1.1       niallo    270:                        exit(1);
                    271:                }
1.29      niallo    272:
1.58      niallo    273:                if (verbose == 1)
                    274:                        printf("%s  <--  %s\n", pb.fpath, pb.filename);
1.61      niallo    275:
1.63      niallo    276:                if (pb.flags & NEWFILE)
                    277:                        status = checkin_init(&pb);
1.15      niallo    278:                else
1.63      niallo    279:                        status = checkin_update(&pb);
1.4       niallo    280:        }
                    281:
1.16      niallo    282:        return (status);
1.4       niallo    283: }
                    284:
1.59      niallo    285: /*
                    286:  * checkin_diff_file()
                    287:  *
1.65      xsa       288:  * Generate the diff between the working file and a revision.
1.59      niallo    289:  * Returns pointer to a char array on success, NULL on failure.
                    290:  */
1.4       niallo    291: static char *
1.58      niallo    292: checkin_diff_file(struct checkin_params *pb)
1.4       niallo    293: {
                    294:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    295:        BUF *b1, *b2, *b3;
                    296:        char rbuf[64], *deltatext;
                    297:
1.58      niallo    298:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4       niallo    299:
1.58      niallo    300:        if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    301:                cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.4       niallo    302:                return (NULL);
1.1       niallo    303:        }
                    304:
1.58      niallo    305:        if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4       niallo    306:                cvs_log(LP_ERR, "failed to load revision");
                    307:                cvs_buf_free(b1);
                    308:                return (NULL);
                    309:        }
                    310:
1.57      xsa       311:        if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4       niallo    312:                cvs_log(LP_ERR, "failed to allocated buffer for diff");
                    313:                cvs_buf_free(b1);
                    314:                cvs_buf_free(b2);
                    315:                return (NULL);
                    316:        }
                    317:
1.50      xsa       318:        strlcpy(path1, rcs_tmpdir, sizeof(path1));
                    319:        strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4       niallo    320:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    321:                cvs_log(LP_ERRNO, "could not write temporary file");
                    322:                cvs_buf_free(b1);
                    323:                cvs_buf_free(b2);
                    324:                return (NULL);
                    325:        }
                    326:        cvs_buf_free(b1);
                    327:
1.50      xsa       328:        strlcpy(path2, rcs_tmpdir, sizeof(path2));
                    329:        strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4       niallo    330:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    331:                cvs_buf_free(b2);
                    332:                (void)unlink(path1);
                    333:                return (NULL);
                    334:        }
                    335:        cvs_buf_free(b2);
                    336:
1.5       niallo    337:        diff_format = D_RCSDIFF;
1.4       niallo    338:        cvs_diffreg(path1, path2, b3);
                    339:        (void)unlink(path1);
                    340:        (void)unlink(path2);
                    341:
                    342:        cvs_buf_putc(b3, '\0');
                    343:        deltatext = (char *)cvs_buf_release(b3);
                    344:
                    345:        return (deltatext);
1.6       niallo    346: }
                    347:
                    348: /*
1.65      xsa       349:  * checkin_getlogmsg()
1.59      niallo    350:  *
1.6       niallo    351:  * Get log message from user interactively.
1.59      niallo    352:  * Returns pointer to a char array on success, NULL on failure.
1.6       niallo    353:  */
                    354: static char *
1.34      niallo    355: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6       niallo    356: {
1.58      niallo    357:        char   *rcs_msg, nrev[16], prev[16];
1.6       niallo    358:        RCSNUM *tmprev;
                    359:
1.7       niallo    360:        rcs_msg = NULL;
1.6       niallo    361:        tmprev = rcsnum_alloc();
                    362:        rcsnum_cpy(rev, tmprev, 16);
1.9       niallo    363:        rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12      niallo    364:        if (rev2 == NULL)
                    365:                rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
                    366:        else
                    367:                rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6       niallo    368:        rcsnum_free(tmprev);
                    369:
1.47      niallo    370:        if (verbose == 1)
                    371:                printf("new revision: %s; previous revision: %s\n", nrev,
                    372:                    prev);
1.32      joris     373:
1.58      niallo    374:        rcs_msg = checkin_getinput(LOG_PROMPT);
                    375:        return (rcs_msg);
                    376: }
                    377:
                    378:
                    379: /*
                    380:  * checkin_getdesc()
                    381:  *
                    382:  * Get file description interactively.
1.59      niallo    383:  * Returns pointer to a char array on success, NULL on failure.
1.58      niallo    384:  */
                    385: static char *
                    386: checkin_getdesc()
                    387: {
                    388:        char *description;
                    389:
                    390:        description = checkin_getinput(DESC_PROMPT);
                    391:        return (description);
                    392: }
                    393:
                    394: /*
                    395:  * checkin_getinput()
                    396:  *
1.59      niallo    397:  * Get some input from the user, in RCS style, prompting with message <prompt>.
                    398:  * Returns pointer to a char array on success, NULL on failure.
1.58      niallo    399:  */
                    400: static char *
                    401: checkin_getinput(const char *prompt)
                    402: {
                    403:        BUF *inputbuf;
                    404:        char *input, buf[128];
                    405:
                    406:        if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
                    407:                cvs_log(LP_ERR, "failed to allocate input buffer");
                    408:                return (NULL);
                    409:        }
                    410:
                    411:        printf(prompt);
1.6       niallo    412:        for (;;) {
                    413:                fgets(buf, (int)sizeof(buf), stdin);
1.9       niallo    414:                if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6       niallo    415:                        break;
1.58      niallo    416:                cvs_buf_append(inputbuf, buf, strlen(buf));
1.46      joris     417:                printf(">> ");
1.6       niallo    418:        }
1.32      joris     419:
1.58      niallo    420:        cvs_buf_putc(inputbuf, '\0');
                    421:        input = (char *)cvs_buf_release(inputbuf);
                    422:
                    423:        return (input);
                    424: }
                    425:
                    426: /*
1.63      niallo    427:  * checkin_update()
                    428:  *
                    429:  * Do a checkin to an existing RCS file.
                    430:  *
                    431:  * On success, return 0. On error return -1.
                    432:  */
                    433: static int
                    434: checkin_update(struct checkin_params *pb)
                    435: {
1.77      xsa       436:        char  *filec, numb1[64], numb2[64];
1.63      niallo    437:        BUF *bp;
                    438:
1.82      joris     439:        /*
                    440:         * XXX this is wrong, we need to get the revision the user
1.85    ! xsa       441:         * has the lock for. So we can decide if we want to create a
1.82      joris     442:         * branch or not. (if it's not current HEAD we need to branch).
                    443:         */
1.63      niallo    444:        pb->frev = pb->file->rf_head;
                    445:
1.82      joris     446:        if (checkin_checklock(pb) < 0)
                    447:                return (-1);
                    448:
                    449:        /* If revision passed on command line is less than HEAD, bail.
                    450:         * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
                    451:         * there is no lock set for the user.
                    452:         */
1.63      niallo    453:        if ((pb->newrev != NULL)
                    454:            && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77      xsa       455:                cvs_log(LP_ERR,
                    456:                    "%s: revision %s too low; must be higher than %s",
                    457:                    pb->file->rf_path,
                    458:                    rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
                    459:                    rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63      niallo    460:                rcs_close(pb->file);
                    461:                return (-1);
                    462:        }
                    463:
1.73      xsa       464:        /* Load file contents */
1.63      niallo    465:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    466:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
                    467:                return (-1);
                    468:        }
                    469:
                    470:        if (cvs_buf_putc(bp, '\0') < 0)
                    471:                return (-1);
                    472:
                    473:        filec = (char *)cvs_buf_release(bp);
                    474:
1.73      xsa       475:        /* Get RCS patch */
1.63      niallo    476:        if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
                    477:                cvs_log(LP_ERR, "failed to get diff");
                    478:                return (-1);
                    479:        }
                    480:
                    481:        /*
                    482:         * If -f is not specified and there are no differences, tell
                    483:         * the user and revert to latest version.
                    484:         */
                    485:        if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
                    486:                checkin_revert(pb);
                    487:                return (0);
                    488:        }
                    489:
1.73      xsa       490:        /* If no log message specified, get it interactively. */
1.63      niallo    491:        if (pb->flags & INTERACTIVE)
                    492:                pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
                    493:
1.82      joris     494:        if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63      niallo    495:                if (rcs_errno != RCS_ERR_NOENT)
1.82      joris     496:                        cvs_log(LP_WARN, "failed to remove lock");
                    497:                else if (!(pb->flags & CO_LOCK))
                    498:                        cvs_log(LP_WARN, "previous revision was not locked; "
                    499:                            "ignoring -l option");
1.63      niallo    500:        }
                    501:
1.73      xsa       502:        /* Current head revision gets the RCS patch as rd_text */
1.63      niallo    503:        if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1) {
                    504:                cvs_log(LP_ERR,
                    505:                    "failed to set new rd_text for head rev");
                    506:                exit (1);
                    507:        }
                    508:
                    509:        /*
                    510:         * Set the date of the revision to be the last modification
                    511:         * time of the working file if -d has no argument.
                    512:         */
                    513:        if (pb->date == DATE_MTIME
                    514:            && (checkin_mtimedate(pb) < 0))
                    515:                return (-1);
                    516:
1.73      xsa       517:        /* Now add our new revision */
1.63      niallo    518:        if (rcs_rev_add(pb->file,
                    519:            (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82      joris     520:            pb->rcs_msg, pb->date, pb->author) != 0) {
1.63      niallo    521:                cvs_log(LP_ERR, "failed to add new revision");
                    522:                return (-1);
                    523:        }
                    524:
                    525:        /*
                    526:         * If we are checking in to a non-default (ie user-specified)
                    527:         * revision, set head to this revision.
                    528:         */
                    529:        if (pb->newrev != NULL)
                    530:                rcs_head_set(pb->file, pb->newrev);
                    531:        else
                    532:                pb->newrev = pb->file->rf_head;
                    533:
1.73      xsa       534:        /* New head revision has to contain entire file; */
1.63      niallo    535:         if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1) {
                    536:                cvs_log(LP_ERR, "failed to set new head revision");
                    537:                exit(1);
                    538:        }
                    539:
1.73      xsa       540:        /* Attach a symbolic name to this revision if specified. */
1.63      niallo    541:        if (pb->symbol != NULL
                    542:            && (checkin_attach_symbol(pb) < 0))
                    543:                return (-1);
                    544:
1.73      xsa       545:        /* Set the state of this revision if specified. */
1.63      niallo    546:        if (pb->state != NULL)
                    547:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    548:
1.84      joris     549:        xfree(pb->deltatext);
                    550:        xfree(filec);
1.63      niallo    551:        (void)unlink(pb->filename);
                    552:
1.73      xsa       553:        /* Do checkout if -u or -l are specified. */
1.63      niallo    554:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    555:            && !(pb->flags & CI_DEFAULT))
                    556:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.82      joris     557:                    pb->username, pb->author, NULL);
1.63      niallo    558:
                    559:        /* File will NOW be synced */
                    560:        rcs_close(pb->file);
                    561:
                    562:        if (pb->flags & INTERACTIVE) {
1.84      joris     563:                xfree(pb->rcs_msg);
1.63      niallo    564:                pb->rcs_msg = NULL;
                    565:        }
                    566:        return (0);
                    567: }
                    568:
                    569: /*
1.58      niallo    570:  * checkin_init()
1.65      xsa       571:  *
1.58      niallo    572:  * Does an initial check in, just enough to create the new ,v file
1.63      niallo    573:  * On success, return 0. On error return -1.
1.58      niallo    574:  */
1.63      niallo    575: static int
1.58      niallo    576: checkin_init(struct checkin_params *pb)
                    577: {
1.80      niallo    578:        BUF *bp, *dp;
                    579:        char *filec;
                    580:        const char *rcs_desc;
1.58      niallo    581:
1.73      xsa       582:        /* Load file contents */
1.58      niallo    583:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    584:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.63      niallo    585:                return (-1);
1.58      niallo    586:        }
                    587:
                    588:        if (cvs_buf_putc(bp, '\0') < 0)
1.63      niallo    589:                return (-1);
1.58      niallo    590:
                    591:        filec = (char *)cvs_buf_release(bp);
                    592:
1.73      xsa       593:        /* Get description from user */
1.80      niallo    594:        if (pb->description == NULL)
                    595:                rcs_desc = (const char *)checkin_getdesc();
                    596:        else {
                    597:                if (*pb->description == '-') {
                    598:                        pb->description++;
1.81      niallo    599:                        rcs_desc = (const char *)pb->description;
                    600:                } else {
1.80      niallo    601:                        dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
                    602:                        if (dp == NULL) {
                    603:                                cvs_log(LP_ERR,
                    604:                                    "failed to load description file '%s'",
                    605:                                    pb->description);
1.84      joris     606:                                xfree(filec);
1.80      niallo    607:                                return (-1);
                    608:                        }
                    609:                        if (cvs_buf_putc(dp, '\0') < 0) {
1.84      joris     610:                                xfree(filec);
1.80      niallo    611:                                return (-1);
                    612:                        }
                    613:                        rcs_desc = (const char *)cvs_buf_release(dp);
                    614:                }
                    615:        }
1.58      niallo    616:        rcs_desc_set(pb->file, rcs_desc);
                    617:
1.73      xsa       618:        /* Now add our new revision */
1.82      joris     619:        if (rcs_rev_add(pb->file, RCS_HEAD_REV, LOG_INIT, -1, pb->author) != 0) {
1.58      niallo    620:                cvs_log(LP_ERR, "failed to add new revision");
1.63      niallo    621:                return (-1);
                    622:        }
                    623:        /*
                    624:         * If we are checking in to a non-default (ie user-specified)
                    625:         * revision, set head to this revision.
                    626:         */
                    627:        if (pb->newrev != NULL)
                    628:                rcs_head_set(pb->file, pb->newrev);
                    629:        else
                    630:                pb->newrev = pb->file->rf_head;
                    631:
1.73      xsa       632:        /* New head revision has to contain entire file; */
1.66      niallo    633:        if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63      niallo    634:                cvs_log(LP_ERR, "failed to set new head revision");
                    635:                return (-1);
1.58      niallo    636:        }
1.73      xsa       637:        /* Attach a symbolic name to this revision if specified. */
1.66      niallo    638:        if (pb->symbol != NULL
                    639:            && (checkin_attach_symbol(pb) < 0))
                    640:                return (-1);
                    641:
1.73      xsa       642:        /* Set the state of this revision if specified. */
1.66      niallo    643:        if (pb->state != NULL)
                    644:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    645:
1.84      joris     646:        xfree(filec);
1.66      niallo    647:        (void)unlink(pb->filename);
                    648:
1.73      xsa       649:        /* Do checkout if -u or -l are specified. */
1.66      niallo    650:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    651:            && !(pb->flags & CI_DEFAULT))
                    652:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.82      joris     653:                    pb->username, pb->author, NULL);
1.63      niallo    654:
1.66      niallo    655:        /* File will NOW be synced */
                    656:        rcs_close(pb->file);
1.63      niallo    657:        return (0);
1.58      niallo    658: }
                    659:
1.59      niallo    660: /*
                    661:  * checkin_attach_symbol()
                    662:  *
                    663:  * Attempt to attach the specified symbol to the revision.
                    664:  * On success, return 0. On error return -1.
                    665:  */
1.58      niallo    666: static int
                    667: checkin_attach_symbol(struct checkin_params *pb)
                    668: {
                    669:        char rbuf[16];
                    670:        int ret;
                    671:        if (verbose == 1)
                    672:                printf("symbol: %s\n", pb->symbol);
                    673:        if (pb->flags & CI_SYMFORCE)
                    674:                rcs_sym_remove(pb->file, pb->symbol);
                    675:        if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
                    676:            && (rcs_errno == RCS_ERR_DUPENT)) {
                    677:                rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
                    678:                    rbuf, sizeof(rbuf));
                    679:                cvs_log(LP_ERR,
                    680:                    "symbolic name %s already bound to %s",
                    681:                    pb->symbol, rbuf);
                    682:                rcs_close(pb->file);
                    683:                return (-1);
                    684:        } else if (ret == -1) {
                    685:                cvs_log(LP_ERR, "problem adding symbol: %s",
                    686:                    pb->symbol);
                    687:                rcs_close(pb->file);
                    688:                return (-1);
                    689:        }
                    690:        return (0);
                    691: }
                    692:
1.59      niallo    693: /*
                    694:  * checkin_revert()
                    695:  *
                    696:  * If there are no differences between the working file and the latest revision
                    697:  * and the -f flag is not specified, simply revert to the latest version and
                    698:  * warn the user.
                    699:  *
                    700:  */
1.58      niallo    701: static void
                    702: checkin_revert(struct checkin_params *pb)
                    703: {
                    704:        char rbuf[16];
                    705:
                    706:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
                    707:        cvs_log(LP_WARN,
                    708:            "file is unchanged; reverting to previous revision %s",
                    709:            rbuf);
                    710:        (void)unlink(pb->filename);
                    711:        if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    712:                checkout_rev(pb->file, pb->frev, pb->filename,
1.82      joris     713:                    pb->flags, pb->username, pb->author, NULL);
                    714:        rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58      niallo    715:        rcs_close(pb->file);
                    716:        if (verbose == 1)
                    717:                printf("done\n");
                    718: }
                    719:
1.59      niallo    720: /*
                    721:  * checkin_checklock()
                    722:  *
                    723:  * Check for the existence of a lock on the file.  If there are no locks, or it
                    724:  * is not locked by the correct user, return -1.  Otherwise, return 0.
                    725:  */
1.58      niallo    726: static int
                    727: checkin_checklock(struct checkin_params *pb)
                    728: {
                    729:        struct rcs_lock *lkp;
                    730:
1.82      joris     731:        TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
                    732:                if ((!strcmp(lkp->rl_name, pb->username)) &&
                    733:                    (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
                    734:                        return (0);
1.58      niallo    735:        }
1.32      joris     736:
1.82      joris     737:        cvs_log(LP_ERR,
                    738:            "%s: no lock set by %s", pb->file->rf_path, pb->username);
                    739:        rcs_close(pb->file);
                    740:        return (-1);
1.61      niallo    741: }
                    742:
                    743: /*
1.62      niallo    744:  * checkin_mtimedate()
1.61      niallo    745:  *
                    746:  * Set the date of the revision to be the last modification
1.62      niallo    747:  * time of the working file.
1.61      niallo    748:  *
                    749:  * On success, return 0. On error return -1.
                    750:  */
                    751: static int
1.62      niallo    752: checkin_mtimedate(struct checkin_params *pb)
1.61      niallo    753: {
                    754:        struct stat sb;
                    755:        if (stat(pb->filename, &sb) != 0) {
1.72      xsa       756:                cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61      niallo    757:                rcs_close(pb->file);
                    758:                return (-1);
                    759:        }
                    760:        pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58      niallo    761:        return (0);
1.63      niallo    762: }
                    763:
                    764: /*
1.75      niallo    765:  * checkin_choose_rcsfile()
1.63      niallo    766:  *
                    767:  * Given a relative filename, decide where the corresponding ,v file
1.65      xsa       768:  * should be.
1.63      niallo    769:  *
                    770:  * Returns pointer to a char array on success, NULL on failure.
                    771:  */
                    772: static char *
                    773: checkin_choose_rcsfile(const char *filename)
                    774: {
1.76      niallo    775:        char name[MAXPATHLEN], *basepath;
                    776:        const char *ptr;
1.63      niallo    777:        size_t len;
                    778:        struct stat sb;
                    779:
1.84      joris     780:        basepath = xmalloc(MAXPATHLEN);
1.76      niallo    781:        if (strchr(filename, '/') == NULL) {
                    782:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    783:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    784:                        /* <path>/RCS/<filename>,v */
                    785:                        strlcat(basepath, filename, MAXPATHLEN);
                    786:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    787:                } else {
                    788:                        /* <path>/<filename>,v */
                    789:                        memset(basepath, '\0', MAXPATHLEN);
                    790:                        strlcpy(basepath, filename, MAXPATHLEN);
                    791:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    792:                }
                    793:        } else {
                    794:                ptr = filename;
                    795:                /* Walk backwards till we find the base directory */
                    796:                len = strlen(filename);
                    797:                ptr += len + 1;
                    798:                while (filename[len] != '/') {
                    799:                        len--;
                    800:                        ptr--;
                    801:                }
1.63      niallo    802:                /*
                    803:                 * Need two bytes extra for trailing slash and
                    804:                 * NUL-termination.
                    805:                 */
                    806:                len += 2;
1.76      niallo    807:                if (len > MAXPATHLEN) {
1.84      joris     808:                        xfree(basepath);
1.63      niallo    809:                        return (NULL);
                    810:                }
1.76      niallo    811:                strlcpy(basepath, filename, len);
                    812:                strlcpy(name, ptr, MAXPATHLEN);
1.63      niallo    813:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    814:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    815:                        /* <path>/RCS/<filename>,v */
1.76      niallo    816:                        strlcat(basepath, name, MAXPATHLEN);
1.63      niallo    817:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    818:                } else {
                    819:                        /* <path>/<filename>,v */
1.69      xsa       820:                        memset(basepath, '\0', MAXPATHLEN);
1.76      niallo    821:                        strlcpy(basepath, filename, MAXPATHLEN);
1.63      niallo    822:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    823:                }
1.76      niallo    824:        }
                    825:        return (basepath);
1.1       niallo    826: }