Annotation of src/usr.bin/rcs/co.c, Revision 1.92
1.92 ! ray 1: /* $OpenBSD: co.c,v 1.91 2006/05/17 19:37:40 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:
1.51 xsa 27: #include "includes.h"
1.1 joris 28:
29: #include "rcsprog.h"
30:
1.70 xsa 31: #define CO_OPTSTRING "d:f::I::k:l::M::p::q::r::s:Tu::Vw::x::z::"
1.38 xsa 32:
1.43 xsa 33: static void checkout_err_nobranch(RCSFILE *, const char *, const char *,
34: const char *, int);
1.4 joris 35:
1.1 joris 36: int
37: checkout_main(int argc, char **argv)
38: {
1.86 joris 39: int fd, i, ch, flags, kflag, status, warg;
1.78 pat 40: RCSNUM *rev;
1.1 joris 41: RCSFILE *file;
1.45 joris 42: char fpath[MAXPATHLEN];
1.78 pat 43: char *author, *date, *rev_str, *username, *state;
1.37 xsa 44: time_t rcs_mtime = -1;
1.1 joris 45:
1.78 pat 46: warg = flags = status = 0;
1.33 xsa 47: kflag = RCS_KWEXP_ERR;
1.1 joris 48: rev = RCS_HEAD_REV;
1.73 ray 49: rev_str = NULL;
1.44 joris 50: state = NULL;
51: author = NULL;
1.49 joris 52: date = NULL;
1.4 joris 53:
1.38 xsa 54: while ((ch = rcs_getopt(argc, argv, CO_OPTSTRING)) != -1) {
1.1 joris 55: switch (ch) {
1.49 joris 56: case 'd':
57: date = xstrdup(rcs_optarg);
58: break;
1.18 joris 59: case 'f':
1.73 ray 60: rcs_setrevstr(&rev_str, rcs_optarg);
1.24 niallo 61: flags |= FORCE;
1.18 joris 62: break;
1.65 niallo 63: case 'I':
1.73 ray 64: rcs_setrevstr(&rev_str, rcs_optarg);
1.65 niallo 65: flags |= INTERACTIVE;
66: break;
67:
1.33 xsa 68: case 'k':
69: kflag = rcs_kflag_get(rcs_optarg);
70: if (RCS_KWEXP_INVAL(kflag)) {
1.83 xsa 71: warnx("invalid RCS keyword substitution mode");
1.33 xsa 72: (usage)();
73: exit(1);
74: }
75: break;
1.4 joris 76: case 'l':
1.53 xsa 77: if (flags & CO_UNLOCK) {
1.81 xsa 78: warnx("warning: -u overridden by -l");
1.53 xsa 79: flags &= ~CO_UNLOCK;
80: }
1.73 ray 81: rcs_setrevstr(&rev_str, rcs_optarg);
1.24 niallo 82: flags |= CO_LOCK;
1.26 niallo 83: break;
84: case 'M':
1.73 ray 85: rcs_setrevstr(&rev_str, rcs_optarg);
1.26 niallo 86: flags |= CO_REVDATE;
1.4 joris 87: break;
1.20 joris 88: case 'p':
1.73 ray 89: rcs_setrevstr(&rev_str, rcs_optarg);
1.80 xsa 90: flags |= PIPEOUT;
1.20 joris 91: break;
1.3 joris 92: case 'q':
1.73 ray 93: rcs_setrevstr(&rev_str, rcs_optarg);
1.79 xsa 94: flags |= QUIET;
1.3 joris 95: break;
1.1 joris 96: case 'r':
1.73 ray 97: rcs_setrevstr(&rev_str, rcs_optarg);
1.4 joris 98: break;
1.24 niallo 99: case 's':
1.47 joris 100: state = xstrdup(rcs_optarg);
1.24 niallo 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.73 ray 107: rcs_setrevstr(&rev_str, rcs_optarg);
1.53 xsa 108: if (flags & CO_LOCK) {
1.81 xsa 109: warnx("warning: -l overridden by -u");
1.53 xsa 110: flags &= ~CO_LOCK;
111: }
1.24 niallo 112: flags |= CO_UNLOCK;
1.1 joris 113: break;
1.7 joris 114: case 'V':
115: printf("%s\n", rcs_version);
116: exit(0);
1.38 xsa 117: case 'w':
1.44 joris 118: /* if no argument, assume current user */
119: if (rcs_optarg == NULL) {
1.48 xsa 120: if ((author = getlogin()) == NULL)
1.84 xsa 121: err(1, "getlogin");
1.78 pat 122: } else {
1.47 joris 123: author = xstrdup(rcs_optarg);
1.78 pat 124: warg = 1;
125: }
1.43 xsa 126: flags |= CO_AUTHOR;
1.38 xsa 127: break;
1.30 xsa 128: case 'x':
1.66 ray 129: /* Use blank extension if none given. */
130: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.59 joris 131: break;
132: case 'z':
133: timezone_flag = rcs_optarg;
1.30 xsa 134: break;
1.1 joris 135: default:
136: (usage)();
137: exit(1);
138: }
139: }
140:
1.13 joris 141: argc -= rcs_optind;
142: argv += rcs_optind;
1.1 joris 143:
144: if (argc == 0) {
1.81 xsa 145: warnx("no input file");
1.1 joris 146: (usage)();
147: exit (1);
148: }
1.11 deraadt 149:
1.81 xsa 150: if ((username = getlogin()) == NULL)
1.84 xsa 151: err(1, "getlogin");
1.38 xsa 152:
1.1 joris 153: for (i = 0; i < argc; i++) {
1.92 ! ray 154: fd = rcs_choosefile(argv[i], fpath, sizeof(fpath));
! 155: if (fd < 0) {
! 156: warnx("%s", fpath);
1.1 joris 157: continue;
1.92 ! ray 158: }
1.1 joris 159:
1.79 xsa 160: if (!(flags & QUIET))
1.90 xsa 161: (void)fprintf(stderr, "%s --> %s\n", fpath,
1.80 xsa 162: (flags & PIPEOUT) ? "standard output" : argv[i]);
1.35 xsa 163:
164: if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
1.81 xsa 165: warnx("%s: cannot combine -kv and -l", fpath);
1.86 joris 166: (void)close(fd);
1.35 xsa 167: continue;
168: }
1.21 niallo 169:
1.86 joris 170: if ((file = rcs_open(fpath, fd,
171: RCS_RDWR|RCS_PARSE_FULLY)) == NULL)
1.1 joris 172: continue;
173:
1.37 xsa 174: if (flags & PRESERVETIME)
1.86 joris 175: rcs_mtime = rcs_get_mtime(file);
1.37 xsa 176:
1.72 xsa 177: rcs_kwexp_set(file, kflag);
1.37 xsa 178:
1.75 ray 179: if (rev_str != NULL) {
180: if ((rev = rcs_getrevnum(rev_str, file)) == NULL)
1.84 xsa 181: errx(1, "invalid revision: %s", rev_str);
1.75 ray 182: } else {
1.76 joris 183: /* no revisions in RCS file, generate empty 0.0 */
184: if (file->rf_ndelta == 0) {
185: rev = rcsnum_parse("0.0");
186: if (rev == NULL)
1.84 xsa 187: errx(1, "failed to generate rev 0.0");
1.76 joris 188: } else {
189: rev = rcsnum_alloc();
190: rcsnum_cpy(file->rf_head, rev, 0);
191: }
1.73 ray 192: }
1.17 joris 193:
1.73 ray 194: if ((status = checkout_rev(file, rev, argv[i], flags,
1.64 niallo 195: username, author, state, date)) < 0) {
1.73 ray 196: rcs_close(file);
197: rcsnum_free(rev);
198: continue;
1.8 niallo 199: }
1.58 xsa 200:
1.79 xsa 201: if (!(flags & QUIET))
1.90 xsa 202: (void)fprintf(stderr, "done\n");
1.17 joris 203:
1.73 ray 204: rcsnum_free(rev);
1.37 xsa 205:
1.86 joris 206: rcs_write(file);
1.37 xsa 207: if (flags & PRESERVETIME)
1.86 joris 208: rcs_set_mtime(file, rcs_mtime);
209: rcs_close(file);
1.1 joris 210: }
211:
1.78 pat 212: if (author != NULL && warg)
213: xfree(author);
214:
215: if (date != NULL)
216: xfree(date);
217:
218: if (state != NULL)
219: xfree(state);
1.1 joris 220:
1.64 niallo 221: return (status);
1.1 joris 222: }
223:
224: void
225: checkout_usage(void)
226: {
1.11 deraadt 227: fprintf(stderr,
1.42 xsa 228: "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
229: " [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
230: " [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1 joris 231: }
1.14 niallo 232:
233: /*
234: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.88 ray 235: * Currently recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14 niallo 236: *
1.49 joris 237: * Looks up revision based upon <lockname>, <author>, <state> and <date>
1.44 joris 238: *
1.14 niallo 239: * Returns 0 on success, -1 on failure.
240: */
241: int
1.24 niallo 242: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.49 joris 243: const char *lockname, const char *author, const char *state,
244: const char *date)
1.14 niallo 245: {
1.44 joris 246: BUF *bp;
1.69 joris 247: u_int i;
1.86 joris 248: int fd, lcount;
1.60 niallo 249: char buf[16];
1.14 niallo 250: mode_t mode = 0444;
1.18 joris 251: struct stat st;
1.44 joris 252: struct rcs_delta *rdp;
253: struct rcs_lock *lkp;
1.49 joris 254: char *content, msg[128], *fdate;
255: time_t rcsdate, givendate;
1.69 joris 256: RCSNUM *rev;
1.49 joris 257:
258: rcsdate = givendate = -1;
259: if (date != NULL)
1.85 joris 260: givendate = rcs_date_parse(date);
1.14 niallo 261:
1.90 xsa 262: if (file->rf_ndelta == 0 && !(flags & QUIET))
263: (void)fprintf(stderr,
264: "no revisions present; generating empty revision 0.0\n");
1.76 joris 265:
1.69 joris 266: /* XXX rcsnum_cmp()
267: * Check out the latest revision if <frev> is greater than HEAD
268: */
1.76 joris 269: if (file->rf_ndelta != 0) {
270: for (i = 0; i < file->rf_head->rn_len; i++) {
271: if (file->rf_head->rn_id[i] < frev->rn_id[i]) {
272: frev = file->rf_head;
273: break;
274: }
1.69 joris 275: }
276: }
1.15 niallo 277:
1.44 joris 278: lcount = 0;
279: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
280: if (!strcmp(lkp->rl_name, lockname))
281: lcount++;
282: }
283:
284: /*
285: * If the user didn't specify any revision, we cycle through
286: * revisions to lookup the first one that matches what he specified.
287: *
288: * If we cannot find one, we return an error.
289: */
290: rdp = NULL;
1.77 deraadt 291: if (file->rf_ndelta != 0 && frev == file->rf_head) {
1.44 joris 292: if (lcount > 1) {
1.81 xsa 293: warnx("multiple revisions locked by %s; "
1.44 joris 294: "please specify one", lockname);
295: return (-1);
296: }
297:
298: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
1.49 joris 299: if (date != NULL) {
300: fdate = asctime(&rdp->rd_date);
1.85 joris 301: rcsdate = rcs_date_parse(fdate);
1.49 joris 302: if (givendate <= rcsdate)
303: continue;
304: }
305:
1.77 deraadt 306: if (author != NULL &&
307: strcmp(rdp->rd_author, author))
1.44 joris 308: continue;
1.49 joris 309:
1.77 deraadt 310: if (state != NULL &&
311: strcmp(rdp->rd_state, state))
1.44 joris 312: continue;
313:
314: frev = rdp->rd_num;
315: break;
316: }
1.76 joris 317: } else if (file->rf_ndelta != 0) {
1.44 joris 318: rdp = rcs_findrev(file, frev);
319: }
320:
1.77 deraadt 321: if (file->rf_ndelta != 0 && rdp == NULL) {
1.49 joris 322: checkout_err_nobranch(file, author, date, state, flags);
1.44 joris 323: return (-1);
324: }
325:
1.76 joris 326: if (file->rf_ndelta == 0)
327: rev = frev;
328: else
329: rev = rdp->rd_num;
330:
1.69 joris 331: rcsnum_tostr(rev, buf, sizeof(buf));
1.15 niallo 332:
1.77 deraadt 333: if (file->rf_ndelta != 0 && rdp->rd_locker != NULL) {
1.44 joris 334: if (strcmp(lockname, rdp->rd_locker)) {
1.87 ray 335: if (strlcpy(msg, "Revision %s is already locked by %s; ",
336: sizeof(msg)) >= sizeof(msg))
337: errx(1, "msg too long");
1.76 joris 338:
339: if (flags & CO_UNLOCK) {
1.87 ray 340: if (strlcat(msg, "use co -r or rcs -u",
341: sizeof(msg)) >= sizeof(msg))
342: errx(1, "msg too long");
1.76 joris 343: }
344:
1.81 xsa 345: warnx(msg, buf, rdp->rd_locker);
1.44 joris 346: return (-1);
347: }
348: }
349:
1.79 xsa 350: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 351: !(flags & CO_REVERT) && file->rf_ndelta != 0)
1.91 xsa 352: (void)fprintf(stderr, "revision %s", buf);
1.74 deraadt 353:
1.76 joris 354: if (file->rf_ndelta != 0) {
355: if ((bp = rcs_getrev(file, rev)) == NULL) {
1.81 xsa 356: warnx("cannot find revision `%s'", buf);
1.76 joris 357: return (-1);
358: }
359: } else {
1.85 joris 360: bp = rcs_buf_alloc(1, 0);
1.14 niallo 361: }
1.69 joris 362:
1.55 niallo 363: /*
364: * Do keyword expansion if required.
365: */
1.76 joris 366: if (file->rf_ndelta != 0)
367: bp = rcs_kwexp_buf(bp, file, rev);
1.14 niallo 368:
1.67 niallo 369: /*
370: * File inherits permissions from its ,v file
371: */
1.86 joris 372: if (file->fd != -1) {
373: if (fstat(file->fd, &st) == -1)
374: err(1, "%s", file->rf_path);
375: mode = st.st_mode;
376: }
1.67 niallo 377:
1.24 niallo 378: if (flags & CO_LOCK) {
1.76 joris 379: if (file->rf_ndelta != 0) {
1.77 deraadt 380: if (lockname != NULL &&
381: rcs_lock_add(file, lockname, rev) < 0) {
1.76 joris 382: if (rcs_errno != RCS_ERR_DUPENT)
383: return (-1);
384: }
1.14 niallo 385: }
1.18 joris 386:
1.67 niallo 387: /* Strip all write bits from mode */
1.86 joris 388: if (file->fd != -1) {
389: mode = st.st_mode &
390: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
391: }
392:
1.67 niallo 393: mode |= S_IWUSR;
1.76 joris 394:
395: if (file->rf_ndelta != 0) {
1.79 xsa 396: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 397: !(flags & CO_REVERT))
1.91 xsa 398: (void)fprintf(stderr, " (locked)");
1.76 joris 399: }
1.24 niallo 400: } else if (flags & CO_UNLOCK) {
1.76 joris 401: if (file->rf_ndelta != 0) {
402: if (rcs_lock_remove(file, lockname, rev) < 0) {
403: if (rcs_errno != RCS_ERR_NOENT)
404: return (-1);
405: }
1.46 joris 406: }
1.18 joris 407:
1.67 niallo 408: /* Strip all write bits from mode */
1.86 joris 409: if (file->fd != -1) {
410: mode = st.st_mode &
411: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
412: }
1.76 joris 413:
414: if (file->rf_ndelta != 0) {
1.79 xsa 415: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 416: !(flags & CO_REVERT))
1.91 xsa 417: (void)fprintf(stderr, " (unlocked)");
1.76 joris 418: }
1.18 joris 419: }
1.56 xsa 420:
1.90 xsa 421: if (file->rf_ndelta == 0 && !(flags & QUIET) &&
1.76 joris 422: ((flags & CO_LOCK) || (flags & CO_UNLOCK))) {
1.91 xsa 423: (void)fprintf(stderr, "no revisions, so nothing can be %s\n",
1.76 joris 424: (flags & CO_LOCK) ? "locked" : "unlocked");
425: } else if (file->rf_ndelta != 0) {
1.88 ray 426: /* XXX - Not a good way to detect if a newline is needed. */
427: if (!(flags & QUIET) && !(flags & NEWFILE) &&
428: !(flags & CO_REVERT))
1.90 xsa 429: (void)fprintf(stderr, "\n");
1.76 joris 430: }
1.18 joris 431:
1.44 joris 432: if (flags & CO_LOCK) {
1.71 niallo 433: if (rcs_errno != RCS_ERR_DUPENT)
434: lcount++;
1.79 xsa 435: if (!(flags & QUIET) && lcount > 1 && !(flags & CO_REVERT))
1.81 xsa 436: warnx("%s: warning: You now have %d locks.",
1.63 xsa 437: file->rf_path, lcount);
1.44 joris 438: }
439:
1.86 joris 440: if (!(flags & PIPEOUT) && stat(dst, &st) != -1 && !(flags & FORCE)) {
1.68 ray 441: /*
442: * XXX - Not sure what is "right". If we go according
443: * to GNU's behavior, an existing file with no writable
444: * bits is overwritten without prompting the user.
445: *
446: * This is dangerous, so we always prompt.
447: * Unfortunately this interferes with an unlocked
448: * checkout followed by a locked checkout, which should
449: * not prompt. One (unimplemented) solution is to check
450: * if the existing file is the same as the checked out
451: * revision, and prompt if there are differences.
452: */
453: if (st.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))
1.91 xsa 454: (void)fprintf(stderr, "writable ");
455: (void)fprintf(stderr, "%s exists%s; ", dst,
1.61 ray 456: (getuid() == st.st_uid) ? "" :
457: ", and you do not own it");
1.91 xsa 458: (void)fprintf(stderr, "remove it? [ny](n): ");
1.61 ray 459: /* default is n */
1.85 joris 460: if (rcs_yesno() == -1) {
1.79 xsa 461: if (!(flags & QUIET) && isatty(STDIN_FILENO))
1.81 xsa 462: warnx("writable %s exists; "
463: "checkout aborted", dst);
1.68 ray 464: else
1.81 xsa 465: warnx("checkout aborted");
1.61 ray 466: return (-1);
1.18 joris 467: }
1.14 niallo 468: }
1.17 joris 469:
1.80 xsa 470: if (flags & PIPEOUT) {
1.85 joris 471: rcs_buf_putc(bp, '\0');
472: content = rcs_buf_release(bp);
1.20 joris 473: printf("%s", content);
1.47 joris 474: xfree(content);
1.20 joris 475: } else {
1.86 joris 476: (void)unlink(dst);
477:
478: if ((fd = open(dst, O_WRONLY|O_CREAT|O_TRUNC, mode)) < 0)
479: err(1, "%s", dst);
480:
481: if (rcs_buf_write_fd(bp, fd) < 0) {
1.81 xsa 482: warnx("failed to write revision to file");
1.85 joris 483: rcs_buf_free(bp);
1.86 joris 484: (void)close(fd);
1.20 joris 485: return (-1);
486: }
1.86 joris 487:
488: if (fchmod(fd, mode) == -1)
489: warn("%s", dst);
490:
1.85 joris 491: rcs_buf_free(bp);
1.86 joris 492:
1.24 niallo 493: if (flags & CO_REVDATE) {
494: struct timeval tv[2];
1.36 xsa 495: memset(&tv, 0, sizeof(tv));
1.69 joris 496: tv[0].tv_sec = (long)rcs_rev_getdate(file, rev);
1.24 niallo 497: tv[1].tv_sec = tv[0].tv_sec;
1.86 joris 498: if (futimes(fd, (const struct timeval *)&tv) < 0)
1.81 xsa 499: warn("utimes");
1.24 niallo 500: }
1.86 joris 501:
502: (void)close(fd);
1.14 niallo 503: }
1.17 joris 504:
1.14 niallo 505: return (0);
1.43 xsa 506: }
507:
508: /*
509: * checkout_err_nobranch()
510: *
511: * XXX - should handle the dates too.
512: */
513: static void
514: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
515: const char *state, int flags)
516: {
517: if (!(flags & CO_AUTHOR))
518: author = NULL;
519: if (!(flags & CO_STATE))
520: state = NULL;
521:
1.81 xsa 522: warnx("%s: No revision on branch has%s%s%s%s%s%s.",
1.43 xsa 523: file->rf_path,
524: date ? " a date before " : "",
525: date ? date : "",
526: author ? " and author " + (date ? 0:4 ) : "",
527: author ? author : "",
528: state ? " and state " + (date || author ? 0:4) : "",
529: state ? state : "");
1.24 niallo 530: }