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

1.62    ! ray         1: /*     $OpenBSD: co.c,v 1.61 2006/03/16 04:01:46 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.59      joris      31: #define CO_OPTSTRING   "d:f::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.33      xsa        39:        int i, ch, flags, kflag;
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.24      niallo     47:        flags = 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.33      xsa        64:                case 'k':
                     65:                        kflag = rcs_kflag_get(rcs_optarg);
                     66:                        if (RCS_KWEXP_INVAL(kflag)) {
                     67:                                cvs_log(LP_ERR,
                     68:                                    "invalid RCS keyword expansion mode");
                     69:                                (usage)();
                     70:                                exit(1);
                     71:                        }
                     72:                        break;
1.4       joris      73:                case 'l':
1.19      joris      74:                        rcs_set_rev(rcs_optarg, &rev);
1.53      xsa        75:                        if (flags & CO_UNLOCK) {
                     76:                                cvs_log(LP_ERR, "warning: -u overridden by -l");
                     77:                                flags &= ~CO_UNLOCK;
                     78:                        }
1.24      niallo     79:                        flags |= CO_LOCK;
1.26      niallo     80:                        break;
                     81:                case 'M':
                     82:                        rcs_set_rev(rcs_optarg, &rev);
                     83:                        flags |= CO_REVDATE;
1.4       joris      84:                        break;
1.20      joris      85:                case 'p':
                     86:                        rcs_set_rev(rcs_optarg, &rev);
                     87:                        pipeout = 1;
                     88:                        break;
1.3       joris      89:                case 'q':
1.32      xsa        90:                        rcs_set_rev(rcs_optarg, &rev);
1.3       joris      91:                        verbose = 0;
                     92:                        break;
1.1       joris      93:                case 'r':
1.19      joris      94:                        rcs_set_rev(rcs_optarg, &rev);
1.4       joris      95:                        break;
1.24      niallo     96:                case 's':
1.47      joris      97:                        state = xstrdup(rcs_optarg);
1.24      niallo     98:                        flags |= CO_STATE;
1.34      xsa        99:                        break;
                    100:                case 'T':
                    101:                        flags |= PRESERVETIME;
1.24      niallo    102:                        break;
1.4       joris     103:                case 'u':
1.19      joris     104:                        rcs_set_rev(rcs_optarg, &rev);
1.53      xsa       105:                        if (flags & CO_LOCK) {
                    106:                                cvs_log(LP_ERR, "warning: -l overridden by -u");
                    107:                                flags &= ~CO_LOCK;
                    108:                        }
1.24      niallo    109:                        flags |= CO_UNLOCK;
1.1       joris     110:                        break;
1.7       joris     111:                case 'V':
                    112:                        printf("%s\n", rcs_version);
                    113:                        exit(0);
1.62    ! ray       114:                        /* NOTREACHED */
1.38      xsa       115:                case 'w':
1.44      joris     116:                        /* if no argument, assume current user */
                    117:                        if (rcs_optarg == NULL) {
1.48      xsa       118:                                if ((author = getlogin()) == NULL)
                    119:                                        fatal("getlogin failed");
1.47      joris     120:                        } else
                    121:                                author = xstrdup(rcs_optarg);
1.43      xsa       122:                        flags |= CO_AUTHOR;
1.38      xsa       123:                        break;
1.30      xsa       124:                case 'x':
                    125:                        rcs_suffixes = rcs_optarg;
1.59      joris     126:                        break;
                    127:                case 'z':
                    128:                        timezone_flag = rcs_optarg;
1.30      xsa       129:                        break;
1.1       joris     130:                default:
                    131:                        (usage)();
                    132:                        exit(1);
                    133:                }
                    134:        }
                    135:
1.13      joris     136:        argc -= rcs_optind;
                    137:        argv += rcs_optind;
1.1       joris     138:
                    139:        if (argc == 0) {
                    140:                cvs_log(LP_ERR, "no input file");
                    141:                (usage)();
                    142:                exit (1);
                    143:        }
1.11      deraadt   144:
1.44      joris     145:        if ((username = getlogin()) == NULL) {
1.38      xsa       146:                cvs_log(LP_ERRNO, "failed to get username");
                    147:                exit (1);
                    148:        }
                    149:
1.1       joris     150:        for (i = 0; i < argc; i++) {
                    151:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    152:                        continue;
                    153:
1.21      niallo    154:                if (verbose == 1)
1.22      xsa       155:                        printf("%s  -->  %s\n", fpath,
                    156:                            (pipeout == 1) ? "standard output" : argv[i]);
1.35      xsa       157:
                    158:                if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
                    159:                        cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
                    160:                        continue;
                    161:                }
1.21      niallo    162:
1.50      niallo    163:                if ((file = rcs_open(fpath, RCS_RDWR|RCS_PARSE_FULLY)) == NULL)
1.1       joris     164:                        continue;
                    165:
1.37      xsa       166:                if (flags & PRESERVETIME)
                    167:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    168:
                    169:                if (kflag != RCS_KWEXP_ERR)
                    170:                        rcs_kwexp_set(file, kflag);
                    171:
1.4       joris     172:                if (rev == RCS_HEAD_REV)
                    173:                        frev = file->rf_head;
                    174:                else
                    175:                        frev = rev;
1.17      joris     176:
1.44      joris     177:                if (checkout_rev(file, frev, argv[i], flags,
1.49      joris     178:                    username, author, state, date) < 0) {
1.24      niallo    179:                                rcs_close(file);
                    180:                                continue;
1.8       niallo    181:                }
1.58      xsa       182:
                    183:                if (verbose == 1)
                    184:                        printf("done\n");
1.17      joris     185:
1.1       joris     186:                rcs_close(file);
1.37      xsa       187:
                    188:                if (flags & PRESERVETIME)
                    189:                        rcs_set_mtime(fpath, rcs_mtime);
1.1       joris     190:        }
                    191:
                    192:        if (rev != RCS_HEAD_REV)
1.5       joris     193:                rcsnum_free(frev);
1.1       joris     194:
                    195:        return (0);
                    196: }
                    197:
                    198: void
                    199: checkout_usage(void)
                    200: {
1.11      deraadt   201:        fprintf(stderr,
1.42      xsa       202:            "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
                    203:            "          [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
                    204:            "          [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1       joris     205: }
1.14      niallo    206:
                    207: /*
                    208:  * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29      xsa       209:  * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14      niallo    210:  *
1.49      joris     211:  * Looks up revision based upon <lockname>, <author>, <state> and <date>
1.44      joris     212:  *
1.14      niallo    213:  * Returns 0 on success, -1 on failure.
                    214:  */
                    215: int
1.24      niallo    216: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.49      joris     217:     const char *lockname, const char *author, const char *state,
                    218:     const char *date)
1.14      niallo    219: {
1.44      joris     220:        BUF *bp;
                    221:        int lcount;
1.60      niallo    222:        char buf[16];
1.14      niallo    223:        mode_t mode = 0444;
1.18      joris     224:        struct stat st;
1.44      joris     225:        struct rcs_delta *rdp;
                    226:        struct rcs_lock *lkp;
1.49      joris     227:        char *content, msg[128], *fdate;
                    228:        time_t rcsdate, givendate;
                    229:
                    230:        rcsdate = givendate = -1;
                    231:        if (date != NULL)
                    232:                givendate = cvs_date_parse(date);
1.14      niallo    233:
1.39      xsa       234:        /* Check out the latest revision if <frev> is greater than HEAD */
1.15      niallo    235:        if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
1.23      xsa       236:                frev = file->rf_head;
1.15      niallo    237:
1.44      joris     238:        lcount = 0;
                    239:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    240:                if (!strcmp(lkp->rl_name, lockname))
                    241:                        lcount++;
                    242:        }
                    243:
                    244:        /*
                    245:         * If the user didn't specify any revision, we cycle through
                    246:         * revisions to lookup the first one that matches what he specified.
                    247:         *
                    248:         * If we cannot find one, we return an error.
                    249:         */
                    250:        rdp = NULL;
                    251:        if (frev == file->rf_head) {
                    252:                if (lcount > 1) {
                    253:                        cvs_log(LP_WARN,
                    254:                            "multiple revisions locked by %s; "
                    255:                            "please specify one", lockname);
                    256:                        return (-1);
                    257:                }
                    258:
                    259:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
1.49      joris     260:                        if (date != NULL) {
                    261:                                fdate = asctime(&rdp->rd_date);
                    262:                                rcsdate = cvs_date_parse(fdate);
                    263:                                if (givendate <= rcsdate)
                    264:                                        continue;
                    265:                        }
                    266:
1.44      joris     267:                        if ((author != NULL) &&
                    268:                            (strcmp(rdp->rd_author, author)))
                    269:                                continue;
1.49      joris     270:
1.44      joris     271:                        if ((state != NULL) &&
                    272:                            (strcmp(rdp->rd_state, state)))
                    273:                                continue;
                    274:
                    275:                        frev = rdp->rd_num;
                    276:                        break;
                    277:                }
                    278:        } else {
                    279:                rdp = rcs_findrev(file, frev);
                    280:        }
                    281:
                    282:        if (rdp == NULL) {
1.49      joris     283:                checkout_err_nobranch(file, author, date, state, flags);
1.44      joris     284:                return (-1);
                    285:        }
                    286:
1.15      niallo    287:        rcsnum_tostr(frev, buf, sizeof(buf));
                    288:
1.44      joris     289:        if (rdp->rd_locker != NULL) {
                    290:                if (strcmp(lockname, rdp->rd_locker)) {
                    291:                        strlcpy(msg, "Revision %s is already locked by %s; ",
                    292:                            sizeof(msg));
                    293:                        if (flags & CO_UNLOCK)
                    294:                                strlcat(msg, "use co -r or rcs -u", sizeof(msg));
                    295:                        cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
                    296:                        return (-1);
                    297:                }
                    298:        }
                    299:
1.53      xsa       300:        if ((verbose == 1) && !(flags & NEWFILE))
1.18      joris     301:                printf("revision %s", buf);
                    302:
1.44      joris     303:
1.14      niallo    304:        if ((bp = rcs_getrev(file, frev)) == NULL) {
                    305:                cvs_log(LP_ERR, "cannot find revision `%s'", buf);
                    306:                return (-1);
                    307:        }
1.55      niallo    308:        /*
                    309:         * Do keyword expansion if required.
                    310:         */
                    311:        bp = rcs_kwexp_buf(bp, file, frev);
1.14      niallo    312:
1.24      niallo    313:        if (flags & CO_LOCK) {
1.44      joris     314:                if ((lockname != NULL)
                    315:                    && (rcs_lock_add(file, lockname, frev) < 0)) {
                    316:                        if (rcs_errno != RCS_ERR_DUPENT)
                    317:                                return (-1);
1.14      niallo    318:                }
1.18      joris     319:
1.14      niallo    320:                mode = 0644;
1.53      xsa       321:                if ((verbose == 1) && !(flags & NEWFILE))
1.57      xsa       322:                        printf(" (locked)");
1.24      niallo    323:        } else if (flags & CO_UNLOCK) {
1.46      joris     324:                if (rcs_lock_remove(file, lockname, frev) < 0) {
                    325:                        if (rcs_errno != RCS_ERR_NOENT)
                    326:                                return (-1);
                    327:                }
1.18      joris     328:
1.14      niallo    329:                mode = 0444;
1.53      xsa       330:                if ((verbose == 1) && !(flags & NEWFILE))
1.57      xsa       331:                        printf(" (unlocked)");
1.18      joris     332:        }
1.56      xsa       333:
                    334:        if ((verbose == 1) && !(flags & NEWFILE))
                    335:                printf("\n");
1.18      joris     336:
1.44      joris     337:        if (flags & CO_LOCK) {
                    338:                lcount++;
                    339:                if (lcount > 1)
                    340:                        cvs_log(LP_WARN, "You now have %d locks.", lcount);
                    341:        }
                    342:
1.41      xsa       343:        if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
1.61      ray       344:                if (verbose == 0) {
                    345:                        cvs_log(LP_ERR,
                    346:                            "writable %s exists; checkout aborted",
                    347:                            dst);
                    348:                        return (-1);
                    349:                }
1.41      xsa       350:
1.61      ray       351:                if (st.st_mode & S_IWUSR)
                    352:                        printf("writable ");
                    353:                printf("%s exists%s; ", dst,
                    354:                    (getuid() == st.st_uid) ? "" :
                    355:                    ", and you do not own it");
                    356:                printf("remove it? [ny](n): ");
                    357:                /* default is n */
                    358:                if (cvs_yesno() == -1) {
                    359:                        cvs_log(LP_ERR, "checkout aborted");
                    360:                        return (-1);
1.18      joris     361:                }
1.14      niallo    362:        }
1.17      joris     363:
1.20      joris     364:        if (pipeout == 1) {
                    365:                cvs_buf_putc(bp, '\0');
                    366:                content = cvs_buf_release(bp);
                    367:                printf("%s", content);
1.47      joris     368:                xfree(content);
1.20      joris     369:        } else {
                    370:                if (cvs_buf_write(bp, dst, mode) < 0) {
                    371:                        cvs_log(LP_ERR, "failed to write revision to file");
                    372:                        cvs_buf_free(bp);
                    373:                        return (-1);
                    374:                }
1.14      niallo    375:                cvs_buf_free(bp);
1.24      niallo    376:                if (flags & CO_REVDATE) {
                    377:                        struct timeval tv[2];
1.36      xsa       378:                        memset(&tv, 0, sizeof(tv));
1.24      niallo    379:                        tv[0].tv_sec = (long)rcs_rev_getdate(file, frev);
                    380:                        tv[1].tv_sec = tv[0].tv_sec;
                    381:                        if (utimes(dst, (const struct timeval *)&tv) < 0)
                    382:                                cvs_log(LP_ERRNO, "error setting utimes");
                    383:                }
1.14      niallo    384:        }
1.17      joris     385:
1.14      niallo    386:        return (0);
1.43      xsa       387: }
                    388:
                    389: /*
                    390:  * checkout_err_nobranch()
                    391:  *
                    392:  * XXX - should handle the dates too.
                    393:  */
                    394: static void
                    395: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
                    396:     const char *state, int flags)
                    397: {
                    398:        if (!(flags & CO_AUTHOR))
                    399:                author = NULL;
                    400:        if (!(flags & CO_STATE))
                    401:                state = NULL;
                    402:
                    403:        cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
                    404:            file->rf_path,
                    405:            date ? " a date before " : "",
                    406:            date ? date : "",
                    407:            author ? " and author " + (date ? 0:4 ) : "",
                    408:            author ? author : "",
                    409:            state  ? " and state " + (date || author ? 0:4) : "",
                    410:            state  ? state : "");
1.24      niallo    411: }