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