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