Annotation of src/usr.bin/cvs/checkout.c, Revision 1.125
1.125 ! tobias 1: /* $OpenBSD: checkout.c,v 1.124 2008/02/03 23:34:41 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.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: }
290: }
1.119 joris 291:
1.123 joris 292: if (mc->mc_canfree == 1) {
293: for (fl = TAILQ_FIRST(&(mc->mc_modules));
294: fl != TAILQ_END(&(mc->mc_modules)); fl = nxt) {
295: nxt = TAILQ_NEXT(fl, flist);
296: TAILQ_REMOVE(&(mc->mc_modules), fl, flist);
297: xfree(fl->file_path);
298: xfree(fl);
299: }
300: }
1.120 joris 301:
1.123 joris 302: while ((ip = TAILQ_FIRST(&checkout_ign_pats)) != NULL) {
303: TAILQ_REMOVE(&checkout_ign_pats, ip, ip_list);
304: xfree(ip);
1.113 tobias 305: }
1.119 joris 306:
307: xfree(mc);
1.113 tobias 308: }
309: }
1.100 tobias 310:
1.113 tobias 311: static int
312: checkout_classify(const char *repo, const char *arg)
313: {
314: char *d, *f, fpath[MAXPATHLEN];
315: struct stat sb;
316:
317: if (stat(repo, &sb) == 0) {
1.123 joris 318: if (S_ISDIR(sb.st_mode))
319: return CVS_DIR;
1.113 tobias 320: }
1.97 joris 321:
1.113 tobias 322: d = dirname(repo);
323: f = basename(repo);
324:
325: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s%s", d, f, RCS_FILE_EXT);
326: if (stat(fpath, &sb) == 0) {
327: if (!S_ISREG(sb.st_mode)) {
328: cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
329: return 0;
330: }
331: return CVS_FILE;
1.33 xsa 332: }
1.113 tobias 333:
334: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s/%s%s",
335: d, CVS_PATH_ATTIC, f, RCS_FILE_EXT);
336: if (stat(fpath, &sb) == 0) {
337: if (!S_ISREG(sb.st_mode)) {
338: cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
339: return 0;
340: }
341: return CVS_FILE;
342: }
343:
344: cvs_log(LP_ERR, "cannot find module `%s' - ignored", arg);
345: return 0;
1.14 joris 346: }
1.9 jfb 347:
1.53 joris 348: static void
349: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 350: {
1.53 joris 351: struct cvs_flisthead fl, dl;
352: struct cvs_recursion cr;
353:
354: TAILQ_INIT(&fl);
355: TAILQ_INIT(&dl);
1.94 joris 356:
1.99 xsa 357: cvs_history_add((cvs_cmdop == CVS_OP_CHECKOUT) ?
358: CVS_HISTORY_CHECKOUT : CVS_HISTORY_EXPORT, NULL, wdbase);
1.41 joris 359:
1.112 tobias 360: if (print_stdout) {
361: cr.enterdir = NULL;
362: cr.leavedir = NULL;
363: } else {
364: cr.enterdir = cvs_update_enterdir;
1.118 tobias 365: cr.leavedir = prune_dirs ? cvs_update_leavedir : NULL;
1.112 tobias 366: }
1.64 joris 367: cr.fileproc = cvs_update_local;
1.78 xsa 368: cr.flags = flags;
1.41 joris 369:
1.53 joris 370: cvs_repository_lock(repobase);
1.111 tobias 371: cvs_repository_getdir(repobase, wdbase, &fl, &dl,
372: flags & CR_RECURSE_DIRS ? 1 : 0);
1.1 jfb 373:
1.53 joris 374: cvs_file_walklist(&fl, &cr);
375: cvs_file_freelist(&fl);
1.23 joris 376:
1.53 joris 377: cvs_repository_unlock(repobase);
1.23 joris 378:
1.53 joris 379: cvs_file_walklist(&dl, &cr);
380: cvs_file_freelist(&dl);
381: }
1.23 joris 382:
1.58 joris 383: void
1.105 joris 384: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, char *tag, int co_flags)
1.53 joris 385: {
1.125 ! tobias 386: int cf_kflag, oflags, exists;
1.53 joris 387: time_t rcstime;
388: CVSENTRIES *ent;
389: struct timeval tv[2];
1.87 joris 390: char *tosend;
1.97 joris 391: char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.115 xsa 392: char kbuf[8], sticky[CVS_REV_BUFSZ], rev[CVS_REV_BUFSZ];
1.96 xsa 393: char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22 jfb 394:
1.87 joris 395: exists = 0;
396: tosend = NULL;
1.104 joris 397:
398: if (!(co_flags & CO_REMOVE))
399: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 400:
1.66 joris 401: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 402: cf->file_path, rev, co_flags,
1.66 joris 403: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 404:
1.78 xsa 405: if (co_flags & CO_DUMP) {
1.106 tobias 406: rcs_rev_write_fd(cf->file_rcs, rnum, STDOUT_FILENO, 0);
1.65 reyk 407: return;
408: }
1.87 joris 409:
1.66 joris 410: if (cvs_server_active == 0) {
1.87 joris 411: if (!(co_flags & CO_MERGE)) {
412: oflags = O_WRONLY | O_TRUNC;
413: if (cf->fd != -1) {
414: exists = 1;
415: (void)close(cf->fd);
416: } else {
417: oflags |= O_CREAT;
418: }
419:
420: cf->fd = open(cf->file_path, oflags);
421: if (cf->fd == -1)
422: fatal("cvs_checkout_file: open: %s",
423: strerror(errno));
424:
1.106 tobias 425: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 0);
1.87 joris 426: } else {
427: cvs_merge_file(cf, 1);
1.66 joris 428: }
429:
430: if (fchmod(cf->fd, 0644) == -1)
431: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
432:
1.87 joris 433: if ((exists == 0) && (cf->file_ent == NULL) &&
434: !(co_flags & CO_MERGE))
1.66 joris 435: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 436: else
1.66 joris 437: time(&rcstime);
438:
439: tv[0].tv_sec = rcstime;
440: tv[0].tv_usec = 0;
441: tv[1] = tv[0];
442: if (futimes(cf->fd, tv) == -1)
443: fatal("cvs_checkout_file: futimes: %s",
444: strerror(errno));
1.53 joris 445: } else {
446: time(&rcstime);
1.41 joris 447: }
448:
1.84 joris 449: asctime_r(gmtime(&rcstime), tbuf);
1.107 tobias 450: tbuf[strcspn(tbuf, "\n")] = '\0';
1.58 joris 451:
1.78 xsa 452: if (co_flags & CO_MERGE) {
1.89 xsa 453: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 454: tbuf);
455: } else {
456: strlcpy(timebuf, tbuf, sizeof(timebuf));
457: }
1.41 joris 458:
1.89 xsa 459: if (co_flags & CO_SETSTICKY)
1.105 joris 460: if (tag != NULL)
1.114 xsa 461: (void)xsnprintf(sticky, sizeof(sticky), "T%s", tag);
1.93 niallo 462: else
1.114 xsa 463: (void)xsnprintf(sticky, sizeof(sticky), "T%s", rev);
1.109 tobias 464: else if (!reset_stickies && cf->file_ent != NULL &&
465: cf->file_ent->ce_tag != NULL)
1.114 xsa 466: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.109 tobias 467: cf->file_ent->ce_tag);
1.89 xsa 468: else
1.114 xsa 469: sticky[0] = '\0';
1.60 joris 470:
1.85 xsa 471: kbuf[0] = '\0';
1.125 ! tobias 472: if (cf->file_rcs->rf_expand != NULL) {
! 473: cf_kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
! 474: if (kflag || cf_kflag != RCS_KWEXP_DEFAULT)
! 475: (void)xsnprintf(kbuf, sizeof(kbuf),
! 476: "-k%s", cf->file_rcs->rf_expand);
! 477: } else if (!reset_stickies && cf->file_ent != NULL) {
1.85 xsa 478: if (cf->file_ent->ce_opts != NULL)
479: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
480: }
481:
1.89 xsa 482: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.114 xsa 483: cf->file_name, rev, timebuf, kbuf, sticky);
1.41 joris 484:
1.66 joris 485: if (cvs_server_active == 0) {
1.108 tobias 486: if (!(co_flags & CO_REMOVE) && cvs_cmdop != CVS_OP_EXPORT) {
1.95 joris 487: ent = cvs_ent_open(cf->file_wd);
488: cvs_ent_add(ent, entry);
489: cvs_ent_close(ent, ENT_SYNC);
490: }
1.66 joris 491: } else {
1.87 joris 492: if (co_flags & CO_MERGE) {
493: cvs_merge_file(cf, 1);
494: tosend = cf->file_path;
495: }
496:
1.78 xsa 497: if (co_flags & CO_COMMIT)
1.69 joris 498: cvs_server_update_entry("Checked-in", cf);
1.87 joris 499: else if (co_flags & CO_MERGE)
500: cvs_server_update_entry("Merged", cf);
1.95 joris 501: else if (co_flags & CO_REMOVE)
502: cvs_server_update_entry("Removed", cf);
1.69 joris 503: else
504: cvs_server_update_entry("Updated", cf);
1.68 joris 505:
1.95 joris 506: if (!(co_flags & CO_REMOVE))
507: cvs_remote_output(entry);
1.66 joris 508:
1.95 joris 509: if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87 joris 510: if (!(co_flags & CO_MERGE)) {
1.89 xsa 511: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 512: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 xsa 513:
1.87 joris 514: rcs_rev_write_stmp(cf->file_rcs, rnum,
515: template, 0);
516: tosend = template;
517: }
518:
519: cvs_remote_send_file(tosend);
520:
521: if (!(co_flags & CO_MERGE)) {
522: (void)unlink(template);
523: cvs_worklist_run(&temp_files,
524: cvs_worklist_unlink);
525: }
1.68 joris 526: }
1.66 joris 527: }
1.1 jfb 528: }