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