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

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