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