2 * locks.c : entry point for locking 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 * ====================================================================
30 #include "svn_pools.h"
33 #include "../libsvn_ra/ra_loader.h"
34 #include "svn_config.h"
37 #include "svn_private_config.h"
43 * This enum represents the current state of our XML parsing for a REPORT.
62 typedef struct lock_info_t {
70 svn_revnum_t revision;
72 svn_boolean_t read_headers;
74 svn_ra_serf__handler_t *handler;
76 /* The expat handler. We wrap this to do a bit more work. */
77 svn_ra_serf__response_handler_t inner_handler;
83 #define S_ SVN_XML_NAMESPACE
84 static const svn_ra_serf__xml_transition_t locks_ttable[] = {
85 /* The INITIAL state can transition into D:prop (LOCK) or
86 to D:multistatus (PROPFIND) */
87 { INITIAL, D_, "prop", PROP,
88 FALSE, { NULL }, FALSE },
89 { INITIAL, D_, "multistatus", MULTISTATUS,
90 FALSE, { NULL }, FALSE },
92 { MULTISTATUS, D_, "response", RESPONSE,
93 FALSE, { NULL }, FALSE },
95 { RESPONSE, D_, "propstat", PROPSTAT,
96 FALSE, { NULL }, FALSE },
98 { PROPSTAT, D_, "prop", PROP,
99 FALSE, { NULL }, FALSE },
101 { PROP, D_, "lockdiscovery", LOCK_DISCOVERY,
102 FALSE, { NULL }, FALSE },
104 { LOCK_DISCOVERY, D_, "activelock", ACTIVE_LOCK,
105 FALSE, { NULL }, FALSE },
108 /* ### we don't really need to parse locktype/lockscope. we know what
109 ### the values are going to be. we *could* validate that the only
110 ### possible children are D:write and D:exclusive. we'd need to
111 ### modify the state transition to tell us about all children
112 ### (ie. maybe support "*" for the name) and then validate. but it
113 ### just isn't important to validate, so disable this for now... */
115 { ACTIVE_LOCK, D_, "locktype", LOCK_TYPE,
116 FALSE, { NULL }, FALSE },
118 { LOCK_TYPE, D_, "write", WRITE,
119 FALSE, { NULL }, TRUE },
121 { ACTIVE_LOCK, D_, "lockscope", LOCK_SCOPE,
122 FALSE, { NULL }, FALSE },
124 { LOCK_SCOPE, D_, "exclusive", EXCLUSIVE,
125 FALSE, { NULL }, TRUE },
128 { ACTIVE_LOCK, D_, "timeout", TIMEOUT,
129 TRUE, { NULL }, TRUE },
131 { ACTIVE_LOCK, D_, "locktoken", LOCK_TOKEN,
132 FALSE, { NULL }, FALSE },
134 { LOCK_TOKEN, D_, "href", HREF,
135 TRUE, { NULL }, TRUE },
137 { ACTIVE_LOCK, D_, "owner", OWNER,
138 TRUE, { NULL }, TRUE },
140 /* ACTIVE_LOCK has a D:depth child, but we can ignore that. */
146 /* Conforms to svn_ra_serf__xml_closed_t */
148 locks_closed(svn_ra_serf__xml_estate_t *xes,
151 const svn_string_t *cdata,
153 apr_pool_t *scratch_pool)
155 lock_info_t *lock_ctx = baton;
157 if (leaving_state == TIMEOUT)
159 if (strcmp(cdata->data, "Infinite") == 0)
160 lock_ctx->lock->expiration_date = 0;
162 SVN_ERR(svn_time_from_cstring(&lock_ctx->lock->creation_date,
163 cdata->data, lock_ctx->pool));
165 else if (leaving_state == HREF)
169 char *buf = apr_pstrmemdup(lock_ctx->pool, cdata->data, cdata->len);
171 apr_collapse_spaces(buf, buf);
172 lock_ctx->lock->token = buf;
175 else if (leaving_state == OWNER)
179 lock_ctx->lock->comment = apr_pstrmemdup(lock_ctx->pool,
180 cdata->data, cdata->len);
189 set_lock_headers(serf_bucket_t *headers,
193 lock_info_t *lock_ctx = baton;
197 serf_bucket_headers_set(headers, SVN_DAV_OPTIONS_HEADER,
198 SVN_DAV_OPTION_LOCK_STEAL);
201 if (SVN_IS_VALID_REVNUM(lock_ctx->revision))
203 serf_bucket_headers_set(headers, SVN_DAV_VERSION_NAME_HEADER,
204 apr_ltoa(pool, lock_ctx->revision));
211 /* Register an error within the session. If something is already there,
212 then it will take precedence. */
214 determine_error(svn_ra_serf__handler_t *handler,
218 apr_status_t errcode;
220 if (handler->sline.code == 423)
221 errcode = SVN_ERR_FS_PATH_ALREADY_LOCKED;
222 else if (handler->sline.code == 403)
223 errcode = SVN_ERR_RA_DAV_FORBIDDEN;
227 /* Client-side or server-side error already. Return it. */
231 /* The server did not send us a detailed human-readable error.
232 Provide a generic error. */
233 err = svn_error_createf(errcode, NULL,
234 _("Lock request failed: %d %s"),
236 handler->sline.reason);
243 /* Implements svn_ra_serf__response_handler_t */
245 handle_lock(serf_request_t *request,
246 serf_bucket_t *response,
250 lock_info_t *ctx = handler_baton;
252 /* 403 (Forbidden) when a lock doesn't exist.
253 423 (Locked) when a lock already exists. */
254 if (ctx->handler->sline.code == 403
255 || ctx->handler->sline.code == 423)
257 /* Go look in the body for a server-provided error. This will
258 reset flags for the core handler to Do The Right Thing. We
259 won't be back to this handler again. */
260 return svn_error_trace(svn_ra_serf__expect_empty_body(
261 request, response, ctx->handler, pool));
264 if (!ctx->read_headers)
266 serf_bucket_t *headers;
269 headers = serf_bucket_response_get_headers(response);
271 val = serf_bucket_headers_get(headers, SVN_DAV_LOCK_OWNER_HEADER);
274 ctx->lock->owner = apr_pstrdup(ctx->pool, val);
277 val = serf_bucket_headers_get(headers, SVN_DAV_CREATIONDATE_HEADER);
280 SVN_ERR(svn_time_from_cstring(&ctx->lock->creation_date, val,
284 ctx->read_headers = TRUE;
287 return ctx->inner_handler(request, response, ctx->inner_baton, pool);
290 /* Implements svn_ra_serf__request_body_delegate_t */
292 create_getlock_body(serf_bucket_t **body_bkt,
294 serf_bucket_alloc_t *alloc,
297 serf_bucket_t *buckets;
299 buckets = serf_bucket_aggregate_create(alloc);
301 svn_ra_serf__add_xml_header_buckets(buckets, alloc);
302 svn_ra_serf__add_open_tag_buckets(buckets, alloc, "propfind",
305 svn_ra_serf__add_open_tag_buckets(buckets, alloc, "prop", NULL);
306 svn_ra_serf__add_tag_buckets(buckets, "lockdiscovery", NULL, alloc);
307 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "prop");
308 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "propfind");
315 setup_getlock_headers(serf_bucket_t *headers,
319 serf_bucket_headers_setn(headers, "Depth", "0");
324 /* Implements svn_ra_serf__request_body_delegate_t */
326 create_lock_body(serf_bucket_t **body_bkt,
328 serf_bucket_alloc_t *alloc,
331 lock_info_t *ctx = baton;
332 serf_bucket_t *buckets;
334 buckets = serf_bucket_aggregate_create(alloc);
336 svn_ra_serf__add_xml_header_buckets(buckets, alloc);
337 svn_ra_serf__add_open_tag_buckets(buckets, alloc, "lockinfo",
341 svn_ra_serf__add_open_tag_buckets(buckets, alloc, "lockscope", NULL);
342 svn_ra_serf__add_tag_buckets(buckets, "exclusive", NULL, alloc);
343 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "lockscope");
345 svn_ra_serf__add_open_tag_buckets(buckets, alloc, "locktype", NULL);
346 svn_ra_serf__add_tag_buckets(buckets, "write", NULL, alloc);
347 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "locktype");
349 if (ctx->lock->comment)
351 svn_ra_serf__add_tag_buckets(buckets, "owner", ctx->lock->comment,
355 svn_ra_serf__add_close_tag_buckets(buckets, alloc, "lockinfo");
362 svn_ra_serf__get_lock(svn_ra_session_t *ra_session,
367 svn_ra_serf__session_t *session = ra_session->priv;
368 svn_ra_serf__handler_t *handler;
369 svn_ra_serf__xml_context_t *xmlctx;
370 lock_info_t *lock_ctx;
374 req_url = svn_path_url_add_component2(session->session_url.path, path, pool);
376 lock_ctx = apr_pcalloc(pool, sizeof(*lock_ctx));
378 lock_ctx->pool = pool;
379 lock_ctx->path = req_url;
380 lock_ctx->lock = svn_lock_create(pool);
381 lock_ctx->lock->path = apr_pstrdup(pool, path); /* be sure */
383 xmlctx = svn_ra_serf__xml_context_create(locks_ttable,
384 NULL, locks_closed, NULL,
387 handler = svn_ra_serf__create_expat_handler(xmlctx, pool);
389 handler->method = "PROPFIND";
390 handler->path = req_url;
391 handler->body_type = "text/xml";
392 handler->conn = session->conns[0];
393 handler->session = session;
395 handler->body_delegate = create_getlock_body;
396 handler->body_delegate_baton = lock_ctx;
398 handler->header_delegate = setup_getlock_headers;
399 handler->header_delegate_baton = lock_ctx;
401 lock_ctx->inner_handler = handler->response_handler;
402 lock_ctx->inner_baton = handler->response_baton;
403 handler->response_handler = handle_lock;
404 handler->response_baton = lock_ctx;
406 lock_ctx->handler = handler;
408 err = svn_ra_serf__context_run_one(handler, pool);
409 err = determine_error(handler, err);
411 if (handler->sline.code == 404)
413 return svn_error_create(SVN_ERR_RA_ILLEGAL_URL, err,
414 _("Malformed URL for repository"));
418 /* TODO Shh. We're telling a white lie for now. */
419 return svn_error_create(SVN_ERR_RA_NOT_IMPLEMENTED, err,
420 _("Server does not support locking features"));
423 *lock = lock_ctx->lock;
429 svn_ra_serf__lock(svn_ra_session_t *ra_session,
430 apr_hash_t *path_revs,
433 svn_ra_lock_callback_t lock_func,
435 apr_pool_t *scratch_pool)
437 svn_ra_serf__session_t *session = ra_session->priv;
438 apr_hash_index_t *hi;
439 apr_pool_t *iterpool;
441 iterpool = svn_pool_create(scratch_pool);
443 /* ### TODO for issue 2263: Send all the locks over the wire at once. This
444 ### loop is just a temporary shim.
445 ### an alternative, which is backwards-compat with all servers is to
446 ### pipeline these requests. ie. stop using run_wait/run_one. */
448 for (hi = apr_hash_first(scratch_pool, path_revs);
450 hi = apr_hash_next(hi))
452 svn_ra_serf__handler_t *handler;
453 svn_ra_serf__xml_context_t *xmlctx;
455 lock_info_t *lock_ctx;
457 svn_error_t *new_err = NULL;
459 svn_pool_clear(iterpool);
461 lock_ctx = apr_pcalloc(iterpool, sizeof(*lock_ctx));
463 lock_ctx->pool = iterpool;
464 lock_ctx->path = svn__apr_hash_index_key(hi);
465 lock_ctx->revision = *((svn_revnum_t*)svn__apr_hash_index_val(hi));
466 lock_ctx->lock = svn_lock_create(iterpool);
467 lock_ctx->lock->path = lock_ctx->path;
468 lock_ctx->lock->comment = comment;
470 lock_ctx->force = force;
471 req_url = svn_path_url_add_component2(session->session_url.path,
472 lock_ctx->path, iterpool);
474 xmlctx = svn_ra_serf__xml_context_create(locks_ttable,
475 NULL, locks_closed, NULL,
478 handler = svn_ra_serf__create_expat_handler(xmlctx, iterpool);
480 handler->method = "LOCK";
481 handler->path = req_url;
482 handler->body_type = "text/xml";
483 handler->conn = session->conns[0];
484 handler->session = session;
486 handler->header_delegate = set_lock_headers;
487 handler->header_delegate_baton = lock_ctx;
489 handler->body_delegate = create_lock_body;
490 handler->body_delegate_baton = lock_ctx;
492 lock_ctx->inner_handler = handler->response_handler;
493 lock_ctx->inner_baton = handler->response_baton;
494 handler->response_handler = handle_lock;
495 handler->response_baton = lock_ctx;
497 lock_ctx->handler = handler;
499 err = svn_ra_serf__context_run_one(handler, iterpool);
500 err = determine_error(handler, err);
503 new_err = lock_func(lock_baton, lock_ctx->path, TRUE, lock_ctx->lock,
505 svn_error_clear(err);
510 svn_pool_destroy(iterpool);
515 struct unlock_context_t {
521 set_unlock_headers(serf_bucket_t *headers,
525 struct unlock_context_t *ctx = baton;
527 serf_bucket_headers_set(headers, "Lock-Token", ctx->token);
530 serf_bucket_headers_set(headers, SVN_DAV_OPTIONS_HEADER,
531 SVN_DAV_OPTION_LOCK_BREAK);
538 svn_ra_serf__unlock(svn_ra_session_t *ra_session,
539 apr_hash_t *path_tokens,
541 svn_ra_lock_callback_t lock_func,
543 apr_pool_t *scratch_pool)
545 svn_ra_serf__session_t *session = ra_session->priv;
546 apr_hash_index_t *hi;
547 apr_pool_t *iterpool;
549 iterpool = svn_pool_create(scratch_pool);
551 /* ### TODO for issue 2263: Send all the locks over the wire at once. This
552 ### loop is just a temporary shim.
553 ### an alternative, which is backwards-compat with all servers is to
554 ### pipeline these requests. ie. stop using run_wait/run_one. */
556 for (hi = apr_hash_first(scratch_pool, path_tokens);
558 hi = apr_hash_next(hi))
560 svn_ra_serf__handler_t *handler;
561 const char *req_url, *path, *token;
562 svn_lock_t *existing_lock = NULL;
563 struct unlock_context_t unlock_ctx;
564 svn_error_t *err = NULL;
565 svn_error_t *new_err = NULL;
568 svn_pool_clear(iterpool);
570 path = svn__apr_hash_index_key(hi);
571 token = svn__apr_hash_index_val(hi);
573 if (force && (!token || token[0] == '\0'))
575 SVN_ERR(svn_ra_serf__get_lock(ra_session, &existing_lock, path,
577 token = existing_lock->token;
580 err = svn_error_createf(SVN_ERR_RA_NOT_LOCKED, NULL,
581 _("'%s' is not locked in the repository"),
587 err2 = lock_func(lock_baton, path, FALSE, NULL, err,
589 svn_error_clear(err);
592 return svn_error_trace(err2);
596 svn_error_clear(err);
603 unlock_ctx.force = force;
604 unlock_ctx.token = apr_pstrcat(iterpool, "<", token, ">", (char *)NULL);
606 req_url = svn_path_url_add_component2(session->session_url.path, path,
609 handler = apr_pcalloc(iterpool, sizeof(*handler));
611 handler->handler_pool = iterpool;
612 handler->method = "UNLOCK";
613 handler->path = req_url;
614 handler->conn = session->conns[0];
615 handler->session = session;
617 handler->header_delegate = set_unlock_headers;
618 handler->header_delegate_baton = &unlock_ctx;
620 handler->response_handler = svn_ra_serf__expect_empty_body;
621 handler->response_baton = handler;
623 SVN_ERR(svn_ra_serf__context_run_one(handler, iterpool));
625 switch (handler->sline.code)
630 /* Api users expect this specific error code to detect failures */
631 err = svn_error_createf(SVN_ERR_FS_LOCK_OWNER_MISMATCH, NULL,
632 _("Unlock request failed: %d %s"),
634 handler->sline.reason);
637 err = svn_error_createf(SVN_ERR_RA_DAV_REQUEST_FAILED, NULL,
638 _("Unlock request failed: %d %s"),
640 handler->sline.reason);
644 new_err = lock_func(lock_baton, path, FALSE, existing_lock, err,
647 svn_error_clear(err);
651 svn_pool_destroy(iterpool);