2 * Copyright (c) 2003-2006, Maxime Henrion <mux@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
47 static int config_parse_refusefiles(struct coll *);
48 static int config_parse_refusefile(struct coll *, char *);
52 /* These are globals because I can't think of a better way with yacc. */
53 static STAILQ_HEAD(, coll) colls;
54 static struct coll *cur_coll;
55 static struct coll *defaults;
56 static struct coll *ovcoll;
58 static const char *cfgfile;
61 * Extract all the configuration information from the config
62 * file and some command line parameters.
65 config_init(const char *file, struct coll *override, int overridemask)
67 struct config *config;
74 config = xmalloc(sizeof(struct config));
75 memset(config, 0, sizeof(struct config));
78 defaults = coll_new(NULL);
79 /* Set the default umask. */
82 defaults->co_umask = mask;
84 ovmask = overridemask;
86 /* Extract a list of collections from the configuration file. */
87 cur_coll = coll_new(defaults);
88 yyin = fopen(file, "r");
90 lprintf(-1, "Cannot open \"%s\": %s\n", file, strerror(errno));
99 memcpy(&config->colls, &colls, sizeof(colls));
100 if (STAILQ_EMPTY(&config->colls)) {
101 lprintf(-1, "Empty supfile\n");
105 /* Fixup the list of collections. */
106 STAILQ_FOREACH(coll, &config->colls, co_next) {
107 if (coll->co_base == NULL)
108 coll->co_base = xstrdup("/usr/local/etc/cvsup");
109 if (coll->co_colldir == NULL)
110 coll->co_colldir = "sup";
111 if (coll->co_prefix == NULL) {
112 coll->co_prefix = xstrdup(coll->co_base);
114 * If prefix is not an absolute pathname, it is
115 * interpreted relative to base.
117 } else if (coll->co_prefix[0] != '/') {
118 slen = strlen(coll->co_base);
119 if (slen > 0 && coll->co_base[slen - 1] != '/')
120 xasprintf(&prefix, "%s/%s", coll->co_base,
123 xasprintf(&prefix, "%s%s", coll->co_base,
125 free(coll->co_prefix);
126 coll->co_prefix = prefix;
128 coll->co_prefixlen = strlen(coll->co_prefix);
129 /* Determine whether to checksum RCS files or not. */
130 if (coll->co_options & CO_EXACTRCS)
131 coll->co_options |= CO_CHECKRCS;
133 coll->co_options &= ~CO_CHECKRCS;
134 /* In recent versions, we always try to set the file modes. */
135 coll->co_options |= CO_SETMODE;
136 coll->co_options |= CO_NORSYNC;
137 error = config_parse_refusefiles(coll);
144 config->host = STAILQ_FIRST(&config->colls)->co_host;
154 config_checkcolls(struct config *config)
159 int error, numvalid, ret;
162 STAILQ_FOREACH(coll, &config->colls, co_next) {
163 error = stat(coll->co_prefix, &sb);
164 if (error || !S_ISDIR(sb.st_mode)) {
165 /* Skip this collection, and warn about it unless its
166 prefix is a symbolic link pointing to "SKIP". */
167 coll->co_options |= CO_SKIP;
168 ret = readlink(coll->co_prefix, linkname,
170 if (ret != 4 || memcmp(linkname, "SKIP", 4) != 0) {
171 lprintf(-1,"Nonexistent prefix \"%s\" for "
172 "%s/%s\n", coll->co_prefix, coll->co_name,
183 config_parse_refusefiles(struct coll *coll)
185 char *collstem, *suffix, *supdir, *path;
188 if (coll->co_colldir[0] == '/')
189 supdir = xstrdup(coll->co_colldir);
191 xasprintf(&supdir, "%s/%s", coll->co_base, coll->co_colldir);
193 /* First, the global refuse file that applies to all collections. */
194 xasprintf(&path, "%s/refuse", supdir);
195 error = config_parse_refusefile(coll, path);
202 /* Next the per-collection refuse files that applies to all release/tag
204 xasprintf(&collstem, "%s/%s/refuse", supdir, coll->co_name);
206 error = config_parse_refusefile(coll, collstem);
212 /* Finally, the per-release and per-tag refuse file. */
213 suffix = coll_statussuffix(coll);
214 if (suffix != NULL) {
215 xasprintf(&path, "%s%s", collstem, suffix);
217 error = config_parse_refusefile(coll, path);
225 * Parses a "refuse" file, and records the relevant information in
226 * coll->co_refusals. If the file does not exist, it is silently
230 config_parse_refusefile(struct coll *coll, char *path)
233 char *cp, *line, *pat;
235 rd = stream_open_file(path, O_RDONLY);
238 while ((line = stream_getln(rd, NULL)) != NULL) {
241 /* Trim leading whitespace. */
242 pat += strspn(pat, " \t");
245 cp = strpbrk(pat, " \t");
248 pattlist_add(coll->co_refusals, pat);
254 if (!stream_eof(rd)) {
256 lprintf(-1, "Read failure from \"%s\": %s\n", path,
265 config_free(struct config *config)
269 while (!STAILQ_EMPTY(&config->colls)) {
270 coll = STAILQ_FIRST(&config->colls);
271 STAILQ_REMOVE_HEAD(&config->colls, co_next);
274 if (config->server != NULL)
275 stream_close(config->server);
276 if (config->laddr != NULL)
281 /* Create a new collection, inheriting options from the default collection. */
283 coll_new(struct coll *def)
287 new = xmalloc(sizeof(struct coll));
288 memset(new, 0, sizeof(struct coll));
290 new->co_options = def->co_options;
291 new->co_umask = def->co_umask;
292 if (def->co_host != NULL)
293 new->co_host = xstrdup(def->co_host);
294 if (def->co_base != NULL)
295 new->co_base = xstrdup(def->co_base);
296 if (def->co_date != NULL)
297 new->co_date = xstrdup(def->co_date);
298 if (def->co_prefix != NULL)
299 new->co_prefix = xstrdup(def->co_prefix);
300 if (def->co_release != NULL)
301 new->co_release = xstrdup(def->co_release);
302 if (def->co_tag != NULL)
303 new->co_tag = xstrdup(def->co_tag);
304 if (def->co_listsuffix != NULL)
305 new->co_listsuffix = xstrdup(def->co_listsuffix);
307 new->co_tag = xstrdup(".");
308 new->co_date = xstrdup(".");
310 new->co_keyword = keyword_new();
311 new->co_accepts = pattlist_new();
312 new->co_refusals = pattlist_new();
313 new->co_attrignore = FA_DEV | FA_INODE;
318 coll_override(struct coll *coll, struct coll *from, int mask)
321 int newoptions, oldoptions;
323 newoptions = from->co_options & mask;
324 oldoptions = coll->co_options & (CO_MASK & ~mask);
326 if (from->co_release != NULL) {
327 if (coll->co_release != NULL)
328 free(coll->co_release);
329 coll->co_release = xstrdup(from->co_release);
331 if (from->co_host != NULL) {
332 if (coll->co_host != NULL)
334 coll->co_host = xstrdup(from->co_host);
336 if (from->co_base != NULL) {
337 if (coll->co_base != NULL)
339 coll->co_base = xstrdup(from->co_base);
341 if (from->co_colldir != NULL)
342 coll->co_colldir = from->co_colldir;
343 if (from->co_prefix != NULL) {
344 if (coll->co_prefix != NULL)
345 free(coll->co_prefix);
346 coll->co_prefix = xstrdup(from->co_prefix);
348 if (newoptions & CO_CHECKOUTMODE) {
349 if (from->co_tag != NULL) {
350 if (coll->co_tag != NULL)
352 coll->co_tag = xstrdup(from->co_tag);
354 if (from->co_date != NULL) {
355 if (coll->co_date != NULL)
357 coll->co_date = xstrdup(from->co_date);
360 if (from->co_listsuffix != NULL) {
361 if (coll->co_listsuffix != NULL)
362 free(coll->co_listsuffix);
363 coll->co_listsuffix = xstrdup(from->co_listsuffix);
365 for (i = 0; i < pattlist_size(from->co_accepts); i++) {
366 pattlist_add(coll->co_accepts,
367 pattlist_get(from->co_accepts, i));
369 for (i = 0; i < pattlist_size(from->co_refusals); i++) {
370 pattlist_add(coll->co_refusals,
371 pattlist_get(from->co_refusals, i));
373 coll->co_options = oldoptions | newoptions;
377 coll_statussuffix(struct coll *coll)
382 if (coll->co_listsuffix != NULL) {
383 xasprintf(&suffix, ".%s", coll->co_listsuffix);
384 } else if (coll->co_options & CO_USERELSUFFIX) {
385 if (coll->co_tag == NULL)
389 if (coll->co_release != NULL) {
390 if (coll->co_options & CO_CHECKOUTMODE) {
391 xasprintf(&suffix, ".%s:%s",
392 coll->co_release, tag);
394 xasprintf(&suffix, ".%s", coll->co_release);
396 } else if (coll->co_options & CO_CHECKOUTMODE) {
397 xasprintf(&suffix, ":%s", tag);
405 coll_statuspath(struct coll *coll)
409 suffix = coll_statussuffix(coll);
410 if (suffix != NULL) {
411 if (coll->co_colldir[0] == '/')
412 xasprintf(&path, "%s/%s/checkouts%s", coll->co_colldir,
413 coll->co_name, suffix);
415 xasprintf(&path, "%s/%s/%s/checkouts%s", coll->co_base,
416 coll->co_colldir, coll->co_name, suffix);
418 if (coll->co_colldir[0] == '/')
419 xasprintf(&path, "%s/%s/checkouts", coll->co_colldir,
422 xasprintf(&path, "%s/%s/%s/checkouts", coll->co_base,
423 coll->co_colldir, coll->co_name);
434 cur_coll->co_name = name;
435 coll_override(cur_coll, ovcoll, ovmask);
436 if (cur_coll->co_release == NULL) {
437 lprintf(-1, "Release not specified for collection "
438 "\"%s\"\n", cur_coll->co_name);
441 if (cur_coll->co_host == NULL) {
442 lprintf(-1, "Host not specified for collection "
443 "\"%s\"\n", cur_coll->co_name);
446 if (!STAILQ_EMPTY(&colls)) {
447 coll = STAILQ_LAST(&colls, coll, co_next);
448 if (strcmp(coll->co_host, cur_coll->co_host) != 0) {
449 lprintf(-1, "All \"host\" fields in the supfile "
450 "must be the same\n");
454 STAILQ_INSERT_TAIL(&colls, cur_coll, co_next);
455 cur_coll = coll_new(defaults);
459 coll_free(struct coll *coll)
464 if (coll->co_host != NULL)
466 if (coll->co_base != NULL)
468 if (coll->co_date != NULL)
470 if (coll->co_prefix != NULL)
471 free(coll->co_prefix);
472 if (coll->co_release != NULL)
473 free(coll->co_release);
474 if (coll->co_tag != NULL)
476 if (coll->co_cvsroot != NULL)
477 free(coll->co_cvsroot);
478 if (coll->co_name != NULL)
480 if (coll->co_listsuffix != NULL)
481 free(coll->co_listsuffix);
482 keyword_free(coll->co_keyword);
483 if (coll->co_dirfilter != NULL)
484 globtree_free(coll->co_dirfilter);
485 if (coll->co_dirfilter != NULL)
486 globtree_free(coll->co_filefilter);
487 if (coll->co_norsync != NULL)
488 globtree_free(coll->co_norsync);
489 if (coll->co_accepts != NULL)
490 pattlist_free(coll->co_accepts);
491 if (coll->co_refusals != NULL)
492 pattlist_free(coll->co_refusals);
497 coll_setopt(int opt, char *value)
505 if (coll->co_host != NULL)
507 coll->co_host = value;
510 if (coll->co_base != NULL)
512 coll->co_base = value;
515 if (coll->co_date != NULL)
517 coll->co_date = value;
518 coll->co_options |= CO_CHECKOUTMODE;
521 if (coll->co_prefix != NULL)
522 free(coll->co_prefix);
523 coll->co_prefix = value;
526 if (coll->co_release != NULL)
527 free(coll->co_release);
528 coll->co_release = value;
531 if (coll->co_tag != NULL)
533 coll->co_tag = value;
534 coll->co_options |= CO_CHECKOUTMODE;
537 if (strchr(value, '/') != NULL) {
538 lprintf(-1, "Parse error in \"%s\": \"list\" suffix "
539 "must not contain slashes\n", cfgfile);
542 if (coll->co_listsuffix != NULL)
543 free(coll->co_listsuffix);
544 coll->co_listsuffix = value;
547 error = asciitoint(value, &mask, 8);
550 lprintf(-1, "Parse error in \"%s\": Invalid "
551 "umask value\n", cfgfile);
554 coll->co_umask = mask;
556 case PT_USE_REL_SUFFIX:
557 coll->co_options |= CO_USERELSUFFIX;
560 coll->co_options |= CO_DELETE | CO_EXACTRCS;
563 coll->co_options |= CO_COMPRESS;
566 coll->co_options |= CO_NORSYNC;
571 /* Set "coll" as being the default collection. */
578 cur_coll = coll_new(defaults);