Annotation of src/usr.bin/cvs/checkout.c, Revision 1.123
1.123 ! joris 1: /* $OpenBSD: checkout.c,v 1.122 2008/02/03 20:01:37 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.123 ! joris 156: char *wdir, *d;
1.79 joris 157: struct cvs_recursion cr;
1.119 joris 158: struct module_checkout *mc;
1.123 ! joris 159: struct cvs_ignpat *ip;
! 160: struct cvs_filelist *fl, *nxt;
! 161: char repo[MAXPATHLEN], fpath[MAXPATHLEN], *f[1];
1.79 joris 162:
1.102 tobias 163: build_dirs = print_stdout ? 0 : 1;
164:
1.79 joris 165: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
166: cvs_client_connect_to_server();
167:
1.97 joris 168: if (cvs_specified_tag != NULL)
169: cvs_client_send_request("Argument -r%s",
170: cvs_specified_tag);
1.79 joris 171: if (reset_stickies == 1)
172: cvs_client_send_request("Argument -A");
1.122 joris 173:
174: if (dflag != NULL)
175: cvs_client_send_request("Argument -d%s", dflag);
1.80 xsa 176:
177: if (!(flags & CR_RECURSE_DIRS))
178: cvs_client_send_request("Argument -l");
1.79 joris 179:
180: if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
181: cvs_client_send_request("Argument -P");
182:
1.101 joris 183: if (print_stdout == 1)
184: cvs_client_send_request("Argument -p");
185:
1.79 joris 186: cr.enterdir = NULL;
187: cr.leavedir = NULL;
1.103 tobias 188: if (print_stdout)
189: cr.fileproc = NULL;
190: else
191: cr.fileproc = cvs_client_sendfile;
192:
193: flags &= ~CR_REPO;
1.79 joris 194: cr.flags = flags;
195:
1.108 tobias 196: if (cvs_cmdop != CVS_OP_EXPORT)
197: cvs_file_run(argc, argv, &cr);
1.79 joris 198:
199: cvs_client_send_files(argv, argc);
200: cvs_client_senddir(".");
201:
202: cvs_client_send_request("%s",
203: (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
204:
205: cvs_client_get_responses();
206:
207: return;
208: }
1.61 xsa 209:
1.104 joris 210: cvs_directory_tag = cvs_specified_tag;
211:
1.53 joris 212: for (i = 0; i < argc; i++) {
1.119 joris 213: mc = cvs_module_lookup(argv[i]);
214: current_module = mc;
1.116 joris 215:
1.123 ! joris 216: TAILQ_FOREACH(fl, &(mc->mc_ignores), flist)
! 217: cvs_file_ignore(fl->file_path, &checkout_ign_pats);
1.13 jfb 218:
1.123 ! joris 219: TAILQ_FOREACH(fl, &(mc->mc_modules), flist) {
! 220: (void)xsnprintf(repo, sizeof(repo), "%s/%s",
! 221: current_cvsroot->cr_dir, fl->file_path);
1.121 joris 222:
1.123 ! joris 223: if (!(mc->mc_flags & MODULE_ALIAS) || dflag != NULL)
! 224: module_repo_root = fl->file_path;
! 225:
! 226: if (mc->mc_flags & MODULE_NORECURSE)
! 227: flags &= ~CR_RECURSE_DIRS;
! 228:
! 229: if (dflag != NULL)
! 230: wdir = dflag;
! 231: else
! 232: wdir = mc->mc_wdir;
! 233:
! 234: switch (checkout_classify(repo, fl->file_path)) {
! 235: case CVS_FILE:
! 236: cr.fileproc = cvs_update_local;
! 237: cr.flags = flags;
! 238:
! 239: if (!(mc->mc_flags & MODULE_ALIAS)) {
! 240: module_repo_root =
! 241: dirname(fl->file_path);
! 242: d = wdir;
! 243: (void)xsnprintf(fpath, sizeof(fpath),
! 244: "%s/%s", d,
! 245: basename(fl->file_path));
! 246: } else {
! 247: d = dirname(wdir);
! 248: strlcpy(fpath, fl->file_path,
! 249: sizeof(fpath));
! 250: }
! 251:
! 252: if (build_dirs == 1)
! 253: cvs_mkpath(d, cvs_specified_tag);
! 254:
! 255: f[0] = fpath;
! 256: cvs_file_run(1, f, &cr);
! 257: break;
! 258: case CVS_DIR:
! 259: if (build_dirs == 1)
! 260: cvs_mkpath(wdir, cvs_specified_tag);
! 261: checkout_repository(repo, wdir);
! 262: break;
! 263: default:
! 264: break;
! 265: }
! 266: }
1.119 joris 267:
1.123 ! joris 268: if (mc->mc_canfree == 1) {
! 269: for (fl = TAILQ_FIRST(&(mc->mc_modules));
! 270: fl != TAILQ_END(&(mc->mc_modules)); fl = nxt) {
! 271: nxt = TAILQ_NEXT(fl, flist);
! 272: TAILQ_REMOVE(&(mc->mc_modules), fl, flist);
! 273: xfree(fl->file_path);
! 274: xfree(fl);
! 275: }
! 276: }
1.120 joris 277:
1.123 ! joris 278: while ((ip = TAILQ_FIRST(&checkout_ign_pats)) != NULL) {
! 279: TAILQ_REMOVE(&checkout_ign_pats, ip, ip_list);
! 280: xfree(ip);
1.113 tobias 281: }
1.119 joris 282:
283: xfree(mc->mc_wdir);
284: xfree(mc);
1.113 tobias 285: }
286: }
1.100 tobias 287:
1.113 tobias 288: static int
289: checkout_classify(const char *repo, const char *arg)
290: {
291: char *d, *f, fpath[MAXPATHLEN];
292: struct stat sb;
293:
294: if (stat(repo, &sb) == 0) {
1.123 ! joris 295: if (S_ISDIR(sb.st_mode))
! 296: return CVS_DIR;
1.113 tobias 297: }
1.97 joris 298:
1.113 tobias 299: d = dirname(repo);
300: f = basename(repo);
301:
302: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s%s", d, f, RCS_FILE_EXT);
303: if (stat(fpath, &sb) == 0) {
304: if (!S_ISREG(sb.st_mode)) {
305: cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
306: return 0;
307: }
308: return CVS_FILE;
1.33 xsa 309: }
1.113 tobias 310:
311: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s/%s%s",
312: d, CVS_PATH_ATTIC, f, RCS_FILE_EXT);
313: if (stat(fpath, &sb) == 0) {
314: if (!S_ISREG(sb.st_mode)) {
315: cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
316: return 0;
317: }
318: return CVS_FILE;
319: }
320:
321: cvs_log(LP_ERR, "cannot find module `%s' - ignored", arg);
322: return 0;
1.14 joris 323: }
1.9 jfb 324:
1.53 joris 325: static void
326: checkout_repository(const char *repobase, const char *wdbase)
1.14 joris 327: {
1.53 joris 328: struct cvs_flisthead fl, dl;
329: struct cvs_recursion cr;
330:
331: TAILQ_INIT(&fl);
332: TAILQ_INIT(&dl);
1.94 joris 333:
1.99 xsa 334: cvs_history_add((cvs_cmdop == CVS_OP_CHECKOUT) ?
335: CVS_HISTORY_CHECKOUT : CVS_HISTORY_EXPORT, NULL, wdbase);
1.41 joris 336:
1.112 tobias 337: if (print_stdout) {
338: cr.enterdir = NULL;
339: cr.leavedir = NULL;
340: } else {
341: cr.enterdir = cvs_update_enterdir;
1.118 tobias 342: cr.leavedir = prune_dirs ? cvs_update_leavedir : NULL;
1.112 tobias 343: }
1.64 joris 344: cr.fileproc = cvs_update_local;
1.78 xsa 345: cr.flags = flags;
1.41 joris 346:
1.53 joris 347: cvs_repository_lock(repobase);
1.111 tobias 348: cvs_repository_getdir(repobase, wdbase, &fl, &dl,
349: flags & CR_RECURSE_DIRS ? 1 : 0);
1.1 jfb 350:
1.53 joris 351: cvs_file_walklist(&fl, &cr);
352: cvs_file_freelist(&fl);
1.23 joris 353:
1.53 joris 354: cvs_repository_unlock(repobase);
1.23 joris 355:
1.53 joris 356: cvs_file_walklist(&dl, &cr);
357: cvs_file_freelist(&dl);
358: }
1.23 joris 359:
1.58 joris 360: void
1.105 joris 361: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, char *tag, int co_flags)
1.53 joris 362: {
1.89 xsa 363: int kflag, oflags, exists;
1.53 joris 364: time_t rcstime;
365: CVSENTRIES *ent;
366: struct timeval tv[2];
1.87 joris 367: char *tosend;
1.97 joris 368: char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.115 xsa 369: char kbuf[8], sticky[CVS_REV_BUFSZ], rev[CVS_REV_BUFSZ];
1.96 xsa 370: char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22 jfb 371:
1.87 joris 372: exists = 0;
373: tosend = NULL;
1.104 joris 374:
375: if (!(co_flags & CO_REMOVE))
376: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 377:
1.66 joris 378: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 379: cf->file_path, rev, co_flags,
1.66 joris 380: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 381:
1.78 xsa 382: if (co_flags & CO_DUMP) {
1.106 tobias 383: rcs_rev_write_fd(cf->file_rcs, rnum, STDOUT_FILENO, 0);
1.65 reyk 384: return;
385: }
1.87 joris 386:
1.66 joris 387: if (cvs_server_active == 0) {
1.87 joris 388: if (!(co_flags & CO_MERGE)) {
389: oflags = O_WRONLY | O_TRUNC;
390: if (cf->fd != -1) {
391: exists = 1;
392: (void)close(cf->fd);
393: } else {
394: oflags |= O_CREAT;
395: }
396:
397: cf->fd = open(cf->file_path, oflags);
398: if (cf->fd == -1)
399: fatal("cvs_checkout_file: open: %s",
400: strerror(errno));
401:
1.106 tobias 402: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 0);
1.87 joris 403: } else {
404: cvs_merge_file(cf, 1);
1.66 joris 405: }
406:
407: if (fchmod(cf->fd, 0644) == -1)
408: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
409:
1.87 joris 410: if ((exists == 0) && (cf->file_ent == NULL) &&
411: !(co_flags & CO_MERGE))
1.66 joris 412: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 413: else
1.66 joris 414: time(&rcstime);
415:
416: tv[0].tv_sec = rcstime;
417: tv[0].tv_usec = 0;
418: tv[1] = tv[0];
419: if (futimes(cf->fd, tv) == -1)
420: fatal("cvs_checkout_file: futimes: %s",
421: strerror(errno));
1.53 joris 422: } else {
423: time(&rcstime);
1.41 joris 424: }
425:
1.84 joris 426: asctime_r(gmtime(&rcstime), tbuf);
1.107 tobias 427: tbuf[strcspn(tbuf, "\n")] = '\0';
1.58 joris 428:
1.78 xsa 429: if (co_flags & CO_MERGE) {
1.89 xsa 430: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 431: tbuf);
432: } else {
433: strlcpy(timebuf, tbuf, sizeof(timebuf));
434: }
1.41 joris 435:
1.89 xsa 436: if (co_flags & CO_SETSTICKY)
1.105 joris 437: if (tag != NULL)
1.114 xsa 438: (void)xsnprintf(sticky, sizeof(sticky), "T%s", tag);
1.93 niallo 439: else
1.114 xsa 440: (void)xsnprintf(sticky, sizeof(sticky), "T%s", rev);
1.109 tobias 441: else if (!reset_stickies && cf->file_ent != NULL &&
442: cf->file_ent->ce_tag != NULL)
1.114 xsa 443: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.109 tobias 444: cf->file_ent->ce_tag);
1.89 xsa 445: else
1.114 xsa 446: sticky[0] = '\0';
1.60 joris 447:
1.85 xsa 448: kbuf[0] = '\0';
449: if (cf->file_ent != NULL) {
450: if (cf->file_ent->ce_opts != NULL)
451: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
452: } else if (cf->file_rcs->rf_expand != NULL) {
453: kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
454: if (!(kflag & RCS_KWEXP_DEFAULT))
1.89 xsa 455: (void)xsnprintf(kbuf, sizeof(kbuf),
1.85 xsa 456: "-k%s", cf->file_rcs->rf_expand);
457: }
458:
1.89 xsa 459: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.114 xsa 460: cf->file_name, rev, timebuf, kbuf, sticky);
1.41 joris 461:
1.66 joris 462: if (cvs_server_active == 0) {
1.108 tobias 463: if (!(co_flags & CO_REMOVE) && cvs_cmdop != CVS_OP_EXPORT) {
1.95 joris 464: ent = cvs_ent_open(cf->file_wd);
465: cvs_ent_add(ent, entry);
466: cvs_ent_close(ent, ENT_SYNC);
467: }
1.66 joris 468: } else {
1.87 joris 469: if (co_flags & CO_MERGE) {
470: cvs_merge_file(cf, 1);
471: tosend = cf->file_path;
472: }
473:
1.78 xsa 474: if (co_flags & CO_COMMIT)
1.69 joris 475: cvs_server_update_entry("Checked-in", cf);
1.87 joris 476: else if (co_flags & CO_MERGE)
477: cvs_server_update_entry("Merged", cf);
1.95 joris 478: else if (co_flags & CO_REMOVE)
479: cvs_server_update_entry("Removed", cf);
1.69 joris 480: else
481: cvs_server_update_entry("Updated", cf);
1.68 joris 482:
1.95 joris 483: if (!(co_flags & CO_REMOVE))
484: cvs_remote_output(entry);
1.66 joris 485:
1.95 joris 486: if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87 joris 487: if (!(co_flags & CO_MERGE)) {
1.89 xsa 488: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 489: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 xsa 490:
1.87 joris 491: rcs_rev_write_stmp(cf->file_rcs, rnum,
492: template, 0);
493: tosend = template;
494: }
495:
496: cvs_remote_send_file(tosend);
497:
498: if (!(co_flags & CO_MERGE)) {
499: (void)unlink(template);
500: cvs_worklist_run(&temp_files,
501: cvs_worklist_unlink);
502: }
1.68 joris 503: }
1.66 joris 504: }
1.1 jfb 505: }