1/*
2 * config_pool.c :  pool of configuration objects
3 *
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
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
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
20 *    under the License.
21 * ====================================================================
22 */
23
24
25
26
27#include "svn_checksum.h"
28#include "svn_path.h"
29#include "svn_pools.h"
30
31#include "private/svn_subr_private.h"
32#include "private/svn_repos_private.h"
33
34#include "svn_private_config.h"
35
36#include "config_file.h"
37
38
39/* Return a memory buffer structure allocated in POOL and containing the
40 * data from CHECKSUM.
41 */
42static svn_membuf_t *
43checksum_as_key(svn_checksum_t *checksum,
44                apr_pool_t *pool)
45{
46  svn_membuf_t *result = apr_pcalloc(pool, sizeof(*result));
47  apr_size_t size = svn_checksum_size(checksum);
48
49  svn_membuf__create(result, size, pool);
50  result->size = size; /* exact length is required! */
51  memcpy(result->data, checksum->digest, size);
52
53  return result;
54}
55
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.
59 *
60 * RESULT_POOL determines the lifetime of the returned reference and
61 * SCRATCH_POOL is being used for temporary allocations.
62 */
63static svn_error_t *
64find_config(svn_config_t **cfg,
65            svn_repos__config_pool_t *config_pool,
66            svn_stream_t *stream,
67            svn_checksum_t *checksum,
68            apr_pool_t *result_pool,
69            apr_pool_t *scratch_pool)
70{
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,
74                                  result_pool));
75
76  /* Not found? => parse and cache */
77  if (!*cfg)
78    {
79      svn_config_t *config;
80
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));
84
85      /* switch config data to r/o mode to guarantee thread-safe access */
86      svn_config__set_read_only(config, cfg_pool);
87
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));
91    }
92
93  return SVN_NO_ERROR;
94}
95
96/* API implementation */
97
98svn_error_t *
99svn_repos__config_pool_create(svn_repos__config_pool_t **config_pool,
100                              svn_boolean_t thread_safe,
101                              apr_pool_t *pool)
102{
103  return svn_error_trace(svn_object_pool__create(config_pool,
104                                                 thread_safe, pool));
105}
106
107svn_error_t *
108svn_repos__config_pool_get(svn_config_t **cfg,
109                           svn_repos__config_pool_t *config_pool,
110                           const char *path,
111                           svn_boolean_t must_exist,
112                           svn_repos_t *preferred_repos,
113                           apr_pool_t *pool)
114{
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,
118                                                            scratch_pool);
119  svn_stream_t *stream;
120  svn_checksum_t *checksum;
121
122  *cfg = NULL;
123  err = svn_repos__get_config(&stream, &checksum, access, path, must_exist,
124                              scratch_pool);
125  if (!err)
126    err = svn_error_quick_wrapf(find_config(cfg, config_pool, stream,
127                                            checksum, pool, scratch_pool),
128                                "Error while parsing config file: '%s':",
129                                path);
130
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))
135    {
136      svn_error_clear(err);
137      err = svn_config_read3(cfg, path, must_exist, FALSE, FALSE, pool);
138    }
139
140  svn_repos__destroy_config_access(access);
141  svn_pool_destroy(scratch_pool);
142
143  /* we need to duplicate the root structure as it contains temp. buffers */
144  if (*cfg)
145    *cfg = svn_config__shallow_copy(*cfg, pool);
146
147  return svn_error_trace(err);
148}
149