In the command-line client, forbid
[svn.git] / subversion / libsvn_ra_neon / lock.c
blobd9261837516162e76621c3754d24cde6f34d0947
1 /*
2 * lock.c : routines for managing lock states in the DAV server
4 * ====================================================================
5 * Copyright (c) 2000-2006 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
21 #define APR_WANT_STRFUNC
22 #include <apr_want.h>
24 #include "svn_error.h"
25 #include "svn_pools.h"
26 #include "svn_ra.h"
27 #include "../libsvn_ra/ra_loader.h"
28 #include "svn_path.h"
29 #include "svn_time.h"
30 #include "svn_private_config.h"
32 #include "ra_neon.h"
34 static const svn_ra_neon__xml_elm_t lock_elements[] =
36 /* lockdiscovery elements */
37 { "DAV:", "response", ELEM_response, 0 },
38 { "DAV:", "propstat", ELEM_propstat, 0 },
39 { "DAV:", "status", ELEM_status, SVN_RA_NEON__XML_CDATA },
40 /* extend lockdiscovery elements here;
41 ### Remember to update do_lock() when you change the number of
42 elements here: it contains a hard reference to the next element. */
44 /* lock and lockdiscovery elements */
45 { "DAV:", "prop", ELEM_prop, 0 },
46 { "DAV:", "lockdiscovery", ELEM_lock_discovery, 0 },
47 { "DAV:", "activelock", ELEM_lock_activelock, 0 },
48 { "DAV:", "locktype", ELEM_lock_type, SVN_RA_NEON__XML_CDATA },
49 { "DAV:", "lockscope", ELEM_lock_scope, SVN_RA_NEON__XML_CDATA },
50 { "DAV:", "depth", ELEM_lock_depth, SVN_RA_NEON__XML_CDATA },
51 { "DAV:", "owner", ELEM_lock_owner, SVN_RA_NEON__XML_COLLECT },
52 { "DAV:", "timeout", ELEM_lock_timeout, SVN_RA_NEON__XML_CDATA },
53 { "DAV:", "locktoken", ELEM_lock_token, 0 },
54 { "DAV:", "href", ELEM_href, SVN_RA_NEON__XML_CDATA },
55 { "", "", ELEM_unknown, SVN_RA_NEON__XML_COLLECT },
56 /* extend lock elements here */
58 { NULL }
61 typedef struct
63 svn_stringbuf_t *cdata;
64 apr_pool_t *pool;
65 const svn_ra_neon__xml_elm_t *xml_table;
67 /* lockdiscovery fields */
68 svn_stringbuf_t *href;
69 svn_stringbuf_t *status_line;
71 /* lock and lockdiscovery fields */
72 int parent;
73 svn_stringbuf_t *owner;
74 svn_stringbuf_t *timeout;
75 svn_stringbuf_t *depth;
76 svn_stringbuf_t *token;
77 } lock_baton_t;
79 static svn_error_t *
80 lock_start_element(int *elem, void *baton, int parent,
81 const char *nspace, const char *name, const char **atts)
83 lock_baton_t *b = baton;
84 const svn_ra_neon__xml_elm_t *elm =
85 svn_ra_neon__lookup_xml_elem(b->xml_table, nspace, name);
87 if (! elm)
89 *elem = NE_XML_DECLINE;
90 return SVN_NO_ERROR;
93 /* collect interesting element contents */
94 /* owner, href inside locktoken, depth, timeout */
95 switch (elm->id)
97 case ELEM_lock_owner:
98 case ELEM_lock_timeout:
99 case ELEM_lock_depth:
100 case ELEM_status:
101 b->cdata = svn_stringbuf_create("", b->pool);
102 break;
104 case ELEM_href:
105 if (parent == ELEM_lock_token
106 || parent == ELEM_response)
107 b->cdata = svn_stringbuf_create("", b->pool);
108 break;
110 default:
111 b->cdata = NULL;
114 b->parent = parent;
115 *elem = elm->id;
116 return SVN_NO_ERROR;
119 static svn_error_t *
120 lock_end_element(void *baton, int state, const char *nspace, const char *name)
122 lock_baton_t *b = baton;
124 if (b->cdata)
125 switch (state)
127 case ELEM_lock_owner:
128 b->owner = b->cdata;
129 break;
131 case ELEM_lock_timeout:
132 b->timeout = b->cdata;
133 break;
135 case ELEM_lock_depth:
136 b->depth = b->cdata;
137 break;
139 case ELEM_href:
140 if (b->parent == ELEM_lock_token)
141 b->token = b->cdata;
142 else
143 b->href = b->cdata;
144 break;
146 case ELEM_status:
147 b->status_line = b->cdata;
148 break;
151 b->cdata = NULL;
153 return SVN_NO_ERROR;
156 static svn_error_t *
157 lock_cdata(void *baton, int state, const char *cdata, size_t len)
159 lock_baton_t *b = baton;
161 if (b->cdata)
162 svn_stringbuf_appendbytes(b->cdata, cdata, len);
164 return SVN_NO_ERROR;
168 static svn_error_t *
169 lock_from_baton(svn_lock_t **lock,
170 svn_ra_neon__request_t *req,
171 const char *path,
172 lock_baton_t *lrb, apr_pool_t *pool)
174 const char *val;
175 svn_lock_t *lck = svn_lock_create(pool);
177 if (lrb->token)
178 lck->token = lrb->token->data;
179 else
181 /* No lock */
182 *lock = NULL;
183 return SVN_NO_ERROR;
186 val = ne_get_response_header(req->ne_req, SVN_DAV_CREATIONDATE_HEADER);
187 if (val)
188 SVN_ERR_W(svn_time_from_cstring(&(lck->creation_date), val, pool),
189 _("Invalid creation date header value in response."));
191 val = ne_get_response_header(req->ne_req, SVN_DAV_LOCK_OWNER_HEADER);
192 if (val)
193 lck->owner = apr_pstrdup(pool, val);
194 if (lrb->owner)
195 lck->comment = lrb->owner->data;
196 if (path)
197 lck->path = path;
198 if (lrb->timeout)
200 const char *timeout_str = lrb->timeout->data;
202 if (strcmp(timeout_str, "Infinite") != 0)
204 if (strncmp("Second-", timeout_str, strlen("Second-")) == 0)
206 int time_offset = atoi(&(timeout_str[7]));
208 lck->expiration_date = lck->creation_date
209 + apr_time_from_sec(time_offset);
211 else
212 return svn_error_create(SVN_ERR_RA_DAV_RESPONSE_HEADER_BADNESS,
213 NULL, _("Invalid timeout value"));
215 else
216 lck->expiration_date = 0;
218 *lock = lck;
220 return SVN_NO_ERROR;
223 static svn_error_t *
224 do_lock(svn_lock_t **lock,
225 svn_ra_session_t *session,
226 const char *path,
227 const char *comment,
228 svn_boolean_t force,
229 svn_revnum_t current_rev,
230 apr_pool_t *pool)
232 svn_ra_neon__request_t *req;
233 svn_stringbuf_t *body;
234 ne_uri uri;
235 int code;
236 const char *url;
237 svn_string_t fs_path;
238 ne_xml_parser *lck_parser;
239 svn_ra_neon__session_t *ras = session->priv;
240 lock_baton_t *lrb = apr_pcalloc(pool, sizeof(*lrb));
241 apr_hash_t *extra_headers;
243 /* To begin, we convert the incoming path into an absolute fs-path. */
244 url = svn_path_url_add_component(ras->url->data, path, pool);
245 SVN_ERR(svn_ra_neon__get_baseline_info(NULL, NULL, &fs_path, NULL, ras,
246 url, SVN_INVALID_REVNUM, pool));
248 if (ne_uri_parse(url, &uri) != 0)
250 ne_uri_free(&uri);
251 return svn_error_createf(SVN_ERR_RA_DAV_CREATING_REQUEST, NULL,
252 _("Failed to parse URI '%s'"), url);
255 req = svn_ra_neon__request_create(ras, "LOCK", uri.path, pool);
256 ne_uri_free(&uri);
258 lrb->pool = pool;
259 lrb->xml_table = &(lock_elements[3]);
260 lck_parser = svn_ra_neon__xml_parser_create
261 (req, ne_accept_2xx,
262 lock_start_element, lock_cdata, lock_end_element, lrb);
264 body = svn_stringbuf_createf
265 (req->pool,
266 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" DEBUG_CR
267 "<D:lockinfo xmlns:D=\"DAV:\">" DEBUG_CR
268 " <D:lockscope><D:exclusive /></D:lockscope>" DEBUG_CR
269 " <D:locktype><D:write /></D:locktype>" DEBUG_CR
270 "%s" /* maybe owner */
271 "</D:lockinfo>",
272 (comment
273 ? apr_psprintf(req->pool, " <D:owner>%s</D:owner>" DEBUG_CR, comment)
274 : ""));
276 extra_headers = apr_hash_make(req->pool);
277 svn_ra_neon__set_header(extra_headers, "Depth", "0");
278 svn_ra_neon__set_header(extra_headers, "Timeout", "Infinite");
279 svn_ra_neon__set_header(extra_headers, "Content-Type",
280 "text/xml; charset=\"utf-8\"");
281 if (force)
282 svn_ra_neon__set_header(extra_headers, SVN_DAV_OPTIONS_HEADER,
283 SVN_DAV_OPTION_LOCK_STEAL);
284 if (SVN_IS_VALID_REVNUM(current_rev))
285 svn_ra_neon__set_header(extra_headers, SVN_DAV_VERSION_NAME_HEADER,
286 apr_psprintf(req->pool, "%ld", current_rev));
288 SVN_ERR(svn_ra_neon__request_dispatch(&code, req, extra_headers, body->data,
289 200, 0, pool));
291 /*###FIXME: we never verified whether we have received back the type
292 of lock we requested: was it shared/exclusive? was it write/otherwise?
293 How many did we get back? Only one? */
294 SVN_ERR(lock_from_baton(lock, req, fs_path.data, lrb, pool));
296 svn_ra_neon__request_destroy(req);
298 return SVN_NO_ERROR;
301 svn_error_t *
302 svn_ra_neon__lock(svn_ra_session_t *session,
303 apr_hash_t *path_revs,
304 const char *comment,
305 svn_boolean_t force,
306 svn_ra_lock_callback_t lock_func,
307 void *lock_baton,
308 apr_pool_t *pool)
310 apr_hash_index_t *hi;
311 apr_pool_t *iterpool = svn_pool_create(pool);
312 svn_ra_neon__session_t *ras = session->priv;
313 svn_error_t *ret_err = NULL;
315 /* ### TODO for issue 2263: Send all the locks over the wire at once. This
316 loop is just a temporary shim. */
317 for (hi = apr_hash_first(pool, path_revs); hi; hi = apr_hash_next(hi))
319 svn_lock_t *lock;
320 const void *key;
321 const char *path;
322 void *val;
323 svn_revnum_t *revnum;
324 svn_error_t *err, *callback_err = NULL;
326 svn_pool_clear(iterpool);
328 apr_hash_this(hi, &key, NULL, &val);
329 path = key;
330 revnum = val;
332 err = do_lock(&lock, session, path, comment, force, *revnum, iterpool);
334 if (err && !SVN_ERR_IS_LOCK_ERROR(err))
336 ret_err = err;
337 goto departure;
340 if (lock_func)
341 callback_err = lock_func(lock_baton, path, TRUE, err ? NULL : lock,
342 err, iterpool);
344 svn_error_clear(err);
346 if (callback_err)
348 ret_err = callback_err;
349 goto departure;
354 svn_pool_destroy(iterpool);
356 departure:
357 return svn_ra_neon__maybe_store_auth_info_after_result(ret_err, ras, pool);
361 /* ###TODO for issue 2263: Send all lock tokens to the server at once. */
362 static svn_error_t *
363 do_unlock(svn_ra_session_t *session,
364 const char *path,
365 const char *token,
366 svn_boolean_t force,
367 apr_pool_t *pool)
369 svn_ra_neon__session_t *ras = session->priv;
370 const char *url;
371 const char *url_path;
372 ne_uri uri;
374 apr_hash_t *extra_headers = apr_hash_make(pool);
376 /* Make a neon lock structure containing token and full URL to unlock. */
377 url = svn_path_url_add_component(ras->url->data, path, pool);
378 if (ne_uri_parse(url, &uri) != 0)
380 ne_uri_free(&uri);
381 return svn_error_createf(SVN_ERR_RA_DAV_CREATING_REQUEST, NULL,
382 _("Failed to parse URI '%s'"), url);
385 url_path = apr_pstrdup(pool, uri.path);
386 ne_uri_free(&uri);
387 /* In the case of 'force', we might not have a token at all.
388 Unfortunately, mod_dav insists on having a valid token for
389 UNLOCK requests. That means we need to fetch the token. */
390 if (! token)
392 svn_lock_t *lock;
394 SVN_ERR(svn_ra_neon__get_lock(session, &lock, path, pool));
395 if (! lock)
396 return svn_error_createf(SVN_ERR_RA_NOT_LOCKED, NULL,
397 _("'%s' is not locked in the repository"),
398 path);
399 token = lock->token;
404 apr_hash_set(extra_headers, "Lock-Token", APR_HASH_KEY_STRING,
405 apr_psprintf(pool, "<%s>", token));
406 if (force)
407 apr_hash_set(extra_headers, SVN_DAV_OPTIONS_HEADER, APR_HASH_KEY_STRING,
408 SVN_DAV_OPTION_LOCK_BREAK);
410 return svn_ra_neon__simple_request(NULL, ras, "UNLOCK", url_path,
411 extra_headers, NULL, 204, 0, pool);
415 svn_error_t *
416 svn_ra_neon__unlock(svn_ra_session_t *session,
417 apr_hash_t *path_tokens,
418 svn_boolean_t force,
419 svn_ra_lock_callback_t lock_func,
420 void *lock_baton,
421 apr_pool_t *pool)
423 apr_hash_index_t *hi;
424 apr_pool_t *iterpool = svn_pool_create(pool);
425 svn_ra_neon__session_t *ras = session->priv;
426 svn_error_t *ret_err = NULL;
428 /* ### TODO for issue 2263: Send all the lock tokens over the wire at once.
429 This loop is just a temporary shim. */
430 for (hi = apr_hash_first(pool, path_tokens); hi; hi = apr_hash_next(hi))
432 const void *key;
433 const char *path;
434 void *val;
435 const char *token;
436 svn_error_t *err, *callback_err = NULL;
438 svn_pool_clear(iterpool);
440 apr_hash_this(hi, &key, NULL, &val);
441 path = key;
442 /* Since we can't store NULL values in a hash, we turn "" to
443 NULL here. */
444 if (strcmp(val, "") != 0)
445 token = val;
446 else
447 token = NULL;
449 err = do_unlock(session, path, token, force, iterpool);
451 if (err && !SVN_ERR_IS_UNLOCK_ERROR(err))
453 ret_err = err;
454 goto departure;
457 if (lock_func)
458 callback_err = lock_func(lock_baton, path, FALSE, NULL, err, iterpool);
460 svn_error_clear(err);
462 if (callback_err)
464 ret_err = callback_err;
465 goto departure;
469 svn_pool_destroy(iterpool);
471 departure:
472 return svn_ra_neon__maybe_store_auth_info_after_result(ret_err, ras, pool);
476 svn_error_t *
477 svn_ra_neon__get_lock(svn_ra_session_t *session,
478 svn_lock_t **lock,
479 const char *path,
480 apr_pool_t *pool)
482 const char *url;
483 svn_string_t fs_path;
484 svn_error_t *err;
485 ne_uri uri;
486 svn_ra_neon__session_t *ras = session->priv;
487 lock_baton_t *lrb = apr_pcalloc(pool, sizeof(*lrb));
488 svn_ra_neon__request_t *req;
489 ne_xml_parser *lck_parser;
490 apr_hash_t *extra_headers;
491 static const char *body =
492 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" DEBUG_CR
493 "<D:propfind xmlns:D=\"DAV:\">" DEBUG_CR
494 " <D:prop>" DEBUG_CR
495 " <D:lockdiscovery />" DEBUG_CR
496 " </D:prop>" DEBUG_CR
497 "</D:propfind>";
499 /* To begin, we convert the incoming path into an absolute fs-path. */
500 url = svn_path_url_add_component(ras->url->data, path, pool);
502 err = svn_ra_neon__get_baseline_info(NULL, NULL, &fs_path, NULL, ras,
503 url, SVN_INVALID_REVNUM, pool);
504 SVN_ERR(svn_ra_neon__maybe_store_auth_info_after_result(err, ras, pool));
506 ne_uri_parse(url, &uri);
507 url = apr_pstrdup(pool, uri.path);
508 ne_uri_free(&uri);
510 req = svn_ra_neon__request_create(ras, "PROPFIND", url, pool);
512 lrb->pool = pool;
513 lrb->xml_table = lock_elements;
514 lck_parser = svn_ra_neon__xml_parser_create
515 (req, ne_accept_207, lock_start_element, lock_cdata, lock_end_element, lrb);
517 extra_headers = apr_hash_make(req->pool);
518 svn_ra_neon__set_header(extra_headers, "Depth", "0");
519 svn_ra_neon__set_header(extra_headers, "Content-Type",
520 "text/xml; charset=\"utf-8\"");
522 SVN_ERR_W(svn_ra_neon__request_dispatch(NULL, req, extra_headers, body,
523 200, 207, pool),
524 _("Failed to fetch lock information"));
525 /*###FIXME We assume here we only got one lock response. The WebDAV
526 spec makes no such guarantees. How to make sure we grab the one we need? */
527 SVN_ERR(lock_from_baton(lock, req, fs_path.data, lrb, pool));
529 return SVN_NO_ERROR;