xref: /openbsd-src/usr.bin/cvs/checkout.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: checkout.c,v 1.168 2011/12/27 13:59:01 nicm Exp $	*/
2 /*
3  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4  *
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.
8  *
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.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/dirent.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <libgen.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 #include "cvs.h"
32 #include "diff.h"
33 #include "remote.h"
34 
35 static void checkout_check_repository(int, char **);
36 static int checkout_classify(const char *, const char *);
37 static void checkout_repository(const char *, const char *);
38 
39 extern int print_stdout;
40 extern int prune_dirs;
41 extern int build_dirs;
42 
43 static int flags = CR_REPO | CR_RECURSE_DIRS;
44 static int Aflag = 0;
45 static char *dflag = NULL;
46 static char *koptstr = NULL;
47 static char *dateflag = NULL;
48 
49 static int nflag = 0;
50 
51 char *checkout_target_dir = NULL;
52 
53 time_t cvs_specified_date = -1;
54 time_t cvs_directory_date = -1;
55 int disable_fast_checkout = 0;
56 
57 struct cvs_cmd cvs_cmd_checkout = {
58 	CVS_OP_CHECKOUT, CVS_USE_WDIR, "checkout",
59 	{ "co", "get" },
60 	"Checkout a working copy of a repository",
61 	"[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
62 	"[-t id] module ...",
63 	"AcD:d:fj:k:lNnPpRr:st:",
64 	NULL,
65 	cvs_checkout
66 };
67 
68 struct cvs_cmd cvs_cmd_export = {
69 	CVS_OP_EXPORT, CVS_USE_WDIR, "export",
70 	{ "exp", "ex" },
71 	"Export sources from CVS, similar to checkout",
72 	"[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
73 	"D:d:k:flNnRr:",
74 	NULL,
75 	cvs_export
76 };
77 
78 int
79 cvs_checkout(int argc, char **argv)
80 {
81 	int ch;
82 
83 	while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
84 		switch (ch) {
85 		case 'A':
86 			Aflag = 1;
87 			if (koptstr == NULL)
88 				reset_option = 1;
89 			if (cvs_specified_tag == NULL)
90 				reset_tag = 1;
91 			break;
92 		case 'c':
93 			cvs_modules_list();
94 			exit(0);
95 		case 'D':
96 			dateflag = optarg;
97 			if ((cvs_specified_date = date_parse(dateflag)) == -1)
98 				fatal("invalid date: %s", dateflag);
99 			reset_tag = 0;
100 			break;
101 		case 'd':
102 			if (dflag != NULL)
103 				fatal("-d specified two or more times");
104 			dflag = optarg;
105 			checkout_target_dir = dflag;
106 
107 			if (cvs_server_active == 1)
108 				disable_fast_checkout = 1;
109 			break;
110 		case 'j':
111 			if (cvs_join_rev1 == NULL)
112 				cvs_join_rev1 = optarg;
113 			else if (cvs_join_rev2 == NULL)
114 				cvs_join_rev2 = optarg;
115 			else
116 				fatal("too many -j options");
117 			break;
118 		case 'k':
119 			reset_option = 0;
120 			koptstr = optarg;
121 			kflag = rcs_kflag_get(koptstr);
122 			if (RCS_KWEXP_INVAL(kflag)) {
123 				cvs_log(LP_ERR,
124 				    "invalid RCS keyword expansion mode");
125 				fatal("%s", cvs_cmd_checkout.cmd_synopsis);
126 			}
127 			break;
128 		case 'l':
129 			flags &= ~CR_RECURSE_DIRS;
130 			break;
131 		case 'N':
132 			break;
133 		case 'n':
134 			nflag = 1;
135 			break;
136 		case 'P':
137 			prune_dirs = 1;
138 			break;
139 		case 'p':
140 			cmdp->cmd_flags &= ~CVS_USE_WDIR;
141 			print_stdout = 1;
142 			cvs_noexec = 1;
143 			nflag = 1;
144 			break;
145 		case 'R':
146 			flags |= CR_RECURSE_DIRS;
147 			break;
148 		case 'r':
149 			reset_tag = 0;
150 			cvs_specified_tag = optarg;
151 			break;
152 		default:
153 			fatal("%s", cvs_cmd_checkout.cmd_synopsis);
154 		}
155 	}
156 
157 	argc -= optind;
158 	argv += optind;
159 
160 	if (argc == 0)
161 		fatal("%s", cvs_cmd_checkout.cmd_synopsis);
162 
163 	if (cvs_server_active == 1 && disable_fast_checkout != 1) {
164 		cmdp->cmd_flags &= ~CVS_USE_WDIR;
165 		cvs_noexec = 1;
166 	}
167 
168 	checkout_check_repository(argc, argv);
169 
170 	if (cvs_server_active == 1 && disable_fast_checkout != 1)
171 		cvs_noexec = 0;
172 
173 	return (0);
174 }
175 
176 int
177 cvs_export(int argc, char **argv)
178 {
179 	int ch;
180 
181 	prune_dirs = 1;
182 
183 	while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
184 		switch (ch) {
185 		case 'd':
186 			if (dflag != NULL)
187 				fatal("-d specified two or more times");
188 			dflag = optarg;
189 			checkout_target_dir = dflag;
190 
191 			if (cvs_server_active == 1)
192 				disable_fast_checkout = 1;
193 			break;
194 		case 'k':
195 			koptstr = optarg;
196 			kflag = rcs_kflag_get(koptstr);
197 			if (RCS_KWEXP_INVAL(kflag)) {
198 				cvs_log(LP_ERR,
199 				    "invalid RCS keyword expansion mode");
200 				fatal("%s", cvs_cmd_export.cmd_synopsis);
201 			}
202 			break;
203 		case 'l':
204 			flags &= ~CR_RECURSE_DIRS;
205 			break;
206 		case 'N':
207 			break;
208 		case 'R':
209 			flags |= CR_RECURSE_DIRS;
210 			break;
211 		case 'r':
212 			cvs_specified_tag = optarg;
213 			break;
214 		default:
215 			fatal("%s", cvs_cmd_export.cmd_synopsis);
216 		}
217 	}
218 
219 	argc -= optind;
220 	argv += optind;
221 
222 	if (cvs_specified_tag == NULL)
223 		fatal("must specify a tag or date");
224 
225 	if (argc == 0)
226 		fatal("%s", cvs_cmd_export.cmd_synopsis);
227 
228 	checkout_check_repository(argc, argv);
229 
230 	return (0);
231 }
232 
233 static void
234 checkout_check_repository(int argc, char **argv)
235 {
236 	int i;
237 	char *wdir, *d;
238 	struct cvs_recursion cr;
239 	struct module_checkout *mc;
240 	struct cvs_ignpat *ip;
241 	struct cvs_filelist *fl, *nxt;
242 	char repo[MAXPATHLEN], fpath[MAXPATHLEN], *f[1];
243 
244 	build_dirs = print_stdout ? 0 : 1;
245 
246 	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
247 		cvs_client_connect_to_server();
248 
249 		if (cvs_specified_tag != NULL)
250 			cvs_client_send_request("Argument -r%s",
251 			    cvs_specified_tag);
252 		if (Aflag)
253 			cvs_client_send_request("Argument -A");
254 
255 		if (dateflag != NULL)
256 			cvs_client_send_request("Argument -D%s", dateflag);
257 
258 		if (kflag)
259 			cvs_client_send_request("Argument -k%s", koptstr);
260 
261 		if (dflag != NULL)
262 			cvs_client_send_request("Argument -d%s", dflag);
263 
264 		if (!(flags & CR_RECURSE_DIRS))
265 			cvs_client_send_request("Argument -l");
266 
267 		if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
268 			cvs_client_send_request("Argument -P");
269 
270 		if (print_stdout == 1)
271 			cvs_client_send_request("Argument -p");
272 
273 		if (nflag == 1)
274 			cvs_client_send_request("Argument -n");
275 
276 		cr.enterdir = NULL;
277 		cr.leavedir = NULL;
278 		if (print_stdout)
279 			cr.fileproc = NULL;
280 		else
281 			cr.fileproc = cvs_client_sendfile;
282 
283 		flags &= ~CR_REPO;
284 		cr.flags = flags;
285 
286 		if (cvs_cmdop != CVS_OP_EXPORT)
287 			cvs_file_run(argc, argv, &cr);
288 
289 		cvs_client_send_files(argv, argc);
290 		cvs_client_senddir(".");
291 
292 		cvs_client_send_request("%s",
293 		    (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
294 
295 		cvs_client_get_responses();
296 
297 		return;
298 	}
299 
300 	for (i = 0; i < argc; i++) {
301 		mc = cvs_module_lookup(argv[i]);
302 		current_module = mc;
303 
304 		RB_FOREACH(fl, cvs_flisthead, &(mc->mc_ignores))
305 			cvs_file_ignore(fl->file_path, &checkout_ign_pats);
306 
307 		RB_FOREACH(fl, cvs_flisthead, &(mc->mc_modules)) {
308 			module_repo_root = NULL;
309 
310 			(void)xsnprintf(repo, sizeof(repo), "%s/%s",
311 			    current_cvsroot->cr_dir, fl->file_path);
312 
313 			if (!(mc->mc_flags & MODULE_ALIAS) || dflag != NULL)
314 				module_repo_root = xstrdup(fl->file_path);
315 
316 			if (mc->mc_flags & MODULE_NORECURSE)
317 				flags &= ~CR_RECURSE_DIRS;
318 
319 			if (dflag != NULL)
320 				wdir = dflag;
321 			else if (mc->mc_flags & MODULE_ALIAS)
322 				wdir = fl->file_path;
323 			else
324 				wdir = mc->mc_name;
325 
326 			switch (checkout_classify(repo, fl->file_path)) {
327 			case CVS_FILE:
328 				cr.fileproc = cvs_update_local;
329 				cr.flags = flags;
330 
331 				if (!(mc->mc_flags & MODULE_ALIAS)) {
332 					module_repo_root =
333 					    xstrdup(dirname(fl->file_path));
334 					d = wdir;
335 					(void)xsnprintf(fpath, sizeof(fpath),
336 					    "%s/%s", d,
337 					    basename(fl->file_path));
338 				} else {
339 					d = dirname(wdir);
340 					strlcpy(fpath, fl->file_path,
341 					    sizeof(fpath));
342 				}
343 
344 				if (build_dirs == 1)
345 					cvs_mkpath(d, cvs_specified_tag);
346 
347 				f[0] = fpath;
348 				cvs_file_run(1, f, &cr);
349 				break;
350 			case CVS_DIR:
351 				if (build_dirs == 1)
352 					cvs_mkpath(wdir, cvs_specified_tag);
353 				checkout_repository(repo, wdir);
354 				break;
355 			default:
356 				break;
357 			}
358 
359 			if (nflag != 1 && mc->mc_prog != NULL &&
360 			    mc->mc_flags & MODULE_RUN_ON_CHECKOUT)
361 				cvs_exec(mc->mc_prog, NULL, 0);
362 
363 			if (module_repo_root != NULL)
364 				xfree(module_repo_root);
365 		}
366 
367 		if (mc->mc_canfree == 1) {
368 			for (fl = RB_MIN(cvs_flisthead, &(mc->mc_modules));
369 			    fl != NULL; fl = nxt) {
370 				nxt = RB_NEXT(cvs_flisthead,
371 				    &(mc->mc_modules), fl);
372 				RB_REMOVE(cvs_flisthead,
373 				    &(mc->mc_modules), fl);
374 				xfree(fl->file_path);
375 				xfree(fl);
376 			}
377 		}
378 
379 		while ((ip = TAILQ_FIRST(&checkout_ign_pats)) != NULL) {
380 			TAILQ_REMOVE(&checkout_ign_pats, ip, ip_list);
381 			xfree(ip);
382 		}
383 
384 		xfree(mc);
385 	}
386 }
387 
388 static int
389 checkout_classify(const char *repo, const char *arg)
390 {
391 	char *d, *f, fpath[MAXPATHLEN];
392 	struct stat sb;
393 
394 	if (stat(repo, &sb) == 0) {
395 		if (S_ISDIR(sb.st_mode))
396 			return CVS_DIR;
397 	}
398 
399 	d = dirname(repo);
400 	f = basename(repo);
401 
402 	(void)xsnprintf(fpath, sizeof(fpath), "%s/%s%s", d, f, RCS_FILE_EXT);
403 	if (stat(fpath, &sb) == 0) {
404 		if (!S_ISREG(sb.st_mode)) {
405 			cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
406 			return 0;
407 		}
408 		return CVS_FILE;
409 	}
410 
411 	(void)xsnprintf(fpath, sizeof(fpath), "%s/%s/%s%s",
412 	    d, CVS_PATH_ATTIC, f, RCS_FILE_EXT);
413 	if (stat(fpath, &sb) == 0) {
414 		if (!S_ISREG(sb.st_mode)) {
415 			cvs_log(LP_ERR, "ignoring %s: not a regular file", arg);
416 			return 0;
417 		}
418 		return CVS_FILE;
419 	}
420 
421 	cvs_log(LP_ERR, "cannot find module `%s' - ignored", arg);
422 	return 0;
423 }
424 
425 static void
426 checkout_repository(const char *repobase, const char *wdbase)
427 {
428 	struct cvs_flisthead fl, dl;
429 	struct cvs_recursion cr;
430 
431 	RB_INIT(&fl);
432 	RB_INIT(&dl);
433 
434 	cvs_history_add((cvs_cmdop == CVS_OP_CHECKOUT) ?
435 	    CVS_HISTORY_CHECKOUT : CVS_HISTORY_EXPORT, NULL, wdbase);
436 
437 	if (print_stdout) {
438 		cr.enterdir = NULL;
439 		cr.leavedir = NULL;
440 	} else {
441 		cr.enterdir = cvs_update_enterdir;
442 		if (cvs_server_active == 1) {
443 			if (disable_fast_checkout != 1)
444 				cr.leavedir = NULL;
445 			else
446 				cr.leavedir = cvs_update_leavedir;
447 		} else {
448 			cr.leavedir = prune_dirs ? cvs_update_leavedir : NULL;
449 		}
450 	}
451 	cr.fileproc = cvs_update_local;
452 	cr.flags = flags;
453 
454 	cvs_repository_lock(repobase, 0);
455 	cvs_repository_getdir(repobase, wdbase, &fl, &dl,
456 	    flags & CR_RECURSE_DIRS ? REPOSITORY_DODIRS : 0);
457 
458 	cvs_file_walklist(&fl, &cr);
459 	cvs_file_freelist(&fl);
460 
461 	cvs_repository_unlock(repobase);
462 
463 	cvs_file_walklist(&dl, &cr);
464 	cvs_file_freelist(&dl);
465 }
466 
467 void
468 cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, char *tag, int co_flags)
469 {
470 	BUF *bp;
471 	mode_t mode;
472 	int cf_kflag, exists;
473 	time_t rcstime;
474 	CVSENTRIES *ent;
475 	struct timeval tv[2];
476 	struct tm datetm;
477 	char *entry, *tosend;
478 	char kbuf[8], sticky[CVS_REV_BUFSZ], rev[CVS_REV_BUFSZ];
479 	char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
480 	static char lastwd[MAXPATHLEN];
481 
482 	exists = 0;
483 	tosend = NULL;
484 
485 	if (!(co_flags & CO_REMOVE))
486 		rcsnum_tostr(rnum, rev, sizeof(rev));
487 	else
488 		rev[0] = '\0';
489 
490 	cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
491 	    cf->file_path, rev, co_flags,
492 	    (cvs_server_active) ? "to client" : "to disk");
493 
494 	if (co_flags & CO_DUMP) {
495 		rcs_rev_write_fd(cf->file_rcs, rnum, STDOUT_FILENO, 0);
496 		return;
497 	}
498 
499 	if (cvs_server_active == 0) {
500 		(void)unlink(cf->file_path);
501 
502 		if (!(co_flags & CO_MERGE)) {
503 			if (cf->file_flags & FILE_ON_DISK) {
504 				exists = 1;
505 				(void)close(cf->fd);
506 			}
507 
508 			cf->fd = open(cf->file_path,
509 			    O_CREAT | O_RDWR | O_TRUNC);
510 			if (cf->fd == -1)
511 				fatal("cvs_checkout_file: open: %s",
512 				    strerror(errno));
513 
514 			rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 0);
515 			cf->file_flags |= FILE_ON_DISK;
516 		} else {
517 			cvs_merge_file(cf, (cvs_join_rev1 == NULL));
518 		}
519 
520 		mode = cf->file_rcs->rf_mode;
521 		mode |= S_IWUSR;
522 
523 		if (fchmod(cf->fd, mode) == -1)
524 			fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
525 
526 		if ((exists == 0) && (cf->file_ent == NULL) &&
527 		    !(co_flags & CO_MERGE))
528 			rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
529 		else
530 			time(&rcstime);
531 
532 		tv[0].tv_sec = rcstime;
533 		tv[0].tv_usec = 0;
534 		tv[1] = tv[0];
535 		if (futimes(cf->fd, tv) == -1)
536 			fatal("cvs_checkout_file: futimes: %s",
537 			    strerror(errno));
538 	} else {
539 		time(&rcstime);
540 	}
541 
542 	gmtime_r(&rcstime, &datetm);
543 	asctime_r(&datetm, tbuf);
544 	tbuf[strcspn(tbuf, "\n")] = '\0';
545 
546 	if (co_flags & CO_MERGE) {
547 		(void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
548 		    tbuf);
549 	} else {
550 		strlcpy(timebuf, tbuf, sizeof(timebuf));
551 	}
552 
553 	if (reset_tag) {
554 		sticky[0] = '\0';
555 	} else if (co_flags & CO_SETSTICKY)
556 		if (tag != NULL)
557 			(void)xsnprintf(sticky, sizeof(sticky), "T%s", tag);
558 		else if (cvs_specified_date != -1) {
559 			gmtime_r(&cvs_specified_date, &datetm);
560 			(void)strftime(sticky, sizeof(sticky),
561 			    "D"CVS_DATE_FMT, &datetm);
562 		} else if (cvs_directory_date != -1) {
563 			gmtime_r(&cvs_directory_date, &datetm);
564 			(void)strftime(sticky, sizeof(sticky),
565 			    "D"CVS_DATE_FMT, &datetm);
566 		} else
567 			(void)xsnprintf(sticky, sizeof(sticky), "T%s", rev);
568 	else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL)
569 		(void)xsnprintf(sticky, sizeof(sticky), "T%s",
570 		    cf->file_ent->ce_tag);
571 	else
572 		sticky[0] = '\0';
573 
574 	kbuf[0] = '\0';
575 	if (cf->file_rcs != NULL && cf->file_rcs->rf_expand != NULL) {
576 		cf_kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
577 		if (kflag || cf_kflag != RCS_KWEXP_DEFAULT)
578 			(void)xsnprintf(kbuf, sizeof(kbuf),
579 			    "-k%s", cf->file_rcs->rf_expand);
580 	} else if (!reset_option && cf->file_ent != NULL) {
581 		if (cf->file_ent->ce_opts != NULL)
582 			strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
583 	}
584 
585 	entry = xmalloc(CVS_ENT_MAXLINELEN);
586 	cvs_ent_line_str(cf->file_name, rev, timebuf, kbuf, sticky, 0, 0,
587 	    entry, CVS_ENT_MAXLINELEN);
588 
589 	if (cvs_server_active == 0) {
590 		if (!(co_flags & CO_REMOVE) && cvs_cmdop != CVS_OP_EXPORT) {
591 			ent = cvs_ent_open(cf->file_wd);
592 			cvs_ent_add(ent, entry);
593 			cf->file_ent = cvs_ent_parse(entry);
594 		}
595 	} else {
596 		if (co_flags & CO_MERGE) {
597 			(void)unlink(cf->file_path);
598 			cvs_merge_file(cf, (cvs_join_rev1 == NULL));
599 			tosend = cf->file_path;
600 		}
601 
602 		/*
603 		 * If this file has a tag, push out the Directory with the
604 		 * tag to the client. Except when this file was explicitly
605 		 * specified on the command line.
606 		 */
607 		if (tag != NULL && strcmp(cf->file_wd, lastwd) &&
608 		    !(cf->file_flags & FILE_USER_SUPPLIED)) {
609 			strlcpy(lastwd, cf->file_wd, MAXPATHLEN);
610 			cvs_server_set_sticky(cf->file_wd, sticky);
611 		}
612 
613 		if (co_flags & CO_COMMIT)
614 			cvs_server_update_entry("Updated", cf);
615 		else if (co_flags & CO_MERGE)
616 			cvs_server_update_entry("Merged", cf);
617 		else if (co_flags & CO_REMOVE)
618 			cvs_server_update_entry("Removed", cf);
619 		else
620 			cvs_server_update_entry("Updated", cf);
621 
622 		if (!(co_flags & CO_REMOVE)) {
623 			cvs_remote_output(entry);
624 
625 			if (!(co_flags & CO_MERGE)) {
626 				mode = cf->file_rcs->rf_mode;
627 				mode |= S_IWUSR;
628 				bp = rcs_rev_getbuf(cf->file_rcs, rnum, 0);
629 				cvs_remote_send_file_buf(cf->file_path,
630 				    bp, mode);
631 			} else {
632 				cvs_remote_send_file(tosend, cf->fd);
633 			}
634 		}
635 	}
636 
637 	xfree(entry);
638 }
639