source: branches/samba-3.2.x/source/lib/dbwrap.c@ 232

Last change on this file since 232 was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

File size: 5.1 KB
Line 
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 3 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, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#ifdef CLUSTER_SUPPORT
24#include "ctdb_private.h"
25#endif
26/*
27 * Fall back using fetch_locked if no genuine fetch operation is provided
28 */
29
30static int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
31 TDB_DATA key, TDB_DATA *data)
32{
33 struct db_record *rec;
34
35 if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
36 return -1;
37 }
38
39 data->dsize = rec->value.dsize;
40 data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
41 TALLOC_FREE(rec);
42 return 0;
43}
44
45/**
46 * If you need transaction support use db_open_trans()
47 */
48struct db_context *db_open(TALLOC_CTX *mem_ctx,
49 const char *name,
50 int hash_size, int tdb_flags,
51 int open_flags, mode_t mode)
52{
53 struct db_context *result = NULL;
54#ifdef CLUSTER_SUPPORT
55 const char *sockname = lp_ctdbd_socket();
56#endif
57
58#ifdef CLUSTER_SUPPORT
59 if(!sockname || !*sockname) {
60 sockname = CTDB_PATH;
61 }
62
63 if (lp_clustering() && socket_exist(sockname)) {
64 const char *partname;
65 /* ctdb only wants the file part of the name */
66 partname = strrchr(name, '/');
67 if (partname) {
68 partname++;
69 } else {
70 partname = name;
71 }
72 /* allow ctdb for individual databases to be disabled */
73 if (lp_parm_bool(-1, "ctdb", partname, True)) {
74 result = db_open_ctdb(mem_ctx, partname, hash_size,
75 tdb_flags, open_flags, mode);
76 if (result == NULL) {
77 DEBUG(0,("failed to attach to ctdb %s\n",
78 partname));
79 smb_panic("failed to attach to a ctdb "
80 "database");
81 }
82 }
83 }
84
85#endif
86
87 if (result == NULL) {
88 result = db_open_tdb(mem_ctx, name, hash_size,
89 tdb_flags, open_flags, mode);
90 }
91
92 if ((result != NULL) && (result->fetch == NULL)) {
93 result->fetch = dbwrap_fallback_fetch;
94 }
95
96 return result;
97}
98
99/**
100 * If you use this you can only modify with a transaction
101 */
102struct db_context *db_open_trans(TALLOC_CTX *mem_ctx,
103 const char *name,
104 int hash_size, int tdb_flags,
105 int open_flags, mode_t mode)
106{
107 bool use_tdb2 = lp_parm_bool(-1, "dbwrap", "use_tdb2", false);
108#ifdef CLUSTER_SUPPORT
109 const char *sockname = lp_ctdbd_socket();
110#endif
111
112 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
113 DEBUG(0,("db_open_trans: called with TDB_CLEAR_IF_FIRST: %s\n",
114 name));
115 smb_panic("db_open_trans: called with TDB_CLEAR_IF_FIRST");
116 }
117
118#ifdef CLUSTER_SUPPORT
119 if(!sockname || !*sockname) {
120 sockname = CTDB_PATH;
121 }
122
123 if (lp_clustering() && socket_exist(sockname)) {
124 const char *partname;
125 /* ctdb only wants the file part of the name */
126 partname = strrchr(name, '/');
127 if (partname) {
128 partname++;
129 } else {
130 partname = name;
131 }
132 /* allow ctdb for individual databases to be disabled */
133 if (lp_parm_bool(-1, "ctdb", partname, true)) {
134 struct db_context *result = NULL;
135 result = db_open_ctdb(mem_ctx, partname, hash_size,
136 tdb_flags, open_flags, mode);
137 if (result == NULL) {
138 DEBUG(0,("failed to attach to ctdb %s\n",
139 partname));
140 smb_panic("failed to attach to a ctdb "
141 "database");
142 }
143 return result;
144 }
145 }
146#endif
147
148 if (use_tdb2) {
149 const char *partname;
150 /* tdb2 only wants the file part of the name */
151 partname = strrchr(name, '/');
152 if (partname) {
153 partname++;
154 } else {
155 partname = name;
156 }
157 /* allow ctdb for individual databases to be disabled */
158 if (lp_parm_bool(-1, "tdb2", partname, true)) {
159 return db_open_tdb2(mem_ctx, partname, hash_size,
160 tdb_flags, open_flags, mode);
161 }
162 }
163
164 return db_open_tdb(mem_ctx, name, hash_size,
165 tdb_flags, open_flags, mode);
166}
167
168NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
169{
170 struct db_record *rec;
171 NTSTATUS status;
172
173 rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
174 if (rec == NULL) {
175 return NT_STATUS_NO_MEMORY;
176 }
177 status = rec->delete_rec(rec);
178 TALLOC_FREE(rec);
179 return status;
180}
181
182NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
183 TDB_DATA data, int flags)
184{
185 struct db_record *rec;
186 NTSTATUS status;
187
188 rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
189 if (rec == NULL) {
190 return NT_STATUS_NO_MEMORY;
191 }
192
193 status = rec->store(rec, data, flags);
194 TALLOC_FREE(rec);
195 return status;
196}
197
198TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
199 const char *key)
200{
201 TDB_DATA result;
202
203 if (db->fetch(db, mem_ctx, string_term_tdb_data(key), &result) == -1) {
204 return make_tdb_data(NULL, 0);
205 }
206
207 return result;
208}
Note: See TracBrowser for help on using the repository browser.