1/*
2 * context.c:  routines for managing a working copy context
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#include <apr_pools.h>
25
26#include "svn_types.h"
27#include "svn_pools.h"
28#include "svn_dirent_uri.h"
29#include "svn_path.h"
30
31#include "wc.h"
32#include "wc_db.h"
33
34#include "svn_private_config.h"
35
36
37
38/* APR cleanup function used to explicitly close any of our dependent
39   data structures before we disappear ourselves. */
40static apr_status_t
41close_ctx_apr(void *data)
42{
43  svn_wc_context_t *ctx = data;
44
45  if (ctx->close_db_on_destroy)
46    {
47      svn_error_t *err = svn_wc__db_close(ctx->db);
48      if (err)
49        {
50          int result = err->apr_err;
51          svn_error_clear(err);
52          return result;
53        }
54    }
55
56  return APR_SUCCESS;
57}
58
59
60svn_error_t *
61svn_wc_context_create(svn_wc_context_t **wc_ctx,
62                      const svn_config_t *config,
63                      apr_pool_t *result_pool,
64                      apr_pool_t *scratch_pool)
65{
66  svn_wc_context_t *ctx = apr_pcalloc(result_pool, sizeof(*ctx));
67
68  /* Create the state_pool, and open up a wc_db in it.
69   * Since config contains a private mutable member but C doesn't support
70   * we need to make it writable */
71  ctx->state_pool = result_pool;
72  SVN_ERR(svn_wc__db_open(&ctx->db, (svn_config_t *)config,
73                          FALSE, TRUE, ctx->state_pool, scratch_pool));
74  ctx->close_db_on_destroy = TRUE;
75
76  apr_pool_cleanup_register(result_pool, ctx, close_ctx_apr,
77                            apr_pool_cleanup_null);
78
79  *wc_ctx = ctx;
80
81  return SVN_NO_ERROR;
82}
83
84
85svn_error_t *
86svn_wc__context_create_with_db(svn_wc_context_t **wc_ctx,
87                               svn_config_t *config,
88                               svn_wc__db_t *db,
89                               apr_pool_t *result_pool)
90{
91  svn_wc_context_t *ctx = apr_pcalloc(result_pool, sizeof(*ctx));
92
93  /* Create the state pool.  We don't put the wc_db in it, because it's
94     already open in a separate pool somewhere.  We also won't close the
95     wc_db when we destroy the context, since it's not ours to close. */
96  ctx->state_pool = result_pool;
97  ctx->db = db;
98  ctx->close_db_on_destroy = FALSE;
99
100  apr_pool_cleanup_register(result_pool, ctx, close_ctx_apr,
101                            apr_pool_cleanup_null);
102
103  *wc_ctx = ctx;
104
105  return SVN_NO_ERROR;
106}
107
108
109svn_error_t *
110svn_wc_context_destroy(svn_wc_context_t *wc_ctx)
111{
112  /* We added a cleanup when creating; just run it now to close the context. */
113  apr_pool_cleanup_run(wc_ctx->state_pool, wc_ctx, close_ctx_apr);
114
115  return SVN_NO_ERROR;
116}
117