dbwrap: don't panic in db_open_trans() if called with TDB_CLEAR_IF_FIRST.
[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 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
30 static 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  */
48 struct 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()) {
64                 const char *partname;
65
66                 if (!socket_exist(sockname)) {
67                         DEBUG(1, ("ctdb socket does not exist - is ctdb not "
68                                   "running?\n"));
69                         return NULL;
70                 }
71
72                 /* ctdb only wants the file part of the name */
73                 partname = strrchr(name, '/');
74                 if (partname) {
75                         partname++;
76                 } else {
77                         partname = name;
78                 }
79                 /* allow ctdb for individual databases to be disabled */
80                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
81                         result = db_open_ctdb(mem_ctx, partname, hash_size,
82                                               tdb_flags, open_flags, mode);
83                         if (result == NULL) {
84                                 DEBUG(0,("failed to attach to ctdb %s\n",
85                                          partname));
86                                 return NULL;
87                         }
88                 }
89         }
90
91 #endif
92
93         if (result == NULL) {
94                 result = db_open_tdb(mem_ctx, name, hash_size,
95                                      tdb_flags, open_flags, mode);
96         }
97
98         if ((result != NULL) && (result->fetch == NULL)) {
99                 result->fetch = dbwrap_fallback_fetch;
100         }
101
102         return result;
103 }
104
105 /**
106  * If you use this you can only modify with a transaction
107  */
108 struct db_context *db_open_trans(TALLOC_CTX *mem_ctx,
109                                  const char *name,
110                                  int hash_size, int tdb_flags,
111                                  int open_flags, mode_t mode)
112 {
113         bool use_tdb2 = lp_parm_bool(-1, "dbwrap", "use_tdb2", false);
114 #ifdef CLUSTER_SUPPORT
115         const char *sockname = lp_ctdbd_socket();
116 #endif
117
118         if (tdb_flags & TDB_CLEAR_IF_FIRST) {
119                 DEBUG(0,("db_open_trans: called with TDB_CLEAR_IF_FIRST: %s\n",
120                          name));
121                 return NULL;
122         }
123
124 #ifdef CLUSTER_SUPPORT
125         if(!sockname || !*sockname) {
126                 sockname = CTDB_PATH;
127         }
128
129         if (lp_clustering()) {
130                 const char *partname;
131
132                 if (!socket_exist(sockname)) {
133                         DEBUG(1, ("ctdb socket does not exist - is ctdb not "
134                                   "running?\n"));
135                         return NULL;
136                 }
137
138                 /* ctdb only wants the file part of the name */
139                 partname = strrchr(name, '/');
140                 if (partname) {
141                         partname++;
142                 } else {
143                         partname = name;
144                 }
145                 /* allow ctdb for individual databases to be disabled */
146                 if (lp_parm_bool(-1, "ctdb", partname, true)) {
147                         struct db_context *result = NULL;
148                         result = db_open_ctdb(mem_ctx, partname, hash_size,
149                                               tdb_flags, open_flags, mode);
150                         if (result == NULL) {
151                                 DEBUG(0,("failed to attach to ctdb %s\n",
152                                          partname));
153                         }
154                         return result;
155                 }
156         }
157 #endif
158
159         if (use_tdb2) {
160                 const char *partname;
161                 /* tdb2 only wants the file part of the name */
162                 partname = strrchr(name, '/');
163                 if (partname) {
164                         partname++;
165                 } else {
166                         partname = name;
167                 }
168                 /* allow ctdb for individual databases to be disabled */
169                 if (lp_parm_bool(-1, "tdb2", partname, true)) {
170                         return db_open_tdb2(mem_ctx, partname, hash_size,
171                                             tdb_flags, open_flags, mode);
172                 }
173         }
174
175         return db_open_tdb(mem_ctx, name, hash_size,
176                            tdb_flags, open_flags, mode);
177 }
178
179 NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
180 {
181         struct db_record *rec;
182         NTSTATUS status;
183
184         rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
185         if (rec == NULL) {
186                 return NT_STATUS_NO_MEMORY;
187         }
188         status = rec->delete_rec(rec);
189         TALLOC_FREE(rec);
190         return status;
191 }
192
193 NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
194                                TDB_DATA data, int flags)
195 {
196         struct db_record *rec;
197         NTSTATUS status;
198
199         rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
200         if (rec == NULL) {
201                 return NT_STATUS_NO_MEMORY;
202         }
203
204         status = rec->store(rec, data, flags);
205         TALLOC_FREE(rec);
206         return status;
207 }
208
209 TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
210                                const char *key)
211 {
212         TDB_DATA result;
213
214         if (db->fetch(db, mem_ctx, string_term_tdb_data(key), &result) == -1) {
215                 return make_tdb_data(NULL, 0);
216         }
217
218         return result;
219 }