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