r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / lib / dbwrap.c
blob3abd0e2ba56b45dcc4eb661b53489525ad44aba6
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
6 Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
25 * Fall back using fetch_locked if no genuine fetch operation is provided
28 static int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
29 TDB_DATA key, TDB_DATA *data)
31 struct db_record *rec;
33 if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
34 return -1;
37 data->dsize = rec->value.dsize;
38 data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
39 TALLOC_FREE(rec);
40 return 0;
43 struct db_context *db_open(TALLOC_CTX *mem_ctx,
44 const char *name,
45 int hash_size, int tdb_flags,
46 int open_flags, mode_t mode)
48 struct db_context *result = NULL;
50 #ifdef CLUSTER_SUPPORT
52 if (lp_clustering()) {
53 const char *partname;
54 /* ctdb only wants the file part of the name */
55 partname = strrchr(name, '/');
56 if (partname) {
57 partname++;
58 } else {
59 partname = name;
61 /* allow ctdb for individual databases to be disabled */
62 if (lp_parm_bool(-1, "ctdb", partname, True)) {
63 result = db_open_ctdb(mem_ctx, partname, hash_size,
64 tdb_flags, open_flags, mode);
65 if (result == NULL) {
66 DEBUG(0,("failed to attach to ctdb %s\n",
67 partname));
68 smb_panic("failed to attach to a ctdb "
69 "database");
74 #endif
76 if (result == NULL) {
77 result = db_open_tdb(mem_ctx, name, hash_size,
78 tdb_flags, open_flags, mode);
81 if ((result != NULL) && (result->fetch == NULL)) {
82 result->fetch = dbwrap_fallback_fetch;
85 return result;