dag.c
来自「linux subdivision ying gai ke yi le ba」· C语言 代码 · 共 1,229 行 · 第 1/3 页
C
1,229 行
/* 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 "svn_pools.h"
#include "dag.h"
#include "err.h"
#include "fs.h"
#include "key-gen.h"
#include "revs-txns.h"
#include "fs_fs.h"
#include "id.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 in this node's POOL.
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_fs__dag_node_kind (dag_node_t *node)
{
return node->kind;
}
const svn_fs_id_t *
svn_fs_fs__dag_get_id (dag_node_t *node)
{
return node->id;
}
const char *
svn_fs_fs__dag_get_created_path (dag_node_t *node)
{
return node->created_path;
}
svn_fs_t *
svn_fs_fs__dag_get_fs (dag_node_t *node)
{
return node->fs;
}
/* 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_fs__id_copy (noderev->predecessor_id, pool);
nr->predecessor_count = noderev->predecessor_count;
if (noderev->copyfrom_path)
nr->copyfrom_path = apr_pstrdup (pool, noderev->copyfrom_path);
nr->copyfrom_rev = noderev->copyfrom_rev;
nr->copyroot_path = apr_pstrdup (pool, noderev->copyroot_path);
nr->copyroot_rev = noderev->copyroot_rev;
nr->predecessor_count = noderev->predecessor_count;
nr->data_rep = svn_fs_fs__rep_copy (noderev->data_rep, pool);
nr->prop_rep = svn_fs_fs__rep_copy (noderev->prop_rep, pool);
if (noderev->created_path)
nr->created_path = apr_pstrdup (pool, noderev->created_path);
return nr;
}
/* Set *NODEREV_P to the cached node-revision for NODE. If NODE is
immutable, the node-revision is allocated in NODE->pool. If NODE
is mutable, the node-revision is allocated in POOL.
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,
apr_pool_t *pool)
{
node_revision_t *noderev;
/* If we've already got a copy, there's no need to read it in. */
if (! node->node_revision)
{
SVN_ERR (svn_fs_fs__get_node_revision (&noderev, node->fs,
node->id, pool));
node->node_revision = noderev;
}
/* Now NODE->node_revision is set. */
*noderev_p = node->node_revision;
return SVN_NO_ERROR;
}
svn_boolean_t svn_fs_fs__dag_check_mutable (dag_node_t *node,
const char *txn_id)
{
return (svn_fs_fs__id_txn_id (svn_fs_fs__dag_get_id (node)) != NULL);
}
svn_error_t *
svn_fs_fs__dag_get_node (dag_node_t **node,
svn_fs_t *fs,
const svn_fs_id_t *id,
apr_pool_t *pool)
{
dag_node_t *new_node;
node_revision_t *noderev;
/* Construct the node. */
new_node = apr_pcalloc (pool, sizeof (*new_node));
new_node->fs = fs;
new_node->id = svn_fs_fs__id_copy (id, pool);
new_node->pool = pool;
/* Grab the contents so we can inspect the node's kind and created path. */
SVN_ERR (get_node_revision (&noderev, new_node, pool));
/* Initialize the KIND and CREATED_PATH attributes */
new_node->kind = noderev->kind;
new_node->created_path = apr_pstrdup (pool, noderev->created_path);
/* Return a fresh new node */
*node = new_node;
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_fs__dag_get_revision (svn_revnum_t *rev,
dag_node_t *node,
apr_pool_t *pool)
{
/* Look up the committed revision from the Node-ID. */
*rev = svn_fs_fs__id_rev (node->id);
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_fs__dag_get_predecessor_id (const svn_fs_id_t **id_p,
dag_node_t *node,
apr_pool_t *pool)
{
node_revision_t *noderev;
SVN_ERR (get_node_revision (&noderev, node, pool));
*id_p = noderev->predecessor_id;
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_fs__dag_get_predecessor_count (int *count,
dag_node_t *node,
apr_pool_t *pool)
{
node_revision_t *noderev;
SVN_ERR (get_node_revision (&noderev, node, pool));
*count = noderev->predecessor_count;
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_fs__dag_walk_predecessors (dag_node_t *node,
dag_pred_func_t callback,
void *baton,
apr_pool_t *pool)
{
svn_fs_t *fs = svn_fs_fs__dag_get_fs (node);
dag_node_t *this_node;
svn_boolean_t done = FALSE;
apr_pool_t *last_iterpool, *iterpool, *tmp_iterpool;
last_iterpool = svn_pool_create (pool);
iterpool = svn_pool_create (pool);
this_node = node;
while ((! done) && this_node)
{
node_revision_t *noderev;
/* Cycle the pools so iterpool will remain valid on the next
* iteration. */
tmp_iterpool = last_iterpool;
last_iterpool = iterpool;
iterpool = tmp_iterpool;
svn_pool_clear (iterpool);
/* Get the node revision for THIS_NODE so we can examine its
predecessor id. */
SVN_ERR (get_node_revision (&noderev, this_node, iterpool));
/* 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_fs__dag_get_node (&this_node, fs,
noderev->predecessor_id,
iterpool));
else
this_node = NULL;
/* Now call the user-supplied callback with our predecessor
node. */
if (callback)
SVN_ERR (callback (baton, this_node, &done, iterpool));
}
apr_pool_destroy (iterpool);
apr_pool_destroy (last_iterpool);
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_fs__dag_init_fs (svn_fs_t *fs)
{
apr_hash_t *proplist;
svn_string_t date;
/* Write out a rev file for revision 0. */
SVN_ERR (svn_fs_fs__write_revision_zero (fs));
/* Set a date on revision 0. */
date.data = svn_time_to_cstring (apr_time_now(), fs->pool);
date.len = strlen (date.data);
proplist = apr_hash_make (fs->pool);
apr_hash_set (proplist, SVN_PROP_REVISION_DATE, APR_HASH_KEY_STRING, &date);
return svn_fs_fs__set_revision_proplist (fs, 0, proplist, fs->pool);
}
/*** Directory node functions ***/
/* Some of these are helpers for functions outside this section. */
/* Set *ID_P to the node-id for entry NAME in PARENT. If no such
entry, set *ID_P to NULL but do not error. The entry is allocated
in POOL or in the same pool as PARENT; the caller should copy if it
cares. */
static svn_error_t *
dir_entry_id_from_node (const svn_fs_id_t **id_p,
dag_node_t *parent,
const char *name,
apr_pool_t *pool)
{
apr_hash_t *entries;
svn_fs_dirent_t *dirent;
SVN_ERR (svn_fs_fs__dag_dir_entries (&entries, parent, pool));
if (entries)
dirent = apr_hash_get (entries, name, APR_HASH_KEY_STRING);
else
dirent = NULL;
*id_p = dirent ? dirent->id : NULL;
return SVN_NO_ERROR;
}
/* Add or set in PARENT a directory entry NAME pointing to ID.
Allocations are done in POOL.
Assumptions:
- PARENT is a mutable directory.
- ID does not refer to an ancestor of parent
- NAME is a single path component
*/
static svn_error_t *
set_entry (dag_node_t *parent,
const char *name,
const svn_fs_id_t *id,
svn_node_kind_t kind,
const char *txn_id,
apr_pool_t *pool)
{
node_revision_t *parent_noderev;
/* Get the parent's node-revision. */
SVN_ERR (get_node_revision (&parent_noderev, parent, pool));
/* Set the new entry. */
SVN_ERR (svn_fs_fs__set_entry (parent->fs, txn_id, parent_noderev, name, id,
kind, pool));
return SVN_NO_ERROR;
}
/* Make a new entry named NAME in PARENT. If IS_DIR is true, then the
node revision the new entry points to will be a directory, else it
will be a file. The new node will be allocated in POOL. PARENT
must be mutable, and must not have an entry named NAME. */
static svn_error_t *
make_entry (dag_node_t **child_p,
dag_node_t *parent,
const char *parent_path,
const char *name,
svn_boolean_t is_dir,
const char *txn_id,
apr_pool_t *pool)
{
const svn_fs_id_t *new_node_id;
node_revision_t new_noderev, *parent_noderev;
/* Make sure that NAME is a single path component. */
if (! svn_path_is_single_path_component (name))
return svn_error_createf
(SVN_ERR_FS_NOT_SINGLE_PATH_COMPONENT, NULL,
"Attempted to create a node with an illegal name '%s'", name);
/* Make sure that parent is a directory */
if (parent->kind != svn_node_dir)
return svn_error_create
(SVN_ERR_FS_NOT_DIRECTORY, NULL,
"Attempted to create entry in non-directory parent");
/* Check that the parent is mutable. */
if (! svn_fs_fs__dag_check_mutable (parent, txn_id))
return svn_error_createf
(SVN_ERR_FS_NOT_MUTABLE, NULL,
"Attempted to clone child of non-mutable node");
/* Create the new node's NODE-REVISION */
memset (&new_noderev, 0, sizeof (new_noderev));
new_noderev.kind = is_dir ? svn_node_dir : svn_node_file;
new_noderev.created_path = svn_path_join (parent_path, name, pool);
SVN_ERR (get_node_revision (&parent_noderev, parent, pool));
new_noderev.copyroot_path = apr_pstrdup (pool,
parent_noderev->copyroot_path);
new_noderev.copyroot_rev = parent_noderev->copyroot_rev;
new_noderev.copyfrom_rev = SVN_INVALID_REVNUM;
new_noderev.copyfrom_path = NULL;
SVN_ERR (svn_fs_fs__create_node
(&new_node_id, svn_fs_fs__dag_get_fs (parent), &new_noderev,
svn_fs_fs__id_copy_id (svn_fs_fs__dag_get_id (parent)),
txn_id, pool));
/* Create a new dag_node_t for our new node */
SVN_ERR (svn_fs_fs__dag_get_node (child_p, svn_fs_fs__dag_get_fs (parent),
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?