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

1.74    ! xsa         1: /*     $OpenBSD: ci.c,v 1.73 2005/11/28 09:16:32 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: {
                    438:        char  *filec;
                    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)) {
                    446:                cvs_log(LP_ERR, "revision is too low!");
                    447:                rcs_close(pb->file);
                    448:                return (-1);
                    449:        }
                    450:
1.73      xsa       451:        /* Load file contents */
1.63      niallo    452:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    453:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
                    454:                return (-1);
                    455:        }
                    456:
                    457:        if (cvs_buf_putc(bp, '\0') < 0)
                    458:                return (-1);
                    459:
                    460:        filec = (char *)cvs_buf_release(bp);
                    461:
1.73      xsa       462:        /* Get RCS patch */
1.63      niallo    463:        if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
                    464:                cvs_log(LP_ERR, "failed to get diff");
                    465:                return (-1);
                    466:        }
                    467:
                    468:        /*
                    469:         * If -f is not specified and there are no differences, tell
                    470:         * the user and revert to latest version.
                    471:         */
                    472:        if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
                    473:                checkin_revert(pb);
                    474:                return (0);
                    475:        }
                    476:
1.73      xsa       477:        /* Check for a lock belonging to this user. If none, abort check-in. */
1.63      niallo    478:        if (checkin_checklock(pb) < 0)
                    479:                return (-1);
                    480:
1.73      xsa       481:        /* If no log message specified, get it interactively. */
1.63      niallo    482:        if (pb->flags & INTERACTIVE)
                    483:                pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
                    484:
                    485:        if (rcs_lock_remove(pb->file, pb->frev) < 0) {
                    486:                if (rcs_errno != RCS_ERR_NOENT)
                    487:                    cvs_log(LP_WARN, "failed to remove lock");
                    488:        }
                    489:
1.73      xsa       490:        /* Current head revision gets the RCS patch as rd_text */
1.63      niallo    491:        if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1) {
                    492:                cvs_log(LP_ERR,
                    493:                    "failed to set new rd_text for head rev");
                    494:                exit (1);
                    495:        }
                    496:
                    497:        /*
                    498:         * Set the date of the revision to be the last modification
                    499:         * time of the working file if -d has no argument.
                    500:         */
                    501:        if (pb->date == DATE_MTIME
                    502:            && (checkin_mtimedate(pb) < 0))
                    503:                return (-1);
                    504:
1.73      xsa       505:        /* Now add our new revision */
1.63      niallo    506:        if (rcs_rev_add(pb->file,
                    507:            (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
                    508:            pb->rcs_msg, pb->date, pb->username) != 0) {
                    509:                cvs_log(LP_ERR, "failed to add new revision");
                    510:                return (-1);
                    511:        }
                    512:
                    513:        /*
                    514:         * If we are checking in to a non-default (ie user-specified)
                    515:         * revision, set head to this revision.
                    516:         */
                    517:        if (pb->newrev != NULL)
                    518:                rcs_head_set(pb->file, pb->newrev);
                    519:        else
                    520:                pb->newrev = pb->file->rf_head;
                    521:
1.73      xsa       522:        /* New head revision has to contain entire file; */
1.63      niallo    523:         if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1) {
                    524:                cvs_log(LP_ERR, "failed to set new head revision");
                    525:                exit(1);
                    526:        }
                    527:
1.73      xsa       528:        /* Attach a symbolic name to this revision if specified. */
1.63      niallo    529:        if (pb->symbol != NULL
                    530:            && (checkin_attach_symbol(pb) < 0))
                    531:                return (-1);
                    532:
1.73      xsa       533:        /* Set the state of this revision if specified. */
1.63      niallo    534:        if (pb->state != NULL)
                    535:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    536:
                    537:        free(pb->deltatext);
                    538:        free(filec);
                    539:        (void)unlink(pb->filename);
                    540:
1.73      xsa       541:        /* Do checkout if -u or -l are specified. */
1.63      niallo    542:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    543:            && !(pb->flags & CI_DEFAULT))
                    544:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
                    545:                    pb->username);
                    546:
                    547:        /* File will NOW be synced */
                    548:        rcs_close(pb->file);
                    549:
                    550:        if (pb->flags & INTERACTIVE) {
                    551:                free(pb->rcs_msg);
                    552:                pb->rcs_msg = NULL;
                    553:        }
                    554:        return (0);
                    555: }
                    556:
                    557: /*
1.58      niallo    558:  * checkin_init()
1.65      xsa       559:  *
1.58      niallo    560:  * Does an initial check in, just enough to create the new ,v file
1.63      niallo    561:  * On success, return 0. On error return -1.
1.58      niallo    562:  */
1.63      niallo    563: static int
1.58      niallo    564: checkin_init(struct checkin_params *pb)
                    565: {
                    566:        BUF *bp;
                    567:        char *rcs_desc, *filec;
                    568:
1.73      xsa       569:        /* Load file contents */
1.58      niallo    570:        if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
                    571:                cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.63      niallo    572:                return (-1);
1.58      niallo    573:        }
                    574:
                    575:        if (cvs_buf_putc(bp, '\0') < 0)
1.63      niallo    576:                return (-1);
1.58      niallo    577:
                    578:        filec = (char *)cvs_buf_release(bp);
                    579:
1.73      xsa       580:        /* Get description from user */
1.58      niallo    581:        rcs_desc = checkin_getdesc();
                    582:        rcs_desc_set(pb->file, rcs_desc);
                    583:
1.73      xsa       584:        /* Now add our new revision */
1.66      niallo    585:        if (rcs_rev_add(pb->file, RCS_HEAD_REV, LOG_INIT, -1, pb->username) != 0) {
1.58      niallo    586:                cvs_log(LP_ERR, "failed to add new revision");
1.63      niallo    587:                return (-1);
                    588:        }
                    589:        /*
                    590:         * If we are checking in to a non-default (ie user-specified)
                    591:         * revision, set head to this revision.
                    592:         */
                    593:        if (pb->newrev != NULL)
                    594:                rcs_head_set(pb->file, pb->newrev);
                    595:        else
                    596:                pb->newrev = pb->file->rf_head;
                    597:
1.73      xsa       598:        /* New head revision has to contain entire file; */
1.66      niallo    599:        if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63      niallo    600:                cvs_log(LP_ERR, "failed to set new head revision");
                    601:                return (-1);
1.58      niallo    602:        }
1.73      xsa       603:        /* Attach a symbolic name to this revision if specified. */
1.66      niallo    604:        if (pb->symbol != NULL
                    605:            && (checkin_attach_symbol(pb) < 0))
                    606:                return (-1);
                    607:
1.73      xsa       608:        /* Set the state of this revision if specified. */
1.66      niallo    609:        if (pb->state != NULL)
                    610:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    611:
                    612:        free(filec);
                    613:        (void)unlink(pb->filename);
                    614:
1.73      xsa       615:        /* Do checkout if -u or -l are specified. */
1.66      niallo    616:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    617:            && !(pb->flags & CI_DEFAULT))
                    618:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
                    619:                    pb->username);
1.63      niallo    620:
1.66      niallo    621:        /* File will NOW be synced */
                    622:        rcs_close(pb->file);
1.63      niallo    623:        return (0);
1.58      niallo    624: }
                    625:
1.59      niallo    626: /*
                    627:  * checkin_attach_symbol()
                    628:  *
                    629:  * Attempt to attach the specified symbol to the revision.
                    630:  * On success, return 0. On error return -1.
                    631:  */
1.58      niallo    632: static int
                    633: checkin_attach_symbol(struct checkin_params *pb)
                    634: {
                    635:        char rbuf[16];
                    636:        int ret;
                    637:        if (verbose == 1)
                    638:                printf("symbol: %s\n", pb->symbol);
                    639:        if (pb->flags & CI_SYMFORCE)
                    640:                rcs_sym_remove(pb->file, pb->symbol);
                    641:        if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
                    642:            && (rcs_errno == RCS_ERR_DUPENT)) {
                    643:                rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
                    644:                    rbuf, sizeof(rbuf));
                    645:                cvs_log(LP_ERR,
                    646:                    "symbolic name %s already bound to %s",
                    647:                    pb->symbol, rbuf);
                    648:                rcs_close(pb->file);
                    649:                return (-1);
                    650:        } else if (ret == -1) {
                    651:                cvs_log(LP_ERR, "problem adding symbol: %s",
                    652:                    pb->symbol);
                    653:                rcs_close(pb->file);
                    654:                return (-1);
                    655:        }
                    656:        return (0);
                    657: }
                    658:
1.59      niallo    659: /*
                    660:  * checkin_revert()
                    661:  *
                    662:  * If there are no differences between the working file and the latest revision
                    663:  * and the -f flag is not specified, simply revert to the latest version and
                    664:  * warn the user.
                    665:  *
                    666:  */
1.58      niallo    667: static void
                    668: checkin_revert(struct checkin_params *pb)
                    669: {
                    670:        char rbuf[16];
                    671:
                    672:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
                    673:        cvs_log(LP_WARN,
                    674:            "file is unchanged; reverting to previous revision %s",
                    675:            rbuf);
                    676:        (void)unlink(pb->filename);
                    677:        if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    678:                checkout_rev(pb->file, pb->frev, pb->filename,
                    679:                    pb->flags, pb->username);
                    680:        rcs_lock_remove(pb->file, pb->frev);
                    681:        rcs_close(pb->file);
                    682:        if (verbose == 1)
                    683:                printf("done\n");
                    684: }
                    685:
1.59      niallo    686: /*
                    687:  * checkin_checklock()
                    688:  *
                    689:  * Check for the existence of a lock on the file.  If there are no locks, or it
                    690:  * is not locked by the correct user, return -1.  Otherwise, return 0.
                    691:  */
1.58      niallo    692: static int
                    693: checkin_checklock(struct checkin_params *pb)
                    694: {
                    695:        int found = 0, notlocked = 1;
                    696:        struct rcs_lock *lkp;
                    697:
                    698:        if (!TAILQ_EMPTY(&(pb->file->rf_locks))) {
                    699:                TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
                    700:                        if (!strcmp(lkp->rl_name, pb->username))
                    701:                                notlocked = 0;
                    702:
                    703:                        if (!strcmp(lkp->rl_name, pb->username) &&
                    704:                            !rcsnum_cmp(lkp->rl_num, pb->frev, 0)) {
                    705:                                found = 1;
                    706:                                return (0);
                    707:                        }
                    708:                }
                    709:        }
1.32      joris     710:
1.58      niallo    711:        if ((found == 0) && (notlocked == 0)) {
                    712:                cvs_log(LP_ERR, "no locks set for '%s'", pb->username);
                    713:                rcs_close(pb->file);
                    714:                return (-1);
                    715:        }
1.61      niallo    716:        return (0);
                    717: }
                    718:
                    719: /*
1.62      niallo    720:  * checkin_mtimedate()
1.61      niallo    721:  *
                    722:  * Set the date of the revision to be the last modification
1.62      niallo    723:  * time of the working file.
1.61      niallo    724:  *
                    725:  * On success, return 0. On error return -1.
                    726:  */
                    727: static int
1.62      niallo    728: checkin_mtimedate(struct checkin_params *pb)
1.61      niallo    729: {
                    730:        struct stat sb;
                    731:        if (stat(pb->filename, &sb) != 0) {
1.72      xsa       732:                cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61      niallo    733:                rcs_close(pb->file);
                    734:                return (-1);
                    735:        }
                    736:        pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58      niallo    737:        return (0);
1.63      niallo    738: }
                    739:
                    740: /*
                    741:  * checkin_rcsfile()
                    742:  *
                    743:  * Given a relative filename, decide where the corresponding ,v file
1.65      xsa       744:  * should be.
1.63      niallo    745:  *
                    746:  * Returns pointer to a char array on success, NULL on failure.
                    747:  */
                    748: static char *
                    749: checkin_choose_rcsfile(const char *filename)
                    750: {
                    751:        char fullpath[MAXPATHLEN], *basepath;
                    752:        size_t len;
                    753:        struct stat sb;
                    754:
                    755:        if (realpath(filename, fullpath) == NULL) {
                    756:                cvs_log(LP_ERRNO, "realpath failed: `%s'", filename);
                    757:                return (NULL);
                    758:        }
                    759:        len = strlen(fullpath);
                    760:        while (fullpath[len] != '/')
                    761:                len--;
                    762:        if (len > 0) {
                    763:                /*
                    764:                 * Need two bytes extra for trailing slash and
                    765:                 * NUL-termination.
                    766:                 */
                    767:                len += 2;
                    768:                if ((basepath = malloc(MAXPATHLEN)) == NULL) {
                    769:                        cvs_log(LP_ERRNO, "could not allocate memory");
                    770:                        return (NULL);
                    771:                }
                    772:                strlcpy(basepath, fullpath, len);
                    773:                strlcat(basepath, RCSDIR"/", MAXPATHLEN);
                    774:                if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
                    775:                        /* <path>/RCS/<filename>,v */
                    776:                        strlcat(basepath, filename, MAXPATHLEN);
                    777:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    778:                } else {
                    779:                        /* <path>/<filename>,v */
1.69      xsa       780:                        memset(basepath, '\0', MAXPATHLEN);
1.63      niallo    781:                        strlcpy(basepath, fullpath, len);
                    782:                        strlcat(basepath, filename, MAXPATHLEN);
                    783:                        strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
                    784:                }
                    785:                return (basepath);
                    786:        } else
                    787:                return (NULL);
1.1       niallo    788: }