summaryrefslogtreecommitdiff
path: root/subversion/libsvn_delta/depth_filter_editor.c
blob: b1619792379988a15b675d2bbabd0c844ec1604a (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * depth_filter_editor.c -- provide a svn_delta_editor_t which wraps
 *                          another editor and provides depth-based filtering
 *
 * ====================================================================
 *    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 "svn_delta.h"


/*** Batons, and the Toys That Create Them ***/

struct edit_baton
{
  /* The editor/baton we're wrapping. */
  const svn_delta_editor_t *wrapped_editor;
  void *wrapped_edit_baton;

  /* The depth to which we are limiting the drive of the wrapped
     editor/baton. */
  svn_depth_t requested_depth;

  /* Does the wrapped editor/baton have an explicit target (in the
     anchor/target sense of the word)? */
  svn_boolean_t has_target;
};

struct node_baton
{
  /* TRUE iff this node was filtered out -- that is, not allowed to
     pass through to the wrapped editor -- by virtue of not appearing
     at a depth in the tree that was "inside" the requested depth.  Of
     course, any children of this node will be deeper still, and so
     will also be filtered out for the same reason. */
  svn_boolean_t filtered;

  /* Pointer to the edit_baton. */
  void *edit_baton;

  /* The real node baton we're wrapping.  May be a directory or file
     baton; we don't care. */
  void *wrapped_baton;

  /* The calculated depth (in terms of counted, stacked, integral
     deepnesses) of this node.  If the node is a directory, this value
     is 1 greater than the value of the same on its parent directory;
     if a file, it is equal to its parent directory's depth value. */
  int dir_depth;
};

/* Allocate and return a new node_baton structure, populated via the
   the input to this helper function. */
static struct node_baton *
make_node_baton(void *edit_baton,
                svn_boolean_t filtered,
                int dir_depth,
                apr_pool_t *pool)
{
  struct node_baton *b = apr_palloc(pool, sizeof(*b));
  b->edit_baton = edit_baton;
  b->wrapped_baton = NULL;
  b->filtered = filtered;
  b->dir_depth = dir_depth;
  return b;
}

/* Return TRUE iff changes to immediate children of the directory
   identified by PB, when those children are of node kind KIND, are
   allowed by the requested depth which this editor is trying to
   preserve.  EB is the edit baton.  */
static svn_boolean_t
okay_to_edit(struct edit_baton *eb,
             struct node_baton *pb,
             svn_node_kind_t kind)
{
  int effective_depth;

  /* If we've already filter out the parent directory, we necessarily
     are filtering out its children, too.  */
  if (pb->filtered)
    return FALSE;

  /* Calculate the effective depth of the parent directory.

     NOTE:  "Depth" in this sense is not the same as the Subversion
     magic depth keywords.  Here, we're talking about a literal,
     integral, stacked depth of directories.

     The root of the edit is generally depth=1, subdirectories thereof
     depth=2, and so on.  But if we have an edit target -- which means
     that the real target of the edit operation isn't the root
     directory, but is instead some immediate child thereof -- we have
     to adjust our calculated effected depth such that the target
     itself is depth=1 (as are its siblings, which we trust aren't
     present in the edit at all), immediate subdirectories thereof are
     depth=2, and so on.
  */
  effective_depth = pb->dir_depth - (eb->has_target ? 1 : 0);
  switch (eb->requested_depth)
    {
    case svn_depth_empty:
      return (effective_depth <= 0);
    case svn_depth_files:
      return ((effective_depth <= 0)
              || (kind == svn_node_file && effective_depth == 1));
    case svn_depth_immediates:
      return (effective_depth <= 1);
    case svn_depth_unknown:
    case svn_depth_exclude:
    case svn_depth_infinity:
      /* Shouldn't reach; see svn_delta_depth_filter_editor() */
    default:
      SVN_ERR_MALFUNCTION_NO_RETURN();
    }
}


/*** Editor Functions ***/

static svn_error_t *
set_target_revision(void *edit_baton,
                    svn_revnum_t target_revision,
                    apr_pool_t *pool)
{
  struct edit_baton *eb = edit_baton;

  /* Nothing depth-y to filter here. */
 return eb->wrapped_editor->set_target_revision(eb->wrapped_edit_baton,
                                                target_revision, pool);
}

static svn_error_t *
open_root(void *edit_baton,
          svn_revnum_t base_revision,
          apr_pool_t *pool,
          void **root_baton)
{
  struct edit_baton *eb = edit_baton;
  struct node_baton *b;

  /* The root node always gets through cleanly. */
  b = make_node_baton(edit_baton, FALSE, 1, pool);
  SVN_ERR(eb->wrapped_editor->open_root(eb->wrapped_edit_baton, base_revision,
                                        pool, &b->wrapped_baton));

  *root_baton = b;
  return SVN_NO_ERROR;
}

static svn_error_t *
delete_entry(const char *path,
             svn_revnum_t base_revision,
             void *parent_baton,
             apr_pool_t *pool)
{
  struct node_baton *pb = parent_baton;
  struct edit_baton *eb = pb->edit_baton;

  /* ### FIXME: We don't know the type of the entry, which ordinarily
     doesn't matter, but is a key (*the* key, in fact) distinction
     between depth "files" and depths "immediates".  If the server is
     telling us to delete a subdirectory and our requested depth was
     "immediates", that's fine; if our requested depth was "files",
     though, this deletion shouldn't survive filtering.  For now,
     we'll claim to our helper function that the to-be-deleted thing
     is a file because that's the conservative route to take. */
  if (okay_to_edit(eb, pb, svn_node_file))
    SVN_ERR(eb->wrapped_editor->delete_entry(path, base_revision,
                                             pb->wrapped_baton, pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
add_directory(const char *path,
              void *parent_baton,
              const char *copyfrom_path,
              svn_revnum_t copyfrom_revision,
              apr_pool_t *pool,
              void **child_baton)
{
  struct node_baton *pb = parent_baton;
  struct edit_baton *eb = pb->edit_baton;
  struct node_baton *b = NULL;

  /* Check for sufficient depth. */
  if (okay_to_edit(eb, pb, svn_node_dir))
    {
      b = make_node_baton(eb, FALSE, pb->dir_depth + 1, pool);
      SVN_ERR(eb->wrapped_editor->add_directory(path, pb->wrapped_baton,
                                                copyfrom_path,
                                                copyfrom_revision,
                                                pool, &b->wrapped_baton));
    }
  else
    {
      b = make_node_baton(eb, TRUE, pb->dir_depth + 1, pool);
    }

  *child_baton = b;
  return SVN_NO_ERROR;
}

static svn_error_t *
open_directory(const char *path,
               void *parent_baton,
               svn_revnum_t base_revision,
               apr_pool_t *pool,
               void **child_baton)
{
  struct node_baton *pb = parent_baton;
  struct edit_baton *eb = pb->edit_baton;
  struct node_baton *b;

  /* Check for sufficient depth. */
  if (okay_to_edit(eb, pb, svn_node_dir))
    {
      b = make_node_baton(eb, FALSE, pb->dir_depth + 1, pool);
      SVN_ERR(eb->wrapped_editor->open_directory(path, pb->wrapped_baton,
                                                 base_revision, pool,
                                                 &b->wrapped_baton));
    }
  else
    {
      b = make_node_baton(eb, TRUE, pb->dir_depth + 1, pool);
    }

  *child_baton = b;
  return SVN_NO_ERROR;
}

static svn_error_t *
add_file(const char *path,
         void *parent_baton,
         const char *copyfrom_path,
         svn_revnum_t copyfrom_revision,
         apr_pool_t *pool,
         void **child_baton)
{
  struct node_baton *pb = parent_baton;
  struct edit_baton *eb = pb->edit_baton;
  struct node_baton *b = NULL;

  /* Check for sufficient depth. */
  if (okay_to_edit(eb, pb, svn_node_file))
    {
      b = make_node_baton(eb, FALSE, pb->dir_depth, pool);
      SVN_ERR(eb->wrapped_editor->add_file(path, pb->wrapped_baton,
                                           copyfrom_path, copyfrom_revision,
                                           pool, &b->wrapped_baton));
    }
  else
    {
      b = make_node_baton(eb, TRUE, pb->dir_depth, pool);
    }

  *child_baton = b;
  return SVN_NO_ERROR;
}

static svn_error_t *
open_file(const char *path,
          void *parent_baton,
          svn_revnum_t base_revision,
          apr_pool_t *pool,
          void **child_baton)
{
  struct node_baton *pb = parent_baton;
  struct edit_baton *eb = pb->edit_baton;
  struct node_baton *b;

  /* Check for sufficient depth. */
  if (okay_to_edit(eb, pb, svn_node_file))
    {
      b = make_node_baton(eb, FALSE, pb->dir_depth, pool);
      SVN_ERR(eb->wrapped_editor->open_file(path, pb->wrapped_baton,
                                            base_revision, pool,
                                            &b->wrapped_baton));
    }
  else
    {
      b = make_node_baton(eb, TRUE, pb->dir_depth, pool);
    }

  *child_baton = b;
  return SVN_NO_ERROR;
}

static svn_error_t *
apply_textdelta(void *file_baton,
                const char *base_checksum,
                apr_pool_t *pool,
                svn_txdelta_window_handler_t *handler,
                void **handler_baton)
{
  struct node_baton *fb = file_baton;
  struct edit_baton *eb = fb->edit_baton;

  /* For filtered files, we just consume the textdelta. */
  if (fb->filtered)
    {
      *handler = svn_delta_noop_window_handler;
      *handler_baton = NULL;
    }
  else
    {
      SVN_ERR(eb->wrapped_editor->apply_textdelta(fb->wrapped_baton,
                                                  base_checksum, pool,
                                                  handler, handler_baton));
    }
  return SVN_NO_ERROR;
}

static svn_error_t *
close_file(void *file_baton,
           const char *text_checksum,
           apr_pool_t *pool)
{
  struct node_baton *fb = file_baton;
  struct edit_baton *eb = fb->edit_baton;

  /* Don't close filtered files. */
  if (! fb->filtered)
    SVN_ERR(eb->wrapped_editor->close_file(fb->wrapped_baton,
                                           text_checksum, pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
absent_file(const char *path,
            void *parent_baton,
            apr_pool_t *pool)
{
  struct node_baton *pb = parent_baton;
  struct edit_baton *eb = pb->edit_baton;

  /* Don't report absent items in filtered directories. */
  if (! pb->filtered)
    SVN_ERR(eb->wrapped_editor->absent_file(path, pb->wrapped_baton, pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
close_directory(void *dir_baton,
                apr_pool_t *pool)
{
  struct node_baton *db = dir_baton;
  struct edit_baton *eb = db->edit_baton;

  /* Don't close filtered directories. */
  if (! db->filtered)
    SVN_ERR(eb->wrapped_editor->close_directory(db->wrapped_baton, pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
absent_directory(const char *path,
                 void *parent_baton,
                 apr_pool_t *pool)
{
  struct node_baton *pb = parent_baton;
  struct edit_baton *eb = pb->edit_baton;

  /* Don't report absent items in filtered directories. */
  if (! pb->filtered)
    SVN_ERR(eb->wrapped_editor->absent_directory(path, pb->wrapped_baton,
                                                 pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
change_file_prop(void *file_baton,
                 const char *name,
                 const svn_string_t *value,
                 apr_pool_t *pool)
{
  struct node_baton *fb = file_baton;
  struct edit_baton *eb = fb->edit_baton;

  /* No propchanges on filtered files. */
  if (! fb->filtered)
    SVN_ERR(eb->wrapped_editor->change_file_prop(fb->wrapped_baton,
                                                 name, value, pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
change_dir_prop(void *dir_baton,
                const char *name,
                const svn_string_t *value,
                apr_pool_t *pool)
{
  struct node_baton *db = dir_baton;
  struct edit_baton *eb = db->edit_baton;

  /* No propchanges on filtered nodes. */
  if (! db->filtered)
    SVN_ERR(eb->wrapped_editor->change_dir_prop(db->wrapped_baton,
                                                name, value, pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
close_edit(void *edit_baton,
           apr_pool_t *pool)
{
  struct edit_baton *eb = edit_baton;
  return eb->wrapped_editor->close_edit(eb->wrapped_edit_baton, pool);
}

svn_error_t *
svn_delta_depth_filter_editor(const svn_delta_editor_t **editor,
                              void **edit_baton,
                              const svn_delta_editor_t *wrapped_editor,
                              void *wrapped_edit_baton,
                              svn_depth_t requested_depth,
                              svn_boolean_t has_target,
                              apr_pool_t *pool)
{
  svn_delta_editor_t *depth_filter_editor;
  struct edit_baton *eb;

  /* Easy out: if the caller wants infinite depth, there's nothing to
     filter, so just return the editor we were supposed to wrap.  And
     if they've asked for an unknown depth, we can't possibly know
     what that means, so why bother?  */
  if ((requested_depth == svn_depth_unknown)
      || (requested_depth == svn_depth_infinity))
    {
      *editor = wrapped_editor;
      *edit_baton = wrapped_edit_baton;
      return SVN_NO_ERROR;
    }

  depth_filter_editor = svn_delta_default_editor(pool);
  depth_filter_editor->set_target_revision = set_target_revision;
  depth_filter_editor->open_root = open_root;
  depth_filter_editor->delete_entry = delete_entry;
  depth_filter_editor->add_directory = add_directory;
  depth_filter_editor->open_directory = open_directory;
  depth_filter_editor->change_dir_prop = change_dir_prop;
  depth_filter_editor->close_directory = close_directory;
  depth_filter_editor->absent_directory = absent_directory;
  depth_filter_editor->add_file = add_file;
  depth_filter_editor->open_file = open_file;
  depth_filter_editor->apply_textdelta = apply_textdelta;
  depth_filter_editor->change_file_prop = change_file_prop;
  depth_filter_editor->close_file = close_file;
  depth_filter_editor->absent_file = absent_file;
  depth_filter_editor->close_edit = close_edit;

  eb = apr_palloc(pool, sizeof(*eb));
  eb->wrapped_editor = wrapped_editor;
  eb->wrapped_edit_baton = wrapped_edit_baton;
  eb->has_target = has_target;
  eb->requested_depth = requested_depth;

  *editor = depth_filter_editor;
  *edit_baton = eb;

  return SVN_NO_ERROR;
}