1/* 2 * getlocks.c : entry point for get_locks RA functions for 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 25 26#include <apr_uri.h> 27 28#include <serf.h> 29 30#include "svn_hash.h" 31#include "svn_path.h" 32#include "svn_pools.h" 33#include "svn_ra.h" 34#include "svn_dav.h" 35#include "svn_time.h" 36#include "svn_xml.h" 37 38#include "private/svn_dav_protocol.h" 39#include "private/svn_fspath.h" 40#include "svn_private_config.h" 41 42#include "../libsvn_ra/ra_loader.h" 43 44#include "ra_serf.h" 45 46 47/* 48 * This enum represents the current state of our XML parsing for a REPORT. 49 */ 50enum { 51 INITIAL = 0, 52 REPORT, 53 LOCK, 54 PATH, 55 TOKEN, 56 OWNER, 57 COMMENT, 58 CREATION_DATE, 59 EXPIRATION_DATE 60}; 61 62typedef struct lock_context_t { 63 apr_pool_t *pool; 64 65 /* target and requested depth of the operation. */ 66 const char *path; 67 svn_depth_t requested_depth; 68 69 /* return hash */ 70 apr_hash_t *hash; 71 72} lock_context_t; 73 74#define D_ "DAV:" 75#define S_ SVN_XML_NAMESPACE 76static const svn_ra_serf__xml_transition_t getlocks_ttable[] = { 77 { INITIAL, S_, "get-locks-report", REPORT, 78 FALSE, { NULL }, FALSE }, 79 80 { REPORT, S_, "lock", LOCK, 81 FALSE, { NULL }, TRUE }, 82 83 { LOCK, S_, "path", PATH, 84 TRUE, { NULL }, TRUE }, 85 86 { LOCK, S_, "token", TOKEN, 87 TRUE, { NULL }, TRUE }, 88 89 { LOCK, S_, "owner", OWNER, 90 TRUE, { NULL }, TRUE }, 91 92 { LOCK, S_, "comment", COMMENT, 93 TRUE, { NULL }, TRUE }, 94 95 { LOCK, S_, SVN_DAV__CREATIONDATE, CREATION_DATE, 96 TRUE, { NULL }, TRUE }, 97 98 { LOCK, S_, "expirationdate", EXPIRATION_DATE, 99 TRUE, { NULL }, TRUE }, 100 101 { 0 } 102}; 103 104 105/* Conforms to svn_ra_serf__xml_closed_t */ 106static svn_error_t * 107getlocks_closed(svn_ra_serf__xml_estate_t *xes, 108 void *baton, 109 int leaving_state, 110 const svn_string_t *cdata, 111 apr_hash_t *attrs, 112 apr_pool_t *scratch_pool) 113{ 114 lock_context_t *lock_ctx = baton; 115 116 if (leaving_state == LOCK) 117 { 118 const char *path = svn_hash_gets(attrs, "path"); 119 svn_boolean_t save_lock = FALSE; 120 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 124 this lock if: 125 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. 130 */ 131 if (strcmp(lock_ctx->path, path) == 0 132 || lock_ctx->requested_depth == svn_depth_infinity) 133 { 134 save_lock = TRUE; 135 } 136 else if (lock_ctx->requested_depth == svn_depth_files 137 || lock_ctx->requested_depth == svn_depth_immediates) 138 { 139 const char *relpath = svn_fspath__skip_ancestor(lock_ctx->path, 140 path); 141 if (relpath && (svn_path_component_count(relpath) == 1)) 142 save_lock = TRUE; 143 } 144 145 if (save_lock) 146 { 147 /* We get to put the structure on the stack rather than using 148 svn_lock_create(). Bwahahaha.... */ 149 svn_lock_t lock = { 0 }; 150 const char *date; 151 svn_lock_t *result_lock; 152 153 /* Note: these "attributes" came from child elements. Some of 154 them may have not been sent, so the value will be NULL. */ 155 156 lock.path = path; 157 lock.token = svn_hash_gets(attrs, "token"); 158 lock.owner = svn_hash_gets(attrs, "owner"); 159 lock.comment = svn_hash_gets(attrs, "comment"); 160 161 date = svn_hash_gets(attrs, SVN_DAV__CREATIONDATE); 162 if (date) 163 SVN_ERR(svn_time_from_cstring(&lock.creation_date, date, 164 scratch_pool)); 165 166 date = svn_hash_gets(attrs, "expirationdate"); 167 if (date) 168 SVN_ERR(svn_time_from_cstring(&lock.expiration_date, date, 169 scratch_pool)); 170 171 result_lock = svn_lock_dup(&lock, lock_ctx->pool); 172 svn_hash_sets(lock_ctx->hash, result_lock->path, result_lock); 173 } 174 } 175 else 176 { 177 const char *name; 178 179 SVN_ERR_ASSERT(cdata != NULL); 180 181 if (leaving_state == PATH) 182 name = "path"; 183 else if (leaving_state == TOKEN) 184 name = "token"; 185 else if (leaving_state == OWNER) 186 name = "owner"; 187 else if (leaving_state == COMMENT) 188 name = "comment"; 189 else if (leaving_state == CREATION_DATE) 190 name = SVN_DAV__CREATIONDATE; 191 else if (leaving_state == EXPIRATION_DATE) 192 name = "expirationdate"; 193 else 194 SVN_ERR_MALFUNCTION(); 195 196 /* Store the lock information onto the LOCK elemstate. */ 197 svn_ra_serf__xml_note(xes, LOCK, name, cdata->data); 198 } 199 200 return SVN_NO_ERROR; 201} 202 203 204/* Implements svn_ra_serf__request_body_delegate_t */ 205static svn_error_t * 206create_getlocks_body(serf_bucket_t **body_bkt, 207 void *baton, 208 serf_bucket_alloc_t *alloc, 209 apr_pool_t *pool) 210{ 211 lock_context_t *lock_ctx = baton; 212 serf_bucket_t *buckets; 213 214 buckets = serf_bucket_aggregate_create(alloc); 215 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"); 220 221 *body_bkt = buckets; 222 return SVN_NO_ERROR; 223} 224 225svn_error_t * 226svn_ra_serf__get_locks(svn_ra_session_t *ra_session, 227 apr_hash_t **locks, 228 const char *path, 229 svn_depth_t depth, 230 apr_pool_t *pool) 231{ 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; 237 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, 240 NULL, pool)); 241 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); 247 248 xmlctx = svn_ra_serf__xml_context_create(getlocks_ttable, 249 NULL, getlocks_closed, NULL, 250 lock_ctx, 251 pool); 252 handler = svn_ra_serf__create_expat_handler(xmlctx, pool); 253 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; 259 260 handler->body_delegate = create_getlocks_body; 261 handler->body_delegate_baton = lock_ctx; 262 263 SVN_ERR(svn_ra_serf__context_run_one(handler, pool)); 264 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) 268 { 269 SVN_ERR(svn_ra_serf__error_on_status(handler->sline, 270 handler->path, 271 handler->location)); 272 } 273 274 *locks = lock_ctx->hash; 275 276 return SVN_NO_ERROR; 277} 278