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