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