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