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