Annotation of src/usr.bin/cvs/checkout.c, Revision 1.75
1.75 ! joris 1: /* $OpenBSD: checkout.c,v 1.74 2007/01/13 15:56:15 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 **);
1.72 joris 27:
1.61 xsa 28: static void checkout_check_repository(int, char **);
1.53 joris 29: static void checkout_repository(const char *, const char *);
1.14 joris 30:
1.54 joris 31: extern int prune_dirs;
1.57 joris 32: extern int build_dirs;
1.54 joris 33:
1.22 jfb 34: struct cvs_cmd cvs_cmd_checkout = {
1.63 joris 35: CVS_OP_CHECKOUT, 0, "checkout",
1.22 jfb 36: { "co", "get" },
1.53 joris 37: "Checkout a working copy of a repository",
1.33 xsa 38: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 39: "[-t id] module ...",
40: "AcD:d:fj:k:lNnPRr:st:",
1.59 joris 41: NULL,
42: cvs_checkout
43: };
44:
45: struct cvs_cmd cvs_cmd_export = {
1.63 joris 46: CVS_OP_EXPORT, 0, "export",
1.59 joris 47: { "exp", "ex" },
48: "Export sources from CVS, similar to checkout",
1.61 xsa 49: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
50: "D:d:k:flNnRr:",
1.22 jfb 51: NULL,
1.61 xsa 52: cvs_export
1.33 xsa 53: };
54:
1.53 joris 55: int
56: cvs_checkout(int argc, char **argv)
1.1 jfb 57: {
1.61 xsa 58: int ch;
1.13 jfb 59:
1.53 joris 60: while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1 jfb 61: switch (ch) {
1.54 joris 62: case 'P':
63: prune_dirs = 1;
64: break;
1.1 jfb 65: default:
1.53 joris 66: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1 jfb 67: }
68: }
69:
70: argc -= optind;
71: argv += optind;
72:
1.53 joris 73: if (argc == 0)
74: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22 jfb 75:
1.61 xsa 76: checkout_check_repository(argc, argv);
77:
78: return (0);
79: }
80:
81: int
82: cvs_export(int argc, char **argv)
83: {
84: int ch, flags;
85:
86: prune_dirs = 1;
87: flags = CR_RECURSE_DIRS;
88:
89: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
90: switch (ch) {
91: case 'l':
92: flags &= ~CR_RECURSE_DIRS;
93: break;
94: case 'R':
95: break;
96: default:
97: fatal("%s", cvs_cmd_export.cmd_synopsis);
98: }
99: }
100:
101: argc -= optind;
102: argv += optind;
103:
104: if (argc == 0)
105: fatal("%s", cvs_cmd_export.cmd_synopsis);
106:
107: checkout_check_repository(argc, argv);
108:
109: return (0);
110: }
111:
112: static void
113: checkout_check_repository(int argc, char **argv)
114: {
1.67 xsa 115: int i;
1.61 xsa 116: char repo[MAXPATHLEN];
117: struct stat st;
118:
1.53 joris 119: for (i = 0; i < argc; i++) {
120: cvs_mkpath(argv[i]);
1.1 jfb 121:
1.67 xsa 122: if (cvs_path_cat(current_cvsroot->cr_dir, argv[i], repo,
123: sizeof(repo)) >= sizeof(repo))
124: fatal("checkout_check_repository: truncation");
1.13 jfb 125:
1.53 joris 126: if (stat(repo, &st) == -1) {
127: cvs_log(LP_ERR, "cannot find repository %s - ignored",
128: argv[i]);
129: continue;
130: }
131:
132: checkout_repository(repo, argv[i]);
1.33 xsa 133: }
1.14 joris 134: }
1.9 jfb 135:
1.53 joris 136: static void
137: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 138: {
1.53 joris 139: struct cvs_flisthead fl, dl;
140: struct cvs_recursion cr;
141:
142: TAILQ_INIT(&fl);
143: TAILQ_INIT(&dl);
1.41 joris 144:
1.57 joris 145: build_dirs = 1;
1.53 joris 146: cr.enterdir = cvs_update_enterdir;
1.54 joris 147: cr.leavedir = cvs_update_leavedir;
1.64 joris 148: cr.fileproc = cvs_update_local;
1.56 joris 149: cr.flags = CR_REPO | CR_RECURSE_DIRS;
1.41 joris 150:
1.53 joris 151: cvs_repository_lock(repobase);
1.56 joris 152: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 153:
1.53 joris 154: cvs_file_walklist(&fl, &cr);
155: cvs_file_freelist(&fl);
1.23 joris 156:
1.53 joris 157: cvs_repository_unlock(repobase);
1.23 joris 158:
1.53 joris 159: cvs_file_walklist(&dl, &cr);
160: cvs_file_freelist(&dl);
161: }
1.23 joris 162:
1.58 joris 163: void
1.73 joris 164: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, int flags)
1.53 joris 165: {
166: int l, oflags, exists;
167: time_t rcstime;
168: CVSENTRIES *ent;
169: struct timeval tv[2];
1.72 joris 170: char *template, *p, *entry, rev[16], timebuf[64];
171: char tbuf[32], stickytag[32];
1.22 jfb 172:
1.53 joris 173: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 174:
1.66 joris 175: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
176: cf->file_path, rev, flags,
177: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 178:
1.65 reyk 179: if (flags & CO_DUMP) {
1.66 joris 180: if (cvs_server_active) {
181: cvs_printf("dump file %s to client\n", cf->file_path);
182: } else {
1.71 joris 183: rcs_rev_write_fd(cf->file_rcs, rnum,
184: STDOUT_FILENO, 1);
1.66 joris 185: }
186:
1.65 reyk 187: return;
188: }
1.42 joris 189:
1.66 joris 190: if (cvs_server_active == 0) {
191: oflags = O_WRONLY | O_TRUNC;
192: if (cf->fd != -1) {
193: exists = 1;
194: (void)close(cf->fd);
195: } else {
196: exists = 0;
197: oflags |= O_CREAT;
198: }
199:
200: cf->fd = open(cf->file_path, oflags);
201: if (cf->fd == -1)
202: fatal("cvs_checkout_file: open: %s", strerror(errno));
203:
1.71 joris 204: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
1.66 joris 205:
206: if (fchmod(cf->fd, 0644) == -1)
207: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
208:
209: if (exists == 0) {
210: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
211: rcstime = cvs_hack_time(rcstime, 0);
212: } else {
213: time(&rcstime);
214: }
215:
216: tv[0].tv_sec = rcstime;
217: tv[0].tv_usec = 0;
218: tv[1] = tv[0];
219: if (futimes(cf->fd, tv) == -1)
220: fatal("cvs_checkout_file: futimes: %s",
221: strerror(errno));
1.53 joris 222: } else {
223: time(&rcstime);
1.41 joris 224: }
225:
1.62 joris 226: rcstime = cvs_hack_time(rcstime, 1);
1.41 joris 227:
1.58 joris 228: ctime_r(&rcstime, tbuf);
229: if (tbuf[strlen(tbuf) - 1] == '\n')
230: tbuf[strlen(tbuf) - 1] = '\0';
231:
232: if (flags & CO_MERGE) {
233: l = snprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
234: tbuf);
235: if (l == -1 || l >= (int)sizeof(timebuf))
236: fatal("cvs_checkout_file: overflow");
237: } else {
238: strlcpy(timebuf, tbuf, sizeof(timebuf));
239: }
1.41 joris 240:
1.60 joris 241: if (flags & CO_SETSTICKY) {
242: l = snprintf(stickytag, sizeof(stickytag), "T%s", rev);
243: if (l == -1 || l >= (int)sizeof(stickytag))
244: fatal("cvs_checkout_file: overflow");
245: } else {
246: stickytag[0] = '\0';
247: }
248:
1.53 joris 249: entry = xmalloc(CVS_ENT_MAXLINELEN);
1.60 joris 250: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//%s", cf->file_name,
251: rev, timebuf, stickytag);
1.41 joris 252:
1.66 joris 253: if (cvs_server_active == 0) {
254: ent = cvs_ent_open(cf->file_wd);
255: cvs_ent_add(ent, entry);
256: cvs_ent_close(ent, ENT_SYNC);
257: } else {
258: if ((p = strrchr(cf->file_rpath, ',')) != NULL)
259: *p = '\0';
260:
1.69 joris 261: if (flags & CO_COMMIT)
262: cvs_server_update_entry("Checked-in", cf);
263: else
264: cvs_server_update_entry("Updated", cf);
1.68 joris 265:
1.66 joris 266: cvs_remote_output(entry);
267:
1.68 joris 268: if (!(flags & CO_COMMIT)) {
1.72 joris 269: (void)xasprintf(&template,
270: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.66 joris 271:
1.72 joris 272: /* XXX - fd race below */
1.75 ! joris 273: rcs_rev_write_stmp(cf->file_rcs, rnum, template, 0);
1.72 joris 274: cvs_remote_send_file(template);
275: (void)unlink(template);
1.74 joris 276: xfree(template);
1.68 joris 277: }
1.66 joris 278:
279: if (p != NULL)
280: *p = ',';
281: }
1.41 joris 282:
1.53 joris 283: xfree(entry);
1.1 jfb 284: }