1/*
2 * get_deleted_rev.c :  ra_serf get_deleted_rev API implementation.
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#include "svn_ra.h"
26#include "svn_xml.h"
27#include "svn_path.h"
28#include "svn_private_config.h"
29
30#include "../libsvn_ra/ra_loader.h"
31
32#include "ra_serf.h"
33
34
35/*
36 * This enum represents the current state of our XML parsing for a REPORT.
37 */
38enum drev_state_e {
39  INITIAL = XML_STATE_INITIAL,
40  REPORT,
41  VERSION_NAME
42};
43
44typedef struct drev_context_t {
45  const char *path;
46  svn_revnum_t peg_revision;
47  svn_revnum_t end_revision;
48
49  /* What revision was PATH@PEG_REVISION first deleted within
50     the range PEG_REVISION-END-END_REVISION? */
51  svn_revnum_t *revision_deleted;
52
53} drev_context_t;
54
55#define D_ "DAV:"
56#define S_ SVN_XML_NAMESPACE
57static const svn_ra_serf__xml_transition_t getdrev_ttable[] = {
58  { INITIAL, S_, "get-deleted-rev-report", REPORT,
59    FALSE, { NULL }, FALSE },
60
61  { REPORT, D_, SVN_DAV__VERSION_NAME, VERSION_NAME,
62    TRUE, { NULL }, TRUE },
63
64  { 0 }
65};
66
67
68/* Conforms to svn_ra_serf__xml_closed_t  */
69static svn_error_t *
70getdrev_closed(svn_ra_serf__xml_estate_t *xes,
71               void *baton,
72               int leaving_state,
73               const svn_string_t *cdata,
74               apr_hash_t *attrs,
75               apr_pool_t *scratch_pool)
76{
77  drev_context_t *drev_ctx = baton;
78  apr_int64_t rev;
79
80  SVN_ERR_ASSERT(leaving_state == VERSION_NAME);
81  SVN_ERR_ASSERT(cdata != NULL);
82
83  SVN_ERR(svn_cstring_atoi64(&rev, cdata->data));
84  *drev_ctx->revision_deleted = (svn_revnum_t)rev;
85
86  return SVN_NO_ERROR;
87}
88
89
90/* Implements svn_ra_serf__request_body_delegate_t */
91static svn_error_t *
92create_getdrev_body(serf_bucket_t **body_bkt,
93                    void *baton,
94                    serf_bucket_alloc_t *alloc,
95                    apr_pool_t *pool /* request pool */,
96                    apr_pool_t *scratch_pool)
97{
98  serf_bucket_t *buckets;
99  drev_context_t *drev_ctx = baton;
100
101  buckets = serf_bucket_aggregate_create(alloc);
102
103  svn_ra_serf__add_open_tag_buckets(buckets, alloc,
104                                    "S:get-deleted-rev-report",
105                                    "xmlns:S", SVN_XML_NAMESPACE,
106                                    "xmlns:D", "DAV:",
107                                    SVN_VA_NULL);
108
109  svn_ra_serf__add_tag_buckets(buckets,
110                               "S:path", drev_ctx->path,
111                               alloc);
112
113  svn_ra_serf__add_tag_buckets(buckets,
114                               "S:peg-revision",
115                               apr_ltoa(pool, drev_ctx->peg_revision),
116                               alloc);
117
118  svn_ra_serf__add_tag_buckets(buckets,
119                               "S:end-revision",
120                               apr_ltoa(pool, drev_ctx->end_revision),
121                               alloc);
122
123  svn_ra_serf__add_close_tag_buckets(buckets, alloc,
124                                     "S:get-deleted-rev-report");
125
126  *body_bkt = buckets;
127  return SVN_NO_ERROR;
128}
129
130svn_error_t *
131svn_ra_serf__get_deleted_rev(svn_ra_session_t *session,
132                             const char *path,
133                             svn_revnum_t peg_revision,
134                             svn_revnum_t end_revision,
135                             svn_revnum_t *revision_deleted,
136                             apr_pool_t *pool)
137{
138  drev_context_t *drev_ctx;
139  svn_ra_serf__session_t *ras = session->priv;
140  svn_ra_serf__handler_t *handler;
141  svn_ra_serf__xml_context_t *xmlctx;
142  const char *req_url;
143  svn_error_t *err;
144
145  drev_ctx = apr_pcalloc(pool, sizeof(*drev_ctx));
146  drev_ctx->path = path;
147  drev_ctx->peg_revision = peg_revision;
148  drev_ctx->end_revision = end_revision;
149  drev_ctx->revision_deleted = revision_deleted;
150
151  SVN_ERR(svn_ra_serf__get_stable_url(&req_url, NULL /* latest_revnum */,
152                                      ras, NULL /* url */, peg_revision,
153                                      pool, pool));
154
155  xmlctx = svn_ra_serf__xml_context_create(getdrev_ttable,
156                                           NULL, getdrev_closed, NULL,
157                                           drev_ctx,
158                                           pool);
159  handler = svn_ra_serf__create_expat_handler(ras, xmlctx, NULL, pool);
160
161  handler->method = "REPORT";
162  handler->path = req_url;
163  handler->body_type = "text/xml";
164  handler->body_delegate = create_getdrev_body;
165  handler->body_delegate_baton = drev_ctx;
166
167  err = svn_ra_serf__context_run_one(handler, pool);
168
169  /* Map status 501: Method Not Implemented to our not implemented error.
170     1.5.x servers and older don't support this report. */
171  if (handler->sline.code == 501)
172    return svn_error_createf(SVN_ERR_RA_NOT_IMPLEMENTED, err,
173                             _("'%s' REPORT not implemented"),
174                             "get-deleted-rev");
175  SVN_ERR(err);
176
177  return SVN_NO_ERROR;
178}
179