2 * getlocks.c : entry point for get_locks RA functions for ra_serf
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * ====================================================================
32 #include "svn_pools.h"
38 #include "private/svn_dav_protocol.h"
39 #include "private/svn_fspath.h"
40 #include "svn_private_config.h"
42 #include "../libsvn_ra/ra_loader.h"
48 * This enum represents the current state of our XML parsing for a REPORT.
62 typedef struct lock_context_t {
65 /* target and requested depth of the operation. */
67 svn_depth_t requested_depth;
75 #define S_ SVN_XML_NAMESPACE
76 static const svn_ra_serf__xml_transition_t getlocks_ttable[] = {
77 { INITIAL, S_, "get-locks-report", REPORT,
78 FALSE, { NULL }, FALSE },
80 { REPORT, S_, "lock", LOCK,
81 FALSE, { NULL }, TRUE },
83 { LOCK, S_, "path", PATH,
84 TRUE, { NULL }, TRUE },
86 { LOCK, S_, "token", TOKEN,
87 TRUE, { NULL }, TRUE },
89 { LOCK, S_, "owner", OWNER,
90 TRUE, { NULL }, TRUE },
92 { LOCK, S_, "comment", COMMENT,
93 TRUE, { NULL }, TRUE },
95 { LOCK, S_, SVN_DAV__CREATIONDATE, CREATION_DATE,
96 TRUE, { NULL }, TRUE },
98 { LOCK, S_, "expirationdate", EXPIRATION_DATE,
99 TRUE, { NULL }, TRUE },
105 /* Conforms to svn_ra_serf__xml_closed_t */
107 getlocks_closed(svn_ra_serf__xml_estate_t *xes,
110 const svn_string_t *cdata,
112 apr_pool_t *scratch_pool)
114 lock_context_t *lock_ctx = baton;
116 if (leaving_state == LOCK)
118 const char *path = svn_hash_gets(attrs, "path");
119 svn_boolean_t save_lock = FALSE;
121 /* Filter out unwanted paths. Since Subversion only allows
122 locks on files, we can treat depth=immediates the same as
123 depth=files for filtering purposes. Meaning, we'll keep
126 a) its path is the very path we queried, or
127 b) we've asked for a fully recursive answer, or
128 c) we've asked for depth=files or depth=immediates, and this
129 lock is on an immediate child of our query path.
131 if (strcmp(lock_ctx->path, path) == 0
132 || lock_ctx->requested_depth == svn_depth_infinity)
136 else if (lock_ctx->requested_depth == svn_depth_files
137 || lock_ctx->requested_depth == svn_depth_immediates)
139 const char *relpath = svn_fspath__skip_ancestor(lock_ctx->path,
141 if (relpath && (svn_path_component_count(relpath) == 1))
147 /* We get to put the structure on the stack rather than using
148 svn_lock_create(). Bwahahaha.... */
149 svn_lock_t lock = { 0 };
151 svn_lock_t *result_lock;
153 /* Note: these "attributes" came from child elements. Some of
154 them may have not been sent, so the value will be NULL. */
157 lock.token = svn_hash_gets(attrs, "token");
158 lock.owner = svn_hash_gets(attrs, "owner");
159 lock.comment = svn_hash_gets(attrs, "comment");
161 date = svn_hash_gets(attrs, SVN_DAV__CREATIONDATE);
163 SVN_ERR(svn_time_from_cstring(&lock.creation_date, date,
166 date = svn_hash_gets(attrs, "expirationdate");
168 SVN_ERR(svn_time_from_cstring(&lock.expiration_date, date,
171 result_lock = svn_lock_dup(&lock, lock_ctx->pool);
172 svn_hash_sets(lock_ctx->hash, result_lock->path, result_lock);
179 SVN_ERR_ASSERT(cdata != NULL);
181 if (leaving_state == PATH)
183 else if (leaving_state == TOKEN)
185 else if (leaving_state == OWNER)
187 else if (leaving_state == COMMENT)
189 else if (leaving_state == CREATION_DATE)
190 name = SVN_DAV__CREATIONDATE;
191 else if (leaving_state == EXPIRATION_DATE)
192 name = "expirationdate";
194 SVN_ERR_MALFUNCTION();
196 /* Store the lock information onto the LOCK elemstate. */
197 svn_ra_serf__xml_note(xes, LOCK, name, cdata->data);
204 /* Implements svn_ra_serf__request_body_delegate_t */
206 create_getlocks_body(serf_bucket_t **body_bkt,
208 serf_bucket_alloc_t *alloc,
211 lock_context_t *lock_ctx = baton;
212 serf_bucket_t *buckets;
214 buckets = serf_bucket_aggregate_create(alloc);
216 svn_ra_serf__add_open_tag_buckets(
217 buckets, alloc, "S:get-locks-report", "xmlns:S", SVN_XML_NAMESPACE,
218 "depth", svn_depth_to_word(lock_ctx->requested_depth), NULL);
219 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "S:get-locks-report");
226 svn_ra_serf__get_locks(svn_ra_session_t *ra_session,
232 lock_context_t *lock_ctx;
233 svn_ra_serf__session_t *session = ra_session->priv;
234 svn_ra_serf__handler_t *handler;
235 svn_ra_serf__xml_context_t *xmlctx;
236 const char *req_url, *rel_path;
238 req_url = svn_path_url_add_component2(session->session_url.path, path, pool);
239 SVN_ERR(svn_ra_serf__get_relative_path(&rel_path, req_url, session,
242 lock_ctx = apr_pcalloc(pool, sizeof(*lock_ctx));
243 lock_ctx->pool = pool;
244 lock_ctx->path = apr_pstrcat(pool, "/", rel_path, (char *)NULL);
245 lock_ctx->requested_depth = depth;
246 lock_ctx->hash = apr_hash_make(pool);
248 xmlctx = svn_ra_serf__xml_context_create(getlocks_ttable,
249 NULL, getlocks_closed, NULL,
252 handler = svn_ra_serf__create_expat_handler(xmlctx, pool);
254 handler->method = "REPORT";
255 handler->path = req_url;
256 handler->body_type = "text/xml";
257 handler->conn = session->conns[0];
258 handler->session = session;
260 handler->body_delegate = create_getlocks_body;
261 handler->body_delegate_baton = lock_ctx;
263 SVN_ERR(svn_ra_serf__context_run_one(handler, pool));
265 /* We get a 404 when a path doesn't exist in HEAD, but it might
266 have existed earlier (E.g. 'svn ls http://s/svn/trunk/file@1' */
267 if (handler->sline.code != 404)
269 SVN_ERR(svn_ra_serf__error_on_status(handler->sline,
274 *locks = lock_ctx->hash;