2 * config_pool.c : pool of configuration objects
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
27 #include "svn_checksum.h"
29 #include "svn_pools.h"
31 #include "private/svn_subr_private.h"
32 #include "private/svn_repos_private.h"
34 #include "svn_private_config.h"
36 #include "config_file.h"
39 /* Return a memory buffer structure allocated in POOL and containing the
43 checksum_as_key(svn_checksum_t *checksum,
46 svn_membuf_t *result = apr_pcalloc(pool, sizeof(*result));
47 apr_size_t size = svn_checksum_size(checksum);
49 svn_membuf__create(result, size, pool);
50 result->size = size; /* exact length is required! */
51 memcpy(result->data, checksum->digest, size);
56 /* Set *CFG to the configuration serialized in STREAM and cache it in
57 * CONFIG_POOL under CHECKSUM. The configuration will only be parsed if
58 * we can't find it the CONFIG_POOL already.
60 * RESULT_POOL determines the lifetime of the returned reference and
61 * SCRATCH_POOL is being used for temporary allocations.
64 find_config(svn_config_t **cfg,
65 svn_repos__config_pool_t *config_pool,
67 svn_checksum_t *checksum,
68 apr_pool_t *result_pool,
69 apr_pool_t *scratch_pool)
71 /* First, attempt the cache lookup. */
72 svn_membuf_t *key = checksum_as_key(checksum, scratch_pool);
73 SVN_ERR(svn_object_pool__lookup((void **)cfg, config_pool, key,
76 /* Not found? => parse and cache */
81 /* create a pool for the new config object and parse the data into it */
82 apr_pool_t *cfg_pool = svn_object_pool__new_item_pool(config_pool);
83 SVN_ERR(svn_config_parse(&config, stream, FALSE, FALSE, cfg_pool));
85 /* switch config data to r/o mode to guarantee thread-safe access */
86 svn_config__set_read_only(config, cfg_pool);
88 /* add config in pool, handle loads races and return the right config */
89 SVN_ERR(svn_object_pool__insert((void **)cfg, config_pool, key,
90 config, cfg_pool, result_pool));
96 /* API implementation */
99 svn_repos__config_pool_create(svn_repos__config_pool_t **config_pool,
100 svn_boolean_t thread_safe,
103 return svn_error_trace(svn_object_pool__create(config_pool,
108 svn_repos__config_pool_get(svn_config_t **cfg,
109 svn_repos__config_pool_t *config_pool,
111 svn_boolean_t must_exist,
112 svn_repos_t *preferred_repos,
115 svn_error_t *err = SVN_NO_ERROR;
116 apr_pool_t *scratch_pool = svn_pool_create(pool);
117 config_access_t *access = svn_repos__create_config_access(preferred_repos,
119 svn_stream_t *stream;
120 svn_checksum_t *checksum;
123 err = svn_repos__get_config(&stream, &checksum, access, path, must_exist,
126 err = svn_error_quick_wrapf(find_config(cfg, config_pool, stream,
127 checksum, pool, scratch_pool),
128 "Error while parsing config file: '%s':",
131 /* Let the standard implementation handle all the difficult cases.
132 * Note that for in-repo configs, there are no further special cases to
133 * check for and deal with. */
134 if (!*cfg && !svn_path_is_url(path))
136 svn_error_clear(err);
137 err = svn_config_read3(cfg, path, must_exist, FALSE, FALSE, pool);
140 svn_repos__destroy_config_access(access);
141 svn_pool_destroy(scratch_pool);
143 /* we need to duplicate the root structure as it contains temp. buffers */
145 *cfg = svn_config__shallow_copy(*cfg, pool);
147 return svn_error_trace(err);