]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/subversion/subversion/libsvn_ra_serf/list.c
Import DTS files for riscv from Linux 5.4
[FreeBSD/FreeBSD.git] / contrib / subversion / subversion / libsvn_ra_serf / list.c
1 /*
2  * list.c :  entry point for the list RA function in 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
25
26
27 #include <apr_uri.h>
28 #include <serf.h>
29
30 #include "svn_hash.h"
31 #include "svn_pools.h"
32 #include "svn_ra.h"
33 #include "svn_dav.h"
34 #include "svn_base64.h"
35 #include "svn_xml.h"
36 #include "svn_config.h"
37 #include "svn_path.h"
38 #include "svn_props.h"
39 #include "svn_time.h"
40
41 #include "private/svn_dav_protocol.h"
42 #include "private/svn_string_private.h"
43 #include "private/svn_subr_private.h"
44 #include "svn_private_config.h"
45
46 #include "ra_serf.h"
47 #include "../libsvn_ra/ra_loader.h"
48
49
50
51 /*
52  * This enum represents the current state of our XML parsing for a REPORT.
53  */
54 enum list_state_e {
55   INITIAL = XML_STATE_INITIAL,
56   REPORT,
57   ITEM,
58   AUTHOR
59 };
60
61 typedef struct list_context_t {
62   apr_pool_t *pool;
63
64   /* parameters set by our caller */
65   const char *path;
66   svn_revnum_t revision;
67   const apr_array_header_t *patterns;
68   svn_depth_t depth;
69   apr_uint32_t dirent_fields;
70   apr_array_header_t *props;
71
72   /* Buffer the author info for the current item.
73    * We use the AUTHOR pointer to differentiate between 0-length author
74    * strings and missing / NULL authors. */
75   const char *author;
76   svn_stringbuf_t *author_buf;
77
78   /* log receiver function and baton */
79   svn_ra_dirent_receiver_t receiver;
80   void *receiver_baton;
81 } list_context_t;
82
83 #define D_ "DAV:"
84 #define S_ SVN_XML_NAMESPACE
85 static const svn_ra_serf__xml_transition_t log_ttable[] = {
86   { INITIAL, S_, "list-report", REPORT,
87     FALSE, { NULL }, FALSE },
88
89   { REPORT, S_, "item", ITEM,
90     TRUE, { "node-kind", "?size", "?has-props", "?created-rev",
91              "?date", NULL }, TRUE },
92
93   { ITEM, D_, "creator-displayname", AUTHOR,
94     TRUE, { "?encoding", NULL }, TRUE },
95
96   { 0 }
97 };
98
99 /* Conforms to svn_ra_serf__xml_closed_t  */
100 static svn_error_t *
101 item_closed(svn_ra_serf__xml_estate_t *xes,
102             void *baton,
103             int leaving_state,
104             const svn_string_t *cdata,
105             apr_hash_t *attrs,
106             apr_pool_t *scratch_pool)
107 {
108   list_context_t *list_ctx = baton;
109
110   if (leaving_state == AUTHOR)
111     {
112       /* For compatibility with liveprops, current servers will not use
113        * base64-encoding for "binary" user names bu simply drop the
114        * offending control chars.
115        *
116        * We might want to switch to revprop-style encoding, though,
117        * and this is the code to do that. */
118       const char *encoding = svn_hash_gets(attrs, "encoding");
119       if (encoding)
120         {
121           /* Check for a known encoding type.  This is easy -- there's
122              only one.  */
123           if (strcmp(encoding, "base64") != 0)
124             {
125               return svn_error_createf(SVN_ERR_RA_DAV_MALFORMED_DATA, NULL,
126                                        _("Unsupported encoding '%s'"),
127                                        encoding);
128             }
129
130           cdata = svn_base64_decode_string(cdata, scratch_pool);
131         }
132
133       /* Remember until the next ITEM closing tag. */
134       svn_stringbuf_set(list_ctx->author_buf, cdata->data);
135       list_ctx->author = list_ctx->author_buf->data;
136     }
137   else if (leaving_state == ITEM)
138     {
139       const char *dirent_path = cdata->data;
140       const char *kind_word, *date, *crev, *size;
141       svn_dirent_t dirent = { 0 };
142
143       kind_word = svn_hash_gets(attrs, "node-kind");
144       size = svn_hash_gets(attrs, "size");
145
146       dirent.has_props = svn_hash__get_bool(attrs, "has-props", FALSE);
147       crev = svn_hash_gets(attrs, "created-rev");
148       date = svn_hash_gets(attrs, "date");
149
150       /* Convert data. */
151       dirent.kind = svn_node_kind_from_word(kind_word);
152
153       if (size)
154         SVN_ERR(svn_cstring_atoi64(&dirent.size, size));
155       else
156         dirent.size = SVN_INVALID_FILESIZE;
157
158       if (crev)
159         SVN_ERR(svn_revnum_parse(&dirent.created_rev, crev, NULL));
160       else
161         dirent.created_rev = SVN_INVALID_REVNUM;
162
163       if (date)
164         SVN_ERR(svn_time_from_cstring(&dirent.time, date, scratch_pool));
165
166       if (list_ctx->author)
167         dirent.last_author = list_ctx->author;
168
169       /* Invoke RECEIVER */
170       SVN_ERR(list_ctx->receiver(dirent_path, &dirent,
171                                  list_ctx->receiver_baton, scratch_pool));
172
173       /* Reset buffered info. */
174       list_ctx->author = NULL;
175     }
176
177   return SVN_NO_ERROR;
178 }
179
180 /* Implements svn_ra_serf__request_body_delegate_t */
181 static svn_error_t *
182 create_list_body(serf_bucket_t **body_bkt,
183                  void *baton,
184                  serf_bucket_alloc_t *alloc,
185                  apr_pool_t *pool /* request pool */,
186                  apr_pool_t *scratch_pool)
187 {
188   serf_bucket_t *buckets;
189   list_context_t *list_ctx = baton;
190   int i;
191
192   buckets = serf_bucket_aggregate_create(alloc);
193
194   svn_ra_serf__add_open_tag_buckets(buckets, alloc,
195                                     "S:list-report",
196                                     "xmlns:S", SVN_XML_NAMESPACE,
197                                     SVN_VA_NULL);
198
199   svn_ra_serf__add_tag_buckets(buckets,
200                                "S:path", list_ctx->path,
201                                alloc);
202   svn_ra_serf__add_tag_buckets(buckets,
203                                "S:revision",
204                                apr_ltoa(pool, list_ctx->revision),
205                                alloc);
206   svn_ra_serf__add_tag_buckets(buckets,
207                                "S:depth", svn_depth_to_word(list_ctx->depth),
208                                alloc);
209
210   if (list_ctx->patterns)
211     {
212       for (i = 0; i < list_ctx->patterns->nelts; i++)
213         {
214           char *name = APR_ARRAY_IDX(list_ctx->patterns, i, char *);
215           svn_ra_serf__add_tag_buckets(buckets,
216                                        "S:pattern", name,
217                                        alloc);
218         }
219       if (list_ctx->patterns->nelts == 0)
220         {
221           svn_ra_serf__add_empty_tag_buckets(buckets, alloc,
222                                              "S:no-patterns", SVN_VA_NULL);
223         }
224     }
225
226   for (i = 0; i < list_ctx->props->nelts; i++)
227     {
228       const svn_ra_serf__dav_props_t *prop
229         = &APR_ARRAY_IDX(list_ctx->props, i, const svn_ra_serf__dav_props_t);
230       const char *name
231         = apr_pstrcat(pool, prop->xmlns, prop->name, SVN_VA_NULL);
232
233       svn_ra_serf__add_tag_buckets(buckets, "S:prop", name, alloc);
234     }
235
236   svn_ra_serf__add_close_tag_buckets(buckets, alloc,
237                                      "S:list-report");
238
239   *body_bkt = buckets;
240   return SVN_NO_ERROR;
241 }
242
243
244 svn_error_t *
245 svn_ra_serf__list(svn_ra_session_t *ra_session,
246                   const char *path,
247                   svn_revnum_t revision,
248                   const apr_array_header_t *patterns,
249                   svn_depth_t depth,
250                   apr_uint32_t dirent_fields,
251                   svn_ra_dirent_receiver_t receiver,
252                   void *receiver_baton,
253                   apr_pool_t *scratch_pool)
254 {
255   list_context_t *list_ctx;
256   svn_ra_serf__session_t *session = ra_session->priv;
257   svn_ra_serf__handler_t *handler;
258   svn_ra_serf__xml_context_t *xmlctx;
259   const char *req_url;
260
261   list_ctx = apr_pcalloc(scratch_pool, sizeof(*list_ctx));
262   list_ctx->pool = scratch_pool;
263   list_ctx->receiver = receiver;
264   list_ctx->receiver_baton = receiver_baton;
265   list_ctx->path = path;
266   list_ctx->revision = revision;
267   list_ctx->patterns = patterns;
268   list_ctx->depth = depth;
269   list_ctx->dirent_fields = dirent_fields;
270   list_ctx->props = svn_ra_serf__get_dirent_props(dirent_fields, session,
271                                                   scratch_pool);
272   list_ctx->author_buf = svn_stringbuf_create_empty(scratch_pool);
273
274   /* At this point, we may have a deleted file.  So, we'll match ra_neon's
275    * behavior and use the larger of start or end as our 'peg' rev.
276    */
277   SVN_ERR(svn_ra_serf__get_stable_url(&req_url, NULL /* latest_revnum */,
278                                       session,
279                                       NULL /* url */, revision,
280                                       scratch_pool, scratch_pool));
281
282   xmlctx = svn_ra_serf__xml_context_create(log_ttable,
283                                            NULL, item_closed, NULL,
284                                            list_ctx,
285                                            scratch_pool);
286   handler = svn_ra_serf__create_expat_handler(session, xmlctx, NULL,
287                                               scratch_pool);
288
289   handler->method = "REPORT";
290   handler->path = req_url;
291   handler->body_delegate = create_list_body;
292   handler->body_delegate_baton = list_ctx;
293   handler->body_type = "text/xml";
294
295   SVN_ERR(svn_ra_serf__context_run_one(handler, scratch_pool));
296
297   if (handler->sline.code != 200)
298     SVN_ERR(svn_ra_serf__unexpected_status(handler));
299
300   return SVN_NO_ERROR;
301 }