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