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