1/*
2 * ctx.c:  initialization function for client 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/* ==================================================================== */
25
26
27
28/*** Includes. ***/
29
30#include <stddef.h>
31#include <apr_pools.h>
32#include "svn_hash.h"
33#include "svn_client.h"
34#include "svn_error.h"
35
36#include "private/svn_wc_private.h"
37
38#include "client.h"
39
40
41/*** Code. ***/
42
43/* Call the notify_func of the context provided by BATON, if non-NULL. */
44static void
45call_notify_func(void *baton, const svn_wc_notify_t *n, apr_pool_t *pool)
46{
47  const svn_client_ctx_t *ctx = baton;
48
49  if (ctx->notify_func)
50    ctx->notify_func(ctx->notify_baton, n->path, n->action, n->kind,
51                     n->mime_type, n->content_state, n->prop_state,
52                     n->revision);
53}
54
55static svn_error_t *
56call_conflict_func(svn_wc_conflict_result_t **result,
57                   const svn_wc_conflict_description2_t *conflict,
58                   void *baton,
59                   apr_pool_t *result_pool,
60                   apr_pool_t *scratch_pool)
61{
62  svn_client_ctx_t *ctx = baton;
63
64  if (ctx->conflict_func)
65    {
66      const svn_wc_conflict_description_t *cd;
67
68      cd = svn_wc__cd2_to_cd(conflict, scratch_pool);
69      SVN_ERR(ctx->conflict_func(result, cd, ctx->conflict_baton,
70                                 result_pool));
71    }
72  else
73    {
74      /* We have to set a result; so we postpone */
75      *result = svn_wc_create_conflict_result(svn_wc_conflict_choose_postpone,
76                                              NULL, result_pool);
77    }
78
79  return SVN_NO_ERROR;
80}
81
82/* The magic number in client_ctx_t.magic_id. */
83#define CLIENT_CTX_MAGIC APR_UINT64_C(0xDEADBEEF600DF00D)
84
85svn_client__private_ctx_t *
86svn_client__get_private_ctx(svn_client_ctx_t *ctx)
87{
88  svn_client__private_ctx_t *const private_ctx =
89    (void*)((char *)ctx - offsetof(svn_client__private_ctx_t, public_ctx));
90  SVN_ERR_ASSERT_NO_RETURN(&private_ctx->public_ctx == ctx);
91  SVN_ERR_ASSERT_NO_RETURN(0 == private_ctx->magic_null);
92  SVN_ERR_ASSERT_NO_RETURN(CLIENT_CTX_MAGIC == private_ctx->magic_id);
93  return private_ctx;
94}
95
96svn_error_t *
97svn_client_create_context2(svn_client_ctx_t **ctx,
98                           apr_hash_t *cfg_hash,
99                           apr_pool_t *pool)
100{
101  svn_config_t *cfg_config;
102
103  svn_client__private_ctx_t *const private_ctx =
104    apr_pcalloc(pool, sizeof(*private_ctx));
105  svn_client_ctx_t *const public_ctx = &private_ctx->public_ctx;
106
107  private_ctx->magic_null = 0;
108  private_ctx->magic_id = CLIENT_CTX_MAGIC;
109
110  public_ctx->notify_func2 = call_notify_func;
111  public_ctx->notify_baton2 = public_ctx;
112
113  public_ctx->conflict_func2 = call_conflict_func;
114  public_ctx->conflict_baton2 = public_ctx;
115
116  public_ctx->config = cfg_hash;
117
118  if (cfg_hash)
119    cfg_config = svn_hash_gets(cfg_hash, SVN_CONFIG_CATEGORY_CONFIG);
120  else
121    cfg_config = NULL;
122
123  SVN_ERR(svn_wc_context_create(&public_ctx->wc_ctx, cfg_config,
124                                pool, pool));
125  *ctx = public_ctx;
126
127  return SVN_NO_ERROR;
128}
129
130svn_error_t *
131svn_client_create_context(svn_client_ctx_t **ctx,
132                          apr_pool_t *pool)
133{
134  return svn_client_create_context2(ctx, NULL, pool);
135}
136