Annotation of src/usr.bin/rcs/co.c, Revision 1.44
1.44 ! joris 1: /* $OpenBSD: co.c,v 1.43 2005/12/05 19:46:24 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.38 xsa 39: #define CO_OPTSTRING "f::k:l::M::p::q::r::s:Tu::Vw::x:"
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.14 niallo 50: char fpath[MAXPATHLEN], buf[16];
1.44 ! joris 51: char *author, *username;
1.38 xsa 52: const char *state;
1.43 xsa 53: struct rcs_delta *rdp;
1.37 xsa 54: time_t rcs_mtime = -1;
1.1 joris 55:
1.24 niallo 56: flags = 0;
1.33 xsa 57: kflag = RCS_KWEXP_ERR;
1.1 joris 58: rev = RCS_HEAD_REV;
1.6 joris 59: frev = NULL;
1.44 ! joris 60: state = NULL;
! 61: author = 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.18 joris 65: case 'f':
1.19 joris 66: rcs_set_rev(rcs_optarg, &rev);
1.24 niallo 67: flags |= FORCE;
1.18 joris 68: break;
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.24 niallo 80: flags |= CO_LOCK;
1.26 niallo 81: break;
82: case 'M':
83: rcs_set_rev(rcs_optarg, &rev);
84: flags |= CO_REVDATE;
1.4 joris 85: break;
1.20 joris 86: case 'p':
87: rcs_set_rev(rcs_optarg, &rev);
88: pipeout = 1;
89: break;
1.3 joris 90: case 'q':
1.32 xsa 91: rcs_set_rev(rcs_optarg, &rev);
1.3 joris 92: verbose = 0;
93: break;
1.1 joris 94: case 'r':
1.19 joris 95: rcs_set_rev(rcs_optarg, &rev);
1.4 joris 96: break;
1.24 niallo 97: case 's':
98: if ((state = strdup(rcs_optarg)) == NULL) {
99: cvs_log(LP_ERRNO, "out of memory");
100: exit(1);
101: }
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) {
! 117: if ((author = getlogin()) == NULL) {
! 118: cvs_log(LP_ERRNO,
! 119: "could not get login");
! 120: exit(1);
! 121: }
! 122: } else if ((author = strdup(rcs_optarg)) == NULL) {
! 123: cvs_log(LP_ERRNO, "out of memory");
! 124: exit(1);
! 125: }
! 126:
1.43 xsa 127: flags |= CO_AUTHOR;
1.38 xsa 128: break;
1.30 xsa 129: case 'x':
130: rcs_suffixes = rcs_optarg;
131: break;
1.1 joris 132: default:
133: (usage)();
134: exit(1);
135: }
136: }
137:
1.13 joris 138: argc -= rcs_optind;
139: argv += rcs_optind;
1.1 joris 140:
141: if (argc == 0) {
142: cvs_log(LP_ERR, "no input file");
143: (usage)();
144: exit (1);
145: }
1.11 deraadt 146:
1.44 ! joris 147: if ((username = getlogin()) == NULL) {
1.38 xsa 148: cvs_log(LP_ERRNO, "failed to get username");
149: exit (1);
150: }
151:
1.1 joris 152: for (i = 0; i < argc; i++) {
153: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
154: continue;
155:
1.21 niallo 156: if (verbose == 1)
1.22 xsa 157: printf("%s --> %s\n", fpath,
158: (pipeout == 1) ? "standard output" : argv[i]);
1.35 xsa 159:
160: if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
161: cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
162: continue;
163: }
1.21 niallo 164:
1.4 joris 165: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1 joris 166: continue;
167:
1.37 xsa 168: if (flags & PRESERVETIME)
169: rcs_mtime = rcs_get_mtime(file->rf_path);
170:
171: if (kflag != RCS_KWEXP_ERR)
172: rcs_kwexp_set(file, kflag);
173:
1.4 joris 174: if (rev == RCS_HEAD_REV)
175: frev = file->rf_head;
176: else
177: frev = rev;
1.17 joris 178:
1.44 ! joris 179: if (checkout_rev(file, frev, argv[i], flags,
! 180: username, author, state) < 0) {
1.24 niallo 181: rcs_close(file);
182: continue;
1.8 niallo 183: }
1.17 joris 184:
1.1 joris 185: rcs_close(file);
1.37 xsa 186:
187: if (flags & PRESERVETIME)
188: rcs_set_mtime(fpath, rcs_mtime);
1.1 joris 189: }
190:
191: if (rev != RCS_HEAD_REV)
1.5 joris 192: rcsnum_free(frev);
1.1 joris 193:
194: return (0);
195: }
196:
197: void
198: checkout_usage(void)
199: {
1.11 deraadt 200: fprintf(stderr,
1.42 xsa 201: "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
202: " [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
203: " [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1 joris 204: }
1.14 niallo 205:
206: /*
207: * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29 xsa 208: * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14 niallo 209: *
1.44 ! joris 210: * Looks up revision based upon <lockname>, <author>, <state>
! 211: *
1.14 niallo 212: * Returns 0 on success, -1 on failure.
213: */
214: int
1.24 niallo 215: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.44 ! joris 216: const char *lockname, const char *author, const char *state)
1.14 niallo 217: {
1.44 ! joris 218: BUF *bp;
! 219: int lcount;
1.18 joris 220: char buf[16], yn;
1.14 niallo 221: mode_t mode = 0444;
1.18 joris 222: struct stat st;
1.44 ! joris 223: struct rcs_delta *rdp;
! 224: struct rcs_lock *lkp;
! 225: char *content, msg[128];
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) {
! 253: if ((author != NULL) &&
! 254: (strcmp(rdp->rd_author, author)))
! 255: continue;
! 256: if ((state != NULL) &&
! 257: (strcmp(rdp->rd_state, state)))
! 258: continue;
! 259:
! 260: frev = rdp->rd_num;
! 261: break;
! 262: }
! 263: } else {
! 264: rdp = rcs_findrev(file, frev);
! 265: }
! 266:
! 267: if (rdp == NULL) {
! 268: checkout_err_nobranch(file, author, NULL, state, flags);
! 269: return (-1);
! 270: }
! 271:
1.15 niallo 272: rcsnum_tostr(frev, buf, sizeof(buf));
273:
1.44 ! joris 274: if (rdp->rd_locker != NULL) {
! 275: if (strcmp(lockname, rdp->rd_locker)) {
! 276: strlcpy(msg, "Revision %s is already locked by %s; ",
! 277: sizeof(msg));
! 278: if (flags & CO_UNLOCK)
! 279: strlcat(msg, "use co -r or rcs -u", sizeof(msg));
! 280: cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
! 281: return (-1);
! 282: }
! 283: }
! 284:
1.18 joris 285: if (verbose == 1)
286: printf("revision %s", buf);
287:
1.44 ! joris 288:
1.14 niallo 289: if ((bp = rcs_getrev(file, frev)) == NULL) {
290: cvs_log(LP_ERR, "cannot find revision `%s'", buf);
291: return (-1);
292: }
293:
1.24 niallo 294: if (flags & CO_LOCK) {
1.44 ! joris 295: if ((lockname != NULL)
! 296: && (rcs_lock_add(file, lockname, frev) < 0)) {
! 297: if (rcs_errno != RCS_ERR_DUPENT)
! 298: return (-1);
1.14 niallo 299: }
1.18 joris 300:
1.14 niallo 301: mode = 0644;
1.18 joris 302: if (verbose == 1)
303: printf(" (locked)");
1.24 niallo 304: } else if (flags & CO_UNLOCK) {
1.44 ! joris 305: if (rcs_lock_remove(file, lockname, frev) < 0)
! 306: return (-1);
1.18 joris 307:
1.14 niallo 308: mode = 0444;
1.18 joris 309: if (verbose == 1)
310: printf(" (unlocked)");
311: }
312:
313: if (verbose == 1)
314: printf("\n");
315:
1.44 ! joris 316: if (flags & CO_LOCK) {
! 317: lcount++;
! 318: if (lcount > 1)
! 319: cvs_log(LP_WARN, "You now have %d locks.", lcount);
! 320: }
! 321:
1.41 xsa 322: if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
1.18 joris 323: if (st.st_mode & S_IWUSR) {
324: yn = 0;
1.21 niallo 325: if (verbose == 0) {
326: cvs_log(LP_ERR,
1.40 xsa 327: "writable %s exists; checkout aborted",
1.21 niallo 328: dst);
329: return (-1);
330: }
1.41 xsa 331:
332: while ((yn != 'y') && (yn != 'n')) {
333: printf("writable %s exists%s; ", dst,
334: ((uid_t)getuid() == st.st_uid) ? "" :
335: ", and you do not own it");
1.21 niallo 336: printf("remove it? [ny](n): ");
1.18 joris 337: fflush(stdout);
338: yn = getchar();
339: }
340:
341: if (yn == 'n') {
1.21 niallo 342: cvs_log(LP_ERR, "checkout aborted");
1.18 joris 343: return (-1);
344: }
345: }
1.14 niallo 346: }
1.17 joris 347:
1.20 joris 348: if (pipeout == 1) {
349: cvs_buf_putc(bp, '\0');
350: content = cvs_buf_release(bp);
351: printf("%s", content);
352: free(content);
353: } else {
354: if (cvs_buf_write(bp, dst, mode) < 0) {
355: cvs_log(LP_ERR, "failed to write revision to file");
356: cvs_buf_free(bp);
357: return (-1);
358: }
1.14 niallo 359: cvs_buf_free(bp);
1.24 niallo 360: if (flags & CO_REVDATE) {
361: struct timeval tv[2];
1.36 xsa 362: memset(&tv, 0, sizeof(tv));
1.24 niallo 363: tv[0].tv_sec = (long)rcs_rev_getdate(file, frev);
364: tv[1].tv_sec = tv[0].tv_sec;
365: if (utimes(dst, (const struct timeval *)&tv) < 0)
366: cvs_log(LP_ERRNO, "error setting utimes");
367: }
1.20 joris 368:
369: if (verbose == 1)
370: printf("done\n");
1.14 niallo 371: }
1.17 joris 372:
1.14 niallo 373: return (0);
1.43 xsa 374: }
375:
376: /*
377: * checkout_err_nobranch()
378: *
379: * XXX - should handle the dates too.
380: */
381: static void
382: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
383: const char *state, int flags)
384: {
385: if (!(flags & CO_AUTHOR))
386: author = NULL;
387: if (!(flags & CO_STATE))
388: state = NULL;
389:
390: cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
391: file->rf_path,
392: date ? " a date before " : "",
393: date ? date : "",
394: author ? " and author " + (date ? 0:4 ) : "",
395: author ? author : "",
396: state ? " and state " + (date || author ? 0:4) : "",
397: state ? state : "");
1.24 niallo 398: }