Annotation of src/usr.bin/rcs/co.c, Revision 1.98
1.98 ! niallo 1: /* $OpenBSD: co.c,v 1.97 2006/08/07 20:55:28 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) {
1.98 ! niallo 153: warn("%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.97 ray 242: char *fdate;
1.49 joris 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.97 ray 323: warnx("Revision %s is already locked by %s; %s",
324: buf, rdp->rd_locker,
325: (flags & CO_UNLOCK) ? "use co -r or rcs -u" : "");
1.44 joris 326: return (-1);
327: }
328: }
329:
1.79 xsa 330: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 331: !(flags & CO_REVERT) && file->rf_ndelta != 0)
1.91 xsa 332: (void)fprintf(stderr, "revision %s", buf);
1.74 deraadt 333:
1.76 joris 334: if (file->rf_ndelta != 0) {
335: if ((bp = rcs_getrev(file, rev)) == NULL) {
1.81 xsa 336: warnx("cannot find revision `%s'", buf);
1.76 joris 337: return (-1);
338: }
339: } else {
1.85 joris 340: bp = rcs_buf_alloc(1, 0);
1.14 niallo 341: }
1.69 joris 342:
1.55 niallo 343: /*
344: * Do keyword expansion if required.
345: */
1.76 joris 346: if (file->rf_ndelta != 0)
347: bp = rcs_kwexp_buf(bp, file, rev);
1.67 niallo 348: /*
349: * File inherits permissions from its ,v file
350: */
1.93 ray 351: if (file->rf_fd != -1) {
352: if (fstat(file->rf_fd, &st) == -1)
1.86 joris 353: err(1, "%s", file->rf_path);
354: mode = st.st_mode;
355: }
1.67 niallo 356:
1.24 niallo 357: if (flags & CO_LOCK) {
1.76 joris 358: if (file->rf_ndelta != 0) {
1.77 deraadt 359: if (lockname != NULL &&
360: rcs_lock_add(file, lockname, rev) < 0) {
1.76 joris 361: if (rcs_errno != RCS_ERR_DUPENT)
362: return (-1);
363: }
1.14 niallo 364: }
1.18 joris 365:
1.67 niallo 366: /* Strip all write bits from mode */
1.93 ray 367: if (file->rf_fd != -1) {
1.86 joris 368: mode = st.st_mode &
369: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
370: }
371:
1.67 niallo 372: mode |= S_IWUSR;
1.76 joris 373:
374: if (file->rf_ndelta != 0) {
1.79 xsa 375: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 376: !(flags & CO_REVERT))
1.91 xsa 377: (void)fprintf(stderr, " (locked)");
1.76 joris 378: }
1.24 niallo 379: } else if (flags & CO_UNLOCK) {
1.76 joris 380: if (file->rf_ndelta != 0) {
381: if (rcs_lock_remove(file, lockname, rev) < 0) {
382: if (rcs_errno != RCS_ERR_NOENT)
383: return (-1);
384: }
1.46 joris 385: }
1.18 joris 386:
1.67 niallo 387: /* Strip all write bits from mode */
1.93 ray 388: if (file->rf_fd != -1) {
1.86 joris 389: mode = st.st_mode &
390: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
391: }
1.76 joris 392:
393: if (file->rf_ndelta != 0) {
1.79 xsa 394: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 395: !(flags & CO_REVERT))
1.91 xsa 396: (void)fprintf(stderr, " (unlocked)");
1.76 joris 397: }
1.18 joris 398: }
1.56 xsa 399:
1.90 xsa 400: if (file->rf_ndelta == 0 && !(flags & QUIET) &&
1.76 joris 401: ((flags & CO_LOCK) || (flags & CO_UNLOCK))) {
1.91 xsa 402: (void)fprintf(stderr, "no revisions, so nothing can be %s\n",
1.76 joris 403: (flags & CO_LOCK) ? "locked" : "unlocked");
404: } else if (file->rf_ndelta != 0) {
1.88 ray 405: /* XXX - Not a good way to detect if a newline is needed. */
406: if (!(flags & QUIET) && !(flags & NEWFILE) &&
407: !(flags & CO_REVERT))
1.90 xsa 408: (void)fprintf(stderr, "\n");
1.76 joris 409: }
1.18 joris 410:
1.44 joris 411: if (flags & CO_LOCK) {
1.71 niallo 412: if (rcs_errno != RCS_ERR_DUPENT)
413: lcount++;
1.79 xsa 414: if (!(flags & QUIET) && lcount > 1 && !(flags & CO_REVERT))
1.81 xsa 415: warnx("%s: warning: You now have %d locks.",
1.63 xsa 416: file->rf_path, lcount);
1.44 joris 417: }
418:
1.86 joris 419: if (!(flags & PIPEOUT) && stat(dst, &st) != -1 && !(flags & FORCE)) {
1.68 ray 420: /*
421: * XXX - Not sure what is "right". If we go according
422: * to GNU's behavior, an existing file with no writable
423: * bits is overwritten without prompting the user.
424: *
425: * This is dangerous, so we always prompt.
426: * Unfortunately this interferes with an unlocked
427: * checkout followed by a locked checkout, which should
428: * not prompt. One (unimplemented) solution is to check
429: * if the existing file is the same as the checked out
430: * revision, and prompt if there are differences.
431: */
432: if (st.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))
1.91 xsa 433: (void)fprintf(stderr, "writable ");
434: (void)fprintf(stderr, "%s exists%s; ", dst,
1.61 ray 435: (getuid() == st.st_uid) ? "" :
436: ", and you do not own it");
1.91 xsa 437: (void)fprintf(stderr, "remove it? [ny](n): ");
1.61 ray 438: /* default is n */
1.85 joris 439: if (rcs_yesno() == -1) {
1.79 xsa 440: if (!(flags & QUIET) && isatty(STDIN_FILENO))
1.81 xsa 441: warnx("writable %s exists; "
442: "checkout aborted", dst);
1.68 ray 443: else
1.81 xsa 444: warnx("checkout aborted");
1.61 ray 445: return (-1);
1.18 joris 446: }
1.14 niallo 447: }
1.17 joris 448:
1.96 ray 449: if (flags & PIPEOUT)
450: rcs_buf_write_fd(bp, STDOUT_FILENO);
451: else {
1.86 joris 452: (void)unlink(dst);
453:
454: if ((fd = open(dst, O_WRONLY|O_CREAT|O_TRUNC, mode)) < 0)
455: err(1, "%s", dst);
456:
457: if (rcs_buf_write_fd(bp, fd) < 0) {
1.81 xsa 458: warnx("failed to write revision to file");
1.85 joris 459: rcs_buf_free(bp);
1.86 joris 460: (void)close(fd);
1.20 joris 461: return (-1);
462: }
1.86 joris 463:
464: if (fchmod(fd, mode) == -1)
465: warn("%s", dst);
466:
1.24 niallo 467: if (flags & CO_REVDATE) {
468: struct timeval tv[2];
1.36 xsa 469: memset(&tv, 0, sizeof(tv));
1.69 joris 470: tv[0].tv_sec = (long)rcs_rev_getdate(file, rev);
1.24 niallo 471: tv[1].tv_sec = tv[0].tv_sec;
1.86 joris 472: if (futimes(fd, (const struct timeval *)&tv) < 0)
1.81 xsa 473: warn("utimes");
1.24 niallo 474: }
1.86 joris 475:
476: (void)close(fd);
1.14 niallo 477: }
1.96 ray 478:
479: rcs_buf_free(bp);
1.17 joris 480:
1.14 niallo 481: return (0);
1.43 xsa 482: }
483:
484: /*
485: * checkout_err_nobranch()
486: *
487: * XXX - should handle the dates too.
488: */
489: static void
490: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
491: const char *state, int flags)
492: {
493: if (!(flags & CO_AUTHOR))
494: author = NULL;
495: if (!(flags & CO_STATE))
496: state = NULL;
497:
1.81 xsa 498: warnx("%s: No revision on branch has%s%s%s%s%s%s.",
1.43 xsa 499: file->rf_path,
500: date ? " a date before " : "",
501: date ? date : "",
502: author ? " and author " + (date ? 0:4 ) : "",
503: author ? author : "",
504: state ? " and state " + (date || author ? 0:4) : "",
505: state ? state : "");
1.24 niallo 506: }