xref: /openbsd-src/usr.bin/cvs/update.c (revision 06ccd5dad87daa7ede3ae33928857a729a8043ea)
1 /*	$OpenBSD: update.c,v 1.5 2004/07/30 20:55:35 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, i, flags;
61 	struct cvs_file *cf;
62 
63 	cf = NULL;
64 	flags = CF_SORT|CF_RECURSE|CF_IGNORE;
65 
66 	while ((ch = getopt(argc, argv, "ACD:dflPpQqRr:")) != -1) {
67 		switch (ch) {
68 		case 'A':
69 		case 'C':
70 		case 'D':
71 		case 'd':
72 		case 'f':
73 			break;
74 		case 'l':
75 			flags &= ~CF_RECURSE;
76 			break;
77 		case 'P':
78 		case 'p':
79 		case 'Q':
80 		case 'q':
81 			break;
82 		case 'R':
83 			flags |= CF_RECURSE;
84 			break;
85 		case 'r':
86 			break;
87 		default:
88 			return (EX_USAGE);
89 		}
90 	}
91 
92 	argc -= optind;
93 	argv += optind;
94 
95 	if (argc == 0) {
96 		cf = cvs_file_get(".", flags);
97 	}
98 	else {
99 		/* don't perform ignore on explicitly listed files */
100 		flags &= ~CF_IGNORE;
101 
102 		for (i = 0; i < argc; i++) {
103 			cf = cvs_file_get(argv[i], flags);
104 		}
105 	}
106 
107 	cvs_file_examine(cf, cvs_update_file, NULL);
108 
109 	cvs_senddir(cf->cf_ddat->cd_root, cf);
110 	cvs_sendreq(cf->cf_ddat->cd_root, CVS_REQ_UPDATE, NULL);
111 
112 	return (0);
113 }
114 
115 
116 /*
117  * cvs_update_file()
118  *
119  * Diff a single file.
120  */
121 
122 int
123 cvs_update_file(CVSFILE *cf, void *arg)
124 {
125 	char *dir, *repo, rcspath[MAXPATHLEN];
126 	RCSFILE *rf;
127 	struct cvsroot *root;
128 	struct cvs_ent *entp;
129 
130 	if (cf->cf_type == DT_DIR) {
131 		root = cf->cf_ddat->cd_root;
132 		if ((cf->cf_parent == NULL) ||
133 		    (root != cf->cf_parent->cf_ddat->cd_root)) {
134 			cvs_connect(root);
135 		}
136 
137 		cvs_senddir(root, cf);
138 		return (0);
139 	}
140 	else
141 		root = cf->cf_parent->cf_ddat->cd_root;
142 
143 	rf = NULL;
144 	if (cf->cf_parent != NULL) {
145 		dir = cf->cf_parent->cf_path;
146 		repo = cf->cf_parent->cf_ddat->cd_repo;
147 	}
148 	else {
149 		dir = ".";
150 		repo = NULL;
151 	}
152 
153 	if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
154 		if (root->cr_method == CVS_METHOD_LOCAL)
155 			cvs_printf("? %s\n", cf->cf_path);
156 		else
157 			cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
158 		return (0);
159 	}
160 
161 	entp = cvs_ent_getent(cf->cf_path);
162 	if (entp == NULL)
163 		return (-1);
164 
165 	if ((root->cr_method != CVS_METHOD_LOCAL) &&
166 	    (cvs_sendentry(root, entp) < 0)) {
167 		cvs_ent_free(entp);
168 		return (-1);
169 	}
170 
171 	if (root->cr_method != CVS_METHOD_LOCAL) {
172 		switch (cf->cf_cvstat) {
173 		case CVS_FST_UPTODATE:
174 			cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
175 			break;
176 		case CVS_FST_ADDED:
177 		case CVS_FST_MODIFIED:
178 			cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
179 			cvs_sendfile(root, cf->cf_path);
180 			break;
181 		default:
182 			return (-1);
183 		}
184 
185 		cvs_ent_free(entp);
186 		return (0);
187 	}
188 
189 	snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
190 	    root->cr_dir, repo, cf->cf_path, RCS_FILE_EXT);
191 
192 	rf = rcs_open(rcspath, RCS_MODE_READ);
193 	if (rf == NULL) {
194 		cvs_ent_free(entp);
195 		return (-1);
196 	}
197 
198 	rcs_close(rf);
199 	cvs_ent_free(entp);
200 	return (0);
201 }
202 
203 
204 /*
205  * cvs_update_prune()
206  *
207  * Prune all directories which contain no more files known to CVS.
208  */
209 
210 int
211 cvs_update_prune(CVSFILE *cf, void *arg)
212 {
213 
214 	return (0);
215 }
216