1 /*-
2  * Copyright (c) 2000-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 
33 #include <stdlib.h>
34 
35 #include <limits.h>
36 #include <pthread.h>
37 
38 #include "compat_stdbool.h"
39 #include "compat_stdint.h"
40 #include "compat_inttypes.h"
41 #include "compat_limits.h"
42 #include "basedef.h"
43 
44 #include "attribute.h"
45 #include "collection.h"
46 #include "cvsync.h"
47 #include "cvsync_attr.h"
48 #include "filetypes.h"
49 #include "hash.h"
50 #include "logmsg.h"
51 #include "scanfile.h"
52 
53 #include "updater.h"
54 
55 bool
updater_rcs_scanfile_attic(struct updater_args * uda)56 updater_rcs_scanfile_attic(struct updater_args *uda)
57 {
58 	const char *cmdmsg, *dirmsg;
59 	struct cvsync_attr *cap = &uda->uda_attr;
60 	struct scanfile_args *sa = uda->uda_scanfile;
61 	size_t auxmax = sizeof(cap->ca_aux), auxlen;
62 
63 	if (sa == NULL)
64 		goto done;
65 
66 	if ((auxlen = attr_rcs_encode_rcs(cap->ca_aux, auxmax, cap->ca_mtime,
67 					  cap->ca_mode)) == 0) {
68 		return (false);
69 	}
70 
71 	if (!scanfile_replace(sa, cap->ca_type, cap->ca_name, cap->ca_namelen,
72 			      cap->ca_aux, auxlen)) {
73 		return (false);
74 	}
75 
76 done:
77 	switch (cap->ca_tag) {
78 	case UPDATER_UPDATE_GENERIC:
79 	case UPDATER_UPDATE_RDIFF:
80 		cmdmsg = "Update";
81 		break;
82 	case UPDATER_UPDATE_RCS:
83 		cmdmsg = "Edit";
84 		break;
85 	default:
86 		return (false);
87 	}
88 
89 	if (cap->ca_type == FILETYPE_RCS)
90 		dirmsg = "<- Attic";
91 	else
92 		dirmsg = "-> Attic";
93 
94 	logmsg(" %s %.*s %s", cmdmsg, cap->ca_namelen, cap->ca_name, dirmsg);
95 
96 	return (true);
97 }
98 
99 bool
updater_rcs_scanfile_create(struct updater_args * uda)100 updater_rcs_scanfile_create(struct updater_args *uda)
101 {
102 	struct cvsync_attr *cap = &uda->uda_attr;
103 	struct scanfile_args *sa = uda->uda_scanfile;
104 	size_t auxmax = sizeof(cap->ca_aux), auxlen;
105 
106 	if (sa == NULL)
107 		goto done;
108 
109 	switch (cap->ca_type) {
110 	case FILETYPE_FILE:
111 		if ((auxlen = attr_rcs_encode_file(cap->ca_aux, auxmax,
112 						   cap->ca_mtime, cap->ca_size,
113 						   cap->ca_mode)) == 0) {
114 			return (false);
115 		}
116 		break;
117 	case FILETYPE_RCS:
118 	case FILETYPE_RCS_ATTIC:
119 		if ((auxlen = attr_rcs_encode_rcs(cap->ca_aux, auxmax,
120 						  cap->ca_mtime,
121 						  cap->ca_mode)) == 0) {
122 			return (false);
123 		}
124 		break;
125 	case FILETYPE_SYMLINK:
126 		auxlen = cap->ca_auxlen;
127 		break;
128 	default:
129 		return (false);
130 	}
131 
132 	if (!scanfile_add(sa, cap->ca_type, cap->ca_name, cap->ca_namelen,
133 			  cap->ca_aux, auxlen)) {
134 		return (false);
135 	}
136 
137 done:
138 	switch (cap->ca_type) {
139 	case FILETYPE_RCS_ATTIC:
140 		logmsg(" Create %.*s -> Attic", cap->ca_namelen, cap->ca_name);
141 		break;
142 	case FILETYPE_SYMLINK:
143 		logmsg(" Symlink %.*s -> %.*s", cap->ca_namelen, cap->ca_name,
144 		       cap->ca_auxlen, cap->ca_aux);
145 		break;
146 	default:
147 		logmsg(" Create %.*s", cap->ca_namelen, cap->ca_name);
148 		break;
149 	}
150 
151 	return (true);
152 }
153 
154 bool
updater_rcs_scanfile_mkdir(struct updater_args * uda)155 updater_rcs_scanfile_mkdir(struct updater_args *uda)
156 {
157 	struct cvsync_attr *cap = &uda->uda_attr;
158 	struct scanfile_args *sa = uda->uda_scanfile;
159 	size_t auxmax = sizeof(cap->ca_aux), auxlen;
160 
161 	if (sa == NULL)
162 		goto done;
163 
164 	if (cap->ca_type != FILETYPE_DIR)
165 		return (false);
166 
167 	if ((auxlen = attr_rcs_encode_dir(cap->ca_aux, auxmax,
168 					  cap->ca_mode)) == 0) {
169 		return (false);
170 	}
171 
172 	if (!scanfile_add(sa, cap->ca_type, cap->ca_name, cap->ca_namelen,
173 			  cap->ca_aux, auxlen)) {
174 		return (false);
175 	}
176 
177 done:
178 	logmsg(" Mkdir %.*s", cap->ca_namelen, cap->ca_name);
179 
180 	return (true);
181 }
182 
183 bool
updater_rcs_scanfile_remove(struct updater_args * uda)184 updater_rcs_scanfile_remove(struct updater_args *uda)
185 {
186 	struct cvsync_attr *cap = &uda->uda_attr;
187 	struct scanfile_args *sa = uda->uda_scanfile;
188 
189 	if (sa == NULL)
190 		goto done;
191 
192 	if (!scanfile_remove(sa, cap->ca_type, cap->ca_name, cap->ca_namelen))
193 		return (false);
194 
195 done:
196 	if (cap->ca_type != FILETYPE_RCS_ATTIC)
197 		logmsg(" Remove %.*s", cap->ca_namelen, cap->ca_name);
198 	else
199 		logmsg(" Remove %.*s in Attic", cap->ca_namelen, cap->ca_name);
200 
201 	return (true);
202 }
203 
204 bool
updater_rcs_scanfile_setattr(struct updater_args * uda)205 updater_rcs_scanfile_setattr(struct updater_args *uda)
206 {
207 	struct cvsync_attr *cap = &uda->uda_attr;
208 	struct scanfile_args *sa = uda->uda_scanfile;
209 	size_t auxmax = sizeof(cap->ca_aux), auxlen;
210 
211 	if (sa == NULL)
212 		goto done;
213 
214 	switch (cap->ca_type) {
215 	case FILETYPE_DIR:
216 		if ((auxlen = attr_rcs_encode_dir(cap->ca_aux, auxmax,
217 						  cap->ca_mode)) == 0) {
218 			return (false);
219 		}
220 		break;
221 	case FILETYPE_FILE:
222 		if ((auxlen = attr_rcs_encode_file(cap->ca_aux, auxmax,
223 						   cap->ca_mtime, cap->ca_size,
224 						   cap->ca_mode)) == 0) {
225 			return (false);
226 		}
227 		break;
228 	case FILETYPE_RCS:
229 	case FILETYPE_RCS_ATTIC:
230 		if ((auxlen = attr_rcs_encode_rcs(cap->ca_aux, auxmax,
231 						  cap->ca_mtime,
232 						  cap->ca_mode)) == 0) {
233 			return (false);
234 		}
235 		break;
236 	default:
237 		return (false);
238 	}
239 
240 	if (!scanfile_update(sa, cap->ca_type, cap->ca_name, cap->ca_namelen,
241 			     cap->ca_aux, auxlen)) {
242 		return (false);
243 	}
244 
245 done:
246 	if (cap->ca_type != FILETYPE_RCS_ATTIC) {
247 		logmsg(" SetAttrs %.*s", cap->ca_namelen, cap->ca_name);
248 	} else {
249 		logmsg(" SetAttrs %.*s in Attic", cap->ca_namelen,
250 		       cap->ca_name);
251 	}
252 
253 	return (true);
254 }
255 
256 bool
updater_rcs_scanfile_update(struct updater_args * uda)257 updater_rcs_scanfile_update(struct updater_args *uda)
258 {
259 	const char *cmdmsg, *posmsg;
260 	struct cvsync_attr *cap = &uda->uda_attr;
261 	struct scanfile_args *sa = uda->uda_scanfile;
262 	size_t auxmax = sizeof(cap->ca_aux), auxlen;
263 
264 	if (sa == NULL)
265 		goto done;
266 
267 	switch (cap->ca_type) {
268 	case FILETYPE_FILE:
269 		if ((auxlen = attr_rcs_encode_file(cap->ca_aux, auxmax,
270 						   cap->ca_mtime, cap->ca_size,
271 						   cap->ca_mode)) == 0) {
272 			return (false);
273 		}
274 		break;
275 	case FILETYPE_RCS:
276 	case FILETYPE_RCS_ATTIC:
277 		if ((auxlen = attr_rcs_encode_rcs(cap->ca_aux, auxmax,
278 						  cap->ca_mtime,
279 						  cap->ca_mode)) == 0) {
280 			return (false);
281 		}
282 		break;
283 	case FILETYPE_SYMLINK:
284 		auxlen = cap->ca_auxlen;
285 		break;
286 	default:
287 		return (false);
288 	}
289 
290 	if (!scanfile_update(sa, cap->ca_type, cap->ca_name, cap->ca_namelen,
291 			     cap->ca_aux, auxlen)) {
292 		return (false);
293 	}
294 
295 done:
296 	switch (cap->ca_tag) {
297 	case UPDATER_UPDATE_GENERIC:
298 	case UPDATER_UPDATE_RDIFF:
299 		cmdmsg = "Update";
300 		break;
301 	case UPDATER_UPDATE_RCS:
302 		cmdmsg = "Edit";
303 		break;
304 	default:
305 		return (false);
306 	}
307 
308 	if (cap->ca_type != FILETYPE_RCS_ATTIC)
309 		posmsg = "";
310 	else
311 		posmsg = " in Attic";
312 
313 	logmsg(" %s %.*s%s", cmdmsg, cap->ca_namelen, cap->ca_name, posmsg);
314 
315 	return (true);
316 }
317