summaryrefslogtreecommitdiff
path: root/subversion/mod_dav_svn/reports/list.c
blob: 2602906db15cbe2485f68bab8ca7b2cca3f4b17c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * list.c: mod_dav_svn REPORT handler for recursive directory listings
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

#include <apr_pools.h>
#include <apr_strings.h>
#include <apr_xml.h>

#include <mod_dav.h>

#include "svn_repos.h"
#include "svn_ctype.h"
#include "svn_string.h"
#include "svn_types.h"
#include "svn_base64.h"
#include "svn_xml.h"
#include "svn_path.h"
#include "svn_dav.h"
#include "svn_pools.h"
#include "svn_props.h"
#include "svn_time.h"

#include "private/svn_log.h"
#include "private/svn_fspath.h"

#include "../dav_svn.h"

/* Baton type to be used with list_receiver. */
typedef struct list_receiver_baton_t
{
  /* this buffers the output for a bit and is automatically flushed,
     at appropriate times, by the Apache filter system. */
  apr_bucket_brigade *bb;

  /* where to deliver the output */
  dav_svn__output *output;

  /* Whether we've written the <S:log-report> header.  Allows for lazy
     writes to support mod_dav-based error handling. */
  svn_boolean_t needs_header;

  /* Are we talking to a SVN client? */
  svn_boolean_t is_svn_client;

  /* Helper variables to force early bucket brigade flushes */
  int result_count;
  int next_forced_flush;

  /* Send the field selected by these flags. */
  apr_uint32_t dirent_fields;
} list_receiver_baton_t;


/* If LRB->needs_header is true, send the "<S:list-report>" start
   element and set LRB->needs_header to zero.  Else do nothing.
   This is basically duplicated in file_revs.c.  Consider factoring if
   duplicating again. */
static svn_error_t *
maybe_send_header(list_receiver_baton_t *lrb)
{
  if (lrb->needs_header)
    {
      SVN_ERR(dav_svn__brigade_puts(lrb->bb, lrb->output,
                                    DAV_XML_HEADER DEBUG_CR
                                    "<S:list-report xmlns:S=\""
                                    SVN_XML_NAMESPACE "\" "
                                    "xmlns:D=\"DAV:\">" DEBUG_CR));
      lrb->needs_header = FALSE;
    }

  return SVN_NO_ERROR;
}


/* Implements svn_repos_dirent_receiver_t, sending DIRENT and PATH to the
 * client.  BATON must be a list_receiver_baton_t. */
static svn_error_t *
list_receiver(const char *path,
              svn_dirent_t *dirent,
              void *baton,
              apr_pool_t *pool)
{
  list_receiver_baton_t *b = baton;
  const char *kind = (b->dirent_fields & SVN_DIRENT_KIND)
                   ? svn_node_kind_to_word(dirent->kind)
                   : "unknown";
  const char *attr_size = "";
  const char *attr_has_props = "";
  const char *attr_created_rev = "";
  const char *attr_date = "";
  const char *tag_author = "";

  if (b->dirent_fields & SVN_DIRENT_SIZE)
    attr_size = apr_psprintf(pool, " size=\"%" SVN_FILESIZE_T_FMT "\"",
                             dirent->size);

  if (b->dirent_fields & SVN_DIRENT_HAS_PROPS)
    attr_has_props = dirent->has_props
                   ? " has-props=\"true\""
                   : " has-props=\"false\"";

  if (b->dirent_fields & SVN_DIRENT_CREATED_REV)
    attr_created_rev = apr_psprintf(pool, " created-rev=\"%ld\"",
                                    dirent->created_rev);

  if (b->dirent_fields & SVN_DIRENT_TIME)
    {
      const char *ctime = svn_time_to_cstring(dirent->time, pool);
      attr_date = apr_psprintf(pool, " date=\"%s\"",
                               apr_xml_quote_string(pool, ctime, 0));
    }

  if ((b->dirent_fields & SVN_DIRENT_LAST_AUTHOR) && dirent->last_author)
    {
      const char *author = dav_svn__fuzzy_escape_author(dirent->last_author,
                                                        b->is_svn_client,
                                                        pool, pool);
      tag_author = apr_psprintf(pool,
                                "<D:creator-displayname>%s"
                                "</D:creator-displayname>",
                                apr_xml_quote_string(pool, author, 1));
    }

  SVN_ERR(maybe_send_header(b));

  /* If we need to close the element, then send the attributes
     that apply to all changed items and then close the element. */
  SVN_ERR(dav_svn__brigade_printf(b->bb, b->output,
                                 "<S:item"
                                 " node-kind=\"%s\""
                                 "%s"
                                 "%s"
                                 "%s"
                                 "%s>%s%s</S:item>" DEBUG_CR,
                                 kind,
                                 attr_size,
                                 attr_has_props,
                                 attr_created_rev,
                                 attr_date,
                                 apr_xml_quote_string(pool, path, 0),
                                 tag_author));

  /* In general APR will flush the brigade every 8000 bytes through the filter
     stack, but log items may not be generated that fast, especially in
     combination with authz and busy servers. We now explictly flush after
     direntry 4, 16, 64 and 256 to produce a few results fast.

     This introduces 4 full flushes of our brigade and the installed output
     filters at growing intervals and then falls back to the standard
     buffering of 8000 bytes + whatever buffers are added in output filters. */
  b->result_count++;
  if (b->result_count == b->next_forced_flush)
    {
      apr_bucket *bkt;

      /* Compared to using ap_filter_flush(), which we use in other place
         this adds a flush frame before flushing the brigade, to make output
         filters perform a flush as well */

      /* No brigade empty check. We want output filters to flush anyway */
      bkt = apr_bucket_flush_create(
                dav_svn__output_get_bucket_alloc(b->output));
      APR_BRIGADE_INSERT_TAIL(b->bb, bkt);
      SVN_ERR(dav_svn__output_pass_brigade(b->output, b->bb));

      if (b->result_count < 256)
        b->next_forced_flush = b->next_forced_flush * 4;
    }

  return SVN_NO_ERROR;
}

dav_error *
dav_svn__list_report(const dav_resource *resource,
                     const apr_xml_doc *doc,
                     dav_svn__output *output)
{
  svn_error_t *serr;
  dav_error *derr = NULL;
  apr_xml_elem *child;
  list_receiver_baton_t lrb = { 0 };
  dav_svn__authz_read_baton arb;
  const dav_svn_repos *repos = resource->info->repos;
  int ns;
  const char *full_path = NULL;
  svn_boolean_t path_info_only;
  svn_fs_root_t *root;
  svn_depth_t depth = svn_depth_unknown;

  /* These get determined from the request document. */
  svn_revnum_t rev = SVN_INVALID_REVNUM;     /* defaults to HEAD */
  apr_array_header_t *patterns = NULL;

  /* Sanity check. */
  if (!resource->info->repos_path)
    return dav_svn__new_error(resource->pool, HTTP_BAD_REQUEST, 0, 0,
                              "The request does not specify a repository path");
  ns = dav_svn__find_ns(doc->namespaces, SVN_XML_NAMESPACE);
  if (ns == -1)
    {
      return dav_svn__new_error_svn(resource->pool, HTTP_BAD_REQUEST, 0, 0,
                                    "The request does not contain the 'svn:' "
                                    "namespace, so it is not going to have "
                                    "certain required elements");
    }

  for (child = doc->root->first_child; child != NULL; child = child->next)
    {
      /* if this element isn't one of ours, then skip it */
      if (child->ns != ns)
        continue;

      else if (strcmp(child->name, "path") == 0)
        {
          const char *rel_path = dav_xml_get_cdata(child, resource->pool, 0);
          if ((derr = dav_svn__test_canonical(rel_path, resource->pool)))
            return derr;

          /* Force REL_PATH to be a relative path, not an fspath. */
          rel_path = svn_relpath_canonicalize(rel_path, resource->pool);

          /* Append the REL_PATH to the base FS path to get an
             absolute repository path. */
          full_path = svn_fspath__join(resource->info->repos_path, rel_path,
                                       resource->pool);
        }
      else if (strcmp(child->name, "revision") == 0)
        rev = SVN_STR_TO_REV(dav_xml_get_cdata(child, resource->pool, 1));
      else if (strcmp(child->name, "depth") == 0)
        depth = svn_depth_from_word(dav_xml_get_cdata(child, resource->pool, 1));
      else if (strcmp(child->name, "no-patterns") == 0)
        {
          /* specified but empty pattern list */
          patterns = apr_array_make(resource->pool, 0, sizeof(const char *));
        }
      else if (strcmp(child->name, "pattern") == 0)
        {
          const char *name = dav_xml_get_cdata(child, resource->pool, 0);
          if (!patterns)
            patterns = apr_array_make(resource->pool, 1, sizeof(const char *));
          APR_ARRAY_PUSH(patterns, const char *) = name;
        }
      else if (strcmp(child->name, "prop") == 0)
        {
          const char *name = dav_xml_get_cdata(child, resource->pool, 0);
          if (strcmp(name, "DAV:resourcetype") == 0)
            lrb.dirent_fields |= SVN_DIRENT_KIND;
          else if (strcmp(name, "DAV:getcontentlength") == 0)
            lrb.dirent_fields |= SVN_DIRENT_SIZE;
          else if (strcmp(name, SVN_DAV_PROP_NS_DAV "deadprop-count") == 0)
            lrb.dirent_fields |= SVN_DIRENT_HAS_PROPS;
          else if (strcmp(name, "DAV:" SVN_DAV__VERSION_NAME) == 0)
            lrb.dirent_fields |= SVN_DIRENT_CREATED_REV;
          else if (strcmp(name, "DAV:" SVN_DAV__CREATIONDATE) == 0)
            lrb.dirent_fields |= SVN_DIRENT_TIME;
          else if (strcmp(name, "DAV:creator-displayname") == 0)
            lrb.dirent_fields |= SVN_DIRENT_LAST_AUTHOR;
          else if (strcmp(name, "DAV:allprop") == 0)
            lrb.dirent_fields |= SVN_DIRENT_ALL;
        }
      /* else unknown element; skip it */
    }

  if (! full_path)
    {
      return dav_svn__new_error_svn(resource->pool, HTTP_BAD_REQUEST, 0, 0,
                                    "Request was missing the path argument");
    }

  /* Build authz read baton */
  arb.r = resource->info->r;
  arb.repos = resource->info->repos;

  /* Build log receiver baton */
  lrb.bb = apr_brigade_create(resource->pool,  /* not the subpool! */
                              dav_svn__output_get_bucket_alloc(output));
  lrb.output = output;
  lrb.needs_header = TRUE;
  lrb.next_forced_flush = 4;
  lrb.is_svn_client = resource->info->repos->is_svn_client;

  /* Fetch the root of the appropriate revision. */
  serr = svn_fs_revision_root(&root, repos->fs, rev, resource->pool);
  if (!serr)
    {
      /* Fetch the directory entries if requested and send them immediately. */
      path_info_only = (lrb.dirent_fields & ~SVN_DIRENT_KIND) == 0;
      serr = svn_repos_list(root, full_path, patterns, depth, path_info_only,
                            dav_svn__authz_read_func(&arb), &arb,
                            list_receiver, &lrb, NULL, NULL, resource->pool);
    }

  if (serr)
    {
      derr = dav_svn__convert_err(serr, HTTP_BAD_REQUEST, NULL,
                                  resource->pool);
      goto cleanup;
    }

  if ((serr = maybe_send_header(&lrb)))
    {
      derr = dav_svn__convert_err(serr, HTTP_INTERNAL_SERVER_ERROR,
                                  "Error beginning REPORT response.",
                                  resource->pool);
      goto cleanup;
    }

  if ((serr = dav_svn__brigade_puts(lrb.bb, lrb.output,
                                    "</S:list-report>" DEBUG_CR)))
    {
      derr = dav_svn__convert_err(serr, HTTP_INTERNAL_SERVER_ERROR,
                                  "Error ending REPORT response.",
                                  resource->pool);
      goto cleanup;
    }

 cleanup:

  dav_svn__operational_log(resource->info,
                           svn_log__list(full_path, rev, patterns, depth,
                                         lrb.dirent_fields, resource->pool));

  return dav_svn__final_flush_or_error(resource->info->r, lrb.bb, output,
                                       derr, resource->pool);
}