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