Annotation of src/usr.bin/cvs/checkout.c, Revision 1.81
1.81 ! thib 1: /* $OpenBSD: checkout.c,v 1.80 2007/01/17 20:38:50 xsa 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++) {
1.67 xsa 161: if (cvs_path_cat(current_cvsroot->cr_dir, argv[i], repo,
162: sizeof(repo)) >= sizeof(repo))
163: fatal("checkout_check_repository: truncation");
1.13 jfb 164:
1.53 joris 165: if (stat(repo, &st) == -1) {
1.81 ! thib 166: cvs_log(LP_ERR, "cannot find module `%s' - ignored",
1.53 joris 167: argv[i]);
168: continue;
169: }
1.81 ! thib 170: cvs_mkpath(argv[i]);
1.53 joris 171: checkout_repository(repo, argv[i]);
1.33 xsa 172: }
1.14 joris 173: }
1.9 jfb 174:
1.53 joris 175: static void
176: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 177: {
1.53 joris 178: struct cvs_flisthead fl, dl;
179: struct cvs_recursion cr;
180:
181: TAILQ_INIT(&fl);
182: TAILQ_INIT(&dl);
1.41 joris 183:
1.57 joris 184: build_dirs = 1;
1.53 joris 185: cr.enterdir = cvs_update_enterdir;
1.54 joris 186: cr.leavedir = cvs_update_leavedir;
1.64 joris 187: cr.fileproc = cvs_update_local;
1.78 xsa 188: cr.flags = flags;
1.41 joris 189:
1.53 joris 190: cvs_repository_lock(repobase);
1.56 joris 191: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 192:
1.53 joris 193: cvs_file_walklist(&fl, &cr);
194: cvs_file_freelist(&fl);
1.23 joris 195:
1.53 joris 196: cvs_repository_unlock(repobase);
1.23 joris 197:
1.53 joris 198: cvs_file_walklist(&dl, &cr);
199: cvs_file_freelist(&dl);
200: }
1.23 joris 201:
1.58 joris 202: void
1.78 xsa 203: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, int co_flags)
1.53 joris 204: {
205: int l, oflags, exists;
206: time_t rcstime;
207: CVSENTRIES *ent;
208: struct timeval tv[2];
1.72 joris 209: char *template, *p, *entry, rev[16], timebuf[64];
210: char tbuf[32], stickytag[32];
1.22 jfb 211:
1.53 joris 212: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 213:
1.66 joris 214: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 215: cf->file_path, rev, co_flags,
1.66 joris 216: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 217:
1.78 xsa 218: if (co_flags & CO_DUMP) {
1.66 joris 219: if (cvs_server_active) {
220: cvs_printf("dump file %s to client\n", cf->file_path);
221: } else {
1.71 joris 222: rcs_rev_write_fd(cf->file_rcs, rnum,
223: STDOUT_FILENO, 1);
1.66 joris 224: }
225:
1.65 reyk 226: return;
227: }
1.42 joris 228:
1.66 joris 229: if (cvs_server_active == 0) {
230: oflags = O_WRONLY | O_TRUNC;
231: if (cf->fd != -1) {
232: exists = 1;
233: (void)close(cf->fd);
234: } else {
235: exists = 0;
236: oflags |= O_CREAT;
237: }
238:
239: cf->fd = open(cf->file_path, oflags);
240: if (cf->fd == -1)
241: fatal("cvs_checkout_file: open: %s", strerror(errno));
242:
1.71 joris 243: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
1.66 joris 244:
245: if (fchmod(cf->fd, 0644) == -1)
246: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
247:
248: if (exists == 0) {
249: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
250: rcstime = cvs_hack_time(rcstime, 0);
251: } else {
252: time(&rcstime);
253: }
254:
255: tv[0].tv_sec = rcstime;
256: tv[0].tv_usec = 0;
257: tv[1] = tv[0];
258: if (futimes(cf->fd, tv) == -1)
259: fatal("cvs_checkout_file: futimes: %s",
260: strerror(errno));
1.53 joris 261: } else {
262: time(&rcstime);
1.41 joris 263: }
264:
1.62 joris 265: rcstime = cvs_hack_time(rcstime, 1);
1.41 joris 266:
1.58 joris 267: ctime_r(&rcstime, tbuf);
268: if (tbuf[strlen(tbuf) - 1] == '\n')
269: tbuf[strlen(tbuf) - 1] = '\0';
270:
1.78 xsa 271: if (co_flags & CO_MERGE) {
1.58 joris 272: l = snprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
273: tbuf);
274: if (l == -1 || l >= (int)sizeof(timebuf))
275: fatal("cvs_checkout_file: overflow");
276: } else {
277: strlcpy(timebuf, tbuf, sizeof(timebuf));
278: }
1.41 joris 279:
1.78 xsa 280: if (co_flags & CO_SETSTICKY) {
1.60 joris 281: l = snprintf(stickytag, sizeof(stickytag), "T%s", rev);
282: if (l == -1 || l >= (int)sizeof(stickytag))
283: fatal("cvs_checkout_file: overflow");
284: } else {
285: stickytag[0] = '\0';
286: }
287:
1.53 joris 288: entry = xmalloc(CVS_ENT_MAXLINELEN);
1.60 joris 289: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//%s", cf->file_name,
290: rev, timebuf, stickytag);
1.41 joris 291:
1.66 joris 292: if (cvs_server_active == 0) {
293: ent = cvs_ent_open(cf->file_wd);
294: cvs_ent_add(ent, entry);
295: cvs_ent_close(ent, ENT_SYNC);
296: } else {
297: if ((p = strrchr(cf->file_rpath, ',')) != NULL)
298: *p = '\0';
299:
1.78 xsa 300: if (co_flags & CO_COMMIT)
1.69 joris 301: cvs_server_update_entry("Checked-in", cf);
302: else
303: cvs_server_update_entry("Updated", cf);
1.68 joris 304:
1.66 joris 305: cvs_remote_output(entry);
306:
1.78 xsa 307: if (!(co_flags & CO_COMMIT)) {
1.72 joris 308: (void)xasprintf(&template,
309: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.66 joris 310:
1.72 joris 311: /* XXX - fd race below */
1.75 joris 312: rcs_rev_write_stmp(cf->file_rcs, rnum, template, 0);
1.72 joris 313: cvs_remote_send_file(template);
314: (void)unlink(template);
1.74 joris 315: xfree(template);
1.68 joris 316: }
1.66 joris 317:
318: if (p != NULL)
319: *p = ',';
320: }
1.41 joris 321:
1.53 joris 322: xfree(entry);
1.1 jfb 323: }