📄 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_path.h"#include "svn_time.h"#include "svn_error.h"#include "svn_md5.h"#include "svn_fs.h"#include "svn_props.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"#include "svn_private_config.h"/* Initializing a filesystem. */struct dag_node_t{ /*** NOTE: Keeping in-memory representations of disk data that can be changed by other accessors is a nasty business. Such representations are basically a cache with some pretty complex invalidation rules. For example, the "node revision" associated with a DAG node ID can look completely different to a process that has modified that information as part of a Berkeley DB transaction than it does to some other process. That said, there are some aspects of a "node revision" which never change, like its 'id' or 'kind'. Our best bet is to limit ourselves to exposing outside of this interface only those immutable aspects of a DAG node representation. ***/ /* The filesystem this dag node came from. */ svn_fs_t *fs; /* The node revision ID for this dag node. */ svn_fs_id_t *id; /* The node's type (file, dir, etc.) */ svn_node_kind_t kind; /* 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;}/* 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;}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, 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_base__id_copy(id, pool); /* Grab the contents so we can cache some of the immutable parts of it. */ SVN_ERR(svn_fs_bdb__get_node_revision(&noderev, fs, id, trail, pool)); /* Initialize the KIND and CREATED_PATH attributes */ new_node->kind = noderev->kind; new_node->created_path = 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, apr_pool_t *pool){ /* 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, pool);}svn_error_t *svn_fs_base__dag_get_predecessor_id(const svn_fs_id_t **id_p, dag_node_t *node, trail_t *trail, apr_pool_t *pool){ node_revision_t *noderev; SVN_ERR(svn_fs_bdb__get_node_revision(&noderev, node->fs, node->id, trail, pool)); *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, apr_pool_t *pool){ node_revision_t *noderev; SVN_ERR(svn_fs_bdb__get_node_revision(&noderev, node->fs, node->id, trail, pool)); *count = noderev->predecessor_count; 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, trail->pool)); /* Create a new transaction (better have an id of "0") */ SVN_ERR(svn_fs_bdb__create_txn(&txn_id, fs, root_id, trail, trail->pool)); 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, trail->pool)); 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, trail->pool)); /* Link it into filesystem revision 0. */ revision.txn_id = txn_id; SVN_ERR(svn_fs_bdb__put_rev(&rev, fs, &revision, trail, trail->pool)); 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, trail->pool)); /* 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, trail->pool);}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 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_pool_t *pool){ apr_hash_t *entries = apr_hash_make(pool); apr_hash_index_t *hi; svn_string_t entries_raw; skel_t *entries_skel; /* Error if this is not a directory. */ if (noderev->kind != svn_node_dir) return svn_error_create (SVN_ERR_FS_NOT_DIRECTORY, NULL, _("Attempted to create entry in non-directory parent")); /* If there's a DATA-KEY, there might be entries to fetch. */ if (noderev->data_key) { /* Now we have a rep, follow through to get the entries. */ SVN_ERR(svn_fs_base__rep_contents(&entries_raw, fs, noderev->data_key, trail, pool)); entries_skel = svn_fs_base__parse_skel(entries_raw.data, entries_raw.len, pool); /* Were there entries? Make a hash from them. */ if (entries_skel) SVN_ERR(svn_fs_base__parse_entries_skel(&entries, entries_skel, pool)); } /* No hash? No problem. */ *entries_p = NULL; if (! entries) return SVN_NO_ERROR; /* Else, convert the hash from a name->id mapping to a name->dirent one. */ *entries_p = apr_hash_make(pool); for (hi = apr_hash_first(pool, entries); hi; hi = apr_hash_next(hi)) { const void *key; apr_ssize_t klen; void *val; svn_fs_dirent_t *dirent = apr_palloc(pool, sizeof(*dirent)); /* KEY will be the entry name in ancestor, VAL the id. */ apr_hash_this(hi, &key, &klen, &val); dirent->name = key; dirent->id = val; dirent->kind = svn_node_unknown; apr_hash_set(*entries_p, key, klen, dirent); } /* Return our findings. */ return SVN_NO_ERROR;}/* Set *ID_P to the node-id for entry NAME in PARENT, as part of TRAIL. 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, trail_t *trail, apr_pool_t *pool){ apr_hash_t *entries; svn_fs_dirent_t *dirent; SVN_ERR(svn_fs_base__dag_dir_entries(&entries, parent, trail, 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 TRAIL. 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 *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -