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