Annotation of src/usr.bin/cvs/checkout.c, Revision 1.69
1.69 ! joris 1: /* $OpenBSD: checkout.c,v 1.68 2007/01/03 20:48:26 joris Exp $ */
1.1 jfb 2: /*
1.53 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.53 joris 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 jfb 8: *
1.53 joris 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.44 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
21: #include "log.h"
1.53 joris 22: #include "diff.h"
1.66 joris 23: #include "remote.h"
1.1 jfb 24:
1.53 joris 25: int cvs_checkout(int, char **);
1.61 xsa 26: int cvs_export(int, char **);
27: static void checkout_check_repository(int, char **);
1.53 joris 28: static void checkout_repository(const char *, const char *);
1.14 joris 29:
1.54 joris 30: extern int prune_dirs;
1.57 joris 31: extern int build_dirs;
1.54 joris 32:
1.22 jfb 33: struct cvs_cmd cvs_cmd_checkout = {
1.63 joris 34: CVS_OP_CHECKOUT, 0, "checkout",
1.22 jfb 35: { "co", "get" },
1.53 joris 36: "Checkout a working copy of a repository",
1.33 xsa 37: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 38: "[-t id] module ...",
39: "AcD:d:fj:k:lNnPRr:st:",
1.59 joris 40: NULL,
41: cvs_checkout
42: };
43:
44: struct cvs_cmd cvs_cmd_export = {
1.63 joris 45: CVS_OP_EXPORT, 0, "export",
1.59 joris 46: { "exp", "ex" },
47: "Export sources from CVS, similar to checkout",
1.61 xsa 48: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
49: "D:d:k:flNnRr:",
1.22 jfb 50: NULL,
1.61 xsa 51: cvs_export
1.33 xsa 52: };
53:
1.53 joris 54: int
55: cvs_checkout(int argc, char **argv)
1.1 jfb 56: {
1.61 xsa 57: int ch;
1.13 jfb 58:
1.53 joris 59: while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1 jfb 60: switch (ch) {
1.54 joris 61: case 'P':
62: prune_dirs = 1;
63: break;
1.1 jfb 64: default:
1.53 joris 65: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1 jfb 66: }
67: }
68:
69: argc -= optind;
70: argv += optind;
71:
1.53 joris 72: if (argc == 0)
73: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22 jfb 74:
1.61 xsa 75: checkout_check_repository(argc, argv);
76:
77: return (0);
78: }
79:
80: int
81: cvs_export(int argc, char **argv)
82: {
83: int ch, flags;
84:
85: prune_dirs = 1;
86: flags = CR_RECURSE_DIRS;
87:
88: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
89: switch (ch) {
90: case 'l':
91: flags &= ~CR_RECURSE_DIRS;
92: break;
93: case 'R':
94: break;
95: default:
96: fatal("%s", cvs_cmd_export.cmd_synopsis);
97: }
98: }
99:
100: argc -= optind;
101: argv += optind;
102:
103: if (argc == 0)
104: fatal("%s", cvs_cmd_export.cmd_synopsis);
105:
106: checkout_check_repository(argc, argv);
107:
108: return (0);
109: }
110:
111: static void
112: checkout_check_repository(int argc, char **argv)
113: {
1.67 xsa 114: int i;
1.61 xsa 115: char repo[MAXPATHLEN];
116: struct stat st;
117:
1.53 joris 118: for (i = 0; i < argc; i++) {
119: cvs_mkpath(argv[i]);
1.1 jfb 120:
1.67 xsa 121: if (cvs_path_cat(current_cvsroot->cr_dir, argv[i], repo,
122: sizeof(repo)) >= sizeof(repo))
123: fatal("checkout_check_repository: truncation");
1.13 jfb 124:
1.53 joris 125: if (stat(repo, &st) == -1) {
126: cvs_log(LP_ERR, "cannot find repository %s - ignored",
127: argv[i]);
128: continue;
129: }
130:
131: checkout_repository(repo, argv[i]);
1.33 xsa 132: }
1.14 joris 133: }
1.9 jfb 134:
1.53 joris 135: static void
136: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 137: {
1.53 joris 138: struct cvs_flisthead fl, dl;
139: struct cvs_recursion cr;
140:
141: TAILQ_INIT(&fl);
142: TAILQ_INIT(&dl);
1.41 joris 143:
1.57 joris 144: build_dirs = 1;
1.53 joris 145: cr.enterdir = cvs_update_enterdir;
1.54 joris 146: cr.leavedir = cvs_update_leavedir;
1.64 joris 147: cr.fileproc = cvs_update_local;
1.56 joris 148: cr.flags = CR_REPO | CR_RECURSE_DIRS;
1.41 joris 149:
1.53 joris 150: cvs_repository_lock(repobase);
1.56 joris 151: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 152:
1.53 joris 153: cvs_file_walklist(&fl, &cr);
154: cvs_file_freelist(&fl);
1.23 joris 155:
1.53 joris 156: cvs_repository_unlock(repobase);
1.23 joris 157:
1.53 joris 158: cvs_file_walklist(&dl, &cr);
159: cvs_file_freelist(&dl);
160: }
1.23 joris 161:
1.58 joris 162: void
163: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, BUF *bp, int flags)
1.53 joris 164: {
1.58 joris 165: BUF *nbp;
1.53 joris 166: int l, oflags, exists;
167: time_t rcstime;
168: CVSENTRIES *ent;
169: struct timeval tv[2];
1.66 joris 170: char *p, *entry, rev[16], timebuf[64], tbuf[32], stickytag[32];
1.22 jfb 171:
1.53 joris 172: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 173:
1.66 joris 174: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
175: cf->file_path, rev, flags,
176: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 177:
1.58 joris 178: nbp = rcs_kwexp_buf(bp, cf->file_rcs, rnum);
1.65 reyk 179:
180: if (flags & CO_DUMP) {
1.66 joris 181: if (cvs_server_active) {
182: cvs_printf("dump file %s to client\n", cf->file_path);
183: } else {
184: if (cvs_buf_write_fd(nbp, STDOUT_FILENO) == -1)
185: fatal("cvs_checkout_file: %s", strerror(errno));
186: }
187:
1.65 reyk 188: cvs_buf_free(nbp);
189: return;
190: }
1.42 joris 191:
1.66 joris 192: if (cvs_server_active == 0) {
193: oflags = O_WRONLY | O_TRUNC;
194: if (cf->fd != -1) {
195: exists = 1;
196: (void)close(cf->fd);
197: } else {
198: exists = 0;
199: oflags |= O_CREAT;
200: }
201:
202: cf->fd = open(cf->file_path, oflags);
203: if (cf->fd == -1)
204: fatal("cvs_checkout_file: open: %s", strerror(errno));
205:
206: if (cvs_buf_write_fd(nbp, cf->fd) == -1)
207: fatal("cvs_checkout_file: %s", strerror(errno));
208:
209: cvs_buf_free(nbp);
210:
211: if (fchmod(cf->fd, 0644) == -1)
212: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
213:
214: if (exists == 0) {
215: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
216: rcstime = cvs_hack_time(rcstime, 0);
217: } else {
218: time(&rcstime);
219: }
220:
221: tv[0].tv_sec = rcstime;
222: tv[0].tv_usec = 0;
223: tv[1] = tv[0];
224: if (futimes(cf->fd, tv) == -1)
225: fatal("cvs_checkout_file: futimes: %s",
226: strerror(errno));
1.53 joris 227: } else {
228: time(&rcstime);
1.41 joris 229: }
230:
1.62 joris 231: rcstime = cvs_hack_time(rcstime, 1);
1.41 joris 232:
1.58 joris 233: ctime_r(&rcstime, tbuf);
234: if (tbuf[strlen(tbuf) - 1] == '\n')
235: tbuf[strlen(tbuf) - 1] = '\0';
236:
237: if (flags & CO_MERGE) {
238: l = snprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
239: tbuf);
240: if (l == -1 || l >= (int)sizeof(timebuf))
241: fatal("cvs_checkout_file: overflow");
242: } else {
243: strlcpy(timebuf, tbuf, sizeof(timebuf));
244: }
1.41 joris 245:
1.60 joris 246: if (flags & CO_SETSTICKY) {
247: l = snprintf(stickytag, sizeof(stickytag), "T%s", rev);
248: if (l == -1 || l >= (int)sizeof(stickytag))
249: fatal("cvs_checkout_file: overflow");
250: } else {
251: stickytag[0] = '\0';
252: }
253:
1.53 joris 254: entry = xmalloc(CVS_ENT_MAXLINELEN);
1.60 joris 255: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//%s", cf->file_name,
256: rev, timebuf, stickytag);
1.41 joris 257:
1.66 joris 258: if (cvs_server_active == 0) {
259: ent = cvs_ent_open(cf->file_wd);
260: cvs_ent_add(ent, entry);
261: cvs_ent_close(ent, ENT_SYNC);
262: } else {
263: if ((p = strrchr(cf->file_rpath, ',')) != NULL)
264: *p = '\0';
265:
1.69 ! joris 266: if (flags & CO_COMMIT)
! 267: cvs_server_update_entry("Checked-in", cf);
! 268: else
! 269: cvs_server_update_entry("Updated", cf);
1.68 joris 270:
1.66 joris 271: cvs_remote_output(entry);
272:
1.68 joris 273: if (!(flags & CO_COMMIT)) {
274: cvs_remote_output("u=rw,g=rw,o=rw");
275:
276: /* XXX */
277: printf("%ld\n", cvs_buf_len(nbp));
1.66 joris 278:
1.68 joris 279: if (cvs_buf_write_fd(nbp, STDOUT_FILENO) == -1)
280: fatal("cvs_checkout_file: failed to send file");
281: cvs_buf_free(nbp);
282: }
1.66 joris 283:
284: if (p != NULL)
285: *p = ',';
286: }
1.41 joris 287:
1.53 joris 288: xfree(entry);
1.1 jfb 289: }