Annotation of src/usr.bin/cvs/checkout.c, Revision 1.107
1.107 ! tobias 1: /* $OpenBSD: checkout.c,v 1.106 2008/01/10 09:54:04 tobias 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.92 otto 18: #include <sys/param.h>
19: #include <sys/dirent.h>
20: #include <sys/stat.h>
21:
22: #include <errno.h>
23: #include <fcntl.h>
1.100 tobias 24: #include <libgen.h>
1.92 otto 25: #include <string.h>
26: #include <unistd.h>
1.1 jfb 27:
28: #include "cvs.h"
1.53 joris 29: #include "diff.h"
1.66 joris 30: #include "remote.h"
1.72 joris 31:
1.61 xsa 32: static void checkout_check_repository(int, char **);
1.53 joris 33: static void checkout_repository(const char *, const char *);
1.14 joris 34:
1.101 joris 35: extern int print_stdout;
1.54 joris 36: extern int prune_dirs;
1.57 joris 37: extern int build_dirs;
1.54 joris 38:
1.78 xsa 39: static int flags = CR_REPO | CR_RECURSE_DIRS;
40:
1.22 jfb 41: struct cvs_cmd cvs_cmd_checkout = {
1.63 joris 42: CVS_OP_CHECKOUT, 0, "checkout",
1.22 jfb 43: { "co", "get" },
1.53 joris 44: "Checkout a working copy of a repository",
1.33 xsa 45: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 46: "[-t id] module ...",
1.77 xsa 47: "AcD:d:fj:k:lNnPpRr:st:",
1.59 joris 48: NULL,
49: cvs_checkout
50: };
51:
52: struct cvs_cmd cvs_cmd_export = {
1.63 joris 53: CVS_OP_EXPORT, 0, "export",
1.59 joris 54: { "exp", "ex" },
55: "Export sources from CVS, similar to checkout",
1.61 xsa 56: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
57: "D:d:k:flNnRr:",
1.22 jfb 58: NULL,
1.61 xsa 59: cvs_export
1.33 xsa 60: };
61:
1.53 joris 62: int
63: cvs_checkout(int argc, char **argv)
1.1 jfb 64: {
1.78 xsa 65: int ch;
1.13 jfb 66:
1.53 joris 67: while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1 jfb 68: switch (ch) {
1.76 xsa 69: case 'A':
70: reset_stickies = 1;
71: break;
1.77 xsa 72: case 'l':
73: flags &= ~CR_RECURSE_DIRS;
74: break;
1.104 joris 75: case 'N':
76: break;
1.54 joris 77: case 'P':
78: prune_dirs = 1;
1.77 xsa 79: break;
1.101 joris 80: case 'p':
81: print_stdout = 1;
82: cvs_noexec = 1;
83: break;
1.77 xsa 84: case 'R':
1.54 joris 85: break;
1.93 niallo 86: case 'r':
1.97 joris 87: cvs_specified_tag = optarg;
1.93 niallo 88: break;
1.1 jfb 89: default:
1.53 joris 90: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1 jfb 91: }
92: }
93:
94: argc -= optind;
95: argv += optind;
96:
1.53 joris 97: if (argc == 0)
98: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22 jfb 99:
1.61 xsa 100: checkout_check_repository(argc, argv);
101:
102: return (0);
103: }
104:
105: int
106: cvs_export(int argc, char **argv)
107: {
1.78 xsa 108: int ch;
1.61 xsa 109:
110: prune_dirs = 1;
111:
112: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
113: switch (ch) {
114: case 'l':
115: flags &= ~CR_RECURSE_DIRS;
116: break;
117: case 'R':
1.98 xsa 118: break;
119: case 'r':
120: cvs_specified_tag = optarg;
1.61 xsa 121: break;
122: default:
123: fatal("%s", cvs_cmd_export.cmd_synopsis);
124: }
125: }
126:
127: argc -= optind;
128: argv += optind;
129:
130: if (argc == 0)
131: fatal("%s", cvs_cmd_export.cmd_synopsis);
132:
133: checkout_check_repository(argc, argv);
134:
135: return (0);
136: }
137:
138: static void
139: checkout_check_repository(int argc, char **argv)
140: {
1.67 xsa 141: int i;
1.61 xsa 142: char repo[MAXPATHLEN];
143: struct stat st;
1.79 joris 144: struct cvs_recursion cr;
145:
1.102 tobias 146: build_dirs = print_stdout ? 0 : 1;
147:
1.79 joris 148: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
149: cvs_client_connect_to_server();
150:
1.97 joris 151: if (cvs_specified_tag != NULL)
152: cvs_client_send_request("Argument -r%s",
153: cvs_specified_tag);
1.79 joris 154: if (reset_stickies == 1)
155: cvs_client_send_request("Argument -A");
1.80 xsa 156:
157: if (!(flags & CR_RECURSE_DIRS))
158: cvs_client_send_request("Argument -l");
1.79 joris 159:
160: if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
161: cvs_client_send_request("Argument -P");
162:
1.101 joris 163: if (print_stdout == 1)
164: cvs_client_send_request("Argument -p");
165:
1.79 joris 166: cr.enterdir = NULL;
167: cr.leavedir = NULL;
1.103 tobias 168: if (print_stdout)
169: cr.fileproc = NULL;
170: else
171: cr.fileproc = cvs_client_sendfile;
172:
173: flags &= ~CR_REPO;
1.79 joris 174: cr.flags = flags;
175:
176: cvs_file_run(argc, argv, &cr);
177:
178: cvs_client_send_files(argv, argc);
179: cvs_client_senddir(".");
180:
181: cvs_client_send_request("%s",
182: (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
183:
184: cvs_client_get_responses();
185:
186: return;
187: }
1.61 xsa 188:
1.104 joris 189: cvs_directory_tag = cvs_specified_tag;
190:
1.53 joris 191: for (i = 0; i < argc; i++) {
1.90 xsa 192: (void)xsnprintf(repo, sizeof(repo), "%s/%s",
193: current_cvsroot->cr_dir, argv[i]);
1.13 jfb 194:
1.53 joris 195: if (stat(repo, &st) == -1) {
1.100 tobias 196: /* check if a single file was requested */
197: strlcat(repo, RCS_FILE_EXT, MAXPATHLEN);
198:
199: if (stat(repo, &st) == -1) {
200: cvs_log(LP_ERR,
201: "cannot find module `%s' - ignored",
202: argv[i]);
203: continue;
204: }
205:
206: cr.fileproc = cvs_update_local;
207: cr.flags = flags;
1.102 tobias 208:
209: if (build_dirs == 1)
210: cvs_mkpath(dirname(argv[i]), cvs_specified_tag);
1.100 tobias 211: cvs_file_run(1, &(argv[i]), &cr);
212:
1.53 joris 213: continue;
214: }
1.97 joris 215:
1.102 tobias 216: if (build_dirs == 1)
217: cvs_mkpath(argv[i], cvs_specified_tag);
1.53 joris 218: checkout_repository(repo, argv[i]);
1.33 xsa 219: }
1.14 joris 220: }
1.9 jfb 221:
1.53 joris 222: static void
223: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 224: {
1.53 joris 225: struct cvs_flisthead fl, dl;
226: struct cvs_recursion cr;
227:
228: TAILQ_INIT(&fl);
229: TAILQ_INIT(&dl);
1.94 joris 230:
1.99 xsa 231: cvs_history_add((cvs_cmdop == CVS_OP_CHECKOUT) ?
232: CVS_HISTORY_CHECKOUT : CVS_HISTORY_EXPORT, NULL, wdbase);
1.41 joris 233:
1.53 joris 234: cr.enterdir = cvs_update_enterdir;
1.54 joris 235: cr.leavedir = cvs_update_leavedir;
1.64 joris 236: cr.fileproc = cvs_update_local;
1.78 xsa 237: cr.flags = flags;
1.41 joris 238:
1.53 joris 239: cvs_repository_lock(repobase);
1.56 joris 240: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 241:
1.53 joris 242: cvs_file_walklist(&fl, &cr);
243: cvs_file_freelist(&fl);
1.23 joris 244:
1.53 joris 245: cvs_repository_unlock(repobase);
1.23 joris 246:
1.53 joris 247: cvs_file_walklist(&dl, &cr);
248: cvs_file_freelist(&dl);
249: }
1.23 joris 250:
1.58 joris 251: void
1.105 joris 252: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, char *tag, int co_flags)
1.53 joris 253: {
1.89 xsa 254: int kflag, oflags, exists;
1.53 joris 255: time_t rcstime;
256: CVSENTRIES *ent;
257: struct timeval tv[2];
1.87 joris 258: char *tosend;
1.97 joris 259: char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.96 xsa 260: char kbuf[8], stickytag[32], rev[CVS_REV_BUFSZ];
261: char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22 jfb 262:
1.87 joris 263: exists = 0;
264: tosend = NULL;
1.104 joris 265:
266: if (!(co_flags & CO_REMOVE))
267: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 268:
1.66 joris 269: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 270: cf->file_path, rev, co_flags,
1.66 joris 271: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 272:
1.78 xsa 273: if (co_flags & CO_DUMP) {
1.106 tobias 274: rcs_rev_write_fd(cf->file_rcs, rnum, STDOUT_FILENO, 0);
1.65 reyk 275: return;
276: }
1.87 joris 277:
1.66 joris 278: if (cvs_server_active == 0) {
1.87 joris 279: if (!(co_flags & CO_MERGE)) {
280: oflags = O_WRONLY | O_TRUNC;
281: if (cf->fd != -1) {
282: exists = 1;
283: (void)close(cf->fd);
284: } else {
285: oflags |= O_CREAT;
286: }
287:
288: cf->fd = open(cf->file_path, oflags);
289: if (cf->fd == -1)
290: fatal("cvs_checkout_file: open: %s",
291: strerror(errno));
292:
1.106 tobias 293: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 0);
1.87 joris 294: } else {
295: cvs_merge_file(cf, 1);
1.66 joris 296: }
297:
298: if (fchmod(cf->fd, 0644) == -1)
299: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
300:
1.87 joris 301: if ((exists == 0) && (cf->file_ent == NULL) &&
302: !(co_flags & CO_MERGE))
1.66 joris 303: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 304: else
1.66 joris 305: time(&rcstime);
306:
307: tv[0].tv_sec = rcstime;
308: tv[0].tv_usec = 0;
309: tv[1] = tv[0];
310: if (futimes(cf->fd, tv) == -1)
311: fatal("cvs_checkout_file: futimes: %s",
312: strerror(errno));
1.53 joris 313: } else {
314: time(&rcstime);
1.41 joris 315: }
316:
1.84 joris 317: asctime_r(gmtime(&rcstime), tbuf);
1.107 ! tobias 318: tbuf[strcspn(tbuf, "\n")] = '\0';
1.58 joris 319:
1.78 xsa 320: if (co_flags & CO_MERGE) {
1.89 xsa 321: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 322: tbuf);
323: } else {
324: strlcpy(timebuf, tbuf, sizeof(timebuf));
325: }
1.41 joris 326:
1.89 xsa 327: if (co_flags & CO_SETSTICKY)
1.105 joris 328: if (tag != NULL)
1.93 niallo 329: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
1.105 joris 330: tag);
1.93 niallo 331: else
332: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
333: rev);
1.89 xsa 334: else
1.60 joris 335: stickytag[0] = '\0';
336:
1.85 xsa 337: kbuf[0] = '\0';
338: if (cf->file_ent != NULL) {
339: if (cf->file_ent->ce_opts != NULL)
340: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
341: } else if (cf->file_rcs->rf_expand != NULL) {
342: kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
343: if (!(kflag & RCS_KWEXP_DEFAULT))
1.89 xsa 344: (void)xsnprintf(kbuf, sizeof(kbuf),
1.85 xsa 345: "-k%s", cf->file_rcs->rf_expand);
346: }
347:
1.89 xsa 348: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.85 xsa 349: cf->file_name, rev, timebuf, kbuf, stickytag);
1.41 joris 350:
1.66 joris 351: if (cvs_server_active == 0) {
1.95 joris 352: if (!(co_flags & CO_REMOVE)) {
353: ent = cvs_ent_open(cf->file_wd);
354: cvs_ent_add(ent, entry);
355: cvs_ent_close(ent, ENT_SYNC);
356: }
1.66 joris 357: } else {
1.87 joris 358: if (co_flags & CO_MERGE) {
359: cvs_merge_file(cf, 1);
360: tosend = cf->file_path;
361: }
362:
1.78 xsa 363: if (co_flags & CO_COMMIT)
1.69 joris 364: cvs_server_update_entry("Checked-in", cf);
1.87 joris 365: else if (co_flags & CO_MERGE)
366: cvs_server_update_entry("Merged", cf);
1.95 joris 367: else if (co_flags & CO_REMOVE)
368: cvs_server_update_entry("Removed", cf);
1.69 joris 369: else
370: cvs_server_update_entry("Updated", cf);
1.68 joris 371:
1.95 joris 372: if (!(co_flags & CO_REMOVE))
373: cvs_remote_output(entry);
1.66 joris 374:
1.95 joris 375: if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87 joris 376: if (!(co_flags & CO_MERGE)) {
1.89 xsa 377: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 378: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 xsa 379:
1.87 joris 380: rcs_rev_write_stmp(cf->file_rcs, rnum,
381: template, 0);
382: tosend = template;
383: }
384:
385: cvs_remote_send_file(tosend);
386:
387: if (!(co_flags & CO_MERGE)) {
388: (void)unlink(template);
389: cvs_worklist_run(&temp_files,
390: cvs_worklist_unlink);
391: }
1.68 joris 392: }
1.66 joris 393: }
1.1 jfb 394: }