Annotation of src/usr.bin/rcs/co.c, Revision 1.43
1.42 xsa 1: /* $OpenBSD: co.c,v 1.41 2005/11/30 18:50:51 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.38 xsa 39: #define CO_OPTSTRING "f::k:l::M::p::q::r::s:Tu::Vw::x:"
40:
1.43 ! xsa 41: static int checkout_state(RCSFILE *, RCSNUM *, const char *, int,
1.24 niallo 42: const char *, const char *);
1.43 ! xsa 43: static void checkout_err_nobranch(RCSFILE *, const char *, const char *,
! 44: const char *, int);
1.4 joris 45:
1.1 joris 46: int
47: checkout_main(int argc, char **argv)
48: {
1.33 xsa 49: int i, ch, flags, kflag;
1.4 joris 50: RCSNUM *frev, *rev;
1.1 joris 51: RCSFILE *file;
1.14 niallo 52: char fpath[MAXPATHLEN], buf[16];
1.4 joris 53: char *username;
1.38 xsa 54: const char *state;
1.43 ! xsa 55: struct rcs_delta *rdp;
1.37 xsa 56: time_t rcs_mtime = -1;
1.1 joris 57:
1.24 niallo 58: flags = 0;
1.33 xsa 59: kflag = RCS_KWEXP_ERR;
1.1 joris 60: rev = RCS_HEAD_REV;
1.6 joris 61: frev = NULL;
1.38 xsa 62: state = username = NULL;
1.4 joris 63:
1.38 xsa 64: while ((ch = rcs_getopt(argc, argv, CO_OPTSTRING)) != -1) {
1.1 joris 65: switch (ch) {
1.18 joris 66: case 'f':
1.19 joris 67: rcs_set_rev(rcs_optarg, &rev);
1.24 niallo 68: flags |= FORCE;
1.18 joris 69: break;
1.33 xsa 70: case 'k':
71: kflag = rcs_kflag_get(rcs_optarg);
72: if (RCS_KWEXP_INVAL(kflag)) {
73: cvs_log(LP_ERR,
74: "invalid RCS keyword expansion mode");
75: (usage)();
76: exit(1);
77: }
78: break;
1.4 joris 79: case 'l':
1.19 joris 80: rcs_set_rev(rcs_optarg, &rev);
1.24 niallo 81: flags |= CO_LOCK;
1.26 niallo 82: break;
83: case 'M':
84: rcs_set_rev(rcs_optarg, &rev);
85: flags |= CO_REVDATE;
1.4 joris 86: break;
1.20 joris 87: case 'p':
88: rcs_set_rev(rcs_optarg, &rev);
89: pipeout = 1;
90: break;
1.3 joris 91: case 'q':
1.32 xsa 92: rcs_set_rev(rcs_optarg, &rev);
1.3 joris 93: verbose = 0;
94: break;
1.1 joris 95: case 'r':
1.19 joris 96: rcs_set_rev(rcs_optarg, &rev);
1.4 joris 97: break;
1.24 niallo 98: case 's':
99: if ((state = strdup(rcs_optarg)) == NULL) {
100: cvs_log(LP_ERRNO, "out of memory");
101: exit(1);
102: }
103: flags |= CO_STATE;
1.34 xsa 104: break;
105: case 'T':
106: flags |= PRESERVETIME;
1.24 niallo 107: break;
1.4 joris 108: case 'u':
1.19 joris 109: rcs_set_rev(rcs_optarg, &rev);
1.24 niallo 110: flags |= CO_UNLOCK;
1.1 joris 111: break;
1.7 joris 112: case 'V':
113: printf("%s\n", rcs_version);
114: exit(0);
1.38 xsa 115: case 'w':
116: username = rcs_optarg;
1.43 ! xsa 117: flags |= CO_AUTHOR;
1.38 xsa 118: break;
1.30 xsa 119: case 'x':
120: rcs_suffixes = rcs_optarg;
121: break;
1.1 joris 122: default:
123: (usage)();
124: exit(1);
125: }
126: }
127:
1.13 joris 128: argc -= rcs_optind;
129: argv += rcs_optind;
1.1 joris 130:
131: if (argc == 0) {
132: cvs_log(LP_ERR, "no input file");
133: (usage)();
134: exit (1);
135: }
1.11 deraadt 136:
1.38 xsa 137: if ((username == NULL) && ((username = getlogin()) == NULL)) {
138: cvs_log(LP_ERRNO, "failed to get username");
139: exit (1);
140: }
141:
1.1 joris 142: for (i = 0; i < argc; i++) {
143: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
144: continue;
145:
1.21 niallo 146: if (verbose == 1)
1.22 xsa 147: printf("%s --> %s\n", fpath,
148: (pipeout == 1) ? "standard output" : argv[i]);
1.35 xsa 149:
150: if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
151: cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
152: continue;
153: }
1.21 niallo 154:
1.4 joris 155: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1 joris 156: continue;
157:
1.37 xsa 158: if (flags & PRESERVETIME)
159: rcs_mtime = rcs_get_mtime(file->rf_path);
160:
161: if (kflag != RCS_KWEXP_ERR)
162: rcs_kwexp_set(file, kflag);
163:
1.4 joris 164: if (rev == RCS_HEAD_REV)
165: frev = file->rf_head;
166: else
167: frev = rev;
1.17 joris 168:
1.4 joris 169: rcsnum_tostr(frev, buf, sizeof(buf));
1.33 xsa 170:
1.43 ! xsa 171: if ((rdp = rcs_findrev(file, frev)) == NULL)
! 172: return (-1);
! 173:
! 174: if (((flags & CO_STATE) &&
! 175: (strcmp(rdp->rd_state, state) != 0)) ||
! 176: ((flags & CO_AUTHOR) &&
! 177: (strcmp(rdp->rd_author, username) != 0))) {
! 178: checkout_err_nobranch(file, username, NULL, state,
! 179: flags);
! 180: return (-1);
! 181: }
! 182:
1.24 niallo 183: if (flags & CO_STATE) {
184: if (checkout_state(file, frev, argv[i], flags,
185: username, state) < 0) {
186: rcs_close(file);
187: continue;
188: }
1.37 xsa 189: } else {
1.24 niallo 190: if (checkout_rev(file, frev, argv[i], flags,
191: username) < 0) {
192: rcs_close(file);
193: continue;
194: }
1.8 niallo 195: }
1.17 joris 196:
1.1 joris 197: rcs_close(file);
1.37 xsa 198:
199: if (flags & PRESERVETIME)
200: rcs_set_mtime(fpath, rcs_mtime);
1.1 joris 201: }
202:
203: if (rev != RCS_HEAD_REV)
1.5 joris 204: rcsnum_free(frev);
1.1 joris 205:
206: return (0);
207: }
208:
209: void
210: checkout_usage(void)
211: {
1.11 deraadt 212: fprintf(stderr,
1.42 xsa 213: "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
214: " [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
215: " [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1 joris 216: }
1.14 niallo 217:
218: /*
219: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29 xsa 220: * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14 niallo 221: *
222: * Returns 0 on success, -1 on failure.
223: */
224: int
1.24 niallo 225: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
226: const char *username)
1.14 niallo 227: {
1.18 joris 228: char buf[16], yn;
1.14 niallo 229: mode_t mode = 0444;
230: BUF *bp;
1.18 joris 231: struct stat st;
1.20 joris 232: char *content;
1.14 niallo 233:
1.39 xsa 234: /* Check out the latest revision if <frev> is greater than HEAD */
1.15 niallo 235: if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
1.23 xsa 236: frev = file->rf_head;
1.15 niallo 237:
238: rcsnum_tostr(frev, buf, sizeof(buf));
239:
1.18 joris 240: if (verbose == 1)
241: printf("revision %s", buf);
242:
1.14 niallo 243: if ((bp = rcs_getrev(file, frev)) == NULL) {
244: cvs_log(LP_ERR, "cannot find revision `%s'", buf);
245: return (-1);
246: }
247:
1.24 niallo 248: if (flags & CO_LOCK) {
1.14 niallo 249: if ((username != NULL)
250: && (rcs_lock_add(file, username, frev) < 0)) {
1.21 niallo 251: if ((rcs_errno != RCS_ERR_DUPENT) && (verbose == 1))
1.14 niallo 252: cvs_log(LP_ERR, "failed to lock '%s'", buf);
253: }
1.18 joris 254:
1.14 niallo 255: mode = 0644;
1.18 joris 256: if (verbose == 1)
257: printf(" (locked)");
1.24 niallo 258: } else if (flags & CO_UNLOCK) {
1.14 niallo 259: if (rcs_lock_remove(file, frev) < 0) {
260: if (rcs_errno != RCS_ERR_NOENT)
261: cvs_log(LP_ERR,
262: "failed to remove lock '%s'", buf);
263: }
1.18 joris 264:
1.14 niallo 265: mode = 0444;
1.18 joris 266: if (verbose == 1)
267: printf(" (unlocked)");
268: }
269:
270: if (verbose == 1)
271: printf("\n");
272:
1.41 xsa 273: if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
1.18 joris 274: if (st.st_mode & S_IWUSR) {
275: yn = 0;
1.21 niallo 276: if (verbose == 0) {
277: cvs_log(LP_ERR,
1.40 xsa 278: "writable %s exists; checkout aborted",
1.21 niallo 279: dst);
280: return (-1);
281: }
1.41 xsa 282:
283: while ((yn != 'y') && (yn != 'n')) {
284: printf("writable %s exists%s; ", dst,
285: ((uid_t)getuid() == st.st_uid) ? "" :
286: ", and you do not own it");
1.21 niallo 287: printf("remove it? [ny](n): ");
1.18 joris 288: fflush(stdout);
289: yn = getchar();
290: }
291:
292: if (yn == 'n') {
1.21 niallo 293: cvs_log(LP_ERR, "checkout aborted");
1.18 joris 294: return (-1);
295: }
296: }
1.14 niallo 297: }
1.17 joris 298:
1.20 joris 299: if (pipeout == 1) {
300: cvs_buf_putc(bp, '\0');
301: content = cvs_buf_release(bp);
302: printf("%s", content);
303: free(content);
304: } else {
305: if (cvs_buf_write(bp, dst, mode) < 0) {
306: cvs_log(LP_ERR, "failed to write revision to file");
307: cvs_buf_free(bp);
308: return (-1);
309: }
1.14 niallo 310: cvs_buf_free(bp);
1.24 niallo 311: if (flags & CO_REVDATE) {
312: struct timeval tv[2];
1.36 xsa 313: memset(&tv, 0, sizeof(tv));
1.24 niallo 314: tv[0].tv_sec = (long)rcs_rev_getdate(file, frev);
315: tv[1].tv_sec = tv[0].tv_sec;
316: if (utimes(dst, (const struct timeval *)&tv) < 0)
317: cvs_log(LP_ERRNO, "error setting utimes");
318: }
1.20 joris 319:
320: if (verbose == 1)
321: printf("done\n");
1.14 niallo 322: }
1.17 joris 323:
1.14 niallo 324: return (0);
325: }
326:
1.24 niallo 327: /*
328: * checkout_state()
329: *
330: * Search from supplied revision backwards until we find one
331: * with state <state> and check that out.
332: *
1.25 niallo 333: * Returns 0 on success, -1 on checkout_rev failure.
1.24 niallo 334: */
335: static int
1.39 xsa 336: checkout_state(RCSFILE *file, RCSNUM *rev, const char *dst, int flags,
1.24 niallo 337: const char *username, const char *state)
338: {
339: const char *tstate;
340:
1.43 ! xsa 341: if (rev == NULL)
1.24 niallo 342: return (-1);
1.43 ! xsa 343: else {
1.39 xsa 344: if (((tstate = rcs_state_get(file, rev)) != NULL)
1.24 niallo 345: && (strcmp(state, tstate) == 0))
1.39 xsa 346: return (checkout_rev(file, rev, dst, flags, username));
1.24 niallo 347: else
348: rev = rcsnum_dec(rev);
1.39 xsa 349: return (checkout_state(file, rev, dst, flags, username, state));
1.24 niallo 350: }
1.43 ! xsa 351: }
! 352:
! 353: /*
! 354: * checkout_err_nobranch()
! 355: *
! 356: * XXX - should handle the dates too.
! 357: */
! 358: static void
! 359: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
! 360: const char *state, int flags)
! 361: {
! 362: if (!(flags & CO_AUTHOR))
! 363: author = NULL;
! 364: if (!(flags & CO_STATE))
! 365: state = NULL;
! 366:
! 367: cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
! 368: file->rf_path,
! 369: date ? " a date before " : "",
! 370: date ? date : "",
! 371: author ? " and author " + (date ? 0:4 ) : "",
! 372: author ? author : "",
! 373: state ? " and state " + (date || author ? 0:4) : "",
! 374: state ? state : "");
1.24 niallo 375: }