Merge branch 'master' of git://git.samba.org/samba into teventfix
[amitay/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  * Fall back using fetch if no genuine parse operation is provided
47  */
48
49 static int dbwrap_fallback_parse_record(struct db_context *db, TDB_DATA key,
50                                         int (*parser)(TDB_DATA key,
51                                                       TDB_DATA data,
52                                                       void *private_data),
53                                         void *private_data)
54 {
55         TDB_DATA data;
56         int res;
57
58         res = db->fetch(db, talloc_tos(), key, &data);
59         if (res != 0) {
60                 return res;
61         }
62
63         res = parser(key, data, private_data);
64         TALLOC_FREE(data.dptr);
65         return res;
66 }
67
68 /**
69  * open a database
70  */
71 struct db_context *db_open(TALLOC_CTX *mem_ctx,
72                            const char *name,
73                            int hash_size, int tdb_flags,
74                            int open_flags, mode_t mode)
75 {
76         struct db_context *result = NULL;
77 #ifdef CLUSTER_SUPPORT
78         const char *sockname = lp_ctdbd_socket();
79 #endif
80
81 #ifdef CLUSTER_SUPPORT
82         if(!sockname || !*sockname) {
83                 sockname = CTDB_PATH;
84         }
85
86         if (lp_clustering()) {
87                 const char *partname;
88
89                 if (!socket_exist(sockname)) {
90                         DEBUG(1, ("ctdb socket does not exist - is ctdb not "
91                                   "running?\n"));
92                         return NULL;
93                 }
94
95                 /* ctdb only wants the file part of the name */
96                 partname = strrchr(name, '/');
97                 if (partname) {
98                         partname++;
99                 } else {
100                         partname = name;
101                 }
102                 /* allow ctdb for individual databases to be disabled */
103                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
104                         result = db_open_ctdb(mem_ctx, partname, hash_size,
105                                               tdb_flags, open_flags, mode);
106                         if (result == NULL) {
107                                 DEBUG(0,("failed to attach to ctdb %s\n",
108                                          partname));
109                                 if (errno == 0) {
110                                         errno = EIO;
111                                 }
112                                 return NULL;
113                         }
114                 }
115         }
116
117 #endif
118
119         if (result == NULL) {
120                 result = db_open_tdb(mem_ctx, name, hash_size,
121                                      tdb_flags, open_flags, mode);
122         }
123
124         if ((result != NULL) && (result->fetch == NULL)) {
125                 result->fetch = dbwrap_fallback_fetch;
126         }
127         if ((result != NULL) && (result->parse_record == NULL)) {
128                 result->parse_record = dbwrap_fallback_parse_record;
129         }
130
131         return result;
132 }
133
134 NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
135 {
136         struct db_record *rec;
137         NTSTATUS status;
138
139         rec = db->fetch_locked(db, talloc_tos(), key);
140         if (rec == NULL) {
141                 return NT_STATUS_NO_MEMORY;
142         }
143         status = rec->delete_rec(rec);
144         TALLOC_FREE(rec);
145         return status;
146 }
147
148 NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
149                       TDB_DATA data, int flags)
150 {
151         struct db_record *rec;
152         NTSTATUS status;
153
154         rec = db->fetch_locked(db, talloc_tos(), key);
155         if (rec == NULL) {
156                 return NT_STATUS_NO_MEMORY;
157         }
158
159         status = rec->store(rec, data, flags);
160         TALLOC_FREE(rec);
161         return status;
162 }
163
164 TDB_DATA dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
165                       TDB_DATA key)
166 {
167         TDB_DATA result;
168
169         if (db->fetch(db, mem_ctx, key, &result) == -1) {
170                 return make_tdb_data(NULL, 0);
171         }
172
173         return result;
174 }
175
176 NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
177 {
178         return dbwrap_delete(db, string_term_tdb_data(key));
179 }
180
181 NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
182                                TDB_DATA data, int flags)
183 {
184         return dbwrap_store(db, string_term_tdb_data(key), data, flags);
185 }
186
187 TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
188                                const char *key)
189 {
190         return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key));
191 }
192