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

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