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