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 const char *token = svn_hash_gets(attrs, "token");
120 svn_boolean_t save_lock = FALSE;
122 /* Filter out unwanted paths. Since Subversion only allows
123 locks on files, we can treat depth=immediates the same as
124 depth=files for filtering purposes. Meaning, we'll keep
127 a) its path is the very path we queried, or
128 b) we've asked for a fully recursive answer, or
129 c) we've asked for depth=files or depth=immediates, and this
130 lock is on an immediate child of our query path.
134 /* A lock without a token is not a lock; just an answer that there
135 is no lock on the node. */
138 if (strcmp(lock_ctx->path, path) == 0
139 || lock_ctx->requested_depth == svn_depth_infinity)
143 else if (lock_ctx->requested_depth == svn_depth_files
144 || lock_ctx->requested_depth == svn_depth_immediates)
146 const char *relpath = svn_fspath__skip_ancestor(lock_ctx->path,
148 if (relpath && (svn_path_component_count(relpath) == 1))
154 /* We get to put the structure on the stack rather than using
155 svn_lock_create(). Bwahahaha.... */
156 svn_lock_t lock = { 0 };
158 svn_lock_t *result_lock;
160 /* Note: these "attributes" came from child elements. Some of
161 them may have not been sent, so the value will be NULL. */
165 lock.owner = svn_hash_gets(attrs, "owner");
166 lock.comment = svn_hash_gets(attrs, "comment");
168 date = svn_hash_gets(attrs, SVN_DAV__CREATIONDATE);
170 SVN_ERR(svn_time_from_cstring(&lock.creation_date, date,
173 date = svn_hash_gets(attrs, "expirationdate");
175 SVN_ERR(svn_time_from_cstring(&lock.expiration_date, date,
178 result_lock = svn_lock_dup(&lock, lock_ctx->pool);
179 svn_hash_sets(lock_ctx->hash, result_lock->path, result_lock);
186 SVN_ERR_ASSERT(cdata != NULL);
188 if (leaving_state == PATH)
190 else if (leaving_state == TOKEN)
192 else if (leaving_state == OWNER)
194 else if (leaving_state == COMMENT)
196 else if (leaving_state == CREATION_DATE)
197 name = SVN_DAV__CREATIONDATE;
198 else if (leaving_state == EXPIRATION_DATE)
199 name = "expirationdate";
201 SVN_ERR_MALFUNCTION();
203 /* Store the lock information onto the LOCK elemstate. */
204 svn_ra_serf__xml_note(xes, LOCK, name, cdata->data);
211 /* Implements svn_ra_serf__request_body_delegate_t */
213 create_getlocks_body(serf_bucket_t **body_bkt,
215 serf_bucket_alloc_t *alloc,
218 lock_context_t *lock_ctx = baton;
219 serf_bucket_t *buckets;
221 buckets = serf_bucket_aggregate_create(alloc);
223 svn_ra_serf__add_open_tag_buckets(
224 buckets, alloc, "S:get-locks-report", "xmlns:S", SVN_XML_NAMESPACE,
225 "depth", svn_depth_to_word(lock_ctx->requested_depth), NULL);
226 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "S:get-locks-report");
233 svn_ra_serf__get_locks(svn_ra_session_t *ra_session,
239 lock_context_t *lock_ctx;
240 svn_ra_serf__session_t *session = ra_session->priv;
241 svn_ra_serf__handler_t *handler;
242 svn_ra_serf__xml_context_t *xmlctx;
243 const char *req_url, *rel_path;
246 req_url = svn_path_url_add_component2(session->session_url.path, path, pool);
247 SVN_ERR(svn_ra_serf__get_relative_path(&rel_path, req_url, session,
250 lock_ctx = apr_pcalloc(pool, sizeof(*lock_ctx));
251 lock_ctx->pool = pool;
252 lock_ctx->path = apr_pstrcat(pool, "/", rel_path, (char *)NULL);
253 lock_ctx->requested_depth = depth;
254 lock_ctx->hash = apr_hash_make(pool);
256 xmlctx = svn_ra_serf__xml_context_create(getlocks_ttable,
257 NULL, getlocks_closed, NULL,
260 handler = svn_ra_serf__create_expat_handler(xmlctx, pool);
262 handler->method = "REPORT";
263 handler->path = req_url;
264 handler->body_type = "text/xml";
265 handler->conn = session->conns[0];
266 handler->session = session;
268 handler->body_delegate = create_getlocks_body;
269 handler->body_delegate_baton = lock_ctx;
271 err = svn_ra_serf__context_run_one(handler, pool);
273 /* Wrap the server generated error for an unsupported report with the
274 documented error for this ra function. */
275 if (svn_error_find_cause(err, SVN_ERR_UNSUPPORTED_FEATURE))
276 err = svn_error_create(SVN_ERR_RA_NOT_IMPLEMENTED, err, NULL);
280 /* We get a 404 when a path doesn't exist in HEAD, but it might
281 have existed earlier (E.g. 'svn ls http://s/svn/trunk/file@1' */
282 if (handler->sline.code != 404)
284 SVN_ERR(svn_ra_serf__error_on_status(handler->sline,
289 *locks = lock_ctx->hash;