Annotation of src/usr.bin/rcs/co.c, Revision 1.78
1.78 ! pat 1: /* $OpenBSD: co.c,v 1.77 2006/04/14 01:11:07 deraadt 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)) {
71: cvs_log(LP_ERR,
72: "invalid RCS keyword expansion mode");
73: (usage)();
74: exit(1);
75: }
76: break;
1.4 joris 77: case 'l':
1.53 xsa 78: if (flags & CO_UNLOCK) {
79: cvs_log(LP_ERR, "warning: -u overridden by -l");
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.20 joris 91: pipeout = 1;
92: break;
1.3 joris 93: case 'q':
1.73 ray 94: rcs_setrevstr(&rev_str, rcs_optarg);
1.3 joris 95: verbose = 0;
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.47 joris 101: state = xstrdup(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) {
110: cvs_log(LP_ERR, "warning: -l overridden by -u");
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.62 ray 118: /* NOTREACHED */
1.38 xsa 119: case 'w':
1.44 joris 120: /* if no argument, assume current user */
121: if (rcs_optarg == NULL) {
1.48 xsa 122: if ((author = getlogin()) == NULL)
123: fatal("getlogin failed");
1.78 ! pat 124: } else {
1.47 joris 125: author = xstrdup(rcs_optarg);
1.78 ! pat 126: warg = 1;
! 127: }
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++) {
158: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
159: continue;
160:
1.21 niallo 161: if (verbose == 1)
1.22 xsa 162: printf("%s --> %s\n", fpath,
163: (pipeout == 1) ? "standard output" : argv[i]);
1.35 xsa 164:
165: if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
166: cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
167: continue;
168: }
1.21 niallo 169:
1.50 niallo 170: if ((file = rcs_open(fpath, RCS_RDWR|RCS_PARSE_FULLY)) == NULL)
1.1 joris 171: continue;
172:
1.37 xsa 173: if (flags & PRESERVETIME)
174: rcs_mtime = rcs_get_mtime(file->rf_path);
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)
180: fatal("invalid revision: %s", rev_str);
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)
186: fatal("failed to generate rev 0.0");
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:
200: if (verbose == 1)
201: printf("done\n");
1.17 joris 202:
1.1 joris 203: rcs_close(file);
1.73 ray 204: rcsnum_free(rev);
1.37 xsa 205:
206: if (flags & PRESERVETIME)
207: rcs_set_mtime(fpath, rcs_mtime);
1.1 joris 208: }
209:
1.78 ! pat 210: if (author != NULL && warg)
! 211: xfree(author);
! 212:
! 213: if (date != NULL)
! 214: xfree(date);
! 215:
! 216: if (state != NULL)
! 217: xfree(state);
1.1 joris 218:
1.64 niallo 219: return (status);
1.1 joris 220: }
221:
222: void
223: checkout_usage(void)
224: {
1.11 deraadt 225: fprintf(stderr,
1.42 xsa 226: "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
227: " [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
228: " [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1 joris 229: }
1.14 niallo 230:
231: /*
232: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29 xsa 233: * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14 niallo 234: *
1.49 joris 235: * Looks up revision based upon <lockname>, <author>, <state> and <date>
1.44 joris 236: *
1.14 niallo 237: * Returns 0 on success, -1 on failure.
238: */
239: int
1.24 niallo 240: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.49 joris 241: const char *lockname, const char *author, const char *state,
242: const char *date)
1.14 niallo 243: {
1.44 joris 244: BUF *bp;
1.69 joris 245: u_int i;
1.44 joris 246: int lcount;
1.60 niallo 247: char buf[16];
1.14 niallo 248: mode_t mode = 0444;
1.18 joris 249: struct stat st;
1.44 joris 250: struct rcs_delta *rdp;
251: struct rcs_lock *lkp;
1.49 joris 252: char *content, msg[128], *fdate;
253: time_t rcsdate, givendate;
1.69 joris 254: RCSNUM *rev;
1.49 joris 255:
256: rcsdate = givendate = -1;
257: if (date != NULL)
258: givendate = cvs_date_parse(date);
1.14 niallo 259:
1.76 joris 260: if (file->rf_ndelta == 0)
261: printf("no revisions present; generating empty revision 0.0\n");
262:
1.69 joris 263: /* XXX rcsnum_cmp()
264: * Check out the latest revision if <frev> is greater than HEAD
265: */
1.76 joris 266: if (file->rf_ndelta != 0) {
267: for (i = 0; i < file->rf_head->rn_len; i++) {
268: if (file->rf_head->rn_id[i] < frev->rn_id[i]) {
269: frev = file->rf_head;
270: break;
271: }
1.69 joris 272: }
273: }
1.15 niallo 274:
1.44 joris 275: lcount = 0;
276: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
277: if (!strcmp(lkp->rl_name, lockname))
278: lcount++;
279: }
280:
281: /*
282: * If the user didn't specify any revision, we cycle through
283: * revisions to lookup the first one that matches what he specified.
284: *
285: * If we cannot find one, we return an error.
286: */
287: rdp = NULL;
1.77 deraadt 288: if (file->rf_ndelta != 0 && frev == file->rf_head) {
1.44 joris 289: if (lcount > 1) {
290: cvs_log(LP_WARN,
291: "multiple revisions locked by %s; "
292: "please specify one", lockname);
293: return (-1);
294: }
295:
296: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
1.49 joris 297: if (date != NULL) {
298: fdate = asctime(&rdp->rd_date);
299: rcsdate = cvs_date_parse(fdate);
300: if (givendate <= rcsdate)
301: continue;
302: }
303:
1.77 deraadt 304: if (author != NULL &&
305: strcmp(rdp->rd_author, author))
1.44 joris 306: continue;
1.49 joris 307:
1.77 deraadt 308: if (state != NULL &&
309: strcmp(rdp->rd_state, state))
1.44 joris 310: continue;
311:
312: frev = rdp->rd_num;
313: break;
314: }
1.76 joris 315: } else if (file->rf_ndelta != 0) {
1.44 joris 316: rdp = rcs_findrev(file, frev);
317: }
318:
1.77 deraadt 319: if (file->rf_ndelta != 0 && rdp == NULL) {
1.49 joris 320: checkout_err_nobranch(file, author, date, state, flags);
1.44 joris 321: return (-1);
322: }
323:
1.76 joris 324: if (file->rf_ndelta == 0)
325: rev = frev;
326: else
327: rev = rdp->rd_num;
328:
1.69 joris 329: rcsnum_tostr(rev, buf, sizeof(buf));
1.15 niallo 330:
1.77 deraadt 331: if (file->rf_ndelta != 0 && rdp->rd_locker != NULL) {
1.44 joris 332: if (strcmp(lockname, rdp->rd_locker)) {
333: strlcpy(msg, "Revision %s is already locked by %s; ",
334: sizeof(msg));
1.76 joris 335:
336: if (flags & CO_UNLOCK) {
337: strlcat(msg, "use co -r or rcs -u",
338: sizeof(msg));
339: }
340:
1.44 joris 341: cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
342: return (-1);
343: }
344: }
345:
1.77 deraadt 346: if (verbose == 1 && !(flags & NEWFILE) &&
347: !(flags & CO_REVERT) && file->rf_ndelta != 0)
1.18 joris 348: printf("revision %s", buf);
1.74 deraadt 349:
1.77 deraadt 350: if (verbose == 1 && (flags & CO_REVERT))
1.71 niallo 351: printf("done");
1.18 joris 352:
1.76 joris 353: if (file->rf_ndelta != 0) {
354: if ((bp = rcs_getrev(file, rev)) == NULL) {
355: cvs_log(LP_ERR, "cannot find revision `%s'", buf);
356: return (-1);
357: }
358: } else {
359: bp = cvs_buf_alloc(1, 0);
1.14 niallo 360: }
1.69 joris 361:
1.55 niallo 362: /*
363: * Do keyword expansion if required.
364: */
1.76 joris 365: if (file->rf_ndelta != 0)
366: bp = rcs_kwexp_buf(bp, file, rev);
1.14 niallo 367:
1.67 niallo 368: /*
369: * File inherits permissions from its ,v file
370: */
371: if (stat(file->rf_path, &st) == -1)
372: fatal("could not stat rcsfile");
373:
374: mode = st.st_mode;
375:
1.24 niallo 376: if (flags & CO_LOCK) {
1.76 joris 377: if (file->rf_ndelta != 0) {
1.77 deraadt 378: if (lockname != NULL &&
379: rcs_lock_add(file, lockname, rev) < 0) {
1.76 joris 380: if (rcs_errno != RCS_ERR_DUPENT)
381: return (-1);
382: }
1.14 niallo 383: }
1.18 joris 384:
1.67 niallo 385: /* Strip all write bits from mode */
386: mode = st.st_mode &
387: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
388: mode |= S_IWUSR;
1.76 joris 389:
390: if (file->rf_ndelta != 0) {
1.77 deraadt 391: if (verbose == 1 && !(flags & NEWFILE) &&
392: !(flags & CO_REVERT))
1.76 joris 393: printf(" (locked)");
394: }
1.24 niallo 395: } else if (flags & CO_UNLOCK) {
1.76 joris 396: if (file->rf_ndelta != 0) {
397: if (rcs_lock_remove(file, lockname, rev) < 0) {
398: if (rcs_errno != RCS_ERR_NOENT)
399: return (-1);
400: }
1.46 joris 401: }
1.18 joris 402:
1.67 niallo 403: /* Strip all write bits from mode */
404: mode = st.st_mode &
405: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
1.76 joris 406:
407: if (file->rf_ndelta != 0) {
1.77 deraadt 408: if (verbose == 1 && !(flags & NEWFILE) &&
409: !(flags & CO_REVERT))
1.76 joris 410: printf(" (unlocked)");
411: }
1.18 joris 412: }
1.56 xsa 413:
1.77 deraadt 414: if (file->rf_ndelta == 0 &&
1.76 joris 415: ((flags & CO_LOCK) || (flags & CO_UNLOCK))) {
416: cvs_log(LP_WARN, "no revisions, so nothing can be %s",
417: (flags & CO_LOCK) ? "locked" : "unlocked");
418: } else if (file->rf_ndelta != 0) {
1.77 deraadt 419: if (verbose == 1 && !(flags & NEWFILE))
1.76 joris 420: printf("\n");
421: }
1.18 joris 422:
1.44 joris 423: if (flags & CO_LOCK) {
1.71 niallo 424: if (rcs_errno != RCS_ERR_DUPENT)
425: lcount++;
1.77 deraadt 426: if (verbose == 1 && lcount > 1 && !(flags & CO_REVERT))
1.63 xsa 427: cvs_log(LP_WARN, "%s: warning: You now have %d locks.",
428: file->rf_path, lcount);
1.44 joris 429: }
430:
1.77 deraadt 431: if (pipeout == 0 && stat(dst, &st) == 0 && !(flags & FORCE)) {
1.68 ray 432: /*
433: * XXX - Not sure what is "right". If we go according
434: * to GNU's behavior, an existing file with no writable
435: * bits is overwritten without prompting the user.
436: *
437: * This is dangerous, so we always prompt.
438: * Unfortunately this interferes with an unlocked
439: * checkout followed by a locked checkout, which should
440: * not prompt. One (unimplemented) solution is to check
441: * if the existing file is the same as the checked out
442: * revision, and prompt if there are differences.
443: */
444: if (st.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))
1.61 ray 445: printf("writable ");
446: printf("%s exists%s; ", dst,
447: (getuid() == st.st_uid) ? "" :
448: ", and you do not own it");
449: printf("remove it? [ny](n): ");
450: /* default is n */
451: if (cvs_yesno() == -1) {
1.77 deraadt 452: if (verbose == 1 && isatty(STDIN_FILENO))
1.68 ray 453: cvs_log(LP_ERR,
454: "writable %s exists; checkout aborted",
455: dst);
456: else
457: cvs_log(LP_ERR, "checkout aborted");
1.61 ray 458: return (-1);
1.18 joris 459: }
1.14 niallo 460: }
1.17 joris 461:
1.20 joris 462: if (pipeout == 1) {
463: cvs_buf_putc(bp, '\0');
464: content = cvs_buf_release(bp);
465: printf("%s", content);
1.47 joris 466: xfree(content);
1.20 joris 467: } else {
468: if (cvs_buf_write(bp, dst, mode) < 0) {
469: cvs_log(LP_ERR, "failed to write revision to file");
470: cvs_buf_free(bp);
471: return (-1);
472: }
1.14 niallo 473: cvs_buf_free(bp);
1.24 niallo 474: if (flags & CO_REVDATE) {
475: struct timeval tv[2];
1.36 xsa 476: memset(&tv, 0, sizeof(tv));
1.69 joris 477: tv[0].tv_sec = (long)rcs_rev_getdate(file, rev);
1.24 niallo 478: tv[1].tv_sec = tv[0].tv_sec;
479: if (utimes(dst, (const struct timeval *)&tv) < 0)
480: cvs_log(LP_ERRNO, "error setting utimes");
481: }
1.14 niallo 482: }
1.17 joris 483:
1.14 niallo 484: return (0);
1.43 xsa 485: }
486:
487: /*
488: * checkout_err_nobranch()
489: *
490: * XXX - should handle the dates too.
491: */
492: static void
493: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
494: const char *state, int flags)
495: {
496: if (!(flags & CO_AUTHOR))
497: author = NULL;
498: if (!(flags & CO_STATE))
499: state = NULL;
500:
501: cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
502: file->rf_path,
503: date ? " a date before " : "",
504: date ? date : "",
505: author ? " and author " + (date ? 0:4 ) : "",
506: author ? author : "",
507: state ? " and state " + (date || author ? 0:4) : "",
508: state ? state : "");
1.24 niallo 509: }