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