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

1.37    ! xsa         1: /*     $OpenBSD: co.c,v 1.36 2005/11/24 15:35:11 xsa 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.24      niallo     39: static int checkout_state(RCSFILE *, RCSNUM *, const char *, int,
                     40:     const char *, const char *);
1.4       joris      41:
1.1       joris      42: int
                     43: checkout_main(int argc, char **argv)
                     44: {
1.33      xsa        45:        int i, ch, flags, kflag;
1.4       joris      46:        RCSNUM *frev, *rev;
1.1       joris      47:        RCSFILE *file;
1.14      niallo     48:        char fpath[MAXPATHLEN], buf[16];
1.4       joris      49:        char *username;
1.24      niallo     50:        const char *state = NULL;
1.37    ! xsa        51:        time_t rcs_mtime = -1;
1.1       joris      52:
1.24      niallo     53:        flags = 0;
1.33      xsa        54:        kflag = RCS_KWEXP_ERR;
1.1       joris      55:        rev = RCS_HEAD_REV;
1.6       joris      56:        frev = NULL;
1.4       joris      57:
                     58:        if ((username = getlogin()) == NULL) {
1.10      xsa        59:                cvs_log(LP_ERRNO, "failed to get username");
1.4       joris      60:                exit (1);
                     61:        }
                     62:
1.37    ! xsa        63:        while ((ch = rcs_getopt(argc, argv, "f::k:l::M::p::q::r::s:Tu::Vx:")) != -1) {
1.1       joris      64:                switch (ch) {
1.18      joris      65:                case 'f':
1.19      joris      66:                        rcs_set_rev(rcs_optarg, &rev);
1.24      niallo     67:                        flags |= FORCE;
1.18      joris      68:                        break;
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.24      niallo     80:                        flags |= CO_LOCK;
1.26      niallo     81:                        break;
                     82:                case 'M':
                     83:                        rcs_set_rev(rcs_optarg, &rev);
                     84:                        flags |= CO_REVDATE;
1.4       joris      85:                        break;
1.20      joris      86:                case 'p':
                     87:                        rcs_set_rev(rcs_optarg, &rev);
                     88:                        pipeout = 1;
                     89:                        break;
1.3       joris      90:                case 'q':
1.32      xsa        91:                        rcs_set_rev(rcs_optarg, &rev);
1.3       joris      92:                        verbose = 0;
                     93:                        break;
1.1       joris      94:                case 'r':
1.19      joris      95:                        rcs_set_rev(rcs_optarg, &rev);
1.4       joris      96:                        break;
1.24      niallo     97:                case 's':
                     98:                        if ((state = strdup(rcs_optarg)) == NULL) {
                     99:                                cvs_log(LP_ERRNO, "out of memory");
                    100:                                exit(1);
                    101:                        }
                    102:                        flags |= CO_STATE;
1.34      xsa       103:                        break;
                    104:                case 'T':
                    105:                        flags |= PRESERVETIME;
1.24      niallo    106:                        break;
1.4       joris     107:                case 'u':
1.19      joris     108:                        rcs_set_rev(rcs_optarg, &rev);
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.30      xsa       114:                case 'x':
                    115:                        rcs_suffixes = rcs_optarg;
                    116:                        break;
1.1       joris     117:                default:
                    118:                        (usage)();
                    119:                        exit(1);
                    120:                }
                    121:        }
                    122:
1.13      joris     123:        argc -= rcs_optind;
                    124:        argv += rcs_optind;
1.1       joris     125:
                    126:        if (argc == 0) {
                    127:                cvs_log(LP_ERR, "no input file");
                    128:                (usage)();
                    129:                exit (1);
                    130:        }
1.11      deraadt   131:
1.1       joris     132:        for (i = 0; i < argc; i++) {
                    133:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    134:                        continue;
                    135:
1.21      niallo    136:                if (verbose == 1)
1.22      xsa       137:                        printf("%s  -->  %s\n", fpath,
                    138:                            (pipeout == 1) ? "standard output" : argv[i]);
1.35      xsa       139:
                    140:                if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
                    141:                        cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
                    142:                        continue;
                    143:                }
1.21      niallo    144:
1.4       joris     145:                if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1       joris     146:                        continue;
                    147:
1.37    ! xsa       148:                if (flags & PRESERVETIME)
        !           149:                        rcs_mtime = rcs_get_mtime(file->rf_path);
        !           150:
        !           151:                if (kflag != RCS_KWEXP_ERR)
        !           152:                        rcs_kwexp_set(file, kflag);
        !           153:
1.4       joris     154:                if (rev == RCS_HEAD_REV)
                    155:                        frev = file->rf_head;
                    156:                else
                    157:                        frev = rev;
1.17      joris     158:
1.4       joris     159:                rcsnum_tostr(frev, buf, sizeof(buf));
1.33      xsa       160:
1.24      niallo    161:                if (flags & CO_STATE) {
                    162:                        if (checkout_state(file, frev, argv[i], flags,
                    163:                            username, state) < 0) {
                    164:                                rcs_close(file);
                    165:                                continue;
                    166:                        }
1.37    ! xsa       167:                } else {
1.24      niallo    168:                        if (checkout_rev(file, frev, argv[i], flags,
                    169:                                username) < 0) {
                    170:                                rcs_close(file);
                    171:                                continue;
                    172:                        }
1.8       niallo    173:                }
1.17      joris     174:
1.1       joris     175:                rcs_close(file);
1.37    ! xsa       176:
        !           177:                if (flags & PRESERVETIME)
        !           178:                        rcs_set_mtime(fpath, rcs_mtime);
1.1       joris     179:        }
                    180:
                    181:        if (rev != RCS_HEAD_REV)
1.5       joris     182:                rcsnum_free(frev);
1.1       joris     183:
                    184:        return (0);
                    185: }
                    186:
                    187: void
                    188: checkout_usage(void)
                    189: {
1.11      deraadt   190:        fprintf(stderr,
1.32      xsa       191:            "usage: co [-V] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
                    192:            "          [-M[rev]] [-mmsg] [-p[rev]] [-q[rev]] [-r[rev]]\n"
                    193:            "          [-sstate] [-u[rev]] [-wuser] [-xsuffixes] [-ztz] file ...\n");
1.1       joris     194: }
1.14      niallo    195:
                    196: /*
                    197:  * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29      xsa       198:  * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14      niallo    199:  *
                    200:  * Returns 0 on success, -1 on failure.
                    201:  */
                    202: int
1.24      niallo    203: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
                    204:     const char *username)
1.14      niallo    205: {
1.18      joris     206:        char buf[16], yn;
1.14      niallo    207:        mode_t mode = 0444;
                    208:        BUF *bp;
1.18      joris     209:        struct stat st;
1.20      joris     210:        char *content;
1.14      niallo    211:
                    212:        /*
1.15      niallo    213:         * Check out the latest revision if <frev> is greater than HEAD
1.14      niallo    214:         */
1.15      niallo    215:        if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
1.23      xsa       216:                frev = file->rf_head;
1.15      niallo    217:
                    218:        rcsnum_tostr(frev, buf, sizeof(buf));
                    219:
1.18      joris     220:        if (verbose == 1)
                    221:                printf("revision %s", buf);
                    222:
1.14      niallo    223:        if ((bp = rcs_getrev(file, frev)) == NULL) {
                    224:                cvs_log(LP_ERR, "cannot find revision `%s'", buf);
                    225:                return (-1);
                    226:        }
                    227:
1.24      niallo    228:        if (flags & CO_LOCK) {
1.14      niallo    229:                if ((username != NULL)
                    230:                    && (rcs_lock_add(file, username, frev) < 0)) {
1.21      niallo    231:                        if ((rcs_errno != RCS_ERR_DUPENT) && (verbose == 1))
1.14      niallo    232:                                cvs_log(LP_ERR, "failed to lock '%s'", buf);
                    233:                }
1.18      joris     234:
1.14      niallo    235:                mode = 0644;
1.18      joris     236:                if (verbose == 1)
                    237:                        printf(" (locked)");
1.24      niallo    238:        } else if (flags & CO_UNLOCK) {
1.14      niallo    239:                if (rcs_lock_remove(file, frev) < 0) {
                    240:                        if (rcs_errno != RCS_ERR_NOENT)
                    241:                                cvs_log(LP_ERR,
                    242:                                    "failed to remove lock '%s'", buf);
                    243:                }
1.18      joris     244:
1.14      niallo    245:                mode = 0444;
1.18      joris     246:                if (verbose == 1)
                    247:                        printf(" (unlocked)");
                    248:        }
                    249:
                    250:        if (verbose == 1)
                    251:                printf("\n");
                    252:
1.31      xsa       253:        if ((pipeout == 0) && (stat(dst, &st) != -1) && !(flags & FORCE)) {
1.18      joris     254:                if (st.st_mode & S_IWUSR) {
                    255:                        yn = 0;
1.21      niallo    256:                        if (verbose == 0) {
                    257:                                cvs_log(LP_ERR,
                    258:                                    "writeable %s exists; checkout aborted",
                    259:                                    dst);
                    260:                                return (-1);
                    261:                        }
1.18      joris     262:                        while (yn != 'y' && yn != 'n') {
1.21      niallo    263:                                printf("writeable %s exists; ", dst);
                    264:                                printf("remove it? [ny](n): ");
1.18      joris     265:                                fflush(stdout);
                    266:                                yn = getchar();
                    267:                        }
                    268:
                    269:                        if (yn == 'n') {
1.21      niallo    270:                                cvs_log(LP_ERR, "checkout aborted");
1.18      joris     271:                                return (-1);
                    272:                        }
                    273:                }
1.14      niallo    274:        }
1.17      joris     275:
1.20      joris     276:        if (pipeout == 1) {
                    277:                cvs_buf_putc(bp, '\0');
                    278:                content = cvs_buf_release(bp);
                    279:                printf("%s", content);
                    280:                free(content);
                    281:        } else {
                    282:                if (cvs_buf_write(bp, dst, mode) < 0) {
                    283:                        cvs_log(LP_ERR, "failed to write revision to file");
                    284:                        cvs_buf_free(bp);
                    285:                        return (-1);
                    286:                }
1.14      niallo    287:                cvs_buf_free(bp);
1.24      niallo    288:                if (flags & CO_REVDATE) {
                    289:                        struct timeval tv[2];
1.36      xsa       290:                        memset(&tv, 0, sizeof(tv));
1.24      niallo    291:                        tv[0].tv_sec = (long)rcs_rev_getdate(file, frev);
                    292:                        tv[1].tv_sec = tv[0].tv_sec;
                    293:                        if (utimes(dst, (const struct timeval *)&tv) < 0)
                    294:                                cvs_log(LP_ERRNO, "error setting utimes");
                    295:                }
1.20      joris     296:
                    297:                if (verbose == 1)
                    298:                        printf("done\n");
1.14      niallo    299:        }
1.17      joris     300:
1.14      niallo    301:        return (0);
                    302: }
                    303:
1.24      niallo    304: /*
                    305:  * checkout_state()
                    306:  *
                    307:  * Search from supplied revision backwards until we find one
                    308:  * with state <state> and check that out.
                    309:  *
1.25      niallo    310:  * Returns 0 on success, -1 on checkout_rev failure.
1.24      niallo    311:  */
                    312: static int
                    313: checkout_state(RCSFILE *rfp, RCSNUM *rev, const char *dst, int flags,
                    314:     const char *username, const char *state)
                    315: {
                    316:        const char *tstate;
                    317:
                    318:        if (rev == NULL) {
                    319:                cvs_log(LP_ERR, "%s: No revision on branch has state %s",
                    320:                    rfp->rf_path, state);
                    321:                return (-1);
                    322:        }
                    323:        else {
                    324:                if (((tstate = rcs_state_get(rfp, rev)) != NULL)
                    325:                    && (strcmp(state, tstate) == 0))
                    326:                        return (checkout_rev(rfp, rev, dst, flags, username));
                    327:                else
                    328:                        rev = rcsnum_dec(rev);
                    329:                return (checkout_state(rfp, rev, dst, flags, username, state));
                    330:        }
                    331: }