⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 env.c

📁 subversion-1.4.3-1.tar.gz 配置svn的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* env.h : managing the BDB environment * * ==================================================================== * Copyright (c) 2000-2005 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 <assert.h>#include <apr.h>#if APR_HAS_THREADS#include <apr_thread_mutex.h>#include <apr_thread_proc.h>#include <apr_time.h>#endif#include <apr_atomic.h>#include <apr_strings.h>#include <apr_hash.h>#include "svn_path.h"#include "svn_pools.h"#include "svn_utf.h"#include "bdb-err.h"#include "bdb_compat.h"#include "env.h"/* A note about the BDB environment descriptor cache.   With the advent of DB_REGISTER in BDB-4.4, a process may only open   an environment handle once.  This means that we must maintain a   cache of open environment handles, with reference counts.  We   allocate each environment descriptor (a bdb_env_t) from its own   pool.  The cache itself (and the cache pool) are shared between   threads, so all direct or indirect access to the pool is serialized   with a global mutex.   Because several threads can now hse the same DB_ENV handle, we must   use the DB_THREAD flag when opening the environments, otherwise the   env handles (and all of libsvn_fs_base) won't be thread-safe.   If we use DB_THREAD, however, all of the code that reads data from   the database without a cursor must use either DB_DBT_MALLOC,   DB_DBT_REALLOC, or DB_DBT_USERMEM, as described in the BDB   documentation.   (Oh, yes -- using DB_THREAD might not work on some systems. But   then, it's quite probable that threading is seriously broken on   those systems anyway, so we'll rely on APR_HAS_THREADS.)*//* The apr_atomic API changed somewhat between apr-0.x and apr-1.x.   ### Should we move these defines to svn_private_config.h? *//* ### Note: svn__atomic_cas should not be combined with the other       svn__atomic operations.  A comment in apr_atomic.h explains       that on some platforms, the CAS function is implemented in a       way that is incompatible with the other atomic operations. */#include <apr_version.h>#if APR_MAJOR_VERSION > 0# define svn__atomic_t apr_uint32_t# define svn__atomic_read(mem) apr_atomic_read32((mem))# define svn__atomic_set(mem, val) apr_atomic_set32((mem), (val))# define svn__atomic_cas(mem, with, cmp) \    apr_atomic_cas32((mem), (with), (cmp))#else# define svn__atomic_t apr_atomic_t# define svn__atomic_read(mem) apr_atomic_read((mem))# define svn__atomic_set(mem, val) apr_atomic_set((mem), (val))# define svn__atomic_cas(mem, with, cmp) \    apr_atomic_cas((mem), (with), (cmp))#endif /* APR_MAJOR_VERSION *//* The cache key for a Berkeley DB environment descriptor.  This is a   combination of the device ID and INODE number of the Berkeley DB   config file.   XXX FIXME: Although the dev+inode combination is supposed do be   unique, apparently that's not always the case with some remote   filesystems.  We /should/ be safe using this as a unique hash key,   because the database must be on a local filesystem.  We can hope,   anyway. */typedef struct{  apr_dev_t device;  apr_ino_t inode;} bdb_env_key_t;/* The cached Berkeley DB environment descriptor. */struct bdb_env_t{  /**************************************************************************/  /* Error Reporting */  /* Berkeley DB returns extended error info by callback before returning     an error code from the failing function.  The callback baton type is a     string, not an arbitrary struct, so we prefix our struct with a valid     string, to avoid problems should BDB ever try to interpret our baton as     a string.  Initializers of this structure must strcpy the value of     BDB_ERRPFX_STRING into this array.  */  char errpfx_string[sizeof(BDB_ERRPFX_STRING)];  /* Extended error information. */#if APR_HAS_THREADS  apr_threadkey_t *error_info;   /* Points to a bdb_error_info_t. */#else  bdb_error_info_t error_info;#endif  /**************************************************************************/  /* BDB Environment Cache */  /* The Berkeley DB environment. */  DB_ENV *env;  /* The flags with which this environment was opened.  Reopening the     environment with a different set of flags is not allowed.  Trying     to change the state of the DB_PRIVATE flag is an especially bad     idea, so svn_fs_bdb__open() forbids any flag changes. */  u_int32_t flags;  /* The home path of this environment; a canonical SVN path encoded in     UTF-8 and allocated from this decriptor's pool. */  const char *path;  /* The home path of this environment, in the form expected by BDB. */  const char *path_bdb;  /* The reference count for this environment handle; this is     essentially the difference between the number of calls to     svn_fs_bdb__open and svn_fs_bdb__close. */  unsigned refcount;  /* If this flag is TRUE, someone has detected that the environment     descriptor is in a panicked state and should be removed from the     cache.     Note 1: Once this flag is set, it must not be cleared again.     Note 2: Unlike other fields in this structure, this field is not             protected by the cache mutex on threaded platforms, and             should only be accesses via the svn__atomic functions. */  volatile svn__atomic_t panic;  /* The key for the environment descriptor cache. */  bdb_env_key_t key;  /* The handle of the open DB_CONFIG file.     We keep the DB_CONFIG file open in this process as long as the     environment handle itself is open.  On Windows, this guarantees     that the cache key remains unique; here's what the Windows SDK     docs have to say about the file index (interpreted as the INODE     number by APR):        "This value is useful only while the file is open by at least        one process.  If no processes have it open, the index may        change the next time the file is opened."     Now, we certainly don't want a unique key to change while it's     being used, do we... */  apr_file_t *dbconfig_file;  /* The pool associated with this environment descriptor.     Because the descriptor has a life of its own, the structure and     any data associated with it are allocated from their own global     pool. */  apr_pool_t *pool;};#if APR_HAS_THREADS/* Get the thread-specific error info from a bdb_env_t. */static bdb_error_info_t *get_error_info(bdb_env_t *bdb){  void *priv;  apr_threadkey_private_get(&priv, bdb->error_info);  if (!priv)    {      priv = calloc(1, sizeof(bdb_error_info_t));      apr_threadkey_private_set(priv, bdb->error_info);    }  return priv;}#else#define get_error_info(bdb) (&(bdb)->error_info)#endif /* APR_HAS_THREADS *//* Convert a BDB error to a Subversion error. */static svn_error_t *convert_bdb_error(bdb_env_t *bdb, int db_err){  if (db_err)    {      bdb_env_baton_t bdb_baton;      bdb_baton.env = bdb->env;      bdb_baton.bdb = bdb;      bdb_baton.error_info = get_error_info(bdb);      SVN_BDB_ERR(&bdb_baton, db_err);    }  return SVN_NO_ERROR;}/* Allocating an appropriate Berkeley DB environment object.  *//* BDB error callback.  See bdb_error_info_t in env.h for more info.   Note: bdb_error_gatherer is a macro with BDB < 4.3, so be careful how   you use it! */static voidbdb_error_gatherer(const DB_ENV *dbenv, const char *baton, const char *msg){  bdb_error_info_t *error_info = get_error_info((bdb_env_t *) baton);  svn_error_t *new_err;  SVN_BDB_ERROR_GATHERER_IGNORE(dbenv);  new_err = svn_error_createf(SVN_NO_ERROR, NULL, "bdb: %s", msg);  if (error_info->pending_errors)    svn_error_compose(error_info->pending_errors, new_err);  else    error_info->pending_errors = new_err;  if (error_info->user_callback)    error_info->user_callback(NULL, (char *)msg); /* ### I hate this cast... */}/* Pool cleanup for the cached environment descriptor. */static apr_status_tcleanup_env(void *data){  bdb_env_t *bdb = data;  bdb->pool = NULL;  bdb->dbconfig_file = NULL;   /* will be closed during pool destruction */#if APR_HAS_THREADS  apr_threadkey_private_delete(bdb->error_info);#endif /* APR_HAS_THREADS */  /* If there are no references to this descriptor, free its memory here,     so that we don't leak it if create_env returns an error.     See bdb_close, which takes care of freeing this memory if the     environment is still open when the cache is destroyed. */  if (!bdb->refcount)    free(data);  return APR_SUCCESS;}#if APR_HAS_THREADS/* This cleanup is the fall back plan.  If the thread exits and the   environment hasn't been closed it's responsible for cleanup of the   thread local error info variable, which would otherwise be leaked.   Normally it will not be called, because svn_fs_base__close will   set the thread's error info to NULL after cleaning it up. */static voidcleanup_error_info(void *baton){  bdb_error_info_t *error_info = baton;  if (error_info)    svn_error_clear(error_info->pending_errors);  free(error_info);}#endif /* APR_HAS_THREADS *//* Create a Berkeley DB environment. */static svn_error_t *create_env(bdb_env_t **bdbp, const char *path, apr_pool_t *pool){  int db_err;  bdb_env_t *bdb;  const char *path_bdb;  char *tmp_path, *tmp_path_bdb;  apr_size_t path_size, path_bdb_size;#if SVN_BDB_PATH_UTF8  path_bdb = svn_path_local_style(path, pool);#else  SVN_ERR(svn_utf_cstring_from_utf8(&path_bdb,                                    svn_path_local_style(path, pool),                                    pool));#endif    /* Allocate the whole structure, including strings, from the heap,     because it must survive the cache pool cleanup. */  path_size = strlen(path) + 1;  path_bdb_size = strlen(path_bdb) + 1;  bdb = calloc(1, sizeof(*bdb) + path_size + path_bdb_size);  /* We must initialize this now, as our callers may assume their bdb     pointer is valid when checking for errors.  */  apr_pool_cleanup_register(pool, bdb, cleanup_env, apr_pool_cleanup_null);  apr_cpystrn(bdb->errpfx_string, BDB_ERRPFX_STRING,              sizeof(bdb->errpfx_string));  bdb->path = tmp_path = (char*)(bdb + 1);  bdb->path_bdb = tmp_path_bdb = tmp_path + path_size;  apr_cpystrn(tmp_path, path, path_size);  apr_cpystrn(tmp_path_bdb, path_bdb, path_bdb_size);  bdb->pool = pool;  *bdbp = bdb;#if APR_HAS_THREADS  {    apr_status_t apr_err = apr_threadkey_private_create(&bdb->error_info,                                                        cleanup_error_info,                                                        pool);    if (apr_err)      return svn_error_create(apr_err, NULL,                              "Can't allocate thread-specific storage"                              " for the Berkeley DB environment descriptor");  }#endif /* APR_HAS_THREADS */  db_err = db_env_create(&(bdb->env), 0);  if (!db_err)    {      bdb->env->set_errpfx(bdb->env, (char *) bdb);      /* bdb_error_gatherer is in parens to stop macro expansion. */      bdb->env->set_errcall(bdb->env, (bdb_error_gatherer));      /* Needed on Windows in case Subversion and Berkeley DB are using         different C runtime libraries  */      db_err = bdb->env->set_alloc(bdb->env, malloc, realloc, free);      /* If we detect a deadlock, select a transaction to abort at         random from those participating in the deadlock.  */      if (!db_err)        db_err = bdb->env->set_lk_detect(bdb->env, DB_LOCK_RANDOM);    }  return convert_bdb_error(bdb, db_err);}/* The environment descriptor cache. *//* The global pool used for this cache. */static apr_pool_t *bdb_cache_pool = NULL;/* The cache.  The items are bdb_env_t structures. */static apr_hash_t *bdb_cache = NULL;#if APR_HAS_THREADS/* The mutex that protects bdb_cache. */static apr_thread_mutex_t *bdb_cache_lock = NULL;/* Cleanup callback to NULL out the cache and its lock, so we don't try to   use them after the pool has been cleared during global shutdown. */static apr_status_tclear_cache(void *data){  bdb_cache = NULL;  bdb_cache_lock = NULL;  return APR_SUCCESS;}/* Magic values for atomic initialization of the environment cache. */#define BDB_CACHE_UNINITIALIZED 0#define BDB_CACHE_START_INIT    1#define BDB_CACHE_INIT_FAILED   2#define BDB_CACHE_INITIALIZED   3static volatile svn__atomic_t bdb_cache_state = BDB_CACHE_UNINITIALIZED;#endif /* APR_HAS_THREADS */svn_error_t *svn_fs_bdb__init(void){  /* We have to initialize the cache exactly once.  Because APR     doesn't have statically-initialized mutexes, we implement a poor     man's spinlock using svn__atomic_cas. */#if APR_HAS_THREADS  apr_status_t apr_err;  svn__atomic_t cache_state = svn__atomic_cas(&bdb_cache_state,                                              BDB_CACHE_START_INIT,                                              BDB_CACHE_UNINITIALIZED);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -