150472Speter/* env.h : managing the BDB environment
27130Srgrimes *
378822Snik * ====================================================================
450203Srgrimes *    Licensed to the Apache Software Foundation (ASF) under one
57130Srgrimes *    or more contributor license agreements.  See the NOTICE file
639161Sobrien *    distributed with this work for additional information
78571Srgrimes *    regarding copyright ownership.  The ASF licenses this file
8130416Smlaier *    to you under the Apache License, Version 2.0 (the
9130416Smlaier *    "License"); you may not use this file except in compliance
108571Srgrimes *    with the License.  You may obtain a copy of the License at
117130Srgrimes *
12146762Srwatson *      http://www.apache.org/licenses/LICENSE-2.0
13146762Srwatson *
14122402Sharti *    Unless required by applicable law or agreed to in writing,
15122402Sharti *    software distributed under the License is distributed on an
16123051Sru *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17170189Sru *    KIND, either express or implied.  See the License for the
18170189Sru *    specific language governing permissions and limitations
19170189Sru *    under the License.
20123051Sru * ====================================================================
21123051Sru */
22132751Skan
23132751Skan#ifndef SVN_LIBSVN_FS_BDB_ENV_H
24123051Sru#define SVN_LIBSVN_FS_BDB_ENV_H
25170189Sru
26170189Sru#define SVN_WANT_BDB
27172422Sru#include "svn_private_config.h"
28170189Sru
29172422Sru#include <apr_pools.h>
30170189Sru#include <apr_file_io.h>
31172422Sru
32170189Sru#include "bdb_compat.h"
33172422Sru
34170189Sru#ifdef __cplusplus
35172422Sruextern "C" {
36170189Sru#endif /* __cplusplus */
37172422Sru
38172422Sru
39172422Sru/* The name of the Berkeley DB config file.  */
40172422Sru#define BDB_CONFIG_FILE "DB_CONFIG"
41170189Sru
42170189Sru/* Prefix string for BDB errors. */
43172422Sru#define BDB_ERRPFX_STRING "svn (bdb): "
44170189Sru
45172422Sru
46170189Sru/* Opaque descriptor of an open BDB environment. */
47172422Srutypedef struct bdb_env_t bdb_env_t;
48170189Sru
49172422Sru
50170189Sru/* Thread-specific error info related to the bdb_env_t. */
51172422Srutypedef struct bdb_error_info_t
52170189Sru{
53172422Sru  /* We hold the extended info here until the Berkeley DB function returns.
54170189Sru     It usually returns an error code, triggering the collection and
55170189Sru     wrapping of the additional errors stored here.
56170189Sru
57172422Sru     Note: In some circumstances BDB will call the error function and not
58170189Sru     go on to return an error code, so the caller must always check whether
59170189Sru     pending_errors is non-NULL to avoid leaking errors.  This behaviour
60170189Sru     has been seen when running recovery on a repository upgraded to 4.3
61172422Sru     that still has old 4.2 log files present, a typical error string is
62170189Sru     "Skipping log file db/log.0000000002: historic log version 8" */
63172422Sru  svn_error_t *pending_errors;
64170189Sru
65172422Sru  /* We permitted clients of our library to install a Berkeley BDB errcall.
66170189Sru     Since we now use the errcall ourselves, we must store and invoke a user
67172422Sru     errcall, to maintain our API guarantees. */
68170189Sru  void (*user_callback)(const char *errpfx, char *msg);
69172422Sru
70170189Sru  /* The reference count.  It counts the number of bdb_env_baton_t
71172422Sru     instances that refer to this object. */
72170189Sru  unsigned refcount;
73170189Sru
74170189Sru} bdb_error_info_t;
75123051Sru
76169716Skan
77169716Skan/* The Berkeley DB environment baton. */
78123051Srutypedef struct bdb_env_baton_t
79123051Sru{
8039250Sgibbs  /* The Berkeley DB environment. This pointer must be identical to
81195534Sscottl     the one in the bdb_env_t. */
82195534Sscottl  DB_ENV *env;
8339250Sgibbs
8439250Sgibbs  /* The (opaque) cached environment descriptor. */
8539250Sgibbs  bdb_env_t *bdb;
86104489Ssam
87104489Ssam  /* The error info related to this baton. */
8856583Sn_hibma  bdb_error_info_t *error_info;
89142744Snjl} bdb_env_baton_t;
90142744Snjl
9188748Sambrisko
9288748Sambrisko
93123288Sobrien/* Flag combination for opening a shared BDB environment. */
94123288Sobrien#define SVN_BDB_STANDARD_ENV_FLAGS (DB_CREATE       \
95123051Sru                                    | DB_INIT_LOCK  \
96123051Sru                                    | DB_INIT_LOG   \
97147191Sjkoshy                                    | DB_INIT_MPOOL \
98147191Sjkoshy                                    | DB_INIT_TXN   \
9977756Sru                                    | SVN_BDB_AUTO_RECOVER)
10077756Sru
101141396Sphk/* Flag combination for opening a private BDB environment. */
102141396Sphk#define SVN_BDB_PRIVATE_ENV_FLAGS (DB_CREATE       \
103103627Struckman                                   | DB_INIT_LOG   \
104103627Struckman                                   | DB_INIT_MPOOL \
105152187Srwatson                                   | DB_INIT_TXN   \
106152187Srwatson                                   | DB_PRIVATE)
107206668Spjd
108206668Spjd
109178818Sjhb/* Iniitalize the BDB back-end's private stuff. */
110178818Sjhbsvn_error_t *svn_fs_bdb__init(apr_pool_t* pool);
111178818Sjhb
112178818Sjhb
113105400Stmm/* Allocate the Berkeley DB descriptor BDB and open the environment.
114105400Stmm *
115140246Sdds * Allocate *BDBP from POOL and open (*BDBP)->env in PATH, using FLAGS
116140246Sdds * and MODE.  If applicable, set the BDB_AUTO_COMMIT flag for this
117172422Sru * environment.
118172422Sru *
11960724Speter * Use POOL for temporary allocation.
12060724Speter *
121103627Struckman * Note: This function may return a bdb_env_baton_t object that refers
122103627Struckman *       to a previously opened environment.  If FLAGS contains
123152318Sdelphij *       DB_PRIVATE and the environment is already open, the function
124152318Sdelphij *       will fail (this isn't a problem in practice, because a caller
12556583Sn_hibma *       should obtain an exclusive lock on the repository before
12656583Sn_hibma *       opening the environment).
127116258Sharti */
128116258Sharti
129162117Semaxsvn_error_t *svn_fs_bdb__open(bdb_env_baton_t **bdb_batonp,
130162117Semax                              const char *path,
13170811Speter                              u_int32_t flags, int mode,
13270811Speter                              apr_pool_t *pool);
13356583Sn_hibma
13475415Sbp/* Close the Berkeley DB descriptor BDB.
13588050Sgreen *
13688050Sgreen * Note: This function might not actually close the environment if it
13777031Sru *       has been opened more than once.
13877031Sru */
13977031Srusvn_error_t *svn_fs_bdb__close(bdb_env_baton_t *bdb_baton);
14077031Sru
14177162Sru
14277162Sru/* Get the panic state of the open BDB environment. */
143192545Srmacklemsvn_boolean_t svn_fs_bdb__get_panic(bdb_env_baton_t *bdb_baton);
144192545Srmacklem
14577223Sru/* Set the panic flag on the open BDB environment. */
14677223Sruvoid svn_fs_bdb__set_panic(bdb_env_baton_t *bdb_baton);
14777031Sru
14877031Sru
14977223Sru/* Remove the Berkeley DB environment at PATH.
15077223Sru *
15177031Sru * Use POOL for temporary allocation.
15277031Sru */
15377031Srusvn_error_t *svn_fs_bdb__remove(const char *path, apr_pool_t *pool);
15477031Sru
15575461Sru#ifdef __cplusplus
15675461Sru}
157123051Sru#endif /* __cplusplus */
158123051Sru
15977031Sru#endif /* SVN_LIBSVN_FS_BDB_ENV_H */
16077031Sru