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

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