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

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