1 /* dag.c : DAG-like interface filesystem, private to libsvn_fs
3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
20 * ====================================================================
26 #include "svn_error.h"
28 #include "svn_props.h"
29 #include "svn_pools.h"
31 #include "cached_data.h"
36 #include "transaction.h"
38 #include "../libsvn_fs/fs-loader.h"
40 #include "private/svn_fspath.h"
41 #include "svn_private_config.h"
42 #include "private/svn_temp_serializer.h"
43 #include "temp_serializer.h"
46 /* Initializing a filesystem. */
50 /* The filesystem this dag node came from. */
53 /* The node revision ID for this dag node, allocated in POOL. */
56 /* In the special case that this node is the root of a transaction
57 that has not yet been modified, the node revision ID for this dag
58 node's predecessor; otherwise NULL. (Used in
59 svn_fs_node_created_rev.) */
60 const svn_fs_id_t *fresh_root_predecessor_id;
62 /* The node's type (file, dir, etc.) */
65 /* The node's NODE-REVISION, or NULL if we haven't read it in yet.
66 This is allocated in this node's POOL.
68 If you're willing to respect all the rules above, you can munge
69 this yourself, but you're probably better off just calling
70 `get_node_revision' and `set_node_revision', which take care of
72 node_revision_t *node_revision;
74 /* The pool to allocate NODE_REVISION in. */
75 apr_pool_t *node_pool;
77 /* the path at which this node was created. */
78 const char *created_path;
83 /* Trivial helper/accessor functions. */
84 svn_node_kind_t svn_fs_fs__dag_node_kind(dag_node_t *node)
91 svn_fs_fs__dag_get_id(const dag_node_t *node)
98 svn_fs_fs__dag_get_created_path(dag_node_t *node)
100 return node->created_path;
105 svn_fs_fs__dag_get_fs(dag_node_t *node)
111 svn_fs_fs__dag_set_fs(dag_node_t *node, svn_fs_t *fs)
117 /* Dup NODEREV and all associated data into POOL.
118 Leaves the id and is_fresh_txn_root fields as zero bytes. */
119 static node_revision_t *
120 copy_node_revision(node_revision_t *noderev,
123 node_revision_t *nr = apr_pcalloc(pool, sizeof(*nr));
124 nr->kind = noderev->kind;
125 if (noderev->predecessor_id)
126 nr->predecessor_id = svn_fs_fs__id_copy(noderev->predecessor_id, pool);
127 nr->predecessor_count = noderev->predecessor_count;
128 if (noderev->copyfrom_path)
129 nr->copyfrom_path = apr_pstrdup(pool, noderev->copyfrom_path);
130 nr->copyfrom_rev = noderev->copyfrom_rev;
131 nr->copyroot_path = apr_pstrdup(pool, noderev->copyroot_path);
132 nr->copyroot_rev = noderev->copyroot_rev;
133 nr->data_rep = svn_fs_fs__rep_copy(noderev->data_rep, pool);
134 nr->prop_rep = svn_fs_fs__rep_copy(noderev->prop_rep, pool);
135 nr->mergeinfo_count = noderev->mergeinfo_count;
136 nr->has_mergeinfo = noderev->has_mergeinfo;
138 if (noderev->created_path)
139 nr->created_path = apr_pstrdup(pool, noderev->created_path);
144 /* Set *NODEREV_P to the cached node-revision for NODE.
145 If the node-revision was not already cached in NODE, read it in,
146 allocating the cache in NODE->NODE_POOL.
148 If you plan to change the contents of NODE, be careful! We're
149 handing you a pointer directly to our cached node-revision, not
150 your own copy. If you change it as part of some operation, but
151 then some Berkeley DB function deadlocks or gets an error, you'll
152 need to back out your changes, or else the cache will reflect
153 changes that never got committed. It's probably best not to change
154 the structure at all. */
156 get_node_revision(node_revision_t **noderev_p,
159 /* If we've already got a copy, there's no need to read it in. */
160 if (! node->node_revision)
162 node_revision_t *noderev;
163 apr_pool_t *scratch_pool = svn_pool_create(node->node_pool);
165 SVN_ERR(svn_fs_fs__get_node_revision(&noderev, node->fs,
166 node->id, node->node_pool,
168 node->node_revision = noderev;
169 svn_pool_destroy(scratch_pool);
172 /* Now NODE->node_revision is set. */
173 *noderev_p = node->node_revision;
178 svn_boolean_t svn_fs_fs__dag_check_mutable(const dag_node_t *node)
180 return svn_fs_fs__id_is_txn(svn_fs_fs__dag_get_id(node));
185 svn_fs_fs__dag_get_node(dag_node_t **node,
187 const svn_fs_id_t *id,
190 dag_node_t *new_node;
191 node_revision_t *noderev;
193 /* Construct the node. */
194 new_node = apr_pcalloc(pool, sizeof(*new_node));
196 new_node->id = svn_fs_fs__id_copy(id, pool);
198 /* Grab the contents so we can inspect the node's kind and created path. */
199 new_node->node_pool = pool;
200 SVN_ERR(get_node_revision(&noderev, new_node));
202 /* Initialize the KIND and CREATED_PATH attributes */
203 new_node->kind = noderev->kind;
204 new_node->created_path = apr_pstrdup(pool, noderev->created_path);
206 if (noderev->is_fresh_txn_root)
207 new_node->fresh_root_predecessor_id = noderev->predecessor_id;
209 new_node->fresh_root_predecessor_id = NULL;
211 /* Return a fresh new node */
218 svn_fs_fs__dag_get_revision(svn_revnum_t *rev,
222 /* In the special case that this is an unmodified transaction root,
223 we need to actually get the revision of the noderev's predecessor
224 (the revision root); see Issue #2608. */
225 const svn_fs_id_t *correct_id = node->fresh_root_predecessor_id
226 ? node->fresh_root_predecessor_id : node->id;
228 /* Look up the committed revision from the Node-ID. */
229 *rev = svn_fs_fs__id_rev(correct_id);
236 svn_fs_fs__dag_get_predecessor_id(const svn_fs_id_t **id_p,
239 node_revision_t *noderev;
241 SVN_ERR(get_node_revision(&noderev, node));
242 *id_p = noderev->predecessor_id;
248 svn_fs_fs__dag_get_predecessor_count(int *count,
251 node_revision_t *noderev;
253 SVN_ERR(get_node_revision(&noderev, node));
254 *count = noderev->predecessor_count;
259 svn_fs_fs__dag_get_mergeinfo_count(apr_int64_t *count,
262 node_revision_t *noderev;
264 SVN_ERR(get_node_revision(&noderev, node));
265 *count = noderev->mergeinfo_count;
270 svn_fs_fs__dag_has_mergeinfo(svn_boolean_t *has_mergeinfo,
273 node_revision_t *noderev;
275 SVN_ERR(get_node_revision(&noderev, node));
276 *has_mergeinfo = noderev->has_mergeinfo;
281 svn_fs_fs__dag_has_descendants_with_mergeinfo(svn_boolean_t *do_they,
284 node_revision_t *noderev;
286 if (node->kind != svn_node_dir)
292 SVN_ERR(get_node_revision(&noderev, node));
293 if (noderev->mergeinfo_count > 1)
295 else if (noderev->mergeinfo_count == 1 && !noderev->has_mergeinfo)
303 /*** Directory node functions ***/
305 /* Some of these are helpers for functions outside this section. */
307 /* Set *ID_P to the node-id for entry NAME in PARENT. If no such
308 entry, set *ID_P to NULL but do not error. The node-id is
309 allocated in POOL. */
311 dir_entry_id_from_node(const svn_fs_id_t **id_p,
314 apr_pool_t *result_pool,
315 apr_pool_t *scratch_pool)
317 svn_fs_dirent_t *dirent;
319 SVN_ERR(svn_fs_fs__dag_dir_entry(&dirent, parent, name, result_pool,
321 *id_p = dirent ? dirent->id : NULL;
327 /* Add or set in PARENT a directory entry NAME pointing to ID.
328 Allocations are done in POOL.
331 - PARENT is a mutable directory.
332 - ID does not refer to an ancestor of parent
333 - NAME is a single path component
336 set_entry(dag_node_t *parent,
338 const svn_fs_id_t *id,
339 svn_node_kind_t kind,
340 const svn_fs_fs__id_part_t *txn_id,
343 node_revision_t *parent_noderev;
345 /* Get the parent's node-revision. */
346 SVN_ERR(get_node_revision(&parent_noderev, parent));
348 /* Set the new entry. */
349 return svn_fs_fs__set_entry(parent->fs, txn_id, parent_noderev, name, id,
354 /* Make a new entry named NAME in PARENT. If IS_DIR is true, then the
355 node revision the new entry points to will be a directory, else it
356 will be a file. The new node will be allocated in POOL. PARENT
357 must be mutable, and must not have an entry named NAME.
359 Use POOL for all allocations, except caching the node_revision in PARENT.
362 make_entry(dag_node_t **child_p,
364 const char *parent_path,
366 svn_boolean_t is_dir,
367 const svn_fs_fs__id_part_t *txn_id,
370 const svn_fs_id_t *new_node_id;
371 node_revision_t new_noderev, *parent_noderev;
373 /* Make sure that NAME is a single path component. */
374 if (! svn_path_is_single_path_component(name))
375 return svn_error_createf
376 (SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
377 _("Attempted to create a node with an illegal name '%s'"), name);
379 /* Make sure that parent is a directory */
380 if (parent->kind != svn_node_dir)
381 return svn_error_create
382 (SVN_ERR_FS_NOT_DIRECTORY, NULL,
383 _("Attempted to create entry in non-directory parent"));
385 /* Check that the parent is mutable. */
386 if (! svn_fs_fs__dag_check_mutable(parent))
387 return svn_error_createf
388 (SVN_ERR_FS_NOT_MUTABLE, NULL,
389 _("Attempted to clone child of non-mutable node"));
391 /* Create the new node's NODE-REVISION */
392 memset(&new_noderev, 0, sizeof(new_noderev));
393 new_noderev.kind = is_dir ? svn_node_dir : svn_node_file;
394 new_noderev.created_path = svn_fspath__join(parent_path, name, pool);
396 SVN_ERR(get_node_revision(&parent_noderev, parent));
397 new_noderev.copyroot_path = apr_pstrdup(pool,
398 parent_noderev->copyroot_path);
399 new_noderev.copyroot_rev = parent_noderev->copyroot_rev;
400 new_noderev.copyfrom_rev = SVN_INVALID_REVNUM;
401 new_noderev.copyfrom_path = NULL;
403 SVN_ERR(svn_fs_fs__create_node
404 (&new_node_id, svn_fs_fs__dag_get_fs(parent), &new_noderev,
405 svn_fs_fs__id_copy_id(svn_fs_fs__dag_get_id(parent)),
408 /* Create a new dag_node_t for our new node */
409 SVN_ERR(svn_fs_fs__dag_get_node(child_p, svn_fs_fs__dag_get_fs(parent),
412 /* We can safely call set_entry because we already know that
413 PARENT is mutable, and we just created CHILD, so we know it has
414 no ancestors (therefore, PARENT cannot be an ancestor of CHILD) */
415 return set_entry(parent, name, svn_fs_fs__dag_get_id(*child_p),
416 new_noderev.kind, txn_id, pool);
421 svn_fs_fs__dag_dir_entries(apr_array_header_t **entries,
425 node_revision_t *noderev;
427 SVN_ERR(get_node_revision(&noderev, node));
429 if (noderev->kind != svn_node_dir)
430 return svn_error_create(SVN_ERR_FS_NOT_DIRECTORY, NULL,
431 _("Can't get entries of non-directory"));
433 return svn_fs_fs__rep_contents_dir(entries, node->fs, noderev, pool, pool);
437 svn_fs_fs__dag_dir_entry(svn_fs_dirent_t **dirent,
440 apr_pool_t *result_pool,
441 apr_pool_t *scratch_pool)
443 node_revision_t *noderev;
444 SVN_ERR(get_node_revision(&noderev, node));
446 if (noderev->kind != svn_node_dir)
447 return svn_error_create(SVN_ERR_FS_NOT_DIRECTORY, NULL,
448 _("Can't get entries of non-directory"));
450 /* Get a dirent hash for this directory. */
451 return svn_fs_fs__rep_contents_dir_entry(dirent, node->fs, noderev, name,
452 result_pool, scratch_pool);
457 svn_fs_fs__dag_set_entry(dag_node_t *node,
458 const char *entry_name,
459 const svn_fs_id_t *id,
460 svn_node_kind_t kind,
461 const svn_fs_fs__id_part_t *txn_id,
464 /* Check it's a directory. */
465 if (node->kind != svn_node_dir)
466 return svn_error_create
467 (SVN_ERR_FS_NOT_DIRECTORY, NULL,
468 _("Attempted to set entry in non-directory node"));
470 /* Check it's mutable. */
471 if (! svn_fs_fs__dag_check_mutable(node))
472 return svn_error_create
473 (SVN_ERR_FS_NOT_MUTABLE, NULL,
474 _("Attempted to set entry in immutable node"));
476 return set_entry(node, entry_name, id, kind, txn_id, pool);
484 svn_fs_fs__dag_get_proplist(apr_hash_t **proplist_p,
488 node_revision_t *noderev;
489 apr_hash_t *proplist = NULL;
491 SVN_ERR(get_node_revision(&noderev, node));
493 SVN_ERR(svn_fs_fs__get_proplist(&proplist, node->fs,
496 *proplist_p = proplist;
502 svn_fs_fs__dag_has_props(svn_boolean_t *has_props,
504 apr_pool_t *scratch_pool)
506 node_revision_t *noderev;
508 SVN_ERR(get_node_revision(&noderev, node));
510 if (! noderev->prop_rep)
512 *has_props = FALSE; /* Easy out */
516 if (svn_fs_fs__id_txn_used(&noderev->prop_rep->txn_id))
518 /* We are in a commit or something. Check actual properties */
519 apr_hash_t *proplist;
521 SVN_ERR(svn_fs_fs__get_proplist(&proplist, node->fs,
522 noderev, scratch_pool));
524 *has_props = proplist ? (0 < apr_hash_count(proplist)) : FALSE;
528 /* Properties are stored as a standard hash stream,
529 always ending with "END\n" (4 bytes) */
530 *has_props = (noderev->prop_rep->expanded_size > 4
531 || (noderev->prop_rep->expanded_size == 0
532 && noderev->prop_rep->size > 4));
539 svn_fs_fs__dag_set_proplist(dag_node_t *node,
540 apr_hash_t *proplist,
543 node_revision_t *noderev;
545 /* Sanity check: this node better be mutable! */
546 if (! svn_fs_fs__dag_check_mutable(node))
548 svn_string_t *idstr = svn_fs_fs__id_unparse(node->id, pool);
549 return svn_error_createf
550 (SVN_ERR_FS_NOT_MUTABLE, NULL,
551 "Can't set proplist on *immutable* node-revision %s",
555 /* Go get a fresh NODE-REVISION for this node. */
556 SVN_ERR(get_node_revision(&noderev, node));
558 /* Set the new proplist. */
559 return svn_fs_fs__set_proplist(node->fs, noderev, proplist, pool);
564 svn_fs_fs__dag_increment_mergeinfo_count(dag_node_t *node,
565 apr_int64_t increment,
568 node_revision_t *noderev;
570 /* Sanity check: this node better be mutable! */
571 if (! svn_fs_fs__dag_check_mutable(node))
573 svn_string_t *idstr = svn_fs_fs__id_unparse(node->id, pool);
574 return svn_error_createf
575 (SVN_ERR_FS_NOT_MUTABLE, NULL,
576 "Can't increment mergeinfo count on *immutable* node-revision %s",
583 /* Go get a fresh NODE-REVISION for this node. */
584 SVN_ERR(get_node_revision(&noderev, node));
586 noderev->mergeinfo_count += increment;
587 if (noderev->mergeinfo_count < 0)
589 svn_string_t *idstr = svn_fs_fs__id_unparse(node->id, pool);
590 return svn_error_createf
591 (SVN_ERR_FS_CORRUPT, NULL,
593 _("Can't increment mergeinfo count on node-revision %%s "
594 "to negative value %%%s"),
596 idstr->data, noderev->mergeinfo_count);
598 if (noderev->mergeinfo_count > 1 && noderev->kind == svn_node_file)
600 svn_string_t *idstr = svn_fs_fs__id_unparse(node->id, pool);
601 return svn_error_createf
602 (SVN_ERR_FS_CORRUPT, NULL,
604 _("Can't increment mergeinfo count on *file* "
605 "node-revision %%s to %%%s (> 1)"),
607 idstr->data, noderev->mergeinfo_count);
611 return svn_fs_fs__put_node_revision(node->fs, noderev->id,
612 noderev, FALSE, pool);
616 svn_fs_fs__dag_set_has_mergeinfo(dag_node_t *node,
617 svn_boolean_t has_mergeinfo,
620 node_revision_t *noderev;
622 /* Sanity check: this node better be mutable! */
623 if (! svn_fs_fs__dag_check_mutable(node))
625 svn_string_t *idstr = svn_fs_fs__id_unparse(node->id, pool);
626 return svn_error_createf
627 (SVN_ERR_FS_NOT_MUTABLE, NULL,
628 "Can't set mergeinfo flag on *immutable* node-revision %s",
632 /* Go get a fresh NODE-REVISION for this node. */
633 SVN_ERR(get_node_revision(&noderev, node));
635 noderev->has_mergeinfo = has_mergeinfo;
638 return svn_fs_fs__put_node_revision(node->fs, noderev->id,
639 noderev, FALSE, pool);
646 svn_fs_fs__dag_revision_root(dag_node_t **node_p,
651 dag_node_t *new_node;
653 /* Construct the node. */
654 new_node = apr_pcalloc(pool, sizeof(*new_node));
656 SVN_ERR(svn_fs_fs__rev_get_root(&new_node->id, fs, rev, pool, pool));
658 /* Grab the contents so we can inspect the node's kind and created path. */
659 new_node->node_pool = pool;
661 /* Initialize the KIND and CREATED_PATH attributes */
662 new_node->kind = svn_node_dir;
663 new_node->created_path = "/";
664 new_node->fresh_root_predecessor_id = NULL;
666 /* Return a fresh new node */
673 svn_fs_fs__dag_txn_root(dag_node_t **node_p,
675 const svn_fs_fs__id_part_t *txn_id,
678 const svn_fs_id_t *root_id, *ignored;
680 SVN_ERR(svn_fs_fs__get_txn_ids(&root_id, &ignored, fs, txn_id, pool));
681 return svn_fs_fs__dag_get_node(node_p, fs, root_id, pool);
686 svn_fs_fs__dag_txn_base_root(dag_node_t **node_p,
688 const svn_fs_fs__id_part_t *txn_id,
691 const svn_fs_id_t *base_root_id, *ignored;
693 SVN_ERR(svn_fs_fs__get_txn_ids(&ignored, &base_root_id, fs, txn_id, pool));
694 return svn_fs_fs__dag_get_node(node_p, fs, base_root_id, pool);
699 svn_fs_fs__dag_clone_child(dag_node_t **child_p,
701 const char *parent_path,
703 const svn_fs_fs__id_part_t *copy_id,
704 const svn_fs_fs__id_part_t *txn_id,
705 svn_boolean_t is_parent_copyroot,
708 dag_node_t *cur_entry; /* parent's current entry named NAME */
709 const svn_fs_id_t *new_node_id; /* node id we'll put into NEW_NODE */
710 svn_fs_t *fs = svn_fs_fs__dag_get_fs(parent);
711 apr_pool_t *subpool = svn_pool_create(pool);
713 /* First check that the parent is mutable. */
714 if (! svn_fs_fs__dag_check_mutable(parent))
715 return svn_error_createf
716 (SVN_ERR_FS_NOT_MUTABLE, NULL,
717 "Attempted to clone child of non-mutable node");
719 /* Make sure that NAME is a single path component. */
720 if (! svn_path_is_single_path_component(name))
721 return svn_error_createf
722 (SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
723 "Attempted to make a child clone with an illegal name '%s'", name);
725 /* Find the node named NAME in PARENT's entries list if it exists. */
726 SVN_ERR(svn_fs_fs__dag_open(&cur_entry, parent, name, pool, subpool));
728 return svn_error_createf
729 (SVN_ERR_FS_NOT_FOUND, NULL,
730 "Attempted to open non-existent child node '%s'", name);
732 /* Check for mutability in the node we found. If it's mutable, we
733 don't need to clone it. */
734 if (svn_fs_fs__dag_check_mutable(cur_entry))
736 /* This has already been cloned */
737 new_node_id = cur_entry->id;
741 node_revision_t *noderev, *parent_noderev;
743 /* Go get a fresh NODE-REVISION for current child node. */
744 SVN_ERR(get_node_revision(&noderev, cur_entry));
746 if (is_parent_copyroot)
748 SVN_ERR(get_node_revision(&parent_noderev, parent));
749 noderev->copyroot_rev = parent_noderev->copyroot_rev;
750 noderev->copyroot_path = apr_pstrdup(pool,
751 parent_noderev->copyroot_path);
754 noderev->copyfrom_path = NULL;
755 noderev->copyfrom_rev = SVN_INVALID_REVNUM;
757 noderev->predecessor_id = svn_fs_fs__id_copy(cur_entry->id, pool);
758 if (noderev->predecessor_count != -1)
759 noderev->predecessor_count++;
760 noderev->created_path = svn_fspath__join(parent_path, name, pool);
762 SVN_ERR(svn_fs_fs__create_successor(&new_node_id, fs, cur_entry->id,
763 noderev, copy_id, txn_id, pool));
765 /* Replace the ID in the parent's ENTRY list with the ID which
766 refers to the mutable clone of this child. */
767 SVN_ERR(set_entry(parent, name, new_node_id, noderev->kind, txn_id,
771 /* Initialize the youngster. */
772 svn_pool_destroy(subpool);
773 return svn_fs_fs__dag_get_node(child_p, fs, new_node_id, pool);
779 svn_fs_fs__dag_clone_root(dag_node_t **root_p,
781 const svn_fs_fs__id_part_t *txn_id,
784 const svn_fs_id_t *base_root_id, *root_id;
786 /* Get the node ID's of the root directories of the transaction and
787 its base revision. */
788 SVN_ERR(svn_fs_fs__get_txn_ids(&root_id, &base_root_id, fs, txn_id, pool));
790 /* Oh, give me a clone...
791 (If they're the same, we haven't cloned the transaction's root
793 SVN_ERR_ASSERT(!svn_fs_fs__id_eq(root_id, base_root_id));
796 * (Sung to the tune of "Home, Home on the Range", with thanks to
797 * Randall Garrett and Isaac Asimov.)
800 /* One way or another, root_id now identifies a cloned root node. */
801 return svn_fs_fs__dag_get_node(root_p, fs, root_id, pool);
806 svn_fs_fs__dag_delete(dag_node_t *parent,
808 const svn_fs_fs__id_part_t *txn_id,
811 node_revision_t *parent_noderev;
812 svn_fs_t *fs = parent->fs;
813 svn_fs_dirent_t *dirent;
817 /* Make sure parent is a directory. */
818 if (parent->kind != svn_node_dir)
819 return svn_error_createf
820 (SVN_ERR_FS_NOT_DIRECTORY, NULL,
821 "Attempted to delete entry '%s' from *non*-directory node", name);
823 /* Make sure parent is mutable. */
824 if (! svn_fs_fs__dag_check_mutable(parent))
825 return svn_error_createf
826 (SVN_ERR_FS_NOT_MUTABLE, NULL,
827 "Attempted to delete entry '%s' from immutable directory node", name);
829 /* Make sure that NAME is a single path component. */
830 if (! svn_path_is_single_path_component(name))
831 return svn_error_createf
832 (SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
833 "Attempted to delete a node with an illegal name '%s'", name);
835 /* Get a fresh NODE-REVISION for the parent node. */
836 SVN_ERR(get_node_revision(&parent_noderev, parent));
838 subpool = svn_pool_create(pool);
840 /* Search this directory for a dirent with that NAME. */
841 SVN_ERR(svn_fs_fs__rep_contents_dir_entry(&dirent, fs, parent_noderev,
842 name, subpool, subpool));
844 /* If we never found ID in ENTRIES (perhaps because there are no
845 ENTRIES, perhaps because ID just isn't in the existing ENTRIES
846 ... it doesn't matter), return an error. */
848 return svn_error_createf
849 (SVN_ERR_FS_NO_SUCH_ENTRY, NULL,
850 "Delete failed--directory has no entry '%s'", name);
852 /* Copy the ID out of the subpool and release the rest of the
853 directory listing. */
854 id = svn_fs_fs__id_copy(dirent->id, pool);
855 svn_pool_destroy(subpool);
857 /* If mutable, remove it and any mutable children from db. */
858 SVN_ERR(svn_fs_fs__dag_delete_if_mutable(parent->fs, id, pool));
860 /* Remove this entry from its parent's entries list. */
861 return svn_fs_fs__set_entry(parent->fs, txn_id, parent_noderev, name,
862 NULL, svn_node_unknown, pool);
867 svn_fs_fs__dag_remove_node(svn_fs_t *fs,
868 const svn_fs_id_t *id,
873 /* Fetch the node. */
874 SVN_ERR(svn_fs_fs__dag_get_node(&node, fs, id, pool));
876 /* If immutable, do nothing and return immediately. */
877 if (! svn_fs_fs__dag_check_mutable(node))
878 return svn_error_createf(SVN_ERR_FS_NOT_MUTABLE, NULL,
879 "Attempted removal of immutable node");
881 /* Delete the node revision. */
882 return svn_fs_fs__delete_node_revision(fs, id, pool);
887 svn_fs_fs__dag_delete_if_mutable(svn_fs_t *fs,
888 const svn_fs_id_t *id,
894 SVN_ERR(svn_fs_fs__dag_get_node(&node, fs, id, pool));
896 /* If immutable, do nothing and return immediately. */
897 if (! svn_fs_fs__dag_check_mutable(node))
900 /* Else it's mutable. Recurse on directories... */
901 if (node->kind == svn_node_dir)
903 apr_array_header_t *entries;
905 apr_pool_t *iterpool = svn_pool_create(pool);
907 /* Loop over directory entries */
908 SVN_ERR(svn_fs_fs__dag_dir_entries(&entries, node, pool));
910 for (i = 0; i < entries->nelts; ++i)
912 svn_pool_clear(iterpool);
913 SVN_ERR(svn_fs_fs__dag_delete_if_mutable(fs,
914 APR_ARRAY_IDX(entries, i, svn_fs_dirent_t *)->id,
918 svn_pool_destroy(iterpool);
921 /* ... then delete the node itself, after deleting any mutable
922 representations and strings it points to. */
923 return svn_fs_fs__dag_remove_node(fs, id, pool);
927 svn_fs_fs__dag_make_file(dag_node_t **child_p,
929 const char *parent_path,
931 const svn_fs_fs__id_part_t *txn_id,
934 /* Call our little helper function */
935 return make_entry(child_p, parent, parent_path, name, FALSE, txn_id, pool);
940 svn_fs_fs__dag_make_dir(dag_node_t **child_p,
942 const char *parent_path,
944 const svn_fs_fs__id_part_t *txn_id,
947 /* Call our little helper function */
948 return make_entry(child_p, parent, parent_path, name, TRUE, txn_id, pool);
953 svn_fs_fs__dag_get_contents(svn_stream_t **contents_p,
957 node_revision_t *noderev;
958 svn_stream_t *contents;
960 /* Make sure our node is a file. */
961 if (file->kind != svn_node_file)
962 return svn_error_createf
963 (SVN_ERR_FS_NOT_FILE, NULL,
964 "Attempted to get textual contents of a *non*-file node");
966 /* Go get a fresh node-revision for FILE. */
967 SVN_ERR(get_node_revision(&noderev, file));
969 /* Get a stream to the contents. */
970 SVN_ERR(svn_fs_fs__get_contents(&contents, file->fs,
971 noderev->data_rep, TRUE, pool));
973 *contents_p = contents;
980 svn_fs_fs__dag_get_file_delta_stream(svn_txdelta_stream_t **stream_p,
985 node_revision_t *src_noderev;
986 node_revision_t *tgt_noderev;
988 /* Make sure our nodes are files. */
989 if ((source && source->kind != svn_node_file)
990 || target->kind != svn_node_file)
991 return svn_error_createf
992 (SVN_ERR_FS_NOT_FILE, NULL,
993 "Attempted to get textual contents of a *non*-file node");
995 /* Go get fresh node-revisions for the nodes. */
997 SVN_ERR(get_node_revision(&src_noderev, source));
1000 SVN_ERR(get_node_revision(&tgt_noderev, target));
1002 /* Get the delta stream. */
1003 return svn_fs_fs__get_file_delta_stream(stream_p, target->fs,
1004 src_noderev, tgt_noderev, pool);
1009 svn_fs_fs__dag_try_process_file_contents(svn_boolean_t *success,
1011 svn_fs_process_contents_func_t processor,
1015 node_revision_t *noderev;
1017 /* Go get fresh node-revisions for the nodes. */
1018 SVN_ERR(get_node_revision(&noderev, node));
1020 return svn_fs_fs__try_process_file_contents(success, node->fs,
1022 processor, baton, pool);
1027 svn_fs_fs__dag_file_length(svn_filesize_t *length,
1031 node_revision_t *noderev;
1033 /* Make sure our node is a file. */
1034 if (file->kind != svn_node_file)
1035 return svn_error_createf
1036 (SVN_ERR_FS_NOT_FILE, NULL,
1037 "Attempted to get length of a *non*-file node");
1039 /* Go get a fresh node-revision for FILE, and . */
1040 SVN_ERR(get_node_revision(&noderev, file));
1042 return svn_fs_fs__file_length(length, noderev, pool);
1047 svn_fs_fs__dag_file_checksum(svn_checksum_t **checksum,
1049 svn_checksum_kind_t kind,
1052 node_revision_t *noderev;
1054 if (file->kind != svn_node_file)
1055 return svn_error_createf
1056 (SVN_ERR_FS_NOT_FILE, NULL,
1057 "Attempted to get checksum of a *non*-file node");
1059 SVN_ERR(get_node_revision(&noderev, file));
1061 return svn_fs_fs__file_checksum(checksum, noderev, kind, pool);
1066 svn_fs_fs__dag_get_edit_stream(svn_stream_t **contents,
1070 node_revision_t *noderev;
1073 /* Make sure our node is a file. */
1074 if (file->kind != svn_node_file)
1075 return svn_error_createf
1076 (SVN_ERR_FS_NOT_FILE, NULL,
1077 "Attempted to set textual contents of a *non*-file node");
1079 /* Make sure our node is mutable. */
1080 if (! svn_fs_fs__dag_check_mutable(file))
1081 return svn_error_createf
1082 (SVN_ERR_FS_NOT_MUTABLE, NULL,
1083 "Attempted to set textual contents of an immutable node");
1085 /* Get the node revision. */
1086 SVN_ERR(get_node_revision(&noderev, file));
1088 SVN_ERR(svn_fs_fs__set_contents(&ws, file->fs, noderev, pool));
1092 return SVN_NO_ERROR;
1098 svn_fs_fs__dag_finalize_edits(dag_node_t *file,
1099 const svn_checksum_t *checksum,
1104 svn_checksum_t *file_checksum;
1106 SVN_ERR(svn_fs_fs__dag_file_checksum(&file_checksum, file,
1107 checksum->kind, pool));
1108 if (!svn_checksum_match(checksum, file_checksum))
1109 return svn_checksum_mismatch_err(checksum, file_checksum, pool,
1110 _("Checksum mismatch for '%s'"),
1111 file->created_path);
1114 return SVN_NO_ERROR;
1119 svn_fs_fs__dag_dup(const dag_node_t *node,
1122 /* Allocate our new node. */
1123 dag_node_t *new_node = apr_pcalloc(pool, sizeof(*new_node));
1125 new_node->fs = node->fs;
1126 new_node->id = svn_fs_fs__id_copy(node->id, pool);
1127 new_node->kind = node->kind;
1128 new_node->created_path = apr_pstrdup(pool, node->created_path);
1130 /* Only copy cached node_revision_t for immutable nodes. */
1131 if (node->node_revision && !svn_fs_fs__dag_check_mutable(node))
1133 new_node->node_revision = copy_node_revision(node->node_revision, pool);
1134 new_node->node_revision->id =
1135 svn_fs_fs__id_copy(node->node_revision->id, pool);
1136 new_node->node_revision->is_fresh_txn_root =
1137 node->node_revision->is_fresh_txn_root;
1139 new_node->node_pool = pool;
1145 svn_fs_fs__dag_serialize(void **data,
1146 apr_size_t *data_len,
1150 dag_node_t *node = in;
1151 svn_stringbuf_t *serialized;
1153 /* create an serialization context and serialize the dag node as root */
1154 svn_temp_serializer__context_t *context =
1155 svn_temp_serializer__init(node,
1157 1024 - SVN_TEMP_SERIALIZER__OVERHEAD,
1160 /* for mutable nodes, we will _never_ cache the noderev */
1161 if (node->node_revision && !svn_fs_fs__dag_check_mutable(node))
1162 svn_fs_fs__noderev_serialize(context, &node->node_revision);
1164 svn_temp_serializer__set_null(context,
1165 (const void * const *)&node->node_revision);
1167 /* The deserializer will use its own pool. */
1168 svn_temp_serializer__set_null(context,
1169 (const void * const *)&node->node_pool);
1171 /* serialize other sub-structures */
1172 svn_fs_fs__id_serialize(context, (const svn_fs_id_t **)&node->id);
1173 svn_fs_fs__id_serialize(context, &node->fresh_root_predecessor_id);
1174 svn_temp_serializer__add_string(context, &node->created_path);
1176 /* return serialized data */
1177 serialized = svn_temp_serializer__get(context);
1178 *data = serialized->data;
1179 *data_len = serialized->len;
1181 return SVN_NO_ERROR;
1185 svn_fs_fs__dag_deserialize(void **out,
1187 apr_size_t data_len,
1190 dag_node_t *node = (dag_node_t *)data;
1192 return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
1193 _("Empty noderev in cache"));
1195 /* Copy the _full_ buffer as it also contains the sub-structures. */
1198 /* fixup all references to sub-structures */
1199 svn_fs_fs__id_deserialize(node, &node->id);
1200 svn_fs_fs__id_deserialize(node,
1201 (svn_fs_id_t **)&node->fresh_root_predecessor_id);
1202 svn_fs_fs__noderev_deserialize(node, &node->node_revision);
1203 node->node_pool = pool;
1205 svn_temp_deserializer__resolve(node, (void**)&node->created_path);
1210 return SVN_NO_ERROR;
1214 svn_fs_fs__dag_open(dag_node_t **child_p,
1217 apr_pool_t *result_pool,
1218 apr_pool_t *scratch_pool)
1220 const svn_fs_id_t *node_id;
1222 /* Ensure that NAME exists in PARENT's entry list. */
1223 SVN_ERR(dir_entry_id_from_node(&node_id, parent, name,
1224 scratch_pool, scratch_pool));
1228 return SVN_NO_ERROR;
1231 /* Make sure that NAME is a single path component. */
1232 if (! svn_path_is_single_path_component(name))
1233 return svn_error_createf
1234 (SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
1235 "Attempted to open node with an illegal name '%s'", name);
1237 /* Now get the node that was requested. */
1238 return svn_fs_fs__dag_get_node(child_p, svn_fs_fs__dag_get_fs(parent),
1239 node_id, result_pool);
1244 svn_fs_fs__dag_copy(dag_node_t *to_node,
1246 dag_node_t *from_node,
1247 svn_boolean_t preserve_history,
1248 svn_revnum_t from_rev,
1249 const char *from_path,
1250 const svn_fs_fs__id_part_t *txn_id,
1253 const svn_fs_id_t *id;
1255 if (preserve_history)
1257 node_revision_t *from_noderev, *to_noderev;
1258 svn_fs_fs__id_part_t copy_id;
1259 const svn_fs_id_t *src_id = svn_fs_fs__dag_get_id(from_node);
1260 svn_fs_t *fs = svn_fs_fs__dag_get_fs(from_node);
1262 /* Make a copy of the original node revision. */
1263 SVN_ERR(get_node_revision(&from_noderev, from_node));
1264 to_noderev = copy_node_revision(from_noderev, pool);
1266 /* Reserve a copy ID for this new copy. */
1267 SVN_ERR(svn_fs_fs__reserve_copy_id(©_id, fs, txn_id, pool));
1269 /* Create a successor with its predecessor pointing at the copy
1271 to_noderev->predecessor_id = svn_fs_fs__id_copy(src_id, pool);
1272 if (to_noderev->predecessor_count != -1)
1273 to_noderev->predecessor_count++;
1274 to_noderev->created_path =
1275 svn_fspath__join(svn_fs_fs__dag_get_created_path(to_node), entry,
1277 to_noderev->copyfrom_path = apr_pstrdup(pool, from_path);
1278 to_noderev->copyfrom_rev = from_rev;
1280 /* Set the copyroot equal to our own id. */
1281 to_noderev->copyroot_path = NULL;
1283 SVN_ERR(svn_fs_fs__create_successor(&id, fs, src_id, to_noderev,
1284 ©_id, txn_id, pool));
1287 else /* don't preserve history */
1289 id = svn_fs_fs__dag_get_id(from_node);
1292 /* Set the entry in to_node to the new id. */
1293 return svn_fs_fs__dag_set_entry(to_node, entry, id, from_node->kind,
1299 /*** Comparison. ***/
1302 svn_fs_fs__dag_things_different(svn_boolean_t *props_changed,
1303 svn_boolean_t *contents_changed,
1306 svn_boolean_t strict,
1309 node_revision_t *noderev1, *noderev2;
1313 /* If we have no place to store our results, don't bother doing
1315 if (! props_changed && ! contents_changed)
1316 return SVN_NO_ERROR;
1318 fs = svn_fs_fs__dag_get_fs(node1);
1320 /* The node revision skels for these two nodes. */
1321 SVN_ERR(get_node_revision(&noderev1, node1));
1322 SVN_ERR(get_node_revision(&noderev2, node2));
1324 /* Compare property keys. */
1325 if (props_changed != NULL)
1327 SVN_ERR(svn_fs_fs__prop_rep_equal(&same, fs, noderev1, noderev2,
1329 *props_changed = !same;
1332 /* Compare contents keys. */
1333 if (contents_changed != NULL)
1335 SVN_ERR(svn_fs_fs__file_text_rep_equal(&same, fs, noderev1, noderev2,
1337 *contents_changed = !same;
1340 return SVN_NO_ERROR;
1344 svn_fs_fs__dag_get_copyroot(svn_revnum_t *rev,
1348 node_revision_t *noderev;
1350 /* Go get a fresh node-revision for NODE. */
1351 SVN_ERR(get_node_revision(&noderev, node));
1353 *rev = noderev->copyroot_rev;
1354 *path = noderev->copyroot_path;
1356 return SVN_NO_ERROR;
1360 svn_fs_fs__dag_get_copyfrom_rev(svn_revnum_t *rev,
1363 node_revision_t *noderev;
1365 /* Go get a fresh node-revision for NODE. */
1366 SVN_ERR(get_node_revision(&noderev, node));
1368 *rev = noderev->copyfrom_rev;
1370 return SVN_NO_ERROR;
1374 svn_fs_fs__dag_get_copyfrom_path(const char **path,
1377 node_revision_t *noderev;
1379 /* Go get a fresh node-revision for NODE. */
1380 SVN_ERR(get_node_revision(&noderev, node));
1382 *path = noderev->copyfrom_path;
1384 return SVN_NO_ERROR;
1388 svn_fs_fs__dag_update_ancestry(dag_node_t *target,
1392 node_revision_t *source_noderev, *target_noderev;
1394 if (! svn_fs_fs__dag_check_mutable(target))
1395 return svn_error_createf
1396 (SVN_ERR_FS_NOT_MUTABLE, NULL,
1397 _("Attempted to update ancestry of non-mutable node"));
1399 SVN_ERR(get_node_revision(&source_noderev, source));
1400 SVN_ERR(get_node_revision(&target_noderev, target));
1402 target_noderev->predecessor_id = source->id;
1403 target_noderev->predecessor_count = source_noderev->predecessor_count;
1404 if (target_noderev->predecessor_count != -1)
1405 target_noderev->predecessor_count++;
1407 return svn_fs_fs__put_node_revision(target->fs, target->id, target_noderev,