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