2 * node_tree.c: an editor for tracking repository deltas changes
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * ====================================================================
24 /* ==================================================================== */
31 #define APR_WANT_STRFUNC
33 #include <apr_pools.h>
35 #include "svn_types.h"
36 #include "svn_error.h"
37 #include "svn_dirent_uri.h"
39 #include "svn_delta.h"
41 #include "svn_repos.h"
43 #include "svn_private_config.h"
44 #include "private/svn_fspath.h"
46 /*** NOTE: This editor is unique in that it currently is hard-coded to
47 be anchored at the root directory of the filesystem. This
48 affords us the ability to use the same paths for filesystem
49 locations and editor paths. ***/
53 /*** Node creation and assembly structures and routines. ***/
54 static svn_repos_node_t *
55 create_node(const char *name,
56 svn_repos_node_t *parent,
59 svn_repos_node_t *node = apr_pcalloc(pool, sizeof(*node));
61 node->kind = svn_node_unknown;
62 node->name = apr_pstrdup(pool, name);
63 node->parent = parent;
68 static svn_repos_node_t *
69 create_sibling_node(svn_repos_node_t *elder,
73 svn_repos_node_t *tmp_node;
75 /* No ELDER sibling? That's just not gonna work out. */
79 /* Run to the end of the list of siblings of ELDER. */
81 while (tmp_node->sibling)
82 tmp_node = tmp_node->sibling;
84 /* Create a new youngest sibling and return that. */
85 return (tmp_node->sibling = create_node(name, elder->parent, pool));
89 static svn_repos_node_t *
90 create_child_node(svn_repos_node_t *parent,
94 /* No PARENT node? That's just not gonna work out. */
98 /* If PARENT has no children, create its first one and return that. */
100 return (parent->child = create_node(name, parent, pool));
102 /* If PARENT already has a child, create a new sibling for its first
103 child and return that. */
104 return create_sibling_node(parent->child, name, pool);
108 static svn_repos_node_t *
109 find_child_by_name(svn_repos_node_t *parent,
112 svn_repos_node_t *tmp_node;
114 /* No PARENT node, or a barren PARENT? Nothing to find. */
115 if ((! parent) || (! parent->child))
118 /* Look through the children for a node with a matching name. */
119 tmp_node = parent->child;
122 if (! strcmp(tmp_node->name, name))
128 if (tmp_node->sibling)
129 tmp_node = tmp_node->sibling;
140 find_real_base_location(const char **path_p,
142 svn_repos_node_t *node,
145 /* If NODE is an add-with-history, then its real base location is
147 if ((node->action == 'A')
148 && node->copyfrom_path
149 && SVN_IS_VALID_REVNUM(node->copyfrom_rev))
151 *path_p = node->copyfrom_path;
152 *rev_p = node->copyfrom_rev;
156 /* Otherwise, if NODE has a parent, we'll recurse, and add NODE's
157 name to whatever the parent's real base path turns out to be (and
158 pass the base revision on through). */
164 find_real_base_location(&path, &rev, node->parent, pool);
165 *path_p = svn_fspath__join(path, node->name, pool);
170 /* Finally, if the node has no parent, then its name is "/", and it
171 has no interesting base revision. */
173 *rev_p = SVN_INVALID_REVNUM;
180 /*** Editor functions and batons. ***/
186 svn_fs_root_t *base_root;
187 apr_pool_t *node_pool;
188 svn_repos_node_t *node;
194 struct edit_baton *edit_baton;
195 struct node_baton *parent_baton;
196 svn_repos_node_t *node;
201 delete_entry(const char *path,
202 svn_revnum_t revision,
206 struct node_baton *d = parent_baton;
207 struct edit_baton *eb = d->edit_baton;
208 svn_repos_node_t *node;
210 const char *base_path;
211 svn_revnum_t base_rev;
212 svn_fs_root_t *base_root;
213 svn_node_kind_t kind;
215 /* Get (or create) the change node and update it. */
216 name = svn_relpath_basename(path, pool);
217 node = find_child_by_name(d->node, name);
219 node = create_child_node(d->node, name, eb->node_pool);
222 /* We need to look up this node's parents to see what its original
223 path in the filesystem was. Why? Because if this deletion
224 occurred underneath a copied path, the thing that was deleted
225 probably lived at a different location (relative to the copy
227 find_real_base_location(&base_path, &base_rev, node, pool);
228 if (! SVN_IS_VALID_REVNUM(base_rev))
230 /* No interesting base revision? We'll just look for the path
232 base_root = eb->base_root;
236 /* Oh. Perhaps some copy goodness happened somewhere? */
237 SVN_ERR(svn_fs_revision_root(&base_root, eb->fs, base_rev, pool));
240 /* Now figure out if this thing was a file or a dir. */
241 SVN_ERR(svn_fs_check_path(&kind, base_root, base_path, pool));
242 if (kind == svn_node_none)
243 return svn_error_createf(SVN_ERR_FS_NOT_FOUND, NULL,
244 _("'%s' not found in filesystem"), path);
253 add_open_helper(const char *path,
255 svn_node_kind_t kind,
257 const char *copyfrom_path,
258 svn_revnum_t copyfrom_rev,
262 struct node_baton *pb = parent_baton;
263 struct edit_baton *eb = pb->edit_baton;
264 struct node_baton *nb = apr_pcalloc(pool, sizeof(*nb));
266 SVN_ERR_ASSERT(parent_baton && path);
269 nb->parent_baton = pb;
271 /* Create and populate the node. */
272 nb->node = create_child_node(pb->node, svn_relpath_basename(path, NULL),
274 nb->node->kind = kind;
275 nb->node->action = action;
276 nb->node->copyfrom_rev = copyfrom_rev;
277 nb->node->copyfrom_path =
278 copyfrom_path ? apr_pstrdup(eb->node_pool, copyfrom_path) : NULL;
286 open_root(void *edit_baton,
287 svn_revnum_t base_revision,
291 struct edit_baton *eb = edit_baton;
292 struct node_baton *d = apr_pcalloc(pool, sizeof(*d));
295 d->parent_baton = NULL;
296 d->node = (eb->node = create_node("", NULL, eb->node_pool));
297 d->node->kind = svn_node_dir;
298 d->node->action = 'R';
306 open_directory(const char *path,
308 svn_revnum_t base_revision,
312 return add_open_helper(path, 'R', svn_node_dir, parent_baton,
313 NULL, SVN_INVALID_REVNUM,
319 add_directory(const char *path,
321 const char *copyfrom_path,
322 svn_revnum_t copyfrom_revision,
326 return add_open_helper(path, 'A', svn_node_dir, parent_baton,
327 copyfrom_path, copyfrom_revision,
333 open_file(const char *path,
335 svn_revnum_t base_revision,
339 return add_open_helper(path, 'R', svn_node_file, parent_baton,
340 NULL, SVN_INVALID_REVNUM,
346 add_file(const char *path,
348 const char *copyfrom_path,
349 svn_revnum_t copyfrom_revision,
353 return add_open_helper(path, 'A', svn_node_file, parent_baton,
354 copyfrom_path, copyfrom_revision,
360 apply_textdelta(void *file_baton,
361 const char *base_checksum,
363 svn_txdelta_window_handler_t *handler,
364 void **handler_baton)
366 struct node_baton *fb = file_baton;
367 fb->node->text_mod = TRUE;
368 *handler = svn_delta_noop_window_handler;
369 *handler_baton = NULL;
376 change_node_prop(void *node_baton,
378 const svn_string_t *value,
381 struct node_baton *nb = node_baton;
382 nb->node->prop_mod = TRUE;
388 svn_repos_node_editor(const svn_delta_editor_t **editor,
391 svn_fs_root_t *base_root,
393 apr_pool_t *node_pool,
396 svn_delta_editor_t *my_editor;
397 struct edit_baton *my_edit_baton;
399 /* Set up the editor. */
400 my_editor = svn_delta_default_editor(pool);
401 my_editor->open_root = open_root;
402 my_editor->delete_entry = delete_entry;
403 my_editor->add_directory = add_directory;
404 my_editor->open_directory = open_directory;
405 my_editor->add_file = add_file;
406 my_editor->open_file = open_file;
407 my_editor->apply_textdelta = apply_textdelta;
408 my_editor->change_file_prop = change_node_prop;
409 my_editor->change_dir_prop = change_node_prop;
411 /* Set up the edit baton. */
412 my_edit_baton = apr_pcalloc(pool, sizeof(*my_edit_baton));
413 my_edit_baton->node_pool = node_pool;
414 my_edit_baton->fs = repos->fs;
415 my_edit_baton->root = root;
416 my_edit_baton->base_root = base_root;
419 *edit_baton = my_edit_baton;
427 svn_repos_node_from_baton(void *edit_baton)
429 struct edit_baton *eb = edit_baton;