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