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

1.67    ! niallo      1: /*     $OpenBSD: co.c,v 1.66 2006/03/24 05:14:48 ray 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.66      ray        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++) {
                    157:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    158:                        continue;
                    159:
1.21      niallo    160:                if (verbose == 1)
1.22      xsa       161:                        printf("%s  -->  %s\n", fpath,
                    162:                            (pipeout == 1) ? "standard output" : argv[i]);
1.35      xsa       163:
                    164:                if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
                    165:                        cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
                    166:                        continue;
                    167:                }
1.21      niallo    168:
1.50      niallo    169:                if ((file = rcs_open(fpath, RCS_RDWR|RCS_PARSE_FULLY)) == NULL)
1.1       joris     170:                        continue;
                    171:
1.37      xsa       172:                if (flags & PRESERVETIME)
                    173:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    174:
                    175:                if (kflag != RCS_KWEXP_ERR)
                    176:                        rcs_kwexp_set(file, kflag);
                    177:
1.4       joris     178:                if (rev == RCS_HEAD_REV)
                    179:                        frev = file->rf_head;
                    180:                else
                    181:                        frev = rev;
1.17      joris     182:
1.64      niallo    183:                if ((status = checkout_rev(file, frev, argv[i], flags,
                    184:                    username, author, state, date)) < 0) {
1.24      niallo    185:                                rcs_close(file);
                    186:                                continue;
1.8       niallo    187:                }
1.58      xsa       188:
                    189:                if (verbose == 1)
                    190:                        printf("done\n");
1.17      joris     191:
1.1       joris     192:                rcs_close(file);
1.37      xsa       193:
                    194:                if (flags & PRESERVETIME)
                    195:                        rcs_set_mtime(fpath, rcs_mtime);
1.1       joris     196:        }
                    197:
                    198:        if (rev != RCS_HEAD_REV)
1.5       joris     199:                rcsnum_free(frev);
1.1       joris     200:
1.64      niallo    201:        return (status);
1.1       joris     202: }
                    203:
                    204: void
                    205: checkout_usage(void)
                    206: {
1.11      deraadt   207:        fprintf(stderr,
1.42      xsa       208:            "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
                    209:            "          [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
                    210:            "          [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1       joris     211: }
1.14      niallo    212:
                    213: /*
                    214:  * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29      xsa       215:  * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14      niallo    216:  *
1.49      joris     217:  * Looks up revision based upon <lockname>, <author>, <state> and <date>
1.44      joris     218:  *
1.14      niallo    219:  * Returns 0 on success, -1 on failure.
                    220:  */
                    221: int
1.24      niallo    222: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.49      joris     223:     const char *lockname, const char *author, const char *state,
                    224:     const char *date)
1.14      niallo    225: {
1.44      joris     226:        BUF *bp;
                    227:        int lcount;
1.60      niallo    228:        char buf[16];
1.14      niallo    229:        mode_t mode = 0444;
1.18      joris     230:        struct stat st;
1.44      joris     231:        struct rcs_delta *rdp;
                    232:        struct rcs_lock *lkp;
1.49      joris     233:        char *content, msg[128], *fdate;
                    234:        time_t rcsdate, givendate;
                    235:
                    236:        rcsdate = givendate = -1;
                    237:        if (date != NULL)
                    238:                givendate = cvs_date_parse(date);
1.14      niallo    239:
1.39      xsa       240:        /* Check out the latest revision if <frev> is greater than HEAD */
1.15      niallo    241:        if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
1.23      xsa       242:                frev = file->rf_head;
1.15      niallo    243:
1.44      joris     244:        lcount = 0;
                    245:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    246:                if (!strcmp(lkp->rl_name, lockname))
                    247:                        lcount++;
                    248:        }
                    249:
                    250:        /*
                    251:         * If the user didn't specify any revision, we cycle through
                    252:         * revisions to lookup the first one that matches what he specified.
                    253:         *
                    254:         * If we cannot find one, we return an error.
                    255:         */
                    256:        rdp = NULL;
                    257:        if (frev == file->rf_head) {
                    258:                if (lcount > 1) {
                    259:                        cvs_log(LP_WARN,
                    260:                            "multiple revisions locked by %s; "
                    261:                            "please specify one", lockname);
                    262:                        return (-1);
                    263:                }
                    264:
                    265:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
1.49      joris     266:                        if (date != NULL) {
                    267:                                fdate = asctime(&rdp->rd_date);
                    268:                                rcsdate = cvs_date_parse(fdate);
                    269:                                if (givendate <= rcsdate)
                    270:                                        continue;
                    271:                        }
                    272:
1.44      joris     273:                        if ((author != NULL) &&
                    274:                            (strcmp(rdp->rd_author, author)))
                    275:                                continue;
1.49      joris     276:
1.44      joris     277:                        if ((state != NULL) &&
                    278:                            (strcmp(rdp->rd_state, state)))
                    279:                                continue;
                    280:
                    281:                        frev = rdp->rd_num;
                    282:                        break;
                    283:                }
                    284:        } else {
                    285:                rdp = rcs_findrev(file, frev);
                    286:        }
                    287:
                    288:        if (rdp == NULL) {
1.49      joris     289:                checkout_err_nobranch(file, author, date, state, flags);
1.44      joris     290:                return (-1);
                    291:        }
                    292:
1.15      niallo    293:        rcsnum_tostr(frev, buf, sizeof(buf));
                    294:
1.44      joris     295:        if (rdp->rd_locker != NULL) {
                    296:                if (strcmp(lockname, rdp->rd_locker)) {
                    297:                        strlcpy(msg, "Revision %s is already locked by %s; ",
                    298:                            sizeof(msg));
                    299:                        if (flags & CO_UNLOCK)
                    300:                                strlcat(msg, "use co -r or rcs -u", sizeof(msg));
                    301:                        cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
                    302:                        return (-1);
                    303:                }
                    304:        }
                    305:
1.53      xsa       306:        if ((verbose == 1) && !(flags & NEWFILE))
1.18      joris     307:                printf("revision %s", buf);
                    308:
1.44      joris     309:
1.14      niallo    310:        if ((bp = rcs_getrev(file, frev)) == NULL) {
                    311:                cvs_log(LP_ERR, "cannot find revision `%s'", buf);
                    312:                return (-1);
                    313:        }
1.55      niallo    314:        /*
                    315:         * Do keyword expansion if required.
                    316:         */
                    317:        bp = rcs_kwexp_buf(bp, file, frev);
1.14      niallo    318:
1.67    ! niallo    319:        /*
        !           320:         * File inherits permissions from its ,v file
        !           321:         */
        !           322:        if (stat(file->rf_path, &st) == -1)
        !           323:                fatal("could not stat rcsfile");
        !           324:
        !           325:        mode = st.st_mode;
        !           326:
1.24      niallo    327:        if (flags & CO_LOCK) {
1.44      joris     328:                if ((lockname != NULL)
                    329:                    && (rcs_lock_add(file, lockname, frev) < 0)) {
                    330:                        if (rcs_errno != RCS_ERR_DUPENT)
                    331:                                return (-1);
1.14      niallo    332:                }
1.18      joris     333:
1.67    ! niallo    334:                /* Strip all write bits from mode */
        !           335:                mode = st.st_mode &
        !           336:                    (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
        !           337:                mode |= S_IWUSR;
1.53      xsa       338:                if ((verbose == 1) && !(flags & NEWFILE))
1.57      xsa       339:                        printf(" (locked)");
1.24      niallo    340:        } else if (flags & CO_UNLOCK) {
1.46      joris     341:                if (rcs_lock_remove(file, lockname, frev) < 0) {
                    342:                        if (rcs_errno != RCS_ERR_NOENT)
                    343:                                return (-1);
                    344:                }
1.18      joris     345:
1.67    ! niallo    346:                /* Strip all write bits from mode */
        !           347:                mode = st.st_mode &
        !           348:                    (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
1.53      xsa       349:                if ((verbose == 1) && !(flags & NEWFILE))
1.57      xsa       350:                        printf(" (unlocked)");
1.18      joris     351:        }
1.56      xsa       352:
                    353:        if ((verbose == 1) && !(flags & NEWFILE))
                    354:                printf("\n");
1.18      joris     355:
1.44      joris     356:        if (flags & CO_LOCK) {
                    357:                lcount++;
1.63      xsa       358:                if ((verbose == 1) && (lcount > 1))
                    359:                        cvs_log(LP_WARN, "%s: warning: You now have %d locks.",
                    360:                            file->rf_path, lcount);
1.44      joris     361:        }
                    362:
1.41      xsa       363:        if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
1.61      ray       364:                if (verbose == 0) {
                    365:                        cvs_log(LP_ERR,
                    366:                            "writable %s exists; checkout aborted",
                    367:                            dst);
                    368:                        return (-1);
                    369:                }
1.41      xsa       370:
1.61      ray       371:                if (st.st_mode & S_IWUSR)
                    372:                        printf("writable ");
                    373:                printf("%s exists%s; ", dst,
                    374:                    (getuid() == st.st_uid) ? "" :
                    375:                    ", and you do not own it");
                    376:                printf("remove it? [ny](n): ");
                    377:                /* default is n */
                    378:                if (cvs_yesno() == -1) {
                    379:                        cvs_log(LP_ERR, "checkout aborted");
                    380:                        return (-1);
1.18      joris     381:                }
1.14      niallo    382:        }
1.17      joris     383:
1.20      joris     384:        if (pipeout == 1) {
                    385:                cvs_buf_putc(bp, '\0');
                    386:                content = cvs_buf_release(bp);
                    387:                printf("%s", content);
1.47      joris     388:                xfree(content);
1.20      joris     389:        } else {
                    390:                if (cvs_buf_write(bp, dst, mode) < 0) {
                    391:                        cvs_log(LP_ERR, "failed to write revision to file");
                    392:                        cvs_buf_free(bp);
                    393:                        return (-1);
                    394:                }
1.14      niallo    395:                cvs_buf_free(bp);
1.24      niallo    396:                if (flags & CO_REVDATE) {
                    397:                        struct timeval tv[2];
1.36      xsa       398:                        memset(&tv, 0, sizeof(tv));
1.24      niallo    399:                        tv[0].tv_sec = (long)rcs_rev_getdate(file, frev);
                    400:                        tv[1].tv_sec = tv[0].tv_sec;
                    401:                        if (utimes(dst, (const struct timeval *)&tv) < 0)
                    402:                                cvs_log(LP_ERRNO, "error setting utimes");
                    403:                }
1.14      niallo    404:        }
1.17      joris     405:
1.14      niallo    406:        return (0);
1.43      xsa       407: }
                    408:
                    409: /*
                    410:  * checkout_err_nobranch()
                    411:  *
                    412:  * XXX - should handle the dates too.
                    413:  */
                    414: static void
                    415: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
                    416:     const char *state, int flags)
                    417: {
                    418:        if (!(flags & CO_AUTHOR))
                    419:                author = NULL;
                    420:        if (!(flags & CO_STATE))
                    421:                state = NULL;
                    422:
                    423:        cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
                    424:            file->rf_path,
                    425:            date ? " a date before " : "",
                    426:            date ? date : "",
                    427:            author ? " and author " + (date ? 0:4 ) : "",
                    428:            author ? author : "",
                    429:            state  ? " and state " + (date || author ? 0:4) : "",
                    430:            state  ? state : "");
1.24      niallo    431: }