]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/subversion/subversion/libsvn_ra_serf/mergeinfo.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / subversion / subversion / libsvn_ra_serf / mergeinfo.c
1 /*
2  * mergeinfo.c : entry point for mergeinfo RA functions for ra_serf
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_tables.h>
25 #include <apr_xml.h>
26
27 #include "svn_hash.h"
28 #include "svn_mergeinfo.h"
29 #include "svn_path.h"
30 #include "svn_ra.h"
31 #include "svn_string.h"
32 #include "svn_xml.h"
33
34 #include "private/svn_dav_protocol.h"
35 #include "../libsvn_ra/ra_loader.h"
36 #include "svn_private_config.h"
37 #include "ra_serf.h"
38
39
40 \f
41
42 /* The current state of our XML parsing. */
43 typedef enum mergeinfo_state_e {
44   INITIAL = 0,
45   MERGEINFO_REPORT,
46   MERGEINFO_ITEM,
47   MERGEINFO_PATH,
48   MERGEINFO_INFO
49 } mergeinfo_state_e;
50
51 /* Baton for accumulating mergeinfo.  RESULT_CATALOG stores the final
52    mergeinfo catalog result we are going to hand back to the caller of
53    get_mergeinfo.  */
54 typedef struct mergeinfo_context_t {
55   apr_pool_t *pool;
56   svn_mergeinfo_t result_catalog;
57   const apr_array_header_t *paths;
58   svn_revnum_t revision;
59   svn_mergeinfo_inheritance_t inherit;
60   svn_boolean_t include_descendants;
61 } mergeinfo_context_t;
62
63
64 #define D_ "DAV:"
65 #define S_ SVN_XML_NAMESPACE
66 static const svn_ra_serf__xml_transition_t mergeinfo_ttable[] = {
67   { INITIAL, S_, SVN_DAV__MERGEINFO_REPORT, MERGEINFO_REPORT,
68     FALSE, { NULL }, FALSE },
69
70   { MERGEINFO_REPORT, S_, SVN_DAV__MERGEINFO_ITEM, MERGEINFO_ITEM,
71     FALSE, { NULL }, TRUE },
72
73   { MERGEINFO_ITEM, S_, SVN_DAV__MERGEINFO_PATH, MERGEINFO_PATH,
74     TRUE, { NULL }, TRUE },
75
76   { MERGEINFO_ITEM, S_, SVN_DAV__MERGEINFO_INFO, MERGEINFO_INFO,
77     TRUE, { NULL }, TRUE },
78
79   { 0 }
80 };
81
82
83 /* Conforms to svn_ra_serf__xml_closed_t  */
84 static svn_error_t *
85 mergeinfo_closed(svn_ra_serf__xml_estate_t *xes,
86                  void *baton,
87                  int leaving_state,
88                  const svn_string_t *cdata,
89                  apr_hash_t *attrs,
90                  apr_pool_t *scratch_pool)
91 {
92   mergeinfo_context_t *mergeinfo_ctx = baton;
93
94   if (leaving_state == MERGEINFO_ITEM)
95     {
96       /* Placed here from the child elements.  */
97       const char *path = svn_hash_gets(attrs, "path");
98       const char *info = svn_hash_gets(attrs, "info");
99
100       if (path != NULL && info != NULL)
101         {
102           svn_mergeinfo_t path_mergeinfo;
103
104           /* Correct for naughty servers that send "relative" paths
105              with leading slashes! */
106           if (path[0] == '/')
107             ++path;
108
109           SVN_ERR(svn_mergeinfo_parse(&path_mergeinfo, info,
110                                       mergeinfo_ctx->pool));
111
112           svn_hash_sets(mergeinfo_ctx->result_catalog,
113                         apr_pstrdup(mergeinfo_ctx->pool, path),
114                         path_mergeinfo);
115         }
116     }
117   else
118     {
119       SVN_ERR_ASSERT(leaving_state == MERGEINFO_PATH
120                      || leaving_state == MERGEINFO_INFO);
121
122       /* Stash the value onto the parent MERGEINFO_ITEM.  */
123       svn_ra_serf__xml_note(xes, MERGEINFO_ITEM,
124                             leaving_state == MERGEINFO_PATH
125                               ? "path"
126                               : "info",
127                             cdata->data);
128     }
129
130   return SVN_NO_ERROR;
131 }
132
133
134 static svn_error_t *
135 create_mergeinfo_body(serf_bucket_t **bkt,
136                       void *baton,
137                       serf_bucket_alloc_t *alloc,
138                       apr_pool_t *pool)
139 {
140   mergeinfo_context_t *mergeinfo_ctx = baton;
141   serf_bucket_t *body_bkt;
142
143   body_bkt = serf_bucket_aggregate_create(alloc);
144
145   svn_ra_serf__add_open_tag_buckets(body_bkt, alloc,
146                                     "S:" SVN_DAV__MERGEINFO_REPORT,
147                                     "xmlns:S", SVN_XML_NAMESPACE,
148                                     NULL);
149
150   svn_ra_serf__add_tag_buckets(body_bkt,
151                                "S:" SVN_DAV__REVISION,
152                                apr_ltoa(pool, mergeinfo_ctx->revision),
153                                alloc);
154   svn_ra_serf__add_tag_buckets(body_bkt, "S:" SVN_DAV__INHERIT,
155                                svn_inheritance_to_word(mergeinfo_ctx->inherit),
156                                alloc);
157   if (mergeinfo_ctx->include_descendants)
158     {
159       svn_ra_serf__add_tag_buckets(body_bkt, "S:"
160                                    SVN_DAV__INCLUDE_DESCENDANTS,
161                                    "yes", alloc);
162     }
163
164   if (mergeinfo_ctx->paths)
165     {
166       int i;
167
168       for (i = 0; i < mergeinfo_ctx->paths->nelts; i++)
169         {
170           const char *this_path = APR_ARRAY_IDX(mergeinfo_ctx->paths,
171                                                 i, const char *);
172
173           svn_ra_serf__add_tag_buckets(body_bkt, "S:" SVN_DAV__PATH,
174                                        this_path, alloc);
175         }
176     }
177
178   svn_ra_serf__add_close_tag_buckets(body_bkt, alloc,
179                                      "S:" SVN_DAV__MERGEINFO_REPORT);
180
181   *bkt = body_bkt;
182   return SVN_NO_ERROR;
183 }
184
185 svn_error_t *
186 svn_ra_serf__get_mergeinfo(svn_ra_session_t *ra_session,
187                            svn_mergeinfo_catalog_t *catalog,
188                            const apr_array_header_t *paths,
189                            svn_revnum_t revision,
190                            svn_mergeinfo_inheritance_t inherit,
191                            svn_boolean_t include_descendants,
192                            apr_pool_t *pool)
193 {
194   svn_error_t *err;
195   mergeinfo_context_t *mergeinfo_ctx;
196   svn_ra_serf__session_t *session = ra_session->priv;
197   svn_ra_serf__handler_t *handler;
198   svn_ra_serf__xml_context_t *xmlctx;
199   const char *path;
200
201   *catalog = NULL;
202
203   SVN_ERR(svn_ra_serf__get_stable_url(&path, NULL /* latest_revnum */,
204                                       session, NULL /* conn */,
205                                       NULL /* url */, revision,
206                                       pool, pool));
207
208   mergeinfo_ctx = apr_pcalloc(pool, sizeof(*mergeinfo_ctx));
209   mergeinfo_ctx->pool = pool;
210   mergeinfo_ctx->result_catalog = apr_hash_make(pool);
211   mergeinfo_ctx->paths = paths;
212   mergeinfo_ctx->revision = revision;
213   mergeinfo_ctx->inherit = inherit;
214   mergeinfo_ctx->include_descendants = include_descendants;
215
216   xmlctx = svn_ra_serf__xml_context_create(mergeinfo_ttable,
217                                            NULL, mergeinfo_closed, NULL,
218                                            mergeinfo_ctx,
219                                            pool);
220   handler = svn_ra_serf__create_expat_handler(xmlctx, pool);
221
222   handler->method = "REPORT";
223   handler->path = path;
224   handler->conn = session->conns[0];
225   handler->session = session;
226   handler->body_delegate = create_mergeinfo_body;
227   handler->body_delegate_baton = mergeinfo_ctx;
228   handler->body_type = "text/xml";
229
230   err = svn_ra_serf__context_run_one(handler, pool);
231
232   SVN_ERR(svn_error_compose_create(
233                 svn_ra_serf__error_on_status(handler->sline, handler->path,
234                                              handler->location),
235                 err));
236
237   if (handler->done && apr_hash_count(mergeinfo_ctx->result_catalog))
238     *catalog = mergeinfo_ctx->result_catalog;
239
240   return SVN_NO_ERROR;
241 }