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