Annotation of src/usr.bin/cvs/checkout.c, Revision 1.128
1.128 ! joris 1: /* $OpenBSD: checkout.c,v 1.127 2008/02/04 21:29:17 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.92 otto 18: #include <sys/param.h>
19: #include <sys/dirent.h>
20: #include <sys/stat.h>
1.117 tobias 21: #include <sys/time.h>
1.92 otto 22:
23: #include <errno.h>
24: #include <fcntl.h>
1.100 tobias 25: #include <libgen.h>
1.92 otto 26: #include <string.h>
27: #include <unistd.h>
1.1 jfb 28:
29: #include "cvs.h"
1.53 joris 30: #include "diff.h"
1.66 joris 31: #include "remote.h"
1.72 joris 32:
1.61 xsa 33: static void checkout_check_repository(int, char **);
1.113 tobias 34: static int checkout_classify(const char *, const char *);
1.53 joris 35: static void checkout_repository(const char *, const char *);
1.14 joris 36:
1.101 joris 37: extern int print_stdout;
1.54 joris 38: extern int prune_dirs;
1.57 joris 39: extern int build_dirs;
1.54 joris 40:
1.78 xsa 41: static int flags = CR_REPO | CR_RECURSE_DIRS;
1.120 joris 42: static char *dflag = NULL;
1.125 tobias 43: static char *koptstr = NULL;
1.78 xsa 44:
1.127 joris 45: static int nflag = 0;
46:
1.22 jfb 47: struct cvs_cmd cvs_cmd_checkout = {
1.112 tobias 48: CVS_OP_CHECKOUT, CVS_USE_WDIR, "checkout",
1.22 jfb 49: { "co", "get" },
1.53 joris 50: "Checkout a working copy of a repository",
1.33 xsa 51: "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22 jfb 52: "[-t id] module ...",
1.77 xsa 53: "AcD:d:fj:k:lNnPpRr:st:",
1.59 joris 54: NULL,
55: cvs_checkout
56: };
57:
58: struct cvs_cmd cvs_cmd_export = {
1.112 tobias 59: CVS_OP_EXPORT, CVS_USE_WDIR, "export",
1.59 joris 60: { "exp", "ex" },
61: "Export sources from CVS, similar to checkout",
1.61 xsa 62: "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
63: "D:d:k:flNnRr:",
1.22 jfb 64: NULL,
1.61 xsa 65: cvs_export
1.33 xsa 66: };
67:
1.53 joris 68: int
69: cvs_checkout(int argc, char **argv)
1.1 jfb 70: {
1.78 xsa 71: int ch;
1.13 jfb 72:
1.53 joris 73: while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1 jfb 74: switch (ch) {
1.76 xsa 75: case 'A':
76: reset_stickies = 1;
77: break;
1.120 joris 78: case 'd':
79: if (dflag != NULL)
80: fatal("-d specified two or more times");
81: dflag = optarg;
82: break;
1.125 tobias 83: case 'k':
84: koptstr = optarg;
85: kflag = rcs_kflag_get(koptstr);
86: if (RCS_KWEXP_INVAL(kflag)) {
87: cvs_log(LP_ERR,
88: "invalid RCS keyword expension mode");
89: fatal("%s", cvs_cmd_add.cmd_synopsis);
90: }
91: break;
1.77 xsa 92: case 'l':
93: flags &= ~CR_RECURSE_DIRS;
94: break;
1.104 joris 95: case 'N':
96: break;
1.127 joris 97: case 'n':
98: nflag = 1;
99: break;
1.54 joris 100: case 'P':
101: prune_dirs = 1;
1.77 xsa 102: break;
1.101 joris 103: case 'p':
1.112 tobias 104: cmdp->cmd_flags &= ~CVS_USE_WDIR;
1.101 joris 105: print_stdout = 1;
106: cvs_noexec = 1;
1.127 joris 107: nflag = 1;
1.101 joris 108: break;
1.77 xsa 109: case 'R':
1.110 tobias 110: flags |= CR_RECURSE_DIRS;
1.54 joris 111: break;
1.93 niallo 112: case 'r':
1.97 joris 113: cvs_specified_tag = optarg;
1.93 niallo 114: break;
1.1 jfb 115: default:
1.53 joris 116: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1 jfb 117: }
118: }
119:
120: argc -= optind;
121: argv += optind;
122:
1.53 joris 123: if (argc == 0)
124: fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22 jfb 125:
1.61 xsa 126: checkout_check_repository(argc, argv);
127:
128: return (0);
129: }
130:
131: int
132: cvs_export(int argc, char **argv)
133: {
1.78 xsa 134: int ch;
1.61 xsa 135:
136: prune_dirs = 1;
137:
138: while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
139: switch (ch) {
1.125 tobias 140: case 'k':
141: koptstr = optarg;
142: kflag = rcs_kflag_get(koptstr);
143: if (RCS_KWEXP_INVAL(kflag)) {
144: cvs_log(LP_ERR,
145: "invalid RCS keyword expension mode");
146: fatal("%s", cvs_cmd_add.cmd_synopsis);
147: }
148: break;
1.61 xsa 149: case 'l':
150: flags &= ~CR_RECURSE_DIRS;
151: break;
152: case 'R':
1.110 tobias 153: flags |= CR_RECURSE_DIRS;
1.98 xsa 154: break;
155: case 'r':
156: cvs_specified_tag = optarg;
1.61 xsa 157: break;
158: default:
159: fatal("%s", cvs_cmd_export.cmd_synopsis);
160: }
161: }
162:
163: argc -= optind;
164: argv += optind;
165:
1.108 tobias 166: if (cvs_specified_tag == NULL)
167: fatal("must specify a tag or date");
168:
1.61 xsa 169: if (argc == 0)
170: fatal("%s", cvs_cmd_export.cmd_synopsis);
171:
172: checkout_check_repository(argc, argv);
173:
174: return (0);
175: }
176:
177: static void
178: checkout_check_repository(int argc, char **argv)
179: {
1.67 xsa 180: int i;
1.123 joris 181: char *wdir, *d;
1.79 joris 182: struct cvs_recursion cr;
1.119 joris 183: struct module_checkout *mc;
1.123 joris 184: struct cvs_ignpat *ip;
185: struct cvs_filelist *fl, *nxt;
186: char repo[MAXPATHLEN], fpath[MAXPATHLEN], *f[1];
1.79 joris 187:
1.102 tobias 188: build_dirs = print_stdout ? 0 : 1;
189:
1.79 joris 190: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
191: cvs_client_connect_to_server();
192:
1.97 joris 193: if (cvs_specified_tag != NULL)
194: cvs_client_send_request("Argument -r%s",
195: cvs_specified_tag);
1.79 joris 196: if (reset_stickies == 1)
197: cvs_client_send_request("Argument -A");
1.122 joris 198:
1.125 tobias 199: if (kflag)
200: cvs_client_send_request("Argument -k%s", koptstr);
201:
1.122 joris 202: if (dflag != NULL)
203: cvs_client_send_request("Argument -d%s", dflag);
1.80 xsa 204:
205: if (!(flags & CR_RECURSE_DIRS))
206: cvs_client_send_request("Argument -l");
1.79 joris 207:
208: if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
209: cvs_client_send_request("Argument -P");
210:
1.101 joris 211: if (print_stdout == 1)
212: cvs_client_send_request("Argument -p");
213:
1.127 joris 214: if (nflag == 1)
215: cvs_client_send_request("Argument -n");
216:
1.79 joris 217: cr.enterdir = NULL;
218: cr.leavedir = NULL;
1.103 tobias 219: if (print_stdout)
220: cr.fileproc = NULL;
221: else
222: cr.fileproc = cvs_client_sendfile;
223:
224: flags &= ~CR_REPO;
1.79 joris 225: cr.flags = flags;
226:
1.108 tobias 227: if (cvs_cmdop != CVS_OP_EXPORT)
228: cvs_file_run(argc, argv, &cr);
1.79 joris 229:
230: cvs_client_send_files(argv, argc);
231: cvs_client_senddir(".");
232:
233: cvs_client_send_request("%s",
234: (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
235:
236: cvs_client_get_responses();
237:
238: return;
239: }
1.61 xsa 240:
1.104 joris 241: cvs_directory_tag = cvs_specified_tag;
242:
1.53 joris 243: for (i = 0; i < argc; i++) {
1.119 joris 244: mc = cvs_module_lookup(argv[i]);
245: current_module = mc;
1.116 joris 246:
1.123 joris 247: TAILQ_FOREACH(fl, &(mc->mc_ignores), flist)
248: cvs_file_ignore(fl->file_path, &checkout_ign_pats);
1.13 jfb 249:
1.123 joris 250: TAILQ_FOREACH(fl, &(mc->mc_modules), flist) {
251: (void)xsnprintf(repo, sizeof(repo), "%s/%s",
252: current_cvsroot->cr_dir, fl->file_path);
1.121 joris 253:
1.123 joris 254: if (!(mc->mc_flags & MODULE_ALIAS) || dflag != NULL)
255: module_repo_root = fl->file_path;
256:
257: if (mc->mc_flags & MODULE_NORECURSE)
258: flags &= ~CR_RECURSE_DIRS;
259:
260: if (dflag != NULL)
261: wdir = dflag;
1.124 joris 262: else if (mc->mc_flags & MODULE_ALIAS)
263: wdir = fl->file_path;
1.123 joris 264: else
1.124 joris 265: wdir = mc->mc_name;
1.123 joris 266:
267: switch (checkout_classify(repo, fl->file_path)) {
268: case CVS_FILE:
269: cr.fileproc = cvs_update_local;
270: cr.flags = flags;
271:
272: if (!(mc->mc_flags & MODULE_ALIAS)) {
273: module_repo_root =
274: dirname(fl->file_path);
275: d = wdir;
276: (void)xsnprintf(fpath, sizeof(fpath),
277: "%s/%s", d,
278: basename(fl->file_path));
279: } else {
280: d = dirname(wdir);
281: strlcpy(fpath, fl->file_path,
282: sizeof(fpath));
283: }
284:
285: if (build_dirs == 1)
286: cvs_mkpath(d, cvs_specified_tag);
287:
288: f[0] = fpath;
289: cvs_file_run(1, f, &cr);
290: break;
291: case CVS_DIR:
292: if (build_dirs == 1)
293: cvs_mkpath(wdir, cvs_specified_tag);
294: checkout_repository(repo, wdir);
295: break;
296: default:
297: break;
298: }
1.126 joris 299:
1.127 joris 300: if (nflag != 1 && mc->mc_prog != NULL &&
1.128 ! joris 301: mc->mc_flags & MODULE_RUN_ON_CHECKOUT)
1.126 joris 302: cvs_exec(mc->mc_prog);
1.123 joris 303: }
1.119 joris 304:
1.123 joris 305: if (mc->mc_canfree == 1) {
306: for (fl = TAILQ_FIRST(&(mc->mc_modules));
307: fl != TAILQ_END(&(mc->mc_modules)); fl = nxt) {
308: nxt = TAILQ_NEXT(fl, flist);
309: TAILQ_REMOVE(&(mc->mc_modules), fl, flist);
310: xfree(fl->file_path);
311: xfree(fl);
312: }
313: }
1.120 joris 314:
1.123 joris 315: while ((ip = TAILQ_FIRST(&checkout_ign_pats)) != NULL) {
316: TAILQ_REMOVE(&checkout_ign_pats, ip, ip_list);
317: xfree(ip);
1.113 tobias 318: }
1.119 joris 319:
320: xfree(mc);
1.113 tobias 321: }
322: }
1.100 tobias 323:
1.113 tobias 324: static int
325: checkout_classify(const char *repo, const char *arg)
326: {
327: char *d, *f, fpath[MAXPATHLEN];
328: struct stat sb;
329:
330: if (stat(repo, &sb) == 0) {
1.123 joris 331: if (S_ISDIR(sb.st_mode))
332: return CVS_DIR;
1.113 tobias 333: }
1.97 joris 334:
1.113 tobias 335: d = dirname(repo);
336: f = basename(repo);
337:
338: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s%s", d, f, RCS_FILE_EXT);
339: if (stat(fpath, &sb) == 0) {
340: if (!S_ISREG(sb.st_mode)) {
341: cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
342: return 0;
343: }
344: return CVS_FILE;
1.33 xsa 345: }
1.113 tobias 346:
347: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s/%s%s",
348: d, CVS_PATH_ATTIC, f, RCS_FILE_EXT);
349: if (stat(fpath, &sb) == 0) {
350: if (!S_ISREG(sb.st_mode)) {
351: cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
352: return 0;
353: }
354: return CVS_FILE;
355: }
356:
357: cvs_log(LP_ERR, "cannot find module `%s' - ignored", arg);
358: return 0;
1.14 joris 359: }
1.9 jfb 360:
1.53 joris 361: static void
362: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 363: {
1.53 joris 364: struct cvs_flisthead fl, dl;
365: struct cvs_recursion cr;
366:
367: TAILQ_INIT(&fl);
368: TAILQ_INIT(&dl);
1.94 joris 369:
1.99 xsa 370: cvs_history_add((cvs_cmdop == CVS_OP_CHECKOUT) ?
371: CVS_HISTORY_CHECKOUT : CVS_HISTORY_EXPORT, NULL, wdbase);
1.41 joris 372:
1.112 tobias 373: if (print_stdout) {
374: cr.enterdir = NULL;
375: cr.leavedir = NULL;
376: } else {
377: cr.enterdir = cvs_update_enterdir;
1.118 tobias 378: cr.leavedir = prune_dirs ? cvs_update_leavedir : NULL;
1.112 tobias 379: }
1.64 joris 380: cr.fileproc = cvs_update_local;
1.78 xsa 381: cr.flags = flags;
1.41 joris 382:
1.53 joris 383: cvs_repository_lock(repobase);
1.111 tobias 384: cvs_repository_getdir(repobase, wdbase, &fl, &dl,
385: flags & CR_RECURSE_DIRS ? 1 : 0);
1.1 jfb 386:
1.53 joris 387: cvs_file_walklist(&fl, &cr);
388: cvs_file_freelist(&fl);
1.23 joris 389:
1.53 joris 390: cvs_repository_unlock(repobase);
1.23 joris 391:
1.53 joris 392: cvs_file_walklist(&dl, &cr);
393: cvs_file_freelist(&dl);
394: }
1.23 joris 395:
1.58 joris 396: void
1.105 joris 397: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, char *tag, int co_flags)
1.53 joris 398: {
1.125 tobias 399: int cf_kflag, oflags, exists;
1.53 joris 400: time_t rcstime;
401: CVSENTRIES *ent;
402: struct timeval tv[2];
1.87 joris 403: char *tosend;
1.97 joris 404: char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.115 xsa 405: char kbuf[8], sticky[CVS_REV_BUFSZ], rev[CVS_REV_BUFSZ];
1.96 xsa 406: char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22 jfb 407:
1.87 joris 408: exists = 0;
409: tosend = NULL;
1.104 joris 410:
411: if (!(co_flags & CO_REMOVE))
412: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 413:
1.66 joris 414: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 415: cf->file_path, rev, co_flags,
1.66 joris 416: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 417:
1.78 xsa 418: if (co_flags & CO_DUMP) {
1.106 tobias 419: rcs_rev_write_fd(cf->file_rcs, rnum, STDOUT_FILENO, 0);
1.65 reyk 420: return;
421: }
1.87 joris 422:
1.66 joris 423: if (cvs_server_active == 0) {
1.87 joris 424: if (!(co_flags & CO_MERGE)) {
425: oflags = O_WRONLY | O_TRUNC;
426: if (cf->fd != -1) {
427: exists = 1;
428: (void)close(cf->fd);
429: } else {
430: oflags |= O_CREAT;
431: }
432:
433: cf->fd = open(cf->file_path, oflags);
434: if (cf->fd == -1)
435: fatal("cvs_checkout_file: open: %s",
436: strerror(errno));
437:
1.106 tobias 438: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 0);
1.87 joris 439: } else {
440: cvs_merge_file(cf, 1);
1.66 joris 441: }
442:
443: if (fchmod(cf->fd, 0644) == -1)
444: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
445:
1.87 joris 446: if ((exists == 0) && (cf->file_ent == NULL) &&
447: !(co_flags & CO_MERGE))
1.66 joris 448: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 449: else
1.66 joris 450: time(&rcstime);
451:
452: tv[0].tv_sec = rcstime;
453: tv[0].tv_usec = 0;
454: tv[1] = tv[0];
455: if (futimes(cf->fd, tv) == -1)
456: fatal("cvs_checkout_file: futimes: %s",
457: strerror(errno));
1.53 joris 458: } else {
459: time(&rcstime);
1.41 joris 460: }
461:
1.84 joris 462: asctime_r(gmtime(&rcstime), tbuf);
1.107 tobias 463: tbuf[strcspn(tbuf, "\n")] = '\0';
1.58 joris 464:
1.78 xsa 465: if (co_flags & CO_MERGE) {
1.89 xsa 466: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 467: tbuf);
468: } else {
469: strlcpy(timebuf, tbuf, sizeof(timebuf));
470: }
1.41 joris 471:
1.89 xsa 472: if (co_flags & CO_SETSTICKY)
1.105 joris 473: if (tag != NULL)
1.114 xsa 474: (void)xsnprintf(sticky, sizeof(sticky), "T%s", tag);
1.93 niallo 475: else
1.114 xsa 476: (void)xsnprintf(sticky, sizeof(sticky), "T%s", rev);
1.109 tobias 477: else if (!reset_stickies && cf->file_ent != NULL &&
478: cf->file_ent->ce_tag != NULL)
1.114 xsa 479: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.109 tobias 480: cf->file_ent->ce_tag);
1.89 xsa 481: else
1.114 xsa 482: sticky[0] = '\0';
1.60 joris 483:
1.85 xsa 484: kbuf[0] = '\0';
1.125 tobias 485: if (cf->file_rcs->rf_expand != NULL) {
486: cf_kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
487: if (kflag || cf_kflag != RCS_KWEXP_DEFAULT)
488: (void)xsnprintf(kbuf, sizeof(kbuf),
489: "-k%s", cf->file_rcs->rf_expand);
490: } else if (!reset_stickies && cf->file_ent != NULL) {
1.85 xsa 491: if (cf->file_ent->ce_opts != NULL)
492: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
493: }
494:
1.89 xsa 495: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.114 xsa 496: cf->file_name, rev, timebuf, kbuf, sticky);
1.41 joris 497:
1.66 joris 498: if (cvs_server_active == 0) {
1.108 tobias 499: if (!(co_flags & CO_REMOVE) && cvs_cmdop != CVS_OP_EXPORT) {
1.95 joris 500: ent = cvs_ent_open(cf->file_wd);
501: cvs_ent_add(ent, entry);
502: cvs_ent_close(ent, ENT_SYNC);
503: }
1.66 joris 504: } else {
1.87 joris 505: if (co_flags & CO_MERGE) {
506: cvs_merge_file(cf, 1);
507: tosend = cf->file_path;
508: }
509:
1.78 xsa 510: if (co_flags & CO_COMMIT)
1.69 joris 511: cvs_server_update_entry("Checked-in", cf);
1.87 joris 512: else if (co_flags & CO_MERGE)
513: cvs_server_update_entry("Merged", cf);
1.95 joris 514: else if (co_flags & CO_REMOVE)
515: cvs_server_update_entry("Removed", cf);
1.69 joris 516: else
517: cvs_server_update_entry("Updated", cf);
1.68 joris 518:
1.95 joris 519: if (!(co_flags & CO_REMOVE))
520: cvs_remote_output(entry);
1.66 joris 521:
1.95 joris 522: if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87 joris 523: if (!(co_flags & CO_MERGE)) {
1.89 xsa 524: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 525: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 xsa 526:
1.87 joris 527: rcs_rev_write_stmp(cf->file_rcs, rnum,
528: template, 0);
529: tosend = template;
530: }
531:
532: cvs_remote_send_file(tosend);
533:
534: if (!(co_flags & CO_MERGE)) {
535: (void)unlink(template);
536: cvs_worklist_run(&temp_files,
537: cvs_worklist_unlink);
538: }
1.68 joris 539: }
1.66 joris 540: }
1.1 jfb 541: }