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