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