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