Annotation of src/usr.bin/rcs/co.c, Revision 1.51
1.51 ! xsa 1: /* $OpenBSD: co.c,v 1.50 2005/12/27 16:05:21 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 "log.h"
30: #include "rcs.h"
31: #include "rcsprog.h"
32:
1.49 joris 33: #define CO_OPTSTRING "d:f::k:l::M::p::q::r::s:Tu::Vw::x:"
1.38 xsa 34:
1.43 xsa 35: static void checkout_err_nobranch(RCSFILE *, const char *, const char *,
36: const char *, int);
1.4 joris 37:
1.1 joris 38: int
39: checkout_main(int argc, char **argv)
40: {
1.33 xsa 41: int i, ch, flags, kflag;
1.4 joris 42: RCSNUM *frev, *rev;
1.1 joris 43: RCSFILE *file;
1.45 joris 44: char fpath[MAXPATHLEN];
1.49 joris 45: char *author, *username, *date;
1.38 xsa 46: const char *state;
1.37 xsa 47: time_t rcs_mtime = -1;
1.1 joris 48:
1.24 niallo 49: flags = 0;
1.33 xsa 50: kflag = RCS_KWEXP_ERR;
1.1 joris 51: rev = RCS_HEAD_REV;
1.6 joris 52: frev = NULL;
1.44 joris 53: state = NULL;
54: author = NULL;
1.49 joris 55: date = NULL;
1.4 joris 56:
1.38 xsa 57: while ((ch = rcs_getopt(argc, argv, CO_OPTSTRING)) != -1) {
1.1 joris 58: switch (ch) {
1.49 joris 59: case 'd':
60: date = xstrdup(rcs_optarg);
61: break;
1.18 joris 62: case 'f':
1.19 joris 63: rcs_set_rev(rcs_optarg, &rev);
1.24 niallo 64: flags |= FORCE;
1.18 joris 65: break;
1.33 xsa 66: case 'k':
67: kflag = rcs_kflag_get(rcs_optarg);
68: if (RCS_KWEXP_INVAL(kflag)) {
69: cvs_log(LP_ERR,
70: "invalid RCS keyword expansion mode");
71: (usage)();
72: exit(1);
73: }
74: break;
1.4 joris 75: case 'l':
1.19 joris 76: rcs_set_rev(rcs_optarg, &rev);
1.24 niallo 77: flags |= CO_LOCK;
1.26 niallo 78: break;
79: case 'M':
80: rcs_set_rev(rcs_optarg, &rev);
81: flags |= CO_REVDATE;
1.4 joris 82: break;
1.20 joris 83: case 'p':
84: rcs_set_rev(rcs_optarg, &rev);
85: pipeout = 1;
86: break;
1.3 joris 87: case 'q':
1.32 xsa 88: rcs_set_rev(rcs_optarg, &rev);
1.3 joris 89: verbose = 0;
90: break;
1.1 joris 91: case 'r':
1.19 joris 92: rcs_set_rev(rcs_optarg, &rev);
1.4 joris 93: break;
1.24 niallo 94: case 's':
1.47 joris 95: state = xstrdup(rcs_optarg);
1.24 niallo 96: flags |= CO_STATE;
1.34 xsa 97: break;
98: case 'T':
99: flags |= PRESERVETIME;
1.24 niallo 100: break;
1.4 joris 101: case 'u':
1.19 joris 102: rcs_set_rev(rcs_optarg, &rev);
1.24 niallo 103: flags |= CO_UNLOCK;
1.1 joris 104: break;
1.7 joris 105: case 'V':
106: printf("%s\n", rcs_version);
107: exit(0);
1.38 xsa 108: case 'w':
1.44 joris 109: /* if no argument, assume current user */
110: if (rcs_optarg == NULL) {
1.48 xsa 111: if ((author = getlogin()) == NULL)
112: fatal("getlogin failed");
1.47 joris 113: } else
114: author = xstrdup(rcs_optarg);
1.43 xsa 115: flags |= CO_AUTHOR;
1.38 xsa 116: break;
1.30 xsa 117: case 'x':
118: rcs_suffixes = rcs_optarg;
119: break;
1.1 joris 120: default:
121: (usage)();
122: exit(1);
123: }
124: }
125:
1.13 joris 126: argc -= rcs_optind;
127: argv += rcs_optind;
1.1 joris 128:
129: if (argc == 0) {
130: cvs_log(LP_ERR, "no input file");
131: (usage)();
132: exit (1);
133: }
1.11 deraadt 134:
1.44 joris 135: if ((username = getlogin()) == NULL) {
1.38 xsa 136: cvs_log(LP_ERRNO, "failed to get username");
137: exit (1);
138: }
139:
1.1 joris 140: for (i = 0; i < argc; i++) {
141: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
142: continue;
143:
1.21 niallo 144: if (verbose == 1)
1.22 xsa 145: printf("%s --> %s\n", fpath,
146: (pipeout == 1) ? "standard output" : argv[i]);
1.35 xsa 147:
148: if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
149: cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
150: continue;
151: }
1.21 niallo 152:
1.50 niallo 153: if ((file = rcs_open(fpath, RCS_RDWR|RCS_PARSE_FULLY)) == NULL)
1.1 joris 154: continue;
155:
1.37 xsa 156: if (flags & PRESERVETIME)
157: rcs_mtime = rcs_get_mtime(file->rf_path);
158:
159: if (kflag != RCS_KWEXP_ERR)
160: rcs_kwexp_set(file, kflag);
161:
1.4 joris 162: if (rev == RCS_HEAD_REV)
163: frev = file->rf_head;
164: else
165: frev = rev;
1.17 joris 166:
1.44 joris 167: if (checkout_rev(file, frev, argv[i], flags,
1.49 joris 168: username, author, state, date) < 0) {
1.24 niallo 169: rcs_close(file);
170: continue;
1.8 niallo 171: }
1.17 joris 172:
1.1 joris 173: rcs_close(file);
1.37 xsa 174:
175: if (flags & PRESERVETIME)
176: rcs_set_mtime(fpath, rcs_mtime);
1.1 joris 177: }
178:
179: if (rev != RCS_HEAD_REV)
1.5 joris 180: rcsnum_free(frev);
1.1 joris 181:
182: return (0);
183: }
184:
185: void
186: checkout_usage(void)
187: {
1.11 deraadt 188: fprintf(stderr,
1.42 xsa 189: "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
190: " [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
191: " [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1 joris 192: }
1.14 niallo 193:
194: /*
195: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29 xsa 196: * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14 niallo 197: *
1.49 joris 198: * Looks up revision based upon <lockname>, <author>, <state> and <date>
1.44 joris 199: *
1.14 niallo 200: * Returns 0 on success, -1 on failure.
201: */
202: int
1.24 niallo 203: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.49 joris 204: const char *lockname, const char *author, const char *state,
205: const char *date)
1.14 niallo 206: {
1.44 joris 207: BUF *bp;
208: int lcount;
1.18 joris 209: char buf[16], yn;
1.14 niallo 210: mode_t mode = 0444;
1.18 joris 211: struct stat st;
1.44 joris 212: struct rcs_delta *rdp;
213: struct rcs_lock *lkp;
1.49 joris 214: char *content, msg[128], *fdate;
215: time_t rcsdate, givendate;
216:
217: rcsdate = givendate = -1;
218: if (date != NULL)
219: givendate = cvs_date_parse(date);
1.14 niallo 220:
1.39 xsa 221: /* Check out the latest revision if <frev> is greater than HEAD */
1.15 niallo 222: if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
1.23 xsa 223: frev = file->rf_head;
1.15 niallo 224:
1.44 joris 225: lcount = 0;
226: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
227: if (!strcmp(lkp->rl_name, lockname))
228: lcount++;
229: }
230:
231: /*
232: * If the user didn't specify any revision, we cycle through
233: * revisions to lookup the first one that matches what he specified.
234: *
235: * If we cannot find one, we return an error.
236: */
237: rdp = NULL;
238: if (frev == file->rf_head) {
239: if (lcount > 1) {
240: cvs_log(LP_WARN,
241: "multiple revisions locked by %s; "
242: "please specify one", lockname);
243: return (-1);
244: }
245:
246: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
1.49 joris 247: if (date != NULL) {
248: fdate = asctime(&rdp->rd_date);
249: rcsdate = cvs_date_parse(fdate);
250: if (givendate <= rcsdate)
251: continue;
252: }
253:
1.44 joris 254: if ((author != NULL) &&
255: (strcmp(rdp->rd_author, author)))
256: continue;
1.49 joris 257:
1.44 joris 258: if ((state != NULL) &&
259: (strcmp(rdp->rd_state, state)))
260: continue;
261:
262: frev = rdp->rd_num;
263: break;
264: }
265: } else {
266: rdp = rcs_findrev(file, frev);
267: }
268:
269: if (rdp == NULL) {
1.49 joris 270: checkout_err_nobranch(file, author, date, state, flags);
1.44 joris 271: return (-1);
272: }
273:
1.15 niallo 274: rcsnum_tostr(frev, buf, sizeof(buf));
275:
1.44 joris 276: if (rdp->rd_locker != NULL) {
277: if (strcmp(lockname, rdp->rd_locker)) {
278: strlcpy(msg, "Revision %s is already locked by %s; ",
279: sizeof(msg));
280: if (flags & CO_UNLOCK)
281: strlcat(msg, "use co -r or rcs -u", sizeof(msg));
282: cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
283: return (-1);
284: }
285: }
286:
1.18 joris 287: if (verbose == 1)
288: printf("revision %s", buf);
289:
1.44 joris 290:
1.14 niallo 291: if ((bp = rcs_getrev(file, frev)) == NULL) {
292: cvs_log(LP_ERR, "cannot find revision `%s'", buf);
293: return (-1);
294: }
295:
1.24 niallo 296: if (flags & CO_LOCK) {
1.44 joris 297: if ((lockname != NULL)
298: && (rcs_lock_add(file, lockname, frev) < 0)) {
299: if (rcs_errno != RCS_ERR_DUPENT)
300: return (-1);
1.14 niallo 301: }
1.18 joris 302:
1.14 niallo 303: mode = 0644;
1.18 joris 304: if (verbose == 1)
305: printf(" (locked)");
1.24 niallo 306: } else if (flags & CO_UNLOCK) {
1.46 joris 307: if (rcs_lock_remove(file, lockname, frev) < 0) {
308: if (rcs_errno != RCS_ERR_NOENT)
309: return (-1);
310: }
1.18 joris 311:
1.14 niallo 312: mode = 0444;
1.18 joris 313: if (verbose == 1)
314: printf(" (unlocked)");
315: }
316:
317: if (verbose == 1)
318: printf("\n");
319:
1.44 joris 320: if (flags & CO_LOCK) {
321: lcount++;
322: if (lcount > 1)
323: cvs_log(LP_WARN, "You now have %d locks.", lcount);
324: }
325:
1.41 xsa 326: if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
1.18 joris 327: if (st.st_mode & S_IWUSR) {
328: yn = 0;
1.21 niallo 329: if (verbose == 0) {
330: cvs_log(LP_ERR,
1.40 xsa 331: "writable %s exists; checkout aborted",
1.21 niallo 332: dst);
333: return (-1);
334: }
1.41 xsa 335:
336: while ((yn != 'y') && (yn != 'n')) {
337: printf("writable %s exists%s; ", dst,
338: ((uid_t)getuid() == st.st_uid) ? "" :
339: ", and you do not own it");
1.21 niallo 340: printf("remove it? [ny](n): ");
1.18 joris 341: fflush(stdout);
342: yn = getchar();
343: }
344:
345: if (yn == 'n') {
1.21 niallo 346: cvs_log(LP_ERR, "checkout aborted");
1.18 joris 347: return (-1);
348: }
349: }
1.14 niallo 350: }
1.17 joris 351:
1.20 joris 352: if (pipeout == 1) {
353: cvs_buf_putc(bp, '\0');
354: content = cvs_buf_release(bp);
355: printf("%s", content);
1.47 joris 356: xfree(content);
1.20 joris 357: } else {
358: if (cvs_buf_write(bp, dst, mode) < 0) {
359: cvs_log(LP_ERR, "failed to write revision to file");
360: cvs_buf_free(bp);
361: return (-1);
362: }
1.14 niallo 363: cvs_buf_free(bp);
1.24 niallo 364: if (flags & CO_REVDATE) {
365: struct timeval tv[2];
1.36 xsa 366: memset(&tv, 0, sizeof(tv));
1.24 niallo 367: tv[0].tv_sec = (long)rcs_rev_getdate(file, frev);
368: tv[1].tv_sec = tv[0].tv_sec;
369: if (utimes(dst, (const struct timeval *)&tv) < 0)
370: cvs_log(LP_ERRNO, "error setting utimes");
371: }
1.20 joris 372:
373: if (verbose == 1)
374: printf("done\n");
1.14 niallo 375: }
1.17 joris 376:
1.14 niallo 377: return (0);
1.43 xsa 378: }
379:
380: /*
381: * checkout_err_nobranch()
382: *
383: * XXX - should handle the dates too.
384: */
385: static void
386: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
387: const char *state, int flags)
388: {
389: if (!(flags & CO_AUTHOR))
390: author = NULL;
391: if (!(flags & CO_STATE))
392: state = NULL;
393:
394: cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
395: file->rf_path,
396: date ? " a date before " : "",
397: date ? date : "",
398: author ? " and author " + (date ? 0:4 ) : "",
399: author ? author : "",
400: state ? " and state " + (date || author ? 0:4) : "",
401: state ? state : "");
1.24 niallo 402: }