[BACK]Return to co.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/co.c, Revision 1.70

1.70    ! xsa         1: /*     $OpenBSD: co.c,v 1.69 2006/03/30 23:06:25 joris Exp $   */
1.1       joris       2: /*
                      3:  * Copyright (c) 2005 Joris Vink <joris@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 conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions 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:
1.51      xsa        27: #include "includes.h"
1.1       joris      28:
                     29: #include "rcsprog.h"
                     30:
1.70    ! xsa        31: #define CO_OPTSTRING   "d:f::I::k:l::M::p::q::r::s:Tu::Vw::x::z::"
1.38      xsa        32:
1.43      xsa        33: static void    checkout_err_nobranch(RCSFILE *, const char *, const char *,
                     34:     const char *, int);
1.4       joris      35:
1.1       joris      36: int
                     37: checkout_main(int argc, char **argv)
                     38: {
1.64      niallo     39:        int i, ch, flags, kflag, status;
1.4       joris      40:        RCSNUM *frev, *rev;
1.1       joris      41:        RCSFILE *file;
1.45      joris      42:        char fpath[MAXPATHLEN];
1.49      joris      43:        char *author, *username, *date;
1.38      xsa        44:        const char *state;
1.37      xsa        45:        time_t rcs_mtime = -1;
1.1       joris      46:
1.64      niallo     47:        flags = status = 0;
1.33      xsa        48:        kflag = RCS_KWEXP_ERR;
1.1       joris      49:        rev = RCS_HEAD_REV;
1.6       joris      50:        frev = NULL;
1.44      joris      51:        state = NULL;
                     52:        author = NULL;
1.49      joris      53:        date = NULL;
1.4       joris      54:
1.38      xsa        55:        while ((ch = rcs_getopt(argc, argv, CO_OPTSTRING)) != -1) {
1.1       joris      56:                switch (ch) {
1.49      joris      57:                case 'd':
                     58:                        date = xstrdup(rcs_optarg);
                     59:                        break;
1.18      joris      60:                case 'f':
1.19      joris      61:                        rcs_set_rev(rcs_optarg, &rev);
1.24      niallo     62:                        flags |= FORCE;
1.18      joris      63:                        break;
1.65      niallo     64:                case 'I':
                     65:                        rcs_set_rev(rcs_optarg, &rev);
                     66:                        flags |= INTERACTIVE;
                     67:                        break;
                     68:
1.33      xsa        69:                case 'k':
                     70:                        kflag = rcs_kflag_get(rcs_optarg);
                     71:                        if (RCS_KWEXP_INVAL(kflag)) {
                     72:                                cvs_log(LP_ERR,
                     73:                                    "invalid RCS keyword expansion mode");
                     74:                                (usage)();
                     75:                                exit(1);
                     76:                        }
                     77:                        break;
1.4       joris      78:                case 'l':
1.19      joris      79:                        rcs_set_rev(rcs_optarg, &rev);
1.53      xsa        80:                        if (flags & CO_UNLOCK) {
                     81:                                cvs_log(LP_ERR, "warning: -u overridden by -l");
                     82:                                flags &= ~CO_UNLOCK;
                     83:                        }
1.24      niallo     84:                        flags |= CO_LOCK;
1.26      niallo     85:                        break;
                     86:                case 'M':
                     87:                        rcs_set_rev(rcs_optarg, &rev);
                     88:                        flags |= CO_REVDATE;
1.4       joris      89:                        break;
1.20      joris      90:                case 'p':
                     91:                        rcs_set_rev(rcs_optarg, &rev);
                     92:                        pipeout = 1;
                     93:                        break;
1.3       joris      94:                case 'q':
1.32      xsa        95:                        rcs_set_rev(rcs_optarg, &rev);
1.3       joris      96:                        verbose = 0;
                     97:                        break;
1.1       joris      98:                case 'r':
1.19      joris      99:                        rcs_set_rev(rcs_optarg, &rev);
1.4       joris     100:                        break;
1.24      niallo    101:                case 's':
1.47      joris     102:                        state = xstrdup(rcs_optarg);
1.24      niallo    103:                        flags |= CO_STATE;
1.34      xsa       104:                        break;
                    105:                case 'T':
                    106:                        flags |= PRESERVETIME;
1.24      niallo    107:                        break;
1.4       joris     108:                case 'u':
1.19      joris     109:                        rcs_set_rev(rcs_optarg, &rev);
1.53      xsa       110:                        if (flags & CO_LOCK) {
                    111:                                cvs_log(LP_ERR, "warning: -l overridden by -u");
                    112:                                flags &= ~CO_LOCK;
                    113:                        }
1.24      niallo    114:                        flags |= CO_UNLOCK;
1.1       joris     115:                        break;
1.7       joris     116:                case 'V':
                    117:                        printf("%s\n", rcs_version);
                    118:                        exit(0);
1.62      ray       119:                        /* NOTREACHED */
1.38      xsa       120:                case 'w':
1.44      joris     121:                        /* if no argument, assume current user */
                    122:                        if (rcs_optarg == NULL) {
1.48      xsa       123:                                if ((author = getlogin()) == NULL)
                    124:                                        fatal("getlogin failed");
1.47      joris     125:                        } else
                    126:                                author = xstrdup(rcs_optarg);
1.43      xsa       127:                        flags |= CO_AUTHOR;
1.38      xsa       128:                        break;
1.30      xsa       129:                case 'x':
1.66      ray       130:                        /* Use blank extension if none given. */
                    131:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.59      joris     132:                        break;
                    133:                case 'z':
                    134:                        timezone_flag = rcs_optarg;
1.30      xsa       135:                        break;
1.1       joris     136:                default:
                    137:                        (usage)();
                    138:                        exit(1);
                    139:                }
                    140:        }
                    141:
1.13      joris     142:        argc -= rcs_optind;
                    143:        argv += rcs_optind;
1.1       joris     144:
                    145:        if (argc == 0) {
                    146:                cvs_log(LP_ERR, "no input file");
                    147:                (usage)();
                    148:                exit (1);
                    149:        }
1.11      deraadt   150:
1.44      joris     151:        if ((username = getlogin()) == NULL) {
1.38      xsa       152:                cvs_log(LP_ERRNO, "failed to get username");
                    153:                exit (1);
                    154:        }
                    155:
1.1       joris     156:        for (i = 0; i < argc; i++) {
1.69      joris     157:                frev = NULL;
1.1       joris     158:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    159:                        continue;
                    160:
1.21      niallo    161:                if (verbose == 1)
1.22      xsa       162:                        printf("%s  -->  %s\n", fpath,
                    163:                            (pipeout == 1) ? "standard output" : argv[i]);
1.35      xsa       164:
                    165:                if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
                    166:                        cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
                    167:                        continue;
                    168:                }
1.21      niallo    169:
1.50      niallo    170:                if ((file = rcs_open(fpath, RCS_RDWR|RCS_PARSE_FULLY)) == NULL)
1.1       joris     171:                        continue;
                    172:
1.37      xsa       173:                if (flags & PRESERVETIME)
                    174:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    175:
                    176:                if (kflag != RCS_KWEXP_ERR)
                    177:                        rcs_kwexp_set(file, kflag);
                    178:
1.4       joris     179:                if (rev == RCS_HEAD_REV)
                    180:                        frev = file->rf_head;
                    181:                else
                    182:                        frev = rev;
1.17      joris     183:
1.64      niallo    184:                if ((status = checkout_rev(file, frev, argv[i], flags,
                    185:                    username, author, state, date)) < 0) {
1.24      niallo    186:                                rcs_close(file);
                    187:                                continue;
1.8       niallo    188:                }
1.58      xsa       189:
                    190:                if (verbose == 1)
                    191:                        printf("done\n");
1.17      joris     192:
1.1       joris     193:                rcs_close(file);
1.37      xsa       194:
                    195:                if (flags & PRESERVETIME)
                    196:                        rcs_set_mtime(fpath, rcs_mtime);
1.1       joris     197:        }
                    198:
1.69      joris     199:        if ((rev != RCS_HEAD_REV) && (frev != NULL))
1.5       joris     200:                rcsnum_free(frev);
1.1       joris     201:
1.64      niallo    202:        return (status);
1.1       joris     203: }
                    204:
                    205: void
                    206: checkout_usage(void)
                    207: {
1.11      deraadt   208:        fprintf(stderr,
1.42      xsa       209:            "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
                    210:            "          [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
                    211:            "          [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1       joris     212: }
1.14      niallo    213:
                    214: /*
                    215:  * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29      xsa       216:  * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14      niallo    217:  *
1.49      joris     218:  * Looks up revision based upon <lockname>, <author>, <state> and <date>
1.44      joris     219:  *
1.14      niallo    220:  * Returns 0 on success, -1 on failure.
                    221:  */
                    222: int
1.24      niallo    223: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.49      joris     224:     const char *lockname, const char *author, const char *state,
                    225:     const char *date)
1.14      niallo    226: {
1.44      joris     227:        BUF *bp;
1.69      joris     228:        u_int i;
1.44      joris     229:        int lcount;
1.60      niallo    230:        char buf[16];
1.14      niallo    231:        mode_t mode = 0444;
1.18      joris     232:        struct stat st;
1.44      joris     233:        struct rcs_delta *rdp;
                    234:        struct rcs_lock *lkp;
1.49      joris     235:        char *content, msg[128], *fdate;
                    236:        time_t rcsdate, givendate;
1.69      joris     237:        RCSNUM *rev;
1.49      joris     238:
                    239:        rcsdate = givendate = -1;
                    240:        if (date != NULL)
                    241:                givendate = cvs_date_parse(date);
1.14      niallo    242:
1.69      joris     243:        /* XXX rcsnum_cmp()
                    244:         * Check out the latest revision if <frev> is greater than HEAD
                    245:         */
                    246:        for (i = 0; i < file->rf_head->rn_len; i++) {
                    247:                if (file->rf_head->rn_id[i] < frev->rn_id[i]) {
                    248:                        frev = file->rf_head;
                    249:                        break;
                    250:                }
                    251:        }
1.15      niallo    252:
1.44      joris     253:        lcount = 0;
                    254:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    255:                if (!strcmp(lkp->rl_name, lockname))
                    256:                        lcount++;
                    257:        }
                    258:
                    259:        /*
                    260:         * If the user didn't specify any revision, we cycle through
                    261:         * revisions to lookup the first one that matches what he specified.
                    262:         *
                    263:         * If we cannot find one, we return an error.
                    264:         */
                    265:        rdp = NULL;
                    266:        if (frev == file->rf_head) {
                    267:                if (lcount > 1) {
                    268:                        cvs_log(LP_WARN,
                    269:                            "multiple revisions locked by %s; "
                    270:                            "please specify one", lockname);
                    271:                        return (-1);
                    272:                }
                    273:
                    274:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
1.49      joris     275:                        if (date != NULL) {
                    276:                                fdate = asctime(&rdp->rd_date);
                    277:                                rcsdate = cvs_date_parse(fdate);
                    278:                                if (givendate <= rcsdate)
                    279:                                        continue;
                    280:                        }
                    281:
1.44      joris     282:                        if ((author != NULL) &&
                    283:                            (strcmp(rdp->rd_author, author)))
                    284:                                continue;
1.49      joris     285:
1.44      joris     286:                        if ((state != NULL) &&
                    287:                            (strcmp(rdp->rd_state, state)))
                    288:                                continue;
                    289:
                    290:                        frev = rdp->rd_num;
                    291:                        break;
                    292:                }
                    293:        } else {
                    294:                rdp = rcs_findrev(file, frev);
                    295:        }
                    296:
                    297:        if (rdp == NULL) {
1.49      joris     298:                checkout_err_nobranch(file, author, date, state, flags);
1.44      joris     299:                return (-1);
                    300:        }
                    301:
1.69      joris     302:        rev = rdp->rd_num;
                    303:        rcsnum_tostr(rev, buf, sizeof(buf));
1.15      niallo    304:
1.44      joris     305:        if (rdp->rd_locker != NULL) {
                    306:                if (strcmp(lockname, rdp->rd_locker)) {
                    307:                        strlcpy(msg, "Revision %s is already locked by %s; ",
                    308:                            sizeof(msg));
                    309:                        if (flags & CO_UNLOCK)
                    310:                                strlcat(msg, "use co -r or rcs -u", sizeof(msg));
                    311:                        cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
                    312:                        return (-1);
                    313:                }
                    314:        }
                    315:
1.53      xsa       316:        if ((verbose == 1) && !(flags & NEWFILE))
1.18      joris     317:                printf("revision %s", buf);
                    318:
1.44      joris     319:
1.69      joris     320:        if ((bp = rcs_getrev(file, rev)) == NULL) {
1.14      niallo    321:                cvs_log(LP_ERR, "cannot find revision `%s'", buf);
                    322:                return (-1);
                    323:        }
1.69      joris     324:
1.55      niallo    325:        /*
                    326:         * Do keyword expansion if required.
                    327:         */
1.69      joris     328:        bp = rcs_kwexp_buf(bp, file, rev);
1.14      niallo    329:
1.67      niallo    330:        /*
                    331:         * File inherits permissions from its ,v file
                    332:         */
                    333:        if (stat(file->rf_path, &st) == -1)
                    334:                fatal("could not stat rcsfile");
                    335:
                    336:        mode = st.st_mode;
                    337:
1.24      niallo    338:        if (flags & CO_LOCK) {
1.44      joris     339:                if ((lockname != NULL)
1.69      joris     340:                    && (rcs_lock_add(file, lockname, rev) < 0)) {
1.44      joris     341:                        if (rcs_errno != RCS_ERR_DUPENT)
                    342:                                return (-1);
1.14      niallo    343:                }
1.18      joris     344:
1.67      niallo    345:                /* Strip all write bits from mode */
                    346:                mode = st.st_mode &
                    347:                    (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
                    348:                mode |= S_IWUSR;
1.53      xsa       349:                if ((verbose == 1) && !(flags & NEWFILE))
1.57      xsa       350:                        printf(" (locked)");
1.24      niallo    351:        } else if (flags & CO_UNLOCK) {
1.69      joris     352:                if (rcs_lock_remove(file, lockname, rev) < 0) {
1.46      joris     353:                        if (rcs_errno != RCS_ERR_NOENT)
                    354:                                return (-1);
                    355:                }
1.18      joris     356:
1.67      niallo    357:                /* Strip all write bits from mode */
                    358:                mode = st.st_mode &
                    359:                    (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
1.53      xsa       360:                if ((verbose == 1) && !(flags & NEWFILE))
1.57      xsa       361:                        printf(" (unlocked)");
1.18      joris     362:        }
1.56      xsa       363:
                    364:        if ((verbose == 1) && !(flags & NEWFILE))
                    365:                printf("\n");
1.18      joris     366:
1.44      joris     367:        if (flags & CO_LOCK) {
                    368:                lcount++;
1.63      xsa       369:                if ((verbose == 1) && (lcount > 1))
                    370:                        cvs_log(LP_WARN, "%s: warning: You now have %d locks.",
                    371:                            file->rf_path, lcount);
1.44      joris     372:        }
                    373:
1.41      xsa       374:        if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
1.68      ray       375:                /*
                    376:                 * XXX - Not sure what is "right".  If we go according
                    377:                 * to GNU's behavior, an existing file with no writable
                    378:                 * bits is overwritten without prompting the user.
                    379:                 *
                    380:                 * This is dangerous, so we always prompt.
                    381:                 * Unfortunately this interferes with an unlocked
                    382:                 * checkout followed by a locked checkout, which should
                    383:                 * not prompt.  One (unimplemented) solution is to check
                    384:                 * if the existing file is the same as the checked out
                    385:                 * revision, and prompt if there are differences.
                    386:                 */
                    387:                if (st.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))
1.61      ray       388:                        printf("writable ");
                    389:                printf("%s exists%s; ", dst,
                    390:                    (getuid() == st.st_uid) ? "" :
                    391:                    ", and you do not own it");
                    392:                printf("remove it? [ny](n): ");
                    393:                /* default is n */
                    394:                if (cvs_yesno() == -1) {
1.68      ray       395:                        if ((verbose == 1) && isatty(STDIN_FILENO))
                    396:                                cvs_log(LP_ERR,
                    397:                                    "writable %s exists; checkout aborted",
                    398:                                    dst);
                    399:                        else
                    400:                                cvs_log(LP_ERR, "checkout aborted");
1.61      ray       401:                        return (-1);
1.18      joris     402:                }
1.14      niallo    403:        }
1.17      joris     404:
1.20      joris     405:        if (pipeout == 1) {
                    406:                cvs_buf_putc(bp, '\0');
                    407:                content = cvs_buf_release(bp);
                    408:                printf("%s", content);
1.47      joris     409:                xfree(content);
1.20      joris     410:        } else {
                    411:                if (cvs_buf_write(bp, dst, mode) < 0) {
                    412:                        cvs_log(LP_ERR, "failed to write revision to file");
                    413:                        cvs_buf_free(bp);
                    414:                        return (-1);
                    415:                }
1.14      niallo    416:                cvs_buf_free(bp);
1.24      niallo    417:                if (flags & CO_REVDATE) {
                    418:                        struct timeval tv[2];
1.36      xsa       419:                        memset(&tv, 0, sizeof(tv));
1.69      joris     420:                        tv[0].tv_sec = (long)rcs_rev_getdate(file, rev);
1.24      niallo    421:                        tv[1].tv_sec = tv[0].tv_sec;
                    422:                        if (utimes(dst, (const struct timeval *)&tv) < 0)
                    423:                                cvs_log(LP_ERRNO, "error setting utimes");
                    424:                }
1.14      niallo    425:        }
1.17      joris     426:
1.14      niallo    427:        return (0);
1.43      xsa       428: }
                    429:
                    430: /*
                    431:  * checkout_err_nobranch()
                    432:  *
                    433:  * XXX - should handle the dates too.
                    434:  */
                    435: static void
                    436: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
                    437:     const char *state, int flags)
                    438: {
                    439:        if (!(flags & CO_AUTHOR))
                    440:                author = NULL;
                    441:        if (!(flags & CO_STATE))
                    442:                state = NULL;
                    443:
                    444:        cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
                    445:            file->rf_path,
                    446:            date ? " a date before " : "",
                    447:            date ? date : "",
                    448:            author ? " and author " + (date ? 0:4 ) : "",
                    449:            author ? author : "",
                    450:            state  ? " and state " + (date || author ? 0:4) : "",
                    451:            state  ? state : "");
1.24      niallo    452: }