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

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