c06cd4bb164587b19eafe0eec87acd6b1cf07d07
[samba.git] / source3 / lib / dbwrap.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Database interface wrapper
4    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
5
6    Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
7    
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 2 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26  * Fall back using fetch_locked if no genuine fetch operation is provided
27  */
28
29 static int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
30                                  TDB_DATA key, TDB_DATA *data)
31 {
32         struct db_record *rec;
33
34         if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
35                 return -1;
36         }
37
38         data->dsize = rec->value.dsize;
39         data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
40         TALLOC_FREE(rec);
41         return 0;
42 }
43
44 struct db_context *db_open(TALLOC_CTX *mem_ctx,
45                            const char *name,
46                            int hash_size, int tdb_flags,
47                            int open_flags, mode_t mode)
48 {
49         struct db_context *result = NULL;
50
51 #ifdef CLUSTER_SUPPORT
52
53         if (lp_clustering()) {
54                 const char *partname;
55                 /* ctdb only wants the file part of the name */
56                 partname = strrchr(name, '/');
57                 if (partname) {
58                         partname++;
59                 } else {
60                         partname = name;
61                 }
62                 /* allow ctdb for individual databases to be disabled */
63                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
64                         result = db_open_ctdb(mem_ctx, partname, hash_size,
65                                               tdb_flags, open_flags, mode);
66                         if (result == NULL) {
67                                 DEBUG(0,("failed to attach to ctdb %s\n",
68                                          partname));
69                                 smb_panic("failed to attach to a ctdb "
70                                           "database");
71                         }
72                 }
73         }
74
75 #endif
76
77         if (result == NULL) {
78                 result = db_open_tdb(mem_ctx, name, hash_size,
79                                      tdb_flags, open_flags, mode);
80         }
81
82         if ((result != NULL) && (result->fetch == NULL)) {
83                 result->fetch = dbwrap_fallback_fetch;
84         }
85
86         return result;
87 }