Annotation of src/usr.bin/rcs/co.c, Revision 1.74
1.74 ! deraadt 1: /* $OpenBSD: co.c,v 1.73 2006/04/12 08:23:30 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.73 ray 179: if (rev_str != NULL)
180: rcs_set_rev(rev_str, &rev);
181: else {
182: rev = rcsnum_alloc();
183: rcsnum_cpy(file->rf_head, rev, 0);
184: }
1.17 joris 185:
1.73 ray 186: if ((status = checkout_rev(file, rev, argv[i], flags,
1.64 niallo 187: username, author, state, date)) < 0) {
1.73 ray 188: rcs_close(file);
189: rcsnum_free(rev);
190: continue;
1.8 niallo 191: }
1.58 xsa 192:
193: if (verbose == 1)
194: printf("done\n");
1.17 joris 195:
1.1 joris 196: rcs_close(file);
1.73 ray 197: rcsnum_free(rev);
1.37 xsa 198:
199: if (flags & PRESERVETIME)
200: rcs_set_mtime(fpath, rcs_mtime);
1.1 joris 201: }
202:
1.69 joris 203: if ((rev != RCS_HEAD_REV) && (frev != NULL))
1.5 joris 204: rcsnum_free(frev);
1.1 joris 205:
1.64 niallo 206: return (status);
1.1 joris 207: }
208:
209: void
210: checkout_usage(void)
211: {
1.11 deraadt 212: fprintf(stderr,
1.42 xsa 213: "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
214: " [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
215: " [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1 joris 216: }
1.14 niallo 217:
218: /*
219: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29 xsa 220: * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14 niallo 221: *
1.49 joris 222: * Looks up revision based upon <lockname>, <author>, <state> and <date>
1.44 joris 223: *
1.14 niallo 224: * Returns 0 on success, -1 on failure.
225: */
226: int
1.24 niallo 227: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.49 joris 228: const char *lockname, const char *author, const char *state,
229: const char *date)
1.14 niallo 230: {
1.44 joris 231: BUF *bp;
1.69 joris 232: u_int i;
1.44 joris 233: int lcount;
1.60 niallo 234: char buf[16];
1.14 niallo 235: mode_t mode = 0444;
1.18 joris 236: struct stat st;
1.44 joris 237: struct rcs_delta *rdp;
238: struct rcs_lock *lkp;
1.49 joris 239: char *content, msg[128], *fdate;
240: time_t rcsdate, givendate;
1.69 joris 241: RCSNUM *rev;
1.49 joris 242:
243: rcsdate = givendate = -1;
244: if (date != NULL)
245: givendate = cvs_date_parse(date);
1.14 niallo 246:
1.69 joris 247: /* XXX rcsnum_cmp()
248: * Check out the latest revision if <frev> is greater than HEAD
249: */
250: for (i = 0; i < file->rf_head->rn_len; i++) {
251: if (file->rf_head->rn_id[i] < frev->rn_id[i]) {
252: frev = file->rf_head;
253: break;
254: }
255: }
1.15 niallo 256:
1.44 joris 257: lcount = 0;
258: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
259: if (!strcmp(lkp->rl_name, lockname))
260: lcount++;
261: }
262:
263: /*
264: * If the user didn't specify any revision, we cycle through
265: * revisions to lookup the first one that matches what he specified.
266: *
267: * If we cannot find one, we return an error.
268: */
269: rdp = NULL;
270: if (frev == file->rf_head) {
271: if (lcount > 1) {
272: cvs_log(LP_WARN,
273: "multiple revisions locked by %s; "
274: "please specify one", lockname);
275: return (-1);
276: }
277:
278: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
1.49 joris 279: if (date != NULL) {
280: fdate = asctime(&rdp->rd_date);
281: rcsdate = cvs_date_parse(fdate);
282: if (givendate <= rcsdate)
283: continue;
284: }
285:
1.44 joris 286: if ((author != NULL) &&
287: (strcmp(rdp->rd_author, author)))
288: continue;
1.49 joris 289:
1.44 joris 290: if ((state != NULL) &&
291: (strcmp(rdp->rd_state, state)))
292: continue;
293:
294: frev = rdp->rd_num;
295: break;
296: }
297: } else {
298: rdp = rcs_findrev(file, frev);
299: }
300:
301: if (rdp == NULL) {
1.49 joris 302: checkout_err_nobranch(file, author, date, state, flags);
1.44 joris 303: return (-1);
304: }
305:
1.69 joris 306: rev = rdp->rd_num;
307: rcsnum_tostr(rev, buf, sizeof(buf));
1.15 niallo 308:
1.44 joris 309: if (rdp->rd_locker != NULL) {
310: if (strcmp(lockname, rdp->rd_locker)) {
311: strlcpy(msg, "Revision %s is already locked by %s; ",
312: sizeof(msg));
313: if (flags & CO_UNLOCK)
314: strlcat(msg, "use co -r or rcs -u", sizeof(msg));
315: cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
316: return (-1);
317: }
318: }
319:
1.71 niallo 320: if ((verbose == 1) && !(flags & NEWFILE) && !(flags & CO_REVERT))
1.18 joris 321: printf("revision %s", buf);
1.74 ! deraadt 322:
1.71 niallo 323: if ((verbose == 1) && (flags & CO_REVERT))
324: printf("done");
1.18 joris 325:
1.44 joris 326:
1.69 joris 327: if ((bp = rcs_getrev(file, rev)) == NULL) {
1.14 niallo 328: cvs_log(LP_ERR, "cannot find revision `%s'", buf);
329: return (-1);
330: }
1.69 joris 331:
1.55 niallo 332: /*
333: * Do keyword expansion if required.
334: */
1.69 joris 335: bp = rcs_kwexp_buf(bp, file, rev);
1.14 niallo 336:
1.67 niallo 337: /*
338: * File inherits permissions from its ,v file
339: */
340: if (stat(file->rf_path, &st) == -1)
341: fatal("could not stat rcsfile");
342:
343: mode = st.st_mode;
344:
1.24 niallo 345: if (flags & CO_LOCK) {
1.44 joris 346: if ((lockname != NULL)
1.69 joris 347: && (rcs_lock_add(file, lockname, rev) < 0)) {
1.44 joris 348: if (rcs_errno != RCS_ERR_DUPENT)
349: return (-1);
1.14 niallo 350: }
1.18 joris 351:
1.67 niallo 352: /* Strip all write bits from mode */
353: mode = st.st_mode &
354: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
355: mode |= S_IWUSR;
1.71 niallo 356: if ((verbose == 1) && !(flags & NEWFILE)
357: && !(flags & CO_REVERT))
1.57 xsa 358: printf(" (locked)");
1.24 niallo 359: } else if (flags & CO_UNLOCK) {
1.69 joris 360: if (rcs_lock_remove(file, lockname, rev) < 0) {
1.46 joris 361: if (rcs_errno != RCS_ERR_NOENT)
362: return (-1);
363: }
1.18 joris 364:
1.67 niallo 365: /* Strip all write bits from mode */
366: mode = st.st_mode &
367: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
1.71 niallo 368: if ((verbose == 1) && !(flags & NEWFILE)
369: && !(flags & CO_REVERT))
1.57 xsa 370: printf(" (unlocked)");
1.18 joris 371: }
1.56 xsa 372:
373: if ((verbose == 1) && !(flags & NEWFILE))
374: printf("\n");
1.18 joris 375:
1.44 joris 376: if (flags & CO_LOCK) {
1.71 niallo 377: if (rcs_errno != RCS_ERR_DUPENT)
378: lcount++;
379: if ((verbose == 1) && (lcount > 1) && !(flags & CO_REVERT))
1.63 xsa 380: cvs_log(LP_WARN, "%s: warning: You now have %d locks.",
381: file->rf_path, lcount);
1.44 joris 382: }
383:
1.41 xsa 384: if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
1.68 ray 385: /*
386: * XXX - Not sure what is "right". If we go according
387: * to GNU's behavior, an existing file with no writable
388: * bits is overwritten without prompting the user.
389: *
390: * This is dangerous, so we always prompt.
391: * Unfortunately this interferes with an unlocked
392: * checkout followed by a locked checkout, which should
393: * not prompt. One (unimplemented) solution is to check
394: * if the existing file is the same as the checked out
395: * revision, and prompt if there are differences.
396: */
397: if (st.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))
1.61 ray 398: printf("writable ");
399: printf("%s exists%s; ", dst,
400: (getuid() == st.st_uid) ? "" :
401: ", and you do not own it");
402: printf("remove it? [ny](n): ");
403: /* default is n */
404: if (cvs_yesno() == -1) {
1.68 ray 405: if ((verbose == 1) && isatty(STDIN_FILENO))
406: cvs_log(LP_ERR,
407: "writable %s exists; checkout aborted",
408: dst);
409: else
410: cvs_log(LP_ERR, "checkout aborted");
1.61 ray 411: return (-1);
1.18 joris 412: }
1.14 niallo 413: }
1.17 joris 414:
1.20 joris 415: if (pipeout == 1) {
416: cvs_buf_putc(bp, '\0');
417: content = cvs_buf_release(bp);
418: printf("%s", content);
1.47 joris 419: xfree(content);
1.20 joris 420: } else {
421: if (cvs_buf_write(bp, dst, mode) < 0) {
422: cvs_log(LP_ERR, "failed to write revision to file");
423: cvs_buf_free(bp);
424: return (-1);
425: }
1.14 niallo 426: cvs_buf_free(bp);
1.24 niallo 427: if (flags & CO_REVDATE) {
428: struct timeval tv[2];
1.36 xsa 429: memset(&tv, 0, sizeof(tv));
1.69 joris 430: tv[0].tv_sec = (long)rcs_rev_getdate(file, rev);
1.24 niallo 431: tv[1].tv_sec = tv[0].tv_sec;
432: if (utimes(dst, (const struct timeval *)&tv) < 0)
433: cvs_log(LP_ERRNO, "error setting utimes");
434: }
1.14 niallo 435: }
1.17 joris 436:
1.14 niallo 437: return (0);
1.43 xsa 438: }
439:
440: /*
441: * checkout_err_nobranch()
442: *
443: * XXX - should handle the dates too.
444: */
445: static void
446: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
447: const char *state, int flags)
448: {
449: if (!(flags & CO_AUTHOR))
450: author = NULL;
451: if (!(flags & CO_STATE))
452: state = NULL;
453:
454: cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
455: file->rf_path,
456: date ? " a date before " : "",
457: date ? date : "",
458: author ? " and author " + (date ? 0:4 ) : "",
459: author ? author : "",
460: state ? " and state " + (date || author ? 0:4) : "",
461: state ? state : "");
1.24 niallo 462: }