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