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

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