xref: /openbsd-src/usr.bin/cvs/update.c (revision 4deeb87832e5d00dbfea5b08ed9c463296b435ef)
1 /*	$OpenBSD: update.c,v 1.7 2004/08/12 18:02:18 jfb Exp $	*/
2 /*
3  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 
30 #include <errno.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <sysexits.h>
37 
38 #include "cvs.h"
39 #include "rcs.h"
40 #include "log.h"
41 #include "proto.h"
42 
43 
44 int  cvs_update_file  (CVSFILE *, void *);
45 int  cvs_update_prune (CVSFILE *, void *);
46 
47 
48 
49 
50 /*
51  * cvs_update()
52  *
53  * Handle the `cvs update' command.
54  * Returns 0 on success, or the appropriate exit code on error.
55  */
56 
57 int
58 cvs_update(int argc, char **argv)
59 {
60 	int ch, flags;
61 
62 	flags = CF_SORT|CF_RECURSE|CF_IGNORE|CF_KNOWN;
63 
64 	while ((ch = getopt(argc, argv, "ACD:dflPpQqRr:")) != -1) {
65 		switch (ch) {
66 		case 'A':
67 		case 'C':
68 		case 'D':
69 		case 'd':
70 		case 'f':
71 			break;
72 		case 'l':
73 			flags &= ~CF_RECURSE;
74 			break;
75 		case 'P':
76 		case 'p':
77 		case 'Q':
78 		case 'q':
79 			break;
80 		case 'R':
81 			flags |= CF_RECURSE;
82 			break;
83 		case 'r':
84 			break;
85 		default:
86 			return (EX_USAGE);
87 		}
88 	}
89 
90 	argc -= optind;
91 	argv += optind;
92 
93 	if (argc == 0) {
94 		cvs_files = cvs_file_get(".", flags);
95 	}
96 	else {
97 		/* don't perform ignore on explicitly listed files */
98 		flags &= ~(CF_IGNORE | CF_RECURSE | CF_SORT);
99 		cvs_files = cvs_file_getspec(argv, argc, flags);
100 	}
101 	if (cvs_files == NULL)
102 		return (EX_DATAERR);
103 
104 	cvs_file_examine(cvs_files, cvs_update_file, NULL);
105 
106 	cvs_senddir(cvs_files->cf_ddat->cd_root, cvs_files);
107 	cvs_sendreq(cvs_files->cf_ddat->cd_root, CVS_REQ_UPDATE, NULL);
108 
109 	return (0);
110 }
111 
112 
113 /*
114  * cvs_update_file()
115  *
116  * Diff a single file.
117  */
118 
119 int
120 cvs_update_file(CVSFILE *cf, void *arg)
121 {
122 	char *dir, *repo, rcspath[MAXPATHLEN];
123 	RCSFILE *rf;
124 	struct cvsroot *root;
125 	struct cvs_ent *entp;
126 
127 	if (cf->cf_type == DT_DIR) {
128 		if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
129 			root = cf->cf_parent->cf_ddat->cd_root;
130 			cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
131 		}
132 		else {
133 			root = cf->cf_ddat->cd_root;
134 			if ((cf->cf_parent == NULL) ||
135 			    (root != cf->cf_parent->cf_ddat->cd_root)) {
136 				cvs_connect(root);
137 			}
138 
139 			cvs_senddir(root, cf);
140 		}
141 
142 		return (0);
143 	}
144 	else
145 		root = cf->cf_parent->cf_ddat->cd_root;
146 
147 	rf = NULL;
148 	if (cf->cf_parent != NULL) {
149 		dir = cf->cf_parent->cf_path;
150 		repo = cf->cf_parent->cf_ddat->cd_repo;
151 	}
152 	else {
153 		dir = ".";
154 		repo = NULL;
155 	}
156 
157 	if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
158 		if (root->cr_method == CVS_METHOD_LOCAL)
159 			cvs_printf("? %s\n", cf->cf_path);
160 		else
161 			cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
162 		return (0);
163 	}
164 
165 	entp = cvs_ent_getent(cf->cf_path);
166 	if (entp == NULL)
167 		return (-1);
168 
169 	if ((root->cr_method != CVS_METHOD_LOCAL) &&
170 	    (cvs_sendentry(root, entp) < 0)) {
171 		cvs_ent_free(entp);
172 		return (-1);
173 	}
174 
175 	if (root->cr_method != CVS_METHOD_LOCAL) {
176 		switch (cf->cf_cvstat) {
177 		case CVS_FST_UPTODATE:
178 			cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
179 			break;
180 		case CVS_FST_ADDED:
181 		case CVS_FST_MODIFIED:
182 			cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
183 			cvs_sendfile(root, cf->cf_path);
184 			break;
185 		default:
186 			return (-1);
187 		}
188 
189 		cvs_ent_free(entp);
190 		return (0);
191 	}
192 
193 	snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
194 	    root->cr_dir, repo, cf->cf_path, RCS_FILE_EXT);
195 
196 	rf = rcs_open(rcspath, RCS_MODE_READ);
197 	if (rf == NULL) {
198 		cvs_ent_free(entp);
199 		return (-1);
200 	}
201 
202 	rcs_close(rf);
203 	cvs_ent_free(entp);
204 	return (0);
205 }
206 
207 
208 /*
209  * cvs_update_prune()
210  *
211  * Prune all directories which contain no more files known to CVS.
212  */
213 
214 int
215 cvs_update_prune(CVSFILE *cf, void *arg)
216 {
217 
218 	return (0);
219 }
220