Annotation of src/usr.bin/rcs/co.c, Revision 1.21
1.21 ! niallo 1: /* $OpenBSD: co.c,v 1.20 2005/10/19 00:30:22 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.4 joris 39: #define LOCK_LOCK 1
40: #define LOCK_UNLOCK 2
41:
1.1 joris 42: int
43: checkout_main(int argc, char **argv)
44: {
45: int i, ch;
1.18 joris 46: int fflag, lock;
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.1 joris 51:
1.18 joris 52: fflag = lock = 0;
1.1 joris 53: rev = RCS_HEAD_REV;
1.6 joris 54: frev = NULL;
1.4 joris 55:
56: if ((username = getlogin()) == NULL) {
1.10 xsa 57: cvs_log(LP_ERRNO, "failed to get username");
1.4 joris 58: exit (1);
59: }
60:
1.20 joris 61: while ((ch = rcs_getopt(argc, argv, "f::l::p::qr::u::V")) != -1) {
1.1 joris 62: switch (ch) {
1.18 joris 63: case 'f':
1.19 joris 64: rcs_set_rev(rcs_optarg, &rev);
1.18 joris 65: fflag = 1;
66: break;
1.4 joris 67: case 'l':
1.19 joris 68: rcs_set_rev(rcs_optarg, &rev);
1.4 joris 69: lock = LOCK_LOCK;
70: break;
1.20 joris 71: case 'p':
72: rcs_set_rev(rcs_optarg, &rev);
73: pipeout = 1;
74: break;
1.3 joris 75: case 'q':
76: verbose = 0;
77: break;
1.1 joris 78: case 'r':
1.19 joris 79: rcs_set_rev(rcs_optarg, &rev);
1.4 joris 80: break;
81: case 'u':
1.19 joris 82: rcs_set_rev(rcs_optarg, &rev);
1.8 niallo 83: lock = LOCK_UNLOCK;
1.1 joris 84: break;
1.7 joris 85: case 'V':
86: printf("%s\n", rcs_version);
87: exit(0);
1.1 joris 88: default:
89: (usage)();
90: exit(1);
91: }
92: }
93:
1.13 joris 94: argc -= rcs_optind;
95: argv += rcs_optind;
1.1 joris 96:
97: if (argc == 0) {
98: cvs_log(LP_ERR, "no input file");
99: (usage)();
100: exit (1);
101: }
1.11 deraadt 102:
1.1 joris 103: for (i = 0; i < argc; i++) {
104: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
105: continue;
106:
1.21 ! niallo 107: if (verbose == 1)
! 108: printf("%s <-- %s\n", fpath, argv[i]);
! 109:
1.4 joris 110: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1 joris 111: continue;
112:
1.4 joris 113: if (rev == RCS_HEAD_REV)
114: frev = file->rf_head;
115: else
116: frev = rev;
1.17 joris 117:
1.4 joris 118: rcsnum_tostr(frev, buf, sizeof(buf));
1.17 joris 119:
1.18 joris 120: if (checkout_rev(file, frev, argv[i], lock, username, fflag) < 0) {
1.8 niallo 121: rcs_close(file);
122: continue;
123: }
1.17 joris 124:
1.1 joris 125: rcs_close(file);
126: }
127:
128: if (rev != RCS_HEAD_REV)
1.5 joris 129: rcsnum_free(frev);
1.1 joris 130:
131: return (0);
132: }
133:
134: void
135: checkout_usage(void)
136: {
1.11 deraadt 137: fprintf(stderr,
1.16 niallo 138: "usage: co [-qV] [-l[rev]] [-r[rev]] [-u[rev]] file ...\n");
1.1 joris 139: }
1.14 niallo 140:
141: /*
142: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
143: * <lkmode> is either LOCK_LOCK or LOCK_UNLOCK or something else
144: * (which has no effect).
145: * In the case of LOCK_LOCK, a lock is set for <username> if it is not NULL.
146: * In the case of LOCK_UNLOCK, all locks are removed for that revision.
147: *
148: * Returns 0 on success, -1 on failure.
149: */
150: int
151: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int lkmode,
1.18 joris 152: const char *username, int force)
1.14 niallo 153: {
1.18 joris 154: char buf[16], yn;
1.14 niallo 155: mode_t mode = 0444;
156: BUF *bp;
1.18 joris 157: struct stat st;
1.20 joris 158: char *content;
1.14 niallo 159:
160: /*
1.15 niallo 161: * Check out the latest revision if <frev> is greater than HEAD
1.14 niallo 162: */
1.15 niallo 163: if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
164: frev = file->rf_head;
165:
166: rcsnum_tostr(frev, buf, sizeof(buf));
167:
1.18 joris 168: if (verbose == 1)
169: printf("revision %s", buf);
170:
1.14 niallo 171: if ((bp = rcs_getrev(file, frev)) == NULL) {
172: cvs_log(LP_ERR, "cannot find revision `%s'", buf);
173: return (-1);
174: }
175:
176: if (lkmode == LOCK_LOCK) {
177: if ((username != NULL)
178: && (rcs_lock_add(file, username, frev) < 0)) {
1.21 ! niallo 179: if ((rcs_errno != RCS_ERR_DUPENT) && (verbose == 1))
1.14 niallo 180: cvs_log(LP_ERR, "failed to lock '%s'", buf);
181: }
1.18 joris 182:
1.14 niallo 183: mode = 0644;
1.18 joris 184: if (verbose == 1)
185: printf(" (locked)");
1.14 niallo 186: } else if (lkmode == LOCK_UNLOCK) {
187: if (rcs_lock_remove(file, frev) < 0) {
188: if (rcs_errno != RCS_ERR_NOENT)
189: cvs_log(LP_ERR,
190: "failed to remove lock '%s'", buf);
191: }
1.18 joris 192:
1.14 niallo 193: mode = 0444;
1.18 joris 194: if (verbose == 1)
195: printf(" (unlocked)");
196: }
197:
198: if (verbose == 1)
199: printf("\n");
200:
201: if ((stat(dst, &st) != -1) && force == 0) {
202: if (st.st_mode & S_IWUSR) {
203: yn = 0;
1.21 ! niallo 204: if (verbose == 0) {
! 205: cvs_log(LP_ERR,
! 206: "writeable %s exists; checkout aborted",
! 207: dst);
! 208: return (-1);
! 209: }
1.18 joris 210: while (yn != 'y' && yn != 'n') {
1.21 ! niallo 211: printf("writeable %s exists; ", dst);
! 212: printf("remove it? [ny](n): ");
1.18 joris 213: fflush(stdout);
214: yn = getchar();
215: }
216:
217: if (yn == 'n') {
1.21 ! niallo 218: cvs_log(LP_ERR, "checkout aborted");
1.18 joris 219: return (-1);
220: }
221: }
1.14 niallo 222: }
1.17 joris 223:
1.20 joris 224: if (pipeout == 1) {
225: cvs_buf_putc(bp, '\0');
226: content = cvs_buf_release(bp);
227: printf("%s", content);
228: free(content);
229: } else {
230: if (cvs_buf_write(bp, dst, mode) < 0) {
231: cvs_log(LP_ERR, "failed to write revision to file");
232: cvs_buf_free(bp);
233: return (-1);
234: }
1.14 niallo 235: cvs_buf_free(bp);
1.20 joris 236:
237: if (verbose == 1)
238: printf("done\n");
1.14 niallo 239: }
1.17 joris 240:
1.14 niallo 241: return (0);
242: }
243: