]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/subversion/subversion/libsvn_repos/reporter.c
Merge ^/vendor/lldb/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / subversion / subversion / libsvn_repos / reporter.c
1 /*
2  * reporter.c : `reporter' vtable routines for updates.
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 #include "svn_dirent_uri.h"
25 #include "svn_hash.h"
26 #include "svn_path.h"
27 #include "svn_types.h"
28 #include "svn_error.h"
29 #include "svn_error_codes.h"
30 #include "svn_fs.h"
31 #include "svn_repos.h"
32 #include "svn_pools.h"
33 #include "svn_props.h"
34 #include "repos.h"
35 #include "svn_private_config.h"
36
37 #include "private/svn_dep_compat.h"
38 #include "private/svn_fspath.h"
39 #include "private/svn_subr_private.h"
40 #include "private/svn_string_private.h"
41
42 #define NUM_CACHED_SOURCE_ROOTS 4
43
44 /* Theory of operation: we write report operations out to a spill-buffer
45    as we receive them.  When the report is finished, we read the
46    operations back out again, using them to guide the progression of
47    the delta between the source and target revs.
48
49    Spill-buffer content format: we use a simple ad-hoc format to store the
50    report operations.  Each report operation is the concatention of
51    the following ("+/-" indicates the single character '+' or '-';
52    <length> and <revnum> are written out as decimal strings):
53
54      +/-                      '-' marks the end of the report
55      If previous is +:
56        <length>:<bytes>       Length-counted path string
57        +/-                    '+' indicates the presence of link_path
58        If previous is +:
59          <length>:<bytes>     Length-counted link_path string
60        +/-                    '+' indicates presence of revnum
61        If previous is +:
62          <revnum>:            Revnum of set_path or link_path
63        +/-                    '+' indicates depth other than svn_depth_infinity
64        If previous is +:
65          <depth>:             "X","E","F","M" =>
66                                  svn_depth_{exclude,empty,files,immediates}
67        +/-                    '+' indicates start_empty field set
68        +/-                    '+' indicates presence of lock_token field.
69        If previous is +:
70          <length>:<bytes>     Length-counted lock_token string
71
72    Terminology: for brevity, this file frequently uses the prefixes
73    "s_" for source, "t_" for target, and "e_" for editor.  Also, to
74    avoid overloading the word "target", we talk about the source
75    "anchor and operand", rather than the usual "anchor and target". */
76
77 /* Describes the state of a working copy subtree, as given by a
78    report.  Because we keep a lookahead pathinfo, we need to allocate
79    each one of these things in a subpool of the report baton and free
80    it when done. */
81 typedef struct path_info_t
82 {
83   const char *path;            /* path, munged to be anchor-relative */
84   const char *link_path;       /* NULL for set_path or delete_path */
85   svn_revnum_t rev;            /* SVN_INVALID_REVNUM for delete_path */
86   svn_depth_t depth;           /* Depth of this path, meaningless for files */
87   svn_boolean_t start_empty;   /* Meaningless for delete_path */
88   const char *lock_token;      /* NULL if no token */
89   apr_pool_t *pool;            /* Container pool */
90 } path_info_t;
91
92 /* Describes the standard revision properties that are relevant for
93    reports.  Since a particular revision will often show up more than
94    once in the report, we cache these properties for the time of the
95    report generation. */
96 typedef struct revision_info_t
97 {
98   svn_revnum_t rev;            /* revision number */
99   svn_string_t* date;          /* revision timestamp */
100   svn_string_t* author;        /* name of the revisions' author */
101 } revision_info_t;
102
103 /* A structure used by the routines within the `reporter' vtable,
104    driven by the client as it describes its working copy revisions. */
105 typedef struct report_baton_t
106 {
107   /* Parameters remembered from svn_repos_begin_report3 */
108   svn_repos_t *repos;
109   const char *fs_base;         /* fspath corresponding to wc anchor */
110   const char *s_operand;       /* anchor-relative wc target (may be empty) */
111   svn_revnum_t t_rev;          /* Revnum which the edit will bring the wc to */
112   const char *t_path;          /* FS path the edit will bring the wc to */
113   svn_boolean_t text_deltas;   /* Whether to report text deltas */
114   apr_size_t zero_copy_limit;  /* Max item size that will be sent using
115                                   the zero-copy code path. */
116
117   /* If the client requested a specific depth, record it here; if the
118      client did not, then this is svn_depth_unknown, and the depth of
119      information transmitted from server to client will be governed
120      strictly by the path-associated depths recorded in the report. */
121   svn_depth_t requested_depth;
122
123   svn_boolean_t ignore_ancestry;
124   svn_boolean_t send_copyfrom_args;
125   svn_boolean_t is_switch;
126   const svn_delta_editor_t *editor;
127   void *edit_baton;
128   svn_repos_authz_func_t authz_read_func;
129   void *authz_read_baton;
130
131   /* The spill-buffer holding the report. */
132   svn_spillbuf_reader_t *reader;
133
134   /* For the actual editor drive, we'll need a lookahead path info
135      entry, a cache of FS roots, and a pool to store them. */
136   path_info_t *lookahead;
137   svn_fs_root_t *t_root;
138   svn_fs_root_t *s_roots[NUM_CACHED_SOURCE_ROOTS];
139
140   /* Cache for revision properties. This is used to eliminate redundant
141      revprop fetching. */
142   apr_hash_t *revision_infos;
143
144   /* This will not change. So, fetch it once and reuse it. */
145   svn_string_t *repos_uuid;
146   apr_pool_t *pool;
147 } report_baton_t;
148
149 /* The type of a function that accepts changes to an object's property
150    list.  OBJECT is the object whose properties are being changed.
151    NAME is the name of the property to change.  VALUE is the new value
152    for the property, or zero if the property should be deleted. */
153 typedef svn_error_t *proplist_change_fn_t(report_baton_t *b, void *object,
154                                           const char *name,
155                                           const svn_string_t *value,
156                                           apr_pool_t *pool);
157
158 static svn_error_t *delta_dirs(report_baton_t *b, svn_revnum_t s_rev,
159                                const char *s_path, const char *t_path,
160                                void *dir_baton, const char *e_path,
161                                svn_boolean_t start_empty,
162                                svn_depth_t wc_depth,
163                                svn_depth_t requested_depth,
164                                apr_pool_t *pool);
165
166 /* --- READING PREVIOUSLY STORED REPORT INFORMATION --- */
167
168 static svn_error_t *
169 read_number(apr_uint64_t *num, svn_spillbuf_reader_t *reader, apr_pool_t *pool)
170 {
171   char c;
172
173   *num = 0;
174   while (1)
175     {
176       SVN_ERR(svn_spillbuf__reader_getc(&c, reader, pool));
177       if (c == ':')
178         break;
179       *num = *num * 10 + (c - '0');
180     }
181   return SVN_NO_ERROR;
182 }
183
184 static svn_error_t *
185 read_string(const char **str, svn_spillbuf_reader_t *reader, apr_pool_t *pool)
186 {
187   apr_uint64_t len;
188   apr_size_t size;
189   apr_size_t amt;
190   char *buf;
191
192   SVN_ERR(read_number(&len, reader, pool));
193
194   /* Len can never be less than zero.  But could len be so large that
195      len + 1 wraps around and we end up passing 0 to apr_palloc(),
196      thus getting a pointer to no storage?  Probably not (16 exabyte
197      string, anyone?) but let's be future-proof anyway. */
198   if (len + 1 < len || len + 1 > APR_SIZE_MAX)
199     {
200       /* xgettext doesn't expand preprocessor definitions, so we must
201          pass translatable string to apr_psprintf() function to create
202          intermediate string with appropriate format specifier. */
203       return svn_error_createf(SVN_ERR_REPOS_BAD_REVISION_REPORT, NULL,
204                                apr_psprintf(pool,
205                                             _("Invalid length (%%%s) when "
206                                               "about to read a string"),
207                                             APR_UINT64_T_FMT),
208                                len);
209     }
210
211   size = (apr_size_t)len;
212   buf = apr_palloc(pool, size+1);
213   if (size > 0)
214     {
215       SVN_ERR(svn_spillbuf__reader_read(&amt, reader, buf, size, pool));
216       SVN_ERR_ASSERT(amt == size);
217     }
218   buf[len] = 0;
219   *str = buf;
220   return SVN_NO_ERROR;
221 }
222
223 static svn_error_t *
224 read_rev(svn_revnum_t *rev, svn_spillbuf_reader_t *reader, apr_pool_t *pool)
225 {
226   char c;
227   apr_uint64_t num;
228
229   SVN_ERR(svn_spillbuf__reader_getc(&c, reader, pool));
230   if (c == '+')
231     {
232       SVN_ERR(read_number(&num, reader, pool));
233       *rev = (svn_revnum_t) num;
234     }
235   else
236     *rev = SVN_INVALID_REVNUM;
237   return SVN_NO_ERROR;
238 }
239
240 /* Read a single character to set *DEPTH (having already read '+')
241    from READER.  PATH is the path to which the depth applies, and is
242    used for error reporting only. */
243 static svn_error_t *
244 read_depth(svn_depth_t *depth, svn_spillbuf_reader_t *reader, const char *path,
245            apr_pool_t *pool)
246 {
247   char c;
248
249   SVN_ERR(svn_spillbuf__reader_getc(&c, reader, pool));
250   switch (c)
251     {
252     case 'X':
253       *depth = svn_depth_exclude;
254       break;
255     case 'E':
256       *depth = svn_depth_empty;
257       break;
258     case 'F':
259       *depth = svn_depth_files;
260       break;
261     case 'M':
262       *depth = svn_depth_immediates;
263       break;
264
265       /* Note that we do not tolerate explicit representation of
266          svn_depth_infinity here, because that's not how
267          write_path_info() writes it. */
268     default:
269       return svn_error_createf(SVN_ERR_REPOS_BAD_REVISION_REPORT, NULL,
270                                _("Invalid depth (%c) for path '%s'"), c, path);
271     }
272
273   return SVN_NO_ERROR;
274 }
275
276 /* Read a report operation *PI out of READER.  Set *PI to NULL if we
277    have reached the end of the report. */
278 static svn_error_t *
279 read_path_info(path_info_t **pi,
280                svn_spillbuf_reader_t *reader,
281                apr_pool_t *pool)
282 {
283   char c;
284
285   SVN_ERR(svn_spillbuf__reader_getc(&c, reader, pool));
286   if (c == '-')
287     {
288       *pi = NULL;
289       return SVN_NO_ERROR;
290     }
291
292   *pi = apr_palloc(pool, sizeof(**pi));
293   SVN_ERR(read_string(&(*pi)->path, reader, pool));
294   SVN_ERR(svn_spillbuf__reader_getc(&c, reader, pool));
295   if (c == '+')
296     SVN_ERR(read_string(&(*pi)->link_path, reader, pool));
297   else
298     (*pi)->link_path = NULL;
299   SVN_ERR(read_rev(&(*pi)->rev, reader, pool));
300   SVN_ERR(svn_spillbuf__reader_getc(&c, reader, pool));
301   if (c == '+')
302     SVN_ERR(read_depth(&((*pi)->depth), reader, (*pi)->path, pool));
303   else
304     (*pi)->depth = svn_depth_infinity;
305   SVN_ERR(svn_spillbuf__reader_getc(&c, reader, pool));
306   (*pi)->start_empty = (c == '+');
307   SVN_ERR(svn_spillbuf__reader_getc(&c, reader, pool));
308   if (c == '+')
309     SVN_ERR(read_string(&(*pi)->lock_token, reader, pool));
310   else
311     (*pi)->lock_token = NULL;
312   (*pi)->pool = pool;
313   return SVN_NO_ERROR;
314 }
315
316 /* Return true if PI's path is a child of PREFIX (which has length PLEN). */
317 static svn_boolean_t
318 relevant(path_info_t *pi, const char *prefix, apr_size_t plen)
319 {
320   return (pi && strncmp(pi->path, prefix, plen) == 0 &&
321           (!*prefix || pi->path[plen] == '/'));
322 }
323
324 /* Fetch the next pathinfo from B->reader for a descendant of
325    PREFIX.  If the next pathinfo is for an immediate child of PREFIX,
326    set *ENTRY to the path component of the report information and
327    *INFO to the path information for that entry.  If the next pathinfo
328    is for a grandchild or other more remote descendant of PREFIX, set
329    *ENTRY to the immediate child corresponding to that descendant and
330    set *INFO to NULL.  If the next pathinfo is not for a descendant of
331    PREFIX, or if we reach the end of the report, set both *ENTRY and
332    *INFO to NULL.
333
334    At all times, B->lookahead is presumed to be the next pathinfo not
335    yet returned as an immediate child, or NULL if we have reached the
336    end of the report.  Because we use a lookahead element, we can't
337    rely on the usual nested pool lifetimes, so allocate each pathinfo
338    in a subpool of the report baton's pool.  The caller should delete
339    (*INFO)->pool when it is done with the information. */
340 static svn_error_t *
341 fetch_path_info(report_baton_t *b, const char **entry, path_info_t **info,
342                 const char *prefix, apr_pool_t *pool)
343 {
344   apr_size_t plen = strlen(prefix);
345   const char *relpath, *sep;
346   apr_pool_t *subpool;
347
348   if (!relevant(b->lookahead, prefix, plen))
349     {
350       /* No more entries relevant to prefix. */
351       *entry = NULL;
352       *info = NULL;
353     }
354   else
355     {
356       /* Take a look at the prefix-relative part of the path. */
357       relpath = b->lookahead->path + (*prefix ? plen + 1 : 0);
358       sep = strchr(relpath, '/');
359       if (sep)
360         {
361           /* Return the immediate child part; do not advance. */
362           *entry = apr_pstrmemdup(pool, relpath, sep - relpath);
363           *info = NULL;
364         }
365       else
366         {
367           /* This is an immediate child; return it and advance. */
368           *entry = relpath;
369           *info = b->lookahead;
370           subpool = svn_pool_create(b->pool);
371           SVN_ERR(read_path_info(&b->lookahead, b->reader, subpool));
372         }
373     }
374   return SVN_NO_ERROR;
375 }
376
377 /* Skip all path info entries relevant to *PREFIX.  Call this when the
378    editor drive skips a directory. */
379 static svn_error_t *
380 skip_path_info(report_baton_t *b, const char *prefix)
381 {
382   apr_size_t plen = strlen(prefix);
383   apr_pool_t *subpool;
384
385   while (relevant(b->lookahead, prefix, plen))
386     {
387       svn_pool_destroy(b->lookahead->pool);
388       subpool = svn_pool_create(b->pool);
389       SVN_ERR(read_path_info(&b->lookahead, b->reader, subpool));
390     }
391   return SVN_NO_ERROR;
392 }
393
394 /* Return true if there is at least one path info entry relevant to *PREFIX. */
395 static svn_boolean_t
396 any_path_info(report_baton_t *b, const char *prefix)
397 {
398   return relevant(b->lookahead, prefix, strlen(prefix));
399 }
400
401 /* --- DRIVING THE EDITOR ONCE THE REPORT IS FINISHED --- */
402
403 /* While driving the editor, the target root will remain constant, but
404    we may have to jump around between source roots depending on the
405    state of the working copy.  If we were to open a root each time we
406    revisit a rev, we would get no benefit from node-id caching; on the
407    other hand, if we hold open all the roots we ever visit, we'll use
408    an unbounded amount of memory.  As a compromise, we maintain a
409    fixed-size LRU cache of source roots.  get_source_root retrieves a
410    root from the cache, using POOL to allocate the new root if
411    necessary.  Be careful not to hold onto the root for too long,
412    particularly after recursing, since another call to get_source_root
413    can close it. */
414 static svn_error_t *
415 get_source_root(report_baton_t *b, svn_fs_root_t **s_root, svn_revnum_t rev)
416 {
417   int i;
418   svn_fs_root_t *root, *prev = NULL;
419
420   /* Look for the desired root in the cache, sliding all the unmatched
421      entries backwards a slot to make room for the right one. */
422   for (i = 0; i < NUM_CACHED_SOURCE_ROOTS; i++)
423     {
424       root = b->s_roots[i];
425       b->s_roots[i] = prev;
426       if (root && svn_fs_revision_root_revision(root) == rev)
427         break;
428       prev = root;
429     }
430
431   /* If we didn't find it, throw out the oldest root and open a new one. */
432   if (i == NUM_CACHED_SOURCE_ROOTS)
433     {
434       if (prev)
435         svn_fs_close_root(prev);
436       SVN_ERR(svn_fs_revision_root(&root, b->repos->fs, rev, b->pool));
437     }
438
439   /* Assign the desired root to the first cache slot and hand it back. */
440   b->s_roots[0] = root;
441   *s_root = root;
442   return SVN_NO_ERROR;
443 }
444
445 /* Call the directory property-setting function of B->editor to set
446    the property NAME to VALUE on DIR_BATON. */
447 static svn_error_t *
448 change_dir_prop(report_baton_t *b, void *dir_baton, const char *name,
449                 const svn_string_t *value, apr_pool_t *pool)
450 {
451   return svn_error_trace(b->editor->change_dir_prop(dir_baton, name, value,
452                                                     pool));
453 }
454
455 /* Call the file property-setting function of B->editor to set the
456    property NAME to VALUE on FILE_BATON. */
457 static svn_error_t *
458 change_file_prop(report_baton_t *b, void *file_baton, const char *name,
459                  const svn_string_t *value, apr_pool_t *pool)
460 {
461   return svn_error_trace(b->editor->change_file_prop(file_baton, name, value,
462                                                      pool));
463 }
464
465 /* For the report B, return the relevant revprop data of revision REV in
466    REVISION_INFO. The revision info will be allocated in b->pool.
467    Temporaries get allocated on SCRATCH_POOL. */
468 static  svn_error_t *
469 get_revision_info(report_baton_t *b,
470                   svn_revnum_t rev,
471                   revision_info_t** revision_info,
472                   apr_pool_t *scratch_pool)
473 {
474   apr_hash_t *r_props;
475   svn_string_t *cdate, *author;
476   revision_info_t* info;
477
478   /* Try to find the info in the report's cache */
479   info = apr_hash_get(b->revision_infos, &rev, sizeof(rev));
480   if (!info)
481     {
482       /* Info is not available, yet.
483          Get all revprops. */
484       SVN_ERR(svn_fs_revision_proplist2(&r_props,
485                                         b->repos->fs,
486                                         rev,
487                                         FALSE,
488                                         scratch_pool,
489                                         scratch_pool));
490
491       /* Extract the committed-date. */
492       cdate = svn_hash_gets(r_props, SVN_PROP_REVISION_DATE);
493
494       /* Extract the last-author. */
495       author = svn_hash_gets(r_props, SVN_PROP_REVISION_AUTHOR);
496
497       /* Create a result object */
498       info = apr_palloc(b->pool, sizeof(*info));
499       info->rev = rev;
500       info->date = svn_string_dup(cdate, b->pool);
501       info->author = svn_string_dup(author, b->pool);
502
503       /* Cache it */
504       apr_hash_set(b->revision_infos, &info->rev, sizeof(info->rev), info);
505     }
506
507   *revision_info = info;
508   return SVN_NO_ERROR;
509 }
510
511
512 /* Generate the appropriate property editing calls to turn the
513    properties of S_REV/S_PATH into those of B->t_root/T_PATH.  If
514    S_PATH is NULL, this is an add, so assume the target starts with no
515    properties.  Pass OBJECT on to the editor function wrapper
516    CHANGE_FN. */
517 static svn_error_t *
518 delta_proplists(report_baton_t *b, svn_revnum_t s_rev, const char *s_path,
519                 const char *t_path, const char *lock_token,
520                 proplist_change_fn_t *change_fn,
521                 void *object, apr_pool_t *pool)
522 {
523   svn_fs_root_t *s_root;
524   apr_hash_t *s_props = NULL, *t_props;
525   svn_revnum_t crev;
526
527   /* Fetch the created-rev and send entry props. */
528   SVN_ERR(svn_fs_node_created_rev(&crev, b->t_root, t_path, pool));
529   if (SVN_IS_VALID_REVNUM(crev))
530     {
531       revision_info_t *revision_info;
532       /* convert committed-rev to  string */
533       char buf[SVN_INT64_BUFFER_SIZE];
534       svn_string_t cr_str;
535       cr_str.data = buf;
536       cr_str.len = svn__i64toa(buf, crev);
537
538       /* Transmit the committed-rev. */
539       SVN_ERR(change_fn(b, object,
540                         SVN_PROP_ENTRY_COMMITTED_REV, &cr_str, pool));
541
542       SVN_ERR(get_revision_info(b, crev, &revision_info, pool));
543
544       /* Transmit the committed-date. */
545       if (revision_info->date || s_path)
546         SVN_ERR(change_fn(b, object, SVN_PROP_ENTRY_COMMITTED_DATE,
547                           revision_info->date, pool));
548
549       /* Transmit the last-author. */
550       if (revision_info->author || s_path)
551         SVN_ERR(change_fn(b, object, SVN_PROP_ENTRY_LAST_AUTHOR,
552                           revision_info->author, pool));
553
554       /* Transmit the UUID. */
555       SVN_ERR(change_fn(b, object, SVN_PROP_ENTRY_UUID,
556                         b->repos_uuid, pool));
557     }
558
559   /* Update lock properties. */
560   if (lock_token)
561     {
562       svn_lock_t *lock;
563       SVN_ERR(svn_fs_get_lock(&lock, b->repos->fs, t_path, pool));
564
565       /* Delete a defunct lock. */
566       if (! lock || strcmp(lock_token, lock->token) != 0)
567         SVN_ERR(change_fn(b, object, SVN_PROP_ENTRY_LOCK_TOKEN,
568                           NULL, pool));
569     }
570
571   if (s_path)
572     {
573       svn_boolean_t changed;
574       SVN_ERR(get_source_root(b, &s_root, s_rev));
575
576       /* Is this deltification worth our time? */
577       SVN_ERR(svn_fs_props_different(&changed, b->t_root, t_path, s_root,
578                                      s_path, pool));
579       if (! changed)
580         return SVN_NO_ERROR;
581
582       /* If so, go ahead and get the source path's properties. */
583       SVN_ERR(svn_fs_node_proplist(&s_props, s_root, s_path, pool));
584     }
585
586   /* Get the target path's properties */
587   SVN_ERR(svn_fs_node_proplist(&t_props, b->t_root, t_path, pool));
588
589   if (s_props && apr_hash_count(s_props))
590     {
591       apr_array_header_t *prop_diffs;
592       int i;
593
594       /* Now transmit the differences. */
595       SVN_ERR(svn_prop_diffs(&prop_diffs, t_props, s_props, pool));
596       for (i = 0; i < prop_diffs->nelts; i++)
597         {
598           const svn_prop_t *pc = &APR_ARRAY_IDX(prop_diffs, i, svn_prop_t);
599           SVN_ERR(change_fn(b, object, pc->name, pc->value, pool));
600         }
601     }
602   else if (apr_hash_count(t_props))
603     {
604       apr_hash_index_t *hi;
605       /* So source, i.e. all new.  Transmit all target props. */
606       for (hi = apr_hash_first(pool, t_props); hi; hi = apr_hash_next(hi))
607         {
608           const char *key = apr_hash_this_key(hi);
609           svn_string_t *val = apr_hash_this_val(hi);
610
611           SVN_ERR(change_fn(b, object, key, val, pool));
612         }
613     }
614
615   return SVN_NO_ERROR;
616 }
617
618 /* Baton type to be passed into send_zero_copy_delta.
619  */
620 typedef struct zero_copy_baton_t
621 {
622   /* don't process data larger than this limit */
623   apr_size_t zero_copy_limit;
624
625   /* window handler and baton to send the data to */
626   svn_txdelta_window_handler_t dhandler;
627   void *dbaton;
628
629   /* return value: will be set to TRUE, if the data was processed. */
630   svn_boolean_t zero_copy_succeeded;
631 } zero_copy_baton_t;
632
633 /* Implement svn_fs_process_contents_func_t.  If LEN is smaller than the
634  * limit given in *BATON, send the CONTENTS as an delta windows to the
635  * handler given in BATON and set the ZERO_COPY_SUCCEEDED flag in that
636  * BATON.  Otherwise, reset it to FALSE.
637  * Use POOL for temporary allocations.
638  */
639 static svn_error_t *
640 send_zero_copy_delta(const unsigned char *contents,
641                      apr_size_t len,
642                      void *baton,
643                      apr_pool_t *pool)
644 {
645   zero_copy_baton_t *zero_copy_baton = baton;
646
647   /* if the item is too large, the caller must revert to traditional
648      streaming code. */
649   if (len > zero_copy_baton->zero_copy_limit)
650     {
651       zero_copy_baton->zero_copy_succeeded = FALSE;
652       return SVN_NO_ERROR;
653     }
654
655   SVN_ERR(svn_txdelta_send_contents(contents, len,
656                                     zero_copy_baton->dhandler,
657                                     zero_copy_baton->dbaton, pool));
658
659   /* all fine now */
660   zero_copy_baton->zero_copy_succeeded = TRUE;
661   return SVN_NO_ERROR;
662 }
663
664
665 /* Make the appropriate edits on FILE_BATON to change its contents and
666    properties from those in S_REV/S_PATH to those in B->t_root/T_PATH,
667    possibly using LOCK_TOKEN to determine if the client's lock on the file
668    is defunct. */
669 static svn_error_t *
670 delta_files(report_baton_t *b, void *file_baton, svn_revnum_t s_rev,
671             const char *s_path, const char *t_path, const char *lock_token,
672             apr_pool_t *pool)
673 {
674   svn_fs_root_t *s_root = NULL;
675   svn_txdelta_stream_t *dstream = NULL;
676   svn_checksum_t *s_checksum;
677   const char *s_hex_digest = NULL;
678   svn_txdelta_window_handler_t dhandler;
679   void *dbaton;
680
681   /* Compare the files' property lists.  */
682   SVN_ERR(delta_proplists(b, s_rev, s_path, t_path, lock_token,
683                           change_file_prop, file_baton, pool));
684
685   if (s_path)
686     {
687       svn_boolean_t changed;
688       SVN_ERR(get_source_root(b, &s_root, s_rev));
689
690       /* We're not interested in the theoretical difference between "has
691          contents which have not changed with respect to" and "has the same
692          actual contents as" when sending text-deltas.  If we know the
693          delta is an empty one, we avoiding sending it in either case. */
694       SVN_ERR(svn_fs_contents_different(&changed, b->t_root, t_path,
695                                         s_root, s_path, pool));
696
697       if (!changed)
698         return SVN_NO_ERROR;
699
700       SVN_ERR(svn_fs_file_checksum(&s_checksum, svn_checksum_md5, s_root,
701                                    s_path, TRUE, pool));
702       s_hex_digest = svn_checksum_to_cstring(s_checksum, pool);
703     }
704
705   /* Send the delta stream if desired, or just a NULL window if not. */
706   SVN_ERR(b->editor->apply_textdelta(file_baton, s_hex_digest, pool,
707                                      &dhandler, &dbaton));
708
709   if (dhandler != svn_delta_noop_window_handler)
710     {
711       if (b->text_deltas)
712         {
713           /* if we send deltas against empty streams, we may use our
714              zero-copy code. */
715           if (b->zero_copy_limit > 0 && s_path == NULL)
716             {
717               zero_copy_baton_t baton;
718               svn_boolean_t called = FALSE;
719
720               baton.zero_copy_limit = b->zero_copy_limit;
721               baton.dhandler = dhandler;
722               baton.dbaton = dbaton;
723               baton.zero_copy_succeeded = FALSE;
724               SVN_ERR(svn_fs_try_process_file_contents(&called,
725                                                        b->t_root, t_path,
726                                                        send_zero_copy_delta,
727                                                        &baton, pool));
728
729               /* data has been available and small enough,
730                  i.e. been processed? */
731               if (called && baton.zero_copy_succeeded)
732                 return SVN_NO_ERROR;
733             }
734
735           SVN_ERR(svn_fs_get_file_delta_stream(&dstream, s_root, s_path,
736                                                b->t_root, t_path, pool));
737           SVN_ERR(svn_txdelta_send_txstream(dstream, dhandler, dbaton, pool));
738         }
739       else
740         SVN_ERR(dhandler(NULL, dbaton));
741     }
742
743   return SVN_NO_ERROR;
744 }
745
746 /* Determine if the user is authorized to view B->t_root/PATH. */
747 static svn_error_t *
748 check_auth(report_baton_t *b, svn_boolean_t *allowed, const char *path,
749            apr_pool_t *pool)
750 {
751   if (b->authz_read_func)
752     return svn_error_trace(b->authz_read_func(allowed, b->t_root, path,
753                                               b->authz_read_baton, pool));
754   *allowed = TRUE;
755   return SVN_NO_ERROR;
756 }
757
758 /* Create a dirent in *ENTRY for the given ROOT and PATH.  We use this to
759    replace the source or target dirent when a report pathinfo tells us to
760    change paths or revisions. */
761 static svn_error_t *
762 fake_dirent(const svn_fs_dirent_t **entry, svn_fs_root_t *root,
763             const char *path, apr_pool_t *pool)
764 {
765   svn_node_kind_t kind;
766   svn_fs_dirent_t *ent;
767
768   SVN_ERR(svn_fs_check_path(&kind, root, path, pool));
769   if (kind == svn_node_none)
770     *entry = NULL;
771   else
772     {
773       ent = apr_palloc(pool, sizeof(**entry));
774       /* ### All callers should be updated to pass just one of these
775              formats */
776       ent->name = (*path == '/') ? svn_fspath__basename(path, pool)
777                                  : svn_relpath_basename(path, pool);
778       SVN_ERR(svn_fs_node_id(&ent->id, root, path, pool));
779       ent->kind = kind;
780       *entry = ent;
781     }
782   return SVN_NO_ERROR;
783 }
784
785
786 /* Given REQUESTED_DEPTH, WC_DEPTH and the current entry's KIND,
787    determine whether we need to send the whole entry, not just deltas.
788    Please refer to delta_dirs' docstring for an explanation of the
789    conditionals below. */
790 static svn_boolean_t
791 is_depth_upgrade(svn_depth_t wc_depth,
792                  svn_depth_t requested_depth,
793                  svn_node_kind_t kind)
794 {
795   if (requested_depth == svn_depth_unknown
796       || requested_depth <= wc_depth
797       || wc_depth == svn_depth_immediates)
798     return FALSE;
799
800   if (kind == svn_node_file
801       && wc_depth == svn_depth_files)
802     return FALSE;
803
804   if (kind == svn_node_dir
805       && wc_depth == svn_depth_empty
806       && requested_depth == svn_depth_files)
807     return FALSE;
808
809   return TRUE;
810 }
811
812
813 /* Call the B->editor's add_file() function to create PATH as a child
814    of PARENT_BATON, returning a new baton in *NEW_FILE_BATON.
815    However, make an attempt to send 'copyfrom' arguments if they're
816    available, by examining the closest copy of the original file
817    O_PATH within B->t_root.  If any copyfrom args are discovered,
818    return those in *COPYFROM_PATH and *COPYFROM_REV;  otherwise leave
819    those return args untouched. */
820 static svn_error_t *
821 add_file_smartly(report_baton_t *b,
822                  const char *path,
823                  void *parent_baton,
824                  const char *o_path,
825                  void **new_file_baton,
826                  const char **copyfrom_path,
827                  svn_revnum_t *copyfrom_rev,
828                  apr_pool_t *pool)
829 {
830   /* ### TODO:  use a subpool to do this work, clear it at the end? */
831   svn_fs_t *fs = svn_repos_fs(b->repos);
832   svn_fs_root_t *closest_copy_root = NULL;
833   const char *closest_copy_path = NULL;
834
835   /* Pre-emptively assume no copyfrom args exist. */
836   *copyfrom_path = NULL;
837   *copyfrom_rev = SVN_INVALID_REVNUM;
838
839   if (b->send_copyfrom_args)
840     {
841       /* Find the destination of the nearest 'copy event' which may have
842          caused o_path@t_root to exist. svn_fs_closest_copy only returns paths
843          starting with '/', so make sure o_path always starts with a '/'
844          too. */
845       if (*o_path != '/')
846         o_path = apr_pstrcat(pool, "/", o_path, SVN_VA_NULL);
847
848       SVN_ERR(svn_fs_closest_copy(&closest_copy_root, &closest_copy_path,
849                                   b->t_root, o_path, pool));
850       if (closest_copy_root != NULL)
851         {
852           /* If the destination of the copy event is the same path as
853              o_path, then we've found something interesting that should
854              have 'copyfrom' history. */
855           if (strcmp(closest_copy_path, o_path) == 0)
856             {
857               SVN_ERR(svn_fs_copied_from(copyfrom_rev, copyfrom_path,
858                                          closest_copy_root, closest_copy_path,
859                                          pool));
860               if (b->authz_read_func)
861                 {
862                   svn_boolean_t allowed;
863                   svn_fs_root_t *copyfrom_root;
864                   SVN_ERR(svn_fs_revision_root(&copyfrom_root, fs,
865                                                *copyfrom_rev, pool));
866                   SVN_ERR(b->authz_read_func(&allowed, copyfrom_root,
867                                              *copyfrom_path, b->authz_read_baton,
868                                              pool));
869                   if (! allowed)
870                     {
871                       *copyfrom_path = NULL;
872                       *copyfrom_rev = SVN_INVALID_REVNUM;
873                     }
874                 }
875             }
876         }
877     }
878
879   return svn_error_trace(b->editor->add_file(path, parent_baton,
880                                              *copyfrom_path, *copyfrom_rev,
881                                              pool, new_file_baton));
882 }
883
884
885 /* Emit a series of editing operations to transform a source entry to
886    a target entry.
887
888    S_REV and S_PATH specify the source entry.  S_ENTRY contains the
889    already-looked-up information about the node-revision existing at
890    that location.  S_PATH and S_ENTRY may be NULL if the entry does
891    not exist in the source.  S_PATH may be non-NULL and S_ENTRY may be
892    NULL if the caller expects INFO to modify the source to an existing
893    location.
894
895    B->t_root and T_PATH specify the target entry.  T_ENTRY contains
896    the already-looked-up information about the node-revision existing
897    at that location.  T_PATH and T_ENTRY may be NULL if the entry does
898    not exist in the target.
899
900    DIR_BATON and E_PATH contain the parameters which should be passed
901    to the editor calls--DIR_BATON for the parent directory baton and
902    E_PATH for the pathname.  (E_PATH is the anchor-relative working
903    copy pathname, which may differ from the source and target
904    pathnames if the report contains a link_path.)
905
906    INFO contains the report information for this working copy path, or
907    NULL if there is none.  This function will internally modify the
908    source and target entries as appropriate based on the report
909    information.
910
911    WC_DEPTH and REQUESTED_DEPTH are propagated to delta_dirs() if
912    necessary.  Refer to delta_dirs' docstring to find out what
913    should happen for various combinations of WC_DEPTH/REQUESTED_DEPTH. */
914 static svn_error_t *
915 update_entry(report_baton_t *b, svn_revnum_t s_rev, const char *s_path,
916              const svn_fs_dirent_t *s_entry, const char *t_path,
917              const svn_fs_dirent_t *t_entry, void *dir_baton,
918              const char *e_path, path_info_t *info, svn_depth_t wc_depth,
919              svn_depth_t requested_depth, apr_pool_t *pool)
920 {
921   svn_fs_root_t *s_root = NULL;
922   svn_boolean_t allowed, related;
923   void *new_baton;
924   svn_checksum_t *checksum;
925   const char *hex_digest;
926
927   /* For non-switch operations, follow link_path in the target. */
928   if (info && info->link_path && !b->is_switch)
929     {
930       t_path = info->link_path;
931       SVN_ERR(fake_dirent(&t_entry, b->t_root, t_path, pool));
932     }
933
934   if (info && !SVN_IS_VALID_REVNUM(info->rev))
935     {
936       /* Delete this entry in the source. */
937       s_path = NULL;
938       s_entry = NULL;
939     }
940   else if (info && s_path)
941     {
942       /* Follow the rev and possibly path in this entry. */
943       s_path = (info->link_path) ? info->link_path : s_path;
944       s_rev = info->rev;
945       SVN_ERR(get_source_root(b, &s_root, s_rev));
946       SVN_ERR(fake_dirent(&s_entry, s_root, s_path, pool));
947     }
948
949   /* Don't let the report carry us somewhere nonexistent. */
950   if (s_path && !s_entry)
951     return svn_error_createf(SVN_ERR_FS_NOT_FOUND, NULL,
952                              _("Working copy path '%s' does not exist in "
953                                "repository"), e_path);
954
955   /* If the source and target both exist and are of the same kind,
956      then find out whether they're related.  If they're exactly the
957      same, then we don't have to do anything (unless the report has
958      changes to the source).  If we're ignoring ancestry, then any two
959      nodes of the same type are related enough for us. */
960   related = FALSE;
961   if (s_entry && t_entry && s_entry->kind == t_entry->kind)
962     {
963       int distance = svn_fs_compare_ids(s_entry->id, t_entry->id);
964       svn_boolean_t changed = TRUE;
965
966       /* Check related files for content changes to avoid reporting
967        * unchanged copies of files to the client as an open_file() call
968        * and change_file_prop()/apply_textdelta() calls with no-op changes.
969        * The client will otherwise raise unnecessary tree conflicts. */
970       if (!b->ignore_ancestry && t_entry->kind == svn_node_file &&
971           distance == 1)
972         {
973           if (s_root == NULL)
974             SVN_ERR(get_source_root(b, &s_root, s_rev));
975
976           SVN_ERR(svn_fs_props_changed(&changed, s_root, s_path,
977                                        b->t_root, t_path, pool));
978           if (!changed)
979             SVN_ERR(svn_fs_contents_changed(&changed, s_root, s_path,
980                                             b->t_root, t_path, pool));
981         }
982
983       if ((distance == 0 || !changed) && !any_path_info(b, e_path)
984           && (requested_depth <= wc_depth || t_entry->kind == svn_node_file))
985         {
986           if (!info)
987             return SVN_NO_ERROR;
988
989           if (!info->start_empty)
990             {
991               svn_lock_t *lock;
992
993               if (!info->lock_token)
994                 return SVN_NO_ERROR;
995
996               SVN_ERR(svn_fs_get_lock(&lock, b->repos->fs, t_path, pool));
997               if (lock && (strcmp(lock->token, info->lock_token) == 0))
998                 return SVN_NO_ERROR;
999             }
1000         }
1001
1002       related = (distance != -1 || b->ignore_ancestry);
1003     }
1004
1005   /* If there's a source and it's not related to the target, nuke it. */
1006   if (s_entry && !related)
1007     {
1008       svn_revnum_t deleted_rev;
1009
1010       SVN_ERR(svn_repos_deleted_rev(svn_fs_root_fs(b->t_root), t_path,
1011                                     s_rev, b->t_rev, &deleted_rev,
1012                                     pool));
1013
1014       if (!SVN_IS_VALID_REVNUM(deleted_rev))
1015         {
1016           /* Two possibilities: either the thing doesn't exist in S_REV; or
1017              it wasn't deleted between S_REV and B->T_REV.  In the first case,
1018              I think we should leave DELETED_REV as SVN_INVALID_REVNUM, but
1019              in the second, it should be set to B->T_REV-1 for the call to
1020              delete_entry() below. */
1021           svn_node_kind_t kind;
1022
1023           SVN_ERR(svn_fs_check_path(&kind, b->t_root, t_path, pool));
1024           if (kind != svn_node_none)
1025             deleted_rev = b->t_rev - 1;
1026         }
1027
1028       SVN_ERR(b->editor->delete_entry(e_path, deleted_rev, dir_baton,
1029                                       pool));
1030       s_path = NULL;
1031     }
1032
1033   /* If there's no target, we have nothing more to do. */
1034   if (!t_entry)
1035     return svn_error_trace(skip_path_info(b, e_path));
1036
1037   /* Check if the user is authorized to find out about the target. */
1038   SVN_ERR(check_auth(b, &allowed, t_path, pool));
1039   if (!allowed)
1040     {
1041       if (t_entry->kind == svn_node_dir)
1042         SVN_ERR(b->editor->absent_directory(e_path, dir_baton, pool));
1043       else
1044         SVN_ERR(b->editor->absent_file(e_path, dir_baton, pool));
1045       return svn_error_trace(skip_path_info(b, e_path));
1046     }
1047
1048   if (t_entry->kind == svn_node_dir)
1049     {
1050       if (related)
1051         SVN_ERR(b->editor->open_directory(e_path, dir_baton, s_rev, pool,
1052                                           &new_baton));
1053       else
1054         SVN_ERR(b->editor->add_directory(e_path, dir_baton, NULL,
1055                                          SVN_INVALID_REVNUM, pool,
1056                                          &new_baton));
1057
1058       SVN_ERR(delta_dirs(b, s_rev, s_path, t_path, new_baton, e_path,
1059                          info ? info->start_empty : FALSE,
1060                          wc_depth, requested_depth, pool));
1061       return svn_error_trace(b->editor->close_directory(new_baton, pool));
1062     }
1063   else
1064     {
1065       if (related)
1066         {
1067           SVN_ERR(b->editor->open_file(e_path, dir_baton, s_rev, pool,
1068                                        &new_baton));
1069           SVN_ERR(delta_files(b, new_baton, s_rev, s_path, t_path,
1070                               info ? info->lock_token : NULL, pool));
1071         }
1072       else
1073         {
1074           svn_revnum_t copyfrom_rev = SVN_INVALID_REVNUM;
1075           const char *copyfrom_path = NULL;
1076           SVN_ERR(add_file_smartly(b, e_path, dir_baton, t_path, &new_baton,
1077                                    &copyfrom_path, &copyfrom_rev, pool));
1078           if (! copyfrom_path)
1079             /* Send txdelta between empty file (s_path@s_rev doesn't
1080                exist) and added file (t_path@t_root). */
1081             SVN_ERR(delta_files(b, new_baton, s_rev, s_path, t_path,
1082                                 info ? info->lock_token : NULL, pool));
1083           else
1084             /* Send txdelta between copied file (copyfrom_path@copyfrom_rev)
1085                and added file (tpath@t_root). */
1086             SVN_ERR(delta_files(b, new_baton, copyfrom_rev, copyfrom_path,
1087                                 t_path, info ? info->lock_token : NULL, pool));
1088         }
1089
1090       SVN_ERR(svn_fs_file_checksum(&checksum, svn_checksum_md5, b->t_root,
1091                                    t_path, TRUE, pool));
1092       hex_digest = svn_checksum_to_cstring(checksum, pool);
1093       return svn_error_trace(b->editor->close_file(new_baton, hex_digest,
1094                                                    pool));
1095     }
1096 }
1097
1098 /* A helper macro for when we have to recurse into subdirectories. */
1099 #define DEPTH_BELOW_HERE(depth) ((depth) == svn_depth_immediates) ? \
1100                                  svn_depth_empty : (depth)
1101
1102 /* Emit edits within directory DIR_BATON (with corresponding path
1103    E_PATH) with the changes from the directory S_REV/S_PATH to the
1104    directory B->t_rev/T_PATH.  S_PATH may be NULL if the entry does
1105    not exist in the source.
1106
1107    WC_DEPTH is this path's depth as reported by set_path/link_path.
1108    REQUESTED_DEPTH is derived from the depth set by
1109    svn_repos_begin_report().
1110
1111    When iterating over this directory's entries, the following tables
1112    describe what happens for all possible combinations
1113    of WC_DEPTH/REQUESTED_DEPTH (rows represent WC_DEPTH, columns
1114    represent REQUESTED_DEPTH):
1115
1116    Legend:
1117      X: ignore this entry (it's either below the requested depth, or
1118         if the requested depth is svn_depth_unknown, below the working
1119         copy depth)
1120      o: handle this entry normally
1121      U: handle the entry as if it were a newly added repository path
1122         (the client is upgrading to a deeper wc and doesn't currently
1123         have this entry, but it should be there after the upgrade, so we
1124         need to send the whole thing, not just deltas)
1125
1126                               For files:
1127    ______________________________________________________________
1128    | req. depth| unknown | empty | files | immediates | infinity |
1129    |wc. depth  |         |       |       |            |          |
1130    |___________|_________|_______|_______|____________|__________|
1131    |empty      |    X    |   X   |   U   |     U      |    U     |
1132    |___________|_________|_______|_______|____________|__________|
1133    |files      |    o    |   X   |   o   |     o      |    o     |
1134    |___________|_________|_______|_______|____________|__________|
1135    |immediates |    o    |   X   |   o   |     o      |    o     |
1136    |___________|_________|_______|_______|____________|__________|
1137    |infinity   |    o    |   X   |   o   |     o      |    o     |
1138    |___________|_________|_______|_______|____________|__________|
1139
1140                             For directories:
1141    ______________________________________________________________
1142    | req. depth| unknown | empty | files | immediates | infinity |
1143    |wc. depth  |         |       |       |            |          |
1144    |___________|_________|_______|_______|____________|__________|
1145    |empty      |    X    |   X   |   X   |     U      |    U     |
1146    |___________|_________|_______|_______|____________|__________|
1147    |files      |    X    |   X   |   X   |     U      |    U     |
1148    |___________|_________|_______|_______|____________|__________|
1149    |immediates |    o    |   X   |   X   |     o      |    o     |
1150    |___________|_________|_______|_______|____________|__________|
1151    |infinity   |    o    |   X   |   X   |     o      |    o     |
1152    |___________|_________|_______|_______|____________|__________|
1153
1154    These rules are enforced by the is_depth_upgrade() function and by
1155    various other checks below.
1156 */
1157 static svn_error_t *
1158 delta_dirs(report_baton_t *b, svn_revnum_t s_rev, const char *s_path,
1159            const char *t_path, void *dir_baton, const char *e_path,
1160            svn_boolean_t start_empty, svn_depth_t wc_depth,
1161            svn_depth_t requested_depth, apr_pool_t *pool)
1162 {
1163   apr_hash_t *s_entries = NULL, *t_entries;
1164   apr_hash_index_t *hi;
1165   apr_pool_t *subpool = svn_pool_create(pool);
1166   apr_array_header_t *t_ordered_entries = NULL;
1167   int i;
1168
1169   /* Compare the property lists.  If we're starting empty, pass a NULL
1170      source path so that we add all the properties.
1171
1172      When we support directory locks, we must pass the lock token here. */
1173   SVN_ERR(delta_proplists(b, s_rev, start_empty ? NULL : s_path, t_path,
1174                           NULL, change_dir_prop, dir_baton, subpool));
1175   svn_pool_clear(subpool);
1176
1177   if (requested_depth > svn_depth_empty
1178       || requested_depth == svn_depth_unknown)
1179     {
1180       apr_pool_t *iterpool;
1181
1182       /* Get the list of entries in each of source and target. */
1183       if (s_path && !start_empty)
1184         {
1185           svn_fs_root_t *s_root;
1186
1187           SVN_ERR(get_source_root(b, &s_root, s_rev));
1188           SVN_ERR(svn_fs_dir_entries(&s_entries, s_root, s_path, subpool));
1189         }
1190       SVN_ERR(svn_fs_dir_entries(&t_entries, b->t_root, t_path, subpool));
1191
1192       /* Iterate over the report information for this directory. */
1193       iterpool = svn_pool_create(subpool);
1194
1195       while (1)
1196         {
1197           path_info_t *info;
1198           const char *name, *s_fullpath, *t_fullpath, *e_fullpath;
1199           const svn_fs_dirent_t *s_entry, *t_entry;
1200
1201           svn_pool_clear(iterpool);
1202           SVN_ERR(fetch_path_info(b, &name, &info, e_path, iterpool));
1203           if (!name)
1204             break;
1205
1206           /* Invalid revnum means we should delete, unless this is
1207              just an excluded subpath. */
1208           if (info
1209               && !SVN_IS_VALID_REVNUM(info->rev)
1210               && info->depth != svn_depth_exclude)
1211             {
1212               /* We want to perform deletes before non-replacement adds,
1213                  for graceful handling of case-only renames on
1214                  case-insensitive client filesystems.  So, if the report
1215                  item is a delete, remove the entry from the source hash,
1216                  but don't update the entry yet. */
1217               if (s_entries)
1218                 svn_hash_sets(s_entries, name, NULL);
1219
1220               svn_pool_destroy(info->pool);
1221               continue;
1222             }
1223
1224           e_fullpath = svn_relpath_join(e_path, name, iterpool);
1225           t_fullpath = svn_fspath__join(t_path, name, iterpool);
1226           t_entry = svn_hash_gets(t_entries, name);
1227           s_fullpath = s_path ? svn_fspath__join(s_path, name, iterpool) : NULL;
1228           s_entry = s_entries ? svn_hash_gets(s_entries, name) : NULL;
1229
1230           /* The only special cases where we don't process the entry are
1231
1232              - When requested_depth is files but the reported path is
1233              a directory.  This is technically a client error, but we
1234              handle it anyway, by skipping the entry.
1235
1236              - When the reported depth is svn_depth_exclude.
1237           */
1238           if (! ((requested_depth == svn_depth_files
1239                   && ((t_entry && t_entry->kind == svn_node_dir)
1240                       || (s_entry && s_entry->kind == svn_node_dir)))
1241                  || (info && info->depth == svn_depth_exclude)))
1242             SVN_ERR(update_entry(b, s_rev, s_fullpath, s_entry, t_fullpath,
1243                                  t_entry, dir_baton, e_fullpath, info,
1244                                  info ? info->depth
1245                                       : DEPTH_BELOW_HERE(wc_depth),
1246                                  DEPTH_BELOW_HERE(requested_depth), iterpool));
1247
1248           /* Don't revisit this name in the target or source entries. */
1249           svn_hash_sets(t_entries, name, NULL);
1250           if (s_entries
1251               /* Keep the entry for later process if it is reported as
1252                  excluded and got deleted in repos. */
1253               && (! info || info->depth != svn_depth_exclude || t_entry))
1254             svn_hash_sets(s_entries, name, NULL);
1255
1256           /* pathinfo entries live in their own subpools due to lookahead,
1257              so we need to clear each one out as we finish with it. */
1258           if (info)
1259             svn_pool_destroy(info->pool);
1260         }
1261
1262       /* Remove any deleted entries.  Do this before processing the
1263          target, for graceful handling of case-only renames. */
1264       if (s_entries)
1265         {
1266           for (hi = apr_hash_first(subpool, s_entries);
1267                hi;
1268                hi = apr_hash_next(hi))
1269             {
1270               const svn_fs_dirent_t *s_entry = apr_hash_this_val(hi);
1271
1272               svn_pool_clear(iterpool);
1273
1274               if (svn_hash_gets(t_entries, s_entry->name) == NULL)
1275                 {
1276                   const char *e_fullpath;
1277                   svn_revnum_t deleted_rev;
1278
1279                   if (s_entry->kind == svn_node_file
1280                       && wc_depth < svn_depth_files)
1281                     continue;
1282
1283                   if (s_entry->kind == svn_node_dir
1284                       && (wc_depth < svn_depth_immediates
1285                           || requested_depth == svn_depth_files))
1286                     continue;
1287
1288                   /* There is no corresponding target entry, so delete. */
1289                   e_fullpath = svn_relpath_join(e_path, s_entry->name, iterpool);
1290                   SVN_ERR(svn_repos_deleted_rev(svn_fs_root_fs(b->t_root),
1291                                                 svn_fspath__join(t_path,
1292                                                                  s_entry->name,
1293                                                                  iterpool),
1294                                                 s_rev, b->t_rev,
1295                                                 &deleted_rev, iterpool));
1296
1297                   SVN_ERR(b->editor->delete_entry(e_fullpath,
1298                                                   deleted_rev,
1299                                                   dir_baton, iterpool));
1300                 }
1301             }
1302         }
1303
1304       /* Loop over the dirents in the target. */
1305       SVN_ERR(svn_fs_dir_optimal_order(&t_ordered_entries, b->t_root,
1306                                        t_entries, subpool, iterpool));
1307       for (i = 0; i < t_ordered_entries->nelts; ++i)
1308         {
1309           const svn_fs_dirent_t *t_entry
1310              = APR_ARRAY_IDX(t_ordered_entries, i, svn_fs_dirent_t *);
1311           const svn_fs_dirent_t *s_entry;
1312           const char *s_fullpath, *t_fullpath, *e_fullpath;
1313
1314           svn_pool_clear(iterpool);
1315
1316           if (is_depth_upgrade(wc_depth, requested_depth, t_entry->kind))
1317             {
1318               /* We're making the working copy deeper, pretend the source
1319                  doesn't exist. */
1320               s_entry = NULL;
1321               s_fullpath = NULL;
1322             }
1323           else
1324             {
1325               if (t_entry->kind == svn_node_file
1326                   && requested_depth == svn_depth_unknown
1327                   && wc_depth < svn_depth_files)
1328                 continue;
1329
1330               if (t_entry->kind == svn_node_dir
1331                   && (wc_depth < svn_depth_immediates
1332                       || requested_depth == svn_depth_files))
1333                 continue;
1334
1335               /* Look for an entry with the same name in the source dirents. */
1336               s_entry = s_entries ?
1337                   svn_hash_gets(s_entries, t_entry->name) : NULL;
1338               s_fullpath = s_entry ?
1339                   svn_fspath__join(s_path, t_entry->name, iterpool) : NULL;
1340             }
1341
1342           /* Compose the report, editor, and target paths for this entry. */
1343           e_fullpath = svn_relpath_join(e_path, t_entry->name, iterpool);
1344           t_fullpath = svn_fspath__join(t_path, t_entry->name, iterpool);
1345
1346           SVN_ERR(update_entry(b, s_rev, s_fullpath, s_entry, t_fullpath,
1347                                t_entry, dir_baton, e_fullpath, NULL,
1348                                DEPTH_BELOW_HERE(wc_depth),
1349                                DEPTH_BELOW_HERE(requested_depth),
1350                                iterpool));
1351         }
1352
1353       /* iterpool is destroyed by destroying its parent (subpool) below */
1354     }
1355
1356   svn_pool_destroy(subpool);
1357
1358   return SVN_NO_ERROR;
1359 }
1360
1361 static svn_error_t *
1362 drive(report_baton_t *b, svn_revnum_t s_rev, path_info_t *info,
1363       apr_pool_t *pool)
1364 {
1365   const char *t_anchor, *s_fullpath;
1366   svn_boolean_t allowed, info_is_set_path;
1367   svn_fs_root_t *s_root;
1368   const svn_fs_dirent_t *s_entry, *t_entry;
1369   void *root_baton;
1370
1371   /* Compute the target path corresponding to the working copy anchor,
1372      and check its authorization. */
1373   t_anchor = *b->s_operand ? svn_fspath__dirname(b->t_path, pool) : b->t_path;
1374   SVN_ERR(check_auth(b, &allowed, t_anchor, pool));
1375   if (!allowed)
1376     return svn_error_create
1377       (SVN_ERR_AUTHZ_ROOT_UNREADABLE, NULL,
1378        _("Not authorized to open root of edit operation"));
1379
1380   /* Collect information about the source and target nodes. */
1381   s_fullpath = svn_fspath__join(b->fs_base, b->s_operand, pool);
1382   SVN_ERR(get_source_root(b, &s_root, s_rev));
1383   SVN_ERR(fake_dirent(&s_entry, s_root, s_fullpath, pool));
1384   SVN_ERR(fake_dirent(&t_entry, b->t_root, b->t_path, pool));
1385
1386   /* If the operand is a locally added file or directory, it won't
1387      exist in the source, so accept that. */
1388   info_is_set_path = (SVN_IS_VALID_REVNUM(info->rev) && !info->link_path);
1389   if (info_is_set_path && !s_entry)
1390     s_fullpath = NULL;
1391
1392   /* Check if the target path exists first.  */
1393   if (!*b->s_operand && !(t_entry))
1394     return svn_error_createf(SVN_ERR_FS_PATH_SYNTAX, NULL,
1395                              _("Target path '%s' does not exist"),
1396                              b->t_path);
1397
1398   /* If the anchor is the operand, the source and target must be dirs.
1399      Check this before opening the root to avoid modifying the wc. */
1400   else if (!*b->s_operand && (!s_entry || s_entry->kind != svn_node_dir
1401                               || t_entry->kind != svn_node_dir))
1402     return svn_error_create(SVN_ERR_FS_PATH_SYNTAX, NULL,
1403                             _("Cannot replace a directory from within"));
1404
1405   SVN_ERR(b->editor->set_target_revision(b->edit_baton, b->t_rev, pool));
1406   SVN_ERR(b->editor->open_root(b->edit_baton, s_rev, pool, &root_baton));
1407
1408   /* If the anchor is the operand, diff the two directories; otherwise
1409      update the operand within the anchor directory. */
1410   if (!*b->s_operand)
1411     SVN_ERR(delta_dirs(b, s_rev, s_fullpath, b->t_path, root_baton,
1412                        "", info->start_empty, info->depth, b->requested_depth,
1413                        pool));
1414   else
1415     SVN_ERR(update_entry(b, s_rev, s_fullpath, s_entry, b->t_path,
1416                          t_entry, root_baton, b->s_operand, info,
1417                          info->depth, b->requested_depth, pool));
1418
1419   return svn_error_trace(b->editor->close_directory(root_baton, pool));
1420 }
1421
1422 /* Initialize the baton fields for editor-driving, and drive the editor. */
1423 static svn_error_t *
1424 finish_report(report_baton_t *b, apr_pool_t *pool)
1425 {
1426   path_info_t *info;
1427   apr_pool_t *subpool;
1428   svn_revnum_t s_rev;
1429   int i;
1430
1431   /* Save our pool to manage the lookahead and fs_root cache with. */
1432   b->pool = pool;
1433
1434   /* Add the end marker. */
1435   SVN_ERR(svn_spillbuf__reader_write(b->reader, "-", 1, pool));
1436
1437   /* Read the first pathinfo from the report and verify that it is a top-level
1438      set_path entry. */
1439   SVN_ERR(read_path_info(&info, b->reader, pool));
1440   if (!info || strcmp(info->path, b->s_operand) != 0
1441       || info->link_path || !SVN_IS_VALID_REVNUM(info->rev))
1442     return svn_error_create(SVN_ERR_REPOS_BAD_REVISION_REPORT, NULL,
1443                             _("Invalid report for top level of working copy"));
1444   s_rev = info->rev;
1445
1446   /* Initialize the lookahead pathinfo. */
1447   subpool = svn_pool_create(pool);
1448   SVN_ERR(read_path_info(&b->lookahead, b->reader, subpool));
1449
1450   if (b->lookahead && strcmp(b->lookahead->path, b->s_operand) == 0)
1451     {
1452       /* If the operand of the wc operation is switched or deleted,
1453          then info above is just a place-holder, and the only thing we
1454          have to do is pass the revision it contains to open_root.
1455          The next pathinfo actually describes the target. */
1456       if (!*b->s_operand)
1457         return svn_error_create(SVN_ERR_REPOS_BAD_REVISION_REPORT, NULL,
1458                                 _("Two top-level reports with no target"));
1459       /* If the client issued a set-path followed by a delete-path, we need
1460          to respect the depth set by the initial set-path. */
1461       if (! SVN_IS_VALID_REVNUM(b->lookahead->rev))
1462         {
1463           b->lookahead->depth = info->depth;
1464         }
1465       info = b->lookahead;
1466       SVN_ERR(read_path_info(&b->lookahead, b->reader, subpool));
1467     }
1468
1469   /* Open the target root and initialize the source root cache. */
1470   SVN_ERR(svn_fs_revision_root(&b->t_root, b->repos->fs, b->t_rev, pool));
1471   for (i = 0; i < NUM_CACHED_SOURCE_ROOTS; i++)
1472     b->s_roots[i] = NULL;
1473
1474   {
1475     svn_error_t *err = svn_error_trace(drive(b, s_rev, info, pool));
1476
1477     if (err == SVN_NO_ERROR)
1478       return svn_error_trace(b->editor->close_edit(b->edit_baton, pool));
1479
1480     return svn_error_trace(
1481                 svn_error_compose_create(err,
1482                                          b->editor->abort_edit(b->edit_baton,
1483                                                                pool)));
1484   }
1485 }
1486
1487 /* --- COLLECTING THE REPORT INFORMATION --- */
1488
1489 /* Record a report operation into the spill buffer.  Return an error
1490    if DEPTH is svn_depth_unknown. */
1491 static svn_error_t *
1492 write_path_info(report_baton_t *b, const char *path, const char *lpath,
1493                 svn_revnum_t rev, svn_depth_t depth,
1494                 svn_boolean_t start_empty,
1495                 const char *lock_token, apr_pool_t *pool)
1496 {
1497   const char *lrep, *rrep, *drep, *ltrep, *rep;
1498
1499   /* Munge the path to be anchor-relative, so that we can use edit paths
1500      as report paths. */
1501   path = svn_relpath_join(b->s_operand, path, pool);
1502
1503   lrep = lpath ? apr_psprintf(pool, "+%" APR_SIZE_T_FMT ":%s",
1504                               strlen(lpath), lpath) : "-";
1505   rrep = (SVN_IS_VALID_REVNUM(rev)) ?
1506     apr_psprintf(pool, "+%ld:", rev) : "-";
1507
1508   if (depth == svn_depth_exclude)
1509     drep = "+X";
1510   else if (depth == svn_depth_empty)
1511     drep = "+E";
1512   else if (depth == svn_depth_files)
1513     drep = "+F";
1514   else if (depth == svn_depth_immediates)
1515     drep = "+M";
1516   else if (depth == svn_depth_infinity)
1517     drep = "-";
1518   else
1519     return svn_error_createf(SVN_ERR_REPOS_BAD_ARGS, NULL,
1520                              _("Unsupported report depth '%s'"),
1521                              svn_depth_to_word(depth));
1522
1523   ltrep = lock_token ? apr_psprintf(pool, "+%" APR_SIZE_T_FMT ":%s",
1524                                     strlen(lock_token), lock_token) : "-";
1525   rep = apr_psprintf(pool, "+%" APR_SIZE_T_FMT ":%s%s%s%s%c%s",
1526                      strlen(path), path, lrep, rrep, drep,
1527                      start_empty ? '+' : '-', ltrep);
1528   return svn_error_trace(
1529             svn_spillbuf__reader_write(b->reader, rep, strlen(rep), pool));
1530 }
1531
1532 svn_error_t *
1533 svn_repos_set_path3(void *baton, const char *path, svn_revnum_t rev,
1534                     svn_depth_t depth, svn_boolean_t start_empty,
1535                     const char *lock_token, apr_pool_t *pool)
1536 {
1537   return svn_error_trace(
1538             write_path_info(baton, path, NULL, rev, depth, start_empty,
1539                             lock_token, pool));
1540 }
1541
1542 svn_error_t *
1543 svn_repos_link_path3(void *baton, const char *path, const char *link_path,
1544                      svn_revnum_t rev, svn_depth_t depth,
1545                      svn_boolean_t start_empty,
1546                      const char *lock_token, apr_pool_t *pool)
1547 {
1548   if (depth == svn_depth_exclude)
1549     return svn_error_create(SVN_ERR_REPOS_BAD_ARGS, NULL,
1550                             _("Depth 'exclude' not supported for link"));
1551
1552   return svn_error_trace(
1553             write_path_info(baton, path, link_path, rev, depth,
1554                             start_empty, lock_token, pool));
1555 }
1556
1557 svn_error_t *
1558 svn_repos_delete_path(void *baton, const char *path, apr_pool_t *pool)
1559 {
1560   /* We pass svn_depth_infinity because deletion of a path always
1561      deletes everything underneath it. */
1562   return svn_error_trace(
1563             write_path_info(baton, path, NULL, SVN_INVALID_REVNUM,
1564                             svn_depth_infinity, FALSE, NULL, pool));
1565 }
1566
1567 svn_error_t *
1568 svn_repos_finish_report(void *baton, apr_pool_t *pool)
1569 {
1570   report_baton_t *b = baton;
1571
1572   SVN_ERR(svn_fs_refresh_revision_props(svn_repos_fs(b->repos), pool));
1573   return svn_error_trace(finish_report(b, pool));
1574 }
1575
1576 svn_error_t *
1577 svn_repos_abort_report(void *baton, apr_pool_t *pool)
1578 {
1579   return SVN_NO_ERROR;
1580 }
1581
1582 /* --- BEGINNING THE REPORT --- */
1583
1584
1585 svn_error_t *
1586 svn_repos_begin_report3(void **report_baton,
1587                         svn_revnum_t revnum,
1588                         svn_repos_t *repos,
1589                         const char *fs_base,
1590                         const char *s_operand,
1591                         const char *switch_path,
1592                         svn_boolean_t text_deltas,
1593                         svn_depth_t depth,
1594                         svn_boolean_t ignore_ancestry,
1595                         svn_boolean_t send_copyfrom_args,
1596                         const svn_delta_editor_t *editor,
1597                         void *edit_baton,
1598                         svn_repos_authz_func_t authz_read_func,
1599                         void *authz_read_baton,
1600                         apr_size_t zero_copy_limit,
1601                         apr_pool_t *pool)
1602 {
1603   report_baton_t *b;
1604   const char *uuid;
1605
1606   if (depth == svn_depth_exclude)
1607     return svn_error_create(SVN_ERR_REPOS_BAD_ARGS, NULL,
1608                             _("Request depth 'exclude' not supported"));
1609
1610   SVN_ERR(svn_fs_get_uuid(repos->fs, &uuid, pool));
1611
1612   /* Build a reporter baton.  Copy strings in case the caller doesn't
1613      keep track of them. */
1614   b = apr_palloc(pool, sizeof(*b));
1615   b->repos = repos;
1616   b->fs_base = svn_fspath__canonicalize(fs_base, pool);
1617   b->s_operand = apr_pstrdup(pool, s_operand);
1618   b->t_rev = revnum;
1619   b->t_path = switch_path ? svn_fspath__canonicalize(switch_path, pool)
1620                           : svn_fspath__join(b->fs_base, s_operand, pool);
1621   b->text_deltas = text_deltas;
1622   b->zero_copy_limit = zero_copy_limit;
1623   b->requested_depth = depth;
1624   b->ignore_ancestry = ignore_ancestry;
1625   b->send_copyfrom_args = send_copyfrom_args;
1626   b->is_switch = (switch_path != NULL);
1627   b->editor = editor;
1628   b->edit_baton = edit_baton;
1629   b->authz_read_func = authz_read_func;
1630   b->authz_read_baton = authz_read_baton;
1631   b->revision_infos = apr_hash_make(pool);
1632   b->pool = pool;
1633   b->reader = svn_spillbuf__reader_create(1000 /* blocksize */,
1634                                           1000000 /* maxsize */,
1635                                           pool);
1636   b->repos_uuid = svn_string_create(uuid, pool);
1637
1638   /* Hand reporter back to client. */
1639   *report_baton = b;
1640   return SVN_NO_ERROR;
1641 }