In the command-line client, forbid
[svn.git] / subversion / libsvn_fs_fs / dag.c
blobecc47c183fad1799fdb764942169381b5c6f5ca2
1 /* dag.c : DAG-like interface filesystem, private to libsvn_fs
3 * ====================================================================
4 * Copyright (c) 2000-2006 CollabNet. All rights reserved.
6 * This software is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at http://subversion.tigris.org/license-1.html.
9 * If newer versions of this license are posted there, you may use a
10 * newer version instead, at your option.
12 * This software consists of voluntary contributions made by many
13 * individuals. For exact contribution history, see the revision
14 * history and logs, available at http://subversion.tigris.org/.
15 * ====================================================================
18 #include <string.h>
19 #include <assert.h>
21 #include "svn_path.h"
22 #include "svn_error.h"
23 #include "svn_fs.h"
24 #include "svn_props.h"
25 #include "svn_pools.h"
26 #include "svn_md5.h"
28 #include "dag.h"
29 #include "err.h"
30 #include "fs.h"
31 #include "key-gen.h"
32 #include "fs_fs.h"
33 #include "id.h"
35 #include "../libsvn_fs/fs-loader.h"
37 #include "svn_private_config.h"
40 /* Initializing a filesystem. */
42 struct dag_node_t
44 /* The filesystem this dag node came from. */
45 svn_fs_t *fs;
47 /* The node revision ID for this dag node, allocated in POOL. */
48 svn_fs_id_t *id;
50 /* In the special case that this node is the root of a transaction
51 that has not yet been modified, the node revision ID for this dag
52 node's predecessor; otherwise NULL. (Used in
53 svn_fs_node_created_rev.) */
54 const svn_fs_id_t *fresh_root_predecessor_id;
56 /* The node's type (file, dir, etc.) */
57 svn_node_kind_t kind;
59 /* The node's NODE-REVISION, or NULL if we haven't read it in yet.
60 This is allocated in this node's POOL.
62 If you're willing to respect all the rules above, you can munge
63 this yourself, but you're probably better off just calling
64 `get_node_revision' and `set_node_revision', which take care of
65 things for you. */
66 node_revision_t *node_revision;
68 /* the path at which this node was created. */
69 const char *created_path;
74 /* Trivial helper/accessor functions. */
75 svn_node_kind_t svn_fs_fs__dag_node_kind(dag_node_t *node)
77 return node->kind;
81 const svn_fs_id_t *
82 svn_fs_fs__dag_get_id(dag_node_t *node)
84 return node->id;
88 const char *
89 svn_fs_fs__dag_get_created_path(dag_node_t *node)
91 return node->created_path;
95 svn_fs_t *
96 svn_fs_fs__dag_get_fs(dag_node_t *node)
98 return node->fs;
102 /* Dup NODEREV and all associated data into POOL.
103 Leaves the id and is_fresh_txn_root fields as zero bytes. */
104 static node_revision_t *
105 copy_node_revision(node_revision_t *noderev,
106 apr_pool_t *pool)
108 node_revision_t *nr = apr_pcalloc(pool, sizeof(*nr));
109 nr->kind = noderev->kind;
110 if (noderev->predecessor_id)
111 nr->predecessor_id = svn_fs_fs__id_copy(noderev->predecessor_id, pool);
112 nr->predecessor_count = noderev->predecessor_count;
113 if (noderev->copyfrom_path)
114 nr->copyfrom_path = apr_pstrdup(pool, noderev->copyfrom_path);
115 nr->copyfrom_rev = noderev->copyfrom_rev;
116 nr->copyroot_path = apr_pstrdup(pool, noderev->copyroot_path);
117 nr->copyroot_rev = noderev->copyroot_rev;
118 nr->predecessor_count = noderev->predecessor_count;
119 nr->data_rep = svn_fs_fs__rep_copy(noderev->data_rep, pool);
120 nr->prop_rep = svn_fs_fs__rep_copy(noderev->prop_rep, pool);
122 if (noderev->created_path)
123 nr->created_path = apr_pstrdup(pool, noderev->created_path);
124 return nr;
128 /* Set *NODEREV_P to the cached node-revision for NODE in POOL.
130 If you plan to change the contents of NODE, be careful! We're
131 handing you a pointer directly to our cached node-revision, not
132 your own copy. If you change it as part of some operation, but
133 then some Berkeley DB function deadlocks or gets an error, you'll
134 need to back out your changes, or else the cache will reflect
135 changes that never got committed. It's probably best not to change
136 the structure at all. */
137 static svn_error_t *
138 get_node_revision(node_revision_t **noderev_p,
139 dag_node_t *node,
140 apr_pool_t *pool)
142 node_revision_t *noderev;
144 /* If we've already got a copy, there's no need to read it in. */
145 if (! node->node_revision)
147 SVN_ERR(svn_fs_fs__get_node_revision(&noderev, node->fs,
148 node->id, pool));
149 node->node_revision = noderev;
152 /* Now NODE->node_revision is set. */
153 *noderev_p = node->node_revision;
154 return SVN_NO_ERROR;
158 svn_boolean_t svn_fs_fs__dag_check_mutable(dag_node_t *node)
160 return (svn_fs_fs__id_txn_id(svn_fs_fs__dag_get_id(node)) != NULL);
164 svn_error_t *
165 svn_fs_fs__dag_get_node(dag_node_t **node,
166 svn_fs_t *fs,
167 const svn_fs_id_t *id,
168 apr_pool_t *pool)
170 dag_node_t *new_node;
171 node_revision_t *noderev;
173 /* Construct the node. */
174 new_node = apr_pcalloc(pool, sizeof(*new_node));
175 new_node->fs = fs;
176 new_node->id = svn_fs_fs__id_copy(id, pool);
178 /* Grab the contents so we can inspect the node's kind and created path. */
179 SVN_ERR(get_node_revision(&noderev, new_node, pool));
181 /* Initialize the KIND and CREATED_PATH attributes */
182 new_node->kind = noderev->kind;
183 new_node->created_path = apr_pstrdup(pool, noderev->created_path);
185 if (noderev->is_fresh_txn_root)
186 new_node->fresh_root_predecessor_id = noderev->predecessor_id;
187 else
188 new_node->fresh_root_predecessor_id = NULL;
190 /* Return a fresh new node */
191 *node = new_node;
192 return SVN_NO_ERROR;
196 svn_error_t *
197 svn_fs_fs__dag_get_revision(svn_revnum_t *rev,
198 dag_node_t *node,
199 apr_pool_t *pool)
201 /* In the special case that this is an unmodified transaction root,
202 we need to actually get the revision of the noderev's predecessor
203 (the revision root); see Issue #2608. */
204 const svn_fs_id_t *correct_id = node->fresh_root_predecessor_id
205 ? node->fresh_root_predecessor_id : node->id;
207 /* Look up the committed revision from the Node-ID. */
208 *rev = svn_fs_fs__id_rev(correct_id);
210 return SVN_NO_ERROR;
214 svn_error_t *
215 svn_fs_fs__dag_get_predecessor_id(const svn_fs_id_t **id_p,
216 dag_node_t *node,
217 apr_pool_t *pool)
219 node_revision_t *noderev;
221 SVN_ERR(get_node_revision(&noderev, node, pool));
222 *id_p = noderev->predecessor_id;
223 return SVN_NO_ERROR;
227 svn_error_t *
228 svn_fs_fs__dag_get_predecessor_count(int *count,
229 dag_node_t *node,
230 apr_pool_t *pool)
232 node_revision_t *noderev;
234 SVN_ERR(get_node_revision(&noderev, node, pool));
235 *count = noderev->predecessor_count;
236 return SVN_NO_ERROR;
241 /*** Directory node functions ***/
243 /* Some of these are helpers for functions outside this section. */
245 /* Set *ID_P to the node-id for entry NAME in PARENT. If no such
246 entry, set *ID_P to NULL but do not error. The node-id is not
247 necessarily allocated in POOL; the caller should copy if it
248 cares. */
249 static svn_error_t *
250 dir_entry_id_from_node(const svn_fs_id_t **id_p,
251 dag_node_t *parent,
252 const char *name,
253 apr_pool_t *pool)
255 apr_hash_t *entries;
256 svn_fs_dirent_t *dirent;
258 SVN_ERR(svn_fs_fs__dag_dir_entries(&entries, parent, pool));
259 if (entries)
260 dirent = apr_hash_get(entries, name, APR_HASH_KEY_STRING);
261 else
262 dirent = NULL;
264 *id_p = dirent ? dirent->id : NULL;
265 return SVN_NO_ERROR;
269 /* Add or set in PARENT a directory entry NAME pointing to ID.
270 Allocations are done in POOL.
272 Assumptions:
273 - PARENT is a mutable directory.
274 - ID does not refer to an ancestor of parent
275 - NAME is a single path component
277 static svn_error_t *
278 set_entry(dag_node_t *parent,
279 const char *name,
280 const svn_fs_id_t *id,
281 svn_node_kind_t kind,
282 const char *txn_id,
283 apr_pool_t *pool)
285 node_revision_t *parent_noderev;
287 /* Get the parent's node-revision. */
288 SVN_ERR(get_node_revision(&parent_noderev, parent, pool));
290 /* Set the new entry. */
291 SVN_ERR(svn_fs_fs__set_entry(parent->fs, txn_id, parent_noderev, name, id,
292 kind, pool));
294 return SVN_NO_ERROR;
298 /* Make a new entry named NAME in PARENT. If IS_DIR is true, then the
299 node revision the new entry points to will be a directory, else it
300 will be a file. The new node will be allocated in POOL. PARENT
301 must be mutable, and must not have an entry named NAME. */
302 static svn_error_t *
303 make_entry(dag_node_t **child_p,
304 dag_node_t *parent,
305 const char *parent_path,
306 const char *name,
307 svn_boolean_t is_dir,
308 const char *txn_id,
309 apr_pool_t *pool)
311 const svn_fs_id_t *new_node_id;
312 node_revision_t new_noderev, *parent_noderev;
314 /* Make sure that NAME is a single path component. */
315 if (! svn_path_is_single_path_component(name))
316 return svn_error_createf
317 (SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
318 _("Attempted to create a node with an illegal name '%s'"), name);
320 /* Make sure that parent is a directory */
321 if (parent->kind != svn_node_dir)
322 return svn_error_create
323 (SVN_ERR_FS_NOT_DIRECTORY, NULL,
324 _("Attempted to create entry in non-directory parent"));
326 /* Check that the parent is mutable. */
327 if (! svn_fs_fs__dag_check_mutable(parent))
328 return svn_error_createf
329 (SVN_ERR_FS_NOT_MUTABLE, NULL,
330 _("Attempted to clone child of non-mutable node"));
332 /* Create the new node's NODE-REVISION */
333 memset(&new_noderev, 0, sizeof(new_noderev));
334 new_noderev.kind = is_dir ? svn_node_dir : svn_node_file;
335 new_noderev.created_path = svn_path_join(parent_path, name, pool);
337 SVN_ERR(get_node_revision(&parent_noderev, parent, pool));
338 new_noderev.copyroot_path = apr_pstrdup(pool,
339 parent_noderev->copyroot_path);
340 new_noderev.copyroot_rev = parent_noderev->copyroot_rev;
341 new_noderev.copyfrom_rev = SVN_INVALID_REVNUM;
342 new_noderev.copyfrom_path = NULL;
344 SVN_ERR(svn_fs_fs__create_node
345 (&new_node_id, svn_fs_fs__dag_get_fs(parent), &new_noderev,
346 svn_fs_fs__id_copy_id(svn_fs_fs__dag_get_id(parent)),
347 txn_id, pool));
349 /* Create a new dag_node_t for our new node */
350 SVN_ERR(svn_fs_fs__dag_get_node(child_p, svn_fs_fs__dag_get_fs(parent),
351 new_node_id, pool));
353 /* We can safely call set_entry because we already know that
354 PARENT is mutable, and we just created CHILD, so we know it has
355 no ancestors (therefore, PARENT cannot be an ancestor of CHILD) */
356 SVN_ERR(set_entry(parent, name, svn_fs_fs__dag_get_id(*child_p),
357 new_noderev.kind, txn_id, pool));
359 return SVN_NO_ERROR;
363 svn_error_t *
364 svn_fs_fs__dag_dir_entries(apr_hash_t **entries,
365 dag_node_t *node,
366 apr_pool_t *pool)
368 node_revision_t *noderev;
370 SVN_ERR(get_node_revision(&noderev, node, pool));
372 if (noderev->kind != svn_node_dir)
373 return svn_error_create(SVN_ERR_FS_NOT_DIRECTORY, NULL,
374 _("Can't get entries of non-directory"));
376 return svn_fs_fs__rep_contents_dir(entries, node->fs, noderev, pool);
380 svn_error_t *
381 svn_fs_fs__dag_set_entry(dag_node_t *node,
382 const char *entry_name,
383 const svn_fs_id_t *id,
384 svn_node_kind_t kind,
385 const char *txn_id,
386 apr_pool_t *pool)
388 /* Check it's a directory. */
389 if (node->kind != svn_node_dir)
390 return svn_error_create
391 (SVN_ERR_FS_NOT_DIRECTORY, NULL,
392 _("Attempted to set entry in non-directory node"));
394 /* Check it's mutable. */
395 if (! svn_fs_fs__dag_check_mutable(node))
396 return svn_error_create
397 (SVN_ERR_FS_NOT_MUTABLE, NULL,
398 _("Attempted to set entry in immutable node"));
400 return set_entry(node, entry_name, id, kind, txn_id, pool);
405 /*** Proplists. ***/
407 svn_error_t *
408 svn_fs_fs__dag_get_proplist(apr_hash_t **proplist_p,
409 dag_node_t *node,
410 apr_pool_t *pool)
412 node_revision_t *noderev;
413 apr_hash_t *proplist = NULL;
415 SVN_ERR(get_node_revision(&noderev, node, pool));
417 SVN_ERR(svn_fs_fs__get_proplist(&proplist, node->fs,
418 noderev, pool));
420 *proplist_p = proplist;
422 return SVN_NO_ERROR;
426 svn_error_t *
427 svn_fs_fs__dag_set_proplist(dag_node_t *node,
428 apr_hash_t *proplist,
429 apr_pool_t *pool)
431 node_revision_t *noderev;
433 /* Sanity check: this node better be mutable! */
434 if (! svn_fs_fs__dag_check_mutable(node))
436 svn_string_t *idstr = svn_fs_fs__id_unparse(node->id, pool);
437 return svn_error_createf
438 (SVN_ERR_FS_NOT_MUTABLE, NULL,
439 "Can't set proplist on *immutable* node-revision %s",
440 idstr->data);
443 /* Go get a fresh NODE-REVISION for this node. */
444 SVN_ERR(get_node_revision(&noderev, node, pool));
446 /* Set the new proplist. */
447 SVN_ERR(svn_fs_fs__set_proplist(node->fs, noderev, proplist, pool));
449 return SVN_NO_ERROR;
454 /*** Roots. ***/
456 svn_error_t *
457 svn_fs_fs__dag_revision_root(dag_node_t **node_p,
458 svn_fs_t *fs,
459 svn_revnum_t rev,
460 apr_pool_t *pool)
462 svn_fs_id_t *root_id;
464 SVN_ERR(svn_fs_fs__rev_get_root(&root_id, fs, rev, pool));
465 return svn_fs_fs__dag_get_node(node_p, fs, root_id, pool);
469 svn_error_t *
470 svn_fs_fs__dag_txn_root(dag_node_t **node_p,
471 svn_fs_t *fs,
472 const char *txn_id,
473 apr_pool_t *pool)
475 const svn_fs_id_t *root_id, *ignored;
477 SVN_ERR(svn_fs_fs__get_txn_ids(&root_id, &ignored, fs, txn_id, pool));
478 return svn_fs_fs__dag_get_node(node_p, fs, root_id, pool);
482 svn_error_t *
483 svn_fs_fs__dag_txn_base_root(dag_node_t **node_p,
484 svn_fs_t *fs,
485 const char *txn_id,
486 apr_pool_t *pool)
488 const svn_fs_id_t *base_root_id, *ignored;
490 SVN_ERR(svn_fs_fs__get_txn_ids(&ignored, &base_root_id, fs, txn_id, pool));
491 return svn_fs_fs__dag_get_node(node_p, fs, base_root_id, pool);
495 svn_error_t *
496 svn_fs_fs__dag_clone_child(dag_node_t **child_p,
497 dag_node_t *parent,
498 const char *parent_path,
499 const char *name,
500 const char *copy_id,
501 const char *txn_id,
502 svn_boolean_t is_parent_copyroot,
503 apr_pool_t *pool)
505 dag_node_t *cur_entry; /* parent's current entry named NAME */
506 const svn_fs_id_t *new_node_id; /* node id we'll put into NEW_NODE */
507 svn_fs_t *fs = svn_fs_fs__dag_get_fs(parent);
509 /* First check that the parent is mutable. */
510 if (! svn_fs_fs__dag_check_mutable(parent))
511 return svn_error_createf
512 (SVN_ERR_FS_NOT_MUTABLE, NULL,
513 "Attempted to clone child of non-mutable node");
515 /* Make sure that NAME is a single path component. */
516 if (! svn_path_is_single_path_component(name))
517 return svn_error_createf
518 (SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
519 "Attempted to make a child clone with an illegal name '%s'", name);
521 /* Find the node named NAME in PARENT's entries list if it exists. */
522 SVN_ERR(svn_fs_fs__dag_open(&cur_entry, parent, name, pool));
524 /* Check for mutability in the node we found. If it's mutable, we
525 don't need to clone it. */
526 if (svn_fs_fs__dag_check_mutable(cur_entry))
528 /* This has already been cloned */
529 new_node_id = cur_entry->id;
531 else
533 node_revision_t *noderev, *parent_noderev;
535 /* Go get a fresh NODE-REVISION for current child node. */
536 SVN_ERR(get_node_revision(&noderev, cur_entry, pool));
538 if (is_parent_copyroot)
540 SVN_ERR(get_node_revision(&parent_noderev, parent, pool));
541 noderev->copyroot_rev = parent_noderev->copyroot_rev;
542 noderev->copyroot_path = apr_pstrdup(pool,
543 parent_noderev->copyroot_path);
546 noderev->copyfrom_path = NULL;
547 noderev->copyfrom_rev = SVN_INVALID_REVNUM;
549 noderev->predecessor_id = svn_fs_fs__id_copy(cur_entry->id, pool);
550 if (noderev->predecessor_count != -1)
551 noderev->predecessor_count++;
552 noderev->created_path = svn_path_join(parent_path, name, pool);
554 SVN_ERR(svn_fs_fs__create_successor(&new_node_id, fs, cur_entry->id,
555 noderev, copy_id, txn_id, pool));
557 /* Replace the ID in the parent's ENTRY list with the ID which
558 refers to the mutable clone of this child. */
559 SVN_ERR(set_entry(parent, name, new_node_id, noderev->kind, txn_id,
560 pool));
563 /* Initialize the youngster. */
564 return svn_fs_fs__dag_get_node(child_p, fs, new_node_id, pool);
569 svn_error_t *
570 svn_fs_fs__dag_clone_root(dag_node_t **root_p,
571 svn_fs_t *fs,
572 const char *txn_id,
573 apr_pool_t *pool)
575 const svn_fs_id_t *base_root_id, *root_id;
577 /* Get the node ID's of the root directories of the transaction and
578 its base revision. */
579 SVN_ERR(svn_fs_fs__get_txn_ids(&root_id, &base_root_id, fs, txn_id, pool));
581 /* Oh, give me a clone...
582 (If they're the same, we haven't cloned the transaction's root
583 directory yet.) */
584 if (svn_fs_fs__id_eq(root_id, base_root_id))
586 abort();
589 /* One way or another, root_id now identifies a cloned root node. */
590 SVN_ERR(svn_fs_fs__dag_get_node(root_p, fs, root_id, pool));
593 * (Sung to the tune of "Home, Home on the Range", with thanks to
594 * Randall Garrett and Isaac Asimov.)
597 return SVN_NO_ERROR;
601 svn_error_t *
602 svn_fs_fs__dag_delete(dag_node_t *parent,
603 const char *name,
604 const char *txn_id,
605 apr_pool_t *pool)
607 node_revision_t *parent_noderev;
608 apr_hash_t *entries;
609 svn_fs_t *fs = parent->fs;
610 svn_fs_dirent_t *dirent;
611 svn_fs_id_t *id;
613 /* Make sure parent is a directory. */
614 if (parent->kind != svn_node_dir)
615 return svn_error_createf
616 (SVN_ERR_FS_NOT_DIRECTORY, NULL,
617 "Attempted to delete entry '%s' from *non*-directory node", name);
619 /* Make sure parent is mutable. */
620 if (! svn_fs_fs__dag_check_mutable(parent))
621 return svn_error_createf
622 (SVN_ERR_FS_NOT_MUTABLE, NULL,
623 "Attempted to delete entry '%s' from immutable directory node", name);
625 /* Make sure that NAME is a single path component. */
626 if (! svn_path_is_single_path_component(name))
627 return svn_error_createf
628 (SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
629 "Attempted to delete a node with an illegal name '%s'", name);
631 /* Get a fresh NODE-REVISION for the parent node. */
632 SVN_ERR(get_node_revision(&parent_noderev, parent, pool));
634 /* Get a dirent hash for this directory. */
635 SVN_ERR(svn_fs_fs__rep_contents_dir(&entries, fs, parent_noderev, pool));
637 /* Find name in the ENTRIES hash. */
638 dirent = apr_hash_get(entries, name, APR_HASH_KEY_STRING);
640 /* If we never found ID in ENTRIES (perhaps because there are no
641 ENTRIES, perhaps because ID just isn't in the existing ENTRIES
642 ... it doesn't matter), return an error. */
643 if (! dirent)
644 return svn_error_createf
645 (SVN_ERR_FS_NO_SUCH_ENTRY, NULL,
646 "Delete failed--directory has no entry '%s'", name);
648 /* Stash a copy of the ID, since dirent will become invalid during
649 svn_fs_fs__dag_delete_if_mutable. */
650 id = svn_fs_fs__id_copy(dirent->id, pool);
652 /* If mutable, remove it and any mutable children from db. */
653 SVN_ERR(svn_fs_fs__dag_delete_if_mutable(parent->fs, id, pool));
655 /* Remove this entry from its parent's entries list. */
656 SVN_ERR(svn_fs_fs__set_entry(parent->fs, txn_id, parent_noderev, name,
657 NULL, svn_node_unknown, pool));
659 return SVN_NO_ERROR;
663 svn_error_t *
664 svn_fs_fs__dag_remove_node(svn_fs_t *fs,
665 const svn_fs_id_t *id,
666 apr_pool_t *pool)
668 dag_node_t *node;
670 /* Fetch the node. */
671 SVN_ERR(svn_fs_fs__dag_get_node(&node, fs, id, pool));
673 /* If immutable, do nothing and return immediately. */
674 if (! svn_fs_fs__dag_check_mutable(node))
675 return svn_error_createf(SVN_ERR_FS_NOT_MUTABLE, NULL,
676 "Attempted removal of immutable node");
678 /* Delete the node revision. */
679 SVN_ERR(svn_fs_fs__delete_node_revision(fs, id, pool));
681 return SVN_NO_ERROR;
685 svn_error_t *
686 svn_fs_fs__dag_delete_if_mutable(svn_fs_t *fs,
687 const svn_fs_id_t *id,
688 apr_pool_t *pool)
690 dag_node_t *node;
692 /* Get the node. */
693 SVN_ERR(svn_fs_fs__dag_get_node(&node, fs, id, pool));
695 /* If immutable, do nothing and return immediately. */
696 if (! svn_fs_fs__dag_check_mutable(node))
697 return SVN_NO_ERROR;
699 /* Else it's mutable. Recurse on directories... */
700 if (node->kind == svn_node_dir)
702 apr_hash_t *entries;
703 apr_hash_index_t *hi;
705 /* Loop over hash entries */
706 SVN_ERR(svn_fs_fs__dag_dir_entries(&entries, node, pool));
707 entries = svn_fs_fs__copy_dir_entries(entries, pool);
708 if (entries)
710 for (hi = apr_hash_first(pool, entries);
712 hi = apr_hash_next(hi))
714 void *val;
715 svn_fs_dirent_t *dirent;
717 apr_hash_this(hi, NULL, NULL, &val);
718 dirent = val;
719 SVN_ERR(svn_fs_fs__dag_delete_if_mutable(fs, dirent->id,
720 pool));
725 /* ... then delete the node itself, after deleting any mutable
726 representations and strings it points to. */
727 SVN_ERR(svn_fs_fs__dag_remove_node(fs, id, pool));
729 return SVN_NO_ERROR;
732 svn_error_t *
733 svn_fs_fs__dag_make_file(dag_node_t **child_p,
734 dag_node_t *parent,
735 const char *parent_path,
736 const char *name,
737 const char *txn_id,
738 apr_pool_t *pool)
740 /* Call our little helper function */
741 return make_entry(child_p, parent, parent_path, name, FALSE, txn_id, pool);
745 svn_error_t *
746 svn_fs_fs__dag_make_dir(dag_node_t **child_p,
747 dag_node_t *parent,
748 const char *parent_path,
749 const char *name,
750 const char *txn_id,
751 apr_pool_t *pool)
753 /* Call our little helper function */
754 return make_entry(child_p, parent, parent_path, name, TRUE, txn_id, pool);
758 svn_error_t *
759 svn_fs_fs__dag_get_contents(svn_stream_t **contents_p,
760 dag_node_t *file,
761 apr_pool_t *pool)
763 node_revision_t *noderev;
764 svn_stream_t *contents;
766 /* Make sure our node is a file. */
767 if (file->kind != svn_node_file)
768 return svn_error_createf
769 (SVN_ERR_FS_NOT_FILE, NULL,
770 "Attempted to get textual contents of a *non*-file node");
772 /* Go get a fresh node-revision for FILE. */
773 SVN_ERR(get_node_revision(&noderev, file, pool));
775 /* Get a stream to the contents. */
776 SVN_ERR(svn_fs_fs__get_contents(&contents, file->fs,
777 noderev, pool));
779 *contents_p = contents;
781 return SVN_NO_ERROR;
785 svn_error_t *
786 svn_fs_fs__dag_get_file_delta_stream(svn_txdelta_stream_t **stream_p,
787 dag_node_t *source,
788 dag_node_t *target,
789 apr_pool_t *pool)
791 node_revision_t *src_noderev;
792 node_revision_t *tgt_noderev;
794 /* Make sure our nodes are files. */
795 if ((source && source->kind != svn_node_file)
796 || target->kind != svn_node_file)
797 return svn_error_createf
798 (SVN_ERR_FS_NOT_FILE, NULL,
799 "Attempted to get textual contents of a *non*-file node");
801 /* Go get fresh node-revisions for the nodes. */
802 if (source)
803 SVN_ERR(get_node_revision(&src_noderev, source, pool));
804 else
805 src_noderev = NULL;
806 SVN_ERR(get_node_revision(&tgt_noderev, target, pool));
808 /* Get the delta stream. */
809 SVN_ERR(svn_fs_fs__get_file_delta_stream(stream_p, target->fs,
810 src_noderev, tgt_noderev, pool));
812 return SVN_NO_ERROR;
816 svn_error_t *
817 svn_fs_fs__dag_file_length(svn_filesize_t *length,
818 dag_node_t *file,
819 apr_pool_t *pool)
821 node_revision_t *noderev;
823 /* Make sure our node is a file. */
824 if (file->kind != svn_node_file)
825 return svn_error_createf
826 (SVN_ERR_FS_NOT_FILE, NULL,
827 "Attempted to get length of a *non*-file node");
829 /* Go get a fresh node-revision for FILE, and . */
830 SVN_ERR(get_node_revision(&noderev, file, pool));
832 SVN_ERR(svn_fs_fs__file_length(length, noderev, pool));
834 return SVN_NO_ERROR;
838 svn_error_t *
839 svn_fs_fs__dag_file_checksum(unsigned char digest[],
840 dag_node_t *file,
841 apr_pool_t *pool)
843 node_revision_t *noderev;
845 if (file->kind != svn_node_file)
846 return svn_error_createf
847 (SVN_ERR_FS_NOT_FILE, NULL,
848 "Attempted to get checksum of a *non*-file node");
850 SVN_ERR(get_node_revision(&noderev, file, pool));
852 SVN_ERR(svn_fs_fs__file_checksum(digest, noderev, pool));
854 return SVN_NO_ERROR;
858 svn_error_t *
859 svn_fs_fs__dag_get_edit_stream(svn_stream_t **contents,
860 dag_node_t *file,
861 apr_pool_t *pool)
863 node_revision_t *noderev;
864 svn_stream_t *ws;
866 /* Make sure our node is a file. */
867 if (file->kind != svn_node_file)
868 return svn_error_createf
869 (SVN_ERR_FS_NOT_FILE, NULL,
870 "Attempted to set textual contents of a *non*-file node");
872 /* Make sure our node is mutable. */
873 if (! svn_fs_fs__dag_check_mutable(file))
874 return svn_error_createf
875 (SVN_ERR_FS_NOT_MUTABLE, NULL,
876 "Attempted to set textual contents of an immutable node");
878 /* Get the node revision. */
879 SVN_ERR(get_node_revision(&noderev, file, pool));
881 SVN_ERR(svn_fs_fs__set_contents(&ws, file->fs, noderev, pool));
883 *contents = ws;
885 return SVN_NO_ERROR;
890 svn_error_t *
891 svn_fs_fs__dag_finalize_edits(dag_node_t *file,
892 const char *checksum,
893 apr_pool_t *pool)
895 unsigned char digest[APR_MD5_DIGESTSIZE];
896 const char *hex;
898 if (checksum)
900 SVN_ERR(svn_fs_fs__dag_file_checksum(digest, file, pool));
901 hex = svn_md5_digest_to_cstring(digest, pool);
902 if (hex && strcmp(checksum, hex) != 0)
903 return svn_error_createf(SVN_ERR_CHECKSUM_MISMATCH, NULL,
904 _("Checksum mismatch, file '%s':\n"
905 " expected: %s\n"
906 " actual: %s\n"),
907 file->created_path, checksum, hex);
910 return SVN_NO_ERROR;
914 dag_node_t *
915 svn_fs_fs__dag_dup(dag_node_t *node,
916 apr_pool_t *pool)
918 /* Allocate our new node. */
919 dag_node_t *new_node = apr_pcalloc(pool, sizeof(*new_node));
921 new_node->fs = node->fs;
922 new_node->id = svn_fs_fs__id_copy(node->id, pool);
923 new_node->kind = node->kind;
924 new_node->created_path = apr_pstrdup(pool, node->created_path);
926 /* Only copy cached node_revision_t for immutable nodes. */
927 if (node->node_revision && !svn_fs_fs__dag_check_mutable(node))
929 new_node->node_revision = copy_node_revision(node->node_revision, pool);
930 new_node->node_revision->id =
931 svn_fs_fs__id_copy(node->node_revision->id, pool);
932 new_node->node_revision->is_fresh_txn_root =
933 node->node_revision->is_fresh_txn_root;
935 return new_node;
939 svn_error_t *
940 svn_fs_fs__dag_open(dag_node_t **child_p,
941 dag_node_t *parent,
942 const char *name,
943 apr_pool_t *pool)
945 const svn_fs_id_t *node_id;
947 /* Ensure that NAME exists in PARENT's entry list. */
948 SVN_ERR(dir_entry_id_from_node(&node_id, parent, name, pool));
949 if (! node_id)
950 return svn_error_createf
951 (SVN_ERR_FS_NOT_FOUND, NULL,
952 "Attempted to open non-existent child node '%s'", name);
954 /* Make sure that NAME is a single path component. */
955 if (! svn_path_is_single_path_component(name))
956 return svn_error_createf
957 (SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
958 "Attempted to open node with an illegal name '%s'", name);
960 /* Now get the node that was requested. */
961 return svn_fs_fs__dag_get_node(child_p, svn_fs_fs__dag_get_fs(parent),
962 node_id, pool);
966 svn_error_t *
967 svn_fs_fs__dag_copy(dag_node_t *to_node,
968 const char *entry,
969 dag_node_t *from_node,
970 svn_boolean_t preserve_history,
971 svn_revnum_t from_rev,
972 const char *from_path,
973 const char *txn_id,
974 apr_pool_t *pool)
976 const svn_fs_id_t *id;
978 if (preserve_history)
980 node_revision_t *from_noderev, *to_noderev;
981 const char *copy_id;
982 const svn_fs_id_t *src_id = svn_fs_fs__dag_get_id(from_node);
983 svn_fs_t *fs = svn_fs_fs__dag_get_fs(from_node);
985 /* Make a copy of the original node revision. */
986 SVN_ERR(get_node_revision(&from_noderev, from_node, pool));
987 to_noderev = copy_node_revision(from_noderev, pool);
989 /* Reserve a copy ID for this new copy. */
990 SVN_ERR(svn_fs_fs__reserve_copy_id(&copy_id, fs, txn_id, pool));
992 /* Create a successor with its predecessor pointing at the copy
993 source. */
994 to_noderev->predecessor_id = svn_fs_fs__id_copy(src_id, pool);
995 if (to_noderev->predecessor_count != -1)
996 to_noderev->predecessor_count++;
997 to_noderev->created_path =
998 svn_path_join(svn_fs_fs__dag_get_created_path(to_node), entry,
999 pool);
1000 to_noderev->copyfrom_path = apr_pstrdup(pool, from_path);
1001 to_noderev->copyfrom_rev = from_rev;
1003 /* Set the copyroot equal to our own id. */
1004 to_noderev->copyroot_path = NULL;
1006 SVN_ERR(svn_fs_fs__create_successor(&id, fs, src_id, to_noderev,
1007 copy_id, txn_id, pool));
1010 else /* don't preserve history */
1012 id = svn_fs_fs__dag_get_id(from_node);
1015 /* Set the entry in to_node to the new id. */
1016 SVN_ERR(svn_fs_fs__dag_set_entry(to_node, entry, id, from_node->kind,
1017 txn_id, pool));
1019 return SVN_NO_ERROR;
1024 /*** Comparison. ***/
1026 svn_error_t *
1027 svn_fs_fs__dag_things_different(svn_boolean_t *props_changed,
1028 svn_boolean_t *contents_changed,
1029 dag_node_t *node1,
1030 dag_node_t *node2,
1031 apr_pool_t *pool)
1033 node_revision_t *noderev1, *noderev2;
1035 /* If we have no place to store our results, don't bother doing
1036 anything. */
1037 if (! props_changed && ! contents_changed)
1038 return SVN_NO_ERROR;
1040 /* The node revision skels for these two nodes. */
1041 SVN_ERR(get_node_revision(&noderev1, node1, pool));
1042 SVN_ERR(get_node_revision(&noderev2, node2, pool));
1044 /* Compare property keys. */
1045 if (props_changed != NULL)
1046 *props_changed = (! svn_fs_fs__noderev_same_rep_key(noderev1->prop_rep,
1047 noderev2->prop_rep));
1049 /* Compare contents keys. */
1050 if (contents_changed != NULL)
1051 *contents_changed =
1052 (! svn_fs_fs__noderev_same_rep_key(noderev1->data_rep,
1053 noderev2->data_rep));
1055 return SVN_NO_ERROR;
1058 svn_error_t *
1059 svn_fs_fs__dag_get_copyroot(svn_revnum_t *rev,
1060 const char **path,
1061 dag_node_t *node,
1062 apr_pool_t *pool)
1064 node_revision_t *noderev;
1066 /* Go get a fresh node-revision for FILE. */
1067 SVN_ERR(get_node_revision(&noderev, node, pool));
1069 *rev = noderev->copyroot_rev;
1070 *path = noderev->copyroot_path;
1072 return SVN_NO_ERROR;
1075 svn_error_t *
1076 svn_fs_fs__dag_get_copyfrom_rev(svn_revnum_t *rev,
1077 dag_node_t *node,
1078 apr_pool_t *pool)
1080 node_revision_t *noderev;
1082 /* Go get a fresh node-revision for FILE. */
1083 SVN_ERR(get_node_revision(&noderev, node, pool));
1085 *rev = noderev->copyfrom_rev;
1087 return SVN_NO_ERROR;
1090 svn_error_t *
1091 svn_fs_fs__dag_get_copyfrom_path(const char **path,
1092 dag_node_t *node,
1093 apr_pool_t *pool)
1095 node_revision_t *noderev;
1097 /* Go get a fresh node-revision for FILE. */
1098 SVN_ERR(get_node_revision(&noderev, node, pool));
1100 *path = noderev->copyfrom_path;
1102 return SVN_NO_ERROR;