📄 dag.c
字号:
/* dag.c : DAG-like interface filesystem, private to libsvn_fs
*
* ====================================================================
* Copyright (c) 2000-2004 CollabNet. All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://subversion.tigris.org/license-1.html.
* If newer versions of this license are posted there, you may use a
* newer version instead, at your option.
*
* This software consists of voluntary contributions made by many
* individuals. For exact contribution history, see the revision
* history and logs, available at http://subversion.tigris.org/.
* ====================================================================
*/
#include <string.h>
#include <assert.h>
#include "svn_pools.h"
#include "svn_path.h"
#include "svn_time.h"
#include "svn_error.h"
#include "svn_md5.h"
#include "svn_fs.h"
#include "dag.h"
#include "err.h"
#include "fs.h"
#include "key-gen.h"
#include "node-rev.h"
#include "trail.h"
#include "reps-strings.h"
#include "revs-txns.h"
#include "id.h"
#include "util/fs_skels.h"
#include "bdb/txn-table.h"
#include "bdb/rev-table.h"
#include "bdb/nodes-table.h"
#include "bdb/copies-table.h"
#include "bdb/reps-table.h"
#include "bdb/strings-table.h"
#include "../libsvn_fs/fs-loader.h"
/* Initializing a filesystem. */
struct dag_node_t
{
/* The filesystem this dag node came from. */
svn_fs_t *fs;
/* The pool in which this dag_node_t was allocated. Unlike
filesystem and root pools, this is not a private pool for this
structure! The caller may have allocated other objects of their
own in it. */
apr_pool_t *pool;
/* The node revision ID for this dag node, allocated in POOL. */
svn_fs_id_t *id;
/* The node's type (file, dir, etc.) */
svn_node_kind_t kind;
/* The node's NODE-REVISION, or NULL if we haven't read it in yet.
This is allocated either in this node's POOL, if the node is
immutable, or in some trail's pool, if the node is mutable. For
mutable nodes, this must be reset to zero as soon as the trail in
which we read it is completed. Otherwise, we will end up with
out-of-date content here.
If you're willing to respect all the rules above, you can munge
this yourself, but you're probably better off just calling
`get_node_revision' and `set_node_revision', which take care of
things for you. */
node_revision_t *node_revision;
/* the path at which this node was created. */
const char *created_path;
};
/* Trivial helper/accessor functions. */
svn_node_kind_t svn_fs_base__dag_node_kind (dag_node_t *node)
{
return node->kind;
}
const svn_fs_id_t *
svn_fs_base__dag_get_id (dag_node_t *node)
{
return node->id;
}
const char *
svn_fs_base__dag_get_created_path (dag_node_t *node)
{
return node->created_path;
}
svn_fs_t *
svn_fs_base__dag_get_fs (dag_node_t *node)
{
return node->fs;
}
/* Clear NODE's cache of its node revision. */
static void
uncache_node_revision (void *baton)
{
dag_node_t *node = baton;
node->node_revision = NULL;
}
/* Dup NODEREV and all associated data into POOL */
static node_revision_t *
copy_node_revision (node_revision_t *noderev,
apr_pool_t *pool)
{
node_revision_t *nr = apr_pcalloc (pool, sizeof (*nr));
nr->kind = noderev->kind;
if (noderev->predecessor_id)
nr->predecessor_id = svn_fs_base__id_copy (noderev->predecessor_id, pool);
nr->predecessor_count = noderev->predecessor_count;
if (noderev->prop_key)
nr->prop_key = apr_pstrdup (pool, noderev->prop_key);
if (noderev->data_key)
nr->data_key = apr_pstrdup (pool, noderev->data_key);
if (noderev->edit_key)
nr->edit_key = apr_pstrdup (pool, noderev->edit_key);
if (noderev->created_path)
nr->created_path = apr_pstrdup (pool, noderev->created_path);
return nr;
}
/* Set NODE's node revision cache to NODEREV, as part of TRAIL.
NODEREV must be allocated in TRAIL->pool. */
static void
cache_node_revision (dag_node_t *node,
node_revision_t *noderev,
trail_t *trail)
{
/* ### todo: For now, we will always throw away the node revision at trail
completion. We used to keep it around longer because we used to
be reading in the full file contents here, but that's no longer
the case. */
#if 0
if (node_rev_is_mutable (noderev))
{
#endif /* 0 */
/* Mutable nodes might have other processes change their
contents, so we must throw away this node revision once the
trail is complete. */
svn_fs_base__record_completion (trail, uncache_node_revision, node);
node->node_revision = noderev;
#if 0
}
else
{
/* For immutable nodes, we can cache the contents permanently,
but we need to copy them over into the node's own pool. */
node->node_revision = copy_node_revision (noderev, node->pool);
}
#endif /* 0 */
}
/* Set *NODEREV_P to the cached node-revision for NODE, as part of
TRAIL. If NODE is immutable, the node-revision is allocated in
NODE->pool. If NODE is mutable, the node-revision is allocated in
TRAIL->pool, and the cache will be cleared as soon as TRAIL
completes.
If you plan to change the contents of NODE, be careful! We're
handing you a pointer directly to our cached node-revision, not
your own copy. If you change it as part of some operation, but
then some Berkeley DB function deadlocks or gets an error, you'll
need to back out your changes, or else the cache will reflect
changes that never got committed. It's probably best not to change
the structure at all. */
static svn_error_t *
get_node_revision (node_revision_t **noderev_p,
dag_node_t *node,
trail_t *trail)
{
node_revision_t *noderev;
/* If we've already got a copy, there's no need to read it in. */
if (! node->node_revision)
{
/* Read it in, and cache it. */
SVN_ERR (svn_fs_bdb__get_node_revision (&noderev, node->fs,
node->id, trail));
cache_node_revision (node, noderev, trail);
}
/* Now NODE->node_revision is set. */
*noderev_p = node->node_revision;
return SVN_NO_ERROR;
}
/* Set the NODE-REVISION of NODE to NODEREV as part of TRAIL, and keep
NODE's cache up to date. NODEREV must be allocated in TRAIL->pool. */
static svn_error_t *
set_node_revision (dag_node_t *node,
node_revision_t *noderev,
trail_t *trail)
{
/* Write it out. */
SVN_ERR (svn_fs_bdb__put_node_revision (node->fs, node->id, noderev, trail));
/* Since the write succeeded, update the cache. */
cache_node_revision (node, noderev, trail);
return SVN_NO_ERROR;
}
svn_boolean_t svn_fs_base__dag_check_mutable (dag_node_t *node,
const char *txn_id)
{
return (strcmp (svn_fs_base__id_txn_id (svn_fs_base__dag_get_id (node)),
txn_id) == 0);
}
svn_error_t *
svn_fs_base__dag_get_node (dag_node_t **node,
svn_fs_t *fs,
const svn_fs_id_t *id,
trail_t *trail)
{
dag_node_t *new_node;
node_revision_t *noderev;
/* Construct the node. */
new_node = apr_pcalloc (trail->pool, sizeof (*new_node));
new_node->fs = fs;
new_node->id = svn_fs_base__id_copy (id, trail->pool);
new_node->pool = trail->pool;
/* Grab the contents so we can inspect the node's kind and created path. */
SVN_ERR (get_node_revision (&noderev, new_node, trail));
/* Initialize the KIND and CREATED_PATH attributes */
new_node->kind = noderev->kind;
new_node->created_path = apr_pstrdup (trail->pool, noderev->created_path);
/* Return a fresh new node */
*node = new_node;
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_base__dag_get_revision (svn_revnum_t *rev,
dag_node_t *node,
trail_t *trail)
{
/* Use the txn ID from the NODE's id to look up the transaction and
get its revision number. */
return svn_fs_base__txn_get_revision
(rev, svn_fs_base__dag_get_fs (node),
svn_fs_base__id_txn_id (svn_fs_base__dag_get_id (node)), trail);
}
svn_error_t *
svn_fs_base__dag_get_predecessor_id (const svn_fs_id_t **id_p,
dag_node_t *node,
trail_t *trail)
{
node_revision_t *noderev;
SVN_ERR (get_node_revision (&noderev, node, trail));
*id_p = noderev->predecessor_id;
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_base__dag_get_predecessor_count (int *count,
dag_node_t *node,
trail_t *trail)
{
node_revision_t *noderev;
SVN_ERR (get_node_revision (&noderev, node, trail));
*count = noderev->predecessor_count;
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_base__dag_walk_predecessors (dag_node_t *node,
dag_pred_func_t callback,
void *baton,
trail_t *trail)
{
svn_fs_t *fs = svn_fs_base__dag_get_fs (node);
dag_node_t *this_node;
svn_boolean_t done = FALSE;
this_node = node;
while ((! done) && this_node)
{
node_revision_t *noderev;
/* Get the node revision for THIS_NODE so we can examine its
predecessor id. */
SVN_ERR (get_node_revision (&noderev, this_node, trail));
/* If THIS_NODE has a predecessor, replace THIS_NODE with the
precessor, else set it to NULL. */
if (noderev->predecessor_id)
SVN_ERR (svn_fs_base__dag_get_node (&this_node, fs,
noderev->predecessor_id, trail));
else
this_node = NULL;
/* Now call the user-supplied callback with our predecessor
node. */
if (callback)
SVN_ERR (callback (baton, this_node, &done, trail));
}
return SVN_NO_ERROR;
}
/* Trail body for svn_fs_base__dag_init_fs. */
static svn_error_t *
txn_body_dag_init_fs (void *baton, trail_t *trail)
{
node_revision_t noderev;
revision_t revision;
svn_revnum_t rev = SVN_INVALID_REVNUM;
svn_fs_t *fs = trail->fs;
svn_string_t date;
const char *txn_id;
const char *copy_id;
svn_fs_id_t *root_id = svn_fs_base__id_create ("0", "0", "0", trail->pool);
/* Create empty root directory with node revision 0.0.0. */
memset (&noderev, 0, sizeof (noderev));
noderev.kind = svn_node_dir;
noderev.created_path = "/";
SVN_ERR (svn_fs_bdb__put_node_revision (fs, root_id, &noderev, trail));
/* Create a new transaction (better have an id of "0") */
SVN_ERR (svn_fs_bdb__create_txn (&txn_id, fs, root_id, trail));
if (strcmp (txn_id, "0"))
return svn_error_createf
(SVN_ERR_FS_CORRUPT, 0,
"Corrupt DB: initial transaction id not '0' in filesystem '%s'",
fs->path);
/* Create a default copy (better have an id of "0") */
SVN_ERR (svn_fs_bdb__reserve_copy_id (©_id, fs, trail));
if (strcmp (copy_id, "0"))
return svn_error_createf
(SVN_ERR_FS_CORRUPT, 0,
"Corrupt DB: initial copy id not '0' in filesystem '%s'", fs->path);
SVN_ERR (svn_fs_bdb__create_copy (fs, copy_id, NULL, NULL, root_id,
copy_kind_real, trail));
/* Link it into filesystem revision 0. */
revision.txn_id = txn_id;
SVN_ERR (svn_fs_bdb__put_rev (&rev, fs, &revision, trail));
if (rev != 0)
return svn_error_createf (SVN_ERR_FS_CORRUPT, 0,
"Corrupt DB: initial revision number is not '0'"
" in filesystem '%s'", fs->path);
/* Promote our transaction to a "committed" transaction. */
SVN_ERR (svn_fs_base__txn_make_committed (fs, txn_id, rev, trail));
/* Set a date on revision 0. */
date.data = svn_time_to_cstring (apr_time_now(), trail->pool);
date.len = strlen (date.data);
return svn_fs_base__set_rev_prop (fs, 0, SVN_PROP_REVISION_DATE, &date,
trail);
}
svn_error_t *
svn_fs_base__dag_init_fs (svn_fs_t *fs)
{
return svn_fs_base__retry_txn (fs, txn_body_dag_init_fs, NULL, fs->pool);
}
/*** Directory node functions ***/
/* Some of these are helpers for functions outside this section. */
/* Given directory NODEREV in FS, set *ENTRIES_P to its entries list
hash, as part of TRAIL, or to NULL if NODEREV has no entries. The
entries list will be allocated in TRAIL->pool, and the entries in
that list will not have interesting value in their 'kind' fields.
If NODEREV is not a directory, return the error SVN_ERR_FS_NOT_DIRECTORY. */
static svn_error_t *
get_dir_entries (apr_hash_t **entries_p,
svn_fs_t *fs,
node_revision_t *noderev,
trail_t *trail)
{
apr_hash_t *entries = apr_hash_make (trail->pool);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -