Skip to content

Commit cdd3d48

Browse files
committed
Backport 1890952, 1890966, 1890980
apr_dbm: Add dedicated apr_dbm_get_driver() function that returns details of the driver selected and any error encountered. Add the apr_dbm_open2() function that references the driver. git-svn-id: https://svn.apache.org/repos/asf/apr/apr-util/branches/1.7.x@1891018 13f79535-47bb-0310-9956-ffa450edef68
1 parent d016e44 commit cdd3d48

File tree

8 files changed

+121
-21
lines changed

8 files changed

+121
-21
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
-*- coding: utf-8 -*-
22
Changes with APR-util 1.7.0
33

4+
*) apr_dbm: Add dedicated apr_dbm_get_driver() function that returns
5+
details of the driver selected and any error encountered. Add the
6+
apr_dbm_open2() function that references the driver. [Graham Leggett]
7+
48
*) Trick autoconf into printing the correct default prefix in the help.
59
[Stefan Fritsch]
610

dbm/apr_dbm.c

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#error a DBM implementation was not specified
5858
#endif
5959

60+
#define ERROR_SIZE 1024
61+
6062
#if APU_DSO_BUILD
6163

6264
static apr_hash_t *drivers = NULL;
@@ -75,12 +77,19 @@ static apr_status_t dbm_term(void *ptr)
7577

7678
#endif /* APU_DSO_BUILD */
7779

78-
static apr_status_t dbm_open_type(apr_dbm_type_t const* * vtable,
79-
const char *type,
80-
apr_pool_t *pool)
80+
APU_DECLARE(apr_status_t) apr_dbm_get_driver(const apr_dbm_driver_t **vtable,
81+
const char *type, const apu_err_t **result, apr_pool_t *pool)
8182
{
8283
#if !APU_DSO_BUILD
8384

85+
if (result) {
86+
*result = NULL; /* until further notice */
87+
}
88+
89+
if (!type) {
90+
type = DBM_NAME;
91+
}
92+
8493
*vtable = NULL;
8594
if (!strcasecmp(type, "default")) *vtable = &DBM_VTABLE;
8695
#if APU_HAVE_DB
@@ -98,18 +107,39 @@ static apr_status_t dbm_open_type(apr_dbm_type_t const* * vtable,
98107
#endif
99108
/* avoid empty block */ ;
100109
}
101-
if (*vtable)
110+
if (*vtable) {
102111
return APR_SUCCESS;
112+
}
113+
114+
if (result && !*result) {
115+
apu_err_t *err = apr_pcalloc(pool, sizeof(apu_err_t));
116+
if (err) {
117+
apr_status_t rv = APR_ENOTIMPL;
118+
err->msg = apr_psprintf(pool, "%pm", &rv);
119+
err->reason = apr_pstrdup(pool, type);
120+
*result = err;
121+
}
122+
}
123+
103124
return APR_ENOTIMPL;
104125

105126
#else /* APU_DSO_BUILD */
106127

107128
char modname[32];
108129
char symname[34];
130+
apr_dso_handle_t *dso;
109131
apr_dso_handle_sym_t symbol;
110132
apr_status_t rv;
111133
int usertype = 0;
112134

135+
if (result) {
136+
*result = NULL; /* until further notice */
137+
}
138+
139+
if (!type) {
140+
type = DBM_NAME;
141+
}
142+
113143
if (!strcasecmp(type, "default")) type = DBM_NAME;
114144
else if (!strcasecmp(type, "db")) type = "db";
115145
else if (*type && !strcasecmp(type + 1, "dbm")) {
@@ -173,7 +203,7 @@ static apr_status_t dbm_open_type(apr_dbm_type_t const* * vtable,
173203
#endif
174204
apr_snprintf(symname, sizeof(symname), "apr_dbm_type_%s", type);
175205

176-
rv = apu_dso_load(NULL, &symbol, modname, symname, pool);
206+
rv = apu_dso_load(&dso, &symbol, modname, symname, pool);
177207
if (rv == APR_SUCCESS || rv == APR_EINIT) { /* previously loaded?!? */
178208
*vtable = symbol;
179209
if (usertype)
@@ -185,6 +215,18 @@ static apr_status_t dbm_open_type(apr_dbm_type_t const* * vtable,
185215
*vtable = NULL;
186216

187217
apu_dso_mutex_unlock();
218+
219+
if (APR_SUCCESS != rv && result && !*result) {
220+
char *buffer = apr_pcalloc(pool, ERROR_SIZE);
221+
apu_err_t *err = apr_pcalloc(pool, sizeof(apu_err_t));
222+
if (err && buffer) {
223+
apr_dso_error(dso, buffer, ERROR_SIZE - 1);
224+
err->msg = buffer;
225+
err->reason = apr_pstrdup(pool, modname);
226+
*result = err;
227+
}
228+
}
229+
188230
return rv;
189231

190232
#endif /* APU_DSO_BUILD */
@@ -196,8 +238,8 @@ APU_DECLARE(apr_status_t) apr_dbm_open_ex(apr_dbm_t **pdb, const char *type,
196238
apr_fileperms_t perm,
197239
apr_pool_t *pool)
198240
{
199-
apr_dbm_type_t const* vtable = NULL;
200-
apr_status_t rv = dbm_open_type(&vtable, type, pool);
241+
apr_dbm_driver_t const* vtable = NULL;
242+
apr_status_t rv = apr_dbm_get_driver(&vtable, type, NULL, pool);
201243

202244
if (rv == APR_SUCCESS) {
203245
rv = (vtable->open)(pdb, pathname, mode, perm, pool);
@@ -212,6 +254,14 @@ APU_DECLARE(apr_status_t) apr_dbm_open(apr_dbm_t **pdb, const char *pathname,
212254
return apr_dbm_open_ex(pdb, DBM_NAME, pathname, mode, perm, pool);
213255
}
214256

257+
APU_DECLARE(apr_status_t) apr_dbm_open2(apr_dbm_t **pdb,
258+
const apr_dbm_driver_t *vtable,
259+
const char *pathname, apr_int32_t mode,
260+
apr_fileperms_t perm, apr_pool_t *pool)
261+
{
262+
return (vtable->open)(pdb, pathname, mode, perm, pool);
263+
}
264+
215265
APU_DECLARE(void) apr_dbm_close(apr_dbm_t *dbm)
216266
{
217267
(*dbm->type->close)(dbm);
@@ -275,8 +325,8 @@ APU_DECLARE(apr_status_t) apr_dbm_get_usednames_ex(apr_pool_t *p,
275325
const char **used1,
276326
const char **used2)
277327
{
278-
apr_dbm_type_t const* vtable;
279-
apr_status_t rv = dbm_open_type(&vtable, type, p);
328+
apr_dbm_driver_t const* vtable;
329+
apr_status_t rv = apr_dbm_get_driver(&vtable, type, NULL, p);
280330

281331
if (rv == APR_SUCCESS) {
282332
(vtable->getusednames)(p, pathname, used1, used2);

dbm/apr_dbm_berkeleydb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ static void vt_db_usednames(apr_pool_t *pool, const char *pathname,
386386
}
387387

388388

389-
APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_db = {
389+
APU_MODULE_DECLARE_DATA const apr_dbm_driver_t apr_dbm_type_db = {
390390
"db",
391391

392392
vt_db_open,

dbm/apr_dbm_gdbm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ static void vt_gdbm_usednames(apr_pool_t *pool, const char *pathname,
248248
*used2 = NULL;
249249
}
250250

251-
APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_gdbm = {
251+
APU_MODULE_DECLARE_DATA const apr_dbm_driver_t apr_dbm_type_gdbm = {
252252
"gdbm",
253253
vt_gdbm_open,
254254
vt_gdbm_close,

dbm/apr_dbm_ndbm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static void vt_ndbm_usednames(apr_pool_t *pool, const char *pathname,
221221
*used2 = NULL;
222222
}
223223

224-
APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_ndbm = {
224+
APU_MODULE_DECLARE_DATA const apr_dbm_driver_t apr_dbm_type_ndbm = {
225225
"ndbm",
226226
vt_ndbm_open,
227227
vt_ndbm_close,

dbm/apr_dbm_sdbm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static void vt_sdbm_usednames(apr_pool_t *pool, const char *pathname,
206206
*used2 = apr_pstrcat(pool, pathname, APR_SDBM_PAGFEXT, NULL);
207207
}
208208

209-
APU_MODULE_DECLARE_DATA const apr_dbm_type_t apr_dbm_type_sdbm = {
209+
APU_MODULE_DECLARE_DATA const apr_dbm_driver_t apr_dbm_type_sdbm = {
210210
"sdbm",
211211
vt_sdbm_open,
212212
vt_sdbm_close,

include/apr_dbm.h

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "apu.h"
2121
#include "apr.h"
2222
#include "apr_errno.h"
23+
#include "apu_errno.h"
2324
#include "apr_pools.h"
2425
#include "apr_file_info.h"
2526

@@ -36,6 +37,11 @@ extern "C" {
3637
* @ingroup APR_Util
3738
* @{
3839
*/
40+
/**
41+
* Structure representing a dbm driver.
42+
*/
43+
typedef struct apr_dbm_driver_t apr_dbm_driver_t;
44+
3945
/**
4046
* Structure for referencing a dbm
4147
*/
@@ -58,6 +64,25 @@ typedef struct
5864
#define APR_DBM_RWCREATE 3 /**< open for r/w, create if needed */
5965
#define APR_DBM_RWTRUNC 4 /**< open for r/w, truncating an existing
6066
DB if present */
67+
68+
/**
69+
* apr_dm_get_driver: get the driver struct for a name
70+
*
71+
* If the driver cannot be found, or cannot be opened, details of the
72+
* error are returned in apu_err_t.
73+
*
74+
* @param driver - pointer to driver struct.
75+
* @param name - driver name
76+
* @param result - result and error message on failure
77+
* @param pool - (process) pool to register cleanup
78+
* @return APR_SUCCESS for success
79+
* @return APR_ENOTIMPL for no driver (when DSO not enabled)
80+
* @return APR_EDSOOPEN if DSO driver file can't be opened
81+
* @return APR_ESYMNOTFOUND if the driver file doesn't contain a driver
82+
*/
83+
APR_DECLARE(apr_status_t) apr_dbm_get_driver(const apr_dbm_driver_t **driver,
84+
const char *name, const apu_err_t **result, apr_pool_t *pool);
85+
6186
/**
6287
* Open a dbm file by file name and type of DBM
6388
* @param dbm The newly opened database
@@ -91,6 +116,27 @@ APU_DECLARE(apr_status_t) apr_dbm_open_ex(apr_dbm_t **dbm, const char* type,
91116
apr_int32_t mode, apr_fileperms_t perm,
92117
apr_pool_t *cntxt);
93118

119+
/**
120+
* Open a dbm file by file name and driver
121+
* @param pdb The newly opened database
122+
* @param driver The dbm driver to use
123+
* @param name The dbm file name to open
124+
* @param mode The flag value
125+
* <PRE>
126+
* APR_DBM_READONLY open for read-only access
127+
* APR_DBM_READWRITE open for read-write access
128+
* APR_DBM_RWCREATE open for r/w, create if needed
129+
* APR_DBM_RWTRUNC open for r/w, truncate if already there
130+
* </PRE>
131+
* @param perm Permissions to apply to if created
132+
* @param pool The pool to use when creating the dbm
133+
* @remark The dbm name may not be a true file name, as many dbm packages
134+
* append suffixes for separate data and index files.
135+
*/
136+
APR_DECLARE(apr_status_t) apr_dbm_open2(apr_dbm_t **pdb,
137+
const apr_dbm_driver_t *driver,
138+
const char *name, apr_int32_t mode,
139+
apr_fileperms_t perm, apr_pool_t *pool);
94140

95141
/**
96142
* Open a dbm file by file name
@@ -106,7 +152,7 @@ APU_DECLARE(apr_status_t) apr_dbm_open_ex(apr_dbm_t **dbm, const char* type,
106152
* @param perm Permissions to apply to if created
107153
* @param cntxt The pool to use when creating the dbm
108154
* @remark The dbm name may not be a true file name, as many dbm packages
109-
* append suffixes for seperate data and index files.
155+
* append suffixes for separate data and index files.
110156
*/
111157
APU_DECLARE(apr_status_t) apr_dbm_open(apr_dbm_t **dbm, const char *name,
112158
apr_int32_t mode, apr_fileperms_t perm,

include/private/apr_dbm_private.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ APU_DECLARE(int) apr_posix_perms2mode(apr_fileperms_t perm);
4444
/**
4545
* Structure to describe the operations of the DBM
4646
*/
47-
typedef struct {
47+
struct apr_dbm_driver_t {
4848
/** The name of the DBM Type */
4949
const char *name;
5050

@@ -84,7 +84,7 @@ typedef struct {
8484
const char **used1,
8585
const char **used2);
8686

87-
} apr_dbm_type_t;
87+
};
8888

8989

9090
/**
@@ -104,15 +104,15 @@ struct apr_dbm_t
104104
const char *errmsg;
105105

106106
/** the type of DBM */
107-
const apr_dbm_type_t *type;
107+
const apr_dbm_driver_t *type;
108108
};
109109

110110

111111
/* Declare all of the DBM provider tables */
112-
APU_MODULE_DECLARE_DATA extern const apr_dbm_type_t apr_dbm_type_sdbm;
113-
APU_MODULE_DECLARE_DATA extern const apr_dbm_type_t apr_dbm_type_gdbm;
114-
APU_MODULE_DECLARE_DATA extern const apr_dbm_type_t apr_dbm_type_ndbm;
115-
APU_MODULE_DECLARE_DATA extern const apr_dbm_type_t apr_dbm_type_db;
112+
APU_MODULE_DECLARE_DATA extern const apr_dbm_driver_t apr_dbm_type_sdbm;
113+
APU_MODULE_DECLARE_DATA extern const apr_dbm_driver_t apr_dbm_type_gdbm;
114+
APU_MODULE_DECLARE_DATA extern const apr_dbm_driver_t apr_dbm_type_ndbm;
115+
APU_MODULE_DECLARE_DATA extern const apr_dbm_driver_t apr_dbm_type_db;
116116

117117
#ifdef __cplusplus
118118
}

0 commit comments

Comments
 (0)