Annotation of src/usr.bin/rcs/co.c, Revision 1.81
1.81 ! xsa 1: /* $OpenBSD: co.c,v 1.80 2006/04/21 07:06:25 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.78 pat 39: int i, ch, flags, kflag, status, warg;
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.81 ! xsa 71: warnx("invalid RCS keyword expansion 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.62 ray 117: /* NOTREACHED */
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)
122: fatal("getlogin failed");
1.78 pat 123: } else {
1.47 joris 124: author = xstrdup(rcs_optarg);
1.78 pat 125: warg = 1;
126: }
1.43 xsa 127: flags |= CO_AUTHOR;
1.38 xsa 128: break;
1.30 xsa 129: case 'x':
1.66 ray 130: /* Use blank extension if none given. */
131: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.59 joris 132: break;
133: case 'z':
134: timezone_flag = rcs_optarg;
1.30 xsa 135: break;
1.1 joris 136: default:
137: (usage)();
138: exit(1);
139: }
140: }
141:
1.13 joris 142: argc -= rcs_optind;
143: argv += rcs_optind;
1.1 joris 144:
145: if (argc == 0) {
1.81 ! xsa 146: warnx("no input file");
1.1 joris 147: (usage)();
148: exit (1);
149: }
1.11 deraadt 150:
1.81 ! xsa 151: if ((username = getlogin()) == NULL)
! 152: fatal("getlogin failed");
1.38 xsa 153:
1.1 joris 154: for (i = 0; i < argc; i++) {
155: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
156: continue;
157:
1.79 xsa 158: if (!(flags & QUIET))
1.22 xsa 159: printf("%s --> %s\n", fpath,
1.80 xsa 160: (flags & PIPEOUT) ? "standard output" : argv[i]);
1.35 xsa 161:
162: if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
1.81 ! xsa 163: warnx("%s: cannot combine -kv and -l", fpath);
1.35 xsa 164: continue;
165: }
1.21 niallo 166:
1.50 niallo 167: if ((file = rcs_open(fpath, RCS_RDWR|RCS_PARSE_FULLY)) == NULL)
1.1 joris 168: continue;
169:
1.37 xsa 170: if (flags & PRESERVETIME)
171: rcs_mtime = rcs_get_mtime(file->rf_path);
172:
1.72 xsa 173: rcs_kwexp_set(file, kflag);
1.37 xsa 174:
1.75 ray 175: if (rev_str != NULL) {
176: if ((rev = rcs_getrevnum(rev_str, file)) == NULL)
177: fatal("invalid revision: %s", rev_str);
178: } else {
1.76 joris 179: /* no revisions in RCS file, generate empty 0.0 */
180: if (file->rf_ndelta == 0) {
181: rev = rcsnum_parse("0.0");
182: if (rev == NULL)
183: fatal("failed to generate rev 0.0");
184: } else {
185: rev = rcsnum_alloc();
186: rcsnum_cpy(file->rf_head, rev, 0);
187: }
1.73 ray 188: }
1.17 joris 189:
1.73 ray 190: if ((status = checkout_rev(file, rev, argv[i], flags,
1.64 niallo 191: username, author, state, date)) < 0) {
1.73 ray 192: rcs_close(file);
193: rcsnum_free(rev);
194: continue;
1.8 niallo 195: }
1.58 xsa 196:
1.79 xsa 197: if (!(flags & QUIET))
1.58 xsa 198: printf("done\n");
1.17 joris 199:
1.1 joris 200: rcs_close(file);
1.73 ray 201: rcsnum_free(rev);
1.37 xsa 202:
203: if (flags & PRESERVETIME)
204: rcs_set_mtime(fpath, rcs_mtime);
1.1 joris 205: }
206:
1.78 pat 207: if (author != NULL && warg)
208: xfree(author);
209:
210: if (date != NULL)
211: xfree(date);
212:
213: if (state != NULL)
214: xfree(state);
1.1 joris 215:
1.64 niallo 216: return (status);
1.1 joris 217: }
218:
219: void
220: checkout_usage(void)
221: {
1.11 deraadt 222: fprintf(stderr,
1.42 xsa 223: "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
224: " [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
225: " [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1 joris 226: }
1.14 niallo 227:
228: /*
229: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29 xsa 230: * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14 niallo 231: *
1.49 joris 232: * Looks up revision based upon <lockname>, <author>, <state> and <date>
1.44 joris 233: *
1.14 niallo 234: * Returns 0 on success, -1 on failure.
235: */
236: int
1.24 niallo 237: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.49 joris 238: const char *lockname, const char *author, const char *state,
239: const char *date)
1.14 niallo 240: {
1.44 joris 241: BUF *bp;
1.69 joris 242: u_int i;
1.44 joris 243: int lcount;
1.60 niallo 244: char buf[16];
1.14 niallo 245: mode_t mode = 0444;
1.18 joris 246: struct stat st;
1.44 joris 247: struct rcs_delta *rdp;
248: struct rcs_lock *lkp;
1.49 joris 249: char *content, msg[128], *fdate;
250: time_t rcsdate, givendate;
1.69 joris 251: RCSNUM *rev;
1.49 joris 252:
253: rcsdate = givendate = -1;
254: if (date != NULL)
255: givendate = cvs_date_parse(date);
1.14 niallo 256:
1.76 joris 257: if (file->rf_ndelta == 0)
258: printf("no revisions present; generating empty revision 0.0\n");
259:
1.69 joris 260: /* XXX rcsnum_cmp()
261: * Check out the latest revision if <frev> is greater than HEAD
262: */
1.76 joris 263: if (file->rf_ndelta != 0) {
264: for (i = 0; i < file->rf_head->rn_len; i++) {
265: if (file->rf_head->rn_id[i] < frev->rn_id[i]) {
266: frev = file->rf_head;
267: break;
268: }
1.69 joris 269: }
270: }
1.15 niallo 271:
1.44 joris 272: lcount = 0;
273: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
274: if (!strcmp(lkp->rl_name, lockname))
275: lcount++;
276: }
277:
278: /*
279: * If the user didn't specify any revision, we cycle through
280: * revisions to lookup the first one that matches what he specified.
281: *
282: * If we cannot find one, we return an error.
283: */
284: rdp = NULL;
1.77 deraadt 285: if (file->rf_ndelta != 0 && frev == file->rf_head) {
1.44 joris 286: if (lcount > 1) {
1.81 ! xsa 287: warnx("multiple revisions locked by %s; "
1.44 joris 288: "please specify one", lockname);
289: return (-1);
290: }
291:
292: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
1.49 joris 293: if (date != NULL) {
294: fdate = asctime(&rdp->rd_date);
295: rcsdate = cvs_date_parse(fdate);
296: if (givendate <= rcsdate)
297: continue;
298: }
299:
1.77 deraadt 300: if (author != NULL &&
301: strcmp(rdp->rd_author, author))
1.44 joris 302: continue;
1.49 joris 303:
1.77 deraadt 304: if (state != NULL &&
305: strcmp(rdp->rd_state, state))
1.44 joris 306: continue;
307:
308: frev = rdp->rd_num;
309: break;
310: }
1.76 joris 311: } else if (file->rf_ndelta != 0) {
1.44 joris 312: rdp = rcs_findrev(file, frev);
313: }
314:
1.77 deraadt 315: if (file->rf_ndelta != 0 && rdp == NULL) {
1.49 joris 316: checkout_err_nobranch(file, author, date, state, flags);
1.44 joris 317: return (-1);
318: }
319:
1.76 joris 320: if (file->rf_ndelta == 0)
321: rev = frev;
322: else
323: rev = rdp->rd_num;
324:
1.69 joris 325: rcsnum_tostr(rev, buf, sizeof(buf));
1.15 niallo 326:
1.77 deraadt 327: if (file->rf_ndelta != 0 && rdp->rd_locker != NULL) {
1.44 joris 328: if (strcmp(lockname, rdp->rd_locker)) {
329: strlcpy(msg, "Revision %s is already locked by %s; ",
330: sizeof(msg));
1.76 joris 331:
332: if (flags & CO_UNLOCK) {
333: strlcat(msg, "use co -r or rcs -u",
334: sizeof(msg));
335: }
336:
1.81 ! xsa 337: warnx(msg, buf, rdp->rd_locker);
1.44 joris 338: return (-1);
339: }
340: }
341:
1.79 xsa 342: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 343: !(flags & CO_REVERT) && file->rf_ndelta != 0)
1.18 joris 344: printf("revision %s", buf);
1.74 deraadt 345:
1.79 xsa 346: if (!(flags & QUIET) && (flags & CO_REVERT))
1.71 niallo 347: printf("done");
1.18 joris 348:
1.76 joris 349: if (file->rf_ndelta != 0) {
350: if ((bp = rcs_getrev(file, rev)) == NULL) {
1.81 ! xsa 351: warnx("cannot find revision `%s'", buf);
1.76 joris 352: return (-1);
353: }
354: } else {
355: bp = cvs_buf_alloc(1, 0);
1.14 niallo 356: }
1.69 joris 357:
1.55 niallo 358: /*
359: * Do keyword expansion if required.
360: */
1.76 joris 361: if (file->rf_ndelta != 0)
362: bp = rcs_kwexp_buf(bp, file, rev);
1.14 niallo 363:
1.67 niallo 364: /*
365: * File inherits permissions from its ,v file
366: */
367: if (stat(file->rf_path, &st) == -1)
368: fatal("could not stat rcsfile");
369:
370: mode = st.st_mode;
371:
1.24 niallo 372: if (flags & CO_LOCK) {
1.76 joris 373: if (file->rf_ndelta != 0) {
1.77 deraadt 374: if (lockname != NULL &&
375: rcs_lock_add(file, lockname, rev) < 0) {
1.76 joris 376: if (rcs_errno != RCS_ERR_DUPENT)
377: return (-1);
378: }
1.14 niallo 379: }
1.18 joris 380:
1.67 niallo 381: /* Strip all write bits from mode */
382: mode = st.st_mode &
383: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
384: mode |= S_IWUSR;
1.76 joris 385:
386: if (file->rf_ndelta != 0) {
1.79 xsa 387: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 388: !(flags & CO_REVERT))
1.76 joris 389: printf(" (locked)");
390: }
1.24 niallo 391: } else if (flags & CO_UNLOCK) {
1.76 joris 392: if (file->rf_ndelta != 0) {
393: if (rcs_lock_remove(file, lockname, rev) < 0) {
394: if (rcs_errno != RCS_ERR_NOENT)
395: return (-1);
396: }
1.46 joris 397: }
1.18 joris 398:
1.67 niallo 399: /* Strip all write bits from mode */
400: mode = st.st_mode &
401: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
1.76 joris 402:
403: if (file->rf_ndelta != 0) {
1.79 xsa 404: if (!(flags & QUIET) && !(flags & NEWFILE) &&
1.77 deraadt 405: !(flags & CO_REVERT))
1.76 joris 406: printf(" (unlocked)");
407: }
1.18 joris 408: }
1.56 xsa 409:
1.77 deraadt 410: if (file->rf_ndelta == 0 &&
1.76 joris 411: ((flags & CO_LOCK) || (flags & CO_UNLOCK))) {
1.81 ! xsa 412: warnx("no revisions, so nothing can be %s",
1.76 joris 413: (flags & CO_LOCK) ? "locked" : "unlocked");
414: } else if (file->rf_ndelta != 0) {
1.79 xsa 415: if (!(flags & QUIET) && !(flags & NEWFILE))
1.76 joris 416: printf("\n");
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.80 xsa 427: if (!(flags & PIPEOUT) && stat(dst, &st) == 0 && !(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.61 ray 441: printf("writable ");
442: printf("%s exists%s; ", dst,
443: (getuid() == st.st_uid) ? "" :
444: ", and you do not own it");
445: printf("remove it? [ny](n): ");
446: /* default is n */
447: if (cvs_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.20 joris 458: cvs_buf_putc(bp, '\0');
459: content = cvs_buf_release(bp);
460: printf("%s", content);
1.47 joris 461: xfree(content);
1.20 joris 462: } else {
463: if (cvs_buf_write(bp, dst, mode) < 0) {
1.81 ! xsa 464: warnx("failed to write revision to file");
1.20 joris 465: cvs_buf_free(bp);
466: return (-1);
467: }
1.14 niallo 468: cvs_buf_free(bp);
1.24 niallo 469: if (flags & CO_REVDATE) {
470: struct timeval tv[2];
1.36 xsa 471: memset(&tv, 0, sizeof(tv));
1.69 joris 472: tv[0].tv_sec = (long)rcs_rev_getdate(file, rev);
1.24 niallo 473: tv[1].tv_sec = tv[0].tv_sec;
474: if (utimes(dst, (const struct timeval *)&tv) < 0)
1.81 ! xsa 475: warn("utimes");
1.24 niallo 476: }
1.14 niallo 477: }
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.24 niallo 504: }