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