Annotation of src/usr.bin/cvs/checkout.c, Revision 1.110
1.110 ! tobias 1: /* $OpenBSD: checkout.c,v 1.109 2008/01/23 09:34:08 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.56 joris 246: cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1 jfb 247:
1.53 joris 248: cvs_file_walklist(&fl, &cr);
249: cvs_file_freelist(&fl);
1.23 joris 250:
1.53 joris 251: cvs_repository_unlock(repobase);
1.23 joris 252:
1.53 joris 253: cvs_file_walklist(&dl, &cr);
254: cvs_file_freelist(&dl);
255: }
1.23 joris 256:
1.58 joris 257: void
1.105 joris 258: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, char *tag, int co_flags)
1.53 joris 259: {
1.89 xsa 260: int kflag, oflags, exists;
1.53 joris 261: time_t rcstime;
262: CVSENTRIES *ent;
263: struct timeval tv[2];
1.87 joris 264: char *tosend;
1.97 joris 265: char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.96 xsa 266: char kbuf[8], stickytag[32], rev[CVS_REV_BUFSZ];
267: char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22 jfb 268:
1.87 joris 269: exists = 0;
270: tosend = NULL;
1.104 joris 271:
272: if (!(co_flags & CO_REMOVE))
273: rcsnum_tostr(rnum, rev, sizeof(rev));
1.41 joris 274:
1.66 joris 275: cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78 xsa 276: cf->file_path, rev, co_flags,
1.66 joris 277: (cvs_server_active) ? "to client" : "to disk");
1.42 joris 278:
1.78 xsa 279: if (co_flags & CO_DUMP) {
1.106 tobias 280: rcs_rev_write_fd(cf->file_rcs, rnum, STDOUT_FILENO, 0);
1.65 reyk 281: return;
282: }
1.87 joris 283:
1.66 joris 284: if (cvs_server_active == 0) {
1.87 joris 285: if (!(co_flags & CO_MERGE)) {
286: oflags = O_WRONLY | O_TRUNC;
287: if (cf->fd != -1) {
288: exists = 1;
289: (void)close(cf->fd);
290: } else {
291: oflags |= O_CREAT;
292: }
293:
294: cf->fd = open(cf->file_path, oflags);
295: if (cf->fd == -1)
296: fatal("cvs_checkout_file: open: %s",
297: strerror(errno));
298:
1.106 tobias 299: rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 0);
1.87 joris 300: } else {
301: cvs_merge_file(cf, 1);
1.66 joris 302: }
303:
304: if (fchmod(cf->fd, 0644) == -1)
305: fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
306:
1.87 joris 307: if ((exists == 0) && (cf->file_ent == NULL) &&
308: !(co_flags & CO_MERGE))
1.66 joris 309: rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84 joris 310: else
1.66 joris 311: time(&rcstime);
312:
313: tv[0].tv_sec = rcstime;
314: tv[0].tv_usec = 0;
315: tv[1] = tv[0];
316: if (futimes(cf->fd, tv) == -1)
317: fatal("cvs_checkout_file: futimes: %s",
318: strerror(errno));
1.53 joris 319: } else {
320: time(&rcstime);
1.41 joris 321: }
322:
1.84 joris 323: asctime_r(gmtime(&rcstime), tbuf);
1.107 tobias 324: tbuf[strcspn(tbuf, "\n")] = '\0';
1.58 joris 325:
1.78 xsa 326: if (co_flags & CO_MERGE) {
1.89 xsa 327: (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58 joris 328: tbuf);
329: } else {
330: strlcpy(timebuf, tbuf, sizeof(timebuf));
331: }
1.41 joris 332:
1.89 xsa 333: if (co_flags & CO_SETSTICKY)
1.105 joris 334: if (tag != NULL)
1.93 niallo 335: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
1.105 joris 336: tag);
1.93 niallo 337: else
338: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
339: rev);
1.109 tobias 340: else if (!reset_stickies && cf->file_ent != NULL &&
341: cf->file_ent->ce_tag != NULL)
342: (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
343: cf->file_ent->ce_tag);
1.89 xsa 344: else
1.60 joris 345: stickytag[0] = '\0';
346:
1.85 xsa 347: kbuf[0] = '\0';
348: if (cf->file_ent != NULL) {
349: if (cf->file_ent->ce_opts != NULL)
350: strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
351: } else if (cf->file_rcs->rf_expand != NULL) {
352: kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
353: if (!(kflag & RCS_KWEXP_DEFAULT))
1.89 xsa 354: (void)xsnprintf(kbuf, sizeof(kbuf),
1.85 xsa 355: "-k%s", cf->file_rcs->rf_expand);
356: }
357:
1.89 xsa 358: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.85 xsa 359: cf->file_name, rev, timebuf, kbuf, stickytag);
1.41 joris 360:
1.66 joris 361: if (cvs_server_active == 0) {
1.108 tobias 362: if (!(co_flags & CO_REMOVE) && cvs_cmdop != CVS_OP_EXPORT) {
1.95 joris 363: ent = cvs_ent_open(cf->file_wd);
364: cvs_ent_add(ent, entry);
365: cvs_ent_close(ent, ENT_SYNC);
366: }
1.66 joris 367: } else {
1.87 joris 368: if (co_flags & CO_MERGE) {
369: cvs_merge_file(cf, 1);
370: tosend = cf->file_path;
371: }
372:
1.78 xsa 373: if (co_flags & CO_COMMIT)
1.69 joris 374: cvs_server_update_entry("Checked-in", cf);
1.87 joris 375: else if (co_flags & CO_MERGE)
376: cvs_server_update_entry("Merged", cf);
1.95 joris 377: else if (co_flags & CO_REMOVE)
378: cvs_server_update_entry("Removed", cf);
1.69 joris 379: else
380: cvs_server_update_entry("Updated", cf);
1.68 joris 381:
1.95 joris 382: if (!(co_flags & CO_REMOVE))
383: cvs_remote_output(entry);
1.66 joris 384:
1.95 joris 385: if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87 joris 386: if (!(co_flags & CO_MERGE)) {
1.89 xsa 387: (void)xsnprintf(template, MAXPATHLEN,
1.87 joris 388: "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89 xsa 389:
1.87 joris 390: rcs_rev_write_stmp(cf->file_rcs, rnum,
391: template, 0);
392: tosend = template;
393: }
394:
395: cvs_remote_send_file(tosend);
396:
397: if (!(co_flags & CO_MERGE)) {
398: (void)unlink(template);
399: cvs_worklist_run(&temp_files,
400: cvs_worklist_unlink);
401: }
1.68 joris 402: }
1.66 joris 403: }
1.1 jfb 404: }